TPIE

v1.1rc1-6-g0c97303
pipeline.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2011, 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef __TPIE_PIPELINING_PIPELINE_H__
21 #define __TPIE_PIPELINING_PIPELINE_H__
22 
23 #include <boost/any.hpp>
24 #include <tpie/types.h>
25 #include <iostream>
26 #include <tpie/pipelining/tokens.h>
28 
29 namespace tpie {
30 
31 namespace pipelining {
32 
33 namespace bits {
34 
40 public:
44  void operator()(stream_size_type items, progress_indicator_base & pi, memory_size_type mem);
45 
56  void plot(std::ostream & out);
57 
58  double memory() const {
59  return m_memory;
60  }
61 
65  virtual ~pipeline_base() {}
66 
67  node_map::ptr get_node_map() const {
68  return m_segmap;
69  }
70 
71  void forward_any(std::string key, const boost::any & value);
72 
73  bool can_fetch(std::string key);
74 
75  boost::any fetch_any(std::string key);
76 
77 protected:
78  node_map::ptr m_segmap;
79  double m_memory;
80 };
81 
87 template <typename fact_t>
88 class pipeline_impl : public pipeline_base {
89 public:
90  typedef typename fact_t::constructed_type gen_t;
91 
92  inline pipeline_impl(const fact_t & factory)
93  : r(factory.construct())
94  {
95  this->m_memory = factory.memory();
96  this->m_segmap = r.get_node_map();
97  }
98 
99  inline operator gen_t() {
100  return r;
101  }
102 
103 private:
104  gen_t r;
105 };
106 
107 } // namespace bits
108 
115 class pipeline {
116 public:
117  pipeline()
118  : p(0)
119  {
120  }
121 
122  template <typename T>
123  inline pipeline(const T & from)
124  : p(0)
125  {
126  *this = from;
127  }
128  template <typename T>
129  pipeline & operator=(const T & from) {
130  if (p) delete p;
131  p = new T(from);
132  return *this;
133  }
134  inline ~pipeline() {
135  delete p;
136  }
137  inline void operator()() {
139  (*p)(1, pi, get_memory_manager().available());
140  }
141  inline void operator()(stream_size_type items, progress_indicator_base & pi) {
142  (*p)(items, pi, get_memory_manager().available());
143  }
144  inline void operator()(stream_size_type items, progress_indicator_base & pi, memory_size_type mem) {
145  (*p)(items, pi, mem);
146  }
147  inline void plot(std::ostream & os = std::cout) {
148  p->plot(os);
149  }
150  inline double memory() const {
151  return p->memory();
152  }
153  inline bits::node_map::ptr get_node_map() const {
154  return p->get_node_map();
155  }
156 
157  bool can_fetch(std::string key) {
158  return p->can_fetch(key);
159  }
160 
161  boost::any fetch_any(std::string key) {
162  return p->fetch_any(key);
163  }
164 
165  template <typename T>
166  T fetch(std::string key) {
167  return boost::any_cast<T>(fetch_any(key));
168  }
169 
170  void forward_any(std::string key, const boost::any & value) {
171  return p->forward_any(key, value);
172  }
173 
174  template <typename T>
175  void forward(std::string key, T value) {
176  forward_any(key, boost::any(value));
177  }
178 
179  void output_memory(std::ostream & o) const;
180 private:
182 };
183 
184 } // namespace pipelining
185 
186 } // namespace tpie
187 
188 #endif // __TPIE_PIPELINING_PIPELINE_H__
Standard types.
The base class for indicating the progress of some task.
Null-object progress indicator.
virtual ~pipeline_base()
Virtual dtor.
Definition: pipeline.h:65
void plot(std::ostream &out)
Generate a GraphViz plot of the actor graph.
size_t available() const
Return the amount of memory still available to allocation.
a dummy progress indicator that produces no output
Virtual superclass for pipelines implementing the function call operator.
Definition: pipeline.h:39
void operator()(stream_size_type items, progress_indicator_base &pi, memory_size_type mem)
Invoke the pipeline.
Pipeline tokens.
memory_manager & get_memory_manager()
Return a reference to the memory manager.
This class is used to avoid writing the template argument in the pipeline_impl type.
Definition: pipeline.h:115