Alsvinn  0.5.3
The fast FVM simulator with UQ support
Memory.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2018 ETH Zurich, Kjetil Olsen Lye
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #pragma once
17 #include <memory>
18 #include "alsfvm/types.hpp"
20 #include "alsfvm/memory/View.hpp"
21 namespace alsfvm {
22 namespace memory {
27 template<class T>
28 class Memory : public MemoryBase,
29  public std::enable_shared_from_this<Memory<T> > {
30 public:
31 
32 
39  Memory(size_t nx, size_t ny, size_t nz);
40 
41  // Note: Virtual distructor since we will inherit
42  // from this.
43  virtual ~Memory() {}
44 
48  virtual size_t getSize() const;
49 
53  virtual size_t getSizeX() const;
54 
58  virtual size_t getSizeY() const;
59 
63  virtual size_t getSizeZ() const;
64 
66  virtual std::shared_ptr<Memory<T> > makeInstance() const = 0;
67 
69  virtual void copyFrom(const Memory<T>& other) = 0;
70 
80  virtual size_t getExtentXInBytes() const;
81 
91  virtual size_t getExtentYInBytes() const;
92 
99  virtual bool isOnHost() const = 0;
100 
101  T* data() {
102  return getPointer();
103  }
104 
105  const T* data() const {
106  return getPointer();
107  }
108 
109  T& operator[](size_t i) {
110  assert(isOnHost());
111  return data()[i];
112  }
113 
114  T operator[](size_t i) const {
115 
116  return data()[i];
117  }
118 
119 
120  T operator()(int x, int y, int z) const {
121  return getView().at(x, y, z);
122  }
123 
124  T& operator()(int x, int y, int z) {
125  return getView().at(x, y, z);
126  }
127 
134  virtual T* getPointer() = 0;
135 
142  virtual const T* getPointer() const = 0;
143 
150  virtual void copyToHost(T* bufferPointer,
151  size_t bufferLength) const = 0;
152 
159  virtual void copyFromHost(const T* bufferPointer,
160  size_t bufferLength) = 0;
161 
166  virtual void operator+=(const Memory<T>& other) = 0;
167 
172  virtual void operator*=(const Memory<T>& other) = 0;
173 
178  virtual void operator-=(const Memory<T>& other) = 0;
179 
184  virtual void operator/=(const Memory<T>& other) = 0;
185 
186 
191  virtual void operator+=(real scalar) = 0;
192 
197  virtual void operator*=(real scalar) = 0;
198 
203  virtual void operator-=(real scalar) = 0;
204 
209  virtual void operator/=(real scalar) = 0;
210 
214  virtual void makeZero() = 0;
215 
247  virtual void copyInternalCells(size_t startX, size_t endX,
248  size_t startY, size_t endY,
249  size_t startZ, size_t endZ,
250  T* output, size_t outputSize) = 0;
251 
256  View<T> getView();
257 
258 
263  View<const T> getView() const;
264 
265 
270  virtual void addLinearCombination(T a1,
271  T a2, const Memory<T>& v2,
272  T a3, const Memory<T>& v3,
273  T a4, const Memory<T>& v4,
274  T a5, const Memory<T>& v5) = 0;
275 
276 
277 
284  virtual void addPower(const Memory<T>& other, double power) = 0;
285 
293  virtual void addPower(const Memory<T>& other, double power, double factor) = 0;
294 
295 
302  virtual void subtractPower(const Memory<T>& other, double power) = 0;
303 
304 
306  virtual std::shared_ptr<Memory<T> > getHostMemory() = 0;
307 
310  virtual const std::shared_ptr<const Memory<T> > getHostMemory() const = 0;
311 
321  virtual real getTotalVariation(int p, const ivec3& start,
322  const ivec3& end) const = 0;
323 
334  virtual real getTotalVariation(int direction, int p, const ivec3& start,
335  const ivec3& end) const = 0;
336 
337 
338 
339 protected:
340  const size_t nx;
341  const size_t ny;
342  const size_t nz;
343 };
344 }
345 }
virtual void copyInternalCells(size_t startX, size_t endX, size_t startY, size_t endY, size_t startZ, size_t endZ, T *output, size_t outputSize)=0
copyInternalCells copies the internal cells into the memory area This is ideal for removing ghost cel...
Memory(size_t nx, size_t ny, size_t nz)
Memory constructs new memory.
Definition: Memory.cpp:21
virtual size_t getExtentYInBytes() const
Definition: Memory.cpp:52
virtual void operator-=(const Memory< T > &other)=0
virtual void addLinearCombination(T a1, T a2, const Memory< T > &v2, T a3, const Memory< T > &v3, T a4, const Memory< T > &v4, T a5, const Memory< T > &v5)=0
virtual void copyToHost(T *bufferPointer, size_t bufferLength) const =0
View< T > getView()
getView gets the view to the memory
Definition: Memory.cpp:57
const size_t ny
Definition: Memory.hpp:341
T & operator[](size_t i)
Definition: Memory.hpp:109
const T * data() const
Definition: Memory.hpp:105
virtual size_t getSizeX() const
Definition: Memory.cpp:32
virtual bool isOnHost() const =0
virtual std::shared_ptr< Memory< T > > getHostMemory()=0
Copies the data to host if it is on GPU, otherwise makes a copy.
T operator()(int x, int y, int z) const
Definition: Memory.hpp:120
double real
Definition: types.hpp:65
virtual void subtractPower(const Memory< T > &other, double power)=0
Definition: Memory.hpp:28
virtual ~Memory()
Definition: Memory.hpp:43
virtual void operator/=(const Memory< T > &other)=0
const size_t nx
Definition: Memory.hpp:340
virtual void addPower(const Memory< T > &other, double power)=0
virtual size_t getExtentXInBytes() const
Definition: Memory.cpp:47
virtual T * getPointer()=0
virtual void copyFromHost(const T *bufferPointer, size_t bufferLength)=0
virtual std::shared_ptr< Memory< T > > makeInstance() const =0
Clones the memory area, but does not copy the content
virtual real getTotalVariation(int p, const ivec3 &start, const ivec3 &end) const =0
virtual size_t getSizeZ() const
Definition: Memory.cpp:42
virtual void makeZero()=0
makeZero sets every element to zero (0)
T operator[](size_t i) const
Definition: Memory.hpp:114
virtual void operator+=(const Memory< T > &other)=0
const size_t nz
Definition: Memory.hpp:342
virtual void copyFrom(const Memory< T > &other)=0
Copies the contents of the other memory area into this one.
Definition: View.hpp:32
Various utility functions to implement the tecno flux.
Definition: types.hpp:30
float p
Definition: sodshocktube.py:5
Definition: MemoryBase.hpp:24
T * data()
Definition: Memory.hpp:101
T & operator()(int x, int y, int z)
Definition: Memory.hpp:124
virtual void operator*=(const Memory< T > &other)=0
virtual size_t getSizeY() const
Definition: Memory.cpp:37
virtual size_t getSize() const
Definition: Memory.cpp:27