QUDA  v1.1.0
A library for QCD on GPUs
malloc_quda.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdlib>
4 #include <cstdint>
5 #include <enum_quda.h>
6 
7 namespace quda {
8 
9  void printPeakMemUsage();
10  void assertAllMemFree();
11 
15  size_t device_allocated();
16 
20  size_t pinned_allocated();
21 
25  size_t mapped_allocated();
26 
30  size_t host_allocated();
31 
35  size_t device_allocated_peak();
36 
40  size_t pinned_allocated_peak();
41 
45  size_t mapped_allocated_peak();
46 
50  size_t host_allocated_peak();
51 
55  bool use_managed_memory();
56 
60  bool is_prefetch_enabled();
61 
62  /*
63  * The following functions should not be called directly. Use the
64  * macros below instead.
65  */
66  void *device_malloc_(const char *func, const char *file, int line, size_t size);
67  void *device_pinned_malloc_(const char *func, const char *file, int line, size_t size);
68  void *device_comms_pinned_malloc_(const char *func, const char *file, int line, size_t size);
69  void *safe_malloc_(const char *func, const char *file, int line, size_t size);
70  void *pinned_malloc_(const char *func, const char *file, int line, size_t size);
71  void *mapped_malloc_(const char *func, const char *file, int line, size_t size);
72  void *managed_malloc_(const char *func, const char *file, int line, size_t size);
73  void device_free_(const char *func, const char *file, int line, void *ptr);
74  void device_pinned_free_(const char *func, const char *file, int line, void *ptr);
75  void device_comms_pinned_free_(const char *func, const char *file, int line, void *ptr);
76  void managed_free_(const char *func, const char *file, int line, void *ptr);
77  void host_free_(const char *func, const char *file, int line, void *ptr);
78 
79  // strip path from __FILE__
80  inline constexpr const char* str_end(const char *str) { return *str ? str_end(str + 1) : str; }
81  inline constexpr bool str_slant(const char *str) { return *str == '/' ? true : (*str ? str_slant(str + 1) : false); }
82  inline constexpr const char* r_slant(const char* str) { return *str == '/' ? (str + 1) : r_slant(str - 1); }
83  inline constexpr const char* file_name(const char* str) { return str_slant(str) ? r_slant(str_end(str)) : str; }
84 
85  QudaFieldLocation get_pointer_location(const void *ptr);
86 
87  /*
88  @brief Get device view of a host-mapped pointer
89  */
90  void *get_mapped_device_pointer_(const char *func, const char *file, int line, const void *ptr);
91 
95  inline bool is_aligned(const void *ptr, size_t alignment)
96  {
97  return (reinterpret_cast<std::uintptr_t>(ptr) & (alignment - 1)) == 0;
98  }
99 
100 } // namespace quda
101 
102 #define device_malloc(size) quda::device_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
103 #define device_pinned_malloc(size) quda::device_pinned_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
104 #define device_comms_pinned_malloc(size) \
105  quda::device_comms_pinned_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
106 #define safe_malloc(size) quda::safe_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
107 #define pinned_malloc(size) quda::pinned_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
108 #define mapped_malloc(size) quda::mapped_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
109 #define managed_malloc(size) quda::managed_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
110 #define device_free(ptr) quda::device_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
111 #define device_pinned_free(ptr) quda::device_pinned_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
112 #define device_comms_pinned_free(ptr) \
113  quda::device_comms_pinned_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
114 #define managed_free(ptr) quda::managed_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
115 #define host_free(ptr) quda::host_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
116 #define get_mapped_device_pointer(ptr) \
117  quda::get_mapped_device_pointer_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
118 
119 namespace quda {
120 
121  namespace pool {
122 
126  void init();
127 
134  void *device_malloc_(const char *func, const char *file, int line, size_t size);
135 
140  void device_free_(const char *func, const char *file, int line, void *ptr);
141 
148  void *pinned_malloc_(const char *func, const char *file, int line, size_t size);
149 
154  void pinned_free_(const char *func, const char *file, int line, void *ptr);
155 
159  void flush_device();
160 
164  void flush_pinned();
165 
166  } // namespace pool
167 
168 }
169 
170 #define pool_device_malloc(size) quda::pool::device_malloc_(__func__, __FILE__, __LINE__, size)
171 #define pool_device_free(ptr) quda::pool::device_free_(__func__, __FILE__, __LINE__, ptr)
172 #define pool_pinned_malloc(size) quda::pool::pinned_malloc_(__func__, __FILE__, __LINE__, size)
173 #define pool_pinned_free(ptr) quda::pool::pinned_free_(__func__, __FILE__, __LINE__, ptr)
enum QudaFieldLocation_s QudaFieldLocation
void init()
Initialize the memory pool allocator.
Definition: malloc.cpp:632
void * pinned_malloc_(const char *func, const char *file, int line, size_t size)
Allocate pinned-memory. If a free pre-existing allocation exists reuse this.
Definition: malloc.cpp:665
void flush_pinned()
Free all outstanding pinned-memory allocations.
Definition: malloc.cpp:753
void flush_device()
Free all outstanding device-memory allocations.
Definition: malloc.cpp:761
void pinned_free_(const char *func, const char *file, int line, void *ptr)
Virtual free of pinned-memory allocation.
Definition: malloc.cpp:692
void device_free_(const char *func, const char *file, int line, void *ptr)
Virtual free of pinned-memory allocation.
Definition: malloc.cpp:730
void * device_malloc_(const char *func, const char *file, int line, size_t size)
Allocate device-memory. If free pre-existing allocation exists reuse this.
Definition: malloc.cpp:703
void device_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:415
void host_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:477
void * device_pinned_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:255
void * mapped_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:324
constexpr const char * str_end(const char *str)
Definition: malloc_quda.h:80
size_t host_allocated()
Definition: malloc.cpp:77
size_t mapped_allocated()
Definition: malloc.cpp:73
void * pinned_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:303
size_t device_allocated()
Definition: malloc.cpp:69
void printPeakMemUsage()
Definition: malloc.cpp:539
bool is_aligned(const void *ptr, size_t alignment)
Definition: malloc_quda.h:95
constexpr const char * file_name(const char *str)
Definition: malloc_quda.h:83
size_t host_allocated_peak()
Definition: malloc.cpp:87
void * get_mapped_device_pointer_(const char *func, const char *file, int line, const void *ptr)
Definition: malloc.cpp:590
void device_pinned_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:440
constexpr const char * r_slant(const char *str)
Definition: malloc_quda.h:82
void managed_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:461
constexpr bool str_slant(const char *str)
Definition: malloc_quda.h:81
size_t pinned_allocated()
Definition: malloc.cpp:71
void device_comms_pinned_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:530
size_t mapped_allocated_peak()
Definition: malloc.cpp:83
void assertAllMemFree()
Definition: malloc.cpp:549
void * device_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:223
void * safe_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:280
bool use_managed_memory()
Definition: malloc.cpp:178
size_t pinned_allocated_peak()
Definition: malloc.cpp:81
size_t device_allocated_peak()
Definition: malloc.cpp:79
bool is_prefetch_enabled()
Definition: malloc.cpp:198
QudaFieldLocation get_pointer_location(const void *ptr)
Definition: malloc.cpp:566
void * managed_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:356
void * device_comms_pinned_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:401