SkePU  1.2
 All Classes Namespaces Files Functions Variables Enumerations Friends Macros Groups Pages
Classes | Public Member Functions | Friends | List of all members
skepu::SparseMatrix< T > Class Template Reference

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 &copy)
 
 SparseMatrix (const std::string &inputfile, enum SparseFileFormat format=MATRIX_MARKET_FORMAT, T zeroValue=T())
 
 ~SparseMatrix ()
 
void operator= (const SparseMatrix< T > &copy)
 
size_t total_nnz () const
 
size_t total_rows () const
 
size_t total_cols () const
 
T * get_values () const
 
iterator begin (unsigned row)
 
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
 
operator() (const size_t row, const size_t col)
 
void resize (SparseMatrix< T > &copy, 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...
 

Detailed Description

template<class T>
class skepu::SparseMatrix< T >

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.

Constructor & Destructor Documentation

template<typename T>
skepu::SparseMatrix< T >::SparseMatrix ( size_t  rows,
size_t  cols,
size_t  nnz,
T *  values,
size_t *  rowPtr,
size_t *  colInd,
bool  dealloc = true,
zeroValue = T(),
bool  transMatrix = false 
)

SparseMatrix Constructor, used to create a sparse matrix with given data (rows-offsets,cols-indices and non-zero elements).

Parameters
rowsNumber of rows in the sparse matrix.
colsNumber of columns in the sparse matrix.
nnzNumber of non-zero elements in the sparse matrix.
valuesAn array containing non-zero elements stored row-wise in C order.
rowPtrAn array containing indices pointing to starting indices of each row in the values array.
colIndAn array containing indices pointing to column indices for each element in the values array.
deallocA boolean defining whether the arrays are going to be de-allocated when the destructor is called.
zeroValuevalue that represent zero value for the given elements type, default will be initial value of that data type.
transMatrixA boolean that specifies whether the matrix is a transpose matrix or a normal one.
template<typename T>
skepu::SparseMatrix< T >::SparseMatrix ( size_t  rows,
size_t  cols,
size_t  nnz,
min,
max,
zeroValue = T() 
)

SparseMatrix Constructor, used to generate a random sparse matrix (in privided min max range) with given rows,cols and non-zero elements.

Parameters
rowsNumber of rows in the sparse matrix.
colsNumber of columns in the sparse matrix.
nnzNumber of non-zero elements in the sparse matrix.
minMinimum value for the random values generated
maxMaximum value for the random values generated
zeroValuevalue that represent zero value for the given elements type, default will be initial value of that data type.

References skepu::get_random_number().

Here is the call graph for this function:

template<typename T>
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.

Parameters
copysparse matrix which we are aopying from.
template<typename T>
skepu::SparseMatrix< T >::SparseMatrix ( const std::string &  inputfile,
enum SparseFileFormat  format = MATRIX_MARKET_FORMAT,
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.

Parameters
inputfileName of the input file.
formatFormat of the matrix storage, currently only MATRIX_MARKET_FORMAT is supported.
zeroValuevalue that represent zero value for the given elements type, default will be initial value of that data type.
template<typename T >
skepu::SparseMatrix< T >::~SparseMatrix ( )

SparseMatrix Destructor. Internally Deallocates memory if proper flags are set.

Member Function Documentation

template<typename T >
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

Parameters
rowIndex of row to get.
colIndex of column to get.
template<typename T >
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

Parameters
indexIndex of row to get.
template<typename T >
SparseMatrix< T >::iterator skepu::SparseMatrix< T >::begin ( unsigned  row)
inline

Please refer to the documentation of std::vector and skepu::Matrix::iterator. Uses row to get an iterator for that row.

Parameters
rowThe 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().

template<typename T >
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.

template<typename T >
void skepu::SparseMatrix< T >::flush_CU ( )

Flushes the matrix.

First it updates the matrix from all its device allocations, then it releases all allocations.

template<class T>
T* skepu::SparseMatrix< T >::get_values ( ) const
inline
template<typename T >
void skepu::SparseMatrix< T >::invalidateDevice ( )
inline

Invalidates all device data that this sparse matrix has allocated.

template<typename T >
bool skepu::SparseMatrix< T >::isModified_CU ( unsigned int  deviceID)
template<typename T >
bool skepu::SparseMatrix< T >::isSparseMatrixOnDevice_CU ( unsigned int  deviceID)
template<typename T >
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.

Parameters
rowIndex to a specific row of the sparse matrix.
colIndex to a specific column of the sparse matrix.
template<typename T >
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.

Parameters
rowIndex to a specific row of the sparse matrix.
colIndex to a specific column of the sparse matrix.
template<typename T>
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.

Parameters
copysparse matrix which we are aopying from.
template<typename T>
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()().

template<class T>
size_t skepu::SparseMatrix< T >::total_cols ( ) const
inline
template<class T>
size_t skepu::SparseMatrix< T >::total_nnz ( ) const
inline
template<class T>
size_t skepu::SparseMatrix< T >::total_rows ( ) const
inline
template<typename T>
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.

Parameters
startPointer to first element in range to be updated with device.
elemsNumber of (non-zero) elements.
devicePointer to the device that should be synched with.
copyBoolean 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().

Here is the call graph for this function:

template<typename T>
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.

Parameters
startPointer to first (non-zero) element in range to be updated with device.
elemsNumber of (non-zero) elements.
deviceIDInteger specififying the device that should be synched with.
copyboolean 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().

Here is the call graph for this function:

template<typename T >
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.

Parameters
startPointer to first index element in range to be updated with device.
elemsNumber of (non-zero) index elements.
devicePointer to the device that should be synched with.
copyBoolean 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().

Here is the call graph for this function:

template<typename T >
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.

Parameters
startPointer to first index element in range to be updated with device.
elemsNumber of (non-zero) index elements.
deviceIDInteger specififying the device that should be synched with.
copyBoolean 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().

Here is the call graph for this function:

template<typename T >
void skepu::SparseMatrix< T >::updateHost ( ) const
inline
template<typename T >
void skepu::SparseMatrix< T >::updateHostAndInvalidateDevice ( )
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().

Friends And Related Function Documentation

template<class T>
std::ostream& operator<< ( std::ostream &  os,
SparseMatrix< T > &  matrix 
)
friend

Overloaded stream operator, for testing purposes.

Outputs the sparse matrix having one element on each line.


The documentation for this class was generated from the following files: