1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#pragma once
#include "libvideostitch/status.hpp"
#include <cassert>
#include <vector>
// TODO_OPENCL_IMPL
// TODO port Cuda::Malloc stuff to GPU::
// TODO handle alloc flags properly
const unsigned int HostAllocDefault = 0x0;
struct cudaArray;
struct cudaChannelFormatDesc;
namespace VideoStitch {
namespace Cuda {
/**
* Allocates GPU memory and prints a message on error.
* @param buf Forwarded to cudaMalloc.
* @param size Forwarded to cudaMalloc.
* @param name Name of the pool whose counter to increment.
* @return false on error.
*/
Status __mallocVS(void** buf, size_t size, const char* name, unsigned flagsUnused = 0, const char* file = NULL,
int line = -1);
#ifdef NDEBUG
#define mallocVS(buf, size, name) __mallocVS(buf, size, name, (unsigned)0)
#else
#define mallocVS(buf, size, name) __mallocVS(buf, size, name, (unsigned)0, __FILE__, __LINE__)
#endif
/**
* Frees GPU memory allocated with __mallocVS
* @param buf Pointer to free. Can be NULL.
*/
Status freeVS(void* buf);
/**
* Allocates Pinned CPU memory and prints a message on error.
* @param buf Forwarded to cudaMallocHost.
* @param size Forwarded to cudaMallocHost.
* @param name Name of the pool whose counter to increment.
* @return false on error.
*/
Status __mallocHostVS(void** buf, size_t size, const char* name, unsigned flags = HostAllocDefault,
const char* file = NULL, int line = -1);
#ifdef NDEBUG
#define mallocHostVS(buf, size, name) __mallocHostVS(buf, size, name)
#define mallocHostFVS(buf, size, name, flags) __mallocHostVS(buf, size, name, flags)
#else
#define mallocHostVS(buf, size, name) __mallocHostVS(buf, size, name, HostAllocDefault, __FILE__, __LINE__)
#define mallocHostFVS(buf, size, name, flags) __mallocHostVS(buf, size, name, flags, __FILE__, __LINE__)
#endif
/**
* Frees Host memory allocated with __mallocHostVS
* @param buf Pointer to free. Can be NULL.
*/
Status freeHostVS(void* buf);
/**
* Allocates a CUDA array and prints a message on error.
* @param array Forwarded to cudaMallocArray.
* @param desc Forwarded to cudaMallocArray.
* @param width Forwarded to cudaMallocArray.
* @param height Forwarded to cudaMallocArray.
* @param flags Forwarded to cudaMallocArray.
* @param name Name of the pool whose counter to increment.
* @return false on error.
*/
Status __mallocArrayVS(struct cudaArray** array, const struct cudaChannelFormatDesc* desc, size_t width, size_t height,
unsigned int flags, const char* name, const char* file = NULL, int line = -1);
#ifdef NDEBUG
#define mallocArrayVS(array, desc, width, height, flags, name) __mallocArrayVS(array, desc, width, height, flags, name)
#else
#define mallocArrayVS(array, desc, width, height, flags, name) \
__mallocArrayVS(array, desc, width, height, flags, name, __FILE__, __LINE__)
#endif
/**
* Frees array memory allocated with __mallocArrayVS
* @param buf Pointer to free. Can be NULL.
*/
Status freeArrayVS(struct cudaArray* array);
/**
* Get the number of bytes allocated in the device pool.
*/
std::size_t getDevicePoolCurrentSize(void);
/**
* Get the number of bytes allocated in the device pool, by devices.
*/
std::vector<std::size_t> getDevicePoolCurrentSizeByDevices(void);
/**
* Get the number of bytes allocated in the host pool.
*/
std::size_t getHostPoolCurrentSize(void);
/**
* Get the number of bytes allocated in the host pool, by devices.
*/
std::vector<std::size_t> getHostPoolCurrentSizeByDevices(void);
/**
* Print the device pool.
*/
void printDevicePool();
/**
* Print the host pool.
*/
void printHostPool();
/**
* Base class for UniquePtrs
*/
template <typename T, Status (*allocer)(void**, size_t, const char*, unsigned, const char*, int),
Status (*deleter)(void*)>
class BaseUniquePtr {
public:
/**
*
*/
operator bool() const { return ptr != NULL; }
/**
* Returns the pointer but keeps ownership.
*/
T* get() const { return ptr; }
/**
* Releases ownership.
*/
T* release() {
T* res = ptr;
ptr = NULL;
numElements = 0;
return res;
}
/**
* Returns the allocated size, in bytes.
*/
size_t byteSize() const { return numElements * sizeof(T); }
/**
* Returns the number of allocated elements.
*/
size_t elemSize() const { return numElements; }
/**
* Allocates a pointer.
* @param size Size (in elements of type T).
* @param name pool name.
*/
Status alloc(size_t size, const char* name, unsigned flags = 0) {
if (ptr) {
deleter(ptr);
ptr = NULL;
numElements = 0;
}
Status status = allocer((void**)&ptr, size * sizeof(T), name, flags, NULL, -1);
if (status.ok()) {
numElements = size;
}
return status;
}
protected:
/**
* Creates a NULL pointer.
*/
BaseUniquePtr() : ptr(NULL), numElements(0) {}
/**
* Takes ownership of ptr.
* @param ptr Pointer.
*/
explicit BaseUniquePtr(T* ptr) : ptr(ptr), numElements(0) {}
~BaseUniquePtr() { deleter(ptr); }
private:
T* ptr;
size_t numElements;
};
/**
* Device equivalent of std::unique_ptr.
*/
template <typename T>
class DeviceUniquePtr : public BaseUniquePtr<T, __mallocVS, freeVS> {
public:
typedef BaseUniquePtr<T, __mallocVS, freeVS> Base;
/**
* Creates a NULL pointer.
*/
DeviceUniquePtr() : Base() {}
/**
* Takes ownership of ptr.
* @param ptr Pointer.
*/
explicit DeviceUniquePtr(T* ptr) : Base(ptr) {}
};
/**
* Host equivalent of std::unique_ptr.
*/
template <typename T>
class HostUniquePtr : public BaseUniquePtr<T, __mallocHostVS, freeHostVS> {
public:
typedef BaseUniquePtr<T, __mallocHostVS, freeHostVS> Base;
/**
* Creates a NULL pointer.
*/
HostUniquePtr() : Base() {}
/**
* Takes ownership of ptr.
* @param ptr Pointer.
*/
explicit HostUniquePtr(T* ptr) : Base(ptr) {}
};
} // namespace Cuda
} // namespace VideoStitch
#if (defined SHOW_CUDA_ALLOCS) && (!defined NDEBUG)
#define cudaMalloc(p, size) ::VideoStitch::Cuda::__mallocPrint(p, size, __FILE__, __LINE__)
/**
* Forwards its arguments to cudaMalloc after printing where where an alloc has been made.
* @param p Where to allocate memory.
* @param size Number of bytes to allocate.
* @param file Filename.
* @param line Line number.
* @note Never called directly.
*/
cudaError __mallocPrint(void** p, size_t size, const char* file, const int line);
#endif