17 #ifndef KOKKOS_STATICCRSGRAPH_HPP
18 #define KOKKOS_STATICCRSGRAPH_HPP
19 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
20 #define KOKKOS_IMPL_PUBLIC_INCLUDE
21 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_STATICCRSGRAPH
27 #include <Kokkos_View.hpp>
29 #include <Kokkos_Parallel_Reduce.hpp>
34 template <
class RowOffsetsType,
class RowBlockOffsetsType>
35 struct StaticCrsGraphBalancerFunctor {
36 using int_type =
typename RowOffsetsType::non_const_value_type;
37 RowOffsetsType row_offsets;
38 RowBlockOffsetsType row_block_offsets;
40 int_type cost_per_row, num_blocks;
42 StaticCrsGraphBalancerFunctor(RowOffsetsType row_offsets_,
43 RowBlockOffsetsType row_block_offsets_,
44 int_type cost_per_row_, int_type num_blocks_)
45 : row_offsets(row_offsets_),
46 row_block_offsets(row_block_offsets_),
47 cost_per_row(cost_per_row_),
48 num_blocks(num_blocks_) {}
50 KOKKOS_INLINE_FUNCTION
51 void operator()(
const int_type& iRow)
const {
52 const int_type num_rows = row_offsets.extent(0) - 1;
53 const int_type num_entries = row_offsets(num_rows);
54 const int_type total_cost = num_entries + num_rows * cost_per_row;
56 const double cost_per_workset = 1.0 * total_cost / num_blocks;
58 const int_type row_cost =
59 row_offsets(iRow + 1) - row_offsets(iRow) + cost_per_row;
61 int_type count = row_offsets(iRow + 1) + cost_per_row * iRow;
63 if (iRow == num_rows - 1) row_block_offsets(num_blocks) = num_rows;
66 int_type current_block =
67 (count - row_cost - cost_per_row) / cost_per_workset;
68 int_type end_block = count / cost_per_workset;
71 if (current_block >= num_blocks - 2) {
72 if ((current_block == num_blocks - 2) &&
73 (count >= (current_block + 1) * cost_per_workset)) {
75 int_type cc = count - row_cost - cost_per_row;
76 int_type block = cc / cost_per_workset;
77 while ((block > 0) && (block == current_block)) {
78 cc = row_offsets(row) + row * cost_per_row;
79 block = cc / cost_per_workset;
82 if ((count - cc - row_cost - cost_per_row) <
83 num_entries - row_offsets(iRow + 1)) {
84 row_block_offsets(current_block + 1) = iRow + 1;
86 row_block_offsets(current_block + 1) = iRow;
90 if ((count >= (current_block + 1) * cost_per_workset) ||
91 (iRow + 2 == int_type(row_offsets.extent(0)))) {
92 if (end_block > current_block + 1) {
93 int_type num_block = end_block - current_block;
94 row_block_offsets(current_block + 1) = iRow;
95 for (int_type block = current_block + 2; block <= end_block;
97 if ((block < current_block + 2 + (num_block - 1) / 2))
98 row_block_offsets(block) = iRow;
100 row_block_offsets(block) = iRow + 1;
102 row_block_offsets(current_block + 1) = iRow + 1;
146 template <
class GraphType>
171 KOKKOS_INLINE_FUNCTION
174 : colidx_(colidx_in), stride_(stride),
length(count) {}
188 template <
class OffsetType>
190 const typename GraphType::entries_type& colidx_in,
192 const OffsetType& idx,
193 const std::enable_if_t<std::is_integral<OffsetType>::value,
int>& = 0)
194 : colidx_(&colidx_in(idx)), stride_(stride),
length(count) {}
213 KOKKOS_INLINE_FUNCTION
215 return colidx_[i * stride_];
219 KOKKOS_INLINE_FUNCTION
256 template <
class DataType,
class Arg1Type,
class Arg2Type = void,
257 class Arg3Type = void,
258 typename SizeType =
typename ViewTraits<DataType*, Arg1Type, Arg2Type,
259 Arg3Type>::size_type>
265 using data_type = DataType;
266 using array_layout =
typename traits::array_layout;
267 using execution_space =
typename traits::execution_space;
268 using device_type =
typename traits::device_type;
269 using memory_traits =
typename traits::memory_traits;
270 using size_type = SizeType;
275 typename traits::host_mirror_space,
276 memory_traits, size_type>;
290 KOKKOS_INLINE_FUNCTION
294 KOKKOS_INLINE_FUNCTION
296 : entries(rhs.entries),
297 row_map(rhs.row_map),
298 row_block_offsets(rhs.row_block_offsets) {}
300 template <
class EntriesType,
class RowMapType>
301 KOKKOS_INLINE_FUNCTION
StaticCrsGraph(
const EntriesType& entries_,
302 const RowMapType& row_map_)
303 : entries(entries_), row_map(row_map_), row_block_offsets() {}
309 KOKKOS_INLINE_FUNCTION
311 entries = rhs.entries;
312 row_map = rhs.row_map;
313 row_block_offsets = rhs.row_block_offsets;
320 KOKKOS_DEFAULTED_FUNCTION
325 KOKKOS_INLINE_FUNCTION
327 return (row_map.extent(0) != 0)
328 ? row_map.extent(0) -
static_cast<size_type
>(1)
329 : static_cast<size_type>(0);
332 KOKKOS_INLINE_FUNCTION constexpr
bool is_allocated()
const {
333 return (row_map.is_allocated() && entries.is_allocated());
354 KOKKOS_INLINE_FUNCTION
356 const size_type start = row_map(i);
359 const data_type count =
static_cast<data_type
>(row_map(i + 1) - start);
372 size_type fix_cost_per_row = 4) {
374 "StatisCrsGraph::load_balance_offsets", num_blocks + 1);
376 Impl::StaticCrsGraphBalancerFunctor<
378 partitioner(row_map, block_offsets, fix_cost_per_row, num_blocks);
380 Kokkos::parallel_for(
"Kokkos::StaticCrsGraph::create_block_partitioning",
383 typename device_type::execution_space().fence(
384 "Kokkos::StaticCrsGraph::create_block_partitioning:: fence after "
387 row_block_offsets = block_offsets;
393 template <
class StaticCrsGraphType,
class InputSizeType>
394 typename StaticCrsGraphType::staticcrsgraph_type create_staticcrsgraph(
395 const std::string& label,
const std::vector<InputSizeType>& input);
397 template <
class StaticCrsGraphType,
class InputSizeType>
398 typename StaticCrsGraphType::staticcrsgraph_type create_staticcrsgraph(
399 const std::string& label,
400 const std::vector<std::vector<InputSizeType> >& input);
404 template <
class DataType,
class Arg1Type,
class Arg2Type,
class Arg3Type,
406 typename StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
407 SizeType>::HostMirror
408 create_mirror_view(
const StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
411 template <
class DataType,
class Arg1Type,
class Arg2Type,
class Arg3Type,
413 typename StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
414 SizeType>::HostMirror
415 create_mirror(
const StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type,
423 #include <impl/Kokkos_StaticCrsGraph_factory.hpp>
431 template <
class GraphType>
432 struct StaticCrsGraphMaximumEntry {
433 using execution_space =
typename GraphType::execution_space;
434 using value_type =
typename GraphType::data_type;
436 const typename GraphType::entries_type entries;
438 StaticCrsGraphMaximumEntry(
const GraphType& graph) : entries(graph.entries) {}
440 KOKKOS_INLINE_FUNCTION
441 void operator()(
const unsigned i, value_type& update)
const {
442 if (update < entries(i)) update = entries(i);
445 KOKKOS_INLINE_FUNCTION
446 void init(value_type& update)
const { update = 0; }
448 KOKKOS_INLINE_FUNCTION
449 void join(value_type& update,
const value_type& input)
const {
450 if (update < input) update = input;
456 template <
class DataType,
class Arg1Type,
class Arg2Type,
class Arg3Type,
458 DataType maximum_entry(
const StaticCrsGraph<DataType, Arg1Type, Arg2Type,
459 Arg3Type, SizeType>& graph) {
461 StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type, SizeType>;
462 using FunctorType = Impl::StaticCrsGraphMaximumEntry<GraphType>;
465 Kokkos::parallel_reduce(
"Kokkos::maximum_entry", graph.entries.extent(0),
466 FunctorType(graph), result);
475 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_STATICCRSGRAPH
476 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
477 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_STATICCRSGRAPH
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 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 typename 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.
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.
KOKKOS_INLINE_FUNCTION GraphRowViewConst(const typename GraphType::entries_type &colidx_in, const ordinal_type &stride, const ordinal_type &count, const OffsetType &idx, const std::enable_if_t< std::is_integral< OffsetType >::value, int > &=0)
Constructor with offset into colidx array.
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...