Skip to content

Commit

Permalink
Added BSRMatrix(CSRMatrix) constructor, need to add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanda Bienz authored and Amanda Bienz committed May 22, 2024
1 parent 3d34a3d commit 80357b8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 100 deletions.
37 changes: 37 additions & 0 deletions raptor/core/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,43 @@ class BSRMatrix : public CSRMatrix
init_from_lists(rowptr, cols, data);
}

BSRMatrix(CSRMatrix* A, int block_row_size, int block_col_size) : CSRMatrix(A->n_rows / block_row_size, A->n_cols / block_col_size, 0)
{
b_rows = block_row_size;
b_cols = block_col_size;
b_size = b_rows * b_cols;

// Convert CSR to BSR
std::vector<int> idx(A->n_cols, -1);
for (int bsr_row = 0; bsr_row < n_rows; bsr_row++)
{
for (int block = 0; block < b_rows; block++)
{
int csr_row = bsr_row+block;

for (int j = A->idx1[csr_row]; j < A->idx1[csr_row+1]; j++)
{
int csr_col = A->idx2[j];
int bsr_col = csr_col / b_rows;
if (idx[bsr_col] != -1)
{
idx[bsr_col] = idx2.size();
idx2.push_back(bsr_col);
block_vals.push_back(new double[b_size]());
}
int idx_row = csr_row % b_rows;
int idx_col = csr_col % b_cols;
block_vals[idx[bsr_col]][idx_row*b_rows + idx_col] = A->vals[j];
}
}
idx1[bsr_row+1] = idx2.size();

// Reset IDX array for next BSR row
for (int j = idx1[bsr_row]; j < idx1[bsr_row+1]; j++)
idx[idx2[j]] = -1;
}
}


BSRMatrix() : CSRMatrix()
{
Expand Down
1 change: 1 addition & 0 deletions raptor/util/linalg/relax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "raptor/core/vector.hpp"
#include "raptor/core/matrix.hpp"
#include "raptor/multilevel/level.hpp"
#include <cstring>

namespace raptor {

Expand Down
5 changes: 5 additions & 0 deletions raptor/util/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ add_test(LaplaceGSTest ./test_gs_laplacian)
#target_link_libraries(test_sor_laplacian raptor ${MPI_LIBRARIES} googletest pthread )
#add_test(LaplaceSORTest ./test_sor_laplacian)

add_executable(test_bsr_jacobi_aniso test_bsr_jacobi_aniso.cpp)
target_link_libraries(test_bsr_jacobi_aniso raptor ${MPI_LIBRARIES} googletest pthread )
add_test(BSRAnisoJacobiTest ./test_bsr_jacobi_aniso)


if (WITH_MPI)
add_executable(test_par_add test_par_add.cpp)
target_link_libraries(test_par_add raptor ${MPI_LIBRARIES} googletest pthread )
Expand Down
105 changes: 5 additions & 100 deletions test_data/RAPtorTests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -755,98 +755,9 @@
},
{
"cell_type": "code",
"execution_count": 68,
"execution_count": 69,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Val[0][0] = 2.600000e+01\n",
"\n",
"Val[0][1] = -1.000000e+00\n",
"\n",
"Val[0][2] = 0.000000e+00\n",
"\n",
"Val[0][3] = 0.000000e+00\n",
"\n",
"Val[0][4] = 0.000000e+00\n",
"\n",
"Val[0][10] = -1.000000e+00\n",
"\n",
"Val[0][11] = -1.000000e+00\n",
"\n",
"Val[0][12] = 0.000000e+00\n",
"\n",
"Val[0][13] = 0.000000e+00\n",
"\n",
"Val[0][14] = 0.000000e+00\n",
"\n",
"Val[0][100] = -1.000000e+00\n",
"\n",
"Val[0][101] = -1.000000e+00\n",
"\n",
"Val[0][102] = 0.000000e+00\n",
"\n",
"Val[0][103] = 0.000000e+00\n",
"\n",
"Val[0][104] = 0.000000e+00\n",
"\n",
"Val[0][110] = -1.000000e+00\n",
"\n",
"Val[0][111] = -1.000000e+00\n",
"\n",
"Val[0][112] = 0.000000e+00\n",
"\n",
"Val[0][113] = 0.000000e+00\n",
"\n",
"Val[0][114] = 0.000000e+00\n",
"\n",
"Val[0][5] = 0.000000e+00\n",
"\n",
"Val[0][6] = 0.000000e+00\n",
"\n",
"Val[0][7] = 0.000000e+00\n",
"\n",
"Val[0][8] = 0.000000e+00\n",
"\n",
"Val[0][9] = 0.000000e+00\n",
"\n",
"Val[0][15] = 0.000000e+00\n",
"\n",
"Val[0][16] = 0.000000e+00\n",
"\n",
"Val[0][17] = 0.000000e+00\n",
"\n",
"Val[0][18] = 0.000000e+00\n",
"\n",
"Val[0][19] = 0.000000e+00\n",
"\n",
"Val[0][105] = 0.000000e+00\n",
"\n",
"Val[0][106] = 0.000000e+00\n",
"\n",
"Val[0][107] = 0.000000e+00\n",
"\n",
"Val[0][108] = 0.000000e+00\n",
"\n",
"Val[0][109] = 0.000000e+00\n",
"\n",
"Val[0][115] = 0.000000e+00\n",
"\n",
"Val[0][116] = 0.000000e+00\n",
"\n",
"Val[0][117] = 0.000000e+00\n",
"\n",
"Val[0][118] = 0.000000e+00\n",
"\n",
"Val[0][119] = 0.000000e+00\n",
"\n",
"[1.]\n",
"[0.03846154]\n"
]
}
],
"outputs": [],
"source": [
"##############################\n",
"### Relaxation Tests\n",
Expand Down Expand Up @@ -903,22 +814,16 @@
"Laplacian = Laplacian.tocsr()\n",
"b = np.ones((Laplacian.shape[0],1))\n",
"x = np.zeros((Laplacian.shape[0],1))\n",
"for i in range(Laplacian.shape[0]):\n",
" for j in range(Laplacian.indptr[i], Laplacian.indptr[i+1]):\n",
" print(\"Val[%d][%d] = %e\\n\" %(i, Laplacian.indices[j], Laplacian.data[j]))\n",
" break\n",
"print(b[0])\n",
"gauss_seidel(Laplacian, x, b)\n",
"print(x[0])\n",
"np.savetxt(\"laplace_gs_ones_1.txt\", x, fmt=\"%f\")\n",
"sor(Laplacian, x, b, 1.0, iterations=1, sweep='forward')\n",
"gauss_seidel(Laplacian, x, b)\n",
"np.savetxt(\"laplace_gs_ones_2.txt\", x, fmt=\"%f\")\n",
"\n",
"b = np.arange(Laplacian.shape[0], dtype='float')\n",
"x = np.zeros((Laplacian.shape[0],1))\n",
"sor(Laplacian, x, b, 1.0, iterations=1, sweep='forward')\n",
"gauss_seidel(Laplacian, x, b)\n",
"np.savetxt(\"laplace_gs_inc_1.txt\", x, fmt=\"%f\")\n",
"sor(Laplacian, x, b, 1.0, iterations=1, sweep='forward')\n",
"gauss_seidel(Laplacian, x, b)\n",
"np.savetxt(\"laplace_gs_inc_2.txt\", x, fmt=\"%f\")\n",
"\n",
"## Forward SOR , Omega = 3.0/4\n",
Expand Down

0 comments on commit 80357b8

Please sign in to comment.