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 tolerance;
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 tolerance(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_residual = 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_residual) > std::abs(tolerance) &&
141 std::abs(last_max - max_residual) > std::abs(tolerance) &&
142 std::abs(off_last_max - max_residual) > std::abs(tolerance) &&
143 iterations++ < max_iterations) {
144 last_max = max_residual;
145 if (!(iterations%2)) {
146 off_last_max = max_residual;
147 }
148 max_residual = max_kernel();
149 }
150
151// In release mode asserts are disables 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 residual reached: " << max_residual
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//------------------------------------------------------------------------------
193//------------------------------------------------------------------------------
194 manager(const size_t index) : context(index), add_reduction(false) {}
195
196//------------------------------------------------------------------------------
205//------------------------------------------------------------------------------
210 const std::string name, const size_t size) {
211 preitems.push_back(std::make_unique<work_item<T, SAFE_MATH>> (in, out,
212 maps, state,
213 name, size,
214 context));
215 }
216
217//------------------------------------------------------------------------------
226//------------------------------------------------------------------------------
231 const std::string name, const size_t size) {
232 items.push_back(std::make_unique<work_item<T, SAFE_MATH>> (in, out,
233 maps, state,
234 name, size,
235 context));
236 }
237
238//------------------------------------------------------------------------------
249//------------------------------------------------------------------------------
254 const std::string name, const size_t size,
255 const T tol=1.0E-30,
256 const size_t max_iter=1000) {
257 add_reduction = true;
258 items.push_back(std::make_unique<converge_item<T, SAFE_MATH>> (in, out,
259 maps, state,
260 name, size,
261 context, tol,
262 max_iter));
263 }
264
265//------------------------------------------------------------------------------
267//------------------------------------------------------------------------------
268 void compile() {
269 context.compile(add_reduction);
270
271 for (auto &item : preitems) {
272 item->create_kernel_call(context);
273 }
274 for (auto &item : items) {
275 item->create_kernel_call(context);
276 }
277 }
278
279//------------------------------------------------------------------------------
281//------------------------------------------------------------------------------
282 void pre_run() {
283 for (auto &item : preitems) {
284 item->run();
285 }
286 }
287
288//------------------------------------------------------------------------------
290//------------------------------------------------------------------------------
291 void run() {
292 for (auto &item : items) {
293 item->run();
294 }
295 }
296
297//------------------------------------------------------------------------------
299//------------------------------------------------------------------------------
300 void wait() {
301 context.wait();
302 }
303
304//------------------------------------------------------------------------------
309//------------------------------------------------------------------------------
311 T *destination) {
312 context.copy_to_device(node, destination);
313 }
314
315//------------------------------------------------------------------------------
320//------------------------------------------------------------------------------
322 T *destination) {
323 context.copy_to_host(node, destination);
324 }
325
326//------------------------------------------------------------------------------
331//------------------------------------------------------------------------------
332 void print(const size_t index,
334 context.print(index, nodes);
335 }
336
337//------------------------------------------------------------------------------
343//------------------------------------------------------------------------------
344 T check_value(const size_t index,
346 return context.check_value(index, node);
347 }
348
349//------------------------------------------------------------------------------
353//------------------------------------------------------------------------------
355 return context;
356 }
357 };
358}
359
360#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: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:206
void run()
Run work items.
Definition workflow.hpp:291
void wait()
Wait for GPU queue to finish.
Definition workflow.hpp:300
void pre_run()
Run pre work items.
Definition workflow.hpp:282
void copy_to_host(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy contexts of buffer to host.
Definition workflow.hpp:321
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:250
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:227
void print(const size_t index, const graph::output_nodes< T, SAFE_MATH > &nodes)
Print results.
Definition workflow.hpp:332
void copy_to_device(graph::shared_leaf< T, SAFE_MATH > &node, T *destination)
Copy buffer contents to the device.
Definition workflow.hpp:310
jit::context< T, SAFE_MATH > & get_context()
Get the jit context.
Definition workflow.hpp:354
manager(const size_t index)
Workflow manager constructor.
Definition workflow.hpp:194
void compile()
Compile the workflow items.
Definition workflow.hpp:268
T check_value(const size_t index, const graph::shared_leaf< T, SAFE_MATH > &node)
Check the value.
Definition workflow.hpp:344
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:272
std::vector< shared_variable< T, SAFE_MATH > > input_nodes
Convenience type alias for a vector of inputs.
Definition node.hpp:1731
std::shared_ptr< leaf_node< T, SAFE_MATH > > shared_leaf
Convenience type alias for shared leaf nodes.
Definition node.hpp:674
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:1735
std::vector< shared_leaf< T, SAFE_MATH > > output_nodes
Convenience type alias for a vector of output nodes.
Definition node.hpp:689
Name space for workflows.
Definition workflow.hpp:14