-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathUmpire.cpp
More file actions
443 lines (362 loc) · 12.6 KB
/
Copy pathUmpire.cpp
File metadata and controls
443 lines (362 loc) · 12.6 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016-26, Lawrence Livermore National Security, LLC and Umpire
// project contributors. See the COPYRIGHT file for details.
//
// SPDX-License-Identifier: (MIT)
//////////////////////////////////////////////////////////////////////////////
#include "umpire/Umpire.hpp"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <sstream>
#include <string>
#include "umpire/ResourceManager.hpp"
#include "umpire/config.hpp"
#if defined(UMPIRE_ENABLE_MPI3_SHARED_MEMORY)
#include "umpire/resource/HostMpi3SharedMemoryResource.hpp"
#endif
#include "umpire/resource/HostSharedMemoryResource.hpp"
#include "umpire/resource/MemoryResource.hpp"
#if defined(UMPIRE_ENABLE_MPI) && defined(UMPIRE_ENABLE_IPC_SHARED_MEMORY)
#if defined(UMPIRE_ENABLE_DEVICE)
#include "umpire/strategy/DeviceIpcAllocator.hpp"
#endif
#endif
#include "umpire/strategy/DynamicPoolList.hpp"
#include "umpire/strategy/QuickPool.hpp"
#include "umpire/strategy/ResourceAwarePool.hpp"
#include "umpire/util/wrap_allocator.hpp"
#if !defined(_MSC_VER)
#include <unistd.h>
#endif
#include <fstream>
#include <sstream>
UMPIRE_EXPORT int UMPIRE_VERSION_SYM{0};
namespace umpire {
void print_allocator_records(Allocator allocator, std::ostream& os)
{
std::stringstream ss;
auto& rm = umpire::ResourceManager::getInstance();
auto strategy = allocator.getAllocationStrategy();
rm.m_allocations.print([strategy](const util::AllocationRecord& rec) { return rec.strategy == strategy; }, ss);
if (!ss.str().empty()) {
os << "Allocations for " << allocator.getName() << " allocator:" << std::endl << ss.str() << std::endl;
}
}
std::vector<util::AllocationRecord> get_allocator_records(Allocator allocator)
{
auto& rm = umpire::ResourceManager::getInstance();
auto strategy = allocator.getAllocationStrategy();
std::vector<util::AllocationRecord> recs;
std::copy_if(rm.m_allocations.begin(), rm.m_allocations.end(), std::back_inserter(recs),
[strategy](const util::AllocationRecord& rec) { return rec.strategy == strategy; });
return recs;
}
bool pointer_overlaps(void* left_ptr, void* right_ptr)
{
auto& rm = umpire::ResourceManager::getInstance();
try {
auto left_record = rm.findAllocationRecord(left_ptr);
auto right_record = rm.findAllocationRecord(right_ptr);
char* left{reinterpret_cast<char*>(left_record->ptr)};
char* right{reinterpret_cast<char*>(right_record->ptr)};
return ((right >= left) && ((left + left_record->size) > right) &&
((right + right_record->size) > (left + left_record->size)));
} catch (umpire::runtime_error&) {
UMPIRE_LOG(Error, "Unknown pointer in pointer_overlaps");
throw;
}
}
bool pointer_contains(void* left_ptr, void* right_ptr)
{
auto& rm = umpire::ResourceManager::getInstance();
try {
auto left_record = rm.findAllocationRecord(left_ptr);
auto right_record = rm.findAllocationRecord(right_ptr);
char* left{reinterpret_cast<char*>(left_record->ptr)};
char* right{reinterpret_cast<char*>(right_record->ptr)};
return ((right >= left) && (left + left_record->size > right) &&
(right + right_record->size <= left + left_record->size));
} catch (umpire::runtime_error&) {
UMPIRE_LOG(Error, "Unknown pointer in pointer_contains");
throw;
}
}
bool is_accessible(Platform p, Allocator a)
{
// get base (parent) resource
umpire::strategy::AllocationStrategy* root = a.getAllocationStrategy();
while ((root->getParent() != nullptr)) {
root = root->getParent();
}
// unwrap the base MemoryResource and return whether or not it's accessible
umpire::resource::MemoryResource* resource = util::unwrap_allocation_strategy<umpire::resource::MemoryResource>(root);
return resource->isAccessibleFrom(p);
}
std::string get_backtrace(void* ptr)
{
#if defined(UMPIRE_ENABLE_BACKTRACE)
auto& rm = umpire::ResourceManager::getInstance();
auto record = rm.findAllocationRecord(ptr);
return umpire::util::backtracer<>::print(record->allocation_backtrace);
#else
UMPIRE_USE_VAR(ptr);
return "[Umpire: UMPIRE_BACKTRACE=Off]";
#endif
}
std::size_t get_process_memory_usage_hwm()
{
#if defined(_MSC_VER) || defined(__APPLE__)
return 0;
#else
std::ifstream status{"/proc/self/status"};
std::size_t rval{0};
std::string line;
while (std::getline(status, line)) {
std::stringstream ss{line};
std::string key;
ss >> key;
if (key == "VmHWM:") {
std::size_t resident_hwm;
ss >> resident_hwm;
//
// "VmHWM" returns the number of kB in use. Convert this to number of bytes
//
rval = std::size_t{resident_hwm * 1024};
break;
}
}
return rval;
#endif
}
std::size_t get_process_memory_usage()
{
#if defined(_MSC_VER) || defined(__APPLE__)
return 0;
#else
std::size_t ignore;
std::size_t resident;
std::ifstream statm("/proc/self/statm");
statm >> ignore >> resident >> ignore;
statm.close();
long page_size{::sysconf(_SC_PAGE_SIZE)};
return std::size_t{resident * page_size};
#endif
}
std::size_t get_internal_memory_usage()
{
return umpire::ResourceManager::getInstance().getInternalMemoryUsage();
}
void mark_event(const std::string& event)
{
umpire::event::record(
[&](auto& e) { e.name("event").category(event::category::metadata).arg("name", event).tag("replay", "true"); });
}
std::size_t get_total_bytes_allocated()
{
auto& rm = umpire::ResourceManager::getInstance();
std::size_t total_memory{0};
for (auto s : rm.getResourceNames()) {
umpire::Allocator alloc = rm.getAllocator(s);
total_memory += alloc.getActualSize();
}
for (auto s : rm.getSharedAllocatorNames()) {
umpire::Allocator alloc = rm.getAllocator(s);
total_memory += alloc.getActualSize();
}
return total_memory;
}
std::size_t get_device_memory_usage(int device_id)
{
#if defined(UMPIRE_ENABLE_CUDA)
std::size_t mem_free{0};
std::size_t mem_tot{0};
int current_device;
cudaError_t err = cudaGetDevice(¤t_device);
if (err != cudaSuccess) {
UMPIRE_ERROR(umpire::runtime_error, fmt::format("cudaGetDevice failed with error: {}", cudaGetErrorString(err)));
}
err = cudaSetDevice(device_id);
if (err != cudaSuccess) {
UMPIRE_ERROR(umpire::runtime_error,
fmt::format("Error when trying to set CUDA Device: {}", cudaGetErrorString(err)));
}
err = cudaMemGetInfo(&mem_free, &mem_tot);
if (err != cudaSuccess) {
UMPIRE_ERROR(umpire::runtime_error, fmt::format("cudaMemGetInfo failed with error: {}", cudaGetErrorString(err)));
}
err = cudaSetDevice(current_device);
if (err != cudaSuccess) {
UMPIRE_ERROR(umpire::runtime_error,
fmt::format("Error when trying to set CUDA Device: {}", cudaGetErrorString(err)));
}
return std::size_t{mem_tot - mem_free};
#elif defined(UMPIRE_ENABLE_HIP)
std::size_t mem_free{0};
std::size_t mem_tot{0};
int current_device;
hipError_t err = hipGetDevice(¤t_device);
if (err != hipSuccess) {
UMPIRE_ERROR(umpire::runtime_error, fmt::format("hipGetDevice failed with error: {}", hipGetErrorString(err)));
}
err = hipSetDevice(device_id);
if (err != hipSuccess) {
UMPIRE_ERROR(umpire::runtime_error, fmt::format("Error when trying to set HIP Device: {}", hipGetErrorString(err)));
}
err = hipMemGetInfo(&mem_free, &mem_tot);
if (err != hipSuccess) {
UMPIRE_ERROR(umpire::runtime_error, fmt::format("hipMemGetInfo failed with error: {}", hipGetErrorString(err)));
}
err = hipSetDevice(current_device);
if (err != hipSuccess) {
UMPIRE_ERROR(umpire::runtime_error, fmt::format("Error when trying to set HIP Device: {}", hipGetErrorString(err)));
}
return std::size_t{mem_tot - mem_free};
#endif
UMPIRE_USE_VAR(device_id);
return 0;
}
std::vector<util::AllocationRecord> get_leaked_allocations(Allocator allocator)
{
return get_allocator_records(allocator);
}
umpire::MemoryResourceTraits get_default_resource_traits(const std::string& name)
{
umpire::resource::MemoryResourceRegistry& registry{umpire::resource::MemoryResourceRegistry::getInstance()};
umpire::MemoryResourceTraits traits(registry.getDefaultTraitsForResource(name));
return traits;
}
void* find_pointer_from_name(Allocator allocator, const std::string& name)
{
void* ptr{nullptr};
#if defined(UMPIRE_ENABLE_IPC_SHARED_MEMORY)
auto base_strategy = util::unwrap_allocator<strategy::AllocationStrategy>(allocator);
umpire::resource::HostSharedMemoryResource* shared_resource =
reinterpret_cast<umpire::resource::HostSharedMemoryResource*>(base_strategy);
if (shared_resource != nullptr) {
ptr = shared_resource->find_pointer_from_name(name);
} else
#else
UMPIRE_USE_VAR(name);
// Using unused attribute to silence warning from rocm compiler
UMPIRE_USE_VAR(allocator);
#endif // defined(UMPIRE_ENABLE_IPC_SHARED_MEMORY)
{
if (ptr == nullptr) {
UMPIRE_ERROR(runtime_error,
fmt::format("Allocator \"{}\" is not a Shared Memory Allocator", allocator.getName()));
}
}
return ptr;
}
#if defined(UMPIRE_ENABLE_MPI)
namespace {
std::map<int, MPI_Comm>& get_cached_communicators()
{
static std::map<int, MPI_Comm> cached_communicators{};
return cached_communicators;
}
} // namespace
MPI_Comm get_communicator_for_allocator(Allocator a, MPI_Comm comm)
{
#if defined(UMPIRE_ENABLE_IPC_SHARED_MEMORY) && defined(UMPIRE_ENABLE_DEVICE)
if (auto alloc = dynamic_cast<strategy::DeviceIpcAllocator*>(a.getAllocationStrategy()))
return alloc->get_scope_communicator();
#endif
#if defined(UMPIRE_ENABLE_MPI3_SHARED_MEMORY)
if (auto resource = dynamic_cast<resource::HostMpi3SharedMemoryResource*>(a.getAllocationStrategy()))
return resource->getSharedCommunicator();
#endif
std::map<int, MPI_Comm>& cached_communicators = get_cached_communicators();
MPI_Comm c;
auto scope = a.getAllocationStrategy()->getTraits().scope;
int id = a.getId();
auto cached_comm = cached_communicators.find(id);
if (cached_comm != cached_communicators.end()) {
c = cached_comm->second;
} else {
if (scope == MemoryResourceTraits::shared_scope::node) {
MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL, &c);
} else {
c = MPI_COMM_NULL;
}
cached_communicators[id] = c;
}
return c;
}
void cleanup_cached_communicators()
{
std::map<int, MPI_Comm>& comm = get_cached_communicators();
for (auto c : comm) {
MPI_Comm_free(&c.second);
}
comm.clear();
}
#endif
void register_external_allocation(void* ptr, util::AllocationRecord record)
{
umpire::event::record([&](auto& event) {
event.name("register_external_allocation")
.category(event::category::operation)
.arg("allocator_ref", (void*)record.strategy)
.arg("size", record.size)
.arg("pointer", record.ptr)
.tag("allocator_name", record.strategy->getName())
.tag("replay", "true");
});
auto& rm = umpire::ResourceManager::getInstance();
rm.registerAllocation(ptr, record);
}
util::AllocationRecord deregister_external_allocation(void* ptr)
{
umpire::event::record([&](auto& event) {
event.name("deregister_external_allocation").category(event::category::operation).tag("replay", "true");
});
auto& rm = umpire::ResourceManager::getInstance();
return rm.deregisterAllocation(ptr);
}
camp::resources::Resource get_resource(Allocator a, void* ptr)
{
UMPIRE_LOG(Warning,
"This function will return a generic Camp resource which is not comparable to a specific Camp resource!");
auto s = a.getAllocationStrategy();
strategy::ResourceAwarePool* rap{dynamic_cast<strategy::ResourceAwarePool*>(s)};
if (!rap) {
UMPIRE_ERROR(runtime_error, fmt::format("Allocator \"{}\" is not a ResourceAwarePool!", a.getName()));
}
return rap->getResource(ptr);
}
std::size_t get_num_pending(Allocator a)
{
auto s = a.getAllocationStrategy();
strategy::ResourceAwarePool* rap{dynamic_cast<strategy::ResourceAwarePool*>(s)};
if (!rap) {
UMPIRE_ERROR(runtime_error, fmt::format("Allocator \"{}\" is not a ResourceAwarePool!", a.getName()));
}
return rap->getNumPending();
}
bool try_coalesce(Allocator a)
{
auto s = a.getAllocationStrategy();
bool coalesced{false};
strategy::QuickPool* qp{dynamic_cast<strategy::QuickPool*>(s)};
if (qp) {
qp->coalesce();
coalesced = true;
}
strategy::DynamicPoolList* dpl{dynamic_cast<strategy::DynamicPoolList*>(s)};
if (dpl) {
dpl->coalesce();
coalesced = true;
}
return coalesced;
}
void coalesce(Allocator a)
{
bool coalesced{try_coalesce(a)};
if (!coalesced)
UMPIRE_ERROR(runtime_error, fmt::format("Allocator \"{}\" could not be coalesced", a.getName()));
}
} // end namespace umpire