AQUAgpusph 5.0.4
Loading...
Searching...
No Matches
Kernel.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 KERNEL_H_INCLUDED
25#define KERNEL_H_INCLUDED
26
28
29#include <vector>
30#if __APPLE__
31#include <OpenCL/cl.h>
32#else
33#include <CL/cl.h>
34#endif
35#include "Tool.hpp"
36
37namespace Aqua {
38namespace CalcServer {
39
47void
48sync_user_event(cl_event user_event, cl_event event);
49
54{
55 public:
60 EventProfile(const std::string name, Tool* tool)
61 : Profile(name, tool)
62 {
63 }
64
68
72 void start(cl_event event);
73
77 void end(cl_event event);
78
81 void sample();
82
83 private:
85 cl_event _start;
86
88 cl_event _end;
89};
90
99{
100 class Arg;
101
102 public:
111 ArgSetter(const std::string name,
112 cl_kernel kernel,
113 std::vector<InputOutput::Variable*> vars);
114
118
122 inline cl_kernel getKernel() const { return _kernel; }
123
127 inline std::vector<InputOutput::Variable*> getVars() const { return _vars; }
128
132 inline std::vector<Arg> getArgs() const { return _args; }
133
142 void execute();
143
144 private:
146 cl_kernel _kernel;
147
149 std::vector<InputOutput::Variable*> _vars;
150
152 std::vector<Arg> _args;
153};
154
163{
164 public:
167 : _size(0)
168 , _value(NULL)
169 , _event(NULL)
170 {
171 }
172
176 Arg(const Arg& arg)
177 : _size(0)
178 , _value(NULL)
179 , _event(NULL)
180 {
181 set(arg.size(), arg.value(), arg.event());
182 }
183
186 {
187 if (_value)
188 free(_value);
189 }
190
194 inline size_t size() const { return _size; }
195
199 inline const void* value() const { return _value; }
200
204 inline cl_event event() const { return _event; }
205
212
219 {
220 return !(*this == var);
221 }
222
227 inline void operator=(const Arg& arg)
228 {
229 set(arg.size(), arg.value(), arg.event());
230 }
231
237 {
238 try {
239 // We can read asynchronously as long as == operator already waited
240 // for the variable writing when required
241 set(var->typesize(), var->get_async(), var->getWritingEvent());
242 } catch (...) {
243 std::stringstream msg;
244 msg << "From variable " << var->name() << std::endl;
245 LOG0(L_DEBUG, msg.str());
246 throw;
247 }
248 }
249
250 protected:
257 inline void set(const size_t s, const void* v, const cl_event e)
258 {
259 if (_size != s) {
260 if (_value)
261 free(_value);
262 _size = s;
263 _value = malloc(_size);
264 if (!_value) {
265 std::stringstream msg;
266 msg << "Failure allocating " << _size << " bytes" << std::endl;
267 LOG(L_ERROR, msg.str());
268 throw std::bad_alloc();
269 }
270 }
271 memcpy(_value, v, s);
272 _event = e;
273 }
274
275 private:
277 size_t _size;
278
280 void* _value;
281
283 cl_event _event;
284};
285
291{
292 public:
300 Kernel(const std::string tool_name,
301 const std::string kernel_path,
302 const std::string entry_point = "entry",
303 const std::string n = "",
304 bool once = false);
305
308 virtual ~Kernel();
309
313 void setup();
314
318 const std::string path() { return (const std::string)_path; }
319
323 size_t workGroupSize() const { return _work_group_size; }
324
328 size_t globalWorkSize() const { return _global_work_size; }
329
330 protected:
335 cl_event _execute(const std::vector<cl_event> events);
336
343 void make(const std::string entry_point = "entry",
344 const std::string flags = "",
345 const std::string header = "");
346
350 void variables();
351
355
356 private:
358 std::string _path;
359
361 std::string _entry_point;
362
364 std::string _n;
365
367 cl_kernel _kernel;
368
370 size_t _work_group_size;
371
373 size_t _global_work_size;
374
376 std::vector<InputOutput::Variable*> _vars;
377
379 ArgSetter* _args_setter;
380};
381
382}
383} // namespace
384
385#endif // KERNEL_H_INCLUDED
#define LOG0(level, log)
Definition Logger.hpp:64
#define LOG(level, log)
Definition Logger.hpp:55
Tools virtual environment to allow the user to define/manipulate the tools used to carry out the simu...
Definition Kernel.hpp:163
const void * value() const
Get the value of the argument.
Definition Kernel.hpp:199
size_t size() const
Get the size of the argument.
Definition Kernel.hpp:194
Arg()
Constructor.
Definition Kernel.hpp:166
bool operator!=(InputOutput::Variable *var)
Check whether the content of a variable is the same than this argument.
Definition Kernel.hpp:218
Arg(const Arg &arg)
Copy Constructor.
Definition Kernel.hpp:176
void operator=(const Arg &arg)
Copy other argument.
Definition Kernel.hpp:227
void set(const size_t s, const void *v, const cl_event e)
Set the argument.
Definition Kernel.hpp:257
~Arg()
Destructor.
Definition Kernel.hpp:185
bool operator==(InputOutput::Variable *var)
Check whether the content of a variable is the same than this argument.
Definition Kernel.cpp:195
cl_event event() const
Get the last recorded writing event.
Definition Kernel.hpp:204
void operator=(InputOutput::Variable *var)
Copy the variable content on the argument.
Definition Kernel.hpp:236
Definition Kernel.hpp:99
void execute()
Set the kernel arguments.
Definition Kernel.cpp:168
std::vector< InputOutput::Variable * > getVars() const
Get the list of variables to be set.
Definition Kernel.hpp:127
~ArgSetter()
Definition Kernel.hpp:117
std::vector< Arg > getArgs() const
Get the list of variables to be set.
Definition Kernel.hpp:132
cl_kernel getKernel() const
Get the OpenCL kernel.
Definition Kernel.hpp:122
ArgSetter(const std::string name, cl_kernel kernel, std::vector< InputOutput::Variable * > vars)
Definition Kernel.cpp:118
~EventProfile()
Definition Kernel.hpp:67
EventProfile(const std::string name, Tool *tool)
Definition Kernel.hpp:60
void end(cl_event event)
Ending event.
Definition Kernel.cpp:65
void sample()
Compute the time stamps.
Definition Kernel.cpp:86
void start(cl_event event)
Starting event.
Definition Kernel.cpp:49
void variables()
Definition Kernel.cpp:498
size_t workGroupSize() const
Definition Kernel.hpp:323
cl_event _execute(const std::vector< cl_event > events)
Definition Kernel.cpp:325
void make(const std::string entry_point="entry", const std::string flags="", const std::string header="")
Definition Kernel.cpp:355
size_t globalWorkSize() const
Definition Kernel.hpp:328
const std::string path()
Definition Kernel.hpp:318
void computeGlobalWorkSize()
Definition Kernel.cpp:559
void setup()
Definition Kernel.cpp:302
Kernel(const std::string tool_name, const std::string kernel_path, const std::string entry_point="entry", const std::string n="", bool once=false)
Definition Kernel.cpp:276
virtual ~Kernel()
Definition Kernel.cpp:293
A helper class for named objects.
Definition Tool.hpp:49
void name(const std::string tool_name)
Definition Tool.hpp:66
Profiler subinstance base class.
Definition Tool.hpp:84
Profile(const std::string name, Tool *tool)
Constructor.
Definition Tool.hpp:90
Tools base class.
Definition Tool.hpp:176
A generic variable. Almost useless, use the overloaded classes instead of this one.
Definition Variable.hpp:68
virtual size_t typesize() const
Get the variable type size.
Definition Variable.hpp:107
void * get_async()
Get variable pointer basis pointer.
Definition Variable.hpp:129
std::string name() const
Name of the variable.
Definition Variable.hpp:97
cl_event getWritingEvent() const
Alias of InputOutput::Variable::getEvent()
Definition Variable.hpp:219
Calculation server name space.
Definition Assert.cpp:32
void sync_user_event(cl_event user_event, cl_event event)
Synchronize an user event with another OpenCL event.
Definition Kernel.cpp:247
Main AQUAgpusph namespace.
Definition ArgumentsManager.cpp:50
@ L_ERROR
Definition Logger.hpp:75
@ L_DEBUG
Definition Logger.hpp:72
Set of definitions and macros related with the implementation.