-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathtradd.cu
More file actions
448 lines (384 loc) · 12.8 KB
/
Copy pathtradd.cu
File metadata and controls
448 lines (384 loc) · 12.8 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
444
445
446
447
448
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 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.
*/
#include <assert.h>
#include <cublasmp.h>
#include <math.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include "helpers.h"
#include "matrix_generator.hxx"
template <typename T>
static bool check_tradd_result(
cublasMpHandle_t mp_handle,
ncclComm_t comm,
cudaStream_t stream,
int rank,
cublasMpGrid_t grid,
int nprow,
int npcol,
int myprow,
int mypcol,
cublasFillMode_t uplo,
cublasOperation_t trans,
int64_t m,
int64_t n,
const T* alpha,
T* d_A,
int64_t ia,
int64_t ja,
cublasMpMatrixDescriptor_t descA,
const T* beta,
T* d_C_ref,
T* d_C,
int64_t ic,
int64_t jc,
cublasMpMatrixDescriptor_t descC)
{
static_assert(
std::is_same_v<T, double> || std::is_same_v<T, float>,
"tradd sample reference check supports float and double only");
const int64_t a_rows = trans == CUBLAS_OP_N ? m : n;
const int64_t a_cols = trans == CUBLAS_OP_N ? n : m;
T* full_A = nullptr;
T* full_C_ref = nullptr;
T* full_C_result = nullptr;
int64_t full_A_lld = 0;
int64_t full_C_ref_lld = 0;
int64_t full_C_result_lld = 0;
gather_matrix(
mp_handle,
comm,
stream,
a_rows,
a_cols,
d_A,
ia,
ja,
descA,
grid,
nprow,
npcol,
myprow,
mypcol,
&full_A,
&full_A_lld);
gather_matrix(
mp_handle,
comm,
stream,
m,
n,
d_C_ref,
ic,
jc,
descC,
grid,
nprow,
npcol,
myprow,
mypcol,
&full_C_ref,
&full_C_ref_lld);
gather_matrix(
mp_handle,
comm,
stream,
m,
n,
d_C,
ic,
jc,
descC,
grid,
nprow,
npcol,
myprow,
mypcol,
&full_C_result,
&full_C_result_lld);
CUDA_CHECK(cudaStreamSynchronize(stream));
bool passed = true;
if (rank == 0)
{
std::vector<T> h_A(full_A_lld * a_cols);
std::vector<T> h_C(full_C_ref_lld * n);
std::vector<T> h_result(full_C_result_lld * n);
CUDA_CHECK(cudaMemcpyAsync(h_A.data(), full_A, h_A.size() * sizeof(T), cudaMemcpyDeviceToHost, stream));
CUDA_CHECK(cudaMemcpyAsync(h_C.data(), full_C_ref, h_C.size() * sizeof(T), cudaMemcpyDeviceToHost, stream));
CUDA_CHECK(cudaMemcpyAsync(
h_result.data(), full_C_result, h_result.size() * sizeof(T), cudaMemcpyDeviceToHost, stream));
CUDA_CHECK(cudaStreamSynchronize(stream));
for (int64_t col = 0; col < n; ++col)
{
for (int64_t row = 0; row < m; ++row)
{
const bool in_triangle = (uplo == CUBLAS_FILL_MODE_LOWER) ? (row >= col) : (row <= col);
if (!in_triangle)
{
continue;
}
const T aval = (trans == CUBLAS_OP_N) ? h_A[row + col * full_A_lld] : h_A[col + row * full_A_lld];
h_C[row + col * full_C_ref_lld] =
static_cast<T>((*alpha) * to_double(aval) + (*beta) * to_double(h_C[row + col * full_C_ref_lld]));
}
}
passed = allclose_host(
"tradd",
h_result.data(),
full_C_result_lld,
h_C.data(),
full_C_ref_lld,
m,
n,
default_rtol<T>(),
default_atol<T>());
}
CUDA_CHECK(cudaFree(full_A));
CUDA_CHECK(cudaFree(full_C_ref));
CUDA_CHECK(cudaFree(full_C_result));
int passed_int = passed ? 1 : 0;
MPI_CHECK(MPI_Bcast(&passed_int, 1, MPI_INT, 0, MPI_COMM_WORLD));
return passed_int != 0;
}
int main(int argc, char* argv[])
{
Options opts = { .m = 10,
.n = 10,
.k = 10,
.mbA = 2,
.nbA = 2,
.mbB = 2,
.nbB = 2,
.mbC = 2,
.nbC = 2,
.ia = 3,
.ja = 3,
.ib = 3,
.jb = 1,
.ic = 1,
.jc = 1,
.p = 2,
.q = 1,
.grid_layout = 'c',
.verbose = false,
.cycles = 10,
.warmup = 5 };
opts.parse(argc, argv);
opts.validate();
MPI_Init(nullptr, nullptr);
const int64_t m = opts.m;
const int64_t n = opts.n;
const int64_t ia = opts.ia;
const int64_t ja = opts.ja;
const int64_t ic = opts.ic;
const int64_t jc = opts.jc;
const int64_t mbA = opts.mbA;
const int64_t nbA = opts.nbA;
const int64_t mbC = opts.mbC;
const int64_t nbC = opts.nbC;
const int nprow = opts.p;
const int npcol = opts.q;
int rank, nranks;
MPI_Comm_size(MPI_COMM_WORLD, &nranks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
const int myprow = (opts.grid_layout == 'c' ? rank % nprow : rank / npcol);
const int mypcol = (opts.grid_layout == 'c' ? rank / nprow : rank % npcol);
const int local_device = getLocalDevice();
CUDA_CHECK(cudaSetDevice(local_device));
CUDA_CHECK(cudaFree(nullptr));
ncclUniqueId id;
if (rank == 0)
{
NCCL_CHECK(ncclGetUniqueId(&id));
}
MPI_CHECK(MPI_Bcast(&id, sizeof(id), MPI_BYTE, 0, MPI_COMM_WORLD));
ncclComm_t comm;
NCCL_CHECK(ncclCommInitRank(&comm, nranks, id, rank));
cudaStream_t stream = nullptr;
CUDA_CHECK(cudaStreamCreate(&stream));
cublasMpHandle_t handle = nullptr;
CUBLASMP_CHECK(cublasMpCreate(&handle, stream));
cublasMpGrid_t grid = nullptr;
cublasMpMatrixDescriptor_t descA = nullptr;
cublasMpMatrixDescriptor_t descC = nullptr;
double* d_A = nullptr;
double* d_C = nullptr;
double* d_C_ref = nullptr;
double* d_work = nullptr;
double alpha = 1.0;
double beta = 1.0;
size_t workspaceInBytesOnDevice = 0;
size_t workspaceInBytesOnHost = 0;
const int64_t global_m_a = (ia - 1) + m;
const int64_t global_n_a = (ja - 1) + n;
const int64_t global_m_c = (ic - 1) + m;
const int64_t global_n_c = (jc - 1) + n;
const int64_t llda = cublasMpNumroc(global_m_a, mbA, myprow, 0, nprow);
const int64_t loc_n_a = cublasMpNumroc(global_n_a, nbA, mypcol, 0, npcol);
const int64_t lldc = cublasMpNumroc(global_m_c, mbC, myprow, 0, nprow);
const int64_t loc_n_c = cublasMpNumroc(global_n_c, nbC, mypcol, 0, npcol);
std::vector<double> h_A(llda * loc_n_a, 0);
std::vector<double> h_C(lldc * loc_n_c, 0);
generate_diag_matrix(m, n, h_A.data(), mbA, nbA, ia, ja, llda, nprow, npcol, myprow, mypcol);
generate_diag_matrix(m, n, h_C.data(), mbC, nbC, ic, jc, lldc, nprow, npcol, myprow, mypcol);
CUDA_CHECK(cudaMalloc(&d_A, llda * loc_n_a * sizeof(double)));
CUDA_CHECK(cudaMalloc(&d_C, lldc * loc_n_c * sizeof(double)));
CUDA_CHECK(cudaMalloc(&d_C_ref, lldc * loc_n_c * sizeof(double)));
CUDA_CHECK(cudaMemcpyAsync(d_A, h_A.data(), llda * loc_n_a * sizeof(double), cudaMemcpyHostToDevice, stream));
CUDA_CHECK(cudaMemcpyAsync(d_C, h_C.data(), lldc * loc_n_c * sizeof(double), cudaMemcpyHostToDevice, stream));
CUDA_CHECK(cudaMemcpyAsync(d_C_ref, h_C.data(), lldc * loc_n_c * sizeof(double), cudaMemcpyHostToDevice, stream));
CUBLASMP_CHECK(cublasMpGridCreate(
nprow,
npcol,
opts.grid_layout == 'c' ? CUBLASMP_GRID_LAYOUT_COL_MAJOR : CUBLASMP_GRID_LAYOUT_ROW_MAJOR,
comm,
&grid));
CUBLASMP_CHECK(
cublasMpMatrixDescriptorCreate(global_m_a, global_n_a, mbA, nbA, 0, 0, llda, CUDA_R_64F, grid, &descA));
CUBLASMP_CHECK(
cublasMpMatrixDescriptorCreate(global_m_c, global_n_c, mbC, nbC, 0, 0, lldc, CUDA_R_64F, grid, &descC));
CUBLASMP_CHECK(cublasMpTradd_bufferSize(
handle,
CUBLAS_FILL_MODE_LOWER,
CUBLAS_OP_N,
m,
n,
&alpha,
d_A,
ia,
ja,
descA,
&beta,
d_C,
ic,
jc,
descC,
&workspaceInBytesOnDevice,
&workspaceInBytesOnHost));
CUDA_CHECK(cudaMalloc(&d_work, workspaceInBytesOnDevice));
std::vector<int8_t> h_work(workspaceInBytesOnHost);
CUDA_CHECK(cudaStreamSynchronize(stream));
cudaEvent_t start, stop;
CUDA_CHECK(cudaEventCreate(&start));
CUDA_CHECK(cudaEventCreate(&stop));
for (int i = 0; i < opts.warmup + opts.cycles; i++)
{
if (i == opts.warmup)
{
CUDA_CHECK(cudaEventRecord(start, stream));
}
CUBLASMP_CHECK(cublasMpTradd(
handle,
CUBLAS_FILL_MODE_LOWER,
CUBLAS_OP_N,
m,
n,
&alpha,
d_A,
ia,
ja,
descA,
&beta,
d_C,
ic,
jc,
descC,
d_work,
workspaceInBytesOnDevice,
h_work.data(),
workspaceInBytesOnHost));
}
CUDA_CHECK(cudaEventRecord(stop, stream));
CUDA_CHECK(cudaEventSynchronize(stop));
float elapsed_ms;
CUDA_CHECK(cudaEventElapsedTime(&elapsed_ms, start, stop));
const double elapsed = (elapsed_ms / 1000.0) / opts.cycles;
if (rank == 0)
{
printf("Duration: %lf\n", elapsed);
}
CUDA_CHECK(cudaEventDestroy(start));
CUDA_CHECK(cudaEventDestroy(stop));
// Reset C to the clean input before the single verification call; the timing loop mutates C repeatedly.
CUDA_CHECK(cudaMemcpyAsync(d_C, d_C_ref, lldc * loc_n_c * sizeof(double), cudaMemcpyDeviceToDevice, stream));
CUBLASMP_CHECK(cublasMpTradd(
handle,
CUBLAS_FILL_MODE_LOWER,
CUBLAS_OP_N,
m,
n,
&alpha,
d_A,
ia,
ja,
descA,
&beta,
d_C,
ic,
jc,
descC,
d_work,
workspaceInBytesOnDevice,
h_work.data(),
workspaceInBytesOnHost));
const bool passed = !opts.check_result || check_tradd_result(
handle,
comm,
stream,
rank,
grid,
nprow,
npcol,
myprow,
mypcol,
CUBLAS_FILL_MODE_LOWER,
CUBLAS_OP_N,
m,
n,
&alpha,
d_A,
ia,
ja,
descA,
&beta,
d_C_ref,
d_C,
ic,
jc,
descC);
CUBLASMP_CHECK(cublasMpMatrixDescriptorDestroy(descA));
CUBLASMP_CHECK(cublasMpMatrixDescriptorDestroy(descC));
CUBLASMP_CHECK(cublasMpGridDestroy(grid));
CUBLASMP_CHECK(cublasMpDestroy(handle));
CUDA_CHECK(cudaFree(d_A));
CUDA_CHECK(cudaFree(d_C));
CUDA_CHECK(cudaFree(d_C_ref));
CUDA_CHECK(cudaFree(d_work));
NCCL_CHECK(ncclCommFinalize(comm));
NCCL_CHECK(ncclCommDestroy(comm));
CUDA_CHECK(cudaStreamDestroy(stream));
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
if (rank == 0)
{
printf(passed ? "[SUCCEEDED]\n" : "[FAILED]\n");
}
return passed ? EXIT_SUCCESS : EXIT_FAILURE;
};