-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlu_decomposition_cusolver.cu
260 lines (203 loc) · 10 KB
/
lu_decomposition_cusolver.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
// 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 <cassert>
#include <cstdio>
#include <stdexcept>
#include <string>
#include <cuComplex.h>
#include <curand.h>
#include <cusolverDn.h>
#include "utilities.h"
#define VERIFY 0
template<typename T>
void SingleGPUManaged( const int &device,
const int &loops,
const int &algo,
const int &N,
const int &lda,
const int &ldb,
T * A,
T * B ) {
size_t sizeBytesA { sizeof( T ) * lda * N };
size_t sizeBytesB { sizeof( T ) * N };
#if VERIFY
T *B_input {};
T *A_input {};
std::printf( "Allocating space for verification\n" );
CUDA_RT_CALL( cudaMallocManaged( &A_input, sizeBytesA ) );
CUDA_RT_CALL( cudaMallocManaged( &B_input, sizeBytesB ) );
CUDA_RT_CALL( cudaMemPrefetchAsync( A_input, sizeBytesA, cudaCpuDeviceId, NULL ) );
CUDA_RT_CALL( cudaMemPrefetchAsync( B_input, sizeBytesB, cudaCpuDeviceId, NULL ) );
std::printf( "Copy A to A_input: Needed for verification\n" );
CUDA_RT_CALL( cudaMemcpy( A_input, A, sizeBytesA, cudaMemcpyDeviceToHost ) );
std::printf( "Copy B to B_input: Needed for verification\n" );
CUDA_RT_CALL( cudaMemcpy( B_input, B, sizeBytesB, cudaMemcpyDeviceToHost ) );
#endif
CUDA_RT_CALL( cudaMemPrefetchAsync( A, sizeBytesA, device, NULL ) );
CUDA_RT_CALL( cudaMemPrefetchAsync( B, sizeBytesB, device, NULL ) );
// 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 ) );
std::printf( "Pivot is on : compute P*A = L*U\n" );
/* step 1: create cusolver handle, bind a stream */
cusolverDnHandle_t cusolverH { nullptr };
CUDA_RT_CALL( cusolverDnCreate( &cusolverH ) );
// Create stream
cudaStream_t stream {};
CUDA_RT_CALL( cudaStreamCreate( &stream ) );
CUDA_RT_CALL( cusolverDnSetStream( cusolverH, stream ) );
/* step 2: copy A to device */
int *d_info { nullptr }; /* error info */
CUDA_RT_CALL( cudaMallocManaged( &d_info, sizeof( int ) ) );
int64_t *d_Ipiv { nullptr }; /* pivoting sequence */
CUDA_RT_CALL( cudaMallocManaged( &d_Ipiv, sizeof( int64_t ) * N ) );
CUDA_RT_CALL( cudaMemPrefetchAsync( d_Ipiv, sizeof( int64_t ) * N, device, stream ) );
void *bufferOnDevice { nullptr };
void *bufferOnHost { nullptr };
size_t workspaceInBytesOnDevice {};
size_t workspaceInBytesOnHost {};
CUDA_RT_CALL( cusolverDnXgetrf_bufferSize(
cusolverH, NULL, N, N, CUDA_C_64F, A, lda, CUDA_C_64F, &workspaceInBytesOnDevice, &workspaceInBytesOnHost ) );
CheckMemoryUsed( 1 );
std::printf( "\nAllocate device workspace, lwork = %lu\n", workspaceInBytesOnDevice );
std::printf( "Allocate host workspace, lwork = %lu\n\n", workspaceInBytesOnHost );
CUDA_RT_CALL( cudaMallocManaged( &bufferOnDevice, workspaceInBytesOnDevice ) );
CUDA_RT_CALL( cudaMemPrefetchAsync( bufferOnDevice, workspaceInBytesOnDevice, device, stream ) );
CUDA_RT_CALL(
cudaMemAdvise( bufferOnDevice, workspaceInBytesOnDevice, cudaMemAdviseSetPreferredLocation, device ) );
CUDA_RT_CALL( cudaMemAdvise( bufferOnDevice, workspaceInBytesOnDevice, cudaMemAdviseSetAccessedBy, device ) );
if ( 0 < workspaceInBytesOnHost ) {
CUDA_RT_CALL( cudaMallocManaged( &bufferOnHost, workspaceInBytesOnHost ) );
CUDA_RT_CALL( cudaMemPrefetchAsync( bufferOnHost, workspaceInBytesOnHost, cudaCpuDeviceId, NULL ) );
assert( NULL != bufferOnHost );
}
// Create advanced params
cusolverDnParams_t params;
CUDA_RT_CALL( cusolverDnCreateParams( ¶ms ) );
if ( algo == 0 ) {
std::printf( "Using New Algo\n" );
CUDA_RT_CALL( cusolverDnSetAdvOptions( params, CUSOLVERDN_GETRF, CUSOLVER_ALG_0 ) );
} else {
std::printf( "Using Legacy Algo\n" );
CUDA_RT_CALL( cusolverDnSetAdvOptions( params, CUSOLVERDN_GETRF, CUSOLVER_ALG_1 ) );
}
// Check GPU memory used on single GPU
CheckMemoryUsed( 1 );
CUDA_RT_CALL( cudaMemAdvise( A, sizeBytesA, cudaMemAdviseSetPreferredLocation, device ) );
CUDA_RT_CALL( cudaMemAdvise( A, sizeBytesA, cudaMemAdviseSetAccessedBy, device ) );
CUDA_RT_CALL( cudaMemAdvise( B, sizeBytesB, cudaMemAdviseSetPreferredLocation, device ) );
CUDA_RT_CALL( cudaMemAdvise( B, sizeBytesB, cudaMemAdviseSetAccessedBy, device ) );
std::printf( "\nRunning GETRF\n" );
CUDA_RT_CALL( cudaEventRecord( startEvent ) );
for ( int i = 0; i < loops; i++ ) {
/* step 4: LU factorization */
CUDA_RT_CALL( cusolverDnXgetrf( cusolverH,
params,
static_cast<int64_t>( N ),
static_cast<int64_t>( N ),
CUDA_C_64F,
A,
static_cast<int64_t>( lda ),
d_Ipiv,
CUDA_C_64F,
bufferOnDevice,
workspaceInBytesOnDevice,
bufferOnHost,
workspaceInBytesOnHost,
d_info ) );
// Must be here to retrieve d_info
CUDA_RT_CALL( cudaStreamSynchronize( stream ) );
if ( *d_info ) {
throw std::runtime_error( std::to_string( -*d_info ) + "-th parameter is wrong (cusolverDnDgetrf) \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
CUDA_RT_CALL( cudaMemPrefetchAsync( B, sizeBytesB, cudaCpuDeviceId, stream ) );
// Calculate Residual Error
CalculateResidualError( N,
lda,
reinterpret_cast<double *>( A_input ),
reinterpret_cast<double *>( B_input ),
reinterpret_cast<double *>( B ) );
#endif
CUDA_RT_CALL( cudaFree( d_Ipiv ) );
CUDA_RT_CALL( cudaFree( d_info ) );
CUDA_RT_CALL( cudaFree( bufferOnDevice ) );
CUDA_RT_CALL( cudaFree( bufferOnHost ) );
CUDA_RT_CALL( cusolverDnDestroy( cusolverH ) );
CUDA_RT_CALL( cudaStreamDestroy( stream ) );
CUDA_RT_CALL( cudaEventDestroy( startEvent ) );
CUDA_RT_CALL( cudaEventDestroy( stopEvent ) );
#if VERIFY
CUDA_RT_CALL( cudaFree( A_input ) );
CUDA_RT_CALL( cudaFree( B_input ) );
#endif
}
int main( int argc, char *argv[] ) {
int m {};
int loops {};
int algo {};
if ( argc < 4 ) {
m = 512;
loops = 5;
algo = 0;
} else {
m = std::atoi( argv[1] );
loops = std::atoi( argv[2] );
algo = std::atoi( argv[3] );
if ( algo > 1 || algo < 0 )
algo = 1;
}
int device = -1;
CUDA_RT_CALL( cudaGetDevice( &device ) );
std::printf( "\ncuSOLVER: SingleGPUManaged GETRF: N = %d\n\n", m );
const int lda { m };
const int ldb { m };
using data_type = cuDoubleComplex;
data_type *m_A {};
data_type *m_B {};
size_t sizeA { static_cast<size_t>( lda ) * m };
size_t sizeB { 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 ) );
// 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\n" );
SingleGPUManaged( device, 1, algo, m, lda, ldb, m_A, m_B );
std::printf( "\n\n******************************************\n" );
std::printf( "Run LU Decomposition\n" );
SingleGPUManaged( device, loops, algo, m, lda, ldb, m_A, m_B );
return ( EXIT_SUCCESS );
}