forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocator.cpp
198 lines (173 loc) · 5.89 KB
/
allocator.cpp
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
//===-- lib/cuda/allocator.cpp ----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Runtime/CUDA/allocator.h"
#include "flang-rt/runtime/allocator-registry.h"
#include "flang-rt/runtime/derived.h"
#include "flang-rt/runtime/descriptor.h"
#include "flang-rt/runtime/environment.h"
#include "flang-rt/runtime/lock.h"
#include "flang-rt/runtime/stat.h"
#include "flang-rt/runtime/terminator.h"
#include "flang-rt/runtime/type-info.h"
#include "flang/Common/ISO_Fortran_binding_wrapper.h"
#include "flang/Runtime/CUDA/common.h"
#include "flang/Support/Fortran.h"
#include "cuda_runtime.h"
namespace Fortran::runtime::cuda {
struct DeviceAllocation {
void *ptr;
std::size_t size;
cudaStream_t stream;
};
// Compare address values. nullptr will be sorted at the end of the array.
int compareDeviceAlloc(const void *a, const void *b) {
const DeviceAllocation *deva = (const DeviceAllocation *)a;
const DeviceAllocation *devb = (const DeviceAllocation *)b;
if (deva->ptr == nullptr && devb->ptr == nullptr)
return 0;
if (deva->ptr == nullptr)
return 1;
if (devb->ptr == nullptr)
return -1;
return deva->ptr < devb->ptr ? -1 : (deva->ptr > devb->ptr ? 1 : 0);
}
// Dynamic array for tracking asynchronous allocations.
static DeviceAllocation *deviceAllocations = nullptr;
Lock lock;
static int maxDeviceAllocations{512}; // Initial size
static int numDeviceAllocations{0};
static constexpr int allocNotFound{-1};
static void initAllocations() {
if (!deviceAllocations) {
deviceAllocations = static_cast<DeviceAllocation *>(
malloc(maxDeviceAllocations * sizeof(DeviceAllocation)));
if (!deviceAllocations) {
Terminator terminator{__FILE__, __LINE__};
terminator.Crash("Failed to allocate tracking array");
}
}
}
// Double the size of the allocation array when size if
static void doubleAllocationArray() {
unsigned newSize = maxDeviceAllocations * 2;
DeviceAllocation *newArray = static_cast<DeviceAllocation *>(
realloc(deviceAllocations, newSize * sizeof(DeviceAllocation)));
if (!newArray) {
Terminator terminator{__FILE__, __LINE__};
terminator.Crash("Failed to reallocate tracking array");
}
deviceAllocations = newArray;
maxDeviceAllocations = newSize;
}
static unsigned findAllocation(void *ptr) {
if (numDeviceAllocations == 0) {
return allocNotFound;
}
int left{0};
int right{numDeviceAllocations - 1};
if (left == right) {
return left;
}
while (left <= right) {
int mid = left + (right - left) / 2;
if (deviceAllocations[mid].ptr == ptr) {
return mid;
}
if (deviceAllocations[mid].ptr < ptr) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return allocNotFound;
}
static void insertAllocation(void *ptr, std::size_t size, std::int64_t stream) {
CriticalSection critical{lock};
initAllocations();
if (numDeviceAllocations >= maxDeviceAllocations) {
doubleAllocationArray();
}
deviceAllocations[numDeviceAllocations].ptr = ptr;
deviceAllocations[numDeviceAllocations].size = size;
deviceAllocations[numDeviceAllocations].stream = (cudaStream_t)stream;
++numDeviceAllocations;
qsort(deviceAllocations, numDeviceAllocations, sizeof(DeviceAllocation),
compareDeviceAlloc);
}
static void eraseAllocation(int pos) {
deviceAllocations[pos].ptr = nullptr;
deviceAllocations[pos].size = 0;
deviceAllocations[pos].stream = (cudaStream_t)0;
qsort(deviceAllocations, numDeviceAllocations, sizeof(DeviceAllocation),
compareDeviceAlloc);
--numDeviceAllocations;
}
extern "C" {
void RTDEF(CUFRegisterAllocator)() {
allocatorRegistry.Register(
kPinnedAllocatorPos, {&CUFAllocPinned, CUFFreePinned});
allocatorRegistry.Register(
kDeviceAllocatorPos, {&CUFAllocDevice, CUFFreeDevice});
allocatorRegistry.Register(
kManagedAllocatorPos, {&CUFAllocManaged, CUFFreeManaged});
allocatorRegistry.Register(
kUnifiedAllocatorPos, {&CUFAllocUnified, CUFFreeUnified});
}
}
void *CUFAllocPinned(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
void *p;
CUDA_REPORT_IF_ERROR(cudaMallocHost((void **)&p, sizeInBytes));
return p;
}
void CUFFreePinned(void *p) { CUDA_REPORT_IF_ERROR(cudaFreeHost(p)); }
void *CUFAllocDevice(std::size_t sizeInBytes, std::int64_t asyncId) {
void *p;
if (Fortran::runtime::executionEnvironment.cudaDeviceIsManaged) {
CUDA_REPORT_IF_ERROR(
cudaMallocManaged((void **)&p, sizeInBytes, cudaMemAttachGlobal));
} else {
if (asyncId == kNoAsyncId) {
CUDA_REPORT_IF_ERROR(cudaMalloc(&p, sizeInBytes));
} else {
CUDA_REPORT_IF_ERROR(
cudaMallocAsync(&p, sizeInBytes, (cudaStream_t)asyncId));
insertAllocation(p, sizeInBytes, asyncId);
}
}
return p;
}
void CUFFreeDevice(void *p) {
CriticalSection critical{lock};
int pos = findAllocation(p);
if (pos >= 0) {
cudaStream_t stream = deviceAllocations[pos].stream;
eraseAllocation(pos);
CUDA_REPORT_IF_ERROR(cudaFreeAsync(p, stream));
} else {
CUDA_REPORT_IF_ERROR(cudaFree(p));
}
}
void *CUFAllocManaged(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
void *p;
CUDA_REPORT_IF_ERROR(
cudaMallocManaged((void **)&p, sizeInBytes, cudaMemAttachGlobal));
return reinterpret_cast<void *>(p);
}
void CUFFreeManaged(void *p) { CUDA_REPORT_IF_ERROR(cudaFree(p)); }
void *CUFAllocUnified(
std::size_t sizeInBytes, [[maybe_unused]] std::int64_t asyncId) {
// Call alloc managed for the time being.
return CUFAllocManaged(sizeInBytes, asyncId);
}
void CUFFreeUnified(void *p) {
// Call free managed for the time being.
CUFFreeManaged(p);
}
} // namespace Fortran::runtime::cuda