CRS.hh

Go to the documentation of this file.
1 
7 #ifndef CRS_hh
8 #define CRS_hh
9 
10 #include <sys/types.h>
11 #include <map>
12 #include "toolbox/array.hh"
13 #include "basics/debug.hh"
15 
16 #define CRS_rowSorting_D 0
17 #define CCS_rowSorting_D 0
18 
19 namespace concepts {
20 
21  // ******************************************************** CRSConvertable **
22 
29  template<class F>
31  public:
33  virtual uint used() const = 0;
38  virtual void convertCRS(F* a, int* asub, int* xa) const = 0;
44  virtual void convertCCS(F* a, int* asub, int* xa) const = 0;
49  virtual void convertIJK(F* a, int* irn, int* jcn) const = 0;
50 
51  virtual ~CRSConvertable() {}
52  };
53 
54  // ************************************************* sparseLineToArrays **
55 
62  template<typename F>
63  void sparseLineToArrays(std::map<int, F>& line, F* a, int* asub) {
64  uint nnz = 0;
65  typename std::map<int, F>::const_iterator j = line.begin();
66  for(; j != line.end(); ++j) {
67  a[nnz] = j->second; // value
68  asub[nnz++] = j->first; // column number
69  }
70  }
71 
72  // ************************************************* convertCRS_rowSorting **
73 
87  template<class F>
88  void convertCRS_rowSorting(F& m, typename F::value_type* a, int* asub,
89  int* xa)
90  {
91  DEBUGL(CRS_rowSorting_D, "CRS format of " << m);
92 
93  typedef typename F::value_type value;
94 
95  int nnz = 0;
96  uint r = 0;
97  xa[0] = 0;
98  std::map<int, value> row; // temporary, for sorting inside row
99 
100  typename F::const_iterator i = m.begin();
101  DEBUGL(CRS_rowSorting_D, "i = " << i);
102  for(; i != m.end(); ++i) {
103  DEBUGL((CRS_rowSorting_D && i != m.begin()), "i = " << i);
104  if (i.row() > r) {
105  sparseLineToArrays(row, a + nnz, asub + nnz);
106  nnz += row.size();
107  row.clear();
108  for(; r < i.row();)
109  xa[++r] = nnz;
110  }
111  row[i.col()] = *i;
112  }
113  sparseLineToArrays(row, a + nnz, asub + nnz);
114  xa[++r] = nnz + row.size();
115  for(; r < getNumberofRows(m) ; )
116  xa[++r] = nnz + row.size();
117  }
118 
119  // ************************************************* convertCCS_rowSorting **
120 
134  template<class F>
135  void convertCCS_rowSorting(F& m, typename F::type* a, int* asub, int* xa) {
136  DEBUGL(CCS_rowSorting_D, "CCS format of " << m);
137 
138  //typedef typename F::type value; this is not used somewhere
139 
140  uint nnz = 0;
141  uint n = m.nofCols(); // number of columns
142 
143  for(uint c = 0; c < n; ++c)
144  xa[c] = 0;
145  // First loop over all entrances and count the number of entrances
146  // per column (instead of last one).
147  for(typename F::const_iterator i = m.begin(); i != m.end(); ++i) {
148  if (i.col() < n-1) ++xa[i.col()+1];
149  ++nnz;
150  }
151 
152  // cumulated sum -> first index of each column
153  for(uint c = 2; c < n; ++c)
154  xa[c] += xa[c-1];
155  xa[n] = nnz;
156 
157  // copy it to this array for the current index in each column
158  Array<int> idx(xa, n+1);
159  DEBUGL(CCS_rowSorting_D, "col_ptr = " << idx);
160  for(typename F::const_iterator i = m.begin(); i != m.end(); ++i) {
161  int &j = idx[i.col()];
162  a[j] = *i ; // nzval
163  asub[j] = i.row(); // rowind
164  DEBUGL(CCS_rowSorting_D, "write (" << i.row() << ", " << i.col() <<
165  ", " << *i << ") to idx " << j);
166  ++j;
167  }
168  }
169 
170  template<class F>
171  void convertIJK_unSorted(F& m, typename F::type* a, int* irn, int* jcn) {
172  //uint nnz = 0; //nothing happens here?
173 
174  }
175 
176 } // namespace concepts
177 
178 #endif // CRS_hh
virtual ~CRSConvertable()
Definition: CRS.hh:51
virtual void convertCRS(F *a, int *asub, int *xa) const =0
Converts to Compressed Row Storage (CRS) format and writes values to field a, the column number asub ...
#define DEBUGL(doit, msg)
void convertIJK_unSorted(F &m, typename F::type *a, int *irn, int *jcn)
Definition: CRS.hh:171
void convertCRS_rowSorting(F &m, typename F::value_type *a, int *asub, int *xa)
Method converts a matrix of type F to Sparse Row Storage (CRS) format.
Definition: CRS.hh:88
virtual uint used() const =0
Returns the number of used entries in the matrix.
#define CCS_rowSorting_D
Definition: CRS.hh:17
void sparseLineToArrays(std::map< int, F > &line, F *a, int *asub)
This function converts a sparse line to an array of values and an array of indices.
Definition: CRS.hh:63
void convertCCS_rowSorting(F &m, typename F::type *a, int *asub, int *xa)
Method converts a matrix of type F to Sparse Column Storage (CCS) format.
Definition: CRS.hh:135
#define CRS_rowSorting_D
Definition: CRS.hh:16
virtual void convertIJK(F *a, int *irn, int *jcn) const =0
Convert to coordinate (COO) format and writes values to field a, the row indices to irn and the colum...
Base class for operators which can be converted to Sparse Row Storage (CRS) or Sparse Column Storage ...
Definition: CRS.hh:30
virtual void convertCCS(F *a, int *asub, int *xa) const =0
Converts to Compressed Column Storage (CCS) format and writes values to field a, the row number asub ...
uint getNumberofRows(HashedSparseMatrix< F > &m)
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich