Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt Kokkos::StaticCrsGraph from "Core" and move Static{Ccs,Crs}Graph to namespace KokkosSparse:: #2419

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions graph/unit_test/Test_Graph_rcm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "KokkosGraph_RCM.hpp"
#include "KokkosKernels_IOUtils.hpp"
#include "Kokkos_StaticCrsGraph.hpp"
#include "KokkosSparse_StaticCrsGraph.hpp"

#include <vector>

Expand Down Expand Up @@ -119,7 +119,7 @@ void test_rcm(const rowmap_t& rowmap, const entries_t& entries, bool expectBandw

template <typename lno_t, typename size_type, typename device>
void test_rcm_zerorows() {
using graph_t = Kokkos::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using graph_t = KokkosSparse::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using rowmap_t = typename graph_t::row_map_type::non_const_type;
using entries_t = typename graph_t::entries_type::non_const_type;
rowmap_t rowmap;
Expand All @@ -129,7 +129,7 @@ void test_rcm_zerorows() {

template <typename lno_t, typename size_type, typename device>
void test_rcm_7pt(lno_t gridX, lno_t gridY, lno_t gridZ, bool expectBandwidthReduced) {
using graph_t = Kokkos::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using graph_t = KokkosSparse::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using rowmap_t = typename graph_t::row_map_type::non_const_type;
using entries_t = typename graph_t::entries_type::non_const_type;
rowmap_t rowmap;
Expand All @@ -140,7 +140,7 @@ void test_rcm_7pt(lno_t gridX, lno_t gridY, lno_t gridZ, bool expectBandwidthRed

template <typename lno_t, typename size_type, typename device>
void test_rcm_4clique() {
using graph_t = Kokkos::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using graph_t = KokkosSparse::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using rowmap_t = typename graph_t::row_map_type::non_const_type;
using entries_t = typename graph_t::entries_type::non_const_type;
rowmap_t rowmap("rowmap", 5);
Expand All @@ -156,7 +156,7 @@ void test_rcm_4clique() {

template <typename lno_t, typename size_type, typename device>
void test_rcm_multiple_components() {
using graph_t = Kokkos::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using graph_t = KokkosSparse::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device, void, size_type>;
using rowmap_t = typename graph_t::row_map_type::non_const_type;
using entries_t = typename graph_t::entries_type::non_const_type;
// Generate a single 3D grid first
Expand Down
3 changes: 2 additions & 1 deletion perf_test/graph/KokkosGraph_triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <iostream>
#include "KokkosKernels_IOUtils.hpp"
#include "KokkosGraph_Triangle.hpp"
#include "KokkosSparse_StaticCrsGraph.hpp"
#include "KokkosSparse_CrsMatrix.hpp"
#include "KokkosSparse_IOUtils.hpp" //for read_kokkos_crst_graph
#include "KokkosKernels_TestStringUtils.hpp"
Expand Down Expand Up @@ -232,7 +233,7 @@ void run_experiment(int argc, char **argv, perf_test::CommonInputParams) {
using device_t = Kokkos::Device<exec_space, mem_space>;
using lno_t = KokkosKernels::default_lno_t;
using size_type = KokkosKernels::default_size_type;
using graph_t = Kokkos::StaticCrsGraph<lno_t, KokkosKernels::default_layout, device_t, void, size_type>;
using graph_t = StaticCrsGraph<lno_t, KokkosKernels::default_layout, device_t, void, size_type>;
using KernelHandle =
KokkosKernels::Experimental::KokkosKernelsHandle<size_type, lno_t, lno_t, exec_space, mem_space, mem_space>;

Expand Down
2 changes: 1 addition & 1 deletion perf_test/sparse/KokkosSparse_spmv_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int main(int argc, char **argv) {

if (check_errors) {
h_y_compare = Kokkos::create_mirror(y);
typename matrix_type::StaticCrsGraphType::HostMirror h_graph = Kokkos::create_mirror(A.graph);
typename matrix_type::StaticCrsGraphType::HostMirror h_graph = KokkosSparse::create_mirror(A.graph);
typename matrix_type::values_type::HostMirror h_values = Kokkos::create_mirror_view(A.values);

// Error Check Gold Values
Expand Down
2 changes: 1 addition & 1 deletion perf_test/sparse/KokkosSparse_spmv_struct_tuning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ int main(int argc, char** argv) {

if (check_errors) {
h_y_compare = Kokkos::create_mirror(y);
typename matrix_type::StaticCrsGraphType::HostMirror h_graph = Kokkos::create_mirror(A.graph);
typename matrix_type::StaticCrsGraphType::HostMirror h_graph = KokkosSparse::create_mirror(A.graph);
typename matrix_type::values_type::HostMirror h_values = Kokkos::create_mirror_view(A.values);

// Error Check Gold Values
Expand Down
2 changes: 1 addition & 1 deletion perf_test/sparse/KokkosSparse_spmv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ SPMVTestData setup_test(spmv_additional_data* data, SPMVTestData::matrix_type A,
test_data.h_y = Kokkos::create_mirror_view(y);
test_data.h_y_compare = Kokkos::create_mirror(y);

h_graph_type h_graph = Kokkos::create_mirror(test_data.A.graph);
h_graph_type h_graph = KokkosSparse::create_mirror(test_data.A.graph);
h_values_type h_values = Kokkos::create_mirror_view(test_data.A.values);

for (int i = 0; i < test_data.numCols; i++) {
Expand Down
9 changes: 3 additions & 6 deletions sparse/src/KokkosSparse_BsrMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <type_traits>

#include "Kokkos_Core.hpp"
#include "Kokkos_StaticCrsGraph.hpp"
#include "Kokkos_ArithTraits.hpp"
#include "KokkosSparse_CrsMatrix.hpp"
#include "KokkosKernels_Error.hpp"
Expand Down Expand Up @@ -332,11 +331,9 @@ class BsrMatrix {
//! Type of a host-memory mirror of the sparse matrix.
typedef BsrMatrix<ScalarType, OrdinalType, host_mirror_space, MemoryTraits, size_type> HostMirror;
//! Type of the graph structure of the sparse matrix.
typedef Kokkos::StaticCrsGraph<ordinal_type, Kokkos::LayoutLeft, device_type, memory_traits, size_type>
StaticCrsGraphType;
typedef StaticCrsGraph<ordinal_type, Kokkos::LayoutLeft, device_type, memory_traits, size_type> StaticCrsGraphType;
//! Type of the graph structure of the sparse matrix - consistent with Kokkos.
typedef Kokkos::StaticCrsGraph<ordinal_type, Kokkos::LayoutLeft, device_type, memory_traits, size_type>
staticcrsgraph_type;
typedef StaticCrsGraph<ordinal_type, Kokkos::LayoutLeft, device_type, memory_traits, size_type> staticcrsgraph_type;
//! Type of column indices in the sparse matrix.
typedef typename staticcrsgraph_type::entries_type index_type;
//! Const version of the type of column indices in the sparse matrix.
Expand Down Expand Up @@ -700,7 +697,7 @@ class BsrMatrix {
// create_staticcrsgraph takes the frequency of blocks per row
// and returns the cum sum pointer row_map with nbrows+1 size, and total
// numBlocks in the final entry
graph = Kokkos::create_staticcrsgraph<staticcrsgraph_type>("blockgraph", block_rows);
graph = create_staticcrsgraph<staticcrsgraph_type>("blockgraph", block_rows);
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
79 changes: 2 additions & 77 deletions sparse/src/KokkosSparse_CcsMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,85 +29,10 @@
#include <stdexcept>
#include <type_traits>
#include "KokkosSparse_findRelOffset.hpp"
#include "KokkosSparse_StaticCcsGraph.hpp"
#include "KokkosKernels_default_types.hpp"
#include "KokkosKernels_Macros.hpp"

namespace Kokkos {
/// \class StaticCcsGraph
/// \brief Compressed column storage array copied from Kokkos::StaticCrsGraph.
///
/// \tparam DataType The type of stored entries. If a StaticCcsGraph is
/// used as the graph of a sparse matrix, then this is usually an
/// integer type, the type of the column indices in the sparse
/// matrix.
///
/// \tparam Arg1Type The second template parameter, corresponding
/// either to the Device type (if there are no more template
/// parameters) or to the Layout type (if there is at least one more
/// template parameter).
///
/// \tparam Arg2Type The third template parameter, which if provided
/// corresponds to the Device type.
///
/// \tparam Arg3Type The third template parameter, which if provided
/// corresponds to the MemoryTraits.
///
/// \tparam SizeType The type of col offsets. Usually the default
/// parameter suffices. However, setting a nondefault value is
/// necessary in some cases, for example, if you want to have a
/// sparse matrices with dimensions (and therefore column indices)
/// that fit in \c int, but want to store more than <tt>INT_MAX</tt>
/// entries in the sparse matrix.
///
/// A col has a range of entries:
/// <ul>
/// <li> <tt> col_map[i0] <= entry < col_map[i0+1] </tt> </li>
/// <li> <tt> 0 <= i1 < col_map[i0+1] - col_map[i0] </tt> </li>
/// <li> <tt> entries( entry , i2 , i3 , ... ); </tt> </li>
/// <li> <tt> entries( col_map[i0] + i1 , i2 , i3 , ... ); </tt> </li>
/// </ul>
template <class DataType, class Arg1Type, class Arg2Type = void, class Arg3Type = void,
typename SizeType = typename ViewTraits<DataType*, Arg1Type, Arg2Type, Arg3Type>::size_type>
class StaticCcsGraph {
private:
using traits = ViewTraits<DataType*, Arg1Type, Arg2Type, Arg3Type>;

public:
using data_type = DataType;
using array_layout = typename traits::array_layout;
using execution_space = typename traits::execution_space;
using device_type = typename traits::device_type;
using memory_traits = typename traits::memory_traits;
using size_type = SizeType;

using col_map_type = View<const size_type*, array_layout, device_type, memory_traits>;
using entries_type = View<data_type*, array_layout, device_type, memory_traits>;
using row_block_type = View<const size_type*, array_layout, device_type, memory_traits>;

entries_type entries;
col_map_type col_map;

//! Construct an empty view.
KOKKOS_INLINE_FUNCTION
StaticCcsGraph() : entries(), col_map() {}

//! Copy constructor (shallow copy).
KOKKOS_INLINE_FUNCTION
StaticCcsGraph(const StaticCcsGraph& rhs) : entries(rhs.entries), col_map(rhs.col_map) {}

template <class EntriesType, class ColMapType>
KOKKOS_INLINE_FUNCTION StaticCcsGraph(const EntriesType& entries_, const ColMapType& col_map_)
: entries(entries_), col_map(col_map_) {}

/** \brief Return number of columns in the graph
*/
KOKKOS_INLINE_FUNCTION
size_type numCols() const {
return (col_map.extent(0) != 0) ? col_map.extent(0) - static_cast<size_type>(1) : static_cast<size_type>(0);
}
};
} // namespace Kokkos

namespace KokkosSparse {
/// \class CcsMatrix
/// \brief Compressed sparse column implementation of a sparse matrix.
Expand Down Expand Up @@ -142,7 +67,7 @@ class CcsMatrix {
//! Type of each (column) index in the matrix.
typedef OrdinalType ordinal_type;
//! Type of the graph structure of the sparse matrix - consistent with Kokkos.
typedef Kokkos::StaticCcsGraph<ordinal_type, KokkosKernels::default_layout, device_type, memory_traits, size_type>
typedef StaticCcsGraph<ordinal_type, KokkosKernels::default_layout, device_type, memory_traits, size_type>
staticccsgraph_type;
//! Type of the "column map" (which contains the offset for each column's
//! data).
Expand Down
17 changes: 8 additions & 9 deletions sparse/src/KokkosSparse_CrsMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#define KOKKOSSPARSE_CRSMATRIX_HPP_

#include "Kokkos_Core.hpp"
#include "Kokkos_StaticCrsGraph.hpp"
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include "KokkosSparse_findRelOffset.hpp"
#include "KokkosSparse_StaticCrsGraph.hpp"
#include "KokkosKernels_default_types.hpp"
#include "KokkosKernels_Macros.hpp"

Expand Down Expand Up @@ -344,10 +344,10 @@ class CrsMatrix {
//! Type of a host-memory mirror of the sparse matrix.
typedef CrsMatrix<ScalarType, OrdinalType, host_mirror_space, MemoryTraits, SizeType> HostMirror;
//! Type of the graph structure of the sparse matrix.
typedef Kokkos::StaticCrsGraph<ordinal_type, KokkosKernels::default_layout, device_type, memory_traits, size_type>
typedef StaticCrsGraph<ordinal_type, KokkosKernels::default_layout, device_type, memory_traits, size_type>
StaticCrsGraphType;
//! Type of the graph structure of the sparse matrix - consistent with Kokkos.
typedef Kokkos::StaticCrsGraph<ordinal_type, KokkosKernels::default_layout, device_type, memory_traits, size_type>
typedef StaticCrsGraph<ordinal_type, KokkosKernels::default_layout, device_type, memory_traits, size_type>
staticcrsgraph_type;
//! Type of column indices in the sparse matrix.
typedef typename staticcrsgraph_type::entries_type index_type;
Expand Down Expand Up @@ -436,13 +436,12 @@ class CrsMatrix {

/// \brief Construct with a graph that will be shared.
///
/// Allocate the values array for subsquent fill.
/// Allocate the values array for subsequent fill.
template <typename InOrdinal, typename InLayout, typename InDevice, typename InMemTraits, typename InSizeType>
[[deprecated(
"Use the constructor that accepts ncols as input "
"instead.")]] CrsMatrix(const std::string& label,
const Kokkos::StaticCrsGraph<InOrdinal, InLayout, InDevice, InMemTraits, InSizeType>&
graph_)
const StaticCrsGraph<InOrdinal, InLayout, InDevice, InMemTraits, InSizeType>& graph_)
: graph(graph_.entries, graph_.row_map),
values(label, graph_.entries.extent(0)),
numCols_(maximum_entry(graph_) + 1) {}
Expand All @@ -457,7 +456,7 @@ class CrsMatrix {
/// \param ncols [in] The number of columns.
template <typename InOrdinal, typename InLayout, typename InDevice, typename InMemTraits, typename InSizeType>
CrsMatrix(const std::string& label,
const Kokkos::StaticCrsGraph<InOrdinal, InLayout, InDevice, InMemTraits, InSizeType>& graph_,
const StaticCrsGraph<InOrdinal, InLayout, InDevice, InMemTraits, InSizeType>& graph_,
const OrdinalType& ncols)
: graph(graph_.entries, graph_.row_map), values(label, graph_.entries.extent(0)), numCols_(ncols) {}

Expand All @@ -471,11 +470,11 @@ class CrsMatrix {
/// \param graph_ The graph for storing the rowmap and col ids.
template <typename InOrdinal, typename InLayout, typename InDevice, typename InMemTraits, typename InSizeType>
CrsMatrix(const std::string&, const OrdinalType& ncols, const values_type& vals,
const Kokkos::StaticCrsGraph<InOrdinal, InLayout, InDevice, InMemTraits, InSizeType>& graph_)
const StaticCrsGraph<InOrdinal, InLayout, InDevice, InMemTraits, InSizeType>& graph_)
: graph(graph_.entries, graph_.row_map), values(vals), numCols_(ncols) {}

/// \brief Constructor that copies raw arrays of host data in
/// 3-array CRS (compresed row storage) format.
/// 3-array CRS (compressed row storage) format.
///
/// On input, the entries must be sorted by row. \c rowmap determines where
/// each row begins and ends. For each entry k (0 <= k < annz), \c cols[k]
Expand Down
108 changes: 108 additions & 0 deletions sparse/src/KokkosSparse_StaticCcsGraph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) 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.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#ifndef KOKKOSSPARSE_STATICCCSGRAPH_HPP_
#define KOKKOSSPARSE_STATICCCSGRAPH_HPP_

#include <Kokkos_Core.hpp>

namespace KokkosSparse {

/// \class StaticCcsGraph
/// \brief Compressed column storage array copied from Kokkos::StaticCrsGraph.
///
/// \tparam DataType The type of stored entries. If a StaticCcsGraph is
/// used as the graph of a sparse matrix, then this is usually an
/// integer type, the type of the column indices in the sparse
/// matrix.
///
/// \tparam Arg1Type The second template parameter, corresponding
/// either to the Device type (if there are no more template
/// parameters) or to the Layout type (if there is at least one more
/// template parameter).
///
/// \tparam Arg2Type The third template parameter, which if provided
/// corresponds to the Device type.
///
/// \tparam Arg3Type The fourth template parameter, which if provided
/// corresponds to the MemoryTraits.
///
/// \tparam SizeType The type of col offsets. Usually the default
/// parameter suffices. However, setting a nondefault value is
/// necessary in some cases, for example, if you want to have a
/// sparse matrices with dimensions (and therefore column indices)
/// that fit in \c int, but want to store more than <tt>INT_MAX</tt>
/// entries in the sparse matrix.
///
/// A col has a range of entries:
/// <ul>
/// <li> <tt> col_map[i0] <= entry < col_map[i0+1] </tt> </li>
/// <li> <tt> 0 <= i1 < col_map[i0+1] - col_map[i0] </tt> </li>
/// <li> <tt> entries( entry , i2 , i3 , ... ); </tt> </li>
/// <li> <tt> entries( col_map[i0] + i1 , i2 , i3 , ... ); </tt> </li>
/// </ul>
template <class DataType, class Arg1Type, class Arg2Type = void, class Arg3Type = void,
typename SizeType = typename Kokkos::ViewTraits<DataType*, Arg1Type, Arg2Type, Arg3Type>::size_type>
class StaticCcsGraph {
private:
using traits = Kokkos::ViewTraits<DataType*, Arg1Type, Arg2Type, Arg3Type>;

public:
using data_type = DataType;
using array_layout = typename traits::array_layout;
using execution_space = typename traits::execution_space;
using device_type = typename traits::device_type;
using memory_traits = typename traits::memory_traits;
using size_type = SizeType;

using col_map_type = Kokkos::View<const size_type*, array_layout, device_type, memory_traits>;
using entries_type = Kokkos::View<data_type*, array_layout, device_type, memory_traits>;
using row_block_type = Kokkos::View<const size_type*, array_layout, device_type, memory_traits>;

entries_type entries;
col_map_type col_map;

//! Construct an empty view.
KOKKOS_INLINE_FUNCTION
StaticCcsGraph() : entries(), col_map() {}

//! Copy constructor (shallow copy).
KOKKOS_INLINE_FUNCTION
StaticCcsGraph(const StaticCcsGraph& rhs) : entries(rhs.entries), col_map(rhs.col_map) {}

template <class EntriesType, class ColMapType>
KOKKOS_INLINE_FUNCTION StaticCcsGraph(const EntriesType& entries_, const ColMapType& col_map_)
: entries(entries_), col_map(col_map_) {}

/** \brief Return number of columns in the graph
*/
KOKKOS_INLINE_FUNCTION
size_type numCols() const {
return (col_map.extent(0) != 0) ? col_map.extent(0) - static_cast<size_type>(1) : static_cast<size_type>(0);
}
};

} // namespace KokkosSparse

#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
namespace Kokkos {
template <class DataType, class Arg1Type, class Arg2Type = void, class Arg3Type = void,
typename SizeType = typename Kokkos::ViewTraits<DataType*, Arg1Type, Arg2Type, Arg3Type>::size_type>
using StaticCcsGraph = KokkosSparse::StaticCcsGraph<DataType, Arg1Type, Arg2Type, Arg3Type, SizeType>;
}
#endif

#endif
Loading
Loading