RaftLib  0.3a
C++ Stream Processing Template Library
partition_old.hpp
1 
20 #ifndef _PARTITION_HPP_
21 #define _PARTITION_HPP_ 1
22 #include <cmath>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstdint>
26 #include <functional>
28 #include <scotch.h>
29 #include "graph.tcc"
30 #include "graphtools.hpp"
31 
32 class Partition
33 {
34 
35 public:
36  Partition() = delete;
37 
38  template < class Container,
39  class MappingContainer >
40  static
41  void
42  simple( Container &c,
43  MappingContainer &mapping,
44  const std::size_t cores )
45  {
46  if( simple_check( c, mapping, cores ) )
47  {
48  return;
49  }
50  auto weight_func(
51  []( PortInfo &a, PortInfo &b, void *weight_data ) -> std::int32_t
52  {
54  return( 1 );
55  }
56  );
57  run_scotch( c,
58  mapping,
59  cores,
60  weight_func,
61  nullptr );
62  return;
63  }
64 
65  template < class Container,
66  class MappingContainer >
67  static
68  void
69  utilization_weighted( Container &c,
70  MappingContainer &mapping,
71  const std::size_t cores )
72  {
73  if( simple_check( c, mapping, cores ) )
74  {
75  return;
76  }
78  auto weight_func(
79  []( PortInfo &a, PortInfo &b, void *weight_data ) -> std::int32_t
80  {
81  const auto size( a.getFIFO()->size() );
82  if( size > 0 )
83  {
84  return( size );
85  }
86  else
87  {
88  return( 1 );
89  }
90  }
91  );
92  run_scotch( c,
93  mapping,
94  cores,
95  weight_func,
96  nullptr );
97  return;
98  }
99 
100 
101 private:
102 
103  using weight_function_t =
104  typename std::function< std::int32_t( PortInfo&,PortInfo&,void* ) >;
105 
106  using raftgraph_t = raft::graph< std::int32_t,
107  std::int32_t >;
108 
109 
110 
111  template < class Container >
112  static
113  void get_graph_info( Container &c,
114  raftgraph_t &raft_graph,
115  weight_function_t weight_func,
116  void *weight_data )
117  {
118  using nummap_t = std::map< raft::kernel const *,
119  std::size_t >;
121  nummap_t numbering;
122  {
123  auto index( 0 );
124  for( raft::kernel const *k : c )
125  {
126  numbering.insert( std::make_pair( k, index++ ) );
127  }
128  }
129  struct LocalData
130  {
131  LocalData( raftgraph_t &g, nummap_t &m )
132  : graph( g ),
133  num_map( m )
134  {}
135 
136  raftgraph_t &graph;
137  nummap_t &num_map;
138  } d ( raft_graph, numbering );
139  auto graph_function =
140  [&]( PortInfo &a,
141  PortInfo &b,
142  void *data )
143  {
144  if( ! Schedule::isActive( a.my_kernel ) )
145  {
146  return;
147  }
148  auto * const local_data(
149  reinterpret_cast< LocalData* >( data ) );
150  const auto num_src( local_data->num_map[ a.my_kernel ] );
151  const auto num_dst( local_data->num_map[ b.my_kernel ] );
152  const auto weight( weight_func( a, b, weight_data ) );
153  local_data->graph.addEdge( num_src, num_dst, weight );
154  return;
155  };
156  GraphTools::BFS( c,
157  graph_function,
158  (void*) &d,
159  false );
160 
161  }
162 
166  template < class Container,
167  class MapContainer >
168  static
169  bool simple_check( Container &c,
170  MapContainer &mapping,
171  const std::size_t cores )
172  {
173  if( c.size() <= cores )
174  {
175  const auto length( c.size() );
176  for( auto i( 0 ); i < length; i++ )
177  {
178  mapping.emplace_back( i );
179  }
180  return( true );
181  }
183  return( false );
184  }
185 
189  template < class Container,
190  class MapContainer >
191  static
192  void run_scotch( Container &c,
193  MapContainer &mapping,
194  const std::size_t cores,
195  weight_function_t weight_func,
196  void *weight )
197  {
198 #if 0
199  static_assert( std::is_signed<
200  std::remove_reference< decltype( c.end() ) >::type >::value,
201  "Container must have signed types so that -1 may signify no mapping" );
202 #endif
203  raftgraph_t raft_graph;
204  get_graph_info( c,
205  raft_graph,
206  weight_func,
207  nullptr );
208  SCOTCH_Graph graph;
209  if( SCOTCH_graphInit( &graph ) != 0 )
210  {
212  std::cerr << "Failed to initialize graph!!\n";
213  exit( EXIT_FAILURE );
214  }
215  auto table( raft_graph.getScotchTables() );
216  if( SCOTCH_graphBuild(
217  &graph ,
218  0 ,
219  table.num_vertices ,
220  table.vtable ,
221  &table.vtable[ 1 ] ,
222  nullptr ,
223  nullptr ,
224  table.num_edges ,
225  table.etable ,
226  table.eweight
227  ) != 0 )
228  {
230  std::cerr << "Failed to build graph\n";
231  exit( EXIT_FAILURE );
232  }
233  if( SCOTCH_graphCheck( &graph ) != 0 )
234  {
236  std::cerr << "Graph is inconsistent\n";
237  std::remove_reference< decltype( table ) >::type::print( std::cerr, table );
238  std::cerr << "\n";
239  raft_graph.print( std::cerr );
240 
241  exit( EXIT_FAILURE );
242  }
244  SCOTCH_Arch archdat;
245  if( SCOTCH_archInit( &archdat ) != 0 )
246  {
248  std::cerr << "Architecture initialization failed\n";
249  exit( EXIT_FAILURE );
250  }
252  if( SCOTCH_archCmplt( &archdat, cores ) != 0 )
253  {
255  std::cerr << "Failed to create architecture file\n";
256  exit( EXIT_FAILURE );
257  }
259  SCOTCH_Strat stradat;
260  if( SCOTCH_stratInit( &stradat ) != 0 )
261  {
263  std::cerr << "Failed to init strategy!!\n";
264  exit( EXIT_FAILURE );
265  }
267  if( SCOTCH_stratGraphClusterBuild(
268  &stradat,
269  SCOTCH_STRATSPEED,
270  cores,
271  .75,
272  .01) != 0 )
273  {
275  std::cerr << "Failed to map strategy graph!!\n";
276  exit( EXIT_FAILURE );
277  }
278  if( SCOTCH_graphMap(
279  &graph ,
280  &archdat,
281  &stradat,
282  table.partition
283  ) != 0 )
284  {
286  std::cerr << "Failed to map!!\n";
287  exit( EXIT_FAILURE );
288  }
300  if( c.size() == table.num_vertices )
301  {
303  for( auto i( 0 ); i < table.num_vertices; i++ )
304  {
305  mapping.emplace_back( table.partition[ i ] );
306  }
307  }
308  else
309  {
310  const auto &vmapping( raft_graph.getVertexNumbersAtIndicies() );
311  auto it_map_index( vmapping.cbegin() );
312  auto table_index( 0 );
313  const auto size( c.size() );
314  for( auto i( 0 ); i < size; i++ )
315  {
316  if( i == (*it_map_index) && it_map_index != vmapping.cend() )
317  {
318  mapping.emplace_back( table.partition[ table_index++ ] );
319  ++it_map_index;
320  }
321  else
322  {
323  mapping.emplace_back( -1 );
324  }
325  }
326  }
328  SCOTCH_graphExit( &graph );
329  SCOTCH_stratExit( &stradat );
330  SCOTCH_archExit ( &archdat );
331  return;
332  }
333 
334 };
335 #endif /* END _PARTITION_HPP_ */
Definition: partition_old.hpp:32
static void simple(Container &c, MappingContainer &mapping, const std::size_t cores)
Definition: partition_old.hpp:42
virtual std::size_t size()=0
static void utilization_weighted(Container &c, MappingContainer &mapping, const std::size_t cores)
Definition: partition_old.hpp:69
Definition: port_info.hpp:39
FIFO * getFIFO()
Definition: port_info.cpp:49
Definition: kernel.hpp:57
static void BFS(std::set< raft::kernel * > &source_kernels, edge_func func, void *data=nullptr, bool connected_error=false)
Definition: graphtools.cpp:36