44 #ifndef KOKKOS_STATICCRSGRAPH_HPP 45 #define KOKKOS_STATICCRSGRAPH_HPP 50 #include <Kokkos_Core.hpp> 55 template<
class RowOffsetsType,
class RowBlockOffsetsType>
56 struct StaticCrsGraphBalancerFunctor {
57 typedef typename RowOffsetsType::non_const_value_type int_type;
58 RowOffsetsType row_offsets;
59 RowBlockOffsetsType row_block_offsets;
61 int_type cost_per_row, num_blocks;
63 StaticCrsGraphBalancerFunctor(RowOffsetsType row_offsets_,
64 RowBlockOffsetsType row_block_offsets_,
65 int_type cost_per_row_, int_type num_blocks_):
66 row_offsets(row_offsets_),
67 row_block_offsets(row_block_offsets_),
68 cost_per_row(cost_per_row_),
69 num_blocks(num_blocks_){}
71 KOKKOS_INLINE_FUNCTION
72 void operator() (
const int_type& iRow)
const {
73 const int_type num_rows = row_offsets.dimension_0()-1;
74 const int_type num_entries = row_offsets(num_rows);
75 const int_type total_cost = num_entries + num_rows*cost_per_row;
77 const double cost_per_workset = 1.0*total_cost/num_blocks;
79 const int_type row_cost = row_offsets(iRow+1)-row_offsets(iRow) + cost_per_row;
81 int_type count = row_offsets(iRow+1) + cost_per_row*iRow;
83 if(iRow == num_rows-1) row_block_offsets(num_blocks) = num_rows;
86 int_type current_block = (count-row_cost-cost_per_row)/cost_per_workset;
87 int_type end_block = count/cost_per_workset;
90 if(current_block >= num_blocks-2) {
91 if((current_block == num_blocks-2) && (count >= (current_block + 1) * cost_per_workset)) {
93 int_type cc = count-row_cost-cost_per_row;
94 int_type block = cc/cost_per_workset;
95 while((block>0) && (block==current_block)) {
96 cc = row_offsets(row)+row*cost_per_row;
97 block = cc/cost_per_workset;
100 if((count-cc-row_cost-cost_per_row) < num_entries-row_offsets(iRow+1)) {
101 row_block_offsets(current_block+1) = iRow+1;
103 row_block_offsets(current_block+1) = iRow;
107 if((count >= (current_block + 1) * cost_per_workset) ||
108 (iRow+2 == row_offsets.dimension_0())) {
109 if(end_block>current_block+1) {
110 int_type num_block = end_block-current_block;
111 row_block_offsets(current_block+1) = iRow;
112 for(int_type block = current_block+2; block <= end_block; block++)
113 if((block<current_block+2+(num_block-1)/2))
114 row_block_offsets(block) = iRow;
116 row_block_offsets(block) = iRow+1;
118 row_block_offsets(current_block+1) = iRow+1;
158 template<
class DataType,
160 class Arg2Type = void,
161 typename SizeType =
typename ViewTraits<DataType*, Arg1Type, Arg2Type, void >::size_type>
167 typedef DataType data_type;
168 typedef typename traits::array_layout array_layout;
169 typedef typename traits::execution_space execution_space;
171 typedef SizeType size_type;
179 entries_type entries;
180 row_map_type row_map;
181 row_block_type row_block_offsets;
188 row_block_offsets(rhs.row_block_offsets)
191 template<
class EntriesType,
class RowMapType>
192 StaticCrsGraph (
const EntriesType& entries_,
const RowMapType& row_map_) : entries (entries_), row_map (row_map_),
201 entries = rhs.entries;
202 row_map = rhs.row_map;
203 row_block_offsets = rhs.row_block_offsets;
214 KOKKOS_INLINE_FUNCTION
216 return (row_map.dimension_0 () != 0) ?
217 row_map.dimension_0 () -
static_cast<size_type
> (1) :
218 static_cast<size_type> (0);
226 block_offsets(
"StatisCrsGraph::load_balance_offsets",num_blocks+1);
228 Impl::StaticCrsGraphBalancerFunctor<row_map_type,View< size_type* , array_layout, device_type > >
229 partitioner(row_map,block_offsets,fix_cost_per_row,num_blocks);
234 row_block_offsets = block_offsets;
240 template<
class StaticCrsGraphType ,
class InputSizeType >
241 typename StaticCrsGraphType::staticcrsgraph_type
242 create_staticcrsgraph(
const std::string & label ,
243 const std::vector< InputSizeType > & input );
245 template<
class StaticCrsGraphType ,
class InputSizeType >
246 typename StaticCrsGraphType::staticcrsgraph_type
247 create_staticcrsgraph(
const std::string & label ,
248 const std::vector< std::vector< InputSizeType > > & input );
252 template<
class DataType ,
259 template<
class DataType ,
271 #include <impl/Kokkos_StaticCrsGraph_factory.hpp> 279 template<
class GraphType >
280 struct StaticCrsGraphMaximumEntry {
282 typedef typename GraphType::execution_space execution_space ;
283 typedef typename GraphType::data_type value_type ;
285 const typename GraphType::entries_type entries ;
287 StaticCrsGraphMaximumEntry(
const GraphType & graph ) : entries( graph.entries ) {}
289 KOKKOS_INLINE_FUNCTION
290 void operator()(
const unsigned i , value_type & update )
const 291 {
if ( update < entries(i) ) update = entries(i); }
293 KOKKOS_INLINE_FUNCTION
294 void init( value_type & update )
const 297 KOKKOS_INLINE_FUNCTION
298 void join(
volatile value_type & update ,
299 volatile const value_type & input )
const 300 {
if ( update < input ) update = input ; }
305 template<
class DataType,
class Arg1Type,
class Arg2Type,
typename SizeType >
309 typedef Impl::StaticCrsGraphMaximumEntry< GraphType > FunctorType ;
311 DataType result = 0 ;
313 FunctorType(graph), result );
void parallel_reduce(const std::string &label, const PolicyType &policy, const FunctorType &functor, ReturnType &return_value, typename Impl::enable_if< Kokkos::Impl::is_execution_policy< PolicyType >::value >::type *=0)
Parallel reduction.
~StaticCrsGraph()
Destroy this view of the array. If the last view then allocated memory is deallocated.
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...
StaticCrsGraph(const StaticCrsGraph &rhs)
Copy constructor (shallow copy).
Memory space for main process and CPU execution spaces.
void parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename Impl::enable_if< ! Impl::is_integral< ExecPolicy >::value >::type *=0)
Execute functor in parallel according to the execution policy.
Execution policy for work over a range of an integral type.
Compressed row storage array.
KOKKOS_INLINE_FUNCTION size_type numRows() const
Return number of rows in the graph.
Traits class for accessing attributes of a View.
StaticCrsGraph()
Construct an empty view.