SkePU 1.0
Classes | Public Member Functions | Friends
skepu::SparseMatrix< T > Class Template Reference

A sparse matrix container class that mainly stores its data in CSR format. More...

#include <sparse_matrix.h>

Collaboration diagram for skepu::SparseMatrix< T >:
Collaboration graph
[legend]

List of all members.

Classes

class  iterator
 An sparse matrix iterator class that tranverses row-wise. More...

Public Member Functions

 SparseMatrix (unsigned int rows, unsigned int cols, unsigned int nnz, T *values, unsigned int *rowPtr, unsigned int *colInd, bool dealloc=true, T zeroValue=T(), bool transMatrix=false)
 SparseMatrix (unsigned int rows, unsigned int cols, unsigned int 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 ()
unsigned int total_nnz () const
unsigned int total_rows () const
unsigned int total_cols () const
T * get_values () const
iterator begin (unsigned row)
at (unsigned int row, unsigned int col) const
const T & at (unsigned int index) const
const T & operator() (const unsigned int row, const unsigned int col) const
operator() (const unsigned int row, const unsigned int col)
void resize (SparseMatrix< T > &copy, bool retainData)
void updateHost () const
void invalidateDevice ()
void updateHostAndInvalidateDevice ()
device_pointer_type_cl updateDevice_CL (T *start, unsigned int elems, Device_CL *device, bool copy)
 Update device with sparse matrix content.
device_pointer_index_type_cl updateDevice_Index_CL (unsigned int *start, unsigned int elems, Device_CL *device, bool copy)
 Update device with sparse matrix index contents that have an "unsigned int" type.
void flush_CL ()
 Flushes the sparse matrix.
device_pointer_type_cu updateDevice_CU (T *start, unsigned int elems, int deviceID, bool copy)
 Update device with matrix content.
device_pointer_index_type_cu updateDevice_Index_CU (unsigned int *start, unsigned int elems, int deviceID, bool copy)
 Update device with sparse matrix index contents that have an "unsigned int" type.
void flush_CU ()
 Flushes the matrix.

Friends

std::ostream & operator<< (std::ostream &os, SparseMatrix< T > &matrix)
 Overloaded stream operator, for testing purposes.

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 ( unsigned int  rows,
unsigned int  cols,
unsigned int  nnz,
T *  values,
unsigned int *  rowPtr,
unsigned int *  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 ( unsigned int  rows,
unsigned int  cols,
unsigned int  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 ( unsigned int  row,
unsigned int  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 ( unsigned int  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.
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]

Returns pointer to actual non zero values in the SparseMatrix.

Returns:
pointer to actual non zero values in the SparseMatrix.
template<typename T >
void skepu::SparseMatrix< T >::invalidateDevice ( ) [inline]

Invalidates all device data that this sparse matrix has allocated.

template<typename T >
const T & skepu::SparseMatrix< T >::operator() ( const unsigned int  row,
const unsigned int  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 unsigned int  row,
const unsigned int  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 >::resize ( SparseMatrix< T > &  copy,
bool  retainData 
)

will resize the matrix, can be dangerous, used with care

template<class T>
unsigned int skepu::SparseMatrix< T >::total_cols ( ) const [inline]

Returns total number of columns in the SparseMatrix.

Returns:
columns in the SparseMatrix.
template<class T>
unsigned int skepu::SparseMatrix< T >::total_nnz ( ) const [inline]

Returns number of non zero elements in the SparseMatrix.

Returns:
count of non zero elements of the SparseMatrix.
template<class T>
unsigned int skepu::SparseMatrix< T >::total_rows ( ) const [inline]

Returns total number of rows in the SparseMatrix.

Returns:
rows in the SparseMatrix.
template<typename T>
SparseMatrix< T >::device_pointer_type_cl skepu::SparseMatrix< T >::updateDevice_CL ( T *  start,
unsigned int  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().

Here is the call graph for this function:

template<typename T>
SparseMatrix< T >::device_pointer_type_cu skepu::SparseMatrix< T >::updateDevice_CU ( T *  start,
unsigned int  elems,
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().

Here is the call graph for this function:

template<typename T >
SparseMatrix< T >::device_pointer_index_type_cl skepu::SparseMatrix< T >::updateDevice_Index_CL ( unsigned int *  start,
unsigned int  elems,
Device_CL device,
bool  copy 
)

Update device with sparse matrix index contents that have an "unsigned int" type.

Update device with a SparseMatrix index range by specifying number of elements of "unsigned int" 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().

Here is the call graph for this function:

template<typename T >
SparseMatrix< T >::device_pointer_index_type_cu skepu::SparseMatrix< T >::updateDevice_Index_CU ( unsigned int *  start,
unsigned int  elems,
int  deviceID,
bool  copy 
)

Update device with sparse matrix index contents that have an "unsigned int" type.

Update device with a SparseMatrix index range by specifying number of elements of "unsigned int" 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().

Here is the call graph for this function:

template<typename T >
void skepu::SparseMatrix< T >::updateHost ( ) const [inline]

Updates the sparse matrix from its device allocations.

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.


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:
 All Classes Namespaces Files Functions Enumerations Friends Defines