forked from NVIDIA/CUDALibrarySamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contraction_multi_gpu.cu
365 lines (308 loc) · 10.9 KB
/
contraction_multi_gpu.cu
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
#include <cutensorMg.h>
#include <cuda_runtime.h>
#include <vector>
#include <cassert>
#include <cstdint>
#include <unordered_map>
#include <chrono>
bool CHECK_success(cudaError_t status)
{
return status == cudaSuccess;
}
const char* CHECK_pretty(cudaError_t status)
{
return cudaGetErrorName(status);
}
bool CHECK_success(cutensorStatus_t status)
{
return status == CUTENSOR_STATUS_SUCCESS;
}
const char* CHECK_pretty(cutensorStatus_t status)
{
return cutensorGetErrorString(status);
}
template<typename T>
T product(const std::vector<T> &values)
{
T result = 1;
for (auto& value : values)
{
result *= value;
}
return result;
}
template<typename T, typename U>
std::vector<T> multiply(const std::vector<T> &lhs, const std::vector<U> &rhs)
{
std::vector<T> result;
assert(lhs.size() == rhs.size() || lhs.empty() || rhs.empty());
for (size_t i = 0; i < lhs.size(); i++)
{
result.push_back((lhs.empty() ? 1 : lhs[i]) * (rhs.empty() ? 1 : rhs[i]));
}
return result;
}
template<typename T, typename U>
std::vector<T> discretize(const std::vector<T> &in, const std::vector<U> &block)
{
if (in.empty())
{
return in;
}
if (block.empty())
{
return in;
}
std::vector<T> result;
assert(in.size() == block.size());
for (size_t i = 0; i < in.size(); i++)
{
U b = block[i];
result.push_back(b * ((in[i] + b - 1) / b));
}
return result;
}
#define CHECK(x) do { auto CHECK_err = (x); if (! CHECK_success(CHECK_err)) { \
printf("Error (%s:%d): \"%s\" returned %s (%d)\n", __FILE__, __LINE__, \
#x, CHECK_pretty(CHECK_err), CHECK_err); exit(-1);} } while(0)
template<typename K, typename V, typename K2>
std::vector<V> collect(const std::unordered_map<K, V> &map, const std::vector<K2> &index)
{
std::vector<V> result;
for (auto& elem : index)
{
result.push_back(map.at(elem));
}
return result;
}
void printDeviceInfo(int deviceId)
{
struct cudaDeviceProp prop;
int currentDeviceId = 0;
CHECK(cudaGetDevice(¤tDeviceId));
CHECK(cudaSetDevice(deviceId));
CHECK(cudaGetDeviceProperties(&prop, deviceId));
printf( "device %d (%s): SMs %2d Capabilities %d.%d, SmClock %.1f Mhz, MemSize (MB) %d, MemClock %.1f Mhz\n",
deviceId,
prop.name,
prop.multiProcessorCount, prop.major, prop.minor,
(float)prop.clockRate*1e-3,
(int)(prop.totalGlobalMem/(1024*1024)),
(float)prop.memoryClockRate*1e-3);
CHECK(cudaSetDevice(currentDeviceId));
}
int main(int argc, char** argv)
{
printf("This sample uses the following GPUs:\n");
std::vector<int32_t> devices;
if (argc == 1)
{
int numDevices;
CHECK(cudaGetDeviceCount(&numDevices));
for (int i = 0; i < numDevices; i++)
{
printDeviceInfo(i);
devices.push_back(i);
}
}
else
{
for (int i = 1; i < argc; i++)
{
const int deviceId = atoi(argv[i]);
printDeviceInfo(deviceId);
devices.push_back(deviceId);
}
}
cutensorMgHandle_t handle;
printf("Initializing cutensorMg handle ... ");
CHECK(cutensorMgCreate(&handle, devices.size(), devices.data()));
printf("done.\n");
std::unordered_map<int32_t, int64_t> extent;
extent['i'] = 4096;
extent['j'] = 4096;
extent['k'] = 4096;
std::unordered_map<int32_t, int64_t> blocksize;
blocksize['i'] = 2048;
blocksize['j'] = 2048;
blocksize['k'] = 2048;
std::unordered_map<int32_t, int32_t> deviceCount;
deviceCount['i'] = 2;
deviceCount['j'] = 2;
deviceCount['k'] = 2;
std::vector<int32_t> modesA {'i', 'k'};
std::vector<int32_t> modesB {'k', 'j'};
std::vector<int32_t> modesC {'i', 'j'};
cudaDataType_t kDataType = CUDA_R_32F;
const int64_t kElementSize = 4;
printf("Creating distributed tensor descriptors ... ");
auto fillUp = [](const std::vector<int32_t> &devices, const int32_t n)
{
std::vector<int32_t> ret;
int32_t numDevices = devices.size();
for(int i=0; i < n; ++i)
{
ret.push_back(devices[i%numDevices]);
}
return ret;
};
cutensorMgTensorDescriptor_t descA;
std::vector<int64_t> extentA = collect(extent, modesA);
std::vector<int64_t> blocksizeA = collect(blocksize, modesA);
std::vector<int32_t> deviceCountA = collect(deviceCount, modesA);
std::vector<int32_t> devicesA = fillUp(devices, product(deviceCountA));
assert(product(deviceCountA) == devicesA.size());
CHECK(cutensorMgCreateTensorDescriptor(handle, &descA, modesA.size(),
extentA.data(), NULL, blocksizeA.data(), NULL,
deviceCountA.data(), devicesA.size(), devicesA.data(), kDataType));
cutensorMgTensorDescriptor_t descB;
std::vector<int64_t> extentB = collect(extent, modesB);
std::vector<int64_t> blocksizeB = collect(blocksize, modesB);
std::vector<int32_t> deviceCountB = collect(deviceCount, modesB);
std::vector<int32_t> devicesB = fillUp(devices, product(deviceCountB));
assert(product(deviceCountB) == devicesB.size());
CHECK(cutensorMgCreateTensorDescriptor(handle, &descB, modesB.size(),
extentB.data(), NULL, blocksizeB.data(), NULL,
deviceCountB.data(), devicesB.size(), devicesB.data(), kDataType));
cutensorMgTensorDescriptor_t descC;
std::vector<int64_t> extentC = collect(extent, modesC);
std::vector<int64_t> blocksizeC = collect(blocksize, modesC);
std::vector<int32_t> deviceCountC = collect(deviceCount, modesC);
std::vector<int32_t> devicesC = fillUp(devices, product(deviceCountC));
assert(product(deviceCountC) == devicesC.size());
CHECK(cutensorMgCreateTensorDescriptor(handle, &descC, modesC.size(),
extentC.data(), NULL, blocksizeC.data(), NULL,
deviceCountC.data(), devicesC.size(), devicesC.data(), kDataType));
printf("done.\n");
printf("Querying workspace size (per GPU) ... ");
const cutensorComputeType_t kComputeType = CUTENSOR_COMPUTE_32F;
const cutensorWorksizePreference_t kWorksizePreference =
CUTENSOR_WORKSPACE_RECOMMENDED;
cutensorMgContractionDescriptor_t contractionDesc;
CHECK(cutensorMgCreateContractionDescriptor(handle, &contractionDesc,
descA, modesA.data(),
descB, modesB.data(),
descC, modesC.data(),
descC, modesC.data(),
kComputeType));
cutensorMgContractionFind_t contractionFind;
CHECK(cutensorMgCreateContractionFind(handle, &contractionFind,
CUTENSORMG_ALGO_DEFAULT));
std::vector<int64_t> workspaceSize(devices.size());
int64_t workspaceHostSize;
CHECK(cutensorMgContractionGetWorkspace(handle,
contractionDesc, contractionFind, kWorksizePreference, workspaceSize.data(), &workspaceHostSize));
printf("done.\n");
printf("Initializing contraction plan ... \n");
cutensorMgContractionPlan_t plan;
CHECK(cutensorMgCreateContractionPlan(handle, &plan,
contractionDesc, contractionFind, workspaceSize.data(), workspaceHostSize));
printf("done.\n");
printf("Allocating data ... ");
int64_t elementsA = product(discretize(extentA, multiply(deviceCountA, blocksizeA))) / product(deviceCountA);
std::vector<void*> memoryA;
for (auto& device : devicesA)
{
void* memory;
CHECK(cudaSetDevice(device));
CHECK(cudaMalloc(&memory, elementsA * kElementSize));
memoryA.push_back(memory);
}
int64_t elementsB = product(discretize(extentB, multiply(deviceCountB, blocksizeB))) / product(deviceCountB);
std::vector<void*> memoryB;
for (auto& device : devicesB)
{
void* memory;
CHECK(cudaSetDevice(device));
CHECK(cudaMalloc(&memory, elementsB * kElementSize));
memoryB.push_back(memory);
}
int64_t elementsC = product(discretize(extentC, multiply(deviceCountC, blocksizeC))) / product(deviceCountC);
std::vector<void*> memoryC;
for (auto& device : devicesC)
{
void* memory;
CHECK(cudaSetDevice(device));
CHECK(cudaMalloc(&memory, elementsC * kElementSize));
memoryC.push_back(memory);
}
std::vector<cudaStream_t> streams;
for (auto& device : devices)
{
cudaStream_t stream;
CHECK(cudaSetDevice(device));
CHECK(cudaStreamCreate(&stream));
streams.push_back(stream);
}
/*
* Allocate workspace
*/
// host
void* workspaceHost = nullptr;
CHECK(cudaMallocHost(&workspaceHost, workspaceHostSize));
// devices
std::vector<void*> workspace;
for (int i = 0; i < devices.size(); i++)
{
void* memory;
CHECK(cudaSetDevice(devices[i]));
CHECK(cudaMalloc(&memory, workspaceSize[i]));
workspace.push_back(memory);
}
printf("done.\n");
printf("Performing distributed tensor contraction ...\n");
float kAlpha = 1;
float kBeta = 0;
int currentDeviceId = -1;
CHECK(cudaGetDevice(¤tDeviceId));
float minElapsed = 0;
const int nRep = 3; // for stable timings
for (int rep = 0; rep < nRep; rep++)
{
const auto start = std::chrono::steady_clock::now();
CHECK(cutensorMgContraction(handle, plan, &kAlpha,
const_cast<const void**>(memoryA.data()),
const_cast<const void**>(memoryB.data()), &kBeta,
const_cast<const void**>(memoryC.data()), memoryC.data(),
workspace.data(), workspaceHost, streams.data()));
for (auto& deviceId : devices)
{
CHECK(cudaSetDevice(deviceId));
CHECK(cudaDeviceSynchronize());
}
const auto end = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> dur = end - start;
if (minElapsed == 0 || minElapsed > dur.count()) {
minElapsed = dur.count();
}
}
CHECK(cudaSetDevice(currentDeviceId));
printf("execution took: %.2e millisec.\n", minElapsed);
printf("Free resources ...\n");
for (auto& stream : streams)
{
CHECK(cudaStreamSynchronize(stream));
CHECK(cudaStreamDestroy(stream));
}
for (auto& memory : memoryA)
{
CHECK(cudaFree(memory));
}
for (auto& memory : memoryB)
{
CHECK(cudaFree(memory));
}
for (auto& memory : memoryC)
{
CHECK(cudaFree(memory));
}
CHECK(cudaFreeHost(workspaceHost));
CHECK(cutensorMgDestroyContractionDescriptor(contractionDesc));
CHECK(cutensorMgDestroyContractionFind(contractionFind));
CHECK(cutensorMgDestroyContractionPlan(plan));
CHECK(cutensorMgDestroyTensorDescriptor(descA));
CHECK(cutensorMgDestroyTensorDescriptor(descB));
CHECK(cutensorMgDestroyTensorDescriptor(descC));
CHECK(cutensorMgDestroy(handle));
printf("Done: everything has completed successfully.\n");
}