how to convert block compressed row to dense matrix?

Viewed 609

I'm interesting to create a class for storing sparse matrix in Block Compressed Sparse Row format BCSR format this method of storage consist to subdivide the matrix into square block of size sz*sz and stored this block in a vector BA , here you can find most information about link basically the matrix is stored using 4 vector :

  • BA contains the elements of the submatrices (blocks) stored in top-down left right order (the first block in the picture of size 2x2 is 11,12,0,22)
  • AN contains the indices of each starting block of the vector BA (in the pictur case the block size is 2x2 so it contains 1,5 ... )
  • AJ contains the column index of blocks in the matrix of blocks (the smaller one in the picture)

  • AI the row pointer vector , it store how many blocks there is in the i-th row ai[i+1]-a[i] = number of block in i-th row

I'm write the constructor for convert a matrix from dense format to BCRS format :

template <typename data_type, std::size_t SZ = 2 >
class BCSRmatrix {

   public:

     constexpr BCSRmatrix(std::initializer_list<std::vector<data_type>> dense );  

    auto constexpr validate_block(const std::vector<std::vector<data_type>>& dense,
                                  std::size_t i, std::size_t j) const noexcept ; 

     auto constexpr insert_block(const std::vector<std::vector<data_type>>& dense,
                                                       std::size_t i, std::size_t j) noexcept ;

  private:

    std::size_t bn  ;
    std::size_t bSZ ;
    std::size_t nnz ;
    std::size_t denseRows ;
    std::size_t denseCols ;

    std::vector<data_type>    ba_ ; 
    std::vector<std::size_t>  an_ ;
    std::vector<std::size_t>  ai_ ;
    std::vector<std::size_t>  aj_ ;


    std::size_t index =0 ;

};

template <typename T, std::size_t SZ>
constexpr BCSRmatrix<T,SZ>::BCSRmatrix(std::initializer_list<std::vector<T>> dense_ )
{
      this->denseRows = dense_.size();   
      auto it         = *(dense_.begin());
      this->denseCols = it.size();

      if( (denseRows*denseCols) % SZ != 0 )
      {
            throw InvalidSizeException("Error block size is not multiple of dense matrix size");
      }

     std::vector<std::vector<T>> dense(dense_);
     bSZ = SZ*SZ ;  
     bn  = denseRows*denseCols/(SZ*SZ) ;
     ai_.resize(denseRows/SZ +1);
    ai_[0] = 1;

    for(std::size_t i = 0; i < dense.size() / SZ ; i++)
    {    
        auto rowCount =0;
        for(std::size_t j = 0; j < dense[i].size() / SZ ; j++)
        {
            if(validate_block(dense,i,j))
            {     
                  aj_.push_back(j+1);
                  insert_block(dense, i, j);
                  rowCount ++ ;
            }      

        }
        ai_[i+1] = ai_[i] + rowCount ;
     }
     printBCSR();
}

template <typename T,std::size_t SZ>
inline auto constexpr BCSRmatrix<T,SZ>::validate_block(const std::vector<std::vector<T>>& dense,
                                                       std::size_t i, std::size_t j) const noexcept
{   
   bool nonzero = false ;
   for(std::size_t m = i * SZ ; m < SZ * (i + 1); ++m)
   {
      for(std::size_t n = j * SZ ; n < SZ * (j + 1); ++n)
      {
            if(dense[m][n] != 0) nonzero = true;
      }
   }

   return nonzero ;
}

template <typename T,std::size_t SZ>
inline auto constexpr BCSRmatrix<T,SZ>::insert_block(const std::vector<std::vector<T>>& dense,
                                                       std::size_t i, std::size_t j) noexcept
{   
   //std::size_t value = index;   
   bool firstElem = true ;
   for(std::size_t m = i * SZ ; m < SZ * (i + 1); ++m)
   {
      for(std::size_t n = j * SZ ; n < SZ * (j + 1); ++n)
      {    
            if(firstElem)
            {
                  an_.push_back(index+1);
                  firstElem = false ;
            }
            ba_.push_back(dense[m][n]);
            index ++ ;
      }
   }


template <typename T, std::size_t SZ>
auto constexpr BCSRmatrix<T,SZ>::printBCSR() const noexcept 
{ 

  std::cout << "ba_ :   " ;
  for(auto &x : ba_ ) 
      std::cout << x << ' ' ;
    std::cout << std::endl; 

  std::cout << "an_ :   " ;
  for(auto &x : an_ ) 
      std::cout <<  x << ' ' ;
    std::cout << std::endl; 

  std::cout << "aj_ :   " ;
  for(auto &x : aj_ ) 
      std::cout <<  x << ' ' ;
    std::cout << std::endl; 

   std::cout << "ai_ :   " ; 
   for(auto &x : ai_ ) 
      std::cout << x << ' ' ;
    std::cout << std::endl; 

}

And the main function for test the class :

    # include "BCSRmatrix.H"

    using namespace std;

    int main(){ 
     BCSRmatrix<int,2> bbcsr2 = {{11,12,0,0,0,0,0,0} ,{0,22,0,0,0,0,0,0} ,{31,32,33,0,0,0,0,0},
                              {41,42,43,44,0,0,0,0}, {0,0,0,0,55,56,0,0},{0,0,0,0,0,66,67,0},{0,0,0,0,0,0,77,78},{0,0,0,0,0,0,87,88}};
     BCSRmatrix<int,4> bbcsr3 = {{11,12,0,0,0,0,0,0} ,{0,22,0,0,0,0,0,0} ,{31,32,33,0,0,0,0,0},
                              {41,42,43,44,0,0,0,0}, {0,0,0,0,55,56,0,0},{0,0,0,0,0,66,67,0},{0,0,0,0,0,0,77,78},{0,0,0,0,0,0,87,88}};
  return 0;
}

Now back to the question .. I obtain the 4 vector as in the picture .. but what about backing from this 4 vector to the dense matrix ? for example how to print out the whole matrix ?

Edit : I've figure out the way to plot the "blocks matrix" the smaller in the picture with relative index of vector AN:

    template <typename T,std::size_t SZ>
    inline auto constexpr BCSRmatrix<T,SZ>::printBlockMatrix() const noexcept  
    {

          for(auto i=0 ; i < denseRows / SZ ; i++)
          {
            for(auto j=1 ; j <= denseCols / SZ  ; j++)
            {
                std::cout << findBlockIndex(i,j) << ' ' ;  
            }
             std::cout << std::endl;   
          }
    }

template <typename T, std::size_t SZ> 
auto constexpr BCSRmatrix<T,SZ>::findBlockIndex(const std::size_t r, const std::size_t c) const noexcept 
{
      for(auto j= ai_.at(r) ; j < ai_.at(r+1) ; j++ )
      {   
         if( aj_.at(j-1) == c  )
         {
            return j ;
         }

      }
}

that when in the main I call :

bbcsr3.printBlockMatrix();

Give me the right result :

1 0 0 0 
2 3 0 0 
0 0 4 5 
0 0 0 6 

Now just the whole matrix missing I think that I missed something in may mind .. but should be something easy but I didn't got the point .. any ideas ?

1 Answers

what about backing from this 4 vector to the dense matrix ? for example how to print out the whole matrix ?

Back to the sparse matrix:

template <typename T, std::size_t SZ> 
auto constexpr BCSRmatrix<T,SZ>::recomposeMatrix() const noexcept {

    std::vector<std::vector<data_type>> sparseMat(denseRows, std::vector<data_type>(denseCols, 0));
    auto BA_i = 0, AJ_i = 0;
    //for each BCSR row
    for(auto r = 0; r < denseRows/SZ; r++){
        //for each Block in row
        for(auto nBlock = 0; nBlock < ai_.at(r+1)-ai_.at(r); nBlock++){  
            //for each subMatrix (Block)
            for(auto rBlock = 0; rBlock < SZ; rBlock++){
                for(auto cBlock = 0; cBlock < SZ; cBlock++){
                    //insert value
                    sparseMat.at(rBlock + r*SZ).at(cBlock + (aj_.at(AJ_i)-1)*SZ) = ba_.at(BA_i);
                    ++BA_i;
                }
            }
        ++AJ_i;
        }
    }
    return sparseMat;
}

Where: BA_i and AJ_i are iterators of the respective vectors.

nBlock keeps the numbers of blocks in row given by ai_.

rBlock and cBlockare the iterators of the sub-matrix sz*sz called "Block".

note: an_ remain unused, you can try replacing BA_i whit it.

Print the matrix:

std::vector<std::vector<int>> sparseMat = bbcsr2.recomposeMatrix();
for(auto i = 0; i < sparseMat.size(); i++){
    for(auto j = 0; j < sparseMat.at(i).size(); j++)
        std::cout<<sparseMat.at(i).at(j) << '\t';
    std::cout << std::endl;
}

I'm not sure I wrote the template correctly, anyway the algorithm should work; let me know if there are problems.


EDIT

make sense in a class that is created for saving time and memory storing sparse matrix it certain way than use a vector for reconstruct the whole matrix ?

You're right, my fault; I thought the problem was recompose the Matrix. I rewritten the methods using findBlockIndex as a reference.

template <typename T, std::size_t SZ> 
auto constexpr BCSRmatrix<T,SZ>::printSparseMatrix() const noexcept {      
    //for each BCSR row
    for(auto i=0 ; i < denseRows / SZ ; i++){
        //for each Block sub row.
        for(auto rBlock = 0; rBlock < SZ; rBlock++){
            //for each BCSR col.
            for(auto j = 1; j <= denseCols / SZ; j++){
                //for each Block sub col.
                for(auto cBlock = 0; cBlock < SZ; cBlock++){
                    std::cout<< findValue(i, j, rBlock, cBlock) <<'\t';
                }
            }
            std::cout << std::endl;
        }
    }
}

template <typename T, std::size_t SZ> 
auto constexpr BCSRmatrix<T,SZ>::findValue(const std::size_t i, const std::size_t j, const std::size_t rBlock, const std::size_t cBlock) const noexcept {

    auto index = findBlockIndex(i,j);
    if(index != 0)
        return ba_.at(an_.at(index-1)-1 + cBlock + /* rBlock*2 */ rBlock*SZ);
}    

I hope to be of help to you, best regards Marco.

Related