A simple question about symmetric matrix
Rhythmicc opened this issue · 5 comments
I used a symmetric matrix file to test this, called as follows:
namespace fmm = fast_matrix_market;
template <typename IT, typename VT>
struct triplet_matrix
{
int64_t nrows = 0, ncols = 0;
std::vector<IT> rows;
std::vector<IT> cols;
std::vector<VT> vals;
};
// read mtx file to csr
template <typename IT, typename VT>
void read_mtx_to_csr(const std::string &filename, int&m, int&n, int&nnz, int **row_ptr, int **col_ind, double **val)
{
std::ifstream file(filename);
triplet_matrix<IT, VT> mtx;
fast_matrix_market::read_options options;
options.parallel_ok = true;
fmm::read_matrix_market_triplet(
file,
mtx.nrows, mtx.ncols,
mtx.rows, mtx.cols, mtx.vals, options
);
...
}
I noticed that the elements on the diagonal are also copied and the value is set to 0; does fmm provide a way to avoid this problem?
Good question.
As of today there are three ways to handle diagonal elements of symmetric matrices:
- the default behavior: create two elements, the second is zero
- create two elements, both identical
- turn off FMM symmetry handling altogether and do it yourself
These are controlled by read_options::generalize_symmetry
and read_options::generalize_coordinate_diagnonal_values
, see
There is no "just one element" option at the moment because such an option is more tricky to implement than it seems, largely because there is no way to know how many diagonal elements there are without reading the entire file.
A common next step after loading is to convert to something like CSR, with a sum of duplicate elements. Hence the default behavior works well for that.
If you don't allow explicit zeros, then use your library's method to drop those. Again, that's often called anyway since the matrix market format allows explicit zeros.
If neither of those apply, then you can do what the Python bindings do. Disable FMM symmetry generalization, then do it yourself. Since you know your datastructure, you can iterate it to count how many elements need to be added, resize the matrix in whatever way your datastructure requires, and duplicate the non-diagonal elements.
Though having written that last paragraph and scanned through the code a little, it seems possible to add that feature for most already supported bindings, including the one you're using. I'll have a crack at it.
Thank you very much for your reply, I did in fact convert the read to CSR format and skipped the handling of the zero elements. What I'm thinking is that there has to be a place for FMM or user call code to add additional judgment conditions, and perhaps a configuration option could be added to weed out the extra zero elements.
I added an option to handle symmetric files at the binding level, and nearly all bindings (including the triplet one you're using) no longer create those zero elements.
@Rhythmicc The change is in main
and on by default, would you be able to test that it works for you?
Sure, and my test results were fine.