QUDA  0.9.0
malloc_quda.h
Go to the documentation of this file.
1 #ifndef _MALLOC_QUDA_H
2 #define _MALLOC_QUDA_H
3 
4 #include <cstdlib>
5 
6 namespace quda {
7 
8  void printPeakMemUsage();
9  void assertAllMemFree();
10 
14  long device_allocated_peak();
15 
19  long pinned_allocated_peak();
20 
24  long mapped_allocated_peak();
25 
29  long host_allocated_peak();
30 
31  /*
32  * The following functions should not be called directly. Use the
33  * macros below instead.
34  */
35  void *device_malloc_(const char *func, const char *file, int line, size_t size);
36  void *device_pinned_malloc_(const char *func, const char *file, int line, size_t size);
37  void *safe_malloc_(const char *func, const char *file, int line, size_t size);
38  void *pinned_malloc_(const char *func, const char *file, int line, size_t size);
39  void *mapped_malloc_(const char *func, const char *file, int line, size_t size);
40  void device_free_(const char *func, const char *file, int line, void *ptr);
41  void device_pinned_free_(const char *func, const char *file, int line, void *ptr);
42  void host_free_(const char *func, const char *file, int line, void *ptr);
43 
44  // strip path from __FILE__
45  inline constexpr const char* str_end(const char *str) { return *str ? str_end(str + 1) : str; }
46  inline constexpr bool str_slant(const char *str) { return *str == '/' ? true : (*str ? str_slant(str + 1) : false); }
47  inline constexpr const char* r_slant(const char* str) { return *str == '/' ? (str + 1) : r_slant(str - 1); }
48  inline constexpr const char* file_name(const char* str) { return str_slant(str) ? r_slant(str_end(str)) : str; }
49 
50 } // namespace quda
51 
52 #define device_malloc(size) quda::device_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
53 #define device_pinned_malloc(size) quda::device_pinned_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
54 #define safe_malloc(size) quda::safe_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
55 #define pinned_malloc(size) quda::pinned_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
56 #define mapped_malloc(size) quda::mapped_malloc_(__func__, quda::file_name(__FILE__), __LINE__, size)
57 #define device_free(ptr) quda::device_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
58 #define device_pinned_free(ptr) quda::device_pinned_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
59 #define host_free(ptr) quda::host_free_(__func__, quda::file_name(__FILE__), __LINE__, ptr)
60 
61 
62 namespace quda {
63 
64  namespace pool {
65 
69  void init();
70 
77  void *device_malloc_(const char *func, const char *file, int line, size_t size);
78 
83  void device_free_(const char *func, const char *file, int line, void *ptr);
84 
91  void *pinned_malloc_(const char *func, const char *file, int line, size_t size);
92 
97  void pinned_free_(const char *func, const char *file, int line, void *ptr);
98 
102  void flush_device();
103 
107  void flush_pinned();
108 
109  } // namespace pool
110 
111 }
112 
113 #define pool_device_malloc(size) quda::pool::device_malloc_(__func__, __FILE__, __LINE__, size)
114 #define pool_device_free(ptr) quda::pool::device_free_(__func__, __FILE__, __LINE__, ptr)
115 #define pool_pinned_malloc(size) quda::pool::pinned_malloc_(__func__, __FILE__, __LINE__, size)
116 #define pool_pinned_free(ptr) quda::pool::pinned_free_(__func__, __FILE__, __LINE__, ptr)
117 
118 
119 #endif // _MALLOC_QUDA_H
long device_allocated_peak()
Definition: malloc.cpp:57
void * safe_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:219
const void * func
void * device_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:167
constexpr const char * r_slant(const char *str)
Definition: malloc_quda.h:47
void assertAllMemFree()
Definition: malloc.cpp:379
void * device_pinned_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:194
void device_free_(const char *func, const char *file, int line, void *ptr)
Virtual free of pinned-memory allocation.
Definition: malloc.cpp:520
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:449
void device_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:292
constexpr const char * str_end(const char *str)
Definition: malloc_quda.h:45
void flush_pinned()
Free all outstanding pinned-memory allocations.
Definition: malloc.cpp:533
void device_pinned_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:316
void init()
Initialize the memory pool allocator.
Definition: malloc.cpp:424
void host_free_(const char *func, const char *file, int line, void *ptr)
Definition: malloc.cpp:340
void flush_device()
Free all outstanding device-memory allocations.
Definition: malloc.cpp:545
long host_allocated_peak()
Definition: malloc.cpp:63
long mapped_allocated_peak()
Definition: malloc.cpp:61
const void * ptr
void * mapped_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:269
constexpr bool str_slant(const char *str)
Definition: malloc_quda.h:46
long pinned_allocated_peak()
Definition: malloc.cpp:59
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:491
void printPeakMemUsage()
Definition: malloc.cpp:371
constexpr const char * file_name(const char *str)
Definition: malloc_quda.h:48
void pinned_free_(const char *func, const char *file, int line, void *ptr)
Virtual free of pinned-memory allocation.
Definition: malloc.cpp:478
void * pinned_malloc_(const char *func, const char *file, int line, size_t size)
Definition: malloc.cpp:246