Skip to content

Commit

Permalink
Adding relaxation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanda Bienz authored and Amanda Bienz committed May 21, 2024
1 parent bce9aef commit 6ed68ed
Show file tree
Hide file tree
Showing 53 changed files with 28,735 additions and 432 deletions.
212 changes: 212 additions & 0 deletions raptor/core/tests/test_block_matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// Copyright (c) 2015-2017, RAPtor Developer Team
// License: Simplified BSD, http://opensource.org/licenses/BSD-2-Clause

#include "gtest/gtest.h"
#include "raptor.hpp"
using namespace raptor;

void compare_vals(CSRMatrix* A, BSRMatrix* B)
{
A->sort();
B->sort();
int ctr = 0;
for (int i = 0; i < B->n_rows; i++)
{
for (int k = 0; k < B->b_rows; k++)
{
for (int j = B->idx1[i]; j < B->idx1[i+1]; j++)
{
double* val = B->block_vals[j];
for (int l = 0; l < B->b_cols; l++)
{
if (fabs(val[k*B->b_cols + l]) > zero_tol)
{
ASSERT_NEAR(val[k*B->b_cols + l], A->vals[ctr++], 1e-10);
}

}
}
}
}

}

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

} // end of main() //

TEST(BlockMatrixTest, TestsInCore)
{
int block_row_size = 2;
int block_col_size = 2;
int block_size = 4;
int block_nnz = 5;
int block_num_rows = 3;
int block_num_cols = 3;
int num_rows = block_num_rows * block_row_size;
int num_cols = block_num_cols * block_col_size;
int nnz = block_nnz * block_size;

std::vector<int> rows = {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 5, 5};
std::vector<int> cols = {0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 4, 5, 4, 5};
std::vector<double> vals = {1.0, 0.0, 2.0, 1.0, 6.0, 7.0, 8.0, 2.0, 1.0, 4.0, 5.0, 1.0,
4.0, 3.0, 0.0, 0.0, 7.0, 2.0, 0.0, 0.0};

std::vector<int> block_row_ptr = {0,2,3,5};
std::vector<int> block_rows = {0, 0, 1, 2, 2};
std::vector<int> block_cols = {0, 1, 1, 1, 2};
std::vector<double*> block_vals;
for (int i = 0; i < block_nnz; i++)
{
double* block = new double[block_size];
for (int j = 0; j < block_size; j++)
{
block[j] = vals[i*block_size+j];
}
block_vals.push_back(block);
}

Matrix* A_bcoo = new BCOOMatrix(block_num_rows, block_num_cols,
block_row_size, block_col_size);
for (int i = 0; i < block_nnz; i++)
A_bcoo->add_value(block_rows[i], block_cols[i], block_vals[i]);

Matrix* A_coo = new COOMatrix(num_rows, num_cols);
for (int i = 0; i < nnz; i++)
A_coo->add_value(rows[i], cols[i], vals[i]);

Matrix* A_bsr = A_bcoo->to_CSR();
Matrix* A_csr = A_coo->to_CSR();
Matrix* A_bsc = A_bsr->to_CSC();
Matrix* A_csc = A_csr->to_CSC();
Matrix* A_csr_from_bsr = A_bsr->to_CSR();

Vector x(num_rows);
Vector b(num_cols);
Vector tmp(num_cols);
x.set_const_value(1.0);

A_bcoo->sort();
A_bcoo->move_diag();
A_bcoo->remove_duplicates();

A_bsr->sort();
A_bsr->move_diag();
A_bsr->remove_duplicates();

A_bsc->sort();
A_bsc->move_diag();
A_bsc->remove_duplicates();

ASSERT_EQ(A_bcoo->n_rows, A_bsr->n_rows);
ASSERT_EQ(A_bsr->n_rows, A_bsc->n_rows);
ASSERT_EQ(A_bcoo->n_cols, A_bsr->n_cols);
ASSERT_EQ(A_bsr->n_cols, A_bsc->n_cols);
ASSERT_EQ(A_bcoo->nnz, A_bsr->nnz);
ASSERT_EQ(A_bsr->nnz, A_bsc->nnz);
ASSERT_EQ(A_csr_from_bsr->nnz, A_csr->nnz);

double** bcoo_vals = (double**) A_bcoo->get_data();
double** bsr_vals = (double**) A_bsr->get_data();
for (int i = 0; i < A_bcoo->nnz; i++)
{
for (int j = 0; j < A_bcoo->b_size; j++)
{
ASSERT_NEAR(bcoo_vals[i][j], bsr_vals[i][j], 1e-10);
}
}

Matrix* Atmp = A_bsc->to_CSR();
Atmp->sort();
Atmp->move_diag();
double** tmp_vals = (double**) Atmp->get_data();
for (int i = 0; i < A_bsr->nnz; i++)
{
for (int j = 0; j < A_bsr->b_size; j++)
{
ASSERT_NEAR(bsr_vals[i][j], tmp_vals[i][j], 1e-10);
}
}

ASSERT_EQ(A_bcoo->format(), BCOO);
ASSERT_EQ(A_coo->format(), COO);
ASSERT_EQ(A_bsr->format(), BSR);
ASSERT_EQ(A_csr->format(), CSR);
ASSERT_EQ(A_bsc->format(), BSC);
ASSERT_EQ(A_csc->format(), CSC);
ASSERT_EQ(A_csr_from_bsr->format(), CSR);

A_csr->mult(x, b);
A_bsr->mult(x, tmp);
for (int i = 0; i < num_cols; i++)
ASSERT_NEAR(b[i], tmp[i], 1e-10);

A_csr_from_bsr->mult(x, tmp);
for (int i = 0; i < num_cols; i++)
ASSERT_NEAR(b[i], tmp[i], 1e-10);

A_coo->mult(x, tmp);
for (int i = 0; i < num_cols; i++)
ASSERT_NEAR(b[i], tmp[i], 1e-10);

A_bcoo->mult(x, tmp);
for (int i = 0; i < num_cols; i++)
ASSERT_NEAR(b[i], tmp[i], 1e-10);

A_csc->mult(x, tmp);
for (int i = 0; i < num_cols; i++)
ASSERT_NEAR(b[i], tmp[i], 1e-10);

A_bsc->mult(x, tmp);
for (int i = 0; i < num_cols; i++)
ASSERT_NEAR(b[i], tmp[i], 1e-10);


CSRMatrix* C_csr = A_csr->mult((CSRMatrix*)A_csr);
CSRMatrix* C_bsr = A_bsr->mult((BSRMatrix*)A_bsr);
ASSERT_EQ(C_csr->n_rows, C_bsr->n_rows * C_bsr->b_rows);
ASSERT_EQ(C_csr->n_cols, C_bsr->n_cols * C_bsr->b_cols);
compare_vals(C_csr, (BSRMatrix*) C_bsr);

CSRMatrix* C_csr_from_bsr = A_csr_from_bsr->mult((CSRMatrix*)A_csr_from_bsr);
ASSERT_EQ(C_csr_from_bsr->n_rows, C_bsr->n_rows * C_bsr->b_rows);
ASSERT_EQ(C_csr_from_bsr->n_cols, C_bsr->n_cols * C_bsr->b_cols);
compare_vals(C_csr_from_bsr, (BSRMatrix*) C_bsr);

CSRMatrix* D_csr = A_csr->mult_T((CSCMatrix*)A_csc);
CSRMatrix* D_bsr = A_bsr->mult_T((BSCMatrix*)A_bsc);
ASSERT_EQ(D_csr->n_rows, D_bsr->n_rows * D_bsr->b_rows);
ASSERT_EQ(D_csr->n_cols, D_bsr->n_cols * D_bsr->b_cols);
compare_vals(D_csr, (BSRMatrix*) D_bsr);

CSRMatrix* D_csr_from_bsr = A_csr_from_bsr->mult_T((CSCMatrix*)A_csc);
ASSERT_EQ(D_csr_from_bsr->n_rows, D_bsr->n_rows * D_bsr->b_rows);
ASSERT_EQ(D_csr_from_bsr->n_cols, D_bsr->n_cols * D_bsr->b_cols);
compare_vals(D_csr_from_bsr, (BSRMatrix*) D_bsr);

delete A_bsr;
delete A_csr;
delete A_bsc;
delete A_csc;
delete A_bcoo;
delete A_coo;
delete A_csr_from_bsr;

delete C_csr;
delete C_bsr;
delete C_csr_from_bsr;

delete D_csr;
delete D_bsr;
delete D_csr_from_bsr;

for (std::vector<double*>::iterator it = block_vals.begin();
it != block_vals.end(); ++it)
delete[] *it;

} // end of TEST(MatrixTest, TestsInCore) //


148 changes: 148 additions & 0 deletions raptor/core/tests/test_par_bsr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) 2015-2017, RAPtor Developer Team
// License: Simplified BSD, http://opensource.org/licenses/BSD-2-Clause

#include "gtest/gtest.h"
#include "raptor.hpp"

using namespace raptor;

int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
::testing::InitGoogleTest(&argc, argv);
int temp=RUN_ALL_TESTS();
MPI_Finalize();
return temp;

} // end of main() //

TEST(ParBSRMatrixTest, TestsInCore)
{
/*int rank, num_procs;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
std::vector<int> row_ptr = {0, 3, 5, 8, 11, 13, 16};
std::vector<int> indices = {0, 1, 4, 1, 3, 1, 2, 5, 1, 3, 4, 0, 4, 2, 4, 5};
std::vector<double> data = {1,0,2,1, 6,7,8,2, 1,0,0,1, 1,4,5,1, 2,0,0,0, 4,3,0,0,
7,2,0,0, 3,0,1,0, 1,0,0,1, 1,0,2,1, 6,7,8,2, 2,0,0,0,
1,4,5,1, 3,0,1,0, 4,3,0,0, 7,2,0,0};
std::vector<std::vector<double>> on_blocks = {{1,0,2,1}, {6,7,8,2}, {1,4,5,1},
{4,3,0,0}, {7,2,0,0}};
std::vector<std::vector<int>> on_indx = {{0,0}, {0,1}, {1,1}, {2,1}, {2,2}};
std::vector<std::vector<double>> off_blocks = {{1,0,0,1}, {2,0,0,0}, {3,0,1,0}};
std::vector<std::vector<int>> off_indx = {{0,4}, {1,3}, {2,5}};
// Create matrices for comparison
BSRMatrix* A_bsr = new BSRMatrix(12, 12, 2, 2, row_ptr, indices, data);
COOMatrix* A_coo = A_bsr->to_COO();
ParBSRMatrix* A_par_bsr = new ParBSRMatrix(12, 12, 2, 2);
// Add on_proc blocks
for (int i=0; i<on_blocks.size(); i++){
A_par_bsr->add_block(on_indx[i][0], on_indx[i][1], on_blocks[i]);
A_par_bsr->add_block(on_indx[i][0]+3, on_indx[i][1]+3, on_blocks[i]);
}
// Add off_proc blocks
for(int i=0; i<off_blocks.size(); i++){
A_par_bsr->add_block(off_indx[i][0], off_indx[i][1], off_blocks[i]);
A_par_bsr->add_block(off_indx[i][0]+3, off_indx[i][1]-3, off_blocks[i]);
}
// Finalize ParBSRMatrix and create on and off process maps
A_par_bsr->finalize(true, 2);
// Compare nnz
int lcl_nnz = A_par_bsr->local_nnz;
int nnz;
MPI_Allreduce(&lcl_nnz, &nnz, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
ASSERT_EQ(A_bsr->nnz, nnz);
// Compare n_blocks
int lcl_nblocks = A_par_bsr->on_proc->idx2.size() + A_par_bsr->off_proc->idx2.size();
int nblocks;
MPI_Allreduce(&lcl_nblocks,& nblocks, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
ASSERT_EQ(A_bsr->n_blocks, nblocks);
// Create dense matrix to compare against
std::vector<double> A_dense = A_bsr->to_dense();
// Compare row_ptrs, indices, and data
if (num_procs <= 1)
{
for (int i=0; i<A_par_bsr->on_proc->idx1.size(); i++)
{
ASSERT_EQ(A_bsr->idx1[i], A_par_bsr->on_proc->idx1[i]);
}
for (int i=0; i<A_par_bsr->on_proc->idx2.size(); i++)
{
ASSERT_EQ(A_bsr->idx2[i], A_par_bsr->on_proc->idx2[i]);
}
for (int i=0; i<A_par_bsr->on_proc->vals.size(); i++)
{
ASSERT_EQ(A_bsr->vals[i], A_par_bsr->on_proc->vals[i]);
}
}
else
{
int block_rows = A_par_bsr->b_rows;
int block_cols = A_par_bsr->b_cols;
int local_rows = A_par_bsr->local_num_rows;
for (int i = 0; i < local_rows/block_rows; i++)
{
int start = A_par_bsr->on_proc->idx1[i];
int end = A_par_bsr->on_proc->idx1[i+1];
for (int j = start; j < end; j++)
{
int upper_i = A_par_bsr->local_row_map[i*block_rows];
int upper_j = A_par_bsr->on_proc_column_map[(A_par_bsr->on_proc->idx2[j])*block_cols];
int data_offset = j * block_rows * block_cols;
for (int bi = 0; bi < block_rows; bi++)
{
for (int bj = 0; bj < block_cols; bj++)
{
int glob_i = upper_i + bi;
int glob_j = upper_j + bj;
int ind = bi * block_cols + bj + data_offset;
double val = A_par_bsr->on_proc->vals[ind];
int glob_ind = glob_i*12+glob_j;
ASSERT_NEAR(A_dense[glob_ind], val, zero_tol);
}
}
}
start = A_par_bsr->off_proc->idx1[i];
end = A_par_bsr->off_proc->idx1[i+1];
for (int j = start; j < end; j++)
{
int upper_i = A_par_bsr->local_row_map[i*block_rows];
int upper_j = A_par_bsr->off_proc_column_map[(A_par_bsr->off_proc->idx2[j])*block_cols];
int data_offset = j * block_rows * block_cols;
for (int bi = 0; bi < block_rows; bi++)
{
for (int bj = 0; bj < block_cols; bj++)
{
int glob_i = upper_i + bi;
int glob_j = upper_j + bj;
int ind = bi * block_cols + bj + data_offset;
int glob_ind = glob_i*12+glob_j;
double val = A_par_bsr->off_proc->vals[ind];
ASSERT_NEAR(A_dense[glob_i*12+glob_j], val, zero_tol);
}
}
}
}
}
// Delete pointers
delete A_par_bsr;
delete A_bsr;
*/

} // end of TEST(ParMatrixTest, TestsInCore) //
Loading

0 comments on commit 6ed68ed

Please sign in to comment.