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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
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 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 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