AQUAgpusph 4.1.2
Loading...
Searching...
No Matches
Variable.h
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
25#ifndef VARIABLE_H_INCLUDED
26#define VARIABLE_H_INCLUDED
27
28#include <Python.h>
29#include <string>
30#include <vector>
31#include <sphPrerequisites.h>
32#include <Tokenizer/Tokenizer.h>
33
34#ifdef HAVE_3D
35#define VecVariable Vec4Variable
36#define IVecVariable IVec4Variable
37#define UIVecVariable UIVec4Variable
38#else
39#define VecVariable Vec2Variable
40#define IVecVariable IVec2Variable
41#define UIVecVariable UIVec2Variable
42#endif
43
44namespace Aqua {
45namespace InputOutput {
46
52{
53 public:
58 Variable(const std::string varname, const std::string vartype);
59
62 virtual ~Variable(){};
63
69 virtual bool isArray() = 0;
70
76 virtual bool isScalar() { return !isArray(); }
77
81 std::string name() const { return _name; }
82
86 virtual std::string type() const { return _typename; }
87
91 virtual size_t typesize() const { return 0; }
92
96 virtual size_t size() const { return typesize(); }
97
101 virtual void* get() { return NULL; }
102
106 virtual void set(void* ptr) = 0;
107
113 virtual PyObject* getPythonObject(int i0 = 0, int n = 0) { return NULL; }
114
121 virtual bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0)
122 {
123 return true;
124 }
125
129 virtual const std::string asString() { return ""; }
130
142 void setEvent(cl_event event);
143
148 cl_event getEvent() { return _event; }
149
150 protected:
158 void sync();
159
160 private:
162 std::string _name;
163
165 std::string _typename;
166
168 cl_event _event;
169
171 bool _synced;
172};
173
177template<class T>
179{
180 public:
185 ScalarVariable(const std::string varname, const std::string vartype);
186
190
195 bool isArray();
196
200 size_t typesize() const { return sizeof(T); }
201
210 inline void* get()
211 {
212 sync();
213 return &_value;
214 }
215
224 inline void* get_async() { return &_value; }
225
233 inline void set(void* ptr)
234 {
235 sync();
236 memcpy(&_value, ptr, sizeof(T));
237 }
238
247 inline void set_async(void* ptr) { memcpy(&_value, ptr, sizeof(T)); }
248
252 virtual const std::string asString() = 0;
253
254 protected:
257};
258
262template<class T>
264{
265 public:
270 ScalarNumberVariable(const std::string varname, const std::string vartype);
271
275
279 virtual const std::string asString();
280};
281
286{
287 public:
291 IntVariable(const std::string varname);
292
296
302 PyObject* getPythonObject(int i0 = 0, int n = 0);
303
310 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
311};
312
316class UIntVariable : public ScalarNumberVariable<unsigned int>
317{
318 public:
322 UIntVariable(const std::string varname);
323
327
333 PyObject* getPythonObject(int i0 = 0, int n = 0);
334
341 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
342};
343
348{
349 public:
353 FloatVariable(const std::string varname);
354
358
364 PyObject* getPythonObject(int i0 = 0, int n = 0);
365
372 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
373};
374
378template<class T>
380{
381 public:
386 ScalarVecVariable(const std::string varname,
387 const std::string vartype,
388 const unsigned int dims);
389
393
397 virtual const std::string asString();
398
399 protected:
410 bool checkPyhonObjectDims(PyObject* obj);
411
412 private:
414 unsigned int _dims;
415};
416
420class Vec2Variable : public ScalarVecVariable<vec2>
421{
422 public:
426 Vec2Variable(const std::string varname);
427
431
437 PyObject* getPythonObject(int i0 = 0, int n = 0);
438
445 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
446};
447
451class Vec3Variable : public ScalarVecVariable<vec3>
452{
453 public:
457 Vec3Variable(const std::string varname);
458
462
468 PyObject* getPythonObject(int i0 = 0, int n = 0);
469
476 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
477};
478
482class Vec4Variable : public ScalarVecVariable<vec4>
483{
484 public:
488 Vec4Variable(const std::string varname);
489
493
499 PyObject* getPythonObject(int i0 = 0, int n = 0);
500
507 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
508};
509
513class IVec2Variable : public ScalarVecVariable<ivec2>
514{
515 public:
519 IVec2Variable(const std::string varname);
520
524
530 PyObject* getPythonObject(int i0 = 0, int n = 0);
531
538 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
539};
540
544class IVec3Variable : public ScalarVecVariable<ivec3>
545{
546 public:
550 IVec3Variable(const std::string varname);
551
555
561 PyObject* getPythonObject(int i0 = 0, int n = 0);
562
569 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
570};
571
575class IVec4Variable : public ScalarVecVariable<ivec4>
576{
577 public:
581 IVec4Variable(const std::string varname);
582
586
592 PyObject* getPythonObject(int i0 = 0, int n = 0);
593
600 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
601};
602
606class UIVec2Variable : public ScalarVecVariable<uivec2>
607{
608 public:
612 UIVec2Variable(const std::string varname);
613
617
623 PyObject* getPythonObject(int i0 = 0, int n = 0);
624
631 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
632};
633
637class UIVec3Variable : public ScalarVecVariable<uivec3>
638{
639 public:
643 UIVec3Variable(const std::string varname);
644
648
654 PyObject* getPythonObject(int i0 = 0, int n = 0);
655
662 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
663};
664
668class UIVec4Variable : public ScalarVecVariable<uivec4>
669{
670 public:
674 UIVec4Variable(const std::string varname);
675
679
685 PyObject* getPythonObject(int i0 = 0, int n = 0);
686
693 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
694};
695
700{
701 public:
706 ArrayVariable(const std::string varname, const std::string vartype);
707
711
716 bool isArray();
717
723 size_t typesize() const { return sizeof(cl_mem); }
724
730 size_t size() const;
731
735 void* get() { return &_value; }
736
740 void set(void* ptr) { _value = *(cl_mem*)ptr; }
741
748 PyObject* getPythonObject(int i0 = 0, int n = 0);
749
756 bool setFromPythonObject(PyObject* obj, int i0 = 0, int n = 0);
757
761 const std::string asString();
762
767 const std::string asString(size_t i);
768
769 private:
771 void cleanMem();
772
774 cl_mem _value;
788 std::vector<void*> _data;
793 std::vector<PyObject*> _objects;
794};
795
796// ---------------------------------------------------------------------------
797// Variables manager
798// ---------------------------------------------------------------------------
799
804{
805 public:
808 Variables();
809
812 ~Variables();
813
823 void registerVariable(const std::string name,
824 const std::string type,
825 const std::string length,
826 const std::string value);
827
832 Variable* get(unsigned int index);
833
838 Variable* get(const std::string name);
839
843 std::vector<Variable*> getAll() { return _vars; }
844
848 unsigned int size() const { return _vars.size(); }
849
854 size_t allocatedMemory();
855
860 static size_t typeToBytes(const std::string type);
861
866 static unsigned int typeToN(const std::string type);
867
874 bool isSameType(const std::string type_a,
875 const std::string type_b,
876 bool ignore_asterisk = true);
877
885 void solve(const std::string type_name,
886 const std::string value,
887 void* data,
888 const std::string name = "NULL");
889
895 void populate(const std::string name = "");
896
901 void populate(Variable* var);
902
903 private:
910 void registerScalar(const std::string name,
911 const std::string type,
912 const std::string value);
920 void registerClMem(const std::string name,
921 const std::string type,
922 const std::string length);
923
931 void readComponents(const std::string name,
932 const std::string value,
933 unsigned int n,
934 float* v);
935
937 std::vector<Variable*> _vars;
939 Tokenizer tok;
940};
941
942}
943} // namespace
944
945#endif // VARIABLE_H_INCLUDED
Python script execution tool. (See Aqua::CalcServer::Python for details)
Math expression evaluator. (See Aqua::Tokenizer for details)
Definition: Variable.h:700
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:699
bool isArray()
Report that the varaible is an array.
Definition: Variable.cpp:674
size_t size() const
Definition: Variable.cpp:680
size_t typesize() const
Definition: Variable.h:723
void * get()
Definition: Variable.h:735
const std::string asString()
Definition: Variable.cpp:930
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:812
~ArrayVariable()
Definition: Variable.cpp:656
void set(void *ptr)
Definition: Variable.h:740
A float variable.
Definition: Variable.h:348
~FloatVariable()
Destructor.
Definition: Variable.h:357
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Set the variable from a Python object.
Definition: Variable.cpp:236
PyObject * getPythonObject(int i0=0, int n=0)
Get a PyFloatObject interpretation of the variable.
Definition: Variable.cpp:229
A ivec2 variable.
Definition: Variable.h:514
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:429
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:437
~IVec2Variable()
Definition: Variable.h:523
A ivec3 variable.
Definition: Variable.h:545
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:467
~IVec3Variable()
Definition: Variable.h:554
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:475
A ivec4 variable.
Definition: Variable.h:576
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:506
~IVec4Variable()
Definition: Variable.h:585
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:514
An integer variable.
Definition: Variable.h:286
~IntVariable()
Destructor.
Definition: Variable.h:295
PyObject * getPythonObject(int i0=0, int n=0)
Get a PyLongObject interpretation of the variable.
Definition: Variable.cpp:169
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Set the variable from a Python object.
Definition: Variable.cpp:176
Definition: Variable.h:264
~ScalarNumberVariable()
Destructor.
Definition: Variable.h:274
virtual const std::string asString()
Get the variable text representation.
Definition: Variable.cpp:154
A generic Scalar variable.
Definition: Variable.h:179
void * get_async()
Get variable pointer basis pointer.
Definition: Variable.h:224
void * get()
Get variable pointer basis pointer.
Definition: Variable.h:210
bool isArray()
Report that the varaible is not an array.
Definition: Variable.cpp:140
void set(void *ptr)
Set variable from memory.
Definition: Variable.h:233
size_t typesize() const
Get the variable type size.
Definition: Variable.h:200
virtual const std::string asString()=0
Get the variable text representation.
T _value
Variable value.
Definition: Variable.h:256
void set_async(void *ptr)
Set variable from memory.
Definition: Variable.h:247
~ScalarVariable()
Destructor.
Definition: Variable.h:189
A generic Scalar variable, of 2 or more components.
Definition: Variable.h:380
~ScalarVecVariable()
Destructor.
Definition: Variable.h:392
virtual const std::string asString()
Get the variable text representation.
Definition: Variable.cpp:294
bool checkPyhonObjectDims(PyObject *obj)
Check that a Python object is compatible with the variable type.
Definition: Variable.cpp:263
A uivec2 variable.
Definition: Variable.h:607
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:543
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:551
~UIVec2Variable()
Definition: Variable.h:616
Definition: Variable.h:638
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:589
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:581
~UIVec3Variable()
Definition: Variable.h:647
A uivec4 variable.
Definition: Variable.h:669
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:620
~UIVec4Variable()
Definition: Variable.h:678
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:628
An integer variable.
Definition: Variable.h:317
~UIntVariable()
Destructor.
Definition: Variable.h:326
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Set the variable from a Python object.
Definition: Variable.cpp:206
PyObject * getPythonObject(int i0=0, int n=0)
Get a PyLongObject interpretation of the variable.
Definition: Variable.cpp:199
A generic variable. Almost useless, use the overloaded classes instead of this one.
Definition: Variable.h:52
virtual PyObject * getPythonObject(int i0=0, int n=0)
Get a Python interpretation of the variable.
Definition: Variable.h:113
virtual size_t typesize() const
Get the variable type size.
Definition: Variable.h:91
virtual const std::string asString()
Get the variable text representation.
Definition: Variable.h:129
virtual bool isArray()=0
Let efficiently know whether the variable is an array or not.
virtual ~Variable()
Destructor.
Definition: Variable.h:62
virtual void set(void *ptr)=0
Set variable from memory.
virtual void * get()
Get variable pointer basis pointer.
Definition: Variable.h:101
cl_event getEvent()
Returns the last event associated to this variable.
Definition: Variable.h:148
virtual bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Set the variable from a Python object.
Definition: Variable.h:121
virtual std::string type() const
Type of the variable.
Definition: Variable.h:86
virtual size_t size() const
Get the variable type size.
Definition: Variable.h:96
virtual bool isScalar()
Let efficiently know whether the variable is a scalar or not.
Definition: Variable.h:76
std::string name() const
Name of the variable.
Definition: Variable.h:81
void setEvent(cl_event event)
Set the variable current event.
Definition: Variable.cpp:86
void sync()
Wait for variable underlying event to be complete.
Definition: Variable.cpp:114
Variables manager, which can interpret the types on the fly.
Definition: Variable.h:804
bool isSameType(const std::string type_a, const std::string type_b, bool ignore_asterisk=true)
Definition: Variable.cpp:1190
size_t allocatedMemory()
Definition: Variable.cpp:1126
static unsigned int typeToN(const std::string type)
Definition: Variable.cpp:1164
unsigned int size() const
Definition: Variable.h:848
void solve(const std::string type_name, const std::string value, void *data, const std::string name="NULL")
Definition: Variable.cpp:1231
std::vector< Variable * > getAll()
Definition: Variable.h:843
void populate(const std::string name="")
Populate variables in order that the tokenizer may get the updated value.
Definition: Variable.cpp:1395
Variables()
Definition: Variable.cpp:1072
Variable * get(unsigned int index)
Definition: Variable.cpp:1106
~Variables()
Definition: Variable.cpp:1074
static size_t typeToBytes(const std::string type)
Definition: Variable.cpp:1139
void registerVariable(const std::string name, const std::string type, const std::string length, const std::string value)
Definition: Variable.cpp:1084
A vec2 variable.
Definition: Variable.h:421
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:323
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:315
~Vec2Variable()
Definition: Variable.h:430
A vec3 variable.
Definition: Variable.h:452
~Vec3Variable()
Definition: Variable.h:461
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:361
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:353
A vec4 variable.
Definition: Variable.h:483
PyObject * getPythonObject(int i0=0, int n=0)
Definition: Variable.cpp:392
bool setFromPythonObject(PyObject *obj, int i0=0, int n=0)
Definition: Variable.cpp:400
~Vec4Variable()
Definition: Variable.h:492
Tool to evaluate math expressions.
Definition: Tokenizer.h:42
Main AQUAgpusph namespace.
Definition: ArgumentsManager.cpp:45
float length(vec v)
Compute the vector length.
Definition: AuxiliarMethods.cpp:521
Set of definitions and macros related with the implementation.