Skip to content

Commit

Permalink
Add reordering function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ypodlesov committed May 19, 2024
1 parent f8ddea6 commit 0bf782c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
15 changes: 13 additions & 2 deletions matrix_powers_mv/matrix_powers_mv.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "matrix_powers_mv.h"
#include <cstdint>
#include <cstring>

int ReorderMatrix(const SparseMatrix<double>& sp_matrix) {
bool ReorderMatrix(SparseMatrix<double>& sp_matrix) {
SparseMatrix<double> a_no_diag;
sp_matrix.RemoveDiag(a_no_diag);
idx_t* perm = new idx_t[a_no_diag.row_cnt_];
Expand All @@ -20,7 +22,16 @@ int ReorderMatrix(const SparseMatrix<double>& 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<double>& /* sp_matrix */, const Vector<double>& /* x */, const Vector<Vector<double>*>& /* res */) {
Expand Down
2 changes: 1 addition & 1 deletion matrix_powers_mv/matrix_powers_mv.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#include <sparse_matrix.h>
#include <vector.h>

int ReorderMatrix(const SparseMatrix<double>& sp_matrix);
bool ReorderMatrix(SparseMatrix<double>& sp_matrix);
void MatrixPowersMV(const SparseMatrix<double>& sp_matrix, const Vector<double>& x, const Vector<Vector<double>*>& res);
23 changes: 1 addition & 22 deletions matrix_powers_mv/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,8 @@ static void Test(const uint32_t n) {
fstream >> a;
fstream.close();
REQUIRE(a.data_);
SparseMatrix<double> 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") {
Expand Down

0 comments on commit 0bf782c

Please sign in to comment.