-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_mpi.cpp
More file actions
318 lines (255 loc) · 10.4 KB
/
gpu_mpi.cpp
File metadata and controls
318 lines (255 loc) · 10.4 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
// MIT License
//
// Copyright (c) 2024-2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <hip/hip_runtime.h>
#include <mpi.h>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <vector>
#define HEAP_SIZE 1 << 30
#define HIP_CHECK(condition) \
{ \
const hipError_t error_code = condition; \
if (error_code != hipSuccess) { \
std::cerr << "HIP Error encountered: " << hipGetErrorString(error_code) \
<< " at " << __FILE__ << ": " << __LINE__ << std::endl; \
std::runtime_error(hipGetErrorString(error_code)); \
} \
}
/// Maintains internal metadata around symmetric heap
/// Used for tracking memory allocations from heap, keeps
/// track of allocated and free blocks.
typedef struct SymmetricHeap {
uint8_t *base;
size_t offset;
size_t size;
};
class Shmem {
public:
hipIpcMemHandle_t ipcMemHandle;
hipIpcMemHandle_t *ipcMemHandles;
SymmetricHeap heap;
int myRank;
int numDevices, currDevice;
void **heapBases;
void **d_heapBases;
void *symmetricheap;
Shmem(int device, int rank, int numdevices)
: currDevice(device), myRank(rank), numDevices(numdevices) {
// Allocate Symmetric heap on local device memory,
// each device will allocate their own heap.
HIP_CHECK(hipMalloc(&symmetricheap, HEAP_SIZE));
heap.base = (uint8_t *)symmetricheap;
heap.offset = 0;
heap.size = HEAP_SIZE;
// Gets IPC memory handle for local device's symmetric heap that can be
// given to hipIpcOpenMemHandle, That will map remote heap allocation to
// local device's address space.
HIP_CHECK(hipIpcGetMemHandle(&ipcMemHandle, symmetricheap));
// gathers IPC handles of heap allocation from all devices
ipcMemHandles =
(hipIpcMemHandle_t *)malloc(numDevices * sizeof(hipIpcMemHandle_t));
MPI_Allgather(&ipcMemHandle, sizeof(hipIpcMemHandle_t), MPI_CHAR,
ipcMemHandles, sizeof(hipIpcMemHandle_t), MPI_CHAR,
MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
heapBases = (void **)malloc(numDevices * sizeof(void *));
heapBases[currDevice] = symmetricheap;
printf("heapBases[%d]: %p \n", currDevice, heapBases[currDevice]);
// hipIpcOpenMemHandle maps remote/peer heap allocation into
// local device address space, heap allocations can be accessed
// via remoteBuf device pointer
for (int i = 0; i < numDevices; i++) {
if (i != currDevice) {
void *remoteBuf = NULL;
HIP_CHECK(hipIpcOpenMemHandle(&remoteBuf, ipcMemHandles[i],
hipIpcMemLazyEnablePeerAccess));
heapBases[i] = remoteBuf;
}
}
MPI_Barrier(MPI_COMM_WORLD);
HIP_CHECK(hipMalloc(&d_heapBases, numDevices * (sizeof(void *))));
HIP_CHECK(hipMemcpy(d_heapBases, heapBases, numDevices * sizeof(void *),
hipMemcpyHostToDevice));
}
void *shmem_malloc(size_t size) {
if (size == 0) return NULL;
if (heap.offset + size > heap.size) {
return NULL;
}
void *ptr = (char *)heap.base + heap.offset;
heap.offset += size;
return ptr;
}
~Shmem() {
for (int i = 0; i < numDevices; i++) {
if (i != currDevice) {
HIP_CHECK(hipIpcCloseMemHandle(heapBases[i]));
}
}
HIP_CHECK(hipFree(d_heapBases));
HIP_CHECK(hipFree(symmetricheap));
free(ipcMemHandles);
free(heapBases);
MPI_Finalize();
}
void shmem_free(void *ptr) {
// Can maintain free list or advanced technique for properly reclaiming
// space in heap currently do nothing for basic version, if we run out of
// space, error out for now
}
};
__device__ __forceinline__ void memcpy(void *dst, void *src, size_t size) {
uint8_t *dst_bytes{static_cast<uint8_t *>(dst)};
uint8_t *src_bytes{static_cast<uint8_t *>(src)};
for (size_t i = 8; i > 1; i >>= 1) {
while (size >= i) {
store_asm(src_bytes, dst_bytes, i);
src_bytes += i;
dst_bytes += i;
size -= i;
}
}
if (size == 1) {
*dst_bytes = *src_bytes;
}
}
__global__ void get(void *src, int currDevice, int peerDevice, size_t nelems,
void **heapBases) {
unsigned int threadId = blockIdx.x * blockDim.x + threadIdx.x;
char *srcHeapBase = heapBases[currDevice];
char *dstHeapBase = heapBases[peerDevice];
char *srcCast = reinterpret_cast<char *> src;
srsrcCast += threadId;
uint64_t offset = (char *)srcCast - srcHeapBase;
char *dst = dstHeapBase + offset;
memcpy(dst, srsrcCast, nelems);
}
__global__ void put(void *src, int currDevice, int peerDevice,
void **heapBases) {
char *srcHeapBase = heapBases[currDevice];
char *dstHeapBase = heapBases[peerDevice];
size_t offset = (size_t)src - srcHeapBase;
char *dst = dstHeapBase + offset;
dst = src;
}
/// \brief Example for IPC memory sharing via symmetric heap
/// when all GPU devices are on same node (intra-node)
/// See collectiveComms.md for more details on intra-node communication via
/// IPC.
int main(int argc, char **argv) {
int myRank, numRanks;
int numDevices, currDevice = 0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank); // Current PE ID
MPI_Comm_size(MPI_COMM_WORLD, &numRanks); // total number of PEs
HIP_CHECK(hipGetDeviceCount(&numDevices));
currDevice = myRank % numDevices;
HIP_CHECK(hipSetDevice(currDevice));
printf("CommSize: %d HIP Device count: %d , CurrDevice: %d Rank: %d\n",
numRanks, numDevices, currDevice, myRank);
Shmem shmem_ctx(currDevice, myRank, numDevices);
for (int i = 0; i < numDevices; i++) {
printf("GPU: %d - heap from GPU %d: %p\n", currDevice, i,
shmem_ctx.heapBases[i]);
}
int peerDevice = (currDevice + 1) % 2;
int threadsPerBlock = 256;
// number of threads in each kernel block
const dim3 block_dim(threadsPerBlock);
// number of blocks in grid
// const dim3 grid_dim((N + threadsPerBlock - 1) /
// threadsPerBlock);
// GPU 0, Puts data into buffer Get buffer from heap
// GPU 1, gets data from remote buffer
if (currDevice == 0) {
// malloc dword
void *buf0 = shmem_ctx.shmem_malloc(4);
put<<<dim3(1), block_dim, 0, hipStreamDefault>>>(
buf0, currDevice, peerDevice, shmem_ctx.d_heapBases);
}
// else if (currDevice == 1) {
// void *buf1;
// std::cout << "Launching Kernel.." << std::endl;
// get<<<grid_dim, block_dim, 0, hipStreamDefault>>>(
// buf1, currDevice, peerDevice, shmem_ctx.d_heapBases);
// }
return 0;
}
void peer2peerWithIpc(int myRank) {
int numDevices, currDevice = 0;
void *symmetricHeap = NULL;
void *remoteBuf = NULL;
size_t heapSize = 1024;
HIP_CHECK(hipGetDeviceCount(&numDevices));
currDevice = myRank % numDevices;
HIP_CHECK(hipSetDevice(currDevice));
printf("HIP Device count: %d , CurrDevice: %d Rank: %d\n", numDevices,
currDevice, myRank);
HIP_CHECK(hipMalloc(&symmetricHeap, HEAP_SIZE));
void **heapBases = (void **)malloc(numDevices * sizeof(void *));
heapBases[currDevice] = symmetricHeap;
printf("heapBases[%d]: %p \n", currDevice, heapBases[currDevice]);
hipIpcMemHandle_t ipcMemHandle;
// Gets IPC memory handle for local device's symmetric heap that can be given
// to hipIpcOpenMemHandle, that will map remote heap allocation to local
// device's address space.
HIP_CHECK(hipIpcGetMemHandle(&ipcMemHandle, symmetricHeap));
hipIpcMemHandle_t *ipcMemHandles =
(hipIpcMemHandle_t *)malloc(numDevices * sizeof(hipIpcMemHandle_t));
MPI_Allgather(&ipcMemHandle, sizeof(hipIpcMemHandle_t), MPI_CHAR,
ipcMemHandles, sizeof(hipIpcMemHandle_t), MPI_CHAR,
MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
// hipIpcOpenMemHandle maps remote/peer heap allocation into
// local device address space, heap allocations can be accessed
// via remoteBuf device pointer
int peerDevice = (currDevice + 1) % 2;
if (currDevice == 0) {
HIP_CHECK(hipIpcOpenMemHandle(&remoteBuf, ipcMemHandles[peerDevice],
hipIpcMemLazyEnablePeerAccess));
printf("GPU: %d - heap from GPU 1: %p\n", currDevice, remoteBuf);
} else if (currDevice == 1) {
HIP_CHECK(hipIpcOpenMemHandle(&remoteBuf, ipcMemHandles[peerDevice],
hipIpcMemLazyEnablePeerAccess));
printf("GPU: %d - heap from GPU 0: %p\n", currDevice, remoteBuf);
}
MPI_Barrier(MPI_COMM_WORLD);
char *hostRemoteBuf = (char *)malloc(heapSize);
HIP_CHECK(
hipMemcpy(hostRemoteBuf, remoteBuf, heapSize, hipMemcpyDeviceToHost));
char *hostLocalBuf = (char *)malloc(heapSize);
HIP_CHECK(
hipMemcpy(hostLocalBuf, symmetricHeap, heapSize, hipMemcpyDeviceToHost));
printf("GPU: %d - data from local buf : %d %d %d\n", currDevice,
hostLocalBuf[0], hostLocalBuf[1], hostLocalBuf[2]);
printf("GPU: %d - data from Remote buf : %d %d %d\n", currDevice,
hostRemoteBuf[0], hostRemoteBuf[1], hostRemoteBuf[2]);
HIP_CHECK(hipIpcCloseMemHandle(remoteBuf));
HIP_CHECK(hipFree(symmetricHeap));
free(hostRemoteBuf);
free(hostLocalBuf);
free(ipcMemHandles);
free(heapBases);
}