This function reads the file at the given filepath, and returns the sparse matrix in the COO struct. All matrix data is written to device memory Pre-condition: This function assumes that the device memory buffers have been pre-allocated by the caller
| 471 | // Pre-condition: This function assumes that the device memory buffers have been |
| 472 | // pre-allocated by the caller |
| 473 | clsparseStatus |
| 474 | clsparseSCooMatrixfromFile( clsparseCooMatrix* cooMatx, const char* filePath, clsparseControl control, cl_bool read_explicit_zeroes ) |
| 475 | { |
| 476 | clsparseCooMatrixPrivate* pCooMatx = static_cast<clsparseCooMatrixPrivate*>( cooMatx ); |
| 477 | |
| 478 | // Check that the file format is matrix market; the only format we can read right now |
| 479 | // This is not a complete solution, and fails for directories with file names etc... |
| 480 | // TODO: Should we use boost filesystem? |
| 481 | std::string strPath( filePath ); |
| 482 | if( strPath.find_last_of( '.' ) != std::string::npos ) |
| 483 | { |
| 484 | std::string ext = strPath.substr( strPath.find_last_of( '.' ) + 1 ); |
| 485 | if( ext != "mtx" ) |
| 486 | return clsparseInvalidFileFormat; |
| 487 | } |
| 488 | else |
| 489 | return clsparseInvalidFileFormat; |
| 490 | |
| 491 | MatrixMarketReader< cl_float > mm_reader; |
| 492 | if( mm_reader.MMReadFormat( filePath, read_explicit_zeroes ) ) |
| 493 | return clsparseInvalidFile; |
| 494 | |
| 495 | pCooMatx->num_rows = mm_reader.GetNumRows( ); |
| 496 | pCooMatx->num_cols = mm_reader.GetNumCols( ); |
| 497 | pCooMatx->num_nonzeros = mm_reader.GetNumNonZeroes( ); |
| 498 | |
| 499 | // Transfers data from CPU buffer to GPU buffers |
| 500 | clMemRAII< cl_float > rCooValues( control->queue( ), pCooMatx->values ); |
| 501 | clMemRAII< clsparseIdx_t > rCoocol_indices( control->queue( ), pCooMatx->col_indices ); |
| 502 | clMemRAII< clsparseIdx_t > rCoorow_indices( control->queue( ), pCooMatx->row_indices ); |
| 503 | |
| 504 | cl_float* fCooValues = rCooValues.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCooMatx->valOffset( ), pCooMatx->num_nonzeros ); |
| 505 | clsparseIdx_t* iCoocol_indices = rCoocol_indices.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCooMatx->colIndOffset( ), pCooMatx->num_nonzeros ); |
| 506 | clsparseIdx_t* iCoorow_indices = rCoorow_indices.clMapMem( CL_TRUE, CL_MAP_WRITE_INVALIDATE_REGION, pCooMatx->rowOffOffset( ), pCooMatx->num_nonzeros ); |
| 507 | |
| 508 | Coordinate< cl_float >* coords = mm_reader.GetUnsymCoordinates( ); |
| 509 | //JPA:: Coo matrix is need to be sorted as well because we need to have matrix |
| 510 | // which is sorted by row and then column, in the mtx files usually is opposite. |
| 511 | std::sort( coords, coords + pCooMatx->num_nonzeros, CoordinateCompare< cl_float > ); |
| 512 | |
| 513 | for( clsparseIdx_t c = 0; c < pCooMatx->num_nonzeros; ++c ) |
| 514 | { |
| 515 | iCoorow_indices[ c ] = coords[ c ].x; |
| 516 | iCoocol_indices[ c ] = coords[ c ].y; |
| 517 | fCooValues[ c ] = coords[ c ].val; |
| 518 | } |
| 519 | |
| 520 | return clsparseSuccess; |
| 521 | } |
| 522 | |
| 523 | clsparseStatus |
| 524 | clsparseDCooMatrixfromFile( clsparseCooMatrix* cooMatx, const char* filePath, clsparseControl control, cl_bool read_explicit_zeroes ) |