diff --git a/src/graph/impl/KokkosGraph_Distance1Color_impl.hpp b/src/graph/impl/KokkosGraph_Distance1Color_impl.hpp index 39e27795cc..1e2433def8 100644 --- a/src/graph/impl/KokkosGraph_Distance1Color_impl.hpp +++ b/src/graph/impl/KokkosGraph_Distance1Color_impl.hpp @@ -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++) { diff --git a/src/sparse/KokkosSparse_csc2csr.hpp b/src/sparse/KokkosSparse_csc2csr.hpp new file mode 100644 index 0000000000..bd4ade4b5b --- /dev/null +++ b/src/sparse/KokkosSparse_csc2csr.hpp @@ -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 \ No newline at end of file diff --git a/test_common/KokkosKernels_TestUtils.hpp b/test_common/KokkosKernels_TestUtils.hpp index 2878543f33..327847b7c1 100644 --- a/test_common/KokkosKernels_TestUtils.hpp +++ b/test_common/KokkosKernels_TestUtils.hpp @@ -45,6 +45,8 @@ #ifndef KOKKOSKERNELS_TEST_UTILS_HPP #define KOKKOSKERNELS_TEST_UTILS_HPP +#include + #include "KokkosKernels_Utils.hpp" #include "Kokkos_ArithTraits.hpp" #include "KokkosSparse_spmv.hpp" @@ -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); } @@ -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 RandCscMat { + private: + using ValViewType = Kokkos::View; + using RowIdViewType = Kokkos::View; + using ColMapViewType = Kokkos::View; + 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 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 + 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 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 diff --git a/unit_test/sparse/Test_Sparse.hpp b/unit_test/sparse/Test_Sparse.hpp index 2afa0fb2db..057cd53d20 100644 --- a/unit_test/sparse/Test_Sparse.hpp +++ b/unit_test/sparse/Test_Sparse.hpp @@ -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 diff --git a/unit_test/sparse/Test_Sparse_TestUtils_RandCscMat.hpp b/unit_test/sparse/Test_Sparse_TestUtils_RandCscMat.hpp new file mode 100644 index 0000000000..e56cd4bb40 --- /dev/null +++ b/unit_test/sparse/Test_Sparse_TestUtils_RandCscMat.hpp @@ -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 +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 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 +void doAllCscMat(size_t m, size_t n) { + int min = 1, max = 10; + + // Verify that CscMax is constructed properly. + doCscMat(m, n, min, max); + doCscMat(m, n, min, max); + + doCscMat(m, n, min, max); + doCscMat(m, n, min, max); + + // Verify that CscMax can be instantiated with complex types. + RandCscMat, Kokkos::LayoutLeft, ExeSpaceType> cmcf( + m, n, min, max); + RandCscMat, 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(dim, dim); + + // Non-square cases + for (int dim = 1; dim < 1024; dim *= 4) { + doAllCscMat(dim * 3, dim); + doAllCscMat(dim, dim * 3); + } +} +} // namespace Test \ No newline at end of file diff --git a/unit_test/sparse/Test_Sparse_csc2csr.hpp b/unit_test/sparse/Test_Sparse_csc2csr.hpp new file mode 100644 index 0000000000..f6f5033dbe --- /dev/null +++ b/unit_test/sparse/Test_Sparse_csc2csr.hpp @@ -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 +// void doCsc2Csr() { +// TODO +// } +} // namespace Test \ No newline at end of file