// 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; }
clsparseStatus clsparseDCsrMatrixfromFile( clsparseCsrMatrix* csrMatx, const char* filePath, clsparseControl control, cl_bool read_explicit_zeroes ) { clsparseCsrMatrixPrivate* pCsrMatx = static_cast<clsparseCsrMatrixPrivate*>( csrMatx ); // 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; // Read data from a file on disk into CPU buffers // Data is read natively as COO format with the reader MatrixMarketReader< cl_double > mm_reader; if( mm_reader.MMReadFormat( filePath, read_explicit_zeroes ) ) return clsparseInvalidFile; // BUG: We need to check to see if openCL buffers currently exist and deallocate them first! // FIX: Below code will check whether the buffers were allocated in the first place; { clsparseStatus validationStatus = validateMemObject(pCsrMatx->values, mm_reader.GetNumNonZeroes() * sizeof(cl_double)); // I dont want to reallocate buffer because I suppress the users buffer memory flags; // It is users responsibility to provide good buffer; if (validationStatus != clsparseSuccess) return validationStatus; validationStatus = validateMemObject(pCsrMatx->col_indices, mm_reader.GetNumNonZeroes() * sizeof(clsparseIdx_t)); if (validationStatus != clsparseSuccess) return validationStatus; validationStatus = validateMemObject(pCsrMatx->row_pointer, (mm_reader.GetNumRows() + 1) * sizeof(clsparseIdx_t)); if (validationStatus != clsparseSuccess) return validationStatus; } pCsrMatx->num_rows = mm_reader.GetNumRows( ); pCsrMatx->num_cols = mm_reader.GetNumCols( ); pCsrMatx->num_nonzeros = mm_reader.GetNumNonZeroes( ); // Transfers data from CPU buffer to GPU buffers cl_int mapStatus = 0; clMemRAII< cl_double > rCsrValues( control->queue( ), pCsrMatx->values); clMemRAII< clsparseIdx_t > rCsrcol_indices( control->queue( ), pCsrMatx->col_indices ); clMemRAII< clsparseIdx_t > rCsrrow_pointer( control->queue( ), pCsrMatx->row_pointer ); cl_double* fCsrValues = rCsrValues.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCsrMatx->valOffset( ), pCsrMatx->num_nonzeros, &mapStatus ); if (mapStatus != CL_SUCCESS) { CLSPARSE_V(mapStatus, "Error: Mapping rCsrValues failed"); return clsparseInvalidMemObj; } clsparseIdx_t* iCsrcol_indices = rCsrcol_indices.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCsrMatx->colIndOffset( ), pCsrMatx->num_nonzeros, &mapStatus ); if (mapStatus != CL_SUCCESS) { CLSPARSE_V(mapStatus, "Error: Mapping rCsrcol_indices failed"); return clsparseInvalidMemObj; } clsparseIdx_t* iCsrrow_pointer = rCsrrow_pointer.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCsrMatx->rowOffOffset( ), pCsrMatx->num_rows + 1, &mapStatus ); if (mapStatus != CL_SUCCESS) { CLSPARSE_V(mapStatus, "Error: Mapping rCsrrow_pointer failed"); return clsparseInvalidMemObj; } // The following section of code converts the sparse format from COO to CSR Coordinate< cl_double >* coords = mm_reader.GetUnsymCoordinates( ); std::sort( coords, coords + pCsrMatx->num_nonzeros, CoordinateCompare< cl_double > ); clsparseIdx_t current_row = 1; iCsrrow_pointer[ 0 ] = 0; for (clsparseIdx_t i = 0; i < pCsrMatx->num_nonzeros; i++) { iCsrcol_indices[ i ] = coords[ i ].y; fCsrValues[ i ] = coords[ i ].val; while( coords[ i ].x >= current_row ) iCsrrow_pointer[ current_row++ ] = i; } iCsrrow_pointer[ current_row ] = pCsrMatx->num_nonzeros; while( current_row <= pCsrMatx->num_rows ) iCsrrow_pointer[ current_row++ ] = pCsrMatx->num_nonzeros; return clsparseSuccess; }