Skip to content

Commit 351a859

Browse files
committed
Introduced abstractions for bool csr matrices
1 parent 4c12769 commit 351a859

File tree

9 files changed

+608
-1
lines changed

9 files changed

+608
-1
lines changed

src/include/clSPARSE-1x.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,39 @@ typedef struct clsparseCsrMatrix_
8989
void* meta;
9090
} clsparseCsrMatrix;
9191

92+
/*! \brief Structure to encapsulate boolean sparse matrix data encoded in CSR
93+
* form to clSPARSE API
94+
*/
95+
typedef struct clsparseBoolCsrMatrix_
96+
{
97+
/** @name CSR matrix data */
98+
/**@{*/
99+
clsparseIdx_t num_rows; /*!< Number of rows this matrix has if viewed as dense */
100+
clsparseIdx_t num_cols; /*!< Number of columns this matrix has if viewed as dense */
101+
clsparseIdx_t num_nonzeros; /*!< Number of values in matrix that are non-zero */
102+
/**@}*/
103+
104+
/** @name OpenCL state */
105+
/**@{*/
106+
cl_mem col_indices; /*!< column index for corresponding value of size num_nonzeros */
107+
cl_mem row_pointer; /*!< Invariant: row_pointer[i+1]-row_pointer[i] = number of values in row i */
108+
/**@}*/
109+
110+
/** @name Buffer offsets */
111+
/**@{*/
112+
/*! Given that cl_mem objects are opaque without pointer arithmetic, these offsets are added to
113+
* the cl_mem locations on device to define beginning of the data in the cl_mem buffers
114+
*/
115+
clsparseIdx_t off_col_indices;
116+
clsparseIdx_t off_row_pointer;
117+
/**@}*/
118+
119+
/*! Pointer to a private structure that contains meta-information the library keeps on a
120+
csr-encoded sparse matrix
121+
*/
122+
void* meta;
123+
} clsparseBoolCsrMatrix;
124+
92125
/*! \brief Structure to encapsulate sparse matrix data encoded in COO
93126
* form to clSPARSE API
94127
* \note The indices stored are 0-based

src/include/clSPARSE-2x.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ typedef struct clsparseCsrMatrix_
6868
void* meta;
6969
} clsparseCsrMatrix;
7070

71+
/*! \brief Structure to encapsulate sparse bool matrix data encoded in CSR
72+
* form to clSPARSE API
73+
*/
74+
typedef struct clsparseBoolCsrMatrix_
75+
{
76+
/** @name CSR matrix data */
77+
/**@{*/
78+
clsparseIdx_t num_rows; /*!< Number of rows this matrix has if viewed as dense */
79+
clsparseIdx_t num_cols; /*!< Number of columns this matrix has if viewed as dense */
80+
clsparseIdx_t num_nonzeros; /*!< Number of values in matrix that are non-zero */
81+
/**@}*/
82+
83+
/** @name OpenCL state */
84+
/**@{*/
85+
void* col_indices; /*!< column index for corresponding value of size num_nonzeros */
86+
void* row_pointer; /*!< Invariant: row_pointer[i+1]-row_pointer[i] = number of values in row i */
87+
/**@}*/
88+
89+
/*! Pointer to a private structure that contains meta-information the library keeps on a
90+
csr-encoded sparse matrix
91+
*/
92+
void* meta;
93+
} clsparseBoolCsrMatrix;
94+
7195
/*! \brief Structure to encapsulate sparse matrix data encoded in COO
7296
* form to clSPARSE API
7397
* \note The indices stored are 0-based

src/include/clSPARSE.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ extern "C" {
200200
CLSPARSE_EXPORT clsparseStatus
201201
clsparseInitCsrMatrix( clsparseCsrMatrix* csrMatx );
202202

203+
/*!
204+
* \brief Initialize a BOOL sparse matrix CSR structure to be used in the clsparse library
205+
* \note It is users responsibility to allocate OpenCL device memory
206+
*
207+
* \param[out] csrMatx Sparse BOOL CSR matrix structure to be initialized
208+
*
209+
* \returns \b clsparseSuccess
210+
*
211+
* \ingroup INIT
212+
*/
213+
CLSPARSE_EXPORT clsparseStatus
214+
clsparseInitBoolCsrMatrix( clsparseBoolCsrMatrix* csrMatx );
215+
203216
/*!
204217
* \brief Initialize a dense matrix structure to be used in the clsparse library
205218
* \note It is users responsibility to allocate OpenCL device memory
@@ -590,6 +603,31 @@ extern "C" {
590603
CLSPARSE_EXPORT clsparseStatus
591604
clsparseSCsrMatrixfromFile( clsparseCsrMatrix* csrMatx, const char* filePath, clsparseControl control, cl_bool read_explicit_zeroes );
592605

606+
/*!
607+
* \brief Read BOOL sparse matrix data from file in CSR format
608+
* \details This function reads the contents of the sparse matrix file into clsparseBoolCsrMatrix data structure.
609+
* The data structure represents the contents of the sparse matrix data in OpenCL device memory.
610+
* This function sorts the values read (on host) by row, then column before copying them into
611+
* device memory
612+
* \param[out] csrMatx The BOOL CSR sparse structure that represents the matrix in device memory
613+
* \param[in] filePath A path in the file-system to the sparse matrix file
614+
* \param[in] control A valid clsparseControl created with clsparseCreateControl
615+
* \param[in] read_explicit_zeroes If the file contains values explicitly declared zero, this controls
616+
* whether they are stored in the CSR
617+
*
618+
* \note The number of non-zeroes actually read from the file may be less than the number of
619+
* non-zeroes reported from the file header. Symmetrix matrices may store up to twice as many non-zero
620+
* values compared to the number of values in the file. Explicitly declared zeroes may be stored
621+
* or not depending on the input \p read_explicit_zeroes.
622+
* \note The OpenCL device memory must be allocated before the call to this function.
623+
* \post The sparse data is sorted first by row, then by column.
624+
* \returns \b clsparseSuccess
625+
*
626+
* \ingroup FILE
627+
*/
628+
CLSPARSE_EXPORT clsparseStatus
629+
clsparseSBoolCsrMatrixfromFile( clsparseBoolCsrMatrix* csrMatx, const char* filePath, clsparseControl control, cl_bool read_explicit_zeroes );
630+
593631
/*!
594632
* \brief Read sparse matrix data from file in double precision CSR format
595633
* \details This function reads the contents of the sparse matrix file into clsparseCsrMatrix data structure.
@@ -664,6 +702,44 @@ extern "C" {
664702
CLSPARSE_EXPORT clsparseStatus
665703
clsparseCsrMetaDelete( clsparseCsrMatrix* csrMatx );
666704

705+
/*!
706+
* \brief Calculate the amount of device memory required to hold meta-data for csr-adaptive SpM-dV algorithm
707+
* \details CSR-adaptive is a high performance sparse matrix times dense vector algorithm. It requires a pre-processing
708+
* step to calculate meta-data on the sparse matrix. This meta-data is stored alongside and carried along
709+
* with the other matrix data.
710+
* \param[in,out] csrMatx The BOOL CSR sparse structure that represents the matrix in device memory
711+
* \param[in] control A valid clsparseControl created with clsparseCreateControl
712+
*
713+
* \ingroup FILE
714+
*/
715+
CLSPARSE_EXPORT clsparseMetaSizeResult
716+
clsparseBoolCsrMetaSize( clsparseBoolCsrMatrix* csrMatx, clsparseControl control );
717+
718+
/*!
719+
* \brief Allocate memory and calculate the meta-data for csr-adaptive SpM-dV algorithm
720+
* \details CSR-adaptive is a high performance sparse matrix times dense vector algorithm. It requires a pre-processing
721+
* step to calculate meta-data on the sparse matrix. This meta-data is stored alongside and carried along
722+
* with the other matrix data. This function allocates memory for the meta-data and initializes it with proper values.
723+
* It is important to remember to deallocate the meta memory with clsparseCsrMetaDelete
724+
* \param[in,out] csrMatx The BOOL CSR sparse structure that represents the matrix in device memory
725+
* \param[in] control A valid clsparseControl created with clsparseCreateControl
726+
* \note This function assumes that the memory for rowBlocks has already been allocated by client program
727+
*
728+
* \ingroup FILE
729+
*/
730+
CLSPARSE_EXPORT clsparseStatus
731+
clsparseBoolCsrMetaCreate( clsparseBoolCsrMatrix* csrMatx, clsparseControl control );
732+
733+
/*!
734+
* \brief Delete meta data associated with a CSR encoded matrix
735+
* \details Meta data for a sparse matrix may occupy device memory, and this informs the library to release it
736+
* \param[in,out] csrMatx The BOOL CSR sparse structure that represents the matrix in device memory
737+
*
738+
* \ingroup FILE
739+
*/
740+
CLSPARSE_EXPORT clsparseStatus
741+
clsparseBoolCsrMetaDelete( clsparseBoolCsrMatrix* csrMatx );
742+
667743
/**@}*/
668744

669745
/*!
@@ -1209,6 +1285,41 @@ extern "C" {
12091285
const clsparseControl control );
12101286
/**@}*/
12111287

1288+
/*!
1289+
* \brief BOOL Single Precision CSR Sparse Matrix times Sparse Matrix
1290+
* \details \f$ C \leftarrow A \ast B \f$
1291+
* \param[in] sparseMatA Input CSR sparse matrix
1292+
* \param[in] sparseMatB Input CSR sparse matrix
1293+
* \param[out] sparseMatC Output CSR sparse matrix
1294+
* \param[in] control A valid clsparseControl created with clsparseCreateControl
1295+
* \pre The input sparse matrices data must first be sorted by rows, then by columns
1296+
* \ingroup BLAS-3
1297+
*/
1298+
CLSPARSE_EXPORT clsparseStatus
1299+
clsparseBoolScsrSpGemm( const clsparseBoolCsrMatrix* sparseMatA,
1300+
const clsparseBoolCsrMatrix* sparseMatB,
1301+
clsparseBoolCsrMatrix* sparseMatC,
1302+
const clsparseControl control );
1303+
/**@}*/
1304+
1305+
/*!
1306+
* \brief BOOL Single Precision CSR Sparse Matrix PLUS Sparse Matrix
1307+
* \details \f$ C \leftarrow A \plus B \f$
1308+
* \param[in] sparseMatA Input CSR sparse matrix
1309+
* \param[in] sparseMatB Input CSR sparse matrix
1310+
* \param[out] sparseMatC Output CSR sparse matrix
1311+
* \param[in] control A valid clsparseControl created with clsparseCreateControl
1312+
* \pre The input sparse matrices data must first be sorted by rows, then by columns
1313+
* \ingroup BLAS-3
1314+
*/
1315+
CLSPARSE_EXPORT clsparseStatus
1316+
clsparseBoolScsrElemAdd( const clsparseBoolCsrMatrix* sparseMatA,
1317+
const clsparseBoolCsrMatrix* sparseMatB,
1318+
clsparseBoolCsrMatrix* sparseMatC,
1319+
const clsparseControl control );
1320+
/**@}*/
1321+
1322+
12121323
/*!
12131324
* \defgroup CONVERT Matrix conversion routines
12141325
*

src/library/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ set( clSPARSE.source.blas3
133133
blas3/clsparse-csrmm.cpp
134134
blas3/clsparse-spm-spm.cpp
135135
blas3/clsparse-bool-spm-spm.cpp
136-
blas3/clsparse-bool-elem-add.cpp
137136
)
138137

139138
set( clSPARSE.source.solvers

src/library/clsparse-init.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ clsparseInitCsrMatrix( clsparseCsrMatrix* csrMatx )
9797
return clsparseSuccess;
9898
};
9999

100+
clsparseStatus
101+
clsparseInitBoolCsrMatrix( clsparseBoolCsrMatrix* csrMatx )
102+
{
103+
clsparseBoolCsrMatrixPrivate* pCsrMatx = static_cast<clsparseBoolCsrMatrixPrivate*>( csrMatx );
104+
pCsrMatx->clear( );
105+
106+
return clsparseSuccess;
107+
};
108+
100109
clsparseStatus
101110
cldenseInitMatrix( cldenseMatrix* denseMatx )
102111
{

src/library/include/clSPARSE-1x.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ class clsparseCsrMatrixPrivate: public clsparseCsrMatrix
233233
}
234234
};
235235

236+
class clsparseBoolCsrMatrixPrivate: public clsparseBoolCsrMatrix
237+
{
238+
public:
239+
void clear( )
240+
{
241+
num_rows = num_cols = num_nonzeros = 0;
242+
col_indices = row_pointer = nullptr;
243+
off_col_indices = off_row_pointer = 0;
244+
meta = nullptr;
245+
}
246+
247+
clsparseIdx_t nnz_per_row() const
248+
{
249+
return num_nonzeros / num_rows;
250+
}
251+
252+
clsparseIdx_t colIndOffset() const
253+
{
254+
return off_col_indices;
255+
}
256+
257+
clsparseIdx_t rowOffOffset() const
258+
{
259+
return off_row_pointer;
260+
}
261+
};
262+
236263
class clsparseCooMatrixPrivate: public clsparseCooMatrix
237264
{
238265
public:
@@ -286,6 +313,7 @@ class cldenseMatrixPrivate: public cldenseMatrix
286313
static_assert( std::is_standard_layout< clsparseScalarPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
287314
static_assert( std::is_standard_layout< cldenseVectorPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
288315
static_assert( std::is_standard_layout< clsparseCsrMatrixPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
316+
static_assert( std::is_standard_layout< clsparseBoolCsrMatrixPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
289317
static_assert( std::is_standard_layout< clsparseCooMatrixPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
290318
static_assert( std::is_standard_layout< cldenseMatrixPrivate>::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
291319

src/library/include/clSPARSE-2x.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,37 @@ class clsparseCsrMatrixPrivate: public clsparseCsrMatrix
208208
}
209209
};
210210

211+
class clsparseBoolCsrMatrixPrivate: public clsparseBoolCsrMatrix
212+
{
213+
public:
214+
void clear( )
215+
{
216+
num_rows = num_cols = num_nonzeros = 0;
217+
col_indices = row_pointer = rowBlocks = nullptr;
218+
rowBlockSize = 0;
219+
}
220+
221+
clsparseIdx_t nnz_per_row() const
222+
{
223+
return num_nonzeros / num_rows;
224+
}
225+
226+
cl_ulong colIndOffset () const
227+
{
228+
return 0;
229+
}
230+
231+
cl_ulong rowOffOffset () const
232+
{
233+
return 0;
234+
}
235+
236+
cl_ulong rowBlocksOffset( ) const
237+
{
238+
return 0;
239+
}
240+
};
241+
211242
class clsparseCooMatrixPrivate: public clsparseCooMatrix
212243
{
213244
public:
@@ -253,6 +284,7 @@ class cldenseMatrixPrivate: public cldenseMatrix
253284
static_assert( std::is_standard_layout< clsparseScalarPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
254285
static_assert( std::is_standard_layout< cldenseVectorPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
255286
static_assert( std::is_standard_layout< clsparseCsrMatrixPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
287+
static_assert( std::is_standard_layout< clsparseBoolCsrMatrixPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
256288
static_assert( std::is_standard_layout< clsparseCooMatrixPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
257289
static_assert( std::is_standard_layout< cldenseMatrixPrivate >::value, "The C++ wrapper classes have to have same memory layout as the C class they inherit from" );
258290

0 commit comments

Comments
 (0)