Alsvinn  0.5.3
The fast FVM simulator with UQ support
HostMemory.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 <vector>
18 #include "alsfvm/memory/Memory.hpp"
19 #include "alsfvm/memory/index.hpp"
20 
21 namespace alsfvm {
22 namespace memory {
23 
24 template<class T>
26 public:
27 
33  HostMemory(size_t nx, size_t ny = 1, size_t nz = 1);
34 
35 
37  virtual std::shared_ptr<Memory<T> > makeInstance() const override;
38 
45  virtual bool isOnHost() const override;
46 
48  virtual void copyFrom(const Memory<T>& other) override;
49 
56  virtual T* getPointer() override;
57 
64  virtual const T* getPointer() const override;
65 
72  virtual void copyToHost(T* bufferPointer,
73  size_t bufferLength) const override;
74 
81  virtual void copyFromHost(const T* bufferPointer,
82  size_t bufferLength) override;
83 
84 
89  virtual void operator+=(const Memory<T>& other) override;
90 
95  virtual void operator*=(const Memory<T>& other) override;
96 
101  virtual void operator-=(const Memory<T>& other) override;
102 
107  virtual void operator/=(const Memory<T>& other) override;
108 
113  virtual void operator+=(real scalar) override;
114 
119  virtual void operator*=(real scalar) override;
120 
125  virtual void operator-=(real scalar) override;
126 
131  virtual void operator/=(real scalar) override;
132 
140  T& at(size_t x, size_t y = 0, size_t z = 0);
141 
142 
150  const T& at(size_t x, size_t y = 0, size_t z = 0) const;
151 
155  virtual void makeZero() override;
156 
186  virtual void copyInternalCells(size_t startX, size_t endX,
187  size_t startY, size_t endY,
188  size_t startZ, size_t endZ,
189  T* output, size_t outputSize) override;
190 
195  virtual void addLinearCombination(T a1,
196  T a2, const Memory<T>& v2,
197  T a3, const Memory<T>& v3,
198  T a4, const Memory<T>& v4,
199  T a5, const Memory<T>& v5) override;
200 
201 
208  virtual void addPower(const Memory<T>& other, double power) override;
209 
210 
217  virtual void addPower(const Memory<T>& other, double power,
218  double factor) override;
219 
226  virtual void subtractPower(const Memory<T>& other, double power) override;
227 
228 
229  virtual std::shared_ptr<Memory<T> > getHostMemory() override;
230 
233  virtual const std::shared_ptr<const Memory<T> > getHostMemory() const override;
234 
244  virtual real getTotalVariation(int p, const ivec3& start,
245  const ivec3& end) const override;
246 
257  virtual real getTotalVariation(int direction, int p, const ivec3& start,
258  const ivec3& end) const override;
259 
260 
261 private:
262  std::vector<T> data;
263 };
264 
265 template<class T>
266 T& HostMemory<T>::at(size_t x, size_t y, size_t z) {
267  return data[calculateIndex(x, y, z, this->nx, this->ny)];
268 }
269 
270 template<class T>
271 const T& HostMemory<T>::at(size_t x, size_t y, size_t z) const {
272  return data[calculateIndex(x, y, z, this->nx, this->ny)];
273 }
274 
275 } // namespace memory
276 } // namespace alsfvm
277 
278 
Definition: HostMemory.hpp:25
virtual T * getPointer() override
Definition: HostMemory.cpp:69
HostMemory(size_t nx, size_t ny=1, size_t nz=1)
Definition: HostMemory.cpp:36
virtual void copyToHost(T *bufferPointer, size_t bufferLength) const override
Definition: HostMemory.cpp:79
virtual std::shared_ptr< Memory< T > > getHostMemory() override
Copies the data to host if it is on GPU, otherwise makes a copy.
Definition: HostMemory.cpp:314
const size_t ny
Definition: Memory.hpp:341
virtual void operator/=(const Memory< T > &other) override
Definition: HostMemory.cpp:159
virtual void copyFrom(const Memory< T > &other) override
Copies the contents of the other memory area into this one.
Definition: HostMemory.cpp:62
virtual void subtractPower(const Memory< T > &other, double power) override
Definition: HostMemory.cpp:304
double real
Definition: types.hpp:65
virtual void operator+=(const Memory< T > &other) override
Definition: HostMemory.cpp:97
virtual void addPower(const Memory< T > &other, double power) override
Definition: HostMemory.cpp:283
T & at(size_t x, size_t y=0, size_t z=0)
at returns the data at the given index
Definition: HostMemory.hpp:266
Definition: Memory.hpp:28
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) override
copyInternalCells copies the internal cells into the memory area This is ideal for removing ghost cel...
Definition: HostMemory.cpp:239
const size_t nx
Definition: Memory.hpp:340
size_t calculateIndex(size_t x, size_t y, size_t z, size_t nx, size_t ny)
calculateIndex calculates the index for the given coordinates
Definition: index.hpp:100
virtual void operator-=(const Memory< T > &other) override
Definition: HostMemory.cpp:137
virtual void copyFromHost(const T *bufferPointer, size_t bufferLength) override
Definition: HostMemory.cpp:85
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) override
Definition: HostMemory.cpp:261
const size_t nz
Definition: Memory.hpp:342
virtual std::shared_ptr< Memory< T > > makeInstance() const override
Clones the memory area, but does not copy the content
Definition: HostMemory.cpp:48
Various utility functions to implement the tecno flux.
Definition: types.hpp:30
float p
Definition: sodshocktube.py:5
T * data()
Definition: Memory.hpp:101
virtual void makeZero() override
makeZero sets every element to zero (0)
Definition: HostMemory.cpp:230
virtual void operator*=(const Memory< T > &other) override
Definition: HostMemory.cpp:115
virtual real getTotalVariation(int p, const ivec3 &start, const ivec3 &end) const override
Definition: HostMemory.cpp:325
virtual bool isOnHost() const override
Definition: HostMemory.cpp:57