AQUAgpusph 5.0.4
Loading...
Searching...
No Matches
CalcServer.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
23
24#ifndef CALCSERVER_H_INCLUDED
25#define CALCSERVER_H_INCLUDED
26
27#include <vector>
28#include <deque>
29#include <map>
30#include <string>
31#include <iterator>
32#include <chrono>
33
38#include "Tool.hpp"
39
40#ifndef N_PROFILING_SNAPSHOTS
41#define N_PROFILING_SNAPSHOTS 2
42#endif
43
44#define CHECK_OCL_OR_THROW(err_code, msg) \
45 if (err_code != CL_SUCCESS) { \
46 LOG(L_ERROR, std::string(msg) + "\n"); \
47 Aqua::InputOutput::Logger::singleton()->printOpenCLError(err_code); \
48 throw std::runtime_error("OpenCL error"); \
49 }
50
51namespace Aqua {
53namespace CalcServer {
54
55class UnSort;
57
65class user_interruption : public std::runtime_error
66{
67 public:
69 user_interruption(const std::string msg)
70 : std::runtime_error(msg){};
71};
72
74typedef struct _ProfilingSample
75{
79 std::string name;
81 cl_ulong start;
83 cl_ulong end;
85
87typedef struct _ProfilingSnapshot
88{
90 cl_ulong step;
92 std::vector<ProfilingSample> samples;
94
99{
100 public:
105 : _step(0)
106 , _n(n)
107 {
108 }
109
112
118 inline cl_ulong step() const { return _step; }
119
127 void sample(cl_ulong step,
128 Tool* tool,
129 std::string name,
130 cl_ulong start,
131 cl_ulong end);
132
140 inline void sample(cl_ulong step,
141 Tool* tool,
142 Profile* substage,
143 cl_ulong start,
144 cl_ulong end)
145 {
146 std::string name = tool->name() + "::" + substage->name();
147 auto parent = tool;
148 while ((parent = parent->parent())) {
149 name = parent->name() + "::" + name;
150 }
151 sample(step, tool, name, start, end);
152 }
153
157 std::deque<ProfilingSnapshot> get() const;
158
168 static inline cl_long delta(const cl_ulong& t, const cl_ulong& t0)
169 {
170 return (t > t0) ? t - t0 : -(cl_long)(t0 - t);
171 }
172
173 protected:
177 void newStep();
178
179 private:
181 cl_ulong _step;
183 cl_uint _n;
185 std::deque<ProfilingSnapshot> _snapshots;
186};
187
202{
203 public:
208
210 ~CalcServer();
211
217 static CalcServer* singleton();
218
227 void raiseSIGINT();
228
236 void update(InputOutput::TimeManager& t_manager);
237
239
243 void setup();
244
248 inline InputOutput::Variables* variables() { return &_vars; }
249
253 inline std::vector<std::string> definitions() const { return _definitions; }
254
258 inline std::vector<Tool*> tools() const { return _tools; }
259
263 inline cl_context context() const { return _context; }
264
268 inline cl_platform_id platform() const { return _platform; }
269
273 inline cl_device_id device() const { return _device; }
274
278 inline cl_uint device_addr_bits() const { return _device_bits; }
279
283 inline const char* device_compile_flags() const
284 {
285 return _device_compile_flags.c_str();
286 }
287
294 template <typename T>
295 cl_int setKernelSizeArg(cl_kernel kernel,
296 cl_uint arg_index,
297 const T arg_value);
298
305
318 cl_command_queue command_queue(
319 cmd_queue which = cmd_queue::cmd_queue_current);
320
325 inline cl_int finish() const
326 {
327 cl_int err_code = CL_SUCCESS;
328 for (auto queue : _command_queues)
329 err_code |= clFinish(queue);
330 err_code |= clFinish(_command_queue_parallel);
331 return err_code;
332 }
333
341 std::vector<cl_event> all_events() const;
342
347 cl_event marker() const;
348
356 cl_event marker(cl_command_queue cmd, std::vector<cl_event> events) const;
357
361 inline cl_ulong device_timer_offset() const { return _device_timer_offset; }
362
366 static inline cl_ulong host_timer()
367 {
368 auto now = std::chrono::system_clock::now();
369 return (cl_ulong)std::chrono::duration_cast<std::chrono::nanoseconds>(
370 now.time_since_epoch())
371 .count();
372 }
373
385 cl_event getUnsortedMem(const std::string var_name,
386 size_t offset,
387 size_t cb,
388 void* ptr);
389
393 const std::string base_path() const { return _base_path.c_str(); }
394
400 inline bool have_3d() const { return _sim_data.dims() == 3; }
401
406 inline bool debug_mode() const
407 {
408 return _sim_data.settings.debug_tools;
409 }
410
416 inline bool debug_mode(debug_opts opt) const
417 {
418 return _sim_data.settings.debug_tools & opt;
419 }
420
425 std::tuple<std::vector<InputOutput::Variable*>,
426 std::vector<InputOutput::Variable*>>
427 eventVars(cl_event event) const;
428
432 Tool* eventTool(cl_event event) const;
433
434 private:
437 void setupOpenCL();
440 void queryOpenCL();
443 void setupPlatform();
446 void setupDevices();
447
449 cl_uint _num_platforms;
451 cl_platform_id* _platforms;
453 cl_uint _num_devices;
455 cl_device_id* _devices;
457 cl_context _context;
459 cl_platform_id _platform;
461 cl_device_id _device;
463 cl_uint _device_bits;
465 std::string _device_compile_flags;
474 bool _is_nvidia;
476 unsigned int _command_queue_current;
478 std::vector<cl_command_queue> _command_queues;
482 cl_command_queue _command_queue_parallel;
483
485 cl_ulong _device_timer_offset;
486
489
491 std::vector<std::string> _definitions;
492
494 std::vector<Tool*> _tools;
495
500 std::string _base_path;
501
510 char* _current_tool_name;
511
515 std::map<std::string, UnSort*> unsorters;
516
519
525 cl_mem __phony_mems[2];
526};
527
528}
529} // namespace
530
531#endif // CALCSERVER_H_INCLUDED
#define N_PROFILING_SNAPSHOTS
Definition CalcServer.hpp:41
Simulation configuration data structures. (See Aqua::InputOutput::ProblemSetup for details)
#define T
Definition Sort.hcl.in:83
Simulation time flow events manager. (See Aqua::InputOutput::TimeManager for details)
Tools virtual environment to allow the user to define/manipulate the tools used to carry out the simu...
Virtual variables environment to allow the user define/manipulate the variables used in the simulatio...
std::vector< std::string > definitions() const
Definition CalcServer.hpp:253
CalcServer(const Aqua::InputOutput::ProblemSetup &sim_data)
Constructor.
Definition CalcServer.cpp:139
void update(InputOutput::TimeManager &t_manager)
Internal time loop.
Definition CalcServer.cpp:592
cl_device_id device() const
Definition CalcServer.hpp:273
std::vector< Tool * > tools() const
Definition CalcServer.hpp:258
InputOutput::Variables * variables()
Definition CalcServer.hpp:248
const std::string base_path() const
Get the AQUAgpusph root path.
Definition CalcServer.hpp:393
bool have_3d() const
Get if 3 dimensions are considered.
Definition CalcServer.hpp:400
void raiseSIGINT()
Raise a SIGINT/SIGTERM signal.
Definition CalcServer.cpp:584
cl_context context() const
Definition CalcServer.hpp:263
void setup()
Setup some additional simulation data.
Definition CalcServer.cpp:1435
bool debug_mode() const
Report if any tools debug option is enabled.
Definition CalcServer.hpp:406
static cl_ulong host_timer()
Get the host timer.
Definition CalcServer.hpp:366
const char * device_compile_flags() const
Get the device compilation flags.
Definition CalcServer.hpp:283
cl_int finish() const
Definition CalcServer.hpp:325
cl_ulong device_timer_offset() const
Get the offset between the device timer and the host one.
Definition CalcServer.hpp:361
cl_int setKernelSizeArg(cl_kernel kernel, cl_uint arg_index, const T arg_value)
wrapper to clSetKernelArg to set variables which size depends on the device address bits
bool debug_mode(debug_opts opt) const
Report if the tools debug mode option is enabled.
Definition CalcServer.hpp:416
cl_uint device_addr_bits() const
Get the device address bits.
Definition CalcServer.hpp:278
cmd_queue
Definition CalcServer.hpp:300
@ cmd_queue_current
Definition CalcServer.hpp:301
@ cmd_queue_new
Definition CalcServer.hpp:302
@ cmd_queue_mpi
Definition CalcServer.hpp:303
cl_platform_id platform() const
Definition CalcServer.hpp:268
static CalcServer * singleton()
Get –creating it the first time– the logger instance.
Definition CalcServer.cpp:578
void name(const std::string tool_name)
Definition Tool.hpp:66
Profiler subinstance base class.
Definition Tool.hpp:84
A FIFO list of profiling snapshots.
Definition CalcServer.hpp:99
cl_ulong step() const
Get the number of times the tools pack has been executed.
Definition CalcServer.hpp:118
std::deque< ProfilingSnapshot > get() const
Get the stored snapshots.
Definition CalcServer.cpp:101
ProfilingInfo(const cl_uint n=N_PROFILING_SNAPSHOTS)
Constructor.
Definition CalcServer.hpp:104
void sample(cl_ulong step, Tool *tool, Profile *substage, cl_ulong start, cl_ulong end)
Add a new sample to an specific step.
Definition CalcServer.hpp:140
~ProfilingInfo()
Destructor.
Definition CalcServer.hpp:111
void sample(cl_ulong step, Tool *tool, std::string name, cl_ulong start, cl_ulong end)
Add a new sample to an specific step.
Definition CalcServer.cpp:108
void newStep()
Let the profiler know that a new step of tools execution is about to start.
Definition CalcServer.cpp:125
static cl_long delta(const cl_ulong &t, const cl_ulong &t0)
Get the delta time.
Definition CalcServer.hpp:168
Tools base class.
Definition Tool.hpp:176
user_interruption(const std::string msg)
Constructor.
Definition CalcServer.hpp:69
enum Aqua::InputOutput::ProblemSetup::sphSettings::_debug debug_opts
Possible debugging modes.
Simulation configuration data.
Definition ProblemSetup.hpp:91
Simulation time flow manager.
Definition TimeManager.hpp:43
Variables manager, which can interpret the types on the fly.
Definition Variable.hpp:1022
struct Aqua::CalcServer::_ProfilingSnapshot ProfilingSnapshot
Profiling snapshot.
InputOutput::ProblemSetup::sphSettings::debug_opts debug_opts
Definition CalcServer.hpp:56
struct Aqua::CalcServer::_ProfilingSample ProfilingSample
Profiling sample.
Main AQUAgpusph namespace.
Definition ArgumentsManager.cpp:50
Set of definitions and macros related with the implementation.
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition sphPrerequisites.hpp:65
Profiling sample.
Definition CalcServer.hpp:75
Tool * tool
The triggering tool.
Definition CalcServer.hpp:77
cl_ulong start
The starting timer.
Definition CalcServer.hpp:81
std::string name
The name of the sample.
Definition CalcServer.hpp:79
cl_ulong end
The ending timer.
Definition CalcServer.hpp:83
Profiling snapshot.
Definition CalcServer.hpp:88
cl_ulong step
The profiling step.
Definition CalcServer.hpp:90
std::vector< ProfilingSample > samples
The list of samples.
Definition CalcServer.hpp:92