ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
run.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// Created by Eduard Valeyev on 11/5/21.
4//
5
6#ifndef TTG_RUN_H
7#define TTG_RUN_H
8
9#include "ttg/fwd.h"
10
11#include "ttg/util/bug.h"
12#include "ttg/util/env.h"
13#include "ttg/world.h"
14
15namespace ttg {
16
17 namespace detail {
18 inline bool& initialized_accessor() {
19 static bool flag = false;
20 return flag;
21 }
22 inline bool& finalized_accessor() {
23 static bool flag = false;
24 return flag;
25 }
26 }
27
30 inline bool initialized() {
32 }
33
36 inline bool finalized() {
38 }
39
41
54 template <typename... RestOfArgs>
55 inline void initialize(int argc, char** argv, int num_threads, RestOfArgs&&... args) {
56 if (!initialized()) {
57 if (finalized()) {
58 throw std::runtime_error("ttg::initialize(): TTG already finalized, cannot be re-initialized");
59 }
60
61 // if requested by user, create a Debugger object
62 if (auto debugger_cstr = std::getenv("TTG_DEBUGGER")) {
63 using ttg::Debugger;
64 auto debugger = std::make_shared<Debugger>();
66 debugger->set_exec(argv[0]);
67 debugger->set_cmd(debugger_cstr);
68 }
69
70 if (num_threads < 1) num_threads = detail::num_threads();
71 TTG_IMPL_NS::ttg_initialize(argc, argv, num_threads, std::forward<RestOfArgs>(args)...);
72
73 // finish setting up the Debugger, if needed
76
78 }
79 else
80 throw std::runtime_error("ttg::initialize(): TTG already initialized");
81 }
82
84
92 inline void finalize() {
93 if (initialized()) {
94 TTG_IMPL_NS::ttg_finalize();
97 }
98 else
99 throw std::runtime_error("ttg::finalize(): TTG has not been initialized yet");
100 }
101
103 [[noreturn]]
104 inline void abort() { TTG_IMPL_NS::ttg_abort(); }
105
107
110 inline World default_execution_context() { return TTG_IMPL_NS::ttg_default_execution_context(); }
111
113
116 inline void execute(World world = default_execution_context()) { TTG_IMPL_NS::ttg_execute(world); }
117
119
123 inline void fence(World world = default_execution_context()) { TTG_IMPL_NS::ttg_fence(world); }
124
127 inline int rank(World world = default_execution_context()) { return world.rank(); }
128
131 inline int size(World world = default_execution_context()) { return world.size(); }
132
138 template <typename TT>
139 inline void invoke_once(TT& tt) {
140 if (tt.keymap() == tt.get_world().rank()) {
141 tt.invoke();
142 }
143 }
144
151 template <typename TT, typename Key>
152 inline void invoke_once(TT&& tt, Key&& key) {
153 if (tt.keymap(key) == tt.get_world().rank()) {
154 tt.invoke(std::forward<Key>(key));
155 }
156 }
157
158} // namespace ttg
159
160#endif // TTG_RUN_H
static void set_default_debugger(const std::shared_ptr< Debugger > &)
Set the global default debugger. The initial value is null.
Definition bug.cpp:407
static std::shared_ptr< Debugger > default_debugger()
Return the global default debugger.
Definition bug.cpp:409
int rank() const
Definition world.h:205
ttg::World get_world() const override final
Definition ttg.h:1373
std::enable_if_t<!ttg::meta::is_void_v< Key > &&!ttg::meta::is_empty_tuple_v< input_values_tuple_type >, void > invoke(const Key &key, const input_values_tuple_type &args)
Definition ttg.h:4305
bool & initialized_accessor()
Definition run.h:18
bool & finalized_accessor()
Definition run.h:22
int num_threads()
Determine the number of compute threads to use by TTG when not given to ttg::initialize
Definition env.cpp:16
top-level TTG namespace contains runtime-neutral functionality
Definition keymap.h:9
void invoke_once(TT &tt)
Definition run.h:139
void execute(ttg::World world)
Starts the execution in the given execution context.
Definition run.h:116
void initialize(int argc, char **argv, int num_threads=-1, RestOfArgs &&...)
int size(World world=default_execution_context())
Definition run.h:131
void abort()
Aborts the TTG program using the default backend's ttg_abort method.
Definition run.h:104
World default_execution_context()
Accesses the default backend's default execution context.
Definition run.h:110
void fence(ttg::World world)
Returns when all tasks associated with the given execution context have finished on all ranks.
Definition run.h:123
bool finalized()
Definition run.h:36
void finalize()
Finalizes the TTG runtime.
Definition func.h:590
int rank(World world=default_execution_context())
Definition run.h:127
bool initialized()
Definition run.h:30