From 80357b84a14b53c032d77a6e20588d3a996157b7 Mon Sep 17 00:00:00 2001 From: Amanda Bienz Date: Wed, 22 May 2024 16:27:02 -0600 Subject: [PATCH] Added BSRMatrix(CSRMatrix) constructor, need to add unit tests --- raptor/core/matrix.hpp | 37 +++++++++++ raptor/util/linalg/relax.hpp | 1 + raptor/util/tests/CMakeLists.txt | 5 ++ test_data/RAPtorTests.ipynb | 105 ++----------------------------- 4 files changed, 48 insertions(+), 100 deletions(-) diff --git a/raptor/core/matrix.hpp b/raptor/core/matrix.hpp index a0b796b5..5d866b07 100644 --- a/raptor/core/matrix.hpp +++ b/raptor/core/matrix.hpp @@ -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 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() { diff --git a/raptor/util/linalg/relax.hpp b/raptor/util/linalg/relax.hpp index 306f0ee4..7bc7995b 100644 --- a/raptor/util/linalg/relax.hpp +++ b/raptor/util/linalg/relax.hpp @@ -7,6 +7,7 @@ #include "raptor/core/vector.hpp" #include "raptor/core/matrix.hpp" #include "raptor/multilevel/level.hpp" +#include namespace raptor { diff --git a/raptor/util/tests/CMakeLists.txt b/raptor/util/tests/CMakeLists.txt index 5cd1bc35..5fc78d63 100644 --- a/raptor/util/tests/CMakeLists.txt +++ b/raptor/util/tests/CMakeLists.txt @@ -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 ) diff --git a/test_data/RAPtorTests.ipynb b/test_data/RAPtorTests.ipynb index 07e063c4..7988cc29 100644 --- a/test_data/RAPtorTests.ipynb +++ b/test_data/RAPtorTests.ipynb @@ -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", @@ -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",