Skip to content

Commit

Permalink
Test both formats - BlockCRS and BSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Zuzek committed Dec 16, 2021
1 parent 8b3ae19 commit 8282092
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 72 deletions.
65 changes: 65 additions & 0 deletions src/common/KokkosKernels_SparseUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include "KokkosKernels_ExecSpaceUtils.hpp"
#include <vector>
#include "KokkosKernels_PrintUtils.hpp"
#include "KokkosSparse_CrsMatrix.hpp"
#include "KokkosSparse_BsrMatrix.hpp"

#ifdef KOKKOSKERNELS_HAVE_PARALLEL_GNUSORT
#include <parallel/algorithm>
Expand Down Expand Up @@ -2059,6 +2061,69 @@ class MatrixRowIndex<BSR, lno_t, size_type>
}
};

template <typename mtx_t>
struct MatrixTraits;

template <typename scalar_t, typename lno_t, typename device,
typename mem_traits, typename size_type>
struct MatrixTraits<
KokkosSparse::CrsMatrix<scalar_t, lno_t, device, mem_traits, size_type>> {
static constexpr auto format = KokkosKernels::BlockCRS;
};

template <typename scalar_t, typename lno_t, typename device,
typename mem_traits, typename size_type>
struct MatrixTraits<KokkosSparse::Experimental::BsrMatrix<
scalar_t, lno_t, device, mem_traits, size_type>> {
static constexpr auto format = KokkosKernels::BSR;
};

template <SparseMatrixFormat /* outFormat */>
struct MatrixConverter;

template <>
struct MatrixConverter<BlockCRS> {
template <
typename scalar_t, typename lno_t, typename device, typename size_type,
typename crsMat_t =
KokkosSparse::CrsMatrix<scalar_t, lno_t, device, void, size_type>>
static crsMat_t from_crs(
const std::string &label,
const KokkosSparse::CrsMatrix<scalar_t, lno_t, device, void, size_type>
&mtx,
lno_t block_size) {
using graph_t = typename crsMat_t::StaticCrsGraphType;
using lno_view_t = typename graph_t::row_map_type::non_const_type;
using lno_nnz_view_t = typename graph_t::entries_type::non_const_type;
using scalar_view_t = typename crsMat_t::values_type::non_const_type;
lno_view_t rows;
lno_nnz_view_t entries;
scalar_view_t vals;
size_t num_rows, num_cols;

KokkosKernels::Impl::kk_create_blockcrs_from_blockcrs_formatted_point_crs(
block_size, mtx.numRows(), mtx.numCols(), mtx.graph.row_map,
mtx.graph.entries, mtx.values, num_rows, num_cols, rows, entries, vals);

return crsMat_t(label, num_cols, vals, graph_t(entries, rows));
}
};

template <>
struct MatrixConverter<BSR> {
template <typename scalar_t, typename lno_t, typename size_type,
typename device,
typename bsrMtx_t = KokkosSparse::Experimental::BsrMatrix<
scalar_t, lno_t, device, void, size_type>>
static bsrMtx_t from_crs(
const std::string & /* label */,
const KokkosSparse::CrsMatrix<scalar_t, lno_t, device, void, size_type>
&mtx,
lno_t block_size) {
return bsrMtx_t(mtx, block_size);
}
};

} // namespace Impl
} // namespace KokkosKernels

Expand Down
132 changes: 60 additions & 72 deletions unit_test/sparse/Test_Sparse_block_gauss_seidel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include "KokkosKernels_TestUtils.hpp"
#include "KokkosKernels_Handle.hpp"
#include "KokkosKernels_IOUtils.hpp"
//#include <Kokkos_Sparse_CrsMatrix.hpp>
#include "KokkosKernels_SparseUtils.hpp"
#include <KokkosSparse_spmv.hpp>
#include <KokkosBlas1_dot.hpp>
#include <KokkosBlas1_axpby.hpp>
Expand All @@ -67,34 +67,36 @@ typedef Kokkos::complex<double> kokkos_complex_double;
typedef Kokkos::complex<float> kokkos_complex_float;

using namespace KokkosKernels;
using namespace KokkosKernels::Impl;
using namespace KokkosKernels::Experimental;
using namespace KokkosSparse;
using namespace KokkosSparse::Experimental;

namespace Test {

template <typename crsMat_t, typename vector_t, typename const_vector_t,
typename device>
template <typename mtx_t, typename vector_t, typename const_vector_t>
int run_block_gauss_seidel_1(
crsMat_t input_mat, int block_size, KokkosSparse::GSAlgorithm gs_algorithm,
mtx_t input_mat, int block_size, KokkosSparse::GSAlgorithm gs_algorithm,
vector_t x_vector, const_vector_t y_vector, bool is_symmetric_graph,
int apply_type = 0, // 0 for symmetric, 1 for forward, 2 for backward.
bool skip_symbolic = false, bool skip_numeric = false,
size_t shmem_size = 32128,
typename crsMat_t::value_type omega =
Kokkos::Details::ArithTraits<typename crsMat_t::value_type>::one()) {
typedef typename crsMat_t::StaticCrsGraphType graph_t;
typename mtx_t::value_type omega =
Kokkos::Details::ArithTraits<typename mtx_t::value_type>::one()) {
typedef typename mtx_t::StaticCrsGraphType graph_t;
typedef typename graph_t::row_map_type lno_view_t;
typedef typename graph_t::entries_type lno_nnz_view_t;
typedef typename crsMat_t::values_type::non_const_type scalar_view_t;
typedef typename mtx_t::values_type::non_const_type scalar_view_t;

typedef typename lno_view_t::value_type size_type;
typedef typename lno_nnz_view_t::value_type lno_t;
typedef typename scalar_view_t::value_type scalar_t;

constexpr auto format = MatrixTraits<mtx_t>::format;

typedef KokkosKernelsHandle<
size_type, lno_t, scalar_t, typename device::execution_space,
typename device::memory_space, typename device::memory_space>
size_type, lno_t, scalar_t, typename mtx_t::execution_space,
typename mtx_t::memory_space, typename mtx_t::memory_space>
KernelHandle;
KernelHandle kh;
kh.set_team_work_size(16);
Expand All @@ -113,32 +115,27 @@ int run_block_gauss_seidel_1(
}

if (!skip_numeric) {
block_gauss_seidel_numeric(&kh, num_rows_1, num_cols_1, block_size,
input_mat.graph.row_map, input_mat.graph.entries,
input_mat.values, is_symmetric_graph);
block_gauss_seidel_numeric<format>(
&kh, num_rows_1, num_cols_1, block_size, input_mat.graph.row_map,
input_mat.graph.entries, input_mat.values, is_symmetric_graph);
}

switch (apply_type) {
case 0:
symmetric_block_gauss_seidel_apply(
&kh, num_rows_1, num_cols_1, block_size, input_mat.graph.row_map,
input_mat.graph.entries, input_mat.values, x_vector, y_vector, false,
true, omega, apply_count);
break;
case 1:
forward_sweep_block_gauss_seidel_apply(
forward_sweep_block_gauss_seidel_apply<format>(
&kh, num_rows_1, num_cols_1, block_size, input_mat.graph.row_map,
input_mat.graph.entries, input_mat.values, x_vector, y_vector, false,
true, omega, apply_count);
break;
case 2:
backward_sweep_block_gauss_seidel_apply(
backward_sweep_block_gauss_seidel_apply<format>(
&kh, num_rows_1, num_cols_1, block_size, input_mat.graph.row_map,
input_mat.graph.entries, input_mat.values, x_vector, y_vector, false,
true, omega, apply_count);
break;
case 0:
default:
symmetric_block_gauss_seidel_apply(
symmetric_block_gauss_seidel_apply<format>(
&kh, num_rows_1, num_cols_1, block_size, input_mat.graph.row_map,
input_mat.graph.entries, input_mat.values, x_vector, y_vector, false,
true, omega, apply_count);
Expand All @@ -151,8 +148,8 @@ int run_block_gauss_seidel_1(

} // namespace Test

template <typename scalar_t, typename lno_t, typename size_type,
typename device>
template <SparseMatrixFormat mtx_format, typename scalar_t, typename lno_t,
typename size_type, typename device>
void test_block_gauss_seidel_rank1(lno_t numRows, size_type nnz,
lno_t bandwidth, lno_t row_size_variance) {
using namespace Test;
Expand Down Expand Up @@ -193,17 +190,9 @@ void test_block_gauss_seidel_rank1(lno_t numRows, size_type nnz,
graph_t static_graph2(pf_e, pf_rm);
crsMat_t crsmat2("CrsMatrix2", out_c, pf_v, static_graph2);

lno_view_t bf_rm;
lno_nnz_view_t bf_e;
scalar_view_t bf_v;
size_t but_r, but_c;

// this converts the previous generated matrix to block crs matrix.
KokkosKernels::Impl::kk_create_blockcrs_from_blockcrs_formatted_point_crs(
block_size, out_r, out_c, pf_rm, pf_e, pf_v, but_r, but_c, bf_rm, bf_e,
bf_v);
graph_t static_graph(bf_e, bf_rm);
crsMat_t input_mat("CrsMatrix", but_c, bf_v, static_graph);
// this converts the previous generated matrix to block matrix.
auto input_mat =
MatrixConverter<mtx_format>::from_crs("BlockMatrix", crsmat2, block_size);

lno_t nv = ((crsmat2.numRows() + block_size - 1) / block_size) * block_size;

Expand Down Expand Up @@ -246,12 +235,10 @@ void test_block_gauss_seidel_rank1(lno_t numRows, size_type nnz,
for (int skip_numeric = 0; skip_numeric < 2; ++skip_numeric) {
Kokkos::Timer timer1;
// int res =
run_block_gauss_seidel_1<crsMat_t, scalar_view_t,
typename scalar_view_t::const_type,
device>(
input_mat, block_size, gs_algorithm, x_vector, y_vector,
is_symmetric_graph, apply_type, skip_symbolic, skip_numeric,
shmem_size, omega);
run_block_gauss_seidel_1(input_mat, block_size, gs_algorithm,
x_vector, y_vector, is_symmetric_graph,
apply_type, skip_symbolic, skip_numeric,
shmem_size, omega);
// double gs = timer1.seconds();
// KokkosKernels::Impl::print_1Dview(x_vector);
KokkosBlas::axpby(alpha, solution_x, -alpha, x_vector);
Expand All @@ -265,8 +252,8 @@ void test_block_gauss_seidel_rank1(lno_t numRows, size_type nnz,
// device::execution_space::finalize();
}

template <typename scalar_t, typename lno_t, typename size_type,
typename device>
template <SparseMatrixFormat mtx_format, typename scalar_t, typename lno_t,
typename size_type, typename device>
void test_block_gauss_seidel_rank2(lno_t numRows, size_type nnz,
lno_t bandwidth, lno_t row_size_variance) {
using namespace Test;
Expand Down Expand Up @@ -308,17 +295,8 @@ void test_block_gauss_seidel_rank2(lno_t numRows, size_type nnz,
graph_t static_graph2(pf_e, pf_rm);
crsMat_t crsmat2("CrsMatrix2", out_c, pf_v, static_graph2);

lno_view_t bf_rm;
lno_nnz_view_t bf_e;
scalar_view_t bf_v;
size_t but_r, but_c;

// this converts the previous generated matrix to block crs matrix.
KokkosKernels::Impl::kk_create_blockcrs_from_blockcrs_formatted_point_crs(
block_size, out_r, out_c, pf_rm, pf_e, pf_v, but_r, but_c, bf_rm, bf_e,
bf_v);
graph_t static_graph(bf_e, bf_rm);
crsMat_t input_mat("CrsMatrix", but_c, bf_v, static_graph);
auto input_mat =
MatrixConverter<mtx_format>::from_crs("BlockMatrix", crsmat2, block_size);

lno_t nv = ((crsmat2.numRows() + block_size - 1) / block_size) * block_size;

Expand Down Expand Up @@ -376,12 +354,10 @@ void test_block_gauss_seidel_rank2(lno_t numRows, size_type nnz,
for (int skip_numeric = 0; skip_numeric < 2; ++skip_numeric) {
Kokkos::Timer timer1;
// int res =
run_block_gauss_seidel_1<crsMat_t, scalar_view2d_t,
typename scalar_view2d_t::const_type,
device>(
input_mat, block_size, gs_algorithm, x_vector, y_vector,
is_symmetric_graph, apply_type, skip_symbolic, skip_numeric,
shmem_size, omega);
run_block_gauss_seidel_1(input_mat, block_size, gs_algorithm,
x_vector, y_vector, is_symmetric_graph,
apply_type, skip_symbolic, skip_numeric,
shmem_size, omega);
// double gs = timer1.seconds();
// KokkosKernels::Impl::print_1Dview(x_vector);
Kokkos::deep_copy(x_host, x_vector);
Expand All @@ -404,18 +380,30 @@ void test_block_gauss_seidel_rank2(lno_t numRows, size_type nnz,
// device::execution_space::finalize();
}

#define EXECUTE_TEST(SCALAR, ORDINAL, OFFSET, DEVICE) \
TEST_F( \
TestCategory, \
sparse##_##block_gauss_seidel_rank1##_##SCALAR##_##ORDINAL##_##OFFSET##_##DEVICE) { \
test_block_gauss_seidel_rank1<SCALAR, ORDINAL, OFFSET, DEVICE>( \
500, 500 * 10, 70, 3); \
} \
TEST_F( \
TestCategory, \
sparse##_##block_gauss_seidel_rank2##_##SCALAR##_##ORDINAL##_##OFFSET##_##DEVICE) { \
test_block_gauss_seidel_rank2<SCALAR, ORDINAL, OFFSET, DEVICE>( \
500, 500 * 10, 70, 3); \
#define EXECUTE_TEST(SCALAR, ORDINAL, OFFSET, DEVICE) \
TEST_F( \
TestCategory, \
sparse_blockcrs_gauss_seidel_rank1##_##SCALAR##_##ORDINAL##_##OFFSET##_##DEVICE) { \
test_block_gauss_seidel_rank1<BlockCRS, SCALAR, ORDINAL, OFFSET, DEVICE>( \
500, 500 * 10, 70, 3); \
} \
TEST_F( \
TestCategory, \
sparse_blockcrs_gauss_seidel_rank2_##SCALAR##_##ORDINAL##_##OFFSET##_##DEVICE) { \
test_block_gauss_seidel_rank2<BlockCRS, SCALAR, ORDINAL, OFFSET, DEVICE>( \
500, 500 * 10, 70, 3); \
} \
TEST_F( \
TestCategory, \
sparse_bsr_gauss_seidel_rank1_##SCALAR##_##ORDINAL##_##OFFSET##_##DEVICE) { \
test_block_gauss_seidel_rank1<BSR, SCALAR, ORDINAL, OFFSET, DEVICE>( \
500, 500 * 10, 70, 3); \
} \
TEST_F( \
TestCategory, \
sparse_bsr_gauss_seidel_rank2_##SCALAR##_##ORDINAL##_##OFFSET##_##DEVICE) { \
test_block_gauss_seidel_rank2<BlockCRS, SCALAR, ORDINAL, OFFSET, DEVICE>( \
500, 500 * 10, 70, 3); \
}

#if (defined(KOKKOSKERNELS_INST_DOUBLE) && \
Expand Down

0 comments on commit 8282092

Please sign in to comment.