From 0bf782cd079910151614fb1ff9269e904fe8bda2 Mon Sep 17 00:00:00 2001 From: ypodlesov Date: Sun, 19 May 2024 13:56:06 +0000 Subject: [PATCH] Add reordering function. --- matrix_powers_mv/matrix_powers_mv.cpp | 15 +++++++++++++-- matrix_powers_mv/matrix_powers_mv.h | 2 +- matrix_powers_mv/test.cpp | 23 +---------------------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/matrix_powers_mv/matrix_powers_mv.cpp b/matrix_powers_mv/matrix_powers_mv.cpp index 20cf8e8..77c6c72 100644 --- a/matrix_powers_mv/matrix_powers_mv.cpp +++ b/matrix_powers_mv/matrix_powers_mv.cpp @@ -1,6 +1,8 @@ #include "matrix_powers_mv.h" +#include +#include -int ReorderMatrix(const SparseMatrix& sp_matrix) { +bool ReorderMatrix(SparseMatrix& sp_matrix) { SparseMatrix a_no_diag; sp_matrix.RemoveDiag(a_no_diag); idx_t* perm = new idx_t[a_no_diag.row_cnt_]; @@ -20,7 +22,16 @@ int ReorderMatrix(const SparseMatrix& sp_matrix) { , perm , iperm ); - return result; + if (result != METIS_OK) { + return false; + } + for (int64_t i = 0; i < sp_matrix.row_cnt_; ++i) { + int64_t cur_row_cnt = sp_matrix.i_a_[i + 1] - sp_matrix.i_a_[i]; + std::memcpy(&sp_matrix.data_[sp_matrix.i_a_[i]], &sp_matrix.data_[sp_matrix.i_a_[perm[i]]], cur_row_cnt * sizeof(double)); + std::memcpy(&sp_matrix.j_a_[sp_matrix.i_a_[i]], &sp_matrix.j_a_[sp_matrix.i_a_[perm[i]]], cur_row_cnt * sizeof(int64_t)); + sp_matrix.i_a_[i] = sp_matrix.i_a_[perm[i]]; + } + return true; } void MatrixPowersMV(const SparseMatrix& /* sp_matrix */, const Vector& /* x */, const Vector*>& /* res */) { diff --git a/matrix_powers_mv/matrix_powers_mv.h b/matrix_powers_mv/matrix_powers_mv.h index 16a4836..fe9e382 100644 --- a/matrix_powers_mv/matrix_powers_mv.h +++ b/matrix_powers_mv/matrix_powers_mv.h @@ -4,5 +4,5 @@ #include #include -int ReorderMatrix(const SparseMatrix& sp_matrix); +bool ReorderMatrix(SparseMatrix& sp_matrix); void MatrixPowersMV(const SparseMatrix& sp_matrix, const Vector& x, const Vector*>& res); \ No newline at end of file diff --git a/matrix_powers_mv/test.cpp b/matrix_powers_mv/test.cpp index 6747b27..2f17d63 100644 --- a/matrix_powers_mv/test.cpp +++ b/matrix_powers_mv/test.cpp @@ -21,29 +21,8 @@ static void Test(const uint32_t n) { fstream >> a; fstream.close(); REQUIRE(a.data_); - SparseMatrix a_no_diag; - a.RemoveDiag(a_no_diag); - idx_t* perm = new idx_t[a_no_diag.row_cnt_]; - idx_t* iperm = new idx_t[a_no_diag.row_cnt_]; - idx_t options[METIS_NOPTIONS]; - METIS_SetDefaultOptions(options); - options[METIS_OPTION_NUMBERING] = 0; - // int METIS_NodeND(idx_t *nvtxs, idx_t *xadj, idx_t *adjncy, idx_t *vwgt, - // idx_t *options, idx_t *perm, idx_t *iperm); - - int result = METIS_NodeND( - &a_no_diag.row_cnt_ - , a_no_diag.i_a_ - , a_no_diag.j_a_ - , nullptr - , options - , perm - , iperm - ); - REQUIRE(result == METIS_OK); } - - // REQUIRE(ReorderMatrix(a_no_diag) == METIS_OK); + REQUIRE(ReorderMatrix(a)); } TEST_CASE("Size 128") {