Skip to content

Commit

Permalink
Unit tests for Matrix rescale methods (#240)
Browse files Browse the repository at this point in the history
* fix issue with multithreaded running

* add unit tests for rescale_cols_max

* add unit tests for rescale_rows_max
  • Loading branch information
cval26 authored Nov 1, 2023
1 parent 45b7832 commit 2b31c41
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/linalg/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ Matrix::rescale_cols_max()

// Get the max across all processes, if applicable.
double global_max[d_num_cols];
if (d_num_procs > 1)
if (d_distributed && d_num_procs > 1)
{
MPI_Allreduce(&local_max, &global_max, d_num_cols, MPI_DOUBLE, MPI_MAX,
MPI_COMM_WORLD);
Expand Down
116 changes: 116 additions & 0 deletions unit_tests/test_Matrix.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,122 @@ TEST(MatrixSerialTest, Test_get_first_n_columns)
EXPECT_DOUBLE_EQ(truncated_matrix->item(3, 1), 13.0);
}

TEST(MatrixSerialTest, Test_Matrix_rescale_rows_max)
{
// Matrix data to rescale.
double d_mat[12] = { 3.0, 5.0, 0.0,
-3.0, -2.0, -1.0,
-1.0, 3.0, -2.0,
2.0, -2.5, 0.5
};

// Target matrix data.
double d_mat2[12] = { 0.6, 1.0, 0.0,
-1.0, -2.0/3.0, -1.0/3.0,
-1.0/3.0, 1.0, -2.0/3.0,
2.0/2.5, -1.0, 0.5/2.5
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_rows_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_Matrix_rescale_rows_max2)
{
// Matrix data to rescale.
double d_mat[12] = { 0.0, -1.0e-16, 1.0e-15,
-3.0, -2.0, -1.0,
-1.0, 3.0, -2.0,
2.0, -2.5, 0.5
};

// Target matrix data.
double d_mat2[12] = { 0.0, -1.0e-16, 1.0e-15,
-1.0, -2.0/3.0, -1.0/3.0,
-1.0/3.0, 1.0, -2.0/3.0,
2.0/2.5, -1.0, 0.5/2.5
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_rows_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_Matrix_rescale_cols_max)
{
// Matrix data to rescale.
double d_mat[12] = { 3.0, 5.0, 0.0,
-3.0, -2.0, -1.0,
-1.0, 3.0, -2.0,
2.0, -2.5, 0.5
};

// Target matrix data.
double d_mat2[12] = { 1.0, 1.0, 0.0,
-1.0, -0.4, -0.5,
-1.0/3.0, 0.6, -1.0,
2.0/3.0, -0.5, 0.25
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_cols_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_Matrix_rescale_cols_max2)
{
// Matrix data to rescale.
double d_mat[12] = { 3.0, 5.0, 1.0e-15,
-3.0, -2.0, 0.0,
-1.0, 3.0, -1.0e-16,
2.0, -2.5, 0.0
};

// Target matrix data.
double d_mat2[12] = { 1.0, 1.0, 1.0e-15,
-1.0, -0.4, 0.0,
-1.0/3.0, 0.6, -1.0e-16,
2.0/3.0, -0.5, 0.0
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_cols_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_pMatrix_mult_reference)
{
/**
Expand Down

0 comments on commit 2b31c41

Please sign in to comment.