GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
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"
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
47static_assert(2 * sizeof(int) == sizeof(quint64), "Unexpected int size in triplet layout");
48
49
58template <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
93template <> inline
95 : _key{ col, lin }, _value(val) {}
96
97// Constructor implementation for CSC layout
98template <> 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).
104Q_DECLARE_TYPEINFO(GmSparseMatrixTripletData<GM_CSC_SPARSE_FORMAT>, Q_PRIMITIVE_TYPE);
105Q_DECLARE_TYPEINFO(GmSparseMatrixTripletData<GM_CSR_SPARSE_FORMAT>, Q_PRIMITIVE_TYPE);
106
107
114
115
119template <GmSparseMatrixLayoutTypes T>
120class GmSparseMatrixTripletBuffer : public GmAppendBuffer<GmSparseMatrixTripletData<T> >
121{
122public:
131
132 static int strToSortStrategy(const QString& str);
134};
135
137template <GmSparseMatrixLayoutTypes T>
138class GmSparseMatrixSingleTripletBuffer : public GmSingleAppendBuffer<GmSparseMatrixTripletData<T>, GmSparseMatrixTripletBuffer<T> >
139{
140public:
142 GmSparseMatrixSingleTripletBuffer(size_t initSize, double resizeFactor = 2.0, int numThreads = -1)
143 : GmSingleAppendBuffer(initSize, resizeFactor, numThreads) {}
144
146};
147
149template <GmSparseMatrixLayoutTypes T>
150class GmSparseMatrixPerThreadTripletBuffer : public GmPerThreadAppendBuffer<GmSparseMatrixTripletData<T>, GmSparseMatrixTripletBuffer<T> >
151{
152public:
154 GmSparseMatrixPerThreadTripletBuffer(size_t initSize, double resizeFactor = 2.0, int numThreads = -1)
155 : GmPerThreadAppendBuffer(initSize, resizeFactor, numThreads) {}
156
158};
159
160#endif
A virtual class representing a buffer of T objects that can be appended in a thread-safe way,...
Definition gmAppendBuffer.h:66
An implementation of the GmAppendBuffer interface based on a "per thread" growing buffer.
Definition gmAppendBuffer.h:153
An implementation of the GmAppendBuffer interface based on synchoronized access to a shared buffer.
Definition gmAppendBuffer.h:403
Especialization of the GmPerThreadAppendBuffer class using the GmSparseMatrixTripletBuffer interface.
Definition gmSparseMatrixTripletData.h:151
GmSparseMatrixPerThreadTripletBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. See the description for the GmPerThreadAppendBuffer constructor.
Definition gmSparseMatrixTripletData.h:154
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
Especialization of the GmSingleAppendBuffer class using the GmSparseMatrixTripletBuffer interface.
Definition gmSparseMatrixTripletData.h:139
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
GmSparseMatrixSingleTripletBuffer(size_t initSize, double resizeFactor=2.0, int numThreads=-1)
Buffer constructor. See the description for the GmSingleAppendBuffer constructor.
Definition gmSparseMatrixTripletData.h:142
An especialization of GmAppendBufffer for triplet data with an extra method for returning the buffer ...
Definition gmSparseMatrixTripletData.h:121
static const char * sortStrategyToStr(GmSparseMatrixTripletBufferSortStrategy st)
Returns the string used to define the given sort strategy.
Definition gmSparseMatrixTripletData.cpp:46
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
virtual GmSparseMatrixTripletData< T > * sortedData(int nlin, GmSparseMatrixTripletBufferSortStrategy st)=0
Similar to data() but returning a sorted vector, obtained by using the given strategy....
Declaration of the GmAppendBuffer class.
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Declaration of the GmSparseMatrixLayout structure and its derived types.
GmSparseMatrixTripletBufferSortStrategy
The strategy used by the GmSparseMatrixTripletBuffer<T>::sortedData() methods.
Definition gmSparseMatrixTripletData.h:110
@ GM_TRIBUF_SS_ISORT_MERGE
Vectors are parallel inplace sorted, followed by a merge operation to the destination vector.
Definition gmSparseMatrixTripletData.h:112
@ GM_TRIBUF_SS_CONCAT_PCSORT
Vectors are concatenated and then parallel copy sorted. Faster when there is plenty of memory.
Definition gmSparseMatrixTripletData.h:111
Aux structure used when building sparse matrices with the help of a triplet list. The tripet structur...
Definition gmSparseMatrixTripletData.h:60
GmSparseMatrixTripletData()
Dummy constructor to allow using the structure inside a vector.
Definition gmSparseMatrixTripletData.h:71
GmSparseMatrixTripletData(int ptr, int index, double val, bool)
Alternate constructor receiving the ptr and the index values.
Definition gmSparseMatrixTripletData.h:68
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
double _value
The triplet value.
Definition gmSparseMatrixTripletData.h:62
GmSparseMatrixTripletData(int lin, int col, double val)
Constructor.
int index() const
Returns either the column (CSR) or line (CSC) data depending on the matrix format.
Definition gmSparseMatrixTripletData.h:77
int ptr() const
Returns either the line (CSR) or column (CSC) data depending on the matrix format.
Definition gmSparseMatrixTripletData.h:74
int _key[2]
Either column + line (CSR) or line + column (CSC), depending on the matrix format.
Definition gmSparseMatrixTripletData.h:61