clsparseStatus clsparseDCooMatrixfromFile( clsparseCooMatrix* cooMatx, const char* filePath, clsparseControl control, cl_bool read_explicit_zeroes ) { clsparseCooMatrixPrivate* pCooMatx = static_cast<clsparseCooMatrixPrivate*>( cooMatx ); // Check that the file format is matrix market; the only format we can read right now // This is not a complete solution, and fails for directories with file names etc... // TODO: Should we use boost filesystem? std::string strPath( filePath ); if( strPath.find_last_of( '.' ) != std::string::npos ) { std::string ext = strPath.substr( strPath.find_last_of( '.' ) + 1 ); if( ext != "mtx" ) return clsparseInvalidFileFormat; } else return clsparseInvalidFileFormat; MatrixMarketReader< cl_double > mm_reader; if( mm_reader.MMReadFormat( filePath, read_explicit_zeroes ) ) return clsparseInvalidFile; pCooMatx->num_rows = mm_reader.GetNumRows( ); pCooMatx->num_cols = mm_reader.GetNumCols( ); pCooMatx->num_nonzeros = mm_reader.GetNumNonZeroes( ); // Transfers data from CPU buffer to GPU buffers clMemRAII< cl_double > rCooValues( control->queue( ), pCooMatx->values ); clMemRAII< clsparseIdx_t > rCoocol_indices( control->queue( ), pCooMatx->col_indices ); clMemRAII< clsparseIdx_t > rCoorow_indices( control->queue( ), pCooMatx->row_indices ); cl_double* fCooValues = rCooValues.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCooMatx->valOffset( ), pCooMatx->num_nonzeros ); clsparseIdx_t* iCoocol_indices = rCoocol_indices.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCooMatx->colIndOffset( ), pCooMatx->num_nonzeros ); clsparseIdx_t* iCoorow_indices = rCoorow_indices.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCooMatx->rowOffOffset( ), pCooMatx->num_nonzeros ); Coordinate< cl_double >* coords = mm_reader.GetUnsymCoordinates( ); //JPA:: Coo matrix is need to be sorted as well because we need to have matrix // which is sorted by row and then column, in the mtx files usually is opposite. std::sort( coords, coords + pCooMatx->num_nonzeros, CoordinateCompare< cl_double > ); for( clsparseIdx_t c = 0; c < pCooMatx->num_nonzeros; ++c ) { iCoorow_indices[ c ] = coords[ c ].x; iCoocol_indices[ c ] = coords[ c ].y; fCooValues[ c ] = coords[ c ].val; } return clsparseSuccess; }
// Converts a sparse matrix in COO compressed format into CSR compressed format // Pre-condition: The CL device memory for CSR values, colIndices, rowOffsets has to be allocated prior to entering this routine // and the offset variables for cl1.2 set clsparseStatus clsparseScoo2csr_host( clsparseCsrMatrix* csrMatx, const clsparseCooMatrix* cooMatx, clsparseControl control ) { if( !clsparseInitialized ) { return clsparseNotInitialized; } //check opencl elements if( control == nullptr ) { return clsparseInvalidControlObject; } const clsparseCooMatrixPrivate* pCooMatx = static_cast<const clsparseCooMatrixPrivate*>( cooMatx ); clsparseCsrMatrixPrivate* pCsrMatx = static_cast<clsparseCsrMatrixPrivate*>( csrMatx ); pCsrMatx->num_rows = pCooMatx->num_rows; pCsrMatx->num_cols = pCooMatx->num_cols; pCsrMatx->num_nonzeros = pCooMatx->num_nonzeros; clMemRAII< cl_float > rCooValues( control->queue( ), pCooMatx->values ); clMemRAII< cl_int > rCooColIndices( control->queue( ), pCooMatx->colIndices ); clMemRAII< cl_int > rCooRowIndices( control->queue( ), pCooMatx->rowIndices ); clMemRAII< cl_float > rCsrValues( control->queue( ), pCsrMatx->values ); clMemRAII< cl_int > rCsrColIndices( control->queue( ), pCsrMatx->colIndices ); clMemRAII< cl_int > rCsrRowOffsets( control->queue( ), pCsrMatx->rowOffsets ); cl_float* fCooValues = rCooValues.clMapMem( CL_TRUE, CL_MAP_READ, pCooMatx->valOffset( ), pCooMatx->num_nonzeros ); cl_int* iCooColIndices = rCooColIndices.clMapMem( CL_TRUE, CL_MAP_READ, pCooMatx->colIndOffset( ), pCooMatx->num_nonzeros ); cl_int* iCooRowIndices = rCooRowIndices.clMapMem( CL_TRUE, CL_MAP_READ, pCooMatx->rowOffOffset( ), pCooMatx->num_nonzeros ); cl_float* fCsrValues = rCsrValues.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCsrMatx->valOffset( ), pCsrMatx->num_nonzeros ); cl_int* iCsrColIndices = rCsrColIndices.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCsrMatx->colIndOffset( ), pCsrMatx->num_nonzeros ); cl_int* iCsrRowOffsets = rCsrRowOffsets.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCsrMatx->rowOffOffset( ), pCsrMatx->num_rows + 1 ); coo2csr_transform( fCooValues, iCooColIndices, iCooRowIndices, pCooMatx->num_nonzeros, fCsrValues, iCsrColIndices, iCsrRowOffsets ); return clsparseSuccess; }