Block-Structured AMR Software Framework
AMReX_MemPool.H
Go to the documentation of this file.
1 #ifndef BL_MEMPOOL_H
2 #define BL_MEMPOOL_H
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_GpuLaunch.H>
6 #include <AMReX_REAL.H>
7 
8 #include <limits>
9 #include <type_traits>
10 
11 extern "C" {
12  void amrex_mempool_init ();
13  void amrex_mempool_finalize ();
14  void* amrex_mempool_alloc (size_t n);
15  void amrex_mempool_free (void* p);
16  void amrex_mempool_get_stats (int& mp_min, int& mp_max, int& mp_tot);
17  void amrex_real_array_init (amrex_real* p, size_t nelems);
18  void amrex_array_init_snan (amrex_real* p, size_t nelems);
19 }
20 
21 namespace amrex {
22  template <RunOn run_on, typename T,
23  std::enable_if_t<std::is_same_v<T,double> || std::is_same_v<T,float>, int> FOO = 0>
24  void fill_snan (T* p, std::size_t nelems)
25  {
26  if (p == nullptr || nelems == 0) { return; }
27 #ifdef AMREX_USE_GPU
28  if (Gpu::inLaunchRegion() && run_on == RunOn::Device) {
29  amrex::ParallelFor(nelems, [=] AMREX_GPU_DEVICE (Long i) noexcept
30  {
31  p[i] = std::numeric_limits<T>::signaling_NaN();
32  });
33  } else
34 #endif
35  {
36  if constexpr (std::is_same_v<T,float>) {
37 #ifdef UINT32_MAX
38  const uint32_t snan = UINT32_C(0x7fa00000);
39  static_assert(sizeof(float) == sizeof(uint32_t), "MemPool: sizeof float != sizeof uint32_t");
40  for (size_t i = 0; i < nelems; ++i) {
41  std::memcpy(p++, &snan, sizeof(float));
42  }
43 #endif
44  } else if constexpr (std::is_same_v<T,double>) {
45 #ifdef UINT64_MAX
46  const uint64_t snan = UINT64_C(0x7ff0000080000001);
47  static_assert(sizeof(double) == sizeof(uint64_t), "MemPool: sizeof double != sizeof uint64_t");
48  for (size_t i = 0; i < nelems; ++i) {
49  std::memcpy(p++, &snan, sizeof(double));
50  }
51 #endif
52  }
53  }
54  }
55 }
56 
57 #endif
#define AMREX_GPU_DEVICE
Definition: AMReX_GpuQualifiers.H:18
void amrex_mempool_free(void *p)
Definition: AMReX_MemPool.cpp:80
void amrex_mempool_init()
Definition: AMReX_MemPool.cpp:31
void amrex_array_init_snan(amrex_real *p, size_t nelems)
void amrex_mempool_get_stats(int &mp_min, int &mp_max, int &mp_tot)
min, max & tot in MB
Definition: AMReX_MemPool.cpp:86
void amrex_mempool_finalize()
Definition: AMReX_MemPool.cpp:68
void * amrex_mempool_alloc(size_t n)
Definition: AMReX_MemPool.cpp:74
void amrex_real_array_init(amrex_real *p, size_t nelems)
double amrex_real
Definition: AMReX_REAL.H:55
bool inLaunchRegion() noexcept
Definition: AMReX_GpuControl.H:86
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void * memcpy(void *dest, const void *src, std::size_t count)
Definition: AMReX_GpuUtility.H:214
Definition: AMReX_Amr.cpp:49
std::enable_if_t< std::is_integral_v< T > > ParallelFor(TypeList< CTOs... > ctos, std::array< int, sizeof...(CTOs)> const &runtime_options, T N, F &&f)
Definition: AMReX_CTOParallelForImpl.H:200
RunOn
Definition: AMReX_GpuControl.H:69
void fill_snan(T *p, std::size_t nelems)
Definition: AMReX_MemPool.H:24