GemaCoreLib
The GeMA Core library
gmSparseMatrixTripletData.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
25 #ifndef _GEMA_SPARSE_MATRIX_TRIPLET_DATA_H_
26 #define _GEMA_SPARSE_MATRIX_TRIPLET_DATA_H_
27 
28 #include "gmCoreConfig.h"
29 #include "gmSparseMatrixLayout.h"
30 #include "gmAppendBuffer.h"
31 
32 // The current implementation expects that the key vector inside
33 // the triplet data in GmSparseMatrixTripletData conforms to:
34 // - Size equal to 64 bits
35 // - Unsigned values (although stored into an int - ugly)
36 // - Little endianess so that when interpreting the key as an unsigned 64 bit
37 // value, the value at position 0 is the LEAST significant value
38 //
39 // This characteristics are used both by the internal operator < (which is much
40 // faster than a standard way with multiple compares) and by integer sorting
41 // routines in gmIntegerSort.h
42 
43 #if Q_BYTE_ORDER != Q_LITTLE_ENDIAN
44 #error Invalid Triplet layout
45 #endif
46 
47 static_assert(2 * sizeof(int) == sizeof(quint64), "Unexpected int size in triplet layout");
48 
49 
58 template <GmSparseMatrixLayoutTypes T>
60 {
61  int _key[2];
62  double _value;
63 
65  GmSparseMatrixTripletData(int lin, int col, double val);
66 
68  GmSparseMatrixTripletData(int ptr, int index, double val, bool) : _key{ index, ptr }, _value(val) {}
69 
71  GmSparseMatrixTripletData() : _key{ -1, -1 }, _value(0.0) {}
72 
74  int ptr() const { return _key[1]; }
75 
77  int index() const { return _key[0]; }
78 
84  bool operator<(const GmSparseMatrixTripletData<T>& other) const
85  {
86  // Same as:
87  // return (_key[1] < other._key[1]) || (_key[1] == other._key[1] && _key[0] < other._key[0]);
88  return *((quint64*)(&_key[0])) < *((quint64*)(&other._key[0]));
89  }
90 };
91 
92 // Constructor implementation for CSR layout
93 template <> inline
95  : _key{ col, lin }, _value(val) {}
96 
97 // Constructor implementation for CSC layout
98 template <> inline
100  : _key{ lin, col }, _value(val) {}
101 
102 
103 // Optimize use with QVarLengthArray like a POD type (no default constructor called, copyable with memcpy, etc).
104 Q_DECLARE_TYPEINFO(GmSparseMatrixTripletData<GM_CSC_SPARSE_FORMAT>, Q_PRIMITIVE_TYPE);
105 Q_DECLARE_TYPEINFO(GmSparseMatrixTripletData<GM_CSR_SPARSE_FORMAT>, Q_PRIMITIVE_TYPE);
106 
107 
110 {
113 };
114 
115 
119 template <GmSparseMatrixLayoutTypes T>
120 class GmSparseMatrixTripletBuffer : public GmAppendBuffer<GmSparseMatrixTripletData<T> >
121 {
122 public:
131 
132  static int strToSortStrategy(const QString& str);
134 };
135 
137 template <GmSparseMatrixLayoutTypes T>
138 class GmSparseMatrixSingleTripletBuffer : public GmSingleAppendBuffer<GmSparseMatrixTripletData<T>, GmSparseMatrixTripletBuffer<T> >
139 {
140 public:
142  GmSparseMatrixSingleTripletBuffer(size_t initSize, double resizeFactor = 2.0, int numThreads = -1)
143  : GmSingleAppendBuffer(initSize, resizeFactor, numThreads) {}
144 
146 };
147 
149 template <GmSparseMatrixLayoutTypes T>
150 class GmSparseMatrixPerThreadTripletBuffer : public GmPerThreadAppendBuffer<GmSparseMatrixTripletData<T>, GmSparseMatrixTripletBuffer<T> >
151 {
152 public:
154  GmSparseMatrixPerThreadTripletBuffer(size_t initSize, double resizeFactor = 2.0, int numThreads = -1)
155  : GmPerThreadAppendBuffer(initSize, resizeFactor, numThreads) {}
156 
158 };
159 
160 #endif
An implementation of the GmAppendBuffer interface based on a "per thread" growing buffer.
Definition: gmAppendBuffer.h:152
virtual GmSparseMatrixTripletData< T > * sortedData(int nlin, GmSparseMatrixTripletBufferSortStrategy st)
Similar to data() but returning a sorted vector, obtained by using the given strategy....
Definition: gmSparseMatrixTripletData.cpp:133
GmSparseMatrixSingleTripletBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. See the description for the GmSingleAppendBuffer constructor.
Definition: gmSparseMatrixTripletData.h:142
Declaration of usefull configuration definitions for the Core library.
GmSparseMatrixTripletData(int ptr, int index, double val, bool)
Alternate constructor receiving the ptr and the index values.
Definition: gmSparseMatrixTripletData.h:68
An implementation of the GmAppendBuffer interface based on synchoronized access to a shared buffer.
Definition: gmAppendBuffer.h:402
Especialization of the GmPerThreadAppendBuffer class using the GmSparseMatrixTripletBuffer interface.
Definition: gmSparseMatrixTripletData.h:150
A virtual class representing a buffer of T objects that can be appended in a thread-safe way,...
Definition: gmArmadilloSolverMatrix.h:36
Especialization of the GmSingleAppendBuffer class using the GmSparseMatrixTripletBuffer interface.
Definition: gmSparseMatrixTripletData.h:138
int ptr() const
Returns either the line (CSR) or column (CSC) data depending on the matrix format.
Definition: gmSparseMatrixTripletData.h:74
int index() const
Returns either the column (CSR) or line (CSC) data depending on the matrix format.
Definition: gmSparseMatrixTripletData.h:77
int _key[2]
Either column + line (CSR) or line + column (CSC), depending on the matrix format.
Definition: gmSparseMatrixTripletData.h:61
Declaration of the GmAppendBuffer class.
Vectors are concatenated and then parallel copy sorted. Faster when there is plenty of memory.
Definition: gmSparseMatrixTripletData.h:111
bool operator<(const GmSparseMatrixTripletData< T > &other) const
Compare operator to enable sorting with standard sort routines. Not needed by integer sorting....
Definition: gmSparseMatrixTripletData.h:84
virtual GmSparseMatrixTripletData< T > * sortedData(int nlin, GmSparseMatrixTripletBufferSortStrategy st)
Similar to data() but returning a sorted vector, obtained by using the given strategy....
Definition: gmSparseMatrixTripletData.cpp:63
An especialization of GmAppendBufffer for triplet data with an extra method for returning the buffer ...
Definition: gmSparseMatrixTripletData.h:120
GmSparseMatrixPerThreadTripletBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. See the description for the GmPerThreadAppendBuffer constructor.
Definition: gmSparseMatrixTripletData.h:154
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
virtual GmSparseMatrixTripletData< T > * sortedData(int nlin, GmSparseMatrixTripletBufferSortStrategy st)=0
Similar to data() but returning a sorted vector, obtained by using the given strategy....
GmSparseMatrixTripletBufferSortStrategy
The strategy used by the GmSparseMatrixTripletBuffer<T>::sortedData() methods.
Definition: gmSparseMatrixTripletData.h:109
Vectors are parallel inplace sorted, followed by a merge operation to the destination vector.
Definition: gmSparseMatrixTripletData.h:112
Aux structure used when building sparse matrices with the help of a triplet list. The tripet structur...
Definition: gmSparseMatrixTripletData.h:59
Declaration of the GmSparseMatrixLayout structure and its derived types.
static const char * sortStrategyToStr(GmSparseMatrixTripletBufferSortStrategy st)
Returns the string used to define the given sort strategy.
Definition: gmSparseMatrixTripletData.cpp:46
double _value
The triplet value.
Definition: gmSparseMatrixTripletData.h:62
static int strToSortStrategy(const QString &str)
Returns the sort strategy associated with the given string. Returns -1 if no match was found.
Definition: gmSparseMatrixTripletData.cpp:32
GmSparseMatrixTripletData()
Dummy constructor to allow using the structure inside a vector.
Definition: gmSparseMatrixTripletData.h:71