简体   繁体   中英

sparse matrix Coordinate Storage format: convert from row-major to column-major

I have two c++ function (foo, goo) operating with sparse matrix in Coordinate Storage Format, that is, the matrix is given as 3 arrays: row_index[nnz], column_index[nnz], value[nnz] where nnz is the number of non-zero elements.

foo returns the sparse matrix "by row-major order", as example:

  • 1 1 4.000000
  • 1 2 4.000000
  • 2 1 6.000000
  • 2 3 8.000000
  • 3 3 10.000000

goo, instead, need the vector to by sorted "by column-major order", that is:

  • 1 1 4.000000
  • 2 1 6.000000 //this is changed
  • 1 2 4.000000 //this is changed
  • 2 3 8.000000
  • 3 3 10.000000

How can I make this conversion in the most efficient way?

Additional info: goo also supports Compressed Column format.

if you have control over the data structure over execution, a clean, efficient way would be to store the format as array of structs, then sort wrt to the relevant column, for example

typedef std::tuple<size_t,size_t,double> elem_t;
// std::get<0>(storage[i]) is the row index of the i-th non-zero
// std::get<1>(storage[i]) is the col index of the i-th non-zero
// std::get<2>(storage[i]) is the value of the i-th non-zero
std::vector<elem_t> storage;
// this sort can be parallel
std::sort(storage.begin(),storage.end(),[](const elem_t& L, const elem_t& R){return std::get<1>(L)<std::get<1>(R);});

If not, one can write a functor to do index sort according to the column, and permute afterwards. This is of course a lot more messy and would incur memory overhead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM