GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
gmSparseMatrixLayout.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_LAYOUT_H_
26#define _GEMA_SPARSE_MATRIX_LAYOUT_H_
27
28#include "gmCoreConfig.h"
29
30#include <armadillo>
31#include <QObject>
32
33#if 0
34#define ENABLE_MATRIX_LAYOUT_INDEX_CACHE 1
35#endif
36
37#ifdef ENABLE_MATRIX_LAYOUT_INDEX_CACHE
39#endif
40
41
42class GmLogCategory;
43
50
51
66{
67 Q_OBJECT
68
69public:
71 GmSparseMatrixLayout(GmSparseMatrixLayoutTypes type) : _type(type), _shared(false) {}
72
75
77 virtual bool empty() const = 0;
78
80 virtual void clear() = 0;
81
83 virtual int index(int row, int col) const = 0;
84
90 virtual int dindex(int n) const = 0;
91
93 virtual void printStatistics(const GmLogCategory& logger) const = 0;
94
96 void emitLayoutCompleted() { emit layoutCompleted(); }
97
99 mutable bool _shared;
100
101#ifdef ENABLE_TESTS
102 virtual void checkLayoutData() const = 0;
103#endif
104
105signals:
111
112private:
114};
115
116
122template <class IndexType, GmSparseMatrixLayoutTypes T>
124{
125public:
132 {
133 _ptr = _index = NULL;
134 _n = _nnz = 0;
135 _arma = arma;
136
137#ifdef ENABLE_MATRIX_LAYOUT_INDEX_CACHE
138 _lastIndex.init(-1);
139#endif
140 }
141
144 {
145 delete[] _ptr;
146 delete[] _index;
147 }
148
149 // See comments on the base class
150 virtual bool empty() const { return _n == 0; }
151
152 // See comments on the base class
153 virtual void clear()
154 {
155 delete[] _ptr;
156 delete[] _index;
157 _ptr = _index = NULL;
158 _n = _nnz = 0;
159
160#ifdef ENABLE_MATRIX_LAYOUT_INDEX_CACHE
161 _lastIndex.init(-1);
162#endif
163 }
164
165 // Index implementation is specialized explicitly in the cpp file to avoid the need of an 'if' checking
166 // the layout type. Although this is static and the compiler should be able to optimize the if, looking
167 // at the generated optimized code does not clarify if the compiler is really doing that :(.
168 virtual int index(int row, int col) const;
169
170 // See comments on the base class. TODO: Should we try to optimize searching first in the center values?
171 virtual int dindex(int n) const { return indexWorker(_ptr[n], _ptr[n+1], n); }
172
173 virtual void printStatistics(const GmLogCategory& logger) const;
174
175#ifdef ENABLE_TESTS
176 virtual void checkLayoutData() const;
177#endif
178
179 IndexType* _ptr;
180 IndexType* _index;
181 int _n;
182 int _nnz;
183 int _arma;
184
185#ifdef ENABLE_MATRIX_LAYOUT_INDEX_CACHE
186 mutable GmTLS<int, true> _lastIndex;
187#endif
188
189protected:
190 inline int indexWorker(int start, int end, int ind) const;
191};
192
193
194/* If ever changing arma type to a 32-bits value, please make sure to review the code in
195 GmVectorCSxSparseMatrixLayoutBuilder. This will open the possibility of reusing the
196 builder internal vector as the layout index vector. Care will also be needed in that case
197 with allocating space for the arma sentinel value in the builder vector.
198*/
199
200
203
206
209
212
213#endif
214
Class representing a category with multiple logging levels.
Definition gmLog.h:58
A class that works together with GmThreadManager to provide thread local storage.
Definition gmThreadLocalStorage.h:132
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
GmCSxSparseMatrixLayout< int, GM_CSR_SPARSE_FORMAT > GmCSRSparseMatrixLayout
CSR layout with 32 bits integer index.
Definition gmSparseMatrixLayout.h:202
GmCSxSparseMatrixLayout< arma::uword, GM_CSR_SPARSE_FORMAT > GmCSRArmadilloSparseMatrixLayout
CSR layout with 64 bits integer index.
Definition gmSparseMatrixLayout.h:205
GmCSxSparseMatrixLayout< int, GM_CSC_SPARSE_FORMAT > GmCSCSparseMatrixLayout
CSC layout with 32 bits integer index.
Definition gmSparseMatrixLayout.h:208
GmSparseMatrixLayoutTypes
Supported sparse matrix types by GmSparseMatrixLayout sub-classes.
Definition gmSparseMatrixLayout.h:46
@ GM_CSC_SPARSE_FORMAT
Data is stored in "Compressed Sparse Column" format.
Definition gmSparseMatrixLayout.h:47
@ GM_CSR_SPARSE_FORMAT
Data is stored in "Compressed Sparse Row" (or old Yale) format.
Definition gmSparseMatrixLayout.h:48
GmCSxSparseMatrixLayout< arma::uword, GM_CSC_SPARSE_FORMAT > GmCSCArmadilloSparseMatrixLayout
CSC layout with 64 bits integer index.
Definition gmSparseMatrixLayout.h:211
Declaration of the GmTLS class.
Q_DISABLE_COPY(Class)
A structure for storing a matrix layout in either CSR or CSC format. The template type defines the ty...
Definition gmSparseMatrixLayout.h:124
IndexType * _ptr
Vector storing the index of the first non zero element of each matrix row(CSR)/column(CSC)....
Definition gmSparseMatrixLayout.h:179
virtual bool empty() const
Returns true if the layout is empty.
Definition gmSparseMatrixLayout.h:150
int _arma
Stores 1 if the layout should store 1 extra space per vector to be compatible with Armadillo requirem...
Definition gmSparseMatrixLayout.h:183
virtual int index(int row, int col) const
Returns the index for the value in the given matrix row, col. Returns -1 if the value is not part of ...
GmCSxSparseMatrixLayout(bool arma)
Constructor.
Definition gmSparseMatrixLayout.h:131
virtual int dindex(int n) const
Returns the index for the value in the diagonal of the given matrix row (column). Returns -1 if the v...
Definition gmSparseMatrixLayout.h:171
int _n
The number of rows(CSR)/columns(CSC) in the matrix.
Definition gmSparseMatrixLayout.h:181
virtual void clear()
Clears the layout releasing memory and making the matrix a zero matrix.
Definition gmSparseMatrixLayout.h:153
IndexType * _index
Vector storing the column(CSR)/row(CSC) index for each non zero value. Size = _nnz.
Definition gmSparseMatrixLayout.h:180
int _nnz
The number of elements stored in the matrix (in some cases zero values can be stored,...
Definition gmSparseMatrixLayout.h:182
virtual ~GmCSxSparseMatrixLayout()
Destructor.
Definition gmSparseMatrixLayout.h:143
A base structure for storing layout data for sparse matrices (fill structure or non zero positions)....
Definition gmSparseMatrixLayout.h:66
virtual bool empty() const =0
Returns true if the layout is empty.
GmSparseMatrixLayoutTypes _type
The stored sparse matrix type.
Definition gmSparseMatrixLayout.h:98
void emitLayoutCompleted()
Emits the layoutCompleted signal.
Definition gmSparseMatrixLayout.h:96
virtual int dindex(int n) const =0
Returns the index for the value in the diagonal of the given matrix row (column). Returns -1 if the v...
void layoutCompleted()
A signal emitted when the layout has been completed. This signal is emitted on behalf of the layout b...
virtual ~GmSparseMatrixLayout()
Virtual destructor.
Definition gmSparseMatrixLayout.h:74
GmSparseMatrixLayout(GmSparseMatrixLayoutTypes type)
Constructor.
Definition gmSparseMatrixLayout.h:71
virtual void printStatistics(const GmLogCategory &logger) const =0
Print layout statistics to the given logger.
bool _shared
Is this layout shared? Mutable to enable matrices sharing a layout to specify so.
Definition gmSparseMatrixLayout.h:99
virtual void clear()=0
Clears the layout releasing memory and making the matrix a zero matrix.
virtual int index(int row, int col) const =0
Returns the index for the value in the given matrix row, col. Returns -1 if the value is not part of ...