45 #ifndef KOKKOS_STATICCRSGRAPH_HPP
46 #define KOKKOS_STATICCRSGRAPH_HPP
51 #include <Kokkos_View.hpp>
53 #include <Kokkos_Parallel_Reduce.hpp>
58 template <
class RowOffsetsType,
class RowBlockOffsetsType>
59 struct StaticCrsGraphBalancerFunctor {
60 typedef typename RowOffsetsType::non_const_value_type int_type;
61 RowOffsetsType row_offsets;
62 RowBlockOffsetsType row_block_offsets;
64 int_type cost_per_row, num_blocks;
66 StaticCrsGraphBalancerFunctor(RowOffsetsType row_offsets_,
67 RowBlockOffsetsType row_block_offsets_,
68 int_type cost_per_row_, int_type num_blocks_)
69 : row_offsets(row_offsets_),
70 row_block_offsets(row_block_offsets_),
71 cost_per_row(cost_per_row_),
72 num_blocks(num_blocks_) {}
74 KOKKOS_INLINE_FUNCTION
75 void operator()(
const int_type& iRow)
const {
76 const int_type num_rows = row_offsets.extent(0) - 1;
77 const int_type num_entries = row_offsets(num_rows);
78 const int_type total_cost = num_entries + num_rows * cost_per_row;
80 const double cost_per_workset = 1.0 * total_cost / num_blocks;
82 const int_type row_cost =
83 row_offsets(iRow + 1) - row_offsets(iRow) + cost_per_row;
85 int_type count = row_offsets(iRow + 1) + cost_per_row * iRow;
87 if (iRow == num_rows - 1) row_block_offsets(num_blocks) = num_rows;
90 int_type current_block =
91 (count - row_cost - cost_per_row) / cost_per_workset;
92 int_type end_block = count / cost_per_workset;
95 if (current_block >= num_blocks - 2) {
96 if ((current_block == num_blocks - 2) &&
97 (count >= (current_block + 1) * cost_per_workset)) {
99 int_type cc = count - row_cost - cost_per_row;
100 int_type block = cc / cost_per_workset;
101 while ((block > 0) && (block == current_block)) {
102 cc = row_offsets(row) + row * cost_per_row;
103 block = cc / cost_per_workset;
106 if ((count - cc - row_cost - cost_per_row) <
107 num_entries - row_offsets(iRow + 1)) {
108 row_block_offsets(current_block + 1) = iRow + 1;
110 row_block_offsets(current_block + 1) = iRow;
114 if ((count >= (current_block + 1) * cost_per_workset) ||
115 (iRow + 2 == int_type(row_offsets.extent(0)))) {
116 if (end_block > current_block + 1) {
117 int_type num_block = end_block - current_block;
118 row_block_offsets(current_block + 1) = iRow;
119 for (int_type block = current_block + 2; block <= end_block;
121 if ((block < current_block + 2 + (num_block - 1) / 2))
122 row_block_offsets(block) = iRow;
124 row_block_offsets(block) = iRow + 1;
126 row_block_offsets(current_block + 1) = iRow + 1;
170 template <
class GraphType>
195 KOKKOS_INLINE_FUNCTION
198 : colidx_(colidx_in), stride_(stride),
length(count) {}
212 template <
class OffsetType>
214 const typename GraphType::entries_type& colidx_in,
216 const OffsetType& idx,
217 const typename std::enable_if<std::is_integral<OffsetType>::value,
219 : colidx_(&colidx_in(idx)), stride_(stride),
length(count) {}
238 KOKKOS_INLINE_FUNCTION
240 return colidx_[i * stride_];
244 KOKKOS_INLINE_FUNCTION
281 template <
class DataType,
class Arg1Type,
class Arg2Type = void,
282 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
284 typename ViewTraits<DataType*, Arg1Type, Arg2Type>::size_type,
285 class Arg3Type =
void>
287 class Arg3Type = void,
288 typename SizeType =
typename ViewTraits<DataType*, Arg1Type, Arg2Type,
289 Arg3Type>::size_type>
296 typedef DataType data_type;
297 typedef typename traits::array_layout array_layout;
298 typedef typename traits::execution_space execution_space;
299 typedef typename traits::device_type device_type;
300 typedef typename traits::memory_traits memory_traits;
301 typedef SizeType size_type;
303 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
307 typename traits::host_mirror_space, size_type,
314 typename traits::host_mirror_space, memory_traits,
331 KOKKOS_INLINE_FUNCTION
335 KOKKOS_INLINE_FUNCTION
337 : entries(rhs.entries),
338 row_map(rhs.row_map),
339 row_block_offsets(rhs.row_block_offsets) {}
341 template <
class EntriesType,
class RowMapType>
342 KOKKOS_INLINE_FUNCTION
StaticCrsGraph(
const EntriesType& entries_,
343 const RowMapType& row_map_)
344 : entries(entries_), row_map(row_map_), row_block_offsets() {}
350 KOKKOS_INLINE_FUNCTION
352 entries = rhs.entries;
353 row_map = rhs.row_map;
354 row_block_offsets = rhs.row_block_offsets;
361 KOKKOS_DEFAULTED_FUNCTION
366 KOKKOS_INLINE_FUNCTION
368 return (row_map.
extent(0) != 0)
369 ? row_map.
extent(0) -
static_cast<size_type
>(1)
370 : static_cast<size_type>(0);
391 KOKKOS_INLINE_FUNCTION
393 const size_type start = row_map(i);
396 const data_type count =
static_cast<data_type
>(row_map(i + 1) - start);
409 size_type fix_cost_per_row = 4) {
411 "StatisCrsGraph::load_balance_offsets", num_blocks + 1);
413 Impl::StaticCrsGraphBalancerFunctor<
415 partitioner(row_map, block_offsets, fix_cost_per_row, num_blocks);
420 typename device_type::execution_space().fence();
422 row_block_offsets = block_offsets;
428 template <
class StaticCrsGraphType,
class InputSizeType>
429 typename StaticCrsGraphType::staticcrsgraph_type create_staticcrsgraph(
430 const std::string& label,
const std::vector<InputSizeType>& input);
432 template <
class StaticCrsGraphType,
class InputSizeType>
433 typename StaticCrsGraphType::staticcrsgraph_type create_staticcrsgraph(
434 const std::string& label,
435 const std::vector<std::vector<InputSizeType> >& input);
439 template <
class DataType,
class Arg1Type,
class Arg2Type,
440 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
441 typename SizeType,
class Arg3Type>
442 typename StaticCrsGraph<DataType, Arg1Type, Arg2Type, SizeType,
443 Arg3Type>::HostMirror
444 create_mirror_view(
const StaticCrsGraph<DataType, Arg1Type, Arg2Type, SizeType,
447 class Arg3Type, typename SizeType>
448 typename StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
449 SizeType>::HostMirror
450 create_mirror_view(const StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
454 template <class DataType, class Arg1Type, class Arg2Type,
455 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
456 typename SizeType, class Arg3Type>
457 typename StaticCrsGraph<DataType, Arg1Type, Arg2Type, SizeType,
458 Arg3Type>::HostMirror
459 create_mirror_view(const StaticCrsGraph<DataType, Arg1Type, Arg2Type, SizeType,
462 class Arg3Type, typename SizeType>
463 typename StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
464 SizeType>::HostMirror
465 create_mirror(const StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
474 #include <impl/Kokkos_StaticCrsGraph_factory.hpp>
482 template <
class GraphType>
483 struct StaticCrsGraphMaximumEntry {
484 typedef typename GraphType::execution_space execution_space;
485 typedef typename GraphType::data_type value_type;
487 const typename GraphType::entries_type entries;
489 StaticCrsGraphMaximumEntry(
const GraphType& graph) : entries(graph.entries) {}
491 KOKKOS_INLINE_FUNCTION
492 void operator()(
const unsigned i, value_type& update)
const {
493 if (update < entries(i)) update = entries(i);
496 KOKKOS_INLINE_FUNCTION
497 void init(value_type& update)
const { update = 0; }
499 KOKKOS_INLINE_FUNCTION
500 void join(
volatile value_type& update,
501 volatile const value_type& input)
const {
502 if (update < input) update = input;
508 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE
509 template <
class DataType,
class Arg1Type,
class Arg2Type,
typename SizeType,
511 DataType maximum_entry(
const StaticCrsGraph<DataType, Arg1Type, Arg2Type,
512 SizeType, Arg3Type>& graph) {
513 typedef StaticCrsGraph<DataType, Arg1Type, Arg2Type, SizeType, Arg3Type>
516 template <
class DataType,
class Arg1Type,
class Arg2Type,
class Arg3Type,
518 DataType maximum_entry(
const StaticCrsGraph<DataType, Arg1Type, Arg2Type,
519 Arg3Type, SizeType>& graph) {
520 typedef StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type, SizeType>
523 typedef Impl::StaticCrsGraphMaximumEntry<GraphType> FunctorType;
527 FunctorType(graph), result);
KOKKOS_INLINE_FUNCTION GraphRowViewConst< StaticCrsGraph > rowConst(const data_type i) const
Return a const view of row i of the graph.
KOKKOS_INLINE_FUNCTION StaticCrsGraph()
Construct an empty view.
const ordinal_type length
Number of entries in the row.
KOKKOS_INLINE_FUNCTION StaticCrsGraph(const StaticCrsGraph &rhs)
Copy constructor (shallow copy).
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< std::is_integral< iType >::value, size_t >::type extent(const iType &r) const noexcept
rank() to be implemented
KOKKOS_INLINE_FUNCTION GraphRowViewConst(ordinal_type *const colidx_in, const ordinal_type &stride, const ordinal_type &count)
Constructor.
KOKKOS_INLINE_FUNCTION ordinal_type & colidx(const ordinal_type &i) const
(Const) reference to the column index of entry i in this row of the sparse matrix.
KOKKOS_DEFAULTED_FUNCTION ~StaticCrsGraph()=default
Destroy this view of the array. If the last view then allocated memory is deallocated.
const GraphType::data_type ordinal_type
The type of the column indices in the row.
KOKKOS_INLINE_FUNCTION ordinal_type & operator()(const ordinal_type &i) const
An alias for colidx.
void create_block_partitioning(size_type num_blocks, size_type fix_cost_per_row=4)
Create a row partitioning into a given number of blocks balancing non-zeros + a fixed cost per row...
Declaration of parallel operators.
void parallel_reduce(const std::string &label, const PolicyType &policy, const FunctorType &functor, ReturnType &return_value, typename std::enable_if< Kokkos::Impl::is_execution_policy< PolicyType >::value >::type *=nullptr)
Parallel reduction.
KOKKOS_INLINE_FUNCTION GraphRowViewConst(const typename GraphType::entries_type &colidx_in, const ordinal_type &stride, const ordinal_type &count, const OffsetType &idx, const typename std::enable_if< std::is_integral< OffsetType >::value, int >::type &=0)
Constructor with offset into colidx array.
void parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename std::enable_if< Kokkos::Impl::is_execution_policy< ExecPolicy >::value >::type *=nullptr)
Execute functor in parallel according to the execution policy.
KOKKOS_INLINE_FUNCTION size_type numRows() const
Return number of rows in the graph.
Execution policy for work over a range of an integral type.
Compressed row storage array.
Traits class for accessing attributes of a View.
View of a row of a sparse graph.
KOKKOS_INLINE_FUNCTION StaticCrsGraph & operator=(const StaticCrsGraph &rhs)
Assign to a view of the rhs array. If the old view is the last view then allocated memory is dealloca...