FlexBox - A Flexible Primal-Dual ToolBox
flexMatrixLogical.h
1 #ifndef flexMatrixLogical_H
2 #define flexMatrixLogical_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 
21 
22 public:
23  std::vector<int> rowToIndexList;
24  std::vector<int> indexList;//
26  flexMatrixLogical() : rowToIndexList(), indexList(), flexLinearOperator<T>(0, 0, matrixOp, false) {};
27 
29 
34  flexMatrixLogical(int aNumRows, int aNumCols, bool aMinus) : rowToIndexList(aNumRows + 1, static_cast<int>(0)), indexList(0, 0), flexLinearOperator<T>(aNumRows, aNumCols, matrixOp, aMinus){};
35 
37  {
38  flexMatrixLogical<T>* A = new flexMatrixLogical<T>(this->getNumRows(), this->getNumCols(), this->isMinus);
39 
40  A->rowToIndexList = indexList;
41  A->indexList = rowToIndexList;
42 
43  return A;
44  }
45 
46  //todo
47  void times(bool transposed, const Tdata &input, Tdata &output)
48  {
49 
50  }
51 
52  void timesPlus(bool transposed, const Tdata &input, Tdata &output)
53  {
54  if (this->isMinus)
55  {
56  doTimesCPU(transposed, input, output,MINUS);
57  }
58  else
59  {
60  doTimesCPU(transposed, input, output,PLUS);
61  }
62  }
63 
64  void timesMinus(bool transposed, const Tdata &input, Tdata &output)
65  {
66  if (this->isMinus)
67  {
68  doTimesCPU(transposed, input, output,PLUS);
69  }
70  else
71  {
72  doTimesCPU(transposed, input, output,MINUS);
73  }
74  }
75 
77 
82  void blockInsert(const std::vector<int> &indexI, const std::vector<int> &indexJ)
83  {
84  //clear matrix
85  //clear();
86 
87  int numberListElements = (int)indexI.size();
88 
89  //initialize vecvector
90  std::vector<int> emptyBucket(0, 0);
91  std::vector < std::vector<int> > buckets(this->getNumRows(), emptyBucket);
92 
93  //add elements to buckets
94  for (int indexInput = 0; indexInput < numberListElements; indexInput++)
95  {
96  buckets[indexI[indexInput]].push_back(indexInput);
97  }
98 
99  //go trough all rows:
100  for (int indexRow = 0; indexRow < this->getNumRows(); indexRow++)
101  {
102  int numElements = 0;
103 
104  //go through bucket
105  for (int indexBucket = 0; indexBucket < (int)buckets[indexRow].size(); indexBucket++)
106  {
107  int tmpIndex = buckets[indexRow][indexBucket];
108 
109  indexList.push_back(indexJ[tmpIndex]);
110  ++numElements;
111  }
112 
113  //update rowToIndexList
114  rowToIndexList[indexRow + 1] = rowToIndexList[indexRow] + numElements;
115  }
116  }
117 
118  int index2DtoLinear(int i, int j)
119  {
120  return i + j*this->getNumRows();
121  }
122 
123  T getMaxRowSumAbs(bool transposed)
124  {
125  std::vector<T> rowSum = this->getAbsRowSum(transposed);
126 
127  return *std::max_element(rowSum.begin(), rowSum.end());
128  }
129 
130 
131  std::vector<T> getAbsRowSum(bool transposed)
132  {
133  if (transposed)
134  {
135  std::vector<T> result(this->getNumCols());
136 
137  //todo check if omp is possible
138  for (int k = 0; k < this->getNumRows(); ++k)
139  {
140  for (int index = rowToIndexList[k]; index < rowToIndexList[k + 1]; ++index)
141  {
142  result[indexList[index]] += (T)1;
143  }
144  }
145 
146  /*for (int k = 0; k < this->getNumCols(); ++k)
147  {
148  printf("T%f\n", result[k]);
149  }*/
150 
151  return result;
152  }
153  else
154  {
155  std::vector<T> result(this->getNumRows());
156 
157  #pragma omp parallel for
158  for (int k = 0; k < this->getNumRows(); ++k)
159  {
160  T tmpSum = static_cast<T>(0);
161  for (int index = rowToIndexList[k]; index < rowToIndexList[k + 1]; ++index)
162  {
163  tmpSum += (T)1;
164  }
165 
166  result[k] = tmpSum;
167  }
168 
169  /*for (int k = 0; k < this->getNumRows(); ++k)
170  {
171  printf("%f\n", result[k]);
172  }*/
173 
174  return result;
175  }
176  }
177 
179 
182  void printRow(int i)
183  {
184  for (int index = rowToIndexList[i]; index < rowToIndexList[i + 1]; ++index)
185  {
186  printf("(%d,%d,1)|", i, indexList[index]);
187  }
188 
189  printf("\n");
190  }
191 
193  void printMatrix()
194  {
195  for (int i = 0; i < this->getNumRows(); i++)
196  {
197  printRow(i);
198  }
199  }
200 
201  //DUMMY FUNCTION
202  #ifdef __CUDACC__
203  thrust::device_vector<T> getAbsRowSumCUDA(bool transposed)
204  {
205  thrust::device_vector<T> result(this->getNumRows(), (T)1);
206 
207  return result;
208  }
209  #endif
210 
211  private:
212  void doTimesCPU(bool transposed, const Tdata &input, Tdata &output, const mySign s)
213  {
214  if (transposed)
215  {
216  //todo: check if transposed multiplication can be parallelized
217  #pragma omp parallel for
218  for (int i = 0; i < this->getNumRows(); ++i)
219  {
220  int indexNext = rowToIndexList[i + 1];
221  for (int index = rowToIndexList[i]; index < indexNext; ++index)
222  {
223  switch (s)
224  {
225  case PLUS:
226  {
227  output[indexList[index]] += input[i];
228  break;
229  }
230  case MINUS:
231  {
232  output[indexList[index]] -= input[i];
233  break;
234  }
235  }
236  }
237  }
238  }
239  else
240  {
241  #pragma omp parallel for
242  for (int i = 0; i < this->getNumRows(); ++i)
243  {
244  T rowsum = (T)0;
245  // initialize result
246  int indexNext = rowToIndexList[i + 1];
247  for (int index = rowToIndexList[i]; index < indexNext; ++index)
248  {
249  rowsum += input[indexList[index]];
250  }
251 
252  switch (s)
253  {
254  case PLUS:
255  {
256  output[i] += rowsum;
257  break;
258  }
259  case MINUS:
260  {
261  output[i] -= rowsum;
262  break;
263  }
264  }
265  }
266  }
267  }
268 };
269 
270 #endif
flexMatrixLogical()
initializes an empty matrix
Definition: flexMatrixLogical.h:26
int getNumRows() const
returns number of rows of the linear operator
Definition: flexLinearOperator.h:57
bool isMinus
determines if operator is negated
Definition: flexLinearOperator.h:25
thrust::device_vector< T > getAbsRowSumCUDA(bool transposed)
same function as getAbsRowSum() but implemented in CUDA
Definition: flexMatrixLogical.h:203
flexMatrixLogical(int aNumRows, int aNumCols, bool aMinus)
initializes a matrix
Definition: flexMatrixLogical.h:34
represents a full (non-CUDA) logical matrix
Definition: flexMatrixLogical.h:10
void blockInsert(const std::vector< int > &indexI, const std::vector< int > &indexJ)
inserts position of all non-zero elements into matrix
Definition: flexMatrixLogical.h:82
int getNumCols() const
returns number of columns of the linear operator
Definition: flexLinearOperator.h:48
T getMaxRowSumAbs(bool transposed)
returns the maximum sum of absolute values per row used for preconditioning
Definition: flexMatrixLogical.h:123
std::vector< T > getAbsRowSum(bool transposed)
returns a vector of sum of absolute values per row used for preconditioning
Definition: flexMatrixLogical.h:131
mySign
enum representing the type of concatenation
Definition: tools.h:56
flexMatrixLogical< T > * copy()
copies the linear operator
Definition: flexMatrixLogical.h:36
void printMatrix()
prints the whole matrix
Definition: flexMatrixLogical.h:193
void printRow(int i)
prints requested row
Definition: flexMatrixLogical.h:182
void timesPlus(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector and adds its result to y
Definition: flexMatrixLogical.h:52
abstract base class for linear operators
Definition: flexLinearOperator.h:12
void times(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector
Definition: flexMatrixLogical.h:47
void timesMinus(bool transposed, const Tdata &input, Tdata &output)
applies linear operator on vector and substracts its result from y
Definition: flexMatrixLogical.h:64