AQUAgpusph 5.0.4
Loading...
Searching...
No Matches
Variable.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 VARIABLE_H_INCLUDED
26#define VARIABLE_H_INCLUDED
27
28#include <Python.h>
29#include <string>
30#include <vector>
31
41#define PY_ARRAY_UNIQUE_SYMBOL AQUA_ARRAY_API
47#define NO_IMPORT_ARRAY
48#include <numpy/npy_no_deprecated_api.h>
49#include <numpy/ndarraytypes.h>
50#include <numpy/ufuncobject.h>
51#include <numpy/npy_3kcompat.h>
52
53#include "sphPrerequisites.hpp"
55
56#ifndef MAX_TYPE_NAME_LEN
57#define MAX_TYPE_NAME_LEN 128
58#endif
59
60namespace Aqua {
61namespace InputOutput {
62
68{
69 public:
74 Variable(const std::string varname, const std::string vartype);
75
78 virtual ~Variable();
79
85 virtual bool isArray() = 0;
86
92 inline bool isScalar() { return !isArray(); }
93
97 std::string name() const { return _name; }
98
102 virtual std::string type() const { return _typename; }
103
107 virtual size_t typesize() const { return 0; }
108
112 virtual size_t size() const { return typesize(); }
113
119 virtual inline void* get(bool UNUSED_PARAM synced = true) { return NULL; }
120
129 inline void* get_async() { return get(false); }
130
140 virtual void set(void* ptr, bool synced = true);
141
150 inline void set_async(void* ptr) { set(ptr, false); }
151
157 virtual PyObject* getPythonObject(int UNUSED_PARAM i0 = 0,
158 int UNUSED_PARAM n = 0)
159 {
160 return NULL;
161 }
162
169 virtual bool setFromPythonObject(PyObject UNUSED_PARAM *obj,
170 int UNUSED_PARAM i0 = 0,
171 int UNUSED_PARAM n = 0)
172 {
173 return true;
174 }
175
179 virtual const std::string asString(bool UNUSED_PARAM synced = true)
180 {
181 return "";
182 }
183
196
205 void setEvent(cl_event event);
206
209 inline void setWritingEvent(cl_event event) { setEvent(event); }
210
215 inline cl_event getEvent() const { return _event; }
216
219 inline cl_event getWritingEvent() const { return getEvent(); }
220
231 void addReadingEvent(cl_event event);
232
241 inline std::vector<cl_event> getReadingEvents() const
242 {
243 return _reading_events;
244 }
245
256 void sync(bool readonly = false);
257
261 protected:
268 void cleanReadingEvents();
269
275 static cl_event createDummyEvent();
276
277 private:
279 std::string _name;
280
282 std::string _typename;
283
285 cl_event _event;
286
288 std::vector<cl_event> _reading_events;
289
295 bool _synced;
296
301 bool _synced_for_read;
302};
303
307template<class T>
309{
310 public:
315 ScalarVariable(const std::string varname, const std::string vartype)
316 : Variable(varname, vartype)
317 {
318 }
319
323
328 inline bool isArray() { return false; }
329
333 size_t typesize() const { return sizeof(T); }
334
344 inline void* get(bool synced = true)
345 {
346 if (synced)
347 sync(true);
348 return &_value;
349 }
350
360 inline void get(T& value, bool synced = true)
361 {
362 if (synced)
363 sync(true);
364 value = _value;
365 }
366
375 inline void set(void* ptr, bool synced = true)
376 {
377 if (synced)
378 sync();
379 memcpy(&_value, ptr, sizeof(T));
380 this->Variable::set(ptr, synced);
381 }
382
391 inline void set(T& value, bool synced = true)
392 {
393 if (synced)
394 sync();
395 _value = value;
396 this->Variable::set(NULL, synced);
397 }
398
404 inline T value(bool synced = true)
405 {
406 if (synced)
407 sync(true);
408 return _value;
409 }
410
416 inline void value(T& value, bool synced = true)
417 {
418 this->set(value, synced);
419 }
420
426 virtual PyObject* getPythonObject(int i0 = 0, int n = 0) = 0;
427
434 virtual bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0) = 0;
435
441 virtual const std::string asString(bool synced) = 0;
442
443 protected:
446};
447
451template<class T>
453{
454 public:
459 ScalarNumberVariable(const std::string varname, const std::string vartype)
460 : ScalarVariable<T>(varname, vartype)
461 {
462 }
463
467
473 virtual const std::string asString(bool synced = true);
474};
475
479class DECLDIR IntVariable final : public ScalarNumberVariable<icl>
480{
481 public:
485 IntVariable(const std::string varname);
486
490
496 PyObject* getPythonObject(int i0 = 0, int n = 0);
497
504 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
505};
506
511{
512 public:
516 LongVariable(const std::string varname);
517
521
527 PyObject* getPythonObject(int i0 = 0, int n = 0);
528
535 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
536};
537
541class DECLDIR UIntVariable final : public ScalarNumberVariable<uicl>
542{
543 public:
547 UIntVariable(const std::string varname);
548
552
558 PyObject* getPythonObject(int i0 = 0, int n = 0);
559
566 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
567};
568
573{
574 public:
578 ULongVariable(const std::string varname);
579
583
589 PyObject* getPythonObject(int i0 = 0, int n = 0);
590
597 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
598};
599
604{
605 public:
609 FloatVariable(const std::string varname);
610
614
620 PyObject* getPythonObject(int i0 = 0, int n = 0);
621
628 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
629};
630
635{
636 public:
640 DoubleVariable(const std::string varname);
641
645
651 PyObject* getPythonObject(int i0 = 0, int n = 0);
652
659 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
660};
661
665template<class T>
667{
668 public:
675 ScalarVecVariable(const std::string varname,
676 const std::string vartype,
677 const unsigned int dims,
678 int np_type);
679
683
689 PyObject* getPythonObject(int i0 = 0, int n = 0);
690
697 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
698
704 virtual const std::string asString(bool synced = true);
705
706 protected:
717 bool checkPyhonObjectDims(PyObject* obj);
718
719 private:
721 unsigned int _dims;
723 int _np_type;
724};
725
726#define __DECLARE_AQUA_VEC(NAME, TYPE) \
727 class DECLDIR NAME final : public ScalarVecVariable<TYPE> \
728 { \
729 public: \
730 NAME(const std::string varname); \
731 ~NAME(){}; \
732 };
733
738
743
748
753
758
763
768
773
778
783
788
793
798
803
808
813
818
823
828
833
838
843
848
853
889
894{
895 public:
900 ArrayVariable(const std::string varname, const std::string vartype);
901
905
910 bool isArray();
911
917 size_t typesize() const { return sizeof(cl_mem); }
918
924 size_t size() const;
925
930 void* get(bool UNUSED_PARAM synced = false) { return &_value; }
931
938 void set(void* ptr, bool synced = false);
939
943 inline bool reallocatable() const { return _reallocatable; }
944
949 inline void reallocatable(bool is) { _reallocatable = is; }
950
957 PyObject* getPythonObject(int i0 = 0, int n = 0);
958
965 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
966
972 const std::string asString(bool synced = true);
973
980 const std::string asString(size_t i, bool synced = true);
981
982 private:
984 void cleanMem();
985
987 cl_mem _value;
988
990 bool _reallocatable;
991
1005 std::vector<void*> _data;
1006
1011 std::vector<PyObject*> _objects;
1012};
1013
1014// ---------------------------------------------------------------------------
1015// Variables manager
1016// ---------------------------------------------------------------------------
1017
1022{
1023 public:
1026 Variables();
1027
1030 ~Variables();
1031
1041 void registerVariable(const std::string name,
1042 const std::string type,
1043 const std::string length,
1044 const std::string value);
1045
1050 Variable* get(unsigned int index);
1051
1056 Variable* get(const std::string name);
1057
1061 std::vector<Variable*> getAll() const { return _vars; }
1062
1066 unsigned int size() const { return _vars.size(); }
1067
1072 size_t allocatedMemory();
1073
1078 static size_t typeToBytes(const std::string type);
1079
1084 static unsigned int typeToN(const std::string type);
1085
1092 static bool isSameType(const std::string type_a,
1093 const std::string type_b,
1094 bool ignore_asterisk = true);
1095
1101 std::vector<Variable*> exprVariables(const std::string& expr);
1102
1110 void solve(const std::string type_name,
1111 const std::string value,
1112 void* data,
1113 const std::string name = "NULL");
1114
1120 void populate(const std::string name = "");
1121
1126 void populate(Variable* var);
1127
1153 static std::string typeAlias(const std::string& t);
1154
1155 private:
1162 void registerScalar(const std::string name,
1163 const std::string type,
1164 const std::string value);
1172 void registerClMem(const std::string name,
1173 const std::string type,
1174 const std::string length);
1175
1183 template<typename T>
1184 T solve(const std::string& name, const std::string& value);
1185
1187 std::vector<Variable*> _vars;
1189 Tokenizer tok;
1190};
1191
1192}
1193} // namespace
1194
1195#endif // VARIABLE_H_INCLUDED
static PyObject * get(PyObject UNUSED_PARAM *self, PyObject *args, PyObject *keywds)
Get a variable by its name.
Definition Python.cpp:73
static PyObject * set(PyObject UNUSED_PARAM *self, PyObject *args, PyObject *keywds)
Set a variable by its name.
Definition Python.cpp:108
#define T
Definition Sort.hcl.in:83
Math expression evaluator. (See Aqua::Tokenizer for details)
#define __DECLARE_AQUA_VEC(NAME, TYPE)
Definition Variable.hpp:726
void reallocatable(bool is)
Set if a variable is reallocatable.
Definition Variable.hpp:949
bool isArray()
Report that the varaible is an array.
Definition Variable.cpp:638
size_t typesize() const
Definition Variable.hpp:917
ArrayVariable(const std::string varname, const std::string vartype)
Definition Variable.cpp:612
void * get(bool UNUSED_PARAM synced=false)
Definition Variable.hpp:930
bool reallocatable() const
Get if a variable is reallocatable.
Definition Variable.hpp:943
A double variable.
Definition Variable.hpp:635
~DoubleVariable()
Destructor.
Definition Variable.hpp:644
DoubleVariable(const std::string varname)
Constructor.
Definition Variable.cpp:409
A float variable.
Definition Variable.hpp:604
~FloatVariable()
Destructor.
Definition Variable.hpp:613
FloatVariable(const std::string varname)
Constructor.
Definition Variable.cpp:377
An integer variable.
Definition Variable.hpp:480
IntVariable(const std::string varname)
Constructor.
Definition Variable.cpp:249
~IntVariable()
Destructor.
Definition Variable.hpp:489
A 64bits integer variable.
Definition Variable.hpp:511
~LongVariable()
Destructor.
Definition Variable.hpp:520
LongVariable(const std::string varname)
Constructor.
Definition Variable.cpp:281
ScalarNumberVariable(const std::string varname, const std::string vartype)
Constructor.
Definition Variable.hpp:459
~ScalarNumberVariable()
Destructor.
Definition Variable.hpp:466
void value(T &value, bool synced=true)
Set the variable value.
Definition Variable.hpp:416
void set(T &value, bool synced=true)
Set variable value.
Definition Variable.hpp:391
virtual const std::string asString(bool synced)=0
Get the variable text representation.
T value(bool synced=true)
Get the variable value.
Definition Variable.hpp:404
void get(T &value, bool synced=true)
Get variable value.
Definition Variable.hpp:360
bool isArray()
Report that the varaible is not an array.
Definition Variable.hpp:328
size_t typesize() const
Get the variable type size.
Definition Variable.hpp:333
virtual bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)=0
Set the variable from a Python object.
virtual PyObject * getPythonObject(int i0=0, int n=0)=0
Get a Python Object representation of the variable.
void * get(bool synced=true)
Get variable pointer basis pointer.
Definition Variable.hpp:344
ScalarVariable(const std::string varname, const std::string vartype)
Constructor.
Definition Variable.hpp:315
void set(void *ptr, bool synced=true)
Set variable from memory.
Definition Variable.hpp:375
T _value
Variable value.
Definition Variable.hpp:445
~ScalarVariable()
Destructor.
Definition Variable.hpp:322
ScalarVecVariable(const std::string varname, const std::string vartype, const unsigned int dims, int np_type)
Constructor.
Definition Variable.cpp:442
~ScalarVecVariable()
Destructor.
Definition Variable.hpp:682
An unsigned integer variable.
Definition Variable.hpp:542
UIntVariable(const std::string varname)
Constructor.
Definition Variable.cpp:313
~UIntVariable()
Destructor.
Definition Variable.hpp:551
An integer variable.
Definition Variable.hpp:573
~ULongVariable()
Destructor.
Definition Variable.hpp:582
ULongVariable(const std::string varname)
Constructor.
Definition Variable.cpp:345
A generic variable. Almost useless, use the overloaded classes instead of this one.
Definition Variable.hpp:68
virtual bool setFromPythonObject(PyObject UNUSED_PARAM *obj, int UNUSED_PARAM i0=0, int UNUSED_PARAM n=0)
Set the variable from a Python object.
Definition Variable.hpp:169
Variable(const std::string varname, const std::string vartype)
Constructor.
Definition Variable.cpp:37
virtual size_t typesize() const
Get the variable type size.
Definition Variable.hpp:107
virtual bool isArray()=0
Let efficiently know whether the variable is an array or not.
virtual void * get(bool UNUSED_PARAM synced=true)
Get variable pointer basis pointer.
Definition Variable.hpp:119
void set_async(void *ptr)
Set variable from memory.
Definition Variable.hpp:150
void * get_async()
Get variable pointer basis pointer.
Definition Variable.hpp:129
virtual std::string type() const
Type of the variable.
Definition Variable.hpp:102
virtual size_t size() const
Get the variable type size.
Definition Variable.hpp:112
virtual PyObject * getPythonObject(int UNUSED_PARAM i0=0, int UNUSED_PARAM n=0)
Get a Python interpretation of the variable.
Definition Variable.hpp:157
std::string name() const
Name of the variable.
Definition Variable.hpp:97
virtual const std::string asString(bool UNUSED_PARAM synced=true)
Get the variable text representation.
Definition Variable.hpp:179
bool isScalar()
Let efficiently know whether the variable is a scalar or not.
Definition Variable.hpp:92
virtual void set(void *ptr, bool synced=true)
Set variable from memory.
Definition Variable.cpp:55
unsigned int size() const
Definition Variable.hpp:1066
std::vector< Variable * > getAll() const
Definition Variable.hpp:1061
Variables()
Definition Variable.cpp:1042
void registerVariable(const std::string name, const std::string type, const std::string length, const std::string value)
Definition Variable.cpp:1053
A dvec2 variable.
A dvec3 variable.
A dvec4 variable.
A dvec8 variable.
A ivec2 variable.
A ivec3 variable.
A ivec4 variable.
A ivec8 variable.
A lvec2 variable.
A lvec3 variable.
A lvec4 variable.
A ivec8 variable.
A uivec2 variable.
A uivec3 variable.
A uivec4 variable.
A uivec8 variable.
A ulvec2 variable.
A ulvec3 variable.
A ulvec4 variable.
A ulvec8 variable.
A vec2 variable.
A vec3 variable.
A vec4 variable.
A vec8 variable.
cl_event getEvent() const
Returns the last writing event associated to this variable.
Definition Variable.hpp:215
void sync(bool readonly=false)
Wait for variable reading and writing events to be completed.
Definition Variable.cpp:141
std::vector< cl_event > getReadingEvents() const
Get the list of reading events.
Definition Variable.hpp:241
void setEvent(cl_event event)
Set the variable current writing event.
Definition Variable.cpp:75
cl_event getWritingEvent() const
Alias of InputOutput::Variable::getEvent()
Definition Variable.hpp:219
void setWritingEvent(cl_event event)
Alias of InputOutput::Variable::setEvent()
Definition Variable.hpp:209
LongVariable lclVariable
Definition Variable.hpp:859
IVec8Variable ivec8Variable
Definition Variable.hpp:867
ULongVariable ulclVariable
Definition Variable.hpp:861
LVec2Variable lvec2Variable
Definition Variable.hpp:868
DoubleVariable dclVariable
Definition Variable.hpp:863
IVec4Variable ivec4Variable
Definition Variable.hpp:866
ULVec3Variable ulvec3Variable
Definition Variable.hpp:877
DVec8Variable dvec8Variable
Definition Variable.hpp:887
ULVec8Variable ulvec8Variable
Definition Variable.hpp:879
UIntVariable uiclVariable
Definition Variable.hpp:860
UIVec2Variable uivec2Variable
Definition Variable.hpp:872
UIVec8Variable uivec8Variable
Definition Variable.hpp:875
DVec3Variable dvec3Variable
Definition Variable.hpp:885
LVec4Variable lvec4Variable
Definition Variable.hpp:870
LVec3Variable lvec3Variable
Definition Variable.hpp:869
ULVec4Variable ulvec4Variable
Definition Variable.hpp:878
Vec4Variable vec4Variable
Definition Variable.hpp:882
Vec2Variable vec2Variable
Definition Variable.hpp:880
FloatVariable fclVariable
Definition Variable.hpp:862
ULVec2Variable ulvec2Variable
Definition Variable.hpp:876
UIVec4Variable uivec4Variable
Definition Variable.hpp:874
Vec3Variable vec3Variable
Definition Variable.hpp:881
IVec3Variable ivec3Variable
Definition Variable.hpp:865
DVec4Variable dvec4Variable
Definition Variable.hpp:886
Vec8Variable vec8Variable
Definition Variable.hpp:883
DVec2Variable dvec2Variable
Definition Variable.hpp:884
IVec2Variable ivec2Variable
Definition Variable.hpp:864
UIVec3Variable uivec3Variable
Definition Variable.hpp:873
LVec8Variable lvec8Variable
Definition Variable.hpp:871
IntVariable iclVariable
Definition Variable.hpp:858
Main AQUAgpusph namespace.
Definition ArgumentsManager.cpp:50
Tokenizer_exprtk Tokenizer
Definition Tokenizer.hpp:34
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