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 1b9c0fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
19 changes: 15 additions & 4 deletions matrix_powers_mv/matrix_powers_mv.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "matrix_powers_mv.h"
#include <cstdint>
#include <cstring>

int ReorderMatrix(const SparseMatrix<double>& sp_matrix) {
bool ReorderMatrix(SparseMatrix<double>& sp_matrix, idx_t* perm, idx_t* iperm) {
SparseMatrix<double> a_no_diag;
sp_matrix.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_];
perm = new idx_t[a_no_diag.row_cnt_];
iperm = new idx_t[a_no_diag.row_cnt_];
idx_t options[METIS_NOPTIONS];
METIS_SetDefaultOptions(options);
options[METIS_OPTION_NUMBERING] = 0;
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(double));
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, idx_t* perm, idx_t* iperm);
void MatrixPowersMV(const SparseMatrix<double>& sp_matrix, const Vector<double>& x, const Vector<Vector<double>*>& res);
25 changes: 3 additions & 22 deletions matrix_powers_mv/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
static void Test(const uint32_t n) {
SparseMatrix<double> a_no_diag;
SparseMatrix<double> a;
idx_t* perm{nullptr};
idx_t* iperm{nullptr};
{
std::stringstream file_name;
file_name << std::filesystem::current_path().string() << "/../" << "matrix_examples/sparse_spd/" << n;
Expand All @@ -21,29 +23,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, perm, iperm));
}

TEST_CASE("Size 128") {
Expand Down

0 comments on commit 1b9c0fb

Please sign in to comment.