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 loop_item final : public work_item<T, SAFE_MATH> {
87 const size_t num_iterations;
88
89 public:
90//------------------------------------------------------------------------------
101//------------------------------------------------------------------------------
106 const std::string name, const size_t size,
108 const size_t iterations) :
109 work_item<T, SAFE_MATH> (inputs, outputs, maps, state, name, size, context),
110 num_iterations(iterations) {}
111
112//------------------------------------------------------------------------------
114//------------------------------------------------------------------------------
115 virtual void run() {
116 for (size_t i = 0; i < num_iterations; i++) {
118 }
119 }
120 };
121
122//------------------------------------------------------------------------------
127//------------------------------------------------------------------------------
128 template<jit::float_scalar T, bool SAFE_MATH=false>
129 class converge_item final : public work_item<T, SAFE_MATH> {
130 private:
132 std::function<T(void)> max_kernel;
134 const T tolerance;
136 const size_t max_iterations;
137
138 public:
139//------------------------------------------------------------------------------
151//------------------------------------------------------------------------------
156 const std::string name, const size_t size,
158 const T tol=1.0E-30,
159 const size_t max_iter=1000) :
160 work_item<T, SAFE_MATH> (inputs, outputs, maps, state, name, size, context),
161 tolerance(tol), max_iterations(max_iter) {
162 context.add_max_reduction(size);
163 }
164
165//------------------------------------------------------------------------------
169//------------------------------------------------------------------------------
172 max_kernel = context.create_max_call(this->outputs.back(),
173 this->kernel);
174 }
175
176//------------------------------------------------------------------------------
178//------------------------------------------------------------------------------
179 virtual void run() {
180 size_t iterations = 0;
181 T max_residual = max_kernel();
182 T last_max = std::numeric_limits<T>::max();
183 T off_last_max = std::numeric_limits<T>::max();
184 while (std::abs(max_residual) > std::abs(tolerance) &&
185 std::abs(last_max - max_residual) > std::abs(tolerance) &&
186 std::abs(off_last_max - max_residual) > std::abs(tolerance) &&
187 iterations++ < max_iterations) {
188 last_max = max_residual;
189 if (!(iterations%2)) {
190 off_last_max = max_residual;
191 }
192 max_residual = max_kernel();
193 }
194
195// In release mode asserts are disables so write error to standard err. Need to
196// flip the comparison operator because we want to assert to trip if false.
197 assert(iterations < max_iterations &&
198 "Workitem failed to converge.");
199 if (iterations > max_iterations) {
200 std::cerr << "Workitem failed to converge with in given iterations."
201 << std::endl;
202 std::cerr << "Minimum residual reached: " << max_residual
203 << std::endl;
204 }
205 }
206 };
207
208//------------------------------------------------------------------------------
213//------------------------------------------------------------------------------
214 template<jit::float_scalar T, bool SAFE_MATH=false>
215 class manager {
216 private:
220 std::vector<std::unique_ptr<work_item<T, SAFE_MATH>>> preitems;
222 std::vector<std::unique_ptr<work_item<T, SAFE_MATH>>> items;
224 bool add_reduction;
225
226 public:
227//------------------------------------------------------------------------------
237//------------------------------------------------------------------------------
238 manager(const size_t index) : context(index), add_reduction(false) {}
239
240//------------------------------------------------------------------------------
249//------------------------------------------------------------------------------
254 const std::string name, const size_t size) {
255 preitems.push_back(std::make_unique<work_item<T, SAFE_MATH>> (in, out,
256 maps, state,
257 name, size,
258 context));
259 }
260
261//------------------------------------------------------------------------------
270//------------------------------------------------------------------------------
275 const std::string name, const size_t size) {
276 items.push_back(std::make_unique<work_item<T, SAFE_MATH>> (in, out,
277 maps, state,
278 name, size,
279 context));
280 }
281
282//------------------------------------------------------------------------------
292//------------------------------------------------------------------------------
297 const std::string name, const size_t size,
298 const size_t iterations) {
299 items.push_back(std::make_unique<loop_item<T, SAFE_MATH>> (in, out,
300 maps, state,
301 name, size,
302 context,
303 iterations));
304 }
305
306//------------------------------------------------------------------------------
317//------------------------------------------------------------------------------
322 const std::string name, const size_t size,
323 const T tol=1.0E-30,
324 const size_t max_iter=1000) {
325 add_reduction = true;
326 items.push_back(std::make_unique<converge_item<T, SAFE_MATH>> (in, out,
327 maps, state,
328 name, size,
329 context, tol,
330 max_iter));
331 }
332
333//------------------------------------------------------------------------------
335//------------------------------------------------------------------------------
336 void compile() {
337 context.compile(add_reduction);
338
339 for (auto &item : preitems) {
340 item->create_kernel_call(context);
341 }
342 for (auto &item : items) {
343 item->create_kernel_call(context);
344 }
345 }
346
347//------------------------------------------------------------------------------
349//------------------------------------------------------------------------------
350 void pre_run() {
351 for (auto &item : preitems) {
352 item->run();
353 }
354 }
355
356//------------------------------------------------------------------------------
358//------------------------------------------------------------------------------
359 void run() {
360 for (auto &item : items) {
361 item->run();
362 }
363 }
364
365//------------------------------------------------------------------------------
367//------------------------------------------------------------------------------
368 void wait() {
369 context.wait();
370 }
371
372//------------------------------------------------------------------------------
377//------------------------------------------------------------------------------
379 T *destination) {
380 context.copy_to_device(node, destination);
381 }
382
383//------------------------------------------------------------------------------
388//------------------------------------------------------------------------------
390 T *destination) {
391 context.copy_to_host(node, destination);
392 }
393
394//------------------------------------------------------------------------------
399//------------------------------------------------------------------------------
400 void print(const size_t index,
402 context.print(index, nodes);
403 }
404
405//------------------------------------------------------------------------------
411//------------------------------------------------------------------------------
412 T check_value(const size_t index,
414 return context.check_value(index, node);
415 }
416
417//------------------------------------------------------------------------------
421//------------------------------------------------------------------------------
423 return context;
424 }
425 };
426}
427
428#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 work item.
Definition workflow.hpp:129
virtual void create_kernel_call(jit::context< T, SAFE_MATH > &context)
Set the kernel function.
Definition workflow.hpp:170
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:152
virtual void run()
Run the workitem.
Definition workflow.hpp:179
Run a work item in a fixed iteration loop.
Definition workflow.hpp:85
loop_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 size_t iterations)
Construct a workflow item.
Definition workflow.hpp:102
virtual void run()
Run the workitem.
Definition workflow.hpp:115
Class representing a workflow manager.
Definition workflow.hpp:215
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:250
void run()
Run work items.
Definition workflow.hpp:359
void wait()
Wait for GPU queue to finish.
Definition workflow.hpp:368
void pre_run()
Run pre work items.
Definition workflow.hpp:350
void copy_to_host(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy contexts of buffer to host.
Definition workflow.hpp:389
void add_loop_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 size_t iterations)
Add a workflow item.
Definition workflow.hpp:293
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:318
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:271
void print(const size_t index, const graph::output_nodes< T, SAFE_MATH > &nodes)
Print results.
Definition workflow.hpp:400
void copy_to_device(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy buffer contents to the device.
Definition workflow.hpp:378
jit::context< T, SAFE_MATH > & get_context()
Get the jit context.
Definition workflow.hpp:422
manager(const size_t index)
Workflow manager constructor.
Definition workflow.hpp:238
void compile()
Compile the workflow items.
Definition workflow.hpp:336
T check_value(const size_t index, const graph::shared_leaf< T, SAFE_MATH > &node)
Check the value.
Definition workflow.hpp:412
Class representing a work item.
Definition workflow.hpp:22
virtual void run()
Run the work item.
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:263
std::vector< shared_variable< T, SAFE_MATH > > input_nodes
Convenience type alias for a vector of inputs.
Definition node.hpp:1711
std::shared_ptr< leaf_node< T, SAFE_MATH > > shared_leaf
Convenience type alias for shared leaf nodes.
Definition node.hpp:676
std::vector< std::pair< shared_leaf< T, SAFE_MATH >, shared_variable< T, SAFE_MATH > > > map_nodes
Convenience type alias for mapping end codes back to inputs.
Definition node.hpp:1715
std::vector< shared_leaf< T, SAFE_MATH > > output_nodes
Convenience type alias for a vector of output nodes.
Definition node.hpp:691
Name space for workflows.
Definition workflow.hpp:14