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