AQUAgpusph 5.0.4
Loading...
Searching...
No Matches
Tool.hpp
Go to the documentation of this file.
1/*
2 * This file is part of AQUAgpusph, a free CFD program based on SPH.
3 * Copyright (C) 2012 Jose Luis Cercos Pita <jl.cercos@upm.es>
4 *
5 * AQUAgpusph is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * AQUAgpusph is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with AQUAgpusph. If not, see <http://www.gnu.org/licenses/>.
17 */
18
24
25#ifndef TOOL_H_INCLUDED
26#define TOOL_H_INCLUDED
27
31#include <math.h>
32#include <vector>
33#include <tuple>
34#include <deque>
35#include <algorithm>
36#include <numeric>
37
38#ifndef N_PROFILE_SAMPLES
39#define N_PROFILE_SAMPLES 100
40#endif
41
42namespace Aqua {
43namespace CalcServer {
44
48class Named
49{
50 public:
54 Named(const std::string name)
55 : _name(name)
56 {
57 }
58
61 ~Named() {}
62
66 inline void name(const std::string tool_name) { _name = tool_name; };
67
71 inline const std::string name() const { return _name; }
72
73 private:
75 std::string _name;
76};
77
78class Tool;
79
84{
85 public:
90 Profile(const std::string name, Tool* tool)
91 : Named(name)
92 , _tool(tool)
93 , _step(0)
94 {
95 }
96
99 virtual ~Profile() {}
100
103 void step();
104
105 protected:
110 void sample(cl_ulong start, cl_ulong end);
111
112 private:
114 Tool* _tool;
115
117 cl_ulong _step;
118};
119
128{
129 public:
134
138 {
139 for (auto i : _instances)
140 delete i;
141 }
142
146 inline std::vector<Profile*> substages() const { return _instances; }
147
148 protected:
152 inline void substages(std::vector<Profile*> instances)
153 {
154 _instances = instances;
155 }
156
157 private:
159 std::vector<Profile*> _instances;
160};
161
176{
177 public:
182 Tool(const std::string tool_name, bool once = false);
183
186 virtual ~Tool();
187
190 virtual void setup();
191
203 virtual void execute();
204
212 virtual inline Tool* next_tool() { return _next_tool; }
213
220 inline Tool* prev_tool() const { return _prev_tool; }
221
227 inline void prev_tool(Tool* tool) { _prev_tool = tool; }
228
232 inline size_t allocatedMemory() const { return _allocated_memory; }
233
237 inline unsigned int used_times() const { return _n_iters; }
238
242 void addElapsedTime(float elapsed_time);
243
249 inline float elapsedTime(bool averaged = true) const
250 {
251 if (!averaged)
252 return _elapsed_time;
253 return _average_elapsed_time;
254 }
255
259 inline float elapsedTimeVariance() const
260 {
261 return _squared_elapsed_time - pow(_average_elapsed_time, 2);
262 }
263
267 inline float elapsedTimeDeviation() const
268 {
269 return sqrt(elapsedTimeVariance());
270 }
271
282 virtual inline int scope_modifier() const { return 0; }
283
287 inline std::vector<InputOutput::Variable*> getInputDependencies() const
288 {
289 return _in_vars;
290 }
291
295 inline std::vector<InputOutput::Variable*> getOutputDependencies() const
296 {
297 return _out_vars;
298 }
299
303 inline std::tuple<std::vector<InputOutput::Variable*>,
304 std::vector<InputOutput::Variable*>>
306 {
307 return { _in_vars, _out_vars };
308 }
309
315 inline cl_event getEvent() const { return _event; }
316
320 inline void parent(Tool* tool) { _parent = tool; }
321
325 inline Tool* parent() { return _parent; }
326
327 protected:
331 int id_in_pipeline() const;
332
339 inline std::string varPrefix() const
340 {
341 return "__tid" + std::to_string(id_in_pipeline()) + "_";
342 }
343
348 void next_tool(Tool* tool) { _next_tool = tool; }
349
353 void allocatedMemory(size_t mem_size) { _allocated_memory = mem_size; }
354
359 virtual cl_event _execute(const UNUSED_PARAM std::vector<cl_event> events)
360 {
361 return NULL;
362 }
363
372 inline void setDependencies(std::vector<std::string> inputs,
373 std::vector<std::string> outputs)
374 {
375 _in_vars = namesToVars(inputs);
376 _out_vars = namesToVars(outputs);
377 }
378
387 inline void setDependencies(std::vector<InputOutput::Variable*> inputs,
388 std::vector<InputOutput::Variable*> outputs)
389 {
390 _in_vars = inputs;
391 _out_vars = outputs;
392 }
393
401 inline void setDependencies(std::vector<std::string> vars)
402 {
403 std::vector<std::string> inputs;
404 setDependencies(inputs, vars);
405 }
406
414 inline void setInputDependencies(std::vector<std::string> vars)
415 {
416 _in_vars = namesToVars(vars);
417 }
418
426 inline void setInputDependencies(std::vector<InputOutput::Variable*> vars)
427 {
428 _in_vars = vars;
429 }
430
438 inline void setOutputDependencies(std::vector<std::string> vars)
439 {
440 _out_vars = namesToVars(vars);
441 }
442
450 inline void setOutputDependencies(std::vector<InputOutput::Variable*> vars)
451 {
452 _out_vars = vars;
453 }
454
462 inline void setDependencies(std::vector<InputOutput::Variable*> vars)
463 {
464 std::vector<InputOutput::Variable*> inputs;
465 setDependencies(inputs, vars);
466 }
467
469 {
470 in = 0x01,
471 out = 0x02,
472 all = 0x03
473 };
474
480 const std::vector<cl_event> getEvents(
481 dep_events which = dep_events::all) const;
482
499 static std::vector<cl_kernel> compile(const std::string source,
500 const std::vector<std::string> names,
501 const std::string flags = "");
502
519 static cl_kernel compile_kernel(const std::string source,
520 const std::string kernel_name,
521 const std::string flags = "");
522
523
524 private:
529 std::vector<InputOutput::Variable*> namesToVars(
530 const std::vector<std::string>& names) const;
531
533 bool _once;
534
536 Tool* _next_tool;
537
539 Tool* _prev_tool;
540
542 size_t _allocated_memory;
543
545 size_t _n_iters;
546
548 float _elapsed_time;
549
551 float _average_elapsed_time;
552
554 float _squared_elapsed_time;
555
557 std::vector<InputOutput::Variable*> _in_vars;
558
560 std::vector<InputOutput::Variable*> _out_vars;
561
563 cl_event _event;
564
566 Tool* _parent;
567};
568
569}
570} // namespace
571
572#endif // TOOL_H_INCLUDED
Set of auxiliar functions.
Virtual variables environment to allow the user define/manipulate the variables used in the simulatio...
A helper class for named objects.
Definition Tool.hpp:49
void name(const std::string tool_name)
Definition Tool.hpp:66
Named(const std::string name)
Definition Tool.hpp:54
~Named()
Definition Tool.hpp:61
const std::string name() const
Get the name.
Definition Tool.hpp:71
void step()
Get the step from the Aqua::CalcServer::CalcServer.
Definition Tool.cpp:36
Profile(const std::string name, Tool *tool)
Constructor.
Definition Tool.hpp:90
void sample(cl_ulong start, cl_ulong end)
Add profiling info.
Definition Tool.cpp:43
virtual ~Profile()
Destructor.
Definition Tool.hpp:99
Profiling base class.
Definition Tool.hpp:128
void substages(std::vector< Profile * > instances)
Set the tool substages.
Definition Tool.hpp:152
std::vector< Profile * > substages() const
Get the substages.
Definition Tool.hpp:146
~Profiler()
Definition Tool.hpp:137
Profiler()
Definition Tool.hpp:133
Tools base class.
Definition Tool.hpp:176
dep_events
Definition Tool.hpp:469
@ in
Definition Tool.hpp:470
@ all
Definition Tool.hpp:472
@ out
Definition Tool.hpp:471
std::vector< InputOutput::Variable * > getOutputDependencies() const
Get the output depedencies of the tool.
Definition Tool.hpp:295
virtual void execute()
Execute the tool measuring the elapsed time.
Definition Tool.cpp:173
virtual Tool * next_tool()
Definition Tool.hpp:212
void setDependencies(std::vector< std::string > inputs, std::vector< std::string > outputs)
Set the depedencies of the tool.
Definition Tool.hpp:372
float elapsedTimeDeviation() const
Definition Tool.hpp:267
virtual cl_event _execute(const UNUSED_PARAM std::vector< cl_event > events)
Definition Tool.hpp:359
size_t allocatedMemory() const
Definition Tool.hpp:232
void allocatedMemory(size_t mem_size)
Definition Tool.hpp:353
float elapsedTime(bool averaged=true) const
Definition Tool.hpp:249
int id_in_pipeline() const
Definition Tool.cpp:287
void setDependencies(std::vector< InputOutput::Variable * > inputs, std::vector< InputOutput::Variable * > outputs)
Set the depedencies of the tool.
Definition Tool.hpp:387
Tool(const std::string tool_name, bool once=false)
Definition Tool.cpp:49
Tool * parent()
Get the tool parent.
Definition Tool.hpp:325
void setOutputDependencies(std::vector< InputOutput::Variable * > vars)
Set the writing depedencies of the tool.
Definition Tool.hpp:450
virtual int scope_modifier() const
Definition Tool.hpp:282
void parent(Tool *tool)
Set the tool parent.
Definition Tool.hpp:320
void setInputDependencies(std::vector< std::string > vars)
Set the reading depedencies of the tool.
Definition Tool.hpp:414
Tool * prev_tool() const
Definition Tool.hpp:220
void setDependencies(std::vector< std::string > vars)
Set the depedencies of the tool.
Definition Tool.hpp:401
void prev_tool(Tool *tool)
Definition Tool.hpp:227
cl_event getEvent() const
Get the tool event.
Definition Tool.hpp:315
void next_tool(Tool *tool)
Definition Tool.hpp:348
std::tuple< std::vector< InputOutput::Variable * >, std::vector< InputOutput::Variable * > > getDependencies() const
Get the depedencies of the tool.
Definition Tool.hpp:305
float elapsedTimeVariance() const
Definition Tool.hpp:259
void setOutputDependencies(std::vector< std::string > vars)
Set the writing depedencies of the tool.
Definition Tool.hpp:438
std::vector< InputOutput::Variable * > getInputDependencies() const
Get the input depedencies of the tool.
Definition Tool.hpp:287
unsigned int used_times() const
Definition Tool.hpp:237
void setInputDependencies(std::vector< InputOutput::Variable * > vars)
Set the reading depedencies of the tool.
Definition Tool.hpp:426
std::string varPrefix() const
Produce a variable name prefix.
Definition Tool.hpp:339
virtual void setup()
Definition Tool.cpp:73
void setDependencies(std::vector< InputOutput::Variable * > vars)
Set the depedencies of the tool.
Definition Tool.hpp:462
Main AQUAgpusph namespace.
Definition ArgumentsManager.cpp:50
Set of definitions and macros related with the implementation.
#define UNUSED_PARAM
Definition sphPrerequisites.hpp:391
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition sphPrerequisites.hpp:65