5 #ifndef _SPARSE_MATRIX_H
6 #define _SPARSE_MATRIX_H
20 #include <OpenCL/opencl.h>
31 #include "skepu/src/malloc_allocator.h"
47 RUTHERFOR_BOEING_FORMAT
104 bool m_transposeValid;
117 void deleteCSCFormat()
122 m_transposeValid =
false;
124 if(m_colPtrCSC!=NULL)
129 if(m_rowIndCSC!=NULL)
134 if(m_valuesCSC!=NULL)
140 if(m_cscMatrix!=NULL)
147 void convertToCSCFormat()
151 std::cerr<<
"Cannot apply transpose operation on a matrix which is already in CSC format.\n";
155 if(m_transposeValid && (m_valuesCSC!=NULL) && (m_rowIndCSC!=NULL) && (m_colPtrCSC!=NULL))
160 if(m_valuesCSC==NULL)
161 m_valuesCSC =
new T[m_nnz];
162 if(m_rowIndCSC == NULL)
163 m_rowIndCSC =
new size_t[m_nnz];
164 if(m_colPtrCSC == NULL)
165 m_colPtrCSC =
new size_t[m_cols+1];
168 size_t nxtRowIdx = 0;
170 std::multimap<size_t, std::pair<size_t, T>, std::less<size_t> > cscFormat;
172 for(
size_t ii = 0; ii < m_rows; ii++)
174 rowIdx = m_rowPtr[ii];
175 nxtRowIdx = m_rowPtr[ii+1];
177 for(
size_t jj=rowIdx; jj<nxtRowIdx; jj++)
179 cscFormat.insert( std::make_pair(m_colInd[jj], std::make_pair(ii, m_values[jj])) );
184 typedef typename std::multimap<size_t, std::pair<size_t, T>, std::less<size_t> >
::iterator mapIter;
187 size_t col = 0, ind=0;
191 size_t expColInd = 0;
193 for (m_it = cscFormat.begin(); m_it != cscFormat.end(); m_it = s_it)
195 size_t colInd = (*m_it).first;
197 if(expColInd < colInd)
199 for(
size_t i=expColInd; i<colInd; i++)
201 m_colPtrCSC[++col] = ind;
204 expColInd = colInd+1;
206 std::pair<mapIter, mapIter> keyRange = cscFormat.equal_range(colInd);
209 for (s_it = keyRange.first; s_it != keyRange.second; ++s_it)
211 m_rowIndCSC[ind] = (*s_it).second.first;
212 m_valuesCSC[ind] = (*s_it).second.second;
215 m_colPtrCSC[++col] = ind;
217 for(
size_t i= (col+1); i<=m_cols; i++)
219 m_colPtrCSC[i] = ind;
222 m_transposeValid =
true;
226 void readMTXFile(
const std::string &inputfile);
231 std::string m_nameVerbose;
234 void printMatrixInDenseFormat()
236 std::cerr<<
"SparseMatrix ("<<m_rows <<
" X "<< m_cols<<
") nnz: "<<m_nnz<<
"\n";
238 T *temp =
new T[m_cols];
240 size_t rowIdx, nxtRowIdx;
241 for(
size_t ii = 0; ii < m_rows; ii++)
243 for(
size_t i=0; i<m_cols; i++)
246 rowIdx = m_rowPtr[ii];
247 nxtRowIdx = m_rowPtr[ii+1];
249 for(
size_t jj=rowIdx; jj<nxtRowIdx; jj++)
251 temp[m_colInd[jj]] = m_values[jj];
254 for(
size_t i=0; i<m_cols; i++)
255 std::cerr<<std::setw(5)<<temp[i];
266 convertToCSCFormat();
268 if(m_cscMatrix==NULL)
270 m_cscMatrix =
new SparseMatrix(m_cols, m_rows, m_nnz, m_valuesCSC, m_colPtrCSC, m_rowIndCSC,
false, T(),
true);
277 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);
296 friend std::ostream& operator<<(std::ostream &os, SparseMatrix<T>& matrix)
300 os<<
"Matrix rows="<< matrix.total_rows() <<
", cols="<<matrix.total_cols()<<
", nnz="<<matrix.total_nnz()<<
"\n";
303 size_t nxtRowIdx = 0;
305 for(
size_t ii = 0; ii < matrix.total_rows(); ii++)
307 rowIdx = matrix.m_rowPtr[ii];
308 nxtRowIdx = matrix.m_rowPtr[ii+1];
310 for(
size_t jj=rowIdx; jj<nxtRowIdx; jj++)
312 os<<
"row: "<<std::setw(8)<<ii<<
", col: "<<std::setw(8)<<matrix.m_colInd[jj]<<
", value"<<std::setw(12)<<matrix.m_values[jj]<<
"\n";
359 size_t* get_row_pointers()
const
364 size_t* get_col_indices()
const
369 size_t get_rowSize(
size_t row)
const
371 return (m_rowPtr[row+1]-m_rowPtr[row]);
374 size_t get_rowOffsetFromStart(
size_t row)
const
377 return m_rowPtr[row];
384 iterator
begin(
unsigned row);
387 T
at(
size_t row,
size_t col )
const;
388 const T&
at(
size_t index)
const;
391 const T& at_internal(
size_t row,
size_t col )
const;
392 T& at_internal(
size_t row,
size_t col );
393 const T& at_internal(
size_t index)
const;
396 const T&
operator()(
const size_t row,
const size_t col)
const;
397 T
operator()(
const size_t row,
const size_t col);
400 void resize(SparseMatrix<T> ©,
bool retainData);
407 device_pointer_type_cl
updateDevice_CL(T* start,
size_t elems, Device_CL* device,
bool copy);
408 device_pointer_index_type_cl
updateDevice_Index_CL(
size_t* start,
size_t elems, Device_CL* device,
bool copy);
413 device_pointer_type_cu
updateDevice_CU(T* start,
size_t elems,
unsigned int deviceID,
bool copy);
414 device_pointer_index_type_cu
updateDevice_Index_CU(
size_t* start,
size_t elems,
unsigned int deviceID,
bool copy);
424 void updateHost_CL()
const;
425 void invalidateDeviceData_CL();
426 void releaseDeviceAllocations_CL();
430 void updateHost_CU()
const;
431 void invalidateDeviceData_CU();
432 void releaseDeviceAllocations_CU();
436 std::map<std::pair< unsigned int, std::pair< T*, size_t > >, device_pointer_type_cu > m_deviceMemPointers_CU;
437 std::map<std::pair< unsigned int, std::pair< size_t*, size_t > >, device_pointer_index_type_cu > m_deviceMemIndexPointers_CU;
441 std::map<std::pair< cl_device_id, std::pair< T*, size_t > >, device_pointer_type_cl > m_deviceMemPointers_CL;
442 std::map<std::pair< cl_device_id, std::pair< size_t*, size_t > >, device_pointer_index_type_cl > m_deviceMemIndexPointers_CL;
Contains the definitions of the SparseMatrix::iterator class.
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.
Definition: sparse_matrix_cu.inl:82
Contains a class declaration for an object which represents an OpenCL device memory allocation for co...
Contains a class declaration for an object which represents an CUDA device memory allocation for Vect...
void resize(SparseMatrix< T > ©, bool retainData)
Definition: sparse_matrix.inl:431
size_t total_cols() const
Definition: sparse_matrix.h:345
void flush_CL()
Flushes the sparse matrix.
Definition: sparse_matrix_cl.inl:123
size_t total_nnz() const
Definition: sparse_matrix.h:327
void flush_CU()
Flushes the matrix.
Definition: sparse_matrix_cu.inl:128
A sparse matrix container class that mainly stores its data in CSR format.
Definition: sparse_matrix.h:73
void updateHostAndInvalidateDevice()
Definition: sparse_matrix.inl:639
~SparseMatrix()
Definition: sparse_matrix.inl:244
A class representing an OpenCL device memory allocation for container.
Definition: device_mem_pointer_cl.h:38
T at(size_t row, size_t col) const
Definition: sparse_matrix.inl:296
T min(T a, T b)
Definition: mapoverlap_convol_kernels.h:212
device_pointer_type_cl updateDevice_CL(T *start, size_t elems, Device_CL *device, bool copy)
Update device with sparse matrix content.
Definition: sparse_matrix_cl.inl:25
iterator begin(unsigned row)
Definition: sparse_matrix.inl:346
T max(T a, T b)
Definition: mapoverlap_convol_kernels.h:203
size_t total_rows() const
Definition: sparse_matrix.h:336
device_pointer_type_cu updateDevice_CU(T *start, size_t elems, unsigned int deviceID, bool copy)
Update device with matrix content.
Definition: sparse_matrix_cu.inl:26
An sparse matrix iterator class that tranverses row-wise.
Definition: sparse_matrix_iterator.inl:20
Contains the definitions of member functions of the SparseMatrix class related to CUDA backend...
bool isModified_CU(unsigned int deviceID)
Definition: sparse_matrix_cu.inl:191
void updateHost() const
Definition: sparse_matrix.inl:609
void invalidateDevice()
Definition: sparse_matrix.inl:624
void operator=(const SparseMatrix< T > ©)
Definition: sparse_matrix.inl:191
bool isSparseMatrixOnDevice_CU(unsigned int deviceID)
Definition: sparse_matrix_cu.inl:208
A class representing a CUDA device memory allocation for container.
Definition: device_mem_pointer_cu.h:58
Contains a class declaration for Environment class.
SparseFileFormat
Can be used to specify the input format for a sparse matrix that is supplied in constructor.
Definition: sparse_matrix.h:43
Contains the definitions of member functions of the SparseMatrix class related to OpenCL backend...
const T & operator()(const size_t row, const size_t col) const
Definition: sparse_matrix.inl:367
Contains the definitions of member functions of the SparseMatrix class that are not related to any ba...
T * get_values() const
Definition: sparse_matrix.h:354
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.
Definition: sparse_matrix_cl.inl:80
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)
Definition: sparse_matrix.inl:29