ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ActuatorDeviceBuffers.H
Go to the documentation of this file.
1 #ifndef ERF_ACTUATOR_DEVICE_BUFFERS_H
2 #define ERF_ACTUATOR_DEVICE_BUFFERS_H
3 
4 /** Device memory that the actuator spreading and sampling kernels keep from one call to the next. */
5 
6 #include <algorithm>
7 #include <cstddef>
8 #include <vector>
9 
10 #include <AMReX_GpuContainers.H>
11 #include <AMReX_INT.H>
12 #include <AMReX_REAL.H>
13 
14 namespace erf_actuator {
15 
16 /** Grow v to hold at least n values; never shrink it, so that a buffer keeps its largest size. */
17 template <class V>
18 void grow_buffer (V& v, std::size_t n)
19 {
20  if (v.size() < n) { v.resize(n); }
21 }
22 
23 /**
24  * An array filled on the host and read by the kernels: the host side is a std::vector (clear() keeps its
25  * capacity), copied through pinned memory to the device with one asynchronous copy. The pinned and device
26  * sides only grow. A call that uploads must synchronize the stream before it returns, so that the next
27  * call may refill the host side.
28  */
29 template <class T>
31 {
32  std::vector<T> host; //!< what the next upload() sends
33  amrex::Gpu::PinnedVector<T> pinned; //!< staging, and the host side of download_async()
34  amrex::Gpu::DeviceVector<T> device;
35 
36  /** Copy host to the device, asynchronously. */
37  void upload ()
38  {
39  grow_buffer(pinned, host.size());
40  grow_buffer(device, host.size());
41  if (host.empty()) { return; }
42  std::copy(host.begin(), host.end(), pinned.begin());
43  amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice, pinned.begin(), pinned.begin() + host.size(), device.begin());
44  }
45  /** Copy the first n device values to pinned, asynchronously: they are there after the next stream synchronization. */
46  void download_async (std::size_t n)
47  {
48  grow_buffer(pinned, n);
49  if (n == 0) { return; }
50  amrex::Gpu::copyAsync(amrex::Gpu::deviceToHost, device.begin(), device.begin() + n, pinned.begin());
51  }
52  T* data () { return device.data(); }
53 };
54 
55 /**
56  * The buffers of the spreading kernels (spread_partial_norms, add_scaled_kernels) and of the sampler
57  * (sample_velocity_local), kept by their caller so that a step allocates no
58  * device memory once the buffers have reached their size. Passing none makes a call use its own.
59  */
61 {
62  // spreading, one entry per periodic image of a point within reach of the domain
63  StagedBuffer<amrex::Real> image_pos; //!< 3 per image: its position (m)
64  StagedBuffer<int> image_point; //!< 1 per image: the point it is an image of
65  StagedBuffer<int> image_reach; //!< 18 per image: per face grid, the faces within its reach (lo x y z, hi x y z)
66  // spreading, one entry per point
67  StagedBuffer<amrex::Real> point_eps; //!< 2 per point: 1 / eps^2 and the squared cut-off radius 9 eps^2
68  StagedBuffer<amrex::Real> scales; //!< 3 per point: F_p / S_p (add_scaled_kernels)
69  StagedBuffer<amrex::Real> norms; //!< 3 per point: the accumulators of S_p (spread_partial_norms)
70  // spreading, per (box, face grid): which images reach its faces, in point then image order
72  // sampling, per point
73  StagedBuffer<amrex::Real> sample_pos; //!< 3 per point (m)
74  StagedBuffer<amrex::Real> sample_vel; //!< 3 per point (m/s)
75  StagedBuffer<int> sample_cnt; //!< 1 per point: how many of this rank's boxes hold it
76 
77  amrex::Long kernel_launches{0}; //!< kernels launched through these buffers so far (for the tests)
78  /** A switch for the unit tests only; a model leaves it false. When true, add_scaled_kernels() runs the
79  * face-by-face pass on the host too, which is the only pass a device has, so a CPU build can compare the
80  * two. Both give the same sources bit for bit; the face-by-face pass is the slower one on a host. */
81  bool force_face_major{false};
82 };
83 
84 } // namespace erf_actuator
85 
86 #endif
@ T
Definition: ERF_IndexDefines.H:180
@ V
Definition: ERF_IndexDefines.H:179
Definition: ERF_ActuatorDeviceBuffers.H:14
void grow_buffer(V &v, std::size_t n)
Definition: ERF_ActuatorDeviceBuffers.H:18
Definition: ERF_ActuatorDeviceBuffers.H:61
StagedBuffer< int > sample_cnt
1 per point: how many of this rank's boxes hold it
Definition: ERF_ActuatorDeviceBuffers.H:75
StagedBuffer< amrex::Real > sample_vel
3 per point (m/s)
Definition: ERF_ActuatorDeviceBuffers.H:74
StagedBuffer< int > bins
Definition: ERF_ActuatorDeviceBuffers.H:71
StagedBuffer< int > image_point
1 per image: the point it is an image of
Definition: ERF_ActuatorDeviceBuffers.H:64
StagedBuffer< amrex::Real > point_eps
2 per point: 1 / eps^2 and the squared cut-off radius 9 eps^2
Definition: ERF_ActuatorDeviceBuffers.H:67
StagedBuffer< amrex::Real > scales
3 per point: F_p / S_p (add_scaled_kernels)
Definition: ERF_ActuatorDeviceBuffers.H:68
StagedBuffer< amrex::Real > norms
3 per point: the accumulators of S_p (spread_partial_norms)
Definition: ERF_ActuatorDeviceBuffers.H:69
bool force_face_major
Definition: ERF_ActuatorDeviceBuffers.H:81
StagedBuffer< int > image_reach
18 per image: per face grid, the faces within its reach (lo x y z, hi x y z)
Definition: ERF_ActuatorDeviceBuffers.H:65
amrex::Long kernel_launches
kernels launched through these buffers so far (for the tests)
Definition: ERF_ActuatorDeviceBuffers.H:77
StagedBuffer< amrex::Real > image_pos
3 per image: its position (m)
Definition: ERF_ActuatorDeviceBuffers.H:63
StagedBuffer< amrex::Real > sample_pos
3 per point (m)
Definition: ERF_ActuatorDeviceBuffers.H:73
Definition: ERF_ActuatorDeviceBuffers.H:31
void upload()
Definition: ERF_ActuatorDeviceBuffers.H:37
std::vector< T > host
what the next upload() sends
Definition: ERF_ActuatorDeviceBuffers.H:32
T * data()
Definition: ERF_ActuatorDeviceBuffers.H:52
void download_async(std::size_t n)
Definition: ERF_ActuatorDeviceBuffers.H:46
amrex::Gpu::DeviceVector< T > device
Definition: ERF_ActuatorDeviceBuffers.H:34
amrex::Gpu::PinnedVector< T > pinned
staging, and the host side of download_async()
Definition: ERF_ActuatorDeviceBuffers.H:33