FlexBox - A Flexible Primal-Dual ToolBox
flexFullMatrix.h
1 #ifndef flexFullMatrix_H
2 #define flexFullMatrix_H
3 
4 #include "flexLinearOperator.h"
5 
6 #include <vector>
7 
9 template<typename T>
11 {
12 
13 #ifdef __CUDACC__
14  typedef thrust::device_vector<T> Tdata;
15 #else
16  typedef std::vector<T> Tdata;
17 #endif
18 
19 private:
20  Tdata valueList;
21 
22 public:
24  flexFullMatrix() : valueList(), flexLinearOperator<T>(0, 0, matrixOp, false) {};
25 
27 
32  flexFullMatrix(int aNumRows, int aNumCols, bool aMinus) : valueList(aNumRows*aNumCols, 0), flexLinearOperator<T>(aNumRows, aNumCols, matrixOp, aMinus){};
33 
35  {
36  flexFullMatrix<T>* A = new flexFullMatrix<T>(this->getNumRows(), this->getNumCols(), this->isMinus);
37 
38  A->valueList = valueList;
39 
40  return A;
41  }
42 
43  void times(bool transposed, const Tdata &input, Tdata &output)
44  {
45 
46  }
47 
48  void timesPlus(bool transposed, const Tdata &input, Tdata &output)
49  {
50  if (this->isMinus)
51  {
52  doTimesCPU(transposed, input, output,MINUS);
53  }
54  else
55  {
56  doTimesCPU(transposed, input, output,PLUS);
57  }
58  }
59 
60  void timesMinus(bool transposed, const Tdata &input, Tdata &output)
61  {
62  if (this->isMinus)
63  {
64  doTimesCPU(transposed, input, output,PLUS);
65  }
66  else
67  {
68  doTimesCPU(transposed, input, output,MINUS);
69  }
70  }
71 
72  //inserts new matrix element val at position [i][j]
73  void insertElement(int i, int j, T val)
74  {
75  this->valueList[index2DtoLinear(i,j)] = val;
76  }
77 
78  void insertElement(int i, T val)
79  {
80  this->valueList[i] = val;
81  }
82 
83  int index2DtoLinear(int i, int j)
84  {
85  return i + j*this->getNumRows();
86  }
87 
88  T getMaxRowSumAbs(bool transposed)
89  {
90  std::vector<T> rowSum = this->getAbsRowSum(transposed);
91 
92  return *std::max_element(rowSum.begin(), rowSum.end());
93  }
94 
95 
96  std::vector<T> getAbsRowSum(bool transposed)
97  {
98  if (transposed)
99  {
100  std::vector<T> result(this->getNumCols(), (T)0);
101 
102  for (int i = 0; i < this->getNumRows(); ++i)
103  {
104  for (int j = 0; j < this->getNumCols(); ++j)
105  {
106  result[j] += std::abs(valueList[index2DtoLinear(i,j)]);
107  }
108  }
109 
110  /*for (int i = 0; i < this->getNumCols(); ++i)
111  {
112  printf("T %f\n", result[i]);
113  }*/
114 
115  return result;
116  }
117  else
118  {
119  std::vector<T> result(this->getNumRows(),(T)0);
120 
121  for (int i = 0; i < this->getNumRows(); ++i)
122  {
123  for (int j = 0; j < this->getNumCols(); ++j)
124  {
125  result[i] += std::abs(valueList[index2DtoLinear(i, j)]);
126  }
127  }
128 
129  /*for (int i = 0; i < this->getNumRows(); ++i)
130  {
131  printf(" %f\n", result[i]);
132  }*/
133 
134  return result;
135  }
136  }
137 
139 
142  void printRow(int i)
143  {
144  for (int j = 0; j < this->getNumCols(); ++j)
145  {
146  printf("(%d,%d,%f)|", i, j, valueList[index2DtoLinear(i, j)]);
147  }
148 
149  printf("\n");
150  }
151 
153  void printMatrix()
154  {
155  for (int i = 0; i < this->getNumRows(); i++)
156  {
157  printRow(i);
158  }
159  }
160 
161  //DUMMY FUNCTION
162  #ifdef __CUDACC__
163  thrust::device_vector<T> getAbsRowSumCUDA(bool transposed)
164  {
165  thrust::device_vector<T> result(this->getNumRows(), (T)1);
166 
167  return result;
168  }
169  #endif
170 
171  private:
172  void doTimesCPU(bool transposed, const Tdata &input, Tdata &output,const mySign s)
173  {
174  if (transposed)
175  {
176  #pragma omp parallel for
177  for (int j = 0; j < this->getNumCols(); ++j)
178  {
179  T tmp = static_cast<T>(0);
180 
181  for (int i = 0; i < this->getNumRows(); ++i)
182  {
183  tmp += input[i] * valueList[index2DtoLinear(i, j)];
184  }
185 
186  switch (s)
187  {
188  case PLUS:
189  {
190  output[j] += tmp;
191  break;
192  }
193  case MINUS:
194  {
195  output[j] -= tmp;
196  break;
197  }
198  }
199  }
200  }
201  else
202  {
203  for (int j = 0; j < this->getNumCols(); ++j)
204  {
205  T tmp = input[j];
206  #pragma omp parallel for
207  for (int i = 0; i < this->getNumRows(); ++i)
208  {
209  switch (s)
210  {
211  case PLUS:
212  {
213  output[i] += tmp * valueList[index2DtoLinear(i, j)];
214  break;
215  }
216  case MINUS:
217  {
218  output[i] -= tmp * valueList[index2DtoLinear(i, j)];
219  break;
220  }
221  }
222  }
223  }
224  }
225  }
226 };
227 
228 #endif
std::vector< T > getAbsRowSum(bool transposed)
returns a vector of sum of absolute values per row used for preconditioning
Definition: flexFullMatrix.h:96
represents a full (non-CUDA) matrix
Definition: flexFullMatrix.h:10
int getNumRows() const
returns number of rows of the linear operator
Definition: flexLinearOperator.h:57
void timesMinus(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector and substracts its result from y
Definition: flexFullMatrix.h:60
flexFullMatrix(int aNumRows, int aNumCols, bool aMinus)
initializes a matrix
Definition: flexFullMatrix.h:32
flexFullMatrix()
initializes an empty matrix
Definition: flexFullMatrix.h:24
T getMaxRowSumAbs(bool transposed)
returns the maximum sum of absolute values per row used for preconditioning
Definition: flexFullMatrix.h:88
bool isMinus
determines if operator is negated
Definition: flexLinearOperator.h:25
void times(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector
Definition: flexFullMatrix.h:43
int getNumCols() const
returns number of columns of the linear operator
Definition: flexLinearOperator.h:48
mySign
enum representing the type of concatenation
Definition: tools.h:56
void printRow(int i)
prints requested row
Definition: flexFullMatrix.h:142
void timesPlus(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector and adds its result to y
Definition: flexFullMatrix.h:48
void printMatrix()
prints the whole matrix
Definition: flexFullMatrix.h:153
flexFullMatrix< T > * copy()
copies the linear operator
Definition: flexFullMatrix.h:34
abstract base class for linear operators
Definition: flexLinearOperator.h:12
thrust::device_vector< T > getAbsRowSumCUDA(bool transposed)
same function as getAbsRowSum() but implemented in CUDA
Definition: flexFullMatrix.h:163