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>
28 #include <Kokkos_Parallel.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>
147 struct GraphRowViewConst {
149 using ordinal_type =
const typename GraphType::data_type;
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_v<OffsetType>,
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>
260 class StaticCrsGraph {
262 using traits = ViewTraits<DataType*, Arg1Type, Arg2Type, Arg3Type>;
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;
272 using staticcrsgraph_type =
273 StaticCrsGraph<DataType, Arg1Type, Arg2Type, Arg3Type, SizeType>;
275 typename traits::host_mirror_space,
276 memory_traits, size_type>;
279 View<const size_type*, array_layout, device_type, memory_traits>;
281 View<data_type*, array_layout, device_type, memory_traits>;
282 using row_block_type =
283 View<const size_type*, array_layout, device_type, memory_traits>;
285 entries_type entries;
286 row_map_type row_map;
287 row_block_type row_block_offsets;
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
355 GraphRowViewConst<StaticCrsGraph>
rowConst(
const data_type i)
const {
356 const size_type start = row_map(i);
359 const data_type count =
static_cast<data_type
>(row_map(i + 1) - start);
362 return GraphRowViewConst<StaticCrsGraph>(
nullptr, 1, 0);
364 return GraphRowViewConst<StaticCrsGraph>(entries, 1, count, start);
372 size_type fix_cost_per_row = 4) {
373 View<size_type*, array_layout, device_type> block_offsets(
374 "StatisCrsGraph::load_balance_offsets", num_blocks + 1);
376 Impl::StaticCrsGraphBalancerFunctor<
377 row_map_type, View<size_type*, array_layout, device_type> >
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
KOKKOS_INLINE_FUNCTION StaticCrsGraph()
const ordinal_type length
Number of entries in the row.
KOKKOS_INLINE_FUNCTION GraphRowViewConst(ordinal_type *const colidx_in, const ordinal_type &stride, const ordinal_type &count)
KOKKOS_INLINE_FUNCTION ordinal_type & colidx(const ordinal_type &i) const
KOKKOS_DEFAULTED_FUNCTION ~StaticCrsGraph()=default
const typename GraphType::data_type ordinal_type
KOKKOS_INLINE_FUNCTION ordinal_type & operator()(const ordinal_type &i) const
void create_block_partitioning(size_type num_blocks, size_type fix_cost_per_row=4)
KOKKOS_INLINE_FUNCTION size_type numRows() const
Execution policy for work over a range of an integral type.
KOKKOS_INLINE_FUNCTION StaticCrsGraph & operator=(const StaticCrsGraph &rhs)