BASKER_INLINE int BaskerMatrix<Int,Entry,Exe_Space>::fill() { if(v_fill == BASKER_TRUE) return -1; for(Int i = 0; i < ncol+1; i++) { col_ptr(i) = 0; } for(Int i = 0; i < nnz; i++) { row_idx(i) = 0; } for(Int i = 0; i < nnz; i++) { val(i) = 0; } //#ifdef BASKER_INC_LVL if(inc_lvl_flg == BASKER_TRUE) { for(Int i = 0; i < nnz; i++) { inc_lvl(i) = 0; } } //#endif v_fill = BASKER_TRUE; return 0; }
inline DMatrixSlice DMatrix::operator()(size_type i1, size_type i2, size_type j1 , size_type j2) { return DMatrixSlice( col_ptr(1) + (i1 - 1) + (j1 - 1) * rows() , max_rows() * cols(), max_rows(), i2 - i1 + 1, j2 - j1 + 1 ); }
BASKER_INLINE void BaskerMatrix<Int,Entry,Exe_Space>::print() { printf("\n Matrix Print: \n Label: %s sr: %d sc: %d \n", label.c_str(), srow, scol); printf("ncol: %d nnz: %d \n", ncol, nnz); printf(" col_ptr: "); for(Int i=0; i < ncol+1; i++) { printf(" %d , ", col_ptr(i)); } printf("\n row_idx: "); for(Int i=0; i < nnz; i++) { printf(" %d , ", row_idx(i)); } printf("\n val: "); for(Int i=0; i < nnz; i++) { printf(" %e , " , val(i)); } printf("\n END PRINT \n"); }//end print()
inline const DMatrixSlice DMatrix::operator()(size_type i1, size_type i2, size_type j1 , size_type j2) const { return DMatrixSlice( const_cast<value_type*>(col_ptr(1)) + (i1 - 1) + (j1 - 1) * rows() , max_rows() * cols(), max_rows(), i2 - i1 + 1, j2 - j1 + 1 ); }
inline const DMatrixSlice DMatrix::operator()(const Range1D& I, const Range1D& J) const { Range1D Ix = RangePack::full_range(I,1,rows()), Jx = RangePack::full_range(J,1,cols()); return DMatrixSlice( const_cast<value_type*>(col_ptr(1)) + (Ix.lbound() - 1) + (Jx.lbound() - 1) * rows() , max_rows() * cols(), max_rows(), Ix.size(), Jx.size() ); }
BASKER_INLINE void BaskerMatrix<Int,Entry,Exe_Space>::print() { std::cout << "\n Matrix Print: \n Label: " << label.c_str() << " sr: " << srow << " sc: " << scol << "\n ncol: " << ncol << " nnz: " << nnz << std::endl; std::cout << " col_ptr: "; for(Int i=0; i < ncol+1; i++) { std::cout << col_ptr(i) << " , "; } std::cout << "\n row_idx: "; for(Int i=0; i < nnz; i++) { std::cout << row_idx(i) << " , "; } std::cout << "\n val: "; for(Int i=0; i < nnz; i++) { std::cout << val(i) << " , "; } std::cout << "\n END PRINT " << std::endl; }//end print()
// TODO: Use utility class here void MathUtilities::deserialize( SparseMatrixsc& A, std::istream& stm ) { assert( stm.good() ); VectorXi col_ptr; VectorXi row_ind; VectorXs val; // Read the number of rows in the matrix int rows{ -1 }; stm.read((char*)&rows,sizeof(int)); assert( rows >= 0 ); // Read the number of columns in the matrix int cols{ -1 }; stm.read((char*)&cols,sizeof(int)); assert( cols >= 0 ); // Read the column pointer array col_ptr.resize(cols+1); stm.read((char*)col_ptr.data(),col_ptr.size()*sizeof(int)); // Read the number of nonzeros in the array int nnz{ -1 }; stm.read((char*)&nnz,sizeof(int)); assert( nnz >= 0 ); // Read the row-index array row_ind.resize(nnz); stm.read((char*)row_ind.data(),row_ind.size()*sizeof(int)); // Read the value array val.resize(nnz); stm.read((char*)val.data(),val.size()*sizeof(scalar)); A.resize(rows,cols); A.reserve(nnz); for( int col = 0; col < cols; ++col ) { A.startVec(col); for( int curel = col_ptr(col); curel < col_ptr(col+1); ++curel ) { int row = row_ind(curel); scalar curval = val(curel); A.insertBack(row,col) = curval; } } A.finalize(); }
BASKER_INLINE Int BaskerMatrixView<Int,Entry,Exe_Space>::nnz() { Int _nnz = 0; for(Int k=scol; k<scol+ncol; k++) { for(Int j = col_ptr(k); j < col_ptr(k+1); j++) { if(good(j) == 0) { _nnz++; } } }//over all columns return _nnz; }
BASKER_INLINE void BaskerMatrix<Int,Entry,Exe_Space>::clean_col() { for(Int i = 0; i < ncol+1; ++i) { col_ptr(i) = (Int) BASKER_MAX_IDX; } nnz = 0; mnnz = 0; }//end clean_col()
BASKER_INLINE void BaskerMatrix<Int, Entry, Exe_Space>::init_col() { BASKER_ASSERT(ncol >= 0, "INIT_COL, ncol > 0"); MALLOC_INT_1DARRAY(col_ptr, ncol+1); for(Int i = 0; i < ncol+1; ++i) { col_ptr(i) = (Int) BASKER_MAX_IDX; } }//end init_col()
BASKER_INLINE void BaskerMatrix<Int,Entry,Exe_Space>::convert2D ( BASKER_MATRIX &M, BASKER_BOOL alloc, Int kid ) { if(nnz == 0) { for(Int i = 0; i < ncol+1; i++) { col_ptr(i) = 0; } MALLOC_INT_1DARRAY(row_idx, 1); row_idx(0) = (Int) 0; MALLOC_ENTRY_1DARRAY(val, 1); val(0) = (Entry) 0; return; } //info(); //We could check some flag ?? //We assume a pre-scan has already happened if(alloc == BASKER_TRUE) { //printf("ALLOC\n"); if(nnz > 0) { BASKER_ASSERT(nnz > 0, "matrix row nnz 2"); MALLOC_INT_1DARRAY(row_idx, nnz); } else if(nnz ==0) { BASKER_ASSERT((nnz+1)>0, "matrix row nnz 3"); MALLOC_INT_1DARRAY(row_idx, nnz+1); } } //init_value(row_idx, nnz, (Int) 0); //printf("clear row: %d \n", nnz); for(Int i = 0; i < nnz; ++i) { //printf("clear row_idx(%d) \n", i); row_idx(i) = 0; } if(alloc == BASKER_TRUE) { if(nnz > 0) { BASKER_ASSERT(nnz > 0, "matrix nnz 4"); MALLOC_ENTRY_1DARRAY(val, nnz); } else if(nnz == 0) { BASKER_ASSERT((nnz+1) > 0, "matrix nnz 5"); MALLOC_ENTRY_1DARRAY(val, nnz+1); } } //init_value(val, nnz, (Entry) 0); for(Int i = 0; i < nnz; ++i) { val(i) = 0; } Int temp_count = 0; for(Int k = scol; k < scol+ncol; ++k) { //note col_ptr[k-scol] contains the starting index if(col_ptr(k-scol) == BASKER_MAX_IDX) { col_ptr(k-scol) = temp_count; //printf("continue called, k: %d \n", k); continue; } for(Int i = col_ptr(k-scol); i < M.col_ptr(k+1); i++) { Int j = M.row_idx(i); //printf("i: %d j:%d \n", i,j); if(j >= srow+nrow) { break; } //printf("writing row_dix: %d i: %d val: %d nnz: %d srow: %d nrow: %d \n", // temp_count, i, j, nnz, // srow, nrow); //BASKER_ASSERT(temp_count < nnz, "2DConvert, too many values"); //row_idx[temp_count] = j; if(j-srow <0) { std::cout << "kid: " << kid << " j: " << j << " srow: " << srow << " k: " << k << " idx: " << i << std::endl; BASKER_ASSERT(0==1, "j-srow NO"); } row_idx(temp_count) = j-srow; val(temp_count) = M.val(i); temp_count++; } col_ptr(k-scol) = temp_count; } //col_ptr[0] = 0; //NO!!1 //Slide over the col counts for(Int i = ncol; i > 0; i--) { col_ptr(i) = col_ptr(i-1); } col_ptr(0) = (Int) 0; //info(); //print(); }//end convert2d(Matrix)
inline const DVectorSlice DMatrix::row(size_type i) const { validate_row_subscript(i); return DVectorSlice( const_cast<value_type*>(col_ptr(1)) + (i-1), cols(), rows() ); }
inline DMatrixSlice DMatrix::operator()() { return DMatrixSlice( col_ptr(1), max_rows() * cols(), max_rows(), rows(), cols() ); }
inline const DVectorSlice DMatrix::col(size_type j) const { validate_col_subscript(j); return DVectorSlice( const_cast<value_type*>(col_ptr(1)) + (j-1) * rows(), rows(), 1 ) ; }
inline DVectorSlice DMatrix::col(size_type j) { validate_col_subscript(j); return DVectorSlice( col_ptr(1) + (j-1) * rows(), rows(), 1 ); }
inline const DMatrixSlice DMatrix::operator()() const { return DMatrixSlice( const_cast<value_type*>(col_ptr(1)), max_rows() * cols(), max_rows() , rows(), cols() ); }
inline DVectorSlice DMatrix::row(size_type i) { validate_row_subscript(i); return DVectorSlice( col_ptr(1) + (i-1), cols(), rows() ); }