FlexBox - A Flexible Primal-Dual ToolBox
flexIdentityOperator.h
1 #ifndef flexIdentityOperator_H
2 #define flexIdentityOperator_H
3 
4 
5 #include "vector"
6 #include "tools.h"
7 #include "flexLinearOperator.h"
8 
10 template<typename T>
12 {
13 
14 #ifdef __CUDACC__
15  typedef thrust::device_vector<T> Tdata;
16 #else
17  typedef std::vector<T> Tdata;
18 #endif
19 
20 public:
21 
23 
28  flexIdentityOperator(int aNumRows, int aNumCols, bool aMinus) : flexLinearOperator<T>(aNumRows, aNumCols, identityOp, aMinus) {}
29 
31  {
33 
34  return A;
35  }
36 
37  //apply linear operator to vector
38  void times(bool transposed, const Tdata &input, Tdata &output)
39  {
40  int numRows = this->getNumRows();
41  int numCols = this->getNumCols();
42 
43  if (this->isMinus)
44  {
45  if(transposed)
46  {
47  #pragma omp parallel for
48  for (int i = 0; i < numCols; ++i)
49  {
50  if(numCols > numRows && i >= numRows)
51  output[i] = 0;
52  else
53  output[i] = -input[i];
54  }
55  }
56  else
57  {
58  #pragma omp parallel for
59  for (int i = 0; i < numRows; ++i)
60  {
61  if(numRows > numCols && i >= numCols)
62  output[i] = 0;
63  else
64  output[i] = -input[i];
65  }
66  }
67  }
68  else
69  {
70  if(transposed)
71  {
72  #pragma omp parallel for
73  for (int i = 0; i < numCols; ++i)
74  {
75  if(numCols > numRows && i >= numRows)
76  output[i] = 0;
77  else
78  output[i] = input[i];
79  }
80  }
81  else
82  {
83  #pragma omp parallel for
84  for (int i = 0; i < numRows; ++i)
85  {
86  if(numRows > numCols && i >= numCols)
87  output[i] = 0;
88  else
89  output[i] = input[i];
90  }
91  }
92  }
93  }
94 
95  void timesPlus(bool transposed, const Tdata &input, Tdata &output)
96  {
97  if (this->isMinus)
98  {
99  doTimesMinus(transposed, input, output);
100  }
101  else
102  {
103  doTimesPlus(transposed, input, output);
104  }
105  }
106 
107  void timesMinus(bool transposed, const Tdata &input, Tdata &output)
108  {
109  if (this->isMinus)
110  {
111  doTimesPlus(transposed, input, output);
112  }
113  else
114  {
115  doTimesMinus(transposed, input, output);
116  }
117  }
118 
119  T getMaxRowSumAbs(bool transposed)
120  {
121  return static_cast<T>(1);
122  }
123 
124  std::vector<T> getAbsRowSum(bool transposed)
125  {
126  std::vector<T> result;
127 
128  if(transposed)
129  result = std::vector<T>(this->getNumCols(), (T)1);
130  else
131  result = std::vector<T>(this->getNumRows(), (T)1);
132 
133  return result;
134  }
135 
136  #ifdef __CUDACC__
137  thrust::device_vector<T> getAbsRowSumCUDA(bool transposed)
138  {
139  thrust::device_vector<T> result;
140 
141  if(transposed)
142  result = thrust::device_vector<T>(this->getNumCols(), (T)1);
143  else
144  result = thrust::device_vector<T>(this->getNumRows(), (T)1);
145 
146  return result;
147  }
148  #endif
149 
150 private:
151  void doTimesPlus(bool transposed, const Tdata &input, Tdata &output)
152  {
153  int numRows = this->getNumRows();
154  int numCols = this->getNumCols();
155 
156  if(transposed)
157  {
158  #ifdef __CUDACC__
159  if(numCols <= numRows)
160  thrust::transform(output.begin(), output.end(), input.begin(), output.begin(), thrust::plus<T>());
161  else
162  {
163  thrust::transform(output.begin(), output.begin() + numRows, input.begin(), output.begin(), thrust::plus<T>());
164  }
165 
166  #else
167  #pragma omp parallel for
168  for (int i = 0; i < numCols; ++i)
169  {
170  if(numCols <= numRows || i < numRows)
171  output[i] += input[i];
172  }
173  #endif
174  }
175  else
176  {
177  #ifdef __CUDACC__
178  if(numRows <= numCols)
179  thrust::transform(output.begin(), output.end(), input.begin(), output.begin(), thrust::plus<T>());
180  else
181  {
182  thrust::transform(output.begin(), output.begin() + numCols, input.begin(), output.begin(), thrust::plus<T>());
183  }
184  #else
185  #pragma omp parallel for
186  for (int i = 0; i < numRows; ++i)
187  {
188  if(numRows <= numCols || i < numCols)
189  output[i] += input[i];
190  }
191  #endif
192  }
193  }
194 
195  void doTimesMinus(bool transposed, const Tdata &input, Tdata &output)
196  {
197  int numRows = this->getNumRows();
198  int numCols = this->getNumCols();
199 
200  if(transposed)
201  {
202  #ifdef __CUDACC__
203  if(numCols <= numRows)
204  thrust::transform(output.begin(), output.end(), input.begin(), output.begin(), thrust::minus<T>());
205  else
206  {
207  thrust::transform(output.begin(), output.begin() + numRows, input.begin(), output.begin(), thrust::minus<T>());
208  }
209 
210  #else
211  #pragma omp parallel for
212  for (int i = 0; i < numCols; ++i)
213  {
214  if(numCols <= numRows || i < numRows)
215  output[i] -= input[i];
216  }
217  #endif
218  }
219  else
220  {
221  #ifdef __CUDACC__
222  if(numRows <= numCols)
223  thrust::transform(output.begin(), output.end(), input.begin(), output.begin(), thrust::minus<T>());
224  else
225  {
226  thrust::transform(output.begin(), output.begin() + numCols, input.begin(), output.begin(), thrust::minus<T>());
227  }
228  #else
229  #pragma omp parallel for
230  for (int i = 0; i < numRows; ++i)
231  {
232  if(numRows <= numCols || i < numCols)
233  output[i] -= input[i];
234  }
235  #endif
236  }
237  }
238 };
239 
240 #endif
int getNumRows() const
returns number of rows of the linear operator
Definition: flexLinearOperator.h:57
thrust::device_vector< T > getAbsRowSumCUDA(bool transposed)
same function as getAbsRowSum() but implemented in CUDA
Definition: flexIdentityOperator.h:137
void times(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector
Definition: flexIdentityOperator.h:38
bool isMinus
determines if operator is negated
Definition: flexLinearOperator.h:25
void timesPlus(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector and adds its result to y
Definition: flexIdentityOperator.h:95
represents an identiy operator
Definition: flexIdentityOperator.h:11
int getNumCols() const
returns number of columns of the linear operator
Definition: flexLinearOperator.h:48
void timesMinus(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector and substracts its result from y
Definition: flexIdentityOperator.h:107
flexIdentityOperator(int aNumRows, int aNumCols, bool aMinus)
initializes the identiy operator
Definition: flexIdentityOperator.h:28
std::vector< T > getAbsRowSum(bool transposed)
returns a vector of sum of absolute values per row used for preconditioning
Definition: flexIdentityOperator.h:124
T getMaxRowSumAbs(bool transposed)
returns the maximum sum of absolute values per row used for preconditioning
Definition: flexIdentityOperator.h:119
abstract base class for linear operators
Definition: flexLinearOperator.h:12
flexIdentityOperator< T > * copy()
copies the linear operator
Definition: flexIdentityOperator.h:30