Skip to content

Commit

Permalink
unit_test/sparse: Added common conversion test code
Browse files Browse the repository at this point in the history
  - Added RandCscMat to KokkosKernels_TestUtils
  - Added Test_Sparse_TestUtils_RandCscMat
  • Loading branch information
e10harvey committed Mar 2, 2022
1 parent f5d7490 commit 88b92cc
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/graph/impl/KokkosGraph_Distance1Color_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ class GraphColor_VB
double total_time_serial_conflict_resolution = 0.0;
Kokkos::Timer timer;
timer.reset();
(void)total;

int iter = 0;
for (; (iter < this->_max_num_iterations) && (numUncolored > 0); iter++) {
Expand Down
48 changes: 48 additions & 0 deletions src/sparse/KokkosSparse_csc2csr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
//@HEADER
// ************************************************************************
//
// Kokkos v. 3.0
// Copyright (2020) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Siva Rajamanickam (srajama@sandia.gov)
//
// ************************************************************************
//@HEADER
*/

#ifndef _KOKKOSSPARSE_CSC2CSR_HPP
#define _KOKKOSSPARSE_CSC2CSR_HPP
// TODO
#endif // _KOKKOSSPARSE_CSC2CSR_HPP
108 changes: 108 additions & 0 deletions test_common/KokkosKernels_TestUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#ifndef KOKKOSKERNELS_TEST_UTILS_HPP
#define KOKKOSKERNELS_TEST_UTILS_HPP

#include <random>

#include "KokkosKernels_Utils.hpp"
#include "Kokkos_ArithTraits.hpp"
#include "KokkosSparse_spmv.hpp"
Expand Down Expand Up @@ -127,6 +129,8 @@ static inline const std::string kk_failure_str(std::string file,
std::string func,
const int line) {
std::string failure_msg = " > from ";
// std::string test =
// ::testing::UnitTest::GetInstance()->current_test_info()->name();
failure_msg += (file + ":" + func + ":" + std::to_string(line) + "\n > ");
return std::string(failure_msg);
}
Expand Down Expand Up @@ -488,5 +492,109 @@ int string_compare_no_case(const char* str1, const char* str2) {
return strcmp(str1_s.c_str(), str2_s.c_str());
}

/// /brief Csc matrix class for testing purposes.
/// \tparam ScalarType
/// \tparam LayoutType
/// \tparam ExeSpaceType
template <class ScalarType, class LayoutType, class ExeSpaceType>
class RandCscMat {
private:
using ValViewType = Kokkos::View<ScalarType*, LayoutType, ExeSpaceType>;
using RowIdViewType = Kokkos::View<size_t*, LayoutType, ExeSpaceType>;
using ColMapViewType = Kokkos::View<size_t*, LayoutType, ExeSpaceType>;
size_t __nrows;
size_t __ncols;
size_t __nnz = 0;
ColMapViewType __col_map;
RowIdViewType __row_ids;
ValViewType __vals;

/// Generates a random column map where:
/// 1. __col_map(i) is in [__row_ids.data(), &row_ids.data()[nnz - 1]
/// 2. __col_map(i) > col_map(i - 1) for i > 1
/// 3. __col_map(i) == col_map(j) iff __col_map(i) == col_map(j) == nullptr
/// 4. __col_map(i) - col_map(i - 1) is in [0, m]
void __populate_random_csc_mat(uint64_t ticks) {
std::srand(ticks);
for (size_t col_idx = 0; col_idx < __ncols; col_idx++) {
size_t r = std::rand() % (__nrows + 1);
if (r == 0) { // 100% sparse column
__col_map(col_idx) = __nnz;
} else { // sparse column with r elements
// Populate r row ids
std::vector<size_t> v(r);

for (size_t i = 0; i < r; i++) v.at(i) = i;

std::shuffle(v.begin(), v.end(), std::mt19937(std::random_device()()));

for (size_t i = 0; i < r; i++) __row_ids(i + __nnz) = v.at(i);

// Point to new column and accumulate number of non zeros
__col_map(col_idx) = __nnz;
__nnz += r;
}
}

// last entry in map points to end of row id list
__col_map(__ncols) = __nnz;
}

template <class T>
T __getter_copy_helper(T src) {
T dst(std::string("RandCscMat.") + typeid(T).name() + " copy",
src.extent(0));
Kokkos::deep_copy(dst, src);
ExeSpaceType().fence();
return dst;
}

public:
std::string info;
/// Constructs a random csc matrix.
/// \param m The number of rows.
/// \param n The number of columns.
/// \param min_val The minimum scalar value in the matrix.
/// \param max_val The maximum scalar value in the matrix.
RandCscMat(size_t m, size_t n, ScalarType min_val, ScalarType max_val) {
__ncols = n;
__nrows = m;
__col_map = ColMapViewType("RandCscMat.ColMapViewType", __ncols + 1);
__row_ids =
RowIdViewType("RandCscMat.RowIdViewType", m * n + 1); // over-allocated

uint64_t ticks =
std::chrono::high_resolution_clock::now().time_since_epoch().count() %
UINT32_MAX;

info = std::string(std::string("RandCscMat<") + typeid(ScalarType).name() +
", " + typeid(LayoutType).name() + ", " +
typeid(ExeSpaceType).name() + ">(" + std::to_string(m) +
", " + std::to_string(n) +
"...): rand seed: " + std::to_string(ticks) + "\n");
Kokkos::Random_XorShift64_Pool<ExeSpaceType> random(ticks);
__populate_random_csc_mat(ticks);

__vals = ValViewType("RandCscMat.ValViewType", __nnz + 1);
Kokkos::fill_random(__vals, random, min_val, max_val); // random scalars
ExeSpaceType().fence();
__vals(__nnz) = ScalarType(0);
}

// O(c), where c is a constant.
ScalarType operator()(size_t idx) { return __vals(idx); }

size_t get_nnz() { return __nnz; }
size_t get_m() { return __nrows; }
size_t get_n() { return __ncols; }
size_t get_col_len(size_t j) {
return j < __ncols ? (__col_map(j + 1) - __col_map(j)) : 0;
}
size_t get_col_start(size_t j) { return j < __ncols ? __col_map(j) : 0; }
ValViewType get_vals() { return __getter_copy_helper(__vals); }
RowIdViewType get_row_ids() { return __getter_copy_helper(__row_ids); }
ColMapViewType get_col_map() { return __getter_copy_helper(__col_map); }
};

} // namespace Test
#endif
2 changes: 2 additions & 0 deletions unit_test/sparse/Test_Sparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
//#include "Test_Sparse_spmv_bsr.hpp"
#include "Test_Sparse_sptrsv.hpp"
#include "Test_Sparse_trsv.hpp"
#include "Test_Sparse_TestUtils_RandCscMat.hpp"
#include "Test_Sparse_csc2csr.hpp"

// TPL specific tests, these require
// particular pairs of backend and TPL
Expand Down
106 changes: 106 additions & 0 deletions unit_test/sparse/Test_Sparse_TestUtils_RandCscMat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
//@HEADER
// ************************************************************************
//
// Kokkos v. 3.0
// Copyright (2020) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Siva Rajamanickam (srajama@sandia.gov)
//
// ************************************************************************
//@HEADER
*/

#include "KokkosKernels_TestUtils.hpp"

namespace Test {
template <class ScalarType, class LayoutType, class ExeSpaceType>
void doCscMat(size_t m, size_t n, ScalarType min_val, ScalarType max_val) {
auto expected_min = ScalarType(1.0);
size_t expected_nnz = 0;
RandCscMat<ScalarType, LayoutType, ExeSpaceType> cm(m, n, min_val, max_val);

std::string fmsg = kk_failure_str(__FILE__, __FUNCTION__, __LINE__);
for (size_t i = 0; i < cm.get_nnz(); ++i)
ASSERT_GE(cm(i), expected_min) << cm.info;

for (size_t j = 0; j < cm.get_n(); ++j) {
for (size_t i = 0; i < cm.get_col_len(j); ++i)
ASSERT_FLOAT_EQ(cm(cm.get_col_start(j) + i), cm(expected_nnz + i))
<< cm.info;
expected_nnz += cm.get_col_len(j);
}
ASSERT_EQ(cm.get_nnz(), expected_nnz) << cm.info;

// No need to check data here. Kokkos unit-tests deep_copy.
auto vals = cm.get_vals();
ASSERT_EQ(vals.extent(0), cm.get_nnz() + 1) << cm.info;

auto row_ids = cm.get_row_ids();
ASSERT_EQ(row_ids.extent(0), cm.get_n() * cm.get_m() + 1) << cm.info;

auto col_map = cm.get_col_map();
ASSERT_EQ(col_map.extent(0), cm.get_n() + 1);
}

template <class ExeSpaceType>
void doAllCscMat(size_t m, size_t n) {
int min = 1, max = 10;

// Verify that CscMax is constructed properly.
doCscMat<float, Kokkos::LayoutLeft, ExeSpaceType>(m, n, min, max);
doCscMat<float, Kokkos::LayoutRight, ExeSpaceType>(m, n, min, max);

doCscMat<double, Kokkos::LayoutLeft, ExeSpaceType>(m, n, min, max);
doCscMat<double, Kokkos::LayoutRight, ExeSpaceType>(m, n, min, max);

// Verify that CscMax can be instantiated with complex types.
RandCscMat<Kokkos::complex<float>, Kokkos::LayoutLeft, ExeSpaceType> cmcf(
m, n, min, max);
RandCscMat<Kokkos::complex<double>, Kokkos::LayoutRight, ExeSpaceType> cmcd(
m, n, min, max);
}

// Test randomly generated csc matrices
TEST_F(TestCategory, sparse_randcscmat) {
// Square cases
for (int dim = 1; dim < 1024; dim *= 4) doAllCscMat<TestExecSpace>(dim, dim);

// Non-square cases
for (int dim = 1; dim < 1024; dim *= 4) {
doAllCscMat<TestExecSpace>(dim * 3, dim);
doAllCscMat<TestExecSpace>(dim, dim * 3);
}
}
} // namespace Test
53 changes: 53 additions & 0 deletions unit_test/sparse/Test_Sparse_csc2csr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
//@HEADER
// ************************************************************************
//
// Kokkos v. 3.0
// Copyright (2020) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Siva Rajamanickam (srajama@sandia.gov)
//
// ************************************************************************
//@HEADER
*/

#include "KokkosSparse_csc2csr.hpp"
#include "KokkosKernels_TestUtils.hpp"

namespace Test {
// template <class ScalarType, class LayoutType, class ExeSpaceType>
// void doCsc2Csr() {
// TODO
// }
} // namespace Test

0 comments on commit 88b92cc

Please sign in to comment.