A sparse matrix container class that mainly stores its data in CSR format. More...
#include <sparse_matrix.h>
Classes | |
class | iterator |
An sparse matrix iterator class that tranverses row-wise. More... | |
Public Member Functions | |
SparseMatrix (size_t rows, size_t cols, size_t nnz, T *values, size_t *rowPtr, size_t *colInd, bool dealloc=true, T zeroValue=T(), bool transMatrix=false) | |
SparseMatrix (size_t rows, size_t cols, size_t nnz, T min, T max, T zeroValue=T()) | |
SparseMatrix (const SparseMatrix ©) | |
SparseMatrix (const std::string &inputfile, enum SparseFileFormat format=MATRIX_MARKET_FORMAT, T zeroValue=T()) | |
~SparseMatrix () | |
void | operator= (const SparseMatrix< T > ©) |
size_t | total_nnz () const |
size_t | total_rows () const |
size_t | total_cols () const |
T * | get_values () const |
iterator | begin (unsigned row) |
T | at (size_t row, size_t col) const |
const T & | at (size_t index) const |
const T & | operator() (const size_t row, const size_t col) const |
T | operator() (const size_t row, const size_t col) |
void | resize (SparseMatrix< T > ©, bool retainData) |
void | updateHost () const |
void | invalidateDevice () |
void | updateHostAndInvalidateDevice () |
device_pointer_type_cl | updateDevice_CL (T *start, size_t elems, Device_CL *device, bool copy) |
Update device with sparse matrix content. More... | |
device_pointer_index_type_cl | updateDevice_Index_CL (size_t *start, size_t elems, Device_CL *device, bool copy) |
Update device with sparse matrix index contents that have an "size_t" type. More... | |
void | flush_CL () |
Flushes the sparse matrix. More... | |
device_pointer_type_cu | updateDevice_CU (T *start, size_t elems, unsigned int deviceID, bool copy) |
Update device with matrix content. More... | |
device_pointer_index_type_cu | updateDevice_Index_CU (size_t *start, size_t elems, unsigned int deviceID, bool copy) |
Update device with sparse matrix index contents that have an "size_t" type. More... | |
void | flush_CU () |
Flushes the matrix. More... | |
bool | isSparseMatrixOnDevice_CU (unsigned int deviceID) |
bool | isModified_CU (unsigned int deviceID) |
Friends | |
std::ostream & | operator<< (std::ostream &os, SparseMatrix< T > &matrix) |
Overloaded stream operator, for testing purposes. More... | |
A sparse matrix container class that mainly stores its data in CSR format.
A skepu::SparseMatrix
is a container for storing sparse 2D structures that are internally stores in a 1D array using CSR format. It supports operation to load sparse matrices from file as well as creating random sparse matrices. As CSR format, it stores 3 arrays: actual values, column indices and row offsets. It also keeps track of which parts of it are currently allocated and uploaded to the GPU. If a computation is done, changing the elements in the GPU memory, it is not directly transferred back to the host memory. Instead, the SparseMatrix waits until an element is accessed before any copying is done. The class also supports transpose operation which it achieves by converting CSR (Compressed Storage Rows) format to CSC (Compressed Storage Column) format which can be useful e.g. when applying column-wise reduction operation. The transpose can be obtained by simply '~' operation, e.g., transpose_m0 = ~m0;
It also implements support for allocating and de-allocating page-locked memory using cudaMallocHost and cudaFreeHost. This could help is running asynchronous operations especially when using multiple CUDA devices. It can be enabled by defining USE_PINNED_MEMORY flag in the skeleton program.
skepu::SparseMatrix< T >::SparseMatrix | ( | size_t | rows, |
size_t | cols, | ||
size_t | nnz, | ||
T * | values, | ||
size_t * | rowPtr, | ||
size_t * | colInd, | ||
bool | dealloc = true , |
||
T | zeroValue = T() , |
||
bool | transMatrix = false |
||
) |
SparseMatrix Constructor, used to create a sparse matrix with given data (rows-offsets,cols-indices and non-zero elements).
rows | Number of rows in the sparse matrix. |
cols | Number of columns in the sparse matrix. |
nnz | Number of non-zero elements in the sparse matrix. |
values | An array containing non-zero elements stored row-wise in C order. |
rowPtr | An array containing indices pointing to starting indices of each row in the values array. |
colInd | An array containing indices pointing to column indices for each element in the values array. |
dealloc | A boolean defining whether the arrays are going to be de-allocated when the destructor is called. |
zeroValue | value that represent zero value for the given elements type, default will be initial value of that data type. |
transMatrix | A boolean that specifies whether the matrix is a transpose matrix or a normal one. |
skepu::SparseMatrix< T >::SparseMatrix | ( | size_t | rows, |
size_t | cols, | ||
size_t | nnz, | ||
T | min, | ||
T | max, | ||
T | zeroValue = T() |
||
) |
SparseMatrix Constructor, used to generate a random sparse matrix (in privided min max range) with given rows,cols and non-zero elements.
rows | Number of rows in the sparse matrix. |
cols | Number of columns in the sparse matrix. |
nnz | Number of non-zero elements in the sparse matrix. |
min | Minimum value for the random values generated |
max | Maximum value for the random values generated |
zeroValue | value that represent zero value for the given elements type, default will be initial value of that data type. |
References skepu::get_random_number().
skepu::SparseMatrix< T >::SparseMatrix | ( | const SparseMatrix< T > & | copy | ) |
SparseMatrix Copy Constructor, used to create a copy of another sparse matrix. Does not copy the transpose matrix part.
copy | sparse matrix which we are aopying from. |
skepu::SparseMatrix< T >::SparseMatrix | ( | const std::string & | inputfile, |
enum SparseFileFormat | format = MATRIX_MARKET_FORMAT , |
||
T | zeroValue = T() |
||
) |
SparseMatrix Constructor, used to read a sparse matrix from a text file (preferably in MTX format) with given rows,cols and non-zero elements.
inputfile | Name of the input file. |
format | Format of the matrix storage, currently only MATRIX_MARKET_FORMAT is supported. |
zeroValue | value that represent zero value for the given elements type, default will be initial value of that data type. |
skepu::SparseMatrix< T >::~SparseMatrix | ( | ) |
SparseMatrix Destructor. Internally Deallocates memory if proper flags are set.
T skepu::SparseMatrix< T >::at | ( | size_t | row, |
size_t | col | ||
) | const |
Uses row
and col
instead to find element. If found, return actual otherwise returns 0
row | Index of row to get. |
col | Index of column to get. |
const T & skepu::SparseMatrix< T >::at | ( | size_t | index | ) | const |
Directly access an element by specifying its index. If found, return actual otherwise returns 0
index | Index of row to get. |
|
inline |
Please refer to the documentation of std::vector
and skepu::Matrix::iterator
. Uses row
to get an iterator for that row.
row | The index of row from where to start iterator. |
Referenced by skepu::Reduce< ReduceFunc, ReduceFunc >::CL(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::CPU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CPU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CU(), skepu::ompIrregularWorkload(), and skepu::ompRegularWorkload().
void skepu::SparseMatrix< T >::flush_CL | ( | ) |
Flushes the sparse matrix.
First it updates the sparse matrix from all its device allocations, then it releases all allocations.
void skepu::SparseMatrix< T >::flush_CU | ( | ) |
Flushes the matrix.
First it updates the matrix from all its device allocations, then it releases all allocations.
|
inline |
Returns pointer to actual non zero values in the SparseMatrix.
Referenced by skepu::MapArray< MapArrayFunc >::CL(), skepu::Reduce< ReduceFunc, ReduceFunc >::CL(), skepu::Map< MapFunc >::CL(), skepu::MapArray< MapArrayFunc >::CPU(), skepu::Map< MapFunc >::CPU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CPU(), skepu::MapArray< MapArrayFunc >::CU(), skepu::Map< MapFunc >::CU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CU(), skepu::MapArray< MapArrayFunc >::OMP(), skepu::Map< MapFunc >::OMP(), and skepu::Reduce< ReduceFunc, ReduceFunc >::OMP().
|
inline |
Invalidates all device data that this sparse matrix has allocated.
bool skepu::SparseMatrix< T >::isModified_CU | ( | unsigned int | deviceID | ) |
Can be used to query whether sparse matrix is modified on a device or not.
Referenced by skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::operator()(), skepu::Map< MapFunc >::operator()(), skepu::MapArray< MapArrayFunc >::operator()(), and skepu::Reduce< ReduceFunc, ReduceFunc >::operator()().
bool skepu::SparseMatrix< T >::isSparseMatrixOnDevice_CU | ( | unsigned int | deviceID | ) |
Can be used to query whether matrix is already available on a device or not.
Referenced by skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::operator()(), skepu::Map< MapFunc >::operator()(), skepu::MapArray< MapArrayFunc >::operator()(), and skepu::Reduce< ReduceFunc, ReduceFunc >::operator()().
const T & skepu::SparseMatrix< T >::operator() | ( | const size_t | row, |
const size_t | col | ||
) | const |
Can be used to access elements by specifying row and column index. it cares about synchronizing with device. Can be used when accessing to access elements row and column wise.
row | Index to a specific row of the sparse matrix. |
col | Index to a specific column of the sparse matrix. |
T skepu::SparseMatrix< T >::operator() | ( | const size_t | row, |
const size_t | col | ||
) |
Behaves like operator
[] and unlike skepu::Vector
, it cares about synchronizing with device. Can be used when accessing to access elements row and column wise.
row | Index to a specific row of the sparse matrix. |
col | Index to a specific column of the sparse matrix. |
void skepu::SparseMatrix< T >::operator= | ( | const SparseMatrix< T > & | other | ) |
SparseMatrix Copy Constructor, used to create a copy of another sparse matrix. Does not copy the transpose matrix part.
copy | sparse matrix which we are aopying from. |
void skepu::SparseMatrix< T >::resize | ( | SparseMatrix< T > & | copy, |
bool | retainData | ||
) |
will resize the matrix, can be dangerous, used with care
Referenced by skepu::Map< MapFunc >::operator()().
|
inline |
Returns total number of columns in the SparseMatrix.
Referenced by skepu::MapArray< MapArrayFunc >::CL(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::CPU(), skepu::MapArray< MapArrayFunc >::CPU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CPU(), skepu::MapArray< MapArrayFunc >::CU(), skepu::MapArray< MapArrayFunc >::OMP(), and skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::operator()().
|
inline |
Returns number of non zero elements in the SparseMatrix.
Referenced by skepu::Reduce< ReduceFunc, ReduceFunc >::CL(), skepu::Map< MapFunc >::CL(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::CPU(), skepu::MapArray< MapArrayFunc >::CPU(), skepu::Map< MapFunc >::CPU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CPU(), skepu::MapArray< MapArrayFunc >::CU(), skepu::Map< MapFunc >::CU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CU(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::OMP(), skepu::MapArray< MapArrayFunc >::OMP(), skepu::Map< MapFunc >::OMP(), skepu::Reduce< ReduceFunc, ReduceFunc >::OMP(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::operator()(), skepu::Map< MapFunc >::operator()(), and skepu::Reduce< ReduceFunc, ReduceFunc >::operator()().
|
inline |
Returns total number of rows in the SparseMatrix.
Referenced by skepu::MapArray< MapArrayFunc >::CL(), skepu::Reduce< ReduceFunc, ReduceFunc >::CL(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::CPU(), skepu::MapArray< MapArrayFunc >::CPU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CPU(), skepu::MapArray< MapArrayFunc >::CU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CU(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::OMP(), skepu::MapArray< MapArrayFunc >::OMP(), skepu::Reduce< ReduceFunc, ReduceFunc >::OMP(), skepu::ompIrregularWorkload(), skepu::ompRegularWorkload(), and skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::operator()().
SparseMatrix< T >::device_pointer_type_cl skepu::SparseMatrix< T >::updateDevice_CL | ( | T * | start, |
size_t | elems, | ||
Device_CL * | device, | ||
bool | copy | ||
) |
Update device with sparse matrix content.
Update device with a SparseMatrix range by specifying number of elements. If sparse matrix does not have an allocation on the device for the current range, create a new allocation and if specified, also copy sparse matrix data to device. Saves newly allocated ranges to m_deviceMemPointers_CL
so sparse matrix can keep track of where and what it has stored on devices.
start | Pointer to first element in range to be updated with device. |
elems | Number of (non-zero) elements. |
device | Pointer to the device that should be synched with. |
copy | Boolean value that tells whether to only allocate or also copy sparse matrix data to device. True copies, False only allocates. |
References skepu::DeviceMemPointer_CL< T >::copyHostToDevice(), and skepu::Device_CL::getDeviceID().
Referenced by skepu::MapArray< MapArrayFunc >::CL(), skepu::Reduce< ReduceFunc, ReduceFunc >::CL(), and skepu::Map< MapFunc >::CL().
SparseMatrix< T >::device_pointer_type_cu skepu::SparseMatrix< T >::updateDevice_CU | ( | T * | start, |
size_t | elems, | ||
unsigned int | deviceID, | ||
bool | copy | ||
) |
Update device with matrix content.
Update device with a SparseMatrix range by specifying array of non-zero elements. If SparseMatrix does not have an allocation on the device for the current range, create a new allocation and if specified, also copy SparseMatrix data to device. Saves newly allocated ranges to m_deviceMemPointers_CU
so SparseMatrix can keep track of where and what it has stored on devices.
start | Pointer to first (non-zero) element in range to be updated with device. |
elems | Number of (non-zero) elements. |
deviceID | Integer specififying the device that should be synched with. |
copy | boolean value that tells whether to only allocate or also copy sparse matrix data to device. True copies, false only allocates. |
References skepu::DeviceMemPointer_CU< T >::copyHostToDevice(), and skepu::Environment< T >::getInstance().
Referenced by skepu::MapArray< MapArrayFunc >::CU(), skepu::Map< MapFunc >::CU(), and skepu::Reduce< ReduceFunc, ReduceFunc >::CU().
SparseMatrix< T >::device_pointer_index_type_cl skepu::SparseMatrix< T >::updateDevice_Index_CL | ( | size_t * | start, |
size_t | elems, | ||
Device_CL * | device, | ||
bool | copy | ||
) |
Update device with sparse matrix index contents that have an "size_t" type.
Update device with a SparseMatrix index range by specifying number of elements of "size_t" type. If sparse matrix does not have an allocation on the device for the current range, create a new allocation and if specified, also copy sparse matrix data to device. Saves newly allocated ranges to m_deviceMemIndexPointers_CL
so sparse matrix can keep track of where and what it has stored on devices.
start | Pointer to first index element in range to be updated with device. |
elems | Number of (non-zero) index elements. |
device | Pointer to the device that should be synched with. |
copy | Boolean value that tells whether to only allocate or also copy sparse matrix index data to device. True copies, False only allocates. |
References skepu::DeviceMemPointer_CL< T >::copyHostToDevice(), and skepu::Device_CL::getDeviceID().
Referenced by skepu::MapArray< MapArrayFunc >::CL().
SparseMatrix< T >::device_pointer_index_type_cu skepu::SparseMatrix< T >::updateDevice_Index_CU | ( | size_t * | start, |
size_t | elems, | ||
unsigned int | deviceID, | ||
bool | copy | ||
) |
Update device with sparse matrix index contents that have an "size_t" type.
Update device with a SparseMatrix index range by specifying number of elements of "size_t" type. If sparse matrix does not have an allocation on the device for the current range, create a new allocation and if specified, also copy sparse matrix data to device. Saves newly allocated ranges to m_deviceMemIndexPointers_CU
so sparse matrix can keep track of where and what it has stored on devices.
start | Pointer to first index element in range to be updated with device. |
elems | Number of (non-zero) index elements. |
deviceID | Integer specififying the device that should be synched with. |
copy | Boolean value that tells whether to only allocate or also copy sparse matrix index data to device. True copies, False only allocates. |
References skepu::DeviceMemPointer_CU< T >::copyHostToDevice(), and skepu::Environment< T >::getInstance().
Referenced by skepu::MapArray< MapArrayFunc >::CU().
|
inline |
Updates the sparse matrix from its device allocations.
Referenced by skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::CPU(), skepu::MapArray< MapArrayFunc >::CPU(), skepu::Map< MapFunc >::CPU(), skepu::Reduce< ReduceFunc, ReduceFunc >::CPU(), skepu::Reduce< ReduceFuncRowWise, ReduceFuncColWise >::OMP(), skepu::MapArray< MapArrayFunc >::OMP(), skepu::Map< MapFunc >::OMP(), and skepu::Reduce< ReduceFunc, ReduceFunc >::OMP().
|
inline |
First updates the sparse matrix from its device allocations. Then invalidates the data allocated on devices.
Referenced by skepu::Map< MapFunc >::CPU(), and skepu::Map< MapFunc >::OMP().
|
friend |
Overloaded stream operator, for testing purposes.
Outputs the sparse matrix having one element on each line.