SENSEI
A frame work for generic in situ analytics
MemoryUtils.h
Go to the documentation of this file.
1 #ifndef MemoryUtils_h
2 #define MemoryUtils_h
3 
4 ///@file
5 
6 #include "senseiConfig.h"
7 #include <memory>
8 
9 //#define SENSEI_DEBUG 1
10 
11 /// SENSEI
12 namespace sensei
13 {
14 /// Functions for dealing with memory access across heterogeneous architectures
15 namespace MemoryUtils
16 {
17 
18 /// return true if the pointer is accessible by code running on a CUDA GPU
19 int CudaAccessible(const void *ptr);
20 
21 /// return true if the pointer is accessible by code running on the CPU
22 int HostAccessible(const void *ptr);
23 
24 /// callback that can free memory managed by CUDA
25 void FreeCudaPtr(void *ptr);
26 
27 /// callback that can free memory managed by malloc
28 void FreeHostPtr(void *ptr);
29 
30 /// A callback that does not free the pointer
31 void DontFreePtr(void *ptr);
32 
33 /** @name MakedCudaAccessible
34  * returns a pointer to data that is accessible from CUDA kernels. If the data
35  * is already accessible, this call is a noop. On the other hand if the data is
36  * not accessible it will be moved. The accessible data is returned as a shared
37  * pointer so that in the case data needed to be moved, the buffers on the GPU
38  * will automatically be released.
39  */
40 ///@{
41 std::shared_ptr<void> MakeCudaAccessible_(void *ptr, size_t nBytes);
42 
43 template <typename data_t>
44 std::shared_ptr<data_t> MakeCudaAccessible(data_t *ptr, size_t nVals)
45 {
46  return std::static_pointer_cast<data_t>(
47  sensei::MemoryUtils::MakeCudaAccessible_(
48  (void*)ptr, nVals*sizeof(data_t)));
49 }
50 ///@}
51 
52 /** @name MakeHostAccessible
53  * returns a pointer to data that is accessible from CPU codes. If the data is
54  * already accessible, this call is a noop. On the other hand if the data is
55  * not accessible it will be moved. The accessible data is returned as a shared
56  * pointer so that in the case data needed to be moved, the buffers on the CPU
57  * will automatically be released.
58  */
59 ///@{
60 std::shared_ptr<void> MakeHostAccessible_(void *ptr, size_t nBytes);
61 
62 template <typename data_t>
63 std::shared_ptr<data_t> MakeHostAccessible(data_t *ptr, size_t nVals)
64 {
65  return std::static_pointer_cast<data_t>(
66  sensei::MemoryUtils::MakeHostAccessible_(
67  (void*)ptr, nVals*sizeof(data_t)));
68 }
69 ///@}
70 
71 }
72 }
73 #endif
int HostAccessible(const void *ptr)
return true if the pointer is accessible by code running on the CPU
int CudaAccessible(const void *ptr)
return true if the pointer is accessible by code running on a CUDA GPU
void FreeHostPtr(void *ptr)
callback that can free memory managed by malloc
void FreeCudaPtr(void *ptr)
callback that can free memory managed by CUDA
SENSEI.
Definition: ADIOS2AnalysisAdaptor.h:27
void DontFreePtr(void *ptr)
A callback that does not free the pointer.