Proto  3.2
Proto_Memory.H
Go to the documentation of this file.
1 #pragma once
2 #ifndef _PROTO_MEMORY_
3 #define _PROTO_MEMORY_
4 
5 #include <cstring>
6 #include <limits>
7 #include <sys/resource.h>
8 
9 #include "Proto_MemType.H"
10 
11 #define PR_UNALLOCATED_FLOAT 7.77e77
12 
13 //#define RUSAGE_SELF 0
14 //#define RUSAGE_CHILDREN -1
15 
16 namespace Proto
17 {
18 
19 // The purpose of these functions is to improve the existing macro-driven
20 // API for memory access in Proto. Existing macros have not been removed,
21 // but will be phased out in favor of templated functions as development
22 // progresses.
23 
24 // TODO: TECHNICALLY the MemType template parameters might be redundant.
25 // Both HIP and CUDA have an API for checking of a pointer is a device
26 // pointer or not, but based on my limited testing, I'm not sure how
27 // much faith I have in said tools.
28 
29 /// Copy Memory
30 /**
31  Executes a low level memory copy between buffers of the specified MemTypes.
32  If both SRC_MEM and DST_MEM are HOST, this is equivalent to <code>std::memcpy</code>.
33  When at least one MemType is DEVICE, this function will call
34  <code>cudaMemcpy</code> or equivalent functions as appropriate.
35 
36  \tparam SRC_MEM MemType of the source buffer
37  \tparam DST_MEM MemType of the destination buffer
38  \param a_src Source buffer
39  \param a_dst Destination buffer
40  \param a_nbytes Number of bytes to copy
41 */
42 template<MemType SRC_MEM=MEMTYPE_DEFAULT, MemType DST_MEM=MEMTYPE_DEFAULT>
43 inline void proto_memcpy(const void* a_src, void* a_dst, size_t a_nbytes);
44 
45 /// Allocate Memory
46 /**
47  Returns a pointer to a region of allocated memory. If <code>MEM==HOST</code>
48  this function is equivalent to <code>std::malloc</code>.
49  If <code>MEM==DEVICE</code>, this function will call <code>cudaMalloc</code>
50  or other equivalent functions as appropriate.
51 
52  \tparam MEM MemType of the desired buffer
53  \param a_nbytes Number of bytes to allocate
54 */
55 template<MemType MEM=MEMTYPE_DEFAULT>
56 inline void* proto_malloc(size_t a_nbytes);
57 
58 /// Free Memory
59 /**
60  Frees a pointer previously allocated using <code>proto_malloc</code>.
61  If <code>MEM==HOST</code> this function is equivalent to <code>std::free</code>.
62  If <code>MEM==DEVICE</code>, this function will call <code>cudaFree</code>
63  or other equivalent functions as appropriate.
64 
65  \tparam MEM MemType of the buffer.
66  \param a_buffer A buffer.
67 */
68 template<MemType MEM=MEMTYPE_DEFAULT>
69 inline void proto_free(void* a_buffer);
70 
71 /// Query Pointer MemType
72 /**
73  This is an EXPERIMENTAL tool for checking the MemType of an arbitrary pointer.
74  Initial testing suggests that this function can distinguish between HOST and
75  DEVICE for cuda pointers. Untested with HIP. Use with healthy skepticism.
76 
77  \param a_ptr A pointer.
78 */
79 inline MemType pointerMemType(const void* a_ptr);
80 
81 template<typename T>
82 inline constexpr T proto_unallocated()
83 {
84  if constexpr (std::is_floating_point_v<T>)
85  {
86  T val = (T)PR_UNALLOCATED_FLOAT;
87  return val;
88  } else {
89  return std::numeric_limits<T>::max()-1;
90  }
91 }
92 
93 }
94 #endif
MemType pointerMemType(const void *a_ptr)
Query Pointer MemType.
MemType
Definition: Proto_MemType.H:7
void proto_memcpy(const void *a_src, void *a_dst, size_t a_nbytes)
Copy Memory.
#define PR_UNALLOCATED_FLOAT
Definition: Proto_Memory.H:11
void * proto_malloc(size_t a_nbytes)
Allocate Memory.
constexpr T proto_unallocated()
Definition: Proto_Memory.H:82
Definition: Proto_Array.H:17
void proto_free(void *a_buffer)
Free Memory.