RaftLib  0.3a
C++ Stream Processing Template Library
mapbase.hpp
1 
29 #ifndef _MAPBASE_HPP_
30 #define _MAPBASE_HPP_ 1
31 #include <typeinfo>
32 #include <cassert>
33 #include <cxxabi.h>
34 #include <thread>
35 #include <sstream>
36 
37 #include "portexception.hpp"
38 #include "schedule.hpp"
39 #include "simpleschedule.hpp"
40 #include "kernel.hpp"
41 #include "port_info.hpp"
42 #include "allocate.hpp"
43 #include "dynalloc.hpp"
44 #include "stdalloc.hpp"
45 
52 {
53 public:
55  src( a ),
56  dst( b )
57  {
58  }
59 
60  kernel_pair_t( const kernel_pair_t &other ) : src( other.src ),
61  dst( other.dst )
62  {
63  }
64 
65  kernel_pair_t& operator == ( kernel_pair_t &&other )
66  {
67  src = other.src;
68  dst = other.dst;
69  return( *this );
70  }
71 
72  raft::kernel& getSrc()
73  {
74  return( *src );
75  }
76 
77  raft::kernel& getDst()
78  {
79  return( *dst );
80  }
81 
82 private:
83  raft::kernel *src;
84  raft::kernel *dst;
85 };
86 
87 
93 namespace order
94 {
95  enum spec { in, out };
96 }
97 
98 class MapBase
99 {
100 public:
105  MapBase();
109  virtual ~MapBase();
110 
111 
128  template < order::spec t = order::in >
130  {
132  if( ! a->input.hasPorts() )
133  {
134  source_kernels.insert( a );
135  }
136  all_kernels.insert( a );
137  all_kernels.insert( b );
138  PortInfo *port_info_a;
139  try{
140  port_info_a = &(a->output.getPortInfo());
141  }
142  catch( PortNotFoundException &ex )
143  {
144  int status( 0 );
145  std::stringstream ss;
146  ss <<
147  "Source port from kernel " <<
148  abi::__cxa_demangle( typeid( *a ).name(), 0, 0, &status ) <<
149  "has more than a single port.";
150 
151  throw AmbiguousPortAssignmentException( ss.str() );
152  }
153  PortInfo *port_info_b;
154  try{
155  port_info_b = &(b->input.getPortInfo());
156  }
157  catch( PortNotFoundException &ex )
158  {
159  int status( 0 );
160  std::stringstream ss;
161  ss << "Destination port from kernel " <<
162  abi::__cxa_demangle( typeid( *b ).name(), 0, 0, &status ) <<
163  "has more than a single port.";
164  throw AmbiguousPortAssignmentException( ss.str() );
165  }
166 
167  join( *a, port_info_a->my_name, *port_info_a,
168  *b, port_info_b->my_name, *port_info_b );
169  switch( t )
170  {
171  case( order::in ):
172  {
173  port_info_a->out_of_order = false;
174  port_info_b->out_of_order = false;
175  }
176  break;
177  case( order::out ):
178  {
179  port_info_a->out_of_order = true;
180  port_info_b->out_of_order = true;
181  }
182  break;
183  }
184  return( kernel_pair_t( a, b ) );
185  }
186 
201  template < order::spec t = order::in >
202  kernel_pair_t link( raft::kernel *a, const std::string a_port, raft::kernel *b )
203  {
204  if( ! a->input.hasPorts() )
205  {
206  source_kernels.insert( a );
207  }
208  all_kernels.insert( a );
209  all_kernels.insert( b );
210  PortInfo &port_info_a( a->output.getPortInfoFor( a_port ) );
211 
212  PortInfo *port_info_b;
213  try{
214  port_info_b = &(b->input.getPortInfo());
215  }
216  catch( PortNotFoundException &ex )
217  {
218  int status( 0 );
219  std::stringstream ss;
220  ss << "Destination port from kernel " <<
221  abi::__cxa_demangle( typeid( *b ).name(), 0, 0, &status ) <<
222  "has more than a single port.";
223  throw AmbiguousPortAssignmentException( ss.str() );
224  }
225  join( *a, a_port , port_info_a,
226  *b, port_info_b->my_name, *port_info_b );
227  switch( t )
228  {
229  case( order::in ):
230  {
231  port_info_a.out_of_order = false;
232  port_info_b->out_of_order = false;
233  }
234  break;
235  case( order::out ):
236  {
237  port_info_a.out_of_order = true;
238  port_info_b->out_of_order = true;
239  }
240  break;
241  }
242  return( kernel_pair_t( a, b ) );
243  }
244 
245 
260  template < order::spec t = order::in >
261  kernel_pair_t link( raft::kernel *a, raft::kernel *b, const std::string b_port )
262  {
263  if( ! a->input.hasPorts() )
264  {
265  source_kernels.insert( a );
266  }
267  all_kernels.insert( a );
268  all_kernels.insert( b );
269  PortInfo *port_info_a;
270  try{
271  port_info_a = &(a->output.getPortInfo() );
272  }
273  catch( PortNotFoundException &ex )
274  {
275  std::stringstream ss;
276  int status( 0 );
277  ss << "Source port from kernel " <<
278  abi::__cxa_demangle( typeid( *a ).name(), 0, 0, &status ) <<
279  "has more than a single port.";
280  throw AmbiguousPortAssignmentException( ss.str() );
281  }
282 
283  PortInfo &port_info_b( b->input.getPortInfoFor( b_port) );
284 
285  join( *a, port_info_a->my_name, *port_info_a,
286  *b, b_port, port_info_b );
287  switch( t )
288  {
289  case( order::in ):
290  {
291  port_info_a->out_of_order = false;
292  port_info_b.out_of_order = false;
293  }
294  break;
295  case( order::out ):
296  {
297  port_info_a->out_of_order = true;
298  port_info_b.out_of_order = true;
299  }
300  break;
301  }
302  return( kernel_pair_t( a, b ) );
303  }
304 
317  template < order::spec t = order::in >
318  kernel_pair_t link( raft::kernel *a, const std::string a_port,
319  raft::kernel *b, const std::string b_port )
320  {
321  if( ! a->input.hasPorts() )
322  {
323  source_kernels.insert( a );
324  }
325  all_kernels.insert( a );
326  all_kernels.insert( b );
327  auto &port_info_a( a->output.getPortInfoFor( a_port ) );
328  auto &port_info_b( b->input.getPortInfoFor( b_port) );
329 
330  join( *a, a_port, port_info_a,
331  *b, b_port, port_info_b );
332  switch( t )
333  {
334  case( order::in ):
335  {
336  port_info_a.out_of_order = false;
337  port_info_b.out_of_order = false;
338  }
339  break;
340  case( order::out ):
341  {
342  port_info_a.out_of_order = true;
343  port_info_b.out_of_order = true;
344  }
345  break;
346  }
347  return( kernel_pair_t( a, b ) );
348  }
349 
350 
351 
352 protected:
353 
354 
369  void join( raft::kernel &a, const std::string name_a, PortInfo &a_info,
370  raft::kernel &b, const std::string name_b, PortInfo &b_info );
371 
372 
374  std::set< raft::kernel* > source_kernels;
375 
377  std::set< raft::kernel* > all_kernels;
378 };
379 #endif /* END _MAPBASE_HPP_ */
void join(raft::kernel &a, const std::string name_a, PortInfo &a_info, raft::kernel &b, const std::string name_b, PortInfo &b_info)
Definition: mapbase.cpp:47
Port input
Definition: kernel.hpp:100
std::set< raft::kernel * > all_kernels
Definition: mapbase.hpp:377
PortInfo & getPortInfoFor(const std::string port_name)
Definition: port.cpp:104
kernel_pair_t link(raft::kernel *a, const std::string a_port, raft::kernel *b, const std::string b_port)
Definition: mapbase.hpp:318
PortInfo & getPortInfo()
Definition: port.cpp:117
Definition: mapbase.hpp:98
kernel_pair_t link(raft::kernel *a, raft::kernel *b, const std::string b_port)
Definition: mapbase.hpp:261
std::set< raft::kernel * > source_kernels
Definition: mapbase.hpp:374
kernel_pair_t link(raft::kernel *a, const std::string a_port, raft::kernel *b)
Definition: mapbase.hpp:202
virtual bool hasPorts()
Definition: port.cpp:80
MapBase()
Definition: mapbase.cpp:35
kernel_pair_t link(raft::kernel *a, raft::kernel *b)
Definition: mapbase.hpp:129
Definition: mapbase.hpp:93
Definition: portexception.hpp:39
Definition: mapbase.hpp:51
Definition: port_info.hpp:39
Definition: kernel.hpp:48
Definition: portexception.hpp:57
virtual ~MapBase()
Definition: mapbase.cpp:40