-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlu_decomposition_cusolvermg.cu
355 lines (292 loc) · 13.9 KB
/
lu_decomposition_cusolvermg.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
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written
// permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstdio>
#include <stdexcept>
#include <string>
#include <curand.h>
#include <cusolverDn.h>
#include <cusolverMg.h>
#include "utilities.h"
#define VERIFY 0
template<typename T>
void MultiGPU( const int &num_devices,
int * device_list,
const int &loops,
const int &N,
const int &lda,
const int &ldb,
T * A,
T * B,
T * X ) {
// Start timer
cudaEvent_t startEvent { nullptr };
cudaEvent_t stopEvent { nullptr };
float elapsed_gpu_ms {};
CUDA_RT_CALL( cudaEventCreate( &startEvent, cudaEventBlockingSync ) );
CUDA_RT_CALL( cudaEventCreate( &stopEvent, cudaEventBlockingSync ) );
cusolverMgHandle_t cusolverMgH { nullptr };
const int IA { 1 };
const int JA { 1 };
const int T_A { 256 }; /* tile size of A */
const int IB { 1 };
const int JB { 1 };
const int T_B { 100 }; /* tile size of B */
int info {};
cudaLibMgMatrixDesc_t descrA;
cudaLibMgMatrixDesc_t descrB;
cudaLibMgGrid_t gridA;
cudaLibMgGrid_t gridB;
cusolverMgGridMapping_t mapping = CUDALIBMG_GRID_MAPPING_COL_MAJOR;
std::printf( "\nCreate Mg handle and select devices\n" );
CUDA_RT_CALL( cusolverMgCreate( &cusolverMgH ) );
CUDA_RT_CALL( cusolverMgDeviceSelect( cusolverMgH, num_devices, device_list ) );
std::printf( "\nCreate matrix descriptors for A and B\n" );
CUDA_RT_CALL( cusolverMgCreateDeviceGrid( &gridA, 1, num_devices, device_list, mapping ) );
CUDA_RT_CALL( cusolverMgCreateDeviceGrid( &gridB, 1, num_devices, device_list, mapping ) );
/* (global) A is N-by-N */
CUDA_RT_CALL( cusolverMgCreateMatrixDesc( &descrA,
N, /* number of rows of (global) A */
N, /* number of columns of (global) A */
N, /* number or rows in a tile */
T_A, /* number of columns in a tile */
CUDA_C_64F,
gridA ) );
/* (global) B is N-by-1 */
CUDA_RT_CALL( cusolverMgCreateMatrixDesc( &descrB,
N, /* number of rows of (global) B */
1, /* number of columns of (global) B */
N, /* number or rows in a tile */
T_B, /* number of columns in a tile */
CUDA_C_64F,
gridB ) );
std::printf( "\nAllocate distributed matrices A, B and IPIV\n" );
std::vector<T *> array_d_A( num_devices );
std::vector<T *> array_d_B( num_devices );
std::vector<int *> array_d_IPIV( num_devices );
/* A := 0 */
std::printf( "Create A\n" );
CreateMat( num_devices,
device_list,
N, /* number of columns of global A */
T_A, /* number of columns per column tile */
lda, /* leading dimension of local A */
array_d_A.data( ) );
/* B := 0 */
std::printf( "Create B\n" );
CreateMat( num_devices,
device_list,
1, /* number of columns of global B */
T_B, /* number of columns per column tile */
ldb, /* leading dimension of local B */
array_d_B.data( ) );
/* IPIV := 0, IPIV is consistent with A */
std::printf( "Create IPIV\n" );
CreateMat( num_devices,
device_list,
N, /* number of columns of global IPIV */
T_A, /* number of columns per column tile */
1, /* leading dimension of local IPIV */
array_d_IPIV.data( ) );
std::printf( "\nPrepare data on devices\n" );
/* distribute A to array_d_A */
std::printf( "Copy A\n" );
MemcpyH2D( num_devices,
device_list,
N,
N, /* input */
A,
lda, /* output */
N, /* number of columns of global A */
T_A, /* number of columns per column tile */
lda, /* leading dimension of local A */
array_d_A.data( ), /* host pointer array of dimension num_devices */
IA,
JA );
/* distribute B to array_d_B */
std::printf( "Copy B\n" );
MemcpyH2D( num_devices,
device_list,
N,
1, /* input */
B,
ldb, /* output */
1, /* number of columns of global B */
T_B, /* number of columns per column tile */
ldb, /* leading dimension of local B */
array_d_B.data( ), /* host pointer array of dimension num_devices */
IB,
JB );
std::printf( "\nAllocate workspace space\n" );
int64_t lwork_getrf {};
int64_t lwork_getrs {};
int64_t lwork {}; /* workspace: number of elements per device */
CUDA_RT_CALL( cusolverMgGetrf_bufferSize( cusolverMgH,
N,
N,
reinterpret_cast<void **>( array_d_A.data( ) ),
IA, /* base-1 */
JA, /* base-1 */
descrA,
array_d_IPIV.data( ),
CUDA_C_64F,
&lwork_getrf ) );
CUDA_RT_CALL( cusolverMgGetrs_bufferSize( cusolverMgH,
CUBLAS_OP_N,
N,
1, /* NRHS */
reinterpret_cast<void **>( array_d_A.data( ) ),
IA,
JA,
descrA,
array_d_IPIV.data( ),
reinterpret_cast<void **>( array_d_B.data( ) ),
IB,
JB,
descrB,
CUDA_C_64F,
&lwork_getrs ) );
lwork = ( lwork_getrf > lwork_getrs ) ? lwork_getrf : lwork_getrs;
std::printf( "\tAllocate device workspace, lwork: %lu (bytes)\n\n", lwork * sizeof( int64_t ) );
std::vector<T *> array_d_work( num_devices );
/* array_d_work[j] points to device workspace of device j */
WorkspaceAlloc( num_devices,
device_list,
sizeof( int64_t ) * lwork, /* number of bytes per device */
array_d_work.data( ) );
CUDA_RT_CALL( cudaDeviceSynchronize( ) ); /* sync all devices */
CheckMemoryUsed( num_devices );
std::printf( "\nRunning GETRF\n" );
CUDA_RT_CALL( cudaEventRecord( startEvent ) );
for ( int i = 0; i < loops; i++ ) {
CUDA_RT_CALL( cusolverMgGetrf( cusolverMgH,
N,
N,
reinterpret_cast<void **>( array_d_A.data( ) ),
IA,
JA,
descrA,
array_d_IPIV.data( ),
CUDA_C_64F,
reinterpret_cast<void **>( array_d_work.data( ) ),
lwork,
&info /* host */ ) );
CUDA_RT_CALL( cudaDeviceSynchronize( ) ); /* sync all devices */
if ( info ) {
throw std::runtime_error( std::to_string( -info ) + "-th parameter is wrong (cusolverMgGetrf) \n" );
}
}
// Stop timer
CUDA_RT_CALL( cudaEventRecord( stopEvent ) );
CUDA_RT_CALL( cudaEventSynchronize( stopEvent ) );
CUDA_RT_CALL( cudaEventElapsedTime( &elapsed_gpu_ms, startEvent, stopEvent ) );
double avg { elapsed_gpu_ms / loops };
double flops { FLOPS_ZGETRF( N, N ) };
double perf { 1e-9 * flops / avg };
std::printf( "\nRuntime = %0.2f ms (avg over %d runs) : @ %0.2f GFLOPs\n\n", avg, loops, perf );
#if VERIFY
std::printf( "Retrieve solution vector X\n" );
MemcpyD2H( num_devices,
device_list,
N,
1, /* input */
1, /* number of columns of global B */
T_B, /* number of columns per column tile */
ldb, /* leading dimension of local B */
array_d_B.data( ),
IB,
JB, /* output */
X, /* N-by-1 */
ldb );
// Calculate Residual Error
CalculateResidualError( N, lda, A, B, X );
#endif
std::printf( "Free resources\n" );
CUDA_RT_CALL( cudaEventDestroy( startEvent ) );
CUDA_RT_CALL( cudaEventDestroy( stopEvent ) );
// CUDA_RT_CALL( cusolverMgDestroy( cusolverMgH ) );
CUDA_RT_CALL( cusolverMgDestroyMatrixDesc( descrA ) );
CUDA_RT_CALL( cusolverMgDestroyMatrixDesc( descrB ) );
CUDA_RT_CALL( cusolverMgDestroyGrid( gridA ) );
CUDA_RT_CALL( cusolverMgDestroyGrid( gridB ) );
DestroyMat( num_devices,
device_list,
N, /* number of columns of global A */
T_A, /* number of columns per column tile */
array_d_A.data( ) );
DestroyMat( num_devices,
device_list,
1, /* number of columns of global B */
T_B, /* number of columns per column tile */
array_d_B.data( ) );
DestroyMat( num_devices,
device_list,
N, /* number of columns of global IPIV */
T_A, /* number of columns per column tile */
array_d_IPIV.data( ) );
WorkspaceFree( num_devices, device_list, array_d_work.data( ) );
}
int main( int argc, char *argv[] ) {
int m {};
int loops {};
int ngpu {};
if ( argc < 4 ) {
m = 512;
loops = 5;
ngpu = 1;
} else {
m = std::atoi( argv[1] );
loops = std::atoi( argv[2] );
ngpu = std::atoi( argv[3] );
}
// Setup for MultiGPU version
std::vector<int> device_list( ngpu );
std::printf( "\ncuSOLVERMg: MultiGPU GETRF: N = %d\n\n", m );
GetDeviceProperties( ngpu, device_list.data( ) );
std::printf( "\ncuSolverMg: MultiGPU GETRF w/ %d GPUs: N = %d\n\n", ngpu, m );
std::printf( "Enable peer access\n" );
EnablePeerAccess( ngpu );
const int lda { m };
const int ldb { m };
using data_type = cuDoubleComplex;
data_type *m_A {};
data_type *m_B {};
data_type *m_X {};
size_t sizeA { static_cast<size_t>( lda ) * m };
size_t sizeB { static_cast<size_t>( m ) };
size_t sizeX { static_cast<size_t>( m ) };
CUDA_RT_CALL( cudaMallocManaged( &m_A, sizeof( data_type ) * sizeA ) );
CUDA_RT_CALL( cudaMallocManaged( &m_B, sizeof( data_type ) * sizeB ) );
CUDA_RT_CALL( cudaMallocManaged( &m_X, sizeof( data_type ) * sizeX ) );
// Generate random numbers on the GPU
// Convert to double and double the number of items for cuRand
CreateRandomData( "A", sizeA * 2, reinterpret_cast<double *>( m_A ) );
CreateRandomData( "B", sizeB * 2, reinterpret_cast<double *>( m_B ) );
CUDA_RT_CALL( cudaDeviceSynchronize( ) );
// Managed Memory
std::printf( "\n\n******************************************\n" );
std::printf( "Run Warmup w/ %d GPUs\n", ngpu );
MultiGPU( ngpu, device_list.data( ), 1, m, lda, ldb, m_A, m_B, m_X );
std::printf( "\n\n******************************************\n" );
std::printf( "Run LU Decomposition w/ %d GPUs\n", ngpu );
MultiGPU( ngpu, device_list.data( ), loops, m, lda, ldb, m_A, m_B, m_X );
CUDA_RT_CALL( cudaFree( m_A ) );
CUDA_RT_CALL( cudaFree( m_B ) );
CUDA_RT_CALL( cudaFree( m_X ) );
return ( EXIT_SUCCESS );
}