-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmultinode_memcpy.cpp
More file actions
327 lines (268 loc) · 12.3 KB
/
multinode_memcpy.cpp
File metadata and controls
327 lines (268 loc) · 12.3 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef MULTINODE
#include <mpi.h>
#include <unistd.h>
#include "kernels.cuh"
#include "multinode_memcpy.h"
MultinodeMemoryAllocation::MultinodeMemoryAllocation(size_t bufferSize, int MPI_rank): bufferSize(bufferSize), MPI_rank(MPI_rank) {
cudaSetDevice(localDevice);
}
static CUresult MPIstreamSyncHelper(CUstream stream) {
CUresult err = CUDA_ERROR_NOT_READY;
int flag;
while (err == CUDA_ERROR_NOT_READY) {
err = cuStreamQuery(stream);
MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
}
return err;
}
CUresult MultinodeMemoryAllocation::streamSynchronizeWrapper(CUstream stream) const {
return MPIstreamSyncHelper(stream);
}
MultinodeMemoryAllocationUnicast::MultinodeMemoryAllocationUnicast(size_t bufferSize, int MPI_rank): MultinodeMemoryAllocation(bufferSize, MPI_rank) {
handleType = CU_MEM_HANDLE_TYPE_FABRIC;
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
prop.location.id = localDevice;
prop.requestedHandleTypes = handleType;
size_t granularity = 0;
CU_ASSERT(cuMemGetAllocationGranularity(&granularity, &prop, CU_MEM_ALLOC_GRANULARITY_RECOMMENDED));
roundedUpAllocationSize = ROUND_UP(bufferSize, granularity);
if (MPI_rank == worldRank) {
// Allocate the memory
CU_ASSERT(cuMemCreate(&handle, roundedUpAllocationSize, &prop, 0 /*flags*/));
// Export the allocation to the importing process
CU_ASSERT(cuMemExportToShareableHandle(&fh, handle, handleType, 0 /*flags*/));
}
MPI_Bcast(&fh, sizeof(fh), MPI_BYTE, MPI_rank, MPI_COMM_WORLD);
if (MPI_rank != worldRank) {
CU_ASSERT(cuMemImportFromShareableHandle(&handle, (void *)&fh, handleType));
}
// Map the memory
CU_ASSERT(cuMemAddressReserve((CUdeviceptr *) &buffer, roundedUpAllocationSize, 0, 0 /*baseVA*/, 0 /*flags*/));
CU_ASSERT(cuMemMap((CUdeviceptr) buffer, roundedUpAllocationSize, 0 /*offset*/, handle, 0 /*flags*/));
desc.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
desc.location.id = localDevice;
desc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
CU_ASSERT(cuMemSetAccess((CUdeviceptr) buffer, roundedUpAllocationSize, &desc, 1 /*count*/));
// Make sure that everyone is done with mapping the fabric allocation
MPI_Barrier(MPI_COMM_WORLD);
}
MultinodeMemoryAllocationUnicast::~MultinodeMemoryAllocationUnicast() {
// Make sure that everyone is done using the memory
MPI_Barrier(MPI_COMM_WORLD);
CU_ASSERT(cuMemUnmap((CUdeviceptr) buffer, roundedUpAllocationSize));
CU_ASSERT(cuMemRelease(handle));
CU_ASSERT(cuMemAddressFree((CUdeviceptr) buffer, roundedUpAllocationSize));
}
MultinodeMemoryAllocationMulticast::MultinodeMemoryAllocationMulticast(size_t bufferSize, int MPI_rank): MultinodeMemoryAllocation(bufferSize, MPI_rank) {
handleType = CU_MEM_HANDLE_TYPE_FABRIC;
multicastProp.numDevices = worldSize;
multicastProp.handleTypes = handleType;
size_t gran;
CU_ASSERT(cuMulticastGetGranularity(&gran, &multicastProp, CU_MULTICAST_GRANULARITY_RECOMMENDED));
roundedUpAllocationSize = ROUND_UP(bufferSize, gran);
multicastProp.size = roundedUpAllocationSize;
if (MPI_rank == worldRank) {
// Allocate the memory
CU_ASSERT(cuMulticastCreate(&multicastHandle, &multicastProp));
// Export the allocation to the importing process
CU_ASSERT(cuMemExportToShareableHandle(&fh, multicastHandle, handleType, 0 /*flags*/));
}
MPI_Bcast(&fh, sizeof(fh), MPI_BYTE, MPI_rank, MPI_COMM_WORLD);
if (MPI_rank != worldRank) {
CU_ASSERT(cuMemImportFromShareableHandle(&multicastHandle, (void *)&fh, handleType));
}
CUdevice dev;
CU_ASSERT(cuDeviceGet(&dev, localDevice));
CU_ASSERT(cuMulticastAddDevice(multicastHandle, dev));
// Ensure all devices in this process are added BEFORE binding mem on any device
MPI_Barrier(MPI_COMM_WORLD);
// Allocate the memory (same as unicast) and bind to MC handle
CUmemAllocationProp prop = {};
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
prop.location.id = localDevice;
prop.requestedHandleTypes = handleType;
CU_ASSERT(cuMemCreate(&handle, roundedUpAllocationSize, &prop, 0 /*flags*/));
CU_ASSERT(cuMulticastBindMem(multicastHandle, 0, handle, 0, roundedUpAllocationSize, 0));
// Map the memory
CU_ASSERT(cuMemAddressReserve((CUdeviceptr *) &buffer, roundedUpAllocationSize, 0, 0 /*baseVA*/, 0 /*flags*/));
CU_ASSERT(cuMemMap((CUdeviceptr) buffer, roundedUpAllocationSize, 0 /*offset*/, multicastHandle, 0 /*flags*/));
desc.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
desc.location.id = localDevice;
desc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
CU_ASSERT(cuMemSetAccess((CUdeviceptr) buffer, roundedUpAllocationSize, &desc, 1 /*count*/));
// Make sure that everyone is done with mapping the fabric allocation
MPI_Barrier(MPI_COMM_WORLD);
}
MultinodeMemoryAllocationMulticast::~MultinodeMemoryAllocationMulticast() {
// Make sure that everyone is done using the memory
MPI_Barrier(MPI_COMM_WORLD);
CUdevice dev;
CU_ASSERT(cuDeviceGet(&dev, localDevice));
CU_ASSERT(cuMulticastUnbind(multicastHandle, dev, 0, roundedUpAllocationSize));
CU_ASSERT(cuMemRelease(handle));
CU_ASSERT(cuMemUnmap((CUdeviceptr) buffer, roundedUpAllocationSize));
CU_ASSERT(cuMemRelease(multicastHandle));
CU_ASSERT(cuMemAddressFree((CUdeviceptr) buffer, roundedUpAllocationSize));
}
MultinodeDeviceBuffer::MultinodeDeviceBuffer(size_t bufferSize, int MPI_rank):
MPI_rank(MPI_rank),
MemcpyBuffer(bufferSize) {
}
int MultinodeDeviceBuffer::getBufferIdx() const {
// only single-GPU supported for now
return 0;
}
std::string MultinodeDeviceBuffer::getBufferString() const {
return "Multinode node " + std::to_string(MPI_rank);
}
CUcontext MultinodeDeviceBuffer::getPrimaryCtx() const {
CUcontext primaryCtx;
CU_ASSERT(cuDevicePrimaryCtxRetain(&primaryCtx, localDevice));
return primaryCtx;
}
int MultinodeDeviceBuffer::getMPIRank() const {
return MPI_rank;
}
MultinodeDeviceBufferUnicast::MultinodeDeviceBufferUnicast(size_t bufferSize, int MPI_rank):
MultinodeDeviceBuffer(bufferSize, MPI_rank),
MemoryAllocation(bufferSize, MPI_rank) {
buffer = MemoryAllocation.getBuffer();
}
MultinodeDeviceBufferMulticast::MultinodeDeviceBufferMulticast(size_t bufferSize, int MPI_rank):
MultinodeDeviceBuffer(bufferSize, MPI_rank),
MemoryAllocation(bufferSize, MPI_rank) {
buffer = MemoryAllocation.getBuffer();
}
MultinodeDeviceBufferLocal::MultinodeDeviceBufferLocal(size_t bufferSize, int MPI_rank):
MultinodeDeviceBuffer(bufferSize, MPI_rank) {
buffer = nullptr;
if (worldRank == MPI_rank) {
CU_ASSERT(cuDevicePrimaryCtxRetain(&primaryCtx, localDevice));
CU_ASSERT(cuCtxSetCurrent(primaryCtx));
if (bufferSize) {
CU_ASSERT(cuMemAlloc((CUdeviceptr*)&buffer, bufferSize));
}
}
}
MultinodeDeviceBufferLocal::~MultinodeDeviceBufferLocal() {
if (buffer) {
CU_ASSERT(cuCtxSetCurrent(primaryCtx));
CU_ASSERT(cuMemFree((CUdeviceptr)buffer));
CU_ASSERT(cuDevicePrimaryCtxRelease(localDevice));
}
}
NodeHelperMulti::NodeHelperMulti() : blockingVarDeviceAllocation(sizeof(*blockingVarDevice), 0) {
CU_ASSERT(cuMemHostAlloc((void **)&blockingVarHost, sizeof(*blockingVarHost), CU_MEMHOSTALLOC_PORTABLE));
blockingVarDevice = (volatile int*) blockingVarDeviceAllocation.getBuffer();
}
NodeHelperMulti::~NodeHelperMulti() {
CU_ASSERT(cuMemFreeHost((void*)blockingVarHost));
}
MemcpyDispatchInfo NodeHelperMulti::dispatchMemcpy(const std::vector<const MemcpyBuffer*> &srcNodesUnfiltered, const std::vector<const MemcpyBuffer*> &dstNodesUnfiltered, ContextPreference ctxPreference) {
std::vector<int> ranksUnfiltered(srcNodesUnfiltered.size(), -1);
std::vector<CUcontext> contextsUnfiltered(srcNodesUnfiltered.size());
std::vector<const MemcpyBuffer*> srcNodes;
std::vector<const MemcpyBuffer*> dstNodes;
std::vector<CUcontext> contexts;
for (int i = 0; i < srcNodesUnfiltered.size(); i++) {
// prefer source context
// determine which ranks executes given operation
if (ctxPreference == PREFER_SRC_CONTEXT && srcNodesUnfiltered[i]->getPrimaryCtx() != nullptr) {
contextsUnfiltered[i] = srcNodesUnfiltered[i]->getPrimaryCtx();
ranksUnfiltered[i] = srcNodesUnfiltered[i]->getMPIRank();
} else if (dstNodesUnfiltered[i]->getPrimaryCtx() != nullptr) {
contextsUnfiltered[i] = dstNodesUnfiltered[i]->getPrimaryCtx();
ranksUnfiltered[i] = dstNodesUnfiltered[i]->getMPIRank();
}
}
for (int i = 0; i < srcNodesUnfiltered.size(); i++) {
if (ranksUnfiltered[i] == worldRank) {
srcNodes.push_back(srcNodesUnfiltered[i]);
dstNodes.push_back(dstNodesUnfiltered[i]);
contexts.push_back(contextsUnfiltered[i]);
}
}
// Don't crash if there are no memcopies to do
if (ranksUnfiltered.size() > 0) {
rankOfFirstMemcpy = ranksUnfiltered[0];
}
return MemcpyDispatchInfo(srcNodes, dstNodes, contexts, ranksUnfiltered);
}
double NodeHelperMulti::calculateTotalBandwidth(double totalTime, double totalSize, size_t loopCount) {
double totalMax = 0;
MPI_Allreduce(&totalTime, &totalMax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
totalTime = totalMax;
double totalSum = 0;
MPI_Allreduce(&totalSize, &totalSum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
totalSize = totalSum;
return (totalSize * loopCount * 1000ull * 1000ull) / totalTime;
}
double NodeHelperMulti::calculateSumBandwidth(std::vector<PerformanceStatistic> &bandwidthStats) {
double sum = 0.0;
for (auto stat : bandwidthStats) {
sum += stat.returnAppropriateMetric() * 1e-9;
}
// Calculate total BW sum across all nodes and memcopies
double totalSum = 0;
MPI_Allreduce(&sum, &totalSum, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
return totalSum;
}
double NodeHelperMulti::calculateFirstBandwidth(std::vector<PerformanceStatistic> &bandwidthStats) {
// Broadcast bandwidth of "first" memcopy to other nodes
double retval = 0;
if (worldRank == rankOfFirstMemcpy) {
retval = bandwidthStats[0].returnAppropriateMetric() * 1e-9;
}
MPI_Bcast(&retval, 1, MPI_DOUBLE, rankOfFirstMemcpy, MPI_COMM_WORLD);
return retval;
}
std::vector<double> NodeHelperMulti::calculateVectorBandwidth(std::vector<double> &results, std::vector<int> originalRanks) {
std::vector<double> retval;
int current_local_elem = 0;
for (int i = 0; i < originalRanks.size(); i++) {
double tmp = 0;
if (worldRank == originalRanks[i]) {
tmp = results[current_local_elem];
current_local_elem++;
}
MPI_Bcast(&tmp, 1, MPI_DOUBLE, originalRanks[i], MPI_COMM_WORLD);
retval.push_back(tmp);
}
return retval;
}
void NodeHelperMulti::synchronizeProcess() {
MPI_Barrier(MPI_COMM_WORLD);
}
CUresult NodeHelperMulti::streamSynchronizeWrapper(CUstream stream) const {
return MPIstreamSyncHelper(stream);
}
void NodeHelperMulti::streamBlockerReset() {
*blockingVarHost = 0;
CU_ASSERT(cuMemsetD32((CUdeviceptr) blockingVarDevice, 0, 1));
}
void NodeHelperMulti::streamBlockerRelease() {
*blockingVarHost = 1;
}
void NodeHelperMulti::streamBlockerBlock(CUstream stream) {
// MPI rank ranks[0] is released by blockingVar, writes to blockingVarDevice, releasing other ranks
CU_ASSERT(spinKernelMultistage((worldRank == rankOfFirstMemcpy) ? blockingVarHost : nullptr, blockingVarDevice, stream));
}
#endif // MULTINODE