-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathmp_laset.c
More file actions
407 lines (337 loc) · 13.5 KB
/
mp_laset.c
File metadata and controls
407 lines (337 loc) · 13.5 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
/*
* 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.
*/
/*
* cuSOLVERMp LASET sample
*
* Demonstrates cusolverMpLaset which initializes a distributed M-by-N matrix:
* - Off-diagonal elements in the specified triangular region are set to alpha
* - Diagonal elements are set to beta
*
* The sample:
* 1. Creates a distributed matrix filled with a sentinel value
* 2. Calls cusolverMpLaset with CUBLAS_FILL_MODE_FULL to set all elements
* 3. Gathers the result to rank 0 and verifies correctness
*
* Usage: mpirun -n 2 ./mp_laset
* mpirun -n 4 ./mp_laset -p 2 -q 2 -m 20 -n 15 -mbA 4 -nbA 4
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include <mpi.h>
#include <cusolverMp.h>
#include "helpers.h"
int main(int argc, char* argv[])
{
Options opts = { .m = 10,
.n = 10,
.nrhs = 1,
.mbA = 3,
.nbA = 3,
.mbB = 1,
.nbB = 1,
.mbQ = 1,
.nbQ = 1,
.ia = 1,
.ja = 1,
.ib = 1,
.jb = 1,
.iq = 1,
.jq = 1,
.p = 2,
.q = 1,
.grid_layout = 'C',
.verbose = false };
parse(&opts, argc, argv);
validate(&opts);
/* Initialize MPI library */
MPI_Init(NULL, NULL);
/* Matrix dimensions and offsets */
const int64_t m = opts.m;
const int64_t n = opts.n;
const int64_t ia = opts.ia;
const int64_t ja = opts.ja;
/* Tile sizes */
const int64_t mbA = opts.mbA;
const int64_t nbA = opts.nbA;
/* Process grid */
const int numRowDevices = opts.p;
const int numColDevices = opts.q;
const cusolverMpGridMapping_t gridLayout =
(opts.grid_layout == 'C' || opts.grid_layout == 'c' ? CUSOLVERMP_GRID_MAPPING_COL_MAJOR
: CUSOLVERMP_GRID_MAPPING_ROW_MAJOR);
/* Current implementation only allows RSRC,CSRC=(0,0) */
const uint32_t rsrca = 0;
const uint32_t csrca = 0;
/* Get rank id and rank size of the comm. */
int commSize, rank;
MPI_Comm_size(MPI_COMM_WORLD, &commSize);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) print(&opts);
/*
* Initialize device context for this process
*/
int localRank = getLocalRank();
cudaError_t cudaStat = cudaSetDevice(localRank);
assert(cudaStat == cudaSuccess);
cudaStat = cudaFree(0);
assert(cudaStat == cudaSuccess);
{
/* Error codes */
cusolverStatus_t cusolverStat = CUSOLVER_STATUS_SUCCESS;
ncclResult_t ncclStat = ncclSuccess;
cudaStat = cudaSetDevice(localRank);
assert(cudaStat == cudaSuccess);
/* Single process per device */
assert((numRowDevices * numColDevices) <= commSize);
/* Create NCCL communicator */
ncclUniqueId id;
if (rank == 0)
{
ncclGetUniqueId(&id);
}
MPI_Bcast((void*)&id, sizeof(id), MPI_BYTE, 0, MPI_COMM_WORLD);
ncclComm_t comm;
ncclStat = ncclCommInitRank(&comm, commSize, id, rank);
assert(ncclStat == ncclSuccess);
/* Create local stream */
cudaStream_t localStream = NULL;
cudaStat = cudaStreamCreate(&localStream);
assert(cudaStat == cudaSuccess);
/* Initialize cusolverMp library handle */
cusolverMpHandle_t cusolverMpHandle = NULL;
cusolverStat = cusolverMpCreate(&cusolverMpHandle, localRank, localStream);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
/* =========================================== */
/* CREATE GRID AND DESCRIPTORS */
/* =========================================== */
/* Global matrix dimensions: large enough to hold submatrix at (IA, JA) */
const int64_t lda = (ia - 1) + m;
const int64_t colsA = (ja - 1) + n;
cusolverMpGrid_t gridA = NULL;
cusolverStat =
cusolverMpCreateDeviceGrid(cusolverMpHandle, &gridA, comm, numRowDevices, numColDevices, gridLayout);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
cusolverMpMatrixDescriptor_t descA = NULL;
/* Compute process grid index */
int myRowRank, myColRank;
if (gridLayout == CUSOLVERMP_GRID_MAPPING_COL_MAJOR)
{
myRowRank = rank % numRowDevices;
myColRank = rank / numRowDevices;
}
else
{
myRowRank = rank / numColDevices;
myColRank = rank % numColDevices;
}
/* Compute local leading dimension */
const int64_t llda = cusolverMpNUMROC(lda, mbA, myRowRank, rsrca, numRowDevices);
const int64_t localColsA = cusolverMpNUMROC(colsA, nbA, myColRank, csrca, numColDevices);
cusolverStat = cusolverMpCreateMatrixDesc(&descA, gridA, CUDA_R_64F, lda, colsA, mbA, nbA, rsrca, csrca, llda);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
/* =========================================== */
/* ALLOCATE DISTRIBUTED MATRIX */
/* =========================================== */
void* d_A = NULL;
cudaStat = cudaMalloc(&d_A, llda * localColsA * sizeof(double));
assert(cudaStat == cudaSuccess);
/* Allocate device info */
int* d_info = NULL;
cudaStat = cudaMalloc((void**)&d_info, sizeof(int));
assert(cudaStat == cudaSuccess);
cudaStat = cudaMemset(d_info, 0, sizeof(int));
assert(cudaStat == cudaSuccess);
/* =========================================== */
/* INITIALIZE MATRIX WITH SENTINEL */
/* =========================================== */
/* Fill entire distributed matrix with a sentinel value (-999.0)
* so we can verify that LASET only touches the intended region. */
const double sentinel = -999.0;
/* Scatter a host matrix filled with sentinel to the distributed matrix */
double* h_sentinel_matrix = NULL;
if (rank == 0)
{
h_sentinel_matrix = (double*)malloc(lda * colsA * sizeof(double));
assert(h_sentinel_matrix != NULL);
for (int64_t i = 0; i < lda * colsA; i++)
{
h_sentinel_matrix[i] = sentinel;
}
}
cusolverStat = cusolverMpMatrixScatterH2D(
cusolverMpHandle, lda, colsA, d_A, 1, 1, descA, 0, h_sentinel_matrix, lda);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
cudaStat = cudaStreamSynchronize(localStream);
assert(cudaStat == cudaSuccess);
if (rank == 0)
{
free(h_sentinel_matrix);
h_sentinel_matrix = NULL;
}
/* =========================================== */
/* CALL cusolverMpLaset */
/* =========================================== */
/* Set off-diagonal to alpha=3.14, diagonal to beta=2.71.
* Alpha and beta pointers can reside on the host or the device;
* here we pass host pointers for simplicity. */
const double alpha = 3.14;
const double beta = 2.71;
cusolverStat = cusolverMpLaset(cusolverMpHandle,
CUBLAS_FILL_MODE_FULL,
m,
n,
&alpha, /* host or device pointer */
&beta, /* host or device pointer */
d_A,
ia,
ja,
descA,
d_info);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
/* Sync after LASET */
cudaStat = cudaStreamSynchronize(localStream);
assert(cudaStat == cudaSuccess);
/* Check info */
int h_info = 0;
cudaStat = cudaMemcpy(&h_info, d_info, sizeof(int), cudaMemcpyDeviceToHost);
assert(cudaStat == cudaSuccess);
assert(h_info == 0);
/* =========================================== */
/* GATHER RESULT AND VERIFY ON RANK 0 */
/* =========================================== */
double* h_A = NULL;
if (rank == 0)
{
h_A = (double*)malloc(lda * colsA * sizeof(double));
assert(h_A != NULL);
}
cusolverStat = cusolverMpMatrixGatherD2H(cusolverMpHandle, lda, colsA, d_A, 1, 1, descA, 0, h_A, lda);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
cudaStat = cudaStreamSynchronize(localStream);
assert(cudaStat == cudaSuccess);
if (rank == 0)
{
if (opts.verbose)
{
printf("Result matrix (global, column-major):\n");
for (int64_t i = 0; i < lda; i++)
{
for (int64_t j = 0; j < colsA; j++)
{
printf("%8.2f ", h_A[i + j * lda]);
}
printf("\n");
}
printf("\n");
}
/* Verify: check each element of the M-by-N submatrix at (IA, JA).
* With FULL mode: diagonal == beta, off-diagonal == alpha.
* Elements outside the submatrix should remain sentinel. */
int errors = 0;
for (int64_t j = 0; j < colsA; j++)
{
for (int64_t i = 0; i < lda; i++)
{
double val = h_A[i + j * lda];
/* Convert to 0-based submatrix coordinates */
int64_t si = i - (ia - 1);
int64_t sj = j - (ja - 1);
/* Check if (i,j) is inside the M-by-N submatrix */
int in_submatrix = (si >= 0 && si < m && sj >= 0 && sj < n);
double expected;
if (!in_submatrix)
{
expected = sentinel;
}
else if (si == sj)
{
expected = beta; /* diagonal */
}
else
{
expected = alpha; /* off-diagonal */
}
if (val != expected)
{
if (errors < 10)
{
printf("MISMATCH at (%ld, %ld): got %.4f, expected %.4f\n",
(long)(i + 1),
(long)(j + 1),
val,
expected);
}
errors++;
}
}
}
if (errors > 0)
{
printf("VERIFICATION FAILED: %d mismatches\n", errors);
}
else
{
printf("Verification passed: all %ld elements correct (%ld x %ld submatrix + %ld sentinel)\n",
(long)(lda * colsA),
(long)m,
(long)n,
(long)(lda * colsA - m * n));
}
}
/* =========================================== */
/* CLEAN UP */
/* =========================================== */
if (rank == 0)
{
free(h_A);
}
cusolverStat = cusolverMpDestroyMatrixDesc(descA);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
cusolverStat = cusolverMpDestroyGrid(gridA);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
if (d_A)
{
cudaStat = cudaFree(d_A);
assert(cudaStat == cudaSuccess);
}
if (d_info)
{
cudaStat = cudaFree(d_info);
assert(cudaStat == cudaSuccess);
}
cusolverStat = cusolverMpDestroy(cusolverMpHandle);
assert(cusolverStat == CUSOLVER_STATUS_SUCCESS);
cudaStat = cudaStreamSynchronize(localStream);
assert(cudaStat == cudaSuccess);
ncclStat = ncclCommDestroy(comm);
assert(ncclStat == ncclSuccess);
cudaStat = cudaStreamDestroy(localStream);
assert(cudaStat == cudaSuccess);
}
/* MPI barrier before MPI_Finalize */
MPI_Barrier(MPI_COMM_WORLD);
/* Finalize MPI environment */
MPI_Finalize();
if (rank == 0)
{
printf("[SUCCEEDED]\n");
}
return 0;
}