-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathDefaultMemoryManager.cpp
More file actions
387 lines (330 loc) · 13.7 KB
/
DefaultMemoryManager.cpp
File metadata and controls
387 lines (330 loc) · 13.7 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*******************************************************
* Copyright (c) 2018, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <common/DefaultMemoryManager.hpp>
#include <common/Logger.hpp>
#include <common/dispatch.hpp>
#include <common/err_common.hpp>
#include <common/util.hpp>
#include <memoryapi.hpp>
#include <af/event.h>
#include <af/memory.h>
#include <algorithm>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
using std::max;
using std::move;
using std::stoi;
using std::string;
using std::vector;
namespace arrayfire {
namespace common {
DefaultMemoryManager::memory_info &
DefaultMemoryManager::getCurrentMemoryInfo() {
return memory[this->getActiveDeviceId()];
}
void DefaultMemoryManager::cleanDeviceMemoryManager(int device) {
if (this->debug_mode) { return; }
// This vector is used to store the pointers which will be deleted by
// the memory manager. We are using this to avoid calling free while
// the lock is being held because the CPU backend calls sync.
vector<void *> free_ptrs;
size_t bytes_freed = 0;
DefaultMemoryManager::memory_info ¤t = memory[device];
{
lock_guard_t lock(this->memory_mutex);
// Return if all buffers are locked
if (current.total_buffers == current.lock_buffers) { return; }
free_ptrs.reserve(current.free_map.size());
for (auto &kv : current.free_map) {
size_t num_ptrs = kv.second.size();
// Free memory by pushing the last element into the free_ptrs
// vector which will be freed once outside of the lock
// for (auto ptr : kv.second) { free_ptrs.emplace_back(pair); }
std::move(begin(kv.second), end(kv.second),
back_inserter(free_ptrs));
current.total_bytes -= num_ptrs * kv.first;
bytes_freed += num_ptrs * kv.first;
current.total_buffers -= num_ptrs;
}
current.free_map.clear();
}
AF_TRACE("GC: Clearing {} buffers {}", free_ptrs.size(),
bytesToString(bytes_freed));
// Free memory outside of the lock
for (auto *ptr : free_ptrs) { this->nativeFree(ptr); }
}
DefaultMemoryManager::DefaultMemoryManager(int num_devices,
unsigned max_buffers, bool debug)
: mem_step_size(1024)
, max_buffers(max_buffers)
, debug_mode(debug)
, memory(num_devices) {
// Check for environment variables
// Debug mode
string env_var = getEnvVar("AF_MEM_DEBUG");
if (!env_var.empty()) { this->debug_mode = env_var[0] != '0'; }
if (this->debug_mode) { mem_step_size = 1; }
// Max Buffer count
env_var = getEnvVar("AF_MAX_BUFFERS");
if (!env_var.empty()) { this->max_buffers = max(1, stoi(env_var)); }
}
void DefaultMemoryManager::initialize() { this->setMaxMemorySize(); }
void DefaultMemoryManager::shutdown() { signalMemoryCleanup(); }
void DefaultMemoryManager::addMemoryManagement(int device) {
// If there is a memory manager allocated for this device id, we might
// as well use it and the buffers allocated for it
if (static_cast<size_t>(device) < memory.size()) { return; }
// Assuming, device need not be always the next device Lets resize to
// current_size + device + 1 +1 is to account for device being 0-based
// index of devices
memory.resize(memory.size() + device + 1);
}
void DefaultMemoryManager::removeMemoryManagement(int device) {
if (static_cast<size_t>(device) >= memory.size()) {
AF_ERROR("No matching device found", AF_ERR_ARG);
}
// Do garbage collection for the device and leave the memory::memory_info
// struct from the memory vector intact
cleanDeviceMemoryManager(device);
}
void DefaultMemoryManager::setMaxMemorySize() {
for (unsigned n = 0; n < memory.size(); n++) {
// Calls garbage collection when: total_bytes > memsize * 0.75 when
// memsize < 4GB total_bytes > memsize - 1 GB when memsize >= 4GB If
// memsize returned 0, then use 1GB
size_t memsize = this->getMaxMemorySize(static_cast<int>(n));
memory[n].max_bytes =
memsize == 0
? ONE_GB
: max(memsize * 0.75, static_cast<double>(memsize - ONE_GB));
AF_TRACE("memory[{}].max_bytes: {}", n,
bytesToString(memory[n].max_bytes));
}
}
float DefaultMemoryManager::getMemoryPressure() {
lock_guard_t lock(this->memory_mutex);
memory_info ¤t = this->getCurrentMemoryInfo();
if (current.lock_bytes > current.max_bytes ||
current.lock_buffers > max_buffers) {
return 1.0;
} else {
return 0.0;
}
}
bool DefaultMemoryManager::jitTreeExceedsMemoryPressure(
size_t jit_tree_buffer_bytes) {
lock_guard_t lock(this->memory_mutex);
memory_info ¤t = this->getCurrentMemoryInfo();
if (current.lock_bytes > 0.25f * current.max_bytes) {
/// Evaluate JIT if half of all locked buffers are locked by this JIT
/// tree
return jit_tree_buffer_bytes > current.lock_bytes * 0.5f;
} else {
/// Evaluate if this JIT Tree accounts for 10% of total memory on the
/// device
return jit_tree_buffer_bytes > 0.10f * current.max_bytes;
}
}
void *DefaultMemoryManager::alloc(bool user_lock, const unsigned ndims,
dim_t *dims, const unsigned element_size) {
size_t bytes = element_size;
for (unsigned i = 0; i < ndims; ++i) { bytes *= dims[i]; }
void *ptr = nullptr;
size_t alloc_bytes = this->debug_mode
? bytes
: (divup(bytes, mem_step_size) * mem_step_size);
if (bytes > 0) {
memory_info ¤t = this->getCurrentMemoryInfo();
locked_info info = {!user_lock, user_lock, alloc_bytes};
// There is no memory cache in debug mode
if (!this->debug_mode) {
// FIXME: Add better checks for garbage collection
// Perhaps look at total memory available as a metric
if (current.lock_bytes >= current.max_bytes ||
current.total_buffers >= this->max_buffers) {
AF_TRACE(
"Running GC: current.lock_bytes({}) >= "
"current.max_bytes({}) || current.total_buffers({}) >= "
"this->max_buffers({})\n",
current.lock_bytes, current.max_bytes,
current.total_buffers, this->max_buffers);
this->signalMemoryCleanup();
}
lock_guard_t lock(this->memory_mutex);
auto free_buffer_iter = current.free_map.find(alloc_bytes);
if (free_buffer_iter != current.free_map.end() &&
!free_buffer_iter->second.empty()) {
// Delete existing buffer info and underlying event
// Set to existing in from free map
vector<void *> &free_buffer_vector = free_buffer_iter->second;
ptr = free_buffer_vector.back();
free_buffer_vector.pop_back();
current.locked_map[ptr] = info;
current.lock_bytes += alloc_bytes;
current.lock_buffers++;
}
}
// Only comes here if buffer size not found or in debug mode
if (ptr == nullptr) {
// Perform garbage collection if memory can not be allocated
try {
ptr = this->nativeAlloc(alloc_bytes);
} catch (const AfError &ex) {
// If out of memory, run garbage collect and try again
if (ex.getError() != AF_ERR_NO_MEM) { throw; }
this->signalMemoryCleanup();
ptr = this->nativeAlloc(alloc_bytes);
}
lock_guard_t lock(this->memory_mutex);
// Increment these two only when it succeeds to come here.
current.total_bytes += alloc_bytes;
current.total_buffers += 1;
current.locked_map[ptr] = info;
current.lock_bytes += alloc_bytes;
current.lock_buffers++;
}
}
return ptr;
}
size_t DefaultMemoryManager::allocated(void *ptr) {
if (!ptr) { return 0; }
memory_info ¤t = this->getCurrentMemoryInfo();
auto locked_iter = current.locked_map.find(ptr);
if (locked_iter == current.locked_map.end()) { return 0; }
return (locked_iter->second).bytes;
}
void DefaultMemoryManager::unlock(void *ptr, bool user_unlock) {
// Shortcut for empty arrays
if (!ptr) { return; }
// Frees the pointer outside the lock.
uptr_t freed_ptr(nullptr, [this](void *p) { this->nativeFree(p); });
{
lock_guard_t lock(this->memory_mutex);
memory_info ¤t = this->getCurrentMemoryInfo();
auto locked_buffer_iter = current.locked_map.find(ptr);
if (locked_buffer_iter == current.locked_map.end()) {
// Pointer not found in locked map
// Probably came from user, just free it
freed_ptr.reset(ptr);
return;
}
locked_info &locked_buffer_info = locked_buffer_iter->second;
void *locked_buffer_ptr = locked_buffer_iter->first;
if (user_unlock) {
locked_buffer_info.user_lock = false;
} else {
locked_buffer_info.manager_lock = false;
}
// Return early if either one is locked
if (locked_buffer_info.user_lock || locked_buffer_info.manager_lock) {
return;
}
size_t bytes = locked_buffer_info.bytes;
current.lock_bytes -= locked_buffer_info.bytes;
current.lock_buffers--;
if (this->debug_mode) {
// Just free memory in debug mode
if (locked_buffer_info.bytes > 0) {
freed_ptr.reset(locked_buffer_ptr);
current.total_buffers--;
current.total_bytes -= locked_buffer_info.bytes;
}
} else {
current.free_map[bytes].emplace_back(ptr);
}
current.locked_map.erase(locked_buffer_iter);
}
}
void DefaultMemoryManager::signalMemoryCleanup() {
cleanDeviceMemoryManager(this->getActiveDeviceId());
}
void DefaultMemoryManager::printInfo(const char *msg, const int device) {
UNUSED(device);
const memory_info ¤t = this->getCurrentMemoryInfo();
printf("%s\n", msg);
printf(
"---------------------------------------------------------\n"
"| POINTER | SIZE | AF LOCK | USER LOCK |\n"
"---------------------------------------------------------\n");
lock_guard_t lock(this->memory_mutex);
for (const auto &kv : current.locked_map) {
const char *status_mngr = "Yes";
const char *status_user = "Unknown";
if (kv.second.user_lock) {
status_user = "Yes";
} else {
status_user = " No";
}
const char *unit = "KB";
double size = static_cast<double>(kv.second.bytes) / 1024;
if (size >= 1024) {
size = size / 1024;
unit = "MB";
}
printf("| %14p | %6.f %s | %9s | %9s |\n", kv.first, size, unit,
status_mngr, status_user);
}
for (const auto &kv : current.free_map) {
const char *status_mngr = "No";
const char *status_user = "No";
const char *unit = "KB";
double size = static_cast<double>(kv.first) / 1024;
if (size >= 1024) {
size = size / 1024;
unit = "MB";
}
for (const auto &ptr : kv.second) {
printf("| %14p | %6.f %s | %9s | %9s |\n", ptr, size, unit,
status_mngr, status_user);
}
}
printf("---------------------------------------------------------\n");
}
void DefaultMemoryManager::usageInfo(size_t *alloc_bytes, size_t *alloc_buffers,
size_t *lock_bytes, size_t *lock_buffers) {
const memory_info ¤t = this->getCurrentMemoryInfo();
lock_guard_t lock(this->memory_mutex);
if (alloc_bytes) { *alloc_bytes = current.total_bytes; }
if (alloc_buffers) { *alloc_buffers = current.total_buffers; }
if (lock_bytes) { *lock_bytes = current.lock_bytes; }
if (lock_buffers) { *lock_buffers = current.lock_buffers; }
}
void DefaultMemoryManager::userLock(const void *ptr) {
memory_info ¤t = this->getCurrentMemoryInfo();
lock_guard_t lock(this->memory_mutex);
auto locked_iter = current.locked_map.find(const_cast<void *>(ptr));
if (locked_iter != current.locked_map.end()) {
locked_iter->second.user_lock = true;
} else {
locked_info info = {false, true, 100}; // This number is not relevant
current.locked_map[const_cast<void *>(ptr)] = info;
}
}
void DefaultMemoryManager::userUnlock(const void *ptr) {
this->unlock(const_cast<void *>(ptr), true);
}
bool DefaultMemoryManager::isUserLocked(const void *ptr) {
memory_info ¤t = this->getCurrentMemoryInfo();
lock_guard_t lock(this->memory_mutex);
auto locked_iter = current.locked_map.find(const_cast<void *>(ptr));
if (locked_iter == current.locked_map.end()) { return false; }
return locked_iter->second.user_lock;
}
size_t DefaultMemoryManager::getMemStepSize() {
lock_guard_t lock(this->memory_mutex);
return this->mem_step_size;
}
void DefaultMemoryManager::setMemStepSize(size_t new_step_size) {
lock_guard_t lock(this->memory_mutex);
this->mem_step_size = new_step_size;
}
} // namespace common
} // namespace arrayfire