<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
workspace.hpp
Go to the documentation of this file.
1
3//
4// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
5//
6// This file is part of <T>LAPACK.
7// <T>LAPACK is free software: you can redistribute it and/or modify it under
8// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
9
10#ifndef TLAPACK_WORKSPACE_HH
11#define TLAPACK_WORKSPACE_HH
12
13namespace tlapack {
14
16struct WorkInfo {
17 size_t m = 0;
18 size_t n = 1;
19 bool isContiguous = false;
20
22 constexpr WorkInfo(size_t m, size_t n) noexcept
23 : m(m), n(n), isVector(false)
24 {}
25 constexpr WorkInfo(size_t s = 0) noexcept : m(s), isVector(true) {}
26
28 constexpr size_t size() const noexcept { return m * n; }
29
39 constexpr void minMax(const WorkInfo& workinfo) noexcept
40 {
41 const size_t& m1 = workinfo.m;
42 const size_t& n1 = workinfo.n;
43 const size_t s1 = workinfo.size();
44 const size_t s = size();
45
46 if (s1 == 0) {
47 // Do nothing
48 }
49 else if (s == 0) {
50 // Copy workinfo
51 m = m1;
52 n = n1;
53 isContiguous = workinfo.isContiguous;
54 isVector = workinfo.isVector;
55 }
56 else {
57 if (isContiguous || workinfo.isContiguous) {
58 // If one of the objects is contiguous, then the result is
59 // contiguous
60 m = std::max(s, s1);
61 n = 1;
62 isContiguous = true;
63 }
64 else if ((m >= m1 && n >= n1) ||
65 (workinfo.isVector && (m >= n1 && n >= m1))) {
66 // Current shape cover workinfo's shape, nothing to be done.
67 // If workinfo represents a vector, then we also check if the
68 // vector fits transposed. In such a case, nothing to be done.
69 }
70 else if ((m1 >= m && n1 >= n) ||
71 (isVector && (m1 >= n && n1 >= m))) {
72 // Same as above, but for the opposite case. THerefore, use the
73 // sizes from workinfo.
74 m = m1;
75 n = n1;
76 }
77 else {
78 // Workspaces are incompatible, use simple solution: contiguous
79 // space in memory
80 m = std::max(s, s1);
81 n = 1;
82 isContiguous = true;
83 }
84
85 // Update isVector
86 isVector = isVector && workinfo.isVector;
87 }
88 }
89
99 constexpr WorkInfo& operator+=(const WorkInfo& workinfo) noexcept
100 {
101 const size_t& m1 = workinfo.m;
102 const size_t& n1 = workinfo.n;
103 const size_t s1 = workinfo.size();
104 const size_t s = size();
105
106 if (s1 == 0) {
107 // Do nothing
108 }
109 else if (s == 0) {
110 // Copy workinfo
111 m = m1;
112 n = n1;
113 isContiguous = workinfo.isContiguous;
114 isVector = workinfo.isVector;
115 }
116 else {
117 if (isContiguous || workinfo.isContiguous) {
118 // One of the objects is contiguous, then the result is
119 // contiguous
120 m = s + s1;
121 n = 1;
122 isContiguous = true;
123 }
124 else if (n == n1) {
125 // Second dimension matches, update first dimension
126 m += m1;
127 }
128 else if (m == m1) {
129 // First dimension matches, update second dimension
130 n += n1;
131 }
132 else if (workinfo.isVector && (n == m1)) {
133 // Second dimension matches the first dimension of workinfo and
134 // workinfo is a vector. Therefore, the result is *this with
135 // one additional row.
136 m += 1;
137 }
138 else if (isVector && (m == n1)) {
139 // First dimension matches the second dimension of workinfo and
140 // *this is a vector. Therefore, the result is workinfo with
141 // one additional row.
142 m = m1 + 1;
143 n = n1;
144 }
145 else {
146 // Sizes do not match. Simple solution: contiguous space in
147 // memory
148 m = s + s1;
149 n = 1;
150 isContiguous = true;
151 }
152
153 // Update isVector
154 isVector = isVector && workinfo.isVector;
155 }
156
157 return *this;
158 }
159
160 constexpr WorkInfo transpose() const noexcept
161 {
162 if (size() == 0 || isVector)
163 return *this;
164 else
165 return WorkInfo(n, m);
166 }
167
168 protected:
169 bool isVector;
170};
171
172} // namespace tlapack
173
174#endif // TLAPACK_WORKSPACE_HH
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
Output information in the workspace query.
Definition workspace.hpp:16
bool isContiguous
True if the Workspace is contiguous.
Definition workspace.hpp:19
size_t n
Number of columns needed in the Workspace.
Definition workspace.hpp:18
constexpr void minMax(const WorkInfo &workinfo) noexcept
Set the current object to a state that fit its current sizes and the sizes of workinfo.
Definition workspace.hpp:39
constexpr WorkInfo(size_t m, size_t n) noexcept
Constructor using sizes.
Definition workspace.hpp:22
constexpr WorkInfo & operator+=(const WorkInfo &workinfo) noexcept
Sum two object by matching sizes.
Definition workspace.hpp:99
size_t m
Number of rows needed in the Workspace.
Definition workspace.hpp:17
bool isVector
True if the Workspace is a vector at compile time.
Definition workspace.hpp:169
constexpr size_t size() const noexcept
Size needed in the Workspace.
Definition workspace.hpp:28