-
Notifications
You must be signed in to change notification settings - Fork 2
/
eigen_GPU_batch.cu
340 lines (303 loc) · 8.84 KB
/
eigen_GPU_batch.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
#define TIMER 0
#if defined(__NVCC__)
#define USE_IMTQL 1
#define DO_PREFETCH 1
#define DO_SORT 1
#endif
#if defined(__HIPCC__)
#define USE_IMTQL 0
#define DO_PREFETCH 0
#define DO_SORT 0
#endif
#include "gpu_arch.h"
//--
#include "misc_gpu.hpp"
#include "eigen_GPU_batch.hpp"
//--
#include "hhsy2tr.hpp"
#include "hhsy2tr_tiled.hpp"
#include "tred1_tiled.hpp"
//--
#include "imtql2.hpp"
#include "imtql2_tiled.hpp"
#include "tql2.hpp"
#include "tql2_tiled.hpp"
//--
#include "hhtr2sy.hpp"
#include "hhtr2sy_tiled.hpp"
//--
#include "jac_tiled.hpp"
//--
template <class T>
__global__ void
eigen_GPU_batch_n1_(const int L, const int nm, const int n, T *a_, T *w_)
{
const int pos = threadIdx.x+blockIdx.x*blockDim.x;
if (pos>=L) return;
const size_t len = nm*n;
T * a = a_ + pos*len;
T * w = w_ + pos;
const int step = blockDim.x*gridDim.x;
#pragma unroll
for (int id=pos; id<L; id+=step) {
*w = *a;
*a = static_cast<T>(1.0);
a += len*step; w += n*step;
}
}
template <class T, int tile_size>
__global__ void
eigen_GPU_batch_tiled_(const int L, const int nm, const int n, const int m, T *a_, T *w_, T *wk_, const int PRELOAD_SLOT)
{
const int pos = (threadIdx.x+blockIdx.x*blockDim.x)/tile_size;
// const int myid = threadIdx.x % tile_size;
// if (pos>=L) return;
const size_t len = (long)nm*n;
const int step = (blockDim.x*gridDim.x)/tile_size;
T * a, * w, * wk;
#if DO_PREFETCH
a = a_ + pos*len;
#pragma unroll 1
for (int id_=0; id_<PRELOAD_SLOT; id_+=step) {
int id = id_ + pos;
if ( id < L )
prefetch_mat_cg<T, tile_size> (nm*n, a);
a += len*step;
}
#endif
a = a_ + pos*len;
w = w_ + pos*m;
wk = wk_ + pos*len;
#pragma unroll 1
for (int id_=0; id_<L; id_+=step) {
const int id = id_ + pos;
const bool run = (id < L);
SYNC_IF_NEEDED();
#if DO_PREFETCH
if ( PRELOAD_SLOT > 0 && id+PRELOAD_SLOT*step < L )
prefetch_mat_cg<T, tile_size> (nm*n, a+PRELOAD_SLOT*len*step);
#endif
SYNC_IF_NEEDED();
#if 1
#if 1
if(run) tred1_tiled_<T, tile_size> (nm, n, a);
#else
const int mb = tile_size<=16?2:3; // 2*(mb+1) <= n
const int lenu = nm*mb+(4+(nm*mb)%2);
if (2*lenu<=n*nm) {
T * u_=wk+0*lenu; // mb*nm+max(BLK_J,BLK_K)
T * v_=wk+1*lenu; // mb*nm+max(BLK_J,BLK_K)
if(run) hhsy2tr_tiled_<T, tile_size> (nm, n, a, mb, u_, v_);
} else {
if(run) tred1_tiled_<T, tile_size> (nm, n, a);
}
#endif
int return_flag = false;
SYNC_IF_NEEDED();
#if USE_IMTQL
if(run) return_flag = imtql2_tiled_<T, tile_size> (nm, n, w, wk);
#else
if(run) return_flag = tql2_tiled_<T, tile_size> (nm, n, w, wk);
#endif
SYNC_IF_NEEDED();
if(run) if ( !return_flag ) hhtr2sy_tiled_<T, tile_size> (nm, n, a, wk);
#else
if(run) jac_tiled<T, tile_size> ( a, wk, nm, n, w );
#endif
a += len*step; w += n*step;
}
}
template <class T>
__global__ void
eigen_GPU_batch_(const int L, const int nm, const int n, const int m, T *a_, T *w_, T *e_, T * wk_, const int PRELOAD_SLOT)
{
const int pos = (threadIdx.x+blockIdx.x*blockDim.x)/WARP_GPU_SIZE;
#if TIMER
const int myid = (threadIdx.x % WARP_GPU_SIZE);
#endif
if (pos>=L) return;
const size_t len = nm*n;
const int step = (blockDim.x*gridDim.x)/WARP_GPU_SIZE;
T * a, * w, * wk, * e;
#if DO_PREFETCH
a = a_ + pos*len;
#pragma unroll 1
for (int id_=0; id_<PRELOAD_SLOT; id_+=step) {
int id = id_ + pos;
if ( id < L )
prefetch_mat_cg<T, WARP_GPU_SIZE> (nm*n, a);
a += len*step;
}
#endif
#if TIMER
unsigned long t0, t1, t2, t3;
#endif
a = a_ + pos*len;
w = w_ + pos*n;
wk = wk_ + pos*len;
e = e_ + pos*n;
#pragma unroll 1
for (int id_=0; id_<L; id_+=step) {
const int id = id_ + pos;
const bool run = (id < L);
#if DO_PREFETCH
if ( PRELOAD_SLOT > 0 && id+PRELOAD_SLOT*step < L )
prefetch_mat_cg<T, WARP_GPU_SIZE> (nm*n, a+PRELOAD_SLOT*len*step);
#endif
#if TIMER
t0 = __global_timer__();
#endif
// const int mb=3; // 3*(mb+1) <= n+1 && 2*mb <= WARP_GPU_SIZE
const int mb=4; // 3*(mb+1) <= n+1 && 2*mb <= WARP_GPU_SIZE
T * u_=wk+0*nm*(mb+1); // mb*nm+max(BLK_J,BLK_K)
T * v_=wk+1*nm*(mb+1); // mb*nm+max(BLK_J,BLK_K)
if(run) hhsy2tr_ (nm, n, a, w, e, mb, u_, v_);
#if TIMER
t1 = __global_timer__();
#endif
int return_flag = false;
#if USE_IMTQL
if(run) return_flag = imtql2_ (nm, n, w, e, wk);
#else
if(run) return_flag = tql2_ (nm, n, w, e, wk);
#endif
if ( return_flag ) break;
#if TIMER
t2 = __global_timer__();
#endif
if(run) hhtr2sy_ (nm, n, a, m, wk
#if DO_SORT
, (int*)e
#endif
);
#if TIMER
t3 = __global_timer__();
#endif
a += len*step; w += n*step;
}
#if TIMER
if (pos==0 && myid == 0) {
double t;
t = (double)(t1-t0);
printf("TRED1 : %le [ns]: %le [GFLOPS]: %le [GB/s]\n",
t, 4.*n*n*n/3/t, (double)sizeof(T)*n*n/2/t);
t = (double)(t2-t1);
printf("TQL2 : %le [ns]: %le [GFLOPS]: %le [GB/s]\n",
t, 0., (double)sizeof(T)*n*(n+2)/t);
t = (double)(t3-t2);
printf("TRBAK1 : %le [ns]: %le [GFLOPS]: %le [GB/s]\n",
t, 2.*n*n*n/t, (double)sizeof(T)*3*n*n/2/t);
}
#endif
}
template <class T>
__host__ gpuError_t
eigen_GPU_batch_RUN(const int L, const int nm, const int n, const int m, T * a, T * w, T * wk_, const gpuStream_t stream)
{
if (L<1||nm<n||n<1||m<1||m>n) return gpuErrorInvalidValue;
gpuError_t err_code;
int grpSZ=0, numTB=0, numGR=0, numTH=0, sizeSH=0, sizeL2=0;
err_code = eigen_GPU_batch_get_Launch_params<T>(L, n, grpSZ, numTB, numGR, numTH, sizeSH, sizeL2);
if (err_code != gpuSuccess) return err_code;
#if DO_PREFETCH
const int len = nm*n;
const int step = (numTH*numTB)/grpSZ;
const int ELE = sizeof(T)*len*step;
// const int lwk = sizeof(T)*(n+(n*nm))*min(numTB*numGR,L);
// const int PRELOAD_SLOT = max(sizeL2-lwk,0)/ELE;
const int PRELOAD_SLOT = min(L,(sizeL2-1)/ELE);
#else
const int PRELOAD_SLOT = 0;
#endif
if (n == 1) {
#if defined(__NVCC__)
cudaFuncSetAttribute( eigen_GPU_batch_n1_ <T>,
cudaFuncAttributeMaxDynamicSharedMemorySize, 16*1024 );
#endif
eigen_GPU_batch_n1_ <T> <<<numTB, numTH, 0, stream>>> (L, nm, n, a, w);
} if ( n <= WARP_GPU_SIZE ) {
switch ( grpSZ ) {
case 4:
#if defined(__NVCC__)
cudaFuncSetAttribute( eigen_GPU_batch_tiled_ <T,4>,
cudaFuncAttributeMaxDynamicSharedMemorySize, 16*1024 );
#endif
eigen_GPU_batch_tiled_ <T, 4> <<<numTB, numTH, sizeSH, stream>>> (L, nm, n, m, a, w, wk_, PRELOAD_SLOT);
break;
case 8:
#if defined(__NVCC__)
cudaFuncSetAttribute( eigen_GPU_batch_tiled_ <T,8>,
cudaFuncAttributeMaxDynamicSharedMemorySize, 16*1024 );
#endif
eigen_GPU_batch_tiled_ <T, 8> <<<numTB, numTH, sizeSH, stream>>> (L, nm, n, m, a, w, wk_, PRELOAD_SLOT);
break;
case 16:
#if defined(__NVCC__)
cudaFuncSetAttribute( eigen_GPU_batch_tiled_ <T,16>,
cudaFuncAttributeMaxDynamicSharedMemorySize, 16*1024 );
#endif
eigen_GPU_batch_tiled_ <T, 16> <<<numTB, numTH, sizeSH, stream>>> (L, nm, n, m, a, w, wk_, PRELOAD_SLOT);
break;
case 32:
#if defined(__NVCC__)
default:
cudaFuncSetAttribute( eigen_GPU_batch_tiled_ <T,32>,
cudaFuncAttributeMaxDynamicSharedMemorySize, 16*1024 );
#endif
eigen_GPU_batch_tiled_ <T, 32> <<<numTB, numTH, sizeSH, stream>>> (L, nm, n, m, a, w, wk_, PRELOAD_SLOT);
break;
#if WARP_GPU_SIZE==64
case 64:
default:
eigen_GPU_batch_tiled_ <T, 64> <<<numTB, numTH, sizeSH, stream>>> (L, nm, n, m, a, w, wk_, PRELOAD_SLOT);
break;
#endif
}
} else {
T *e = wk_;
T *wk = e + n*min(numTB*numGR,L);
#if defined(__NVCC__)
cudaFuncSetAttribute( eigen_GPU_batch_ <T>,
cudaFuncAttributeMaxDynamicSharedMemorySize, 16*1024 );
#endif
eigen_GPU_batch_ <T> <<<numTB, numTH, sizeSH, stream>>> (L, nm, n, m, a, w, e, wk, PRELOAD_SLOT);
}
return gpuSuccess;
}
extern "C" {
__host__ void
eigen_GPU_batch_DP(const int L, const int nm, const int n, const int m, double * a, double * w, double *wk, const gpuStream_t stream)
{
if ( wk == NULL ) {
if ( n > 1 ) {
size_t lwork;
eigen_GPU_batch_BufferSize <double>(L, nm, n, m, NULL, NULL, &lwork);
*((size_t *)w) = lwork;
} else {
*((int64_t *)w) = 0;
}
} else {
eigen_GPU_batch_RUN <double>(L, nm, n, m, a, w, wk, stream);
}
}
__host__ void
eigen_GPU_batch_FP(const int L, const int nm, const int n, const int m, float * a, float * w, float *wk, const gpuStream_t stream)
{
if ( wk == NULL ) {
if ( n > 1 ) {
size_t lwork;
eigen_GPU_batch_BufferSize <float>(L, nm, n, m, NULL, NULL, &lwork);
*((size_t *)w) = lwork;
} else {
*((int32_t *)w) = 0;
}
} else {
eigen_GPU_batch_RUN <float>(L, nm, n, m, a, w, wk, stream);
}
}
//__host__ void
//eigen_GPU_batch_HP(const int L, const int nm, const int n, const int m, half * a, half * w, half *wk)
//{
// eigen_GPU_batch_RUN <half>(L, nm, n, m, a, w, wk);
//}
}