world.h
Go to the documentation of this file.
1 #ifndef TTG_BASE_WORLD_H
2 #define TTG_BASE_WORLD_H
3 
4 #include <cassert>
5 #include <future>
6 #include <iostream>
7 #include <list>
8 #include <memory>
9 #include <set>
10 
11 #include "ttg/base/tt.h"
12 
13 namespace ttg {
14 
15  namespace base {
16  // forward decl
17  class WorldImplBase;
18  } // namespace base
19 
20  /* forward declaration */
21  namespace detail {
22 
23  /* TODO: how should the MADNESS and PaRSEC init/finalize play together? */
26  void destroy_worlds(void);
27 
28  } // namespace detail
29 
30  namespace base {
31 
33  class WorldImplBase {
34  private:
35  template <typename T>
36  std::function<void(void*)> make_deleter() {
37  return {[](void* p) { delete static_cast<T*>(p); }};
38  }
39 
40  std::list<ttg::TTBase*> m_op_register;
41  std::vector<std::shared_ptr<std::promise<void>>> m_statuses;
42  std::vector<std::function<void()>> m_callbacks;
43  std::vector<std::shared_ptr<void>> m_ptrs;
44  std::vector<std::unique_ptr<void, std::function<void(void*)>>> m_unique_ptrs;
45  int world_size;
46  int world_rank;
47  bool m_is_valid = true;
48 
49  protected:
50  void mark_invalid() { m_is_valid = false; }
51 
52  virtual void fence_impl(void) = 0;
53 
54  void release_ops(void) {
55  while (!m_op_register.empty()) {
56  (*m_op_register.begin())->release();
57  }
58  }
59 
60  protected:
62  : world_size(size), world_rank(rank)
63  {}
64 
65  public:
66  virtual ~WorldImplBase(void) { m_is_valid = false; }
67 
71  int size() {
72  return world_size;
73  }
74 
78  int rank() {
79  return world_rank;
80  }
81 
82  virtual void destroy(void) = 0;
83 
84  template <typename T>
85  void register_ptr(const std::shared_ptr<T>& ptr) {
86  m_ptrs.emplace_back(ptr);
87  }
88 
89  template <typename T>
90  void register_ptr(std::unique_ptr<T>&& ptr) {
91  m_unique_ptrs.emplace_back(ptr.release(), make_deleter<T>());
92  }
93 
94  void register_status(const std::shared_ptr<std::promise<void>>& status_ptr) {
95  m_statuses.emplace_back(status_ptr);
96  }
97 
98  template <typename Callback>
99  void register_callback(Callback&& callback) {
100  m_callbacks.emplace_back(callback);
101  }
102 
103 
109  void fence(void) {
110  fence_impl();
111  for (auto& status : m_statuses) {
112  status->set_value();
113  }
114  m_statuses.clear(); // clear out the statuses
115  for (auto&& callback : m_callbacks) {
116  callback();
117  }
118  m_callbacks.clear(); // clear out the statuses
119  }
120 
128  virtual void execute() {}
129 
130 
136  // TODO: do we need locking here?
137  m_op_register.push_back(op);
138  }
139 
145  // TODO: do we need locking here?
146  m_op_register.remove(op);
147  }
148 
149 
154  bool is_valid(void) const { return m_is_valid; }
155 
156  virtual void final_task() {}
157 
158  virtual void profile_on() { }
159  virtual void profile_off() { }
160  virtual bool profiling() { return false; }
161 
162  virtual void dag_on(const std::string &filename) { }
163  virtual void dag_off() { }
164  virtual bool dag_profiling() { return false; }
165 
166  };
167 
173  template <typename WorldImplT>
174  class World {
175  private:
176  std::shared_ptr<ttg::base::WorldImplBase> m_impl;
177 
178  public:
179  World(void) {}
180 
181  World(std::shared_ptr<ttg::base::WorldImplBase> world_impl) : m_impl(world_impl) {}
182 
183  /* Defaulted copy ctor */
184  World(const World& other) = default;
185 
186  /* Defaulted move ctor */
187  World(World&& other) = default;
188 
189  ~World() {}
190 
191  /* Defaulted copy assignment */
192  World& operator=(const World& other) = default;
193 
194  /* Defaulted move assignment */
195  World& operator=(World&& other) = default;
196 
197  /* Get the number of ranks in this world */
198  int size() const {
199  assert(is_valid());
200  return m_impl->size();
201  }
202 
203  /* Get the current rank in this world */
204  int rank() const {
205  assert(is_valid());
206  return m_impl->rank();
207  }
208 
209  /* Returns true if the World instance is valid, i.e., if it has a valid
210  * pointer to a World implementation object */
211  bool is_valid(void) const { return static_cast<bool>(m_impl); }
212 
213  virtual void final_task() {}
214 
215  /* Get an unmanaged reference to the world implementation */
216  WorldImplT& impl(void) {
217  assert(is_valid());
218  return *reinterpret_cast<WorldImplT*>(m_impl.get());
219  }
220 
221  const WorldImplT& impl(void) const {
222  assert(is_valid());
223  return *reinterpret_cast<WorldImplT*>(m_impl.get());
224  }
225 
226  void profile_on() { m_impl->profile_on(); }
227  void profile_off() { m_impl->profile_off(); }
228  bool profiling() { return m_impl->profiling(); }
229 
230  void dag_on(const std::string &filename) { m_impl->dag_on(filename); }
231  void dag_off() { m_impl->dag_off(); }
232  bool dag_profiling() { return m_impl->dag_profiling(); }
233 
234  };
235 
236  } // namespace base
237 
238 } // namespace ttg
239 #endif // TTG_BASE_WORLD_H
A base class for all template tasks.
Definition: tt.h:30
WorldImplT & impl(void)
Definition: world.h:216
void profile_on()
Definition: world.h:226
World & operator=(World &&other)=default
int size() const
Definition: world.h:198
void dag_on(const std::string &filename)
Definition: world.h:230
void profile_off()
Definition: world.h:227
virtual void final_task()
Definition: world.h:213
int rank() const
Definition: world.h:204
void dag_off()
Definition: world.h:231
const WorldImplT & impl(void) const
Definition: world.h:221
World(std::shared_ptr< ttg::base::WorldImplBase > world_impl)
Definition: world.h:181
World(World &&other)=default
bool profiling()
Definition: world.h:228
World(void)
Definition: world.h:179
World(const World &other)=default
bool dag_profiling()
Definition: world.h:232
bool is_valid(void) const
Definition: world.h:211
World & operator=(const World &other)=default
Base class for implementation-specific Worlds.
Definition: world.h:33
void fence(void)
Definition: world.h:109
virtual void destroy(void)=0
virtual void dag_off()
Definition: world.h:163
virtual void final_task()
Definition: world.h:156
virtual bool profiling()
Definition: world.h:160
void release_ops(void)
Definition: world.h:54
virtual void dag_on(const std::string &filename)
Definition: world.h:162
virtual void fence_impl(void)=0
void register_op(ttg::TTBase *op)
Definition: world.h:135
virtual void profile_off()
Definition: world.h:159
virtual void profile_on()
Definition: world.h:158
void deregister_op(ttg::TTBase *op)
Definition: world.h:144
void register_ptr(std::unique_ptr< T > &&ptr)
Definition: world.h:90
virtual ~WorldImplBase(void)
Definition: world.h:66
WorldImplBase(int size, int rank)
Definition: world.h:61
virtual void execute()
Definition: world.h:128
void register_status(const std::shared_ptr< std::promise< void >> &status_ptr)
Definition: world.h:94
void register_callback(Callback &&callback)
Definition: world.h:99
virtual bool dag_profiling()
Definition: world.h:164
bool is_valid(void) const
Definition: world.h:154
void register_ptr(const std::shared_ptr< T > &ptr)
Definition: world.h:85
void deregister_world(ttg::base::WorldImplBase &world)
void register_world(ttg::base::WorldImplBase &world)
void destroy_worlds(void)
Definition: world.h:70
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8