Graph Framework
Loading...
Searching...
No Matches
workflow.hpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
6//------------------------------------------------------------------------------
7
8#ifndef workflow_h
9#define workflow_h
10
11#include "jit.hpp"
12
14namespace workflow {
15//------------------------------------------------------------------------------
20//------------------------------------------------------------------------------
21 template<jit::float_scalar T, bool SAFE_MATH=false>
22 class work_item {
23 protected:
25 const std::string kernel_name;
27 const size_t kernel_size;
35 std::function<void(void)> kernel;
36
37 public:
38//------------------------------------------------------------------------------
48//------------------------------------------------------------------------------
53 const std::string name, const size_t size,
55 inputs(in), outputs(out), state(state),
56 kernel_name(name), kernel_size(size) {
57 context.add_kernel(name, in, out, maps, state, size);
58 }
59
60//------------------------------------------------------------------------------
64//------------------------------------------------------------------------------
69
70//------------------------------------------------------------------------------
72//------------------------------------------------------------------------------
73 virtual void run() {
74 kernel();
75 }
76 };
77
78//------------------------------------------------------------------------------
83//------------------------------------------------------------------------------
84 template<jit::float_scalar T, bool SAFE_MATH=false>
85 class converge_item final : public work_item<T, SAFE_MATH> {
86 private:
88 std::function<T(void)> max_kernel;
90 const T tolarance;
92 const size_t max_iterations;
93
94 public:
95//------------------------------------------------------------------------------
107//------------------------------------------------------------------------------
112 const std::string name, const size_t size,
114 const T tol=1.0E-30,
115 const size_t max_iter=1000) :
116 work_item<T, SAFE_MATH> (inputs, outputs, maps, state, name, size, context),
117 tolarance(tol), max_iterations(max_iter) {
118 context.add_max_reduction(size);
119 }
120
121//------------------------------------------------------------------------------
125//------------------------------------------------------------------------------
128 max_kernel = context.create_max_call(this->outputs.back(),
129 this->kernel);
130 }
131
132//------------------------------------------------------------------------------
134//------------------------------------------------------------------------------
135 virtual void run() {
136 size_t iterations = 0;
137 T max_residule = max_kernel();
138 T last_max = std::numeric_limits<T>::max();
139 T off_last_max = std::numeric_limits<T>::max();
140 while (std::abs(max_residule) > std::abs(tolarance) &&
141 std::abs(last_max - max_residule) > std::abs(tolarance) &&
142 std::abs(off_last_max - max_residule) > std::abs(tolarance) &&
143 iterations++ < max_iterations) {
144 last_max = max_residule;
145 if (!(iterations%2)) {
146 off_last_max = max_residule;
147 }
148 max_residule = max_kernel();
149 }
150
151// In release mode asserts are diaables so write error to standard err. Need to
152// flip the comparison operator because we want to assert to trip if false.
153 assert(iterations < max_iterations &&
154 "Workitem failed to converge.");
155 if (iterations > max_iterations) {
156 std::cerr << "Workitem failed to converge with in given iterations."
157 << std::endl;
158 std::cerr << "Minimum residule reached: " << max_residule
159 << std::endl;
160 }
161 }
162 };
163
164//------------------------------------------------------------------------------
169//------------------------------------------------------------------------------
170 template<jit::float_scalar T, bool SAFE_MATH=false>
171 class manager {
172 private:
176 std::vector<std::unique_ptr<work_item<T, SAFE_MATH>>> preitems;
178 std::vector<std::unique_ptr<work_item<T, SAFE_MATH>>> items;
180 bool add_reduction;
181
182 public:
183//------------------------------------------------------------------------------
187//------------------------------------------------------------------------------
188 manager(const size_t index) : context(index), add_reduction(false) {}
189
190//------------------------------------------------------------------------------
199//------------------------------------------------------------------------------
204 const std::string name, const size_t size) {
205 preitems.push_back(std::make_unique<work_item<T, SAFE_MATH>> (in, out,
206 maps, state,
207 name, size,
208 context));
209 }
210
211//------------------------------------------------------------------------------
220//------------------------------------------------------------------------------
225 const std::string name, const size_t size) {
226 items.push_back(std::make_unique<work_item<T, SAFE_MATH>> (in, out,
227 maps, state,
228 name, size,
229 context));
230 }
231
232//------------------------------------------------------------------------------
243//------------------------------------------------------------------------------
248 const std::string name, const size_t size,
249 const T tol=1.0E-30,
250 const size_t max_iter=1000) {
251 add_reduction = true;
252 items.push_back(std::make_unique<converge_item<T, SAFE_MATH>> (in, out,
253 maps, state,
254 name, size,
255 context, tol,
256 max_iter));
257 }
258
259//------------------------------------------------------------------------------
261//------------------------------------------------------------------------------
262 void compile() {
263 context.compile(add_reduction);
264
265 for (auto &item : preitems) {
266 item->create_kernel_call(context);
267 }
268 for (auto &item : items) {
269 item->create_kernel_call(context);
270 }
271 }
272
273//------------------------------------------------------------------------------
275//------------------------------------------------------------------------------
276 void pre_run() {
277 for (auto &item : preitems) {
278 item->run();
279 }
280 }
281
282//------------------------------------------------------------------------------
284//------------------------------------------------------------------------------
285 void run() {
286 for (auto &item : items) {
287 item->run();
288 }
289 }
290
291//------------------------------------------------------------------------------
293//------------------------------------------------------------------------------
294 void wait() {
295 context.wait();
296 }
297
298//------------------------------------------------------------------------------
303//------------------------------------------------------------------------------
305 T *destination) {
306 context.copy_to_device(node, destination);
307 }
308
309//------------------------------------------------------------------------------
314//------------------------------------------------------------------------------
316 T *destination) {
317 context.copy_to_host(node, destination);
318 }
319
320//------------------------------------------------------------------------------
325//------------------------------------------------------------------------------
326 void print(const size_t index,
328 context.print(index, nodes);
329 }
330
331//------------------------------------------------------------------------------
337//------------------------------------------------------------------------------
338 T check_value(const size_t index,
340 return context.check_value(index, node);
341 }
342
343//------------------------------------------------------------------------------
347//------------------------------------------------------------------------------
349 return context;
350 }
351 };
352}
353
354#endif /* workflow_h */
Class for JIT compile of the GPU kernels.
Definition jit.hpp:49
void compile(const bool add_reduction=false)
Compile the kernel.
Definition jit.hpp:230
void wait()
Wait for kernel to finish.
Definition jit.hpp:297
std::function< T(void)> create_max_call(graph::shared_leaf< T, SAFE_MATH > &argument, std::function< void(void)> run)
Create a max compute kernel calling function.
Definition jit.hpp:266
void add_max_reduction(const size_t size)
Add max reduction kernel.
Definition jit.hpp:193
std::function< void(void)> create_kernel_call(const std::string kernel_name, graph::input_nodes< T, SAFE_MATH > inputs, graph::output_nodes< T, SAFE_MATH > outputs, graph::shared_random_state< T, SAFE_MATH > state, const size_t num_rays)
Create a kernel calling function.
Definition jit.hpp:249
void print(const size_t index, const graph::output_nodes< T, SAFE_MATH > &nodes)
Print output.
Definition jit.hpp:277
void copy_to_device(graph::shared_leaf< T, SAFE_MATH > &node, T *source)
Copy contexts of buffer to device.
Definition jit.hpp:307
T check_value(const size_t index, const graph::shared_leaf< T, SAFE_MATH > &node)
Check the value.
Definition jit.hpp:289
void add_kernel(const std::string name, graph::input_nodes< T, SAFE_MATH > inputs, graph::output_nodes< T, SAFE_MATH > outputs, graph::map_nodes< T, SAFE_MATH > setters, graph::shared_random_state< T, SAFE_MATH > state, const size_t size)
Add a kernel.
Definition jit.hpp:116
void copy_to_host(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy contexts of buffer to host.
Definition jit.hpp:318
Class representing a convergence workitem.
Definition workflow.hpp:85
virtual void create_kernel_call(jit::context< T, SAFE_MATH > &context)
Set the kernel function.
Definition workflow.hpp:126
converge_item(graph::input_nodes< T, SAFE_MATH > inputs, graph::output_nodes< T, SAFE_MATH > outputs, graph::map_nodes< T, SAFE_MATH > maps, graph::shared_random_state< T, SAFE_MATH > state, const std::string name, const size_t size, jit::context< T, SAFE_MATH > &context, const T tol=1.0E-30, const size_t max_iter=1000)
Construct a workflow item.
Definition workflow.hpp:108
virtual void run()
Run the workitem.
Definition workflow.hpp:135
Class representing a workflow manager.
Definition workflow.hpp:171
void add_preitem(graph::input_nodes< T, SAFE_MATH > in, graph::output_nodes< T, SAFE_MATH > out, graph::map_nodes< T, SAFE_MATH > maps, graph::shared_random_state< T, SAFE_MATH > state, const std::string name, const size_t size)
Add a pre workflow item.
Definition workflow.hpp:200
void run()
Run work items.
Definition workflow.hpp:285
void wait()
Wait for GPU queue to finish.
Definition workflow.hpp:294
void pre_run()
Run prework items.
Definition workflow.hpp:276
void copy_to_host(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy contexts of buffer to host.
Definition workflow.hpp:315
void add_converge_item(graph::input_nodes< T, SAFE_MATH > in, graph::output_nodes< T, SAFE_MATH > out, graph::map_nodes< T, SAFE_MATH > maps, graph::shared_random_state< T, SAFE_MATH > state, const std::string name, const size_t size, const T tol=1.0E-30, const size_t max_iter=1000)
Add a converge item.
Definition workflow.hpp:244
void add_item(graph::input_nodes< T, SAFE_MATH > in, graph::output_nodes< T, SAFE_MATH > out, graph::map_nodes< T, SAFE_MATH > maps, graph::shared_random_state< T, SAFE_MATH > state, const std::string name, const size_t size)
Add a workflow item.
Definition workflow.hpp:221
void print(const size_t index, const graph::output_nodes< T, SAFE_MATH > &nodes)
Print results.
Definition workflow.hpp:326
void copy_to_device(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy buffer contents to the device.
Definition workflow.hpp:304
jit::context< T, SAFE_MATH > & get_context()
Get the jit context.
Definition workflow.hpp:348
manager(const size_t index)
Workflow manager constructor.
Definition workflow.hpp:188
void compile()
Compile the workflow items.
Definition workflow.hpp:262
T check_value(const size_t index, const graph::shared_leaf< T, SAFE_MATH > &node)
Check the value.
Definition workflow.hpp:338
Class representing a workitem.
Definition workflow.hpp:22
virtual void run()
Run the workitem.
Definition workflow.hpp:73
const std::string kernel_name
Name of the GPU kernel.
Definition workflow.hpp:25
virtual void create_kernel_call(jit::context< T, SAFE_MATH > &context)
Set the kernel function.
Definition workflow.hpp:65
graph::shared_random_state< T, SAFE_MATH > state
Random state node.
Definition workflow.hpp:33
work_item(graph::input_nodes< T, SAFE_MATH > in, graph::output_nodes< T, SAFE_MATH > out, graph::map_nodes< T, SAFE_MATH > maps, graph::shared_random_state< T, SAFE_MATH > state, const std::string name, const size_t size, jit::context< T, SAFE_MATH > &context)
Construct a workflow item.
Definition workflow.hpp:49
graph::input_nodes< T, SAFE_MATH > inputs
Input nodes.
Definition workflow.hpp:29
const size_t kernel_size
Name of the GPU kernel.
Definition workflow.hpp:27
graph::output_nodes< T, SAFE_MATH > outputs
Output nodes.
Definition workflow.hpp:31
std::function< void(void)> kernel
Kernel function.
Definition workflow.hpp:35
subroutine assert(test, message)
Assert check.
Definition f_binding_test.f90:38
Class to just in time compile a kernel.
std::shared_ptr< random_state_node< T, SAFE_MATH > > shared_random_state
Convenience type alias for shared sqrt nodes.
Definition random.hpp:272
std::vector< shared_variable< T, SAFE_MATH > > input_nodes
Convenience type alias for a vector of inputs.
Definition node.hpp:1730
std::shared_ptr< leaf_node< T, SAFE_MATH > > shared_leaf
Convenience type alias for shared leaf nodes.
Definition node.hpp:673
std::vector< std::pair< shared_leaf< T, SAFE_MATH >, shared_variable< T, SAFE_MATH > > > map_nodes
Convenience type alias for maping end codes back to inputs.
Definition node.hpp:1734
std::vector< shared_leaf< T, SAFE_MATH > > output_nodes
Convenience type alias for a vector of output nodes.
Definition node.hpp:688
Name space for workflows.
Definition workflow.hpp:14