Skip to content

Commit

Permalink
src/sparse: Add crs2ccs
Browse files Browse the repository at this point in the history
  - Adds KokkosSparse::crs2ccs
  - Adds KokkosSparse::CcsMatrix
  - Rewrite ccs2crs to call transpose_matrix
  • Loading branch information
e10harvey committed Nov 10, 2022
1 parent ad1a51d commit 9c0b52d
Show file tree
Hide file tree
Showing 18 changed files with 1,149 additions and 356 deletions.
15 changes: 15 additions & 0 deletions docs/developer/apidocs/sparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ crsmatrix
.. doxygenclass:: KokkosSparse::CrsMatrix
:members:

ccsmatrix
---------
.. doxygenclass:: KokkosSparse::CcsMatrix
:members:

crs2ccs
-------
.. doxygenfunction:: KokkosSparse::crs2ccs(OrdinalType nrows, OrdinalType ncols, SizeType nnz, ValViewType vals, RowMapViewType row_map, ColIdViewType col_ids)
.. doxygenfunction:: KokkosSparse::crs2ccs(KokkosSparse::CrsMatrix<ScalarType, OrdinalType, DeviceType, MemoryTraitsType, SizeType> &crsMatrix)

ccs2crs
-------
.. doxygenfunction:: KokkosSparse::ccs2crs(OrdinalType nrows, OrdinalType ncols, SizeType nnz, ValViewType vals, ColMapViewType col_map, RowIdViewType row_ids)
.. doxygenfunction:: KokkosSparse::ccs2crs(KokkosSparse::CcsMatrix<ScalarType, OrdinalType, DeviceType, MemoryTraitsType, SizeType> &ccsMatrix)

spmv
----

Expand Down
2 changes: 1 addition & 1 deletion sparse/impl/KokkosSparse_spgemm_imp_outer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ void KokkosSPGEMM<HandleType, a_row_view_t_, a_lno_nnz_view_t_,
Kokkos::view_alloc(Kokkos::WithoutInitializing, "transpose_col_values"),
entriesA.extent(0));

KokkosKernels::Impl::transpose_matrix<
KokkosSparse::Impl::transpose_matrix<
const_a_lno_row_view_t, const_a_lno_nnz_view_t, const_a_scalar_nnz_view_t,
row_lno_temp_work_view_t, nnz_lno_temp_work_view_t,
scalar_temp_work_view_t, row_lno_temp_work_view_t, MyExecSpace>(
Expand Down
2 changes: 1 addition & 1 deletion sparse/impl/KokkosSparse_spmv_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct SPMV {
///
/// The first 5 template parameters are the template parameters of the
/// input 1-D View of coefficients 'alpha'. The next 5 template
/// parameters are the same as those of KokkosSparse::CrsMatrix. In
/// parameters are the same as those of KokkosSparse::CrsMatrix. In
/// particular:
///
/// AT: type of each entry of the sparse matrix
Expand Down
2 changes: 1 addition & 1 deletion sparse/impl/KokkosSparse_spmv_struct_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct SPMV_STRUCT {
///
/// The first 5 template parameters are the template parameters of the
/// input 1-D View of coefficients 'alpha'. The next 5 template
/// parameters are the same as those of KokkosSparse::CrsMatrix. In
/// parameters are the same as those of KokkosSparse::CrsMatrix. In
/// particular:
///
/// AT: type of each entry of the sparse matrix
Expand Down
293 changes: 293 additions & 0 deletions sparse/src/KokkosSparse_CcsMatrix.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
/*
//@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
*/

/// \file KokkosSparse_CcsMatrix.hpp
/// \brief Local sparse matrix interface
///
/// This file provides KokkosSparse::CcsMatrix. This implements a
/// local (no MPI) sparse matrix stored in compressed column sparse
/// ("Ccs") format.

#ifndef KOKKOS_SPARSE_CCSMATRIX_HPP_
#define KOKKOS_SPARSE_CCSMATRIX_HPP_

#include "Kokkos_Core.hpp"
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include "KokkosSparse_findRelOffset.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.
/// \tparam ScalarType The type of entries in the sparse matrix.
/// \tparam OrdinalType The type of column indices in the sparse matrix.
/// \tparam Device The Kokkos Device type.
/// \tparam MemoryTraits Traits describing how Kokkos manages and
/// accesses data. The default parameter suffices for most users.
///
/// "Ccs" stands for "compressed column sparse."
template <class ScalarType, class OrdinalType, class Device,
class MemoryTraits = void,
class SizeType = typename Kokkos::ViewTraits<OrdinalType*, Device,
void, void>::size_type>
class CcsMatrix {
static_assert(
std::is_signed<OrdinalType>::value,
"CcsMatrix requires that OrdinalType is a signed integer type.");

public:
//! Type of the matrix's execution space.
typedef typename Device::execution_space execution_space;
//! Type of the matrix's memory space.
typedef typename Device::memory_space memory_space;
//! Canonical device type
typedef Kokkos::Device<execution_space, memory_space> device_type;
typedef MemoryTraits memory_traits;

/// \brief Type of each entry of the "column map."
///
/// The "column map" corresponds to the \c ptr array of column offsets in
/// compressed sparse column (CCS) storage.
typedef SizeType size_type;
//! Type of each value in the matrix.
typedef ScalarType value_type;
//! 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, default_layout, device_type,
memory_traits, size_type>
staticccsgraph_type;
//! Type of the "column map" (which contains the offset for each column's
//! data).
typedef typename staticccsgraph_type::col_map_type col_map_type;
typedef Kokkos::View<value_type*, Kokkos::LayoutRight, device_type,
MemoryTraits>
values_type;
//! Type of column indices in the sparse matrix.
typedef typename staticccsgraph_type::entries_type index_type;

/// \name Storage of the actual sparsity structure and values.
///
/// CcsMatrix uses the compressed sparse column (CCS) storage format to
/// store the sparse matrix.
//@{
//! The graph (sparsity structure) of the sparse matrix.
staticccsgraph_type graph;
//! The 1-D array of values of the sparse matrix.
values_type values;
//@}

private:
/// \brief The number of rows in the CCS matrix
ordinal_type numRows_;

public:
/// \brief Default constructor; constructs an empty sparse matrix.
KOKKOS_INLINE_FUNCTION
CcsMatrix() : numRows_(0) {}

// clang-format off
/// \brief Constructor that accepts a column map, row indices, and
/// values.
///
/// The matrix will store and use the column map, indices, and values
/// directly (by view, not by deep copy).
///
/// \param label [in] The sparse matrix's label.
/// \param nrows [in] The number of rows.
/// \param ncols [in] The number of columns.
/// \param annz [in] The number of entries.
/// \param vals [in] The entries.
/// \param colmap [in] The column map (containing the offsets to the data in
/// each column).
/// \param rows [in] The row indices.
// clang-format on
CcsMatrix(const std::string& /* label */, const OrdinalType nrows,
const OrdinalType ncols, const size_type annz,
const values_type& vals, const col_map_type& colmap,
const index_type& rows)
: graph(rows, colmap), values(vals), numRows_(nrows) {
const ordinal_type actualNumRows =
(colmap.extent(0) != 0)
? static_cast<ordinal_type>(colmap.extent(0) -
static_cast<size_type>(1))
: static_cast<ordinal_type>(0);
if (ncols != actualNumRows) {
std::ostringstream os;
os << "Input argument ncols = " << ncols
<< " != the actual number of "
"rows "
<< actualNumRows << " according to the 'rows' input argument.";
throw std::invalid_argument(os.str());
}
if (annz != nnz()) {
std::ostringstream os;
os << "Input argument annz = " << annz << " != this->nnz () = " << nnz()
<< ".";
throw std::invalid_argument(os.str());
}
}

//! The number of rows in the sparse matrix.
KOKKOS_INLINE_FUNCTION ordinal_type numCols() const {
return graph.numCols();
}

//! The number of columns in the sparse matrix.
KOKKOS_INLINE_FUNCTION ordinal_type numRows() const { return numRows_; }

//! The number of "point" (non-block) rows in the matrix. Since Ccs is not
//! blocked, this is just the number of regular rows.
KOKKOS_INLINE_FUNCTION ordinal_type numPointRows() const { return numRows(); }

//! The number of "point" (non-block) columns in the matrix. Since Ccs is not
//! blocked, this is just the number of regular columns.
KOKKOS_INLINE_FUNCTION ordinal_type numPointCols() const { return numCols(); }

//! The number of stored entries in the sparse matrix.
KOKKOS_INLINE_FUNCTION size_type nnz() const {
return graph.entries.extent(0);
}
};

/// \class is_ccs_matrix
/// \brief is_ccs_matrix<T>::value is true if T is a CcsMatrix<...>, false
/// otherwise
template <typename>
struct is_ccs_matrix : public std::false_type {};
template <typename... P>
struct is_ccs_matrix<CcsMatrix<P...>> : public std::true_type {};
template <typename... P>
struct is_ccs_matrix<const CcsMatrix<P...>> : public std::true_type {};

} // namespace KokkosSparse
#endif
2 changes: 1 addition & 1 deletion sparse/src/KokkosSparse_CrsMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
//@HEADER
*/

/// \file Kokkos_Sparse_CrsMatrix.hpp
/// \file KokkosSparse_CrsMatrix.hpp
/// \brief Local sparse matrix interface
///
/// This file provides KokkosSparse::CrsMatrix. This implements a
Expand Down
4 changes: 2 additions & 2 deletions sparse/src/KokkosSparse_Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void transpose_matrix(
TransposeFunctor_t tm(num_rows, num_cols, xadj, adj, vals, t_xadj, t_adj,
t_vals, tmp_row_view, true, team_size);

Kokkos::parallel_for("KokkosKernels::Impl::transpose_matrix::S0",
Kokkos::parallel_for("KokkosSparse::Impl::transpose_matrix::S0",
count_tp_t((num_rows + team_size - 1) / team_size,
team_size, thread_size),
tm);
Expand All @@ -446,7 +446,7 @@ void transpose_matrix(
Kokkos::deep_copy(tmp_row_view, t_xadj);

Kokkos::parallel_for(
"KokkosKernels::Impl::transpose_matrix::S1",
"KokkosSparse::Impl::transpose_matrix::S1",
fill_tp_t((num_rows + team_size - 1) / team_size, team_size, thread_size),
tm);

Expand Down
Loading

0 comments on commit 9c0b52d

Please sign in to comment.