Skip to content

Commit

Permalink
#29 Simplified random BSR generation with CRS converting constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Zuzek committed Sep 8, 2021
1 parent bcb5650 commit eae9f61
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 80 deletions.
118 changes: 42 additions & 76 deletions src/common/KokkosKernels_IOUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <Kokkos_Core.hpp>
#include "Kokkos_Random.hpp"
#include "KokkosKernels_SimpleUtils.hpp"
#include "KokkosSparse_CrsMatrix.hpp"
#include <sys/stat.h>

namespace KokkosKernels{
Expand Down Expand Up @@ -97,8 +98,7 @@ void kk_sparseMatrix_generate(
OrdinalType bandwidth,
ScalarType* &values,
SizeType* &rowPtr,
OrdinalType* &colInd,
OrdinalType block_elem_count = 1) // allows non-square blocks
OrdinalType* &colInd)
{
rowPtr = new SizeType[nrows+1];

Expand All @@ -113,7 +113,7 @@ void kk_sparseMatrix_generate(
rowPtr[row+1] = rowPtr[row] + numRowEntries;
}
nnz = rowPtr[nrows];
values = new ScalarType[nnz * block_elem_count];
values = new ScalarType[nnz];
colInd = new OrdinalType[nnz];
for(OrdinalType row = 0; row < nrows; row++) {
OrdinalType k0 = rowPtr[row], k1 = rowPtr[row + 1];
Expand All @@ -132,9 +132,7 @@ void kk_sparseMatrix_generate(
}
}
colInd[k] = pos;
SizeType kk = k * block_elem_count;
for(OrdinalType i = 0; i < block_elem_count; ++i)
values[kk++] = 100.0 * rand() / RAND_MAX - 50.0;
values[k] = 100.0 * rand() / RAND_MAX - 50.0;
}
std::sort(&colInd[k0], &colInd[k1]); // don't bother about sorting values - they're random anyway
}
Expand Down Expand Up @@ -412,111 +410,79 @@ crsMat_t kk_generate_triangular_sparse_matrix(
return crsmat;
}

template<typename lno_t,
typename size_type,
typename row_map_view_t,
typename cols_view_t,
typename values_view_t>
void generate_bsr_content(
const lno_t block_dim,
const lno_t nrows,
const lno_t ncols,
size_type &nnz,
const lno_t row_size_variance,
const lno_t bandwidth,
row_map_view_t &rowmap_view,
cols_view_t &columns_view,
values_view_t &values_view) {

// typedef typename row_map_view_t::non_const_value_type size_type;
// typedef typename cols_view_t::non_const_value_type lno_t;
template <typename crsMat_t>
crsMat_t kk_generate_sparse_matrix(
typename crsMat_t::const_ordinal_type nrows,
typename crsMat_t::const_ordinal_type ncols,
typename crsMat_t::non_const_size_type &nnz,
typename crsMat_t::const_ordinal_type row_size_variance,
typename crsMat_t::const_ordinal_type bandwidth){

typedef typename crsMat_t::StaticCrsGraphType graph_t;
typedef typename graph_t::row_map_type::non_const_type row_map_view_t;
typedef typename graph_t::entries_type::non_const_type cols_view_t;
typedef typename crsMat_t::values_type::non_const_type values_view_t;


typedef typename row_map_view_t::non_const_value_type size_type;
typedef typename cols_view_t::non_const_value_type lno_t;
typedef typename values_view_t::non_const_value_type scalar_t;
lno_t *adj;
size_type *xadj;
size_type *xadj;//, nnzA;
scalar_t *values;

kk_sparseMatrix_generate<scalar_t, lno_t, size_type>(
nrows, ncols, nnz, row_size_variance, bandwidth,
values, xadj, adj, block_dim * block_dim);
values, xadj, adj);

row_map_view_t rowmap_view("rowmap_view", nrows+1);
cols_view_t columns_view("colsmap_view", nnz);
values_view_t values_view("values_view", nnz);

const lno_t nvals = nnz * block_dim * block_dim;
rowmap_view = row_map_view_t("rowmap_view", nrows + 1);
columns_view = cols_view_t("colsmap_view", nnz);
values_view = values_view_t("values_view", nvals);
{
typename row_map_view_t::HostMirror hr = Kokkos::create_mirror_view (rowmap_view);
typename cols_view_t::HostMirror hc = Kokkos::create_mirror_view (columns_view);
typename values_view_t::HostMirror hv = Kokkos::create_mirror_view (values_view);

Kokkos::parallel_for(nrows + 1, KOKKOS_LAMBDA(const lno_t i) {
for (lno_t i = 0; i <= nrows; ++i){
hr(i) = xadj[i];
});
Kokkos::parallel_for(nnz, KOKKOS_LAMBDA(const size_type i) {
}

for (size_type i = 0; i < nnz; ++i){
hc(i) = adj[i];
});
Kokkos::parallel_for(nvals, KOKKOS_LAMBDA(const size_type i) {
hv(i) = values[i];
});
}
Kokkos::deep_copy (rowmap_view , hr);
Kokkos::deep_copy (columns_view , hc);
Kokkos::deep_copy (values_view , hv);
Kokkos::fence();
}
delete [] xadj;
delete [] adj;
delete [] values;
}

template <typename crsMat_t>
crsMat_t kk_generate_sparse_matrix(
typename crsMat_t::const_ordinal_type nrows,
typename crsMat_t::const_ordinal_type ncols,
typename crsMat_t::non_const_size_type &nnz,
typename crsMat_t::const_ordinal_type row_size_variance,
typename crsMat_t::const_ordinal_type bandwidth){

typedef typename crsMat_t::StaticCrsGraphType graph_t;
typedef typename graph_t::row_map_type::non_const_type row_map_view_t;
typedef typename graph_t::entries_type::non_const_type cols_view_t;
typedef typename crsMat_t::values_type::non_const_type values_view_t;

row_map_view_t rowmap_view;
cols_view_t columns_view;
values_view_t values_view;

// Note: block_dim=1 generates CRS content
generate_bsr_content(1, nrows, ncols, nnz, row_size_variance,
bandwidth, rowmap_view, columns_view, values_view);

typename crsMat_t::StaticCrsGraphType static_graph(columns_view, rowmap_view);
graph_t static_graph (columns_view, rowmap_view);
crsMat_t crsmat("CrsMatrix", ncols, values_view, static_graph);
delete [] xadj; delete [] adj; delete [] values;
return crsmat;
}

template <typename bsrMat_t>
bsrMat_t kk_generate_sparse_matrix(
const char* const label,
typename bsrMat_t::const_ordinal_type block_dim,
typename bsrMat_t::const_ordinal_type nrows,
typename bsrMat_t::const_ordinal_type ncols,
typename bsrMat_t::non_const_size_type &nnz,
typename bsrMat_t::const_ordinal_type row_size_variance,
typename bsrMat_t::const_ordinal_type bandwidth){

typedef typename bsrMat_t::StaticCrsGraphType graph_t;
typedef typename graph_t::row_map_type::non_const_type row_map_view_t;
typedef typename graph_t::entries_type::non_const_type cols_view_t;
typedef typename bsrMat_t::values_type::non_const_type values_view_t;

row_map_view_t rowmap_view;
cols_view_t columns_view;
values_view_t values_view;

generate_bsr_content(block_dim, nrows, ncols, nnz, row_size_variance,
bandwidth, rowmap_view, columns_view, values_view);
typedef KokkosSparse::CrsMatrix<
typename bsrMat_t::value_type,
typename bsrMat_t::ordinal_type,
typename bsrMat_t::device_type,
typename bsrMat_t::memory_traits,
typename bsrMat_t::size_type> crsMat_t;

typename bsrMat_t::StaticCrsGraphType static_graph(columns_view, rowmap_view);
bsrMat_t bsrmat(label, ncols, values_view, static_graph, block_dim);
const auto crs_mtx = kk_generate_sparse_matrix<crsMat_t>(
nrows * block_dim, ncols * block_dim, nnz, row_size_variance, bandwidth);
bsrMat_t bsrmat(crs_mtx, block_dim);
return bsrmat;
}

Expand Down
6 changes: 3 additions & 3 deletions src/sparse/KokkosSparse_BsrMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,9 @@ class BsrMatrix {
typedef typename crs_graph_type::row_map_type crs_graph_row_map_type;

assert(
(crs_mtx.numCols() % blockDim_ == 0) &&
(crs_mtx.numCols() % blockDimIn == 0) &&
"BsrMatrix: input CrsMatrix columns is not a multiple of block size");
assert((crs_mtx.numRows() % blockDim_ == 0) &&
assert((crs_mtx.numRows() % blockDimIn == 0) &&
"BsrMatrix: input CrsMatrix rows is not a multiple of block size");

blockDim_ = blockDimIn;
Expand Down Expand Up @@ -699,7 +699,7 @@ class BsrMatrix {
// numBlocks in the final entry
graph = Kokkos::create_staticcrsgraph<staticcrsgraph_type>("blockgraph",
block_rows);
typename index_type::HostMirror h_row_map =
typename row_map_type::HostMirror h_row_map =
Kokkos::create_mirror_view(graph.row_map);
Kokkos::deep_copy(h_row_map, graph.row_map);

Expand Down
2 changes: 1 addition & 1 deletion unit_test/sparse/Test_Sparse_bspgemm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void test_bspgemm(lno_t blockDim, lno_t numRows, size_type nnz, lno_t bandwidth,
#else // test on random matrices, as requested by the test
lno_t numCols = numRows;
// Generate random BSR matrix
bsrMat_t A = KokkosKernels::Impl::kk_generate_sparse_matrix<bsrMat_t>("input_bsr_matrix", blockDim, numRows, numCols, nnz, row_size_variance, bandwidth);
bsrMat_t A = KokkosKernels::Impl::kk_generate_sparse_matrix<bsrMat_t>(blockDim, numRows, numCols, nnz, row_size_variance, bandwidth);
bsrMat_t B = A;
if(SHOW_TIMINGS)
std::cout << " nnz_in=" << A.nnz() << std::endl;
Expand Down

0 comments on commit eae9f61

Please sign in to comment.