My Project
port.hpp
1 
20 #ifndef _PORT_HPP_
21 #define _PORT_HPP_ 1
22 
23 #include <map>
24 #include <vector>
25 #include <string>
26 #include <utility>
27 #include <typeinfo>
28 #include <typeindex>
29 #include <functional>
30 #include <utility>
31 
32 #include "portbase.hpp"
33 #include "graphtools.hpp"
34 #include "ringbuffertypes.hpp"
35 #include "fifo.hpp"
36 #include "port_info.hpp"
37 #include "ringbuffer.tcc"
38 #include "port_info_types.hpp"
39 #include "portmap_t.hpp"
40 #include "portiterator.hpp"
41 #include "portexception.hpp"
42 
44 class MapBase;
45 class roundrobin;
46 class basic_parallel;
48 namespace raft
49 {
50  class kernel;
51  class parallel_k;
52  template < class T, class method > class join;
53  template < class T, class method > class split;
54 }
55 
56 
57 class Port : public PortBase
58 {
59 public:
66  Port( raft::kernel *k );
67 
76  Port( raft::kernel *k, void * const ptr, const std::size_t nbytes );
81  virtual ~Port();
82 
91  template < class T >
92  void addPort( const std::string &&port_name )
93  {
100  PortInfo pi( typeid( T ) );
101  pi.my_kernel = kernel;
102  pi.my_name = port_name;
103  (this)->initializeConstMap<T>( pi );
104  (this)->initializeSplit< T >( pi );
105  (this)->initializeJoin< T >( pi );
106  const auto ret_val(
107  portmap.map.insert( std::make_pair( port_name,
108  pi ) ) );
109 
110  if( not ret_val.second )
111  {
112  throw PortAlreadyExists( "FATAL ERROR: port \"" + port_name + "\" already exists!" );
113  }
114  return;
115  }
116 
123  template < class T >
124  bool addInPlacePorts( const std::size_t n_ports )
125  {
126  T *existing_buff_t( reinterpret_cast< T* >( alloc_ptr ) );
127  std::size_t length( alloc_ptr_length / sizeof( T ) );
128  const std::size_t inc( length / n_ports );
129  const std::size_t adder( length % n_ports );
130 
131  for( auto index( 0 ); index < n_ports; index++ )
132  {
133  const std::size_t start_index( index * inc );
134  PortInfo pi( typeid( T ),
135  (void*)&( existing_buff_t[ start_index ] ) ,
136  inc + ( index == (n_ports - 1) ? adder : 0 ),
137  start_index );
138  pi.my_kernel = kernel;
139  const std::string name( std::to_string( index ) );
140  pi.my_name = name;
141  (this)->initializeConstMap< T >( pi );
142  (this)->initializeSplit< T >( pi );
143  (this)->initializeJoin< T >( pi );
144  portmap.map.insert( std::make_pair( name, pi ) );
145  }
146  return( true );
147  }
148 
159  const std::type_index& getPortType( const std::string &&port_name );
160 
161 
166  virtual FIFO& operator[]( const std::string &&port_name );
167 
168 
174  virtual bool hasPorts();
175 
180  virtual PortIterator begin();
181 
186  virtual PortIterator end();
187 
192  std::size_t count();
193 
194  /*******
195  * SOME OPERATOR OVERLOADING STUFFS TO MAKE LIFE EASIER
196  */
197 
198 protected:
207  template < class T > void initializeConstMap( PortInfo &pi )
208  {
209  pi.const_map.insert(
210  std::make_pair( Type::Heap , new instr_map_t() ) );
211 
212  pi.const_map[ Type::Heap ]->insert(
213  std::make_pair( false ,
214  RingBuffer< T, Type::Heap, false >::make_new_fifo ) );
215  pi.const_map[ Type::Heap ]->insert(
216  std::make_pair( true ,
217  RingBuffer< T, Type::Heap, true >::make_new_fifo ) );
218 
219  pi.const_map.insert( std::make_pair( Type::SharedMemory, new instr_map_t() ) );
220  pi.const_map[ Type::SharedMemory ]->insert(
221  std::make_pair( false ,
222  RingBuffer< T, Type::SharedMemory >::make_new_fifo ) );
229  return;
230  }
231 
237  template < class T > void initializeSplit( PortInfo &pi )
238  {
239  pi.split_func =
240  []() -> raft::kernel*
241  {
242  return( new raft::split< T, roundrobin >() );
243  };
244  return;
245  }
246 
247 
248 
256  template < class T > void initializeJoin( PortInfo &pi )
257  {
258  pi.join_func =
259  []() -> raft::kernel*
260  {
261  return( new raft::join< T, roundrobin >() );
262  };
263  return;
264  }
265 
273 
280  PortInfo& getPortInfoFor( const std::string port_name );
281 
287 
291  raft::kernel * kernel = nullptr;
292 
298  void * const alloc_ptr = nullptr;
299 
304  const std::size_t alloc_ptr_length = 0;
305 
307  friend class MapBase;
308  friend class Map;
309  friend class GraphTools;
310  friend class basic_parallel;
311  friend class raft::parallel_k;
312 };
313 
314 
315 
316 #endif /* END _PORT_HPP_ */
const std::size_t alloc_ptr_length
Definition: port.hpp:304
void initializeConstMap(PortInfo &pi)
Definition: port.hpp:207
PortInfo & getPortInfoFor(const std::string port_name)
Definition: port.cpp:108
std::map< Type::RingBufferType, instr_map_t * > const_map
Definition: port_info.hpp:88
PortInfo & getPortInfo()
Definition: port.cpp:121
Port(raft::kernel *k)
Definition: port.cpp:33
Definition: basicparallel.hpp:48
Definition: mapbase.hpp:100
raft::kernel * kernel
Definition: port.hpp:291
virtual PortIterator begin()
Definition: port.cpp:90
portmap_t portmap
Definition: port.hpp:286
Definition: parallelk.hpp:35
Definition: graphtools.hpp:56
Definition: port.hpp:53
void *const alloc_ptr
Definition: port.hpp:298
Definition: portbase.hpp:28
virtual bool hasPorts()
Definition: port.cpp:84
split_factory_t split_func
Definition: port_info.hpp:96
bool addInPlacePorts(const std::size_t n_ports)
Definition: port.hpp:124
virtual ~Port()
Definition: port.cpp:48
void initializeJoin(PortInfo &pi)
Definition: port.hpp:256
Definition: map.hpp:47
virtual FIFO & operator[](const std::string &&port_name)
Definition: port.cpp:68
std::size_t count()
Definition: port.cpp:102
Definition: port.hpp:52
Definition: port.hpp:57
Definition: port_info.hpp:39
virtual PortIterator end()
Definition: port.cpp:96
const std::type_index & getPortType(const std::string &&port_name)
Definition: port.cpp:57
Definition: kernel.hpp:57
void addPort(const std::string &&port_name)
Definition: port.hpp:92
Definition: globalmap.cpp:3
void initializeSplit(PortInfo &pi)
Definition: port.hpp:237
Definition: portexception.hpp:74
Definition: portiterator.hpp:32
Definition: portmap_t.hpp:27
Definition: roundrobin.hpp:27