File: lfc-reduce.cpp

package info (click to toggle)
gpaw 25.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,888 kB
  • sloc: python: 174,804; ansic: 17,564; cpp: 5,668; sh: 972; csh: 139; makefile: 45
file content (412 lines) | stat: -rw-r--r-- 14,725 bytes parent folder | download | duplicates (2)
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
#ifndef REDUCE_LFC

#define REDUCE_LFC_MAX_THREADS  (64)
#define REDUCE_LFC_MAX_THREADS2 (64)
#define REDUCE_LFC_MAX_BLOCKS   (32)
#define REDUCE_LFC_MAX_BLOCKS2  (32)
#define REDUCE_LFC_MAX_YBLOCKS  (65535)
#define REDUCE_LFC_BUFFER_SIZE  ((2 * GPU_BLOCKS_MAX \
                                 * MAX(REDUCE_LFC_MAX_BLOCKS, \
                                       REDUCE_LFC_MAX_BLOCKS2)) * 16)

static void *lfc_reduce_buffer = NULL;
static int lfc_reduce_buffer_size = 0;

extern "C"
void lfc_reduce_init_buffers_gpu()
{
    lfc_reduce_buffer = NULL;
    lfc_reduce_buffer_size = 0;
}

extern "C"
void lfc_reduce_dealloc_gpu()
{
    gpuFree(lfc_reduce_buffer);
    gpuCheckLastError();
    lfc_reduce_init_buffers_gpu();
}

static void lfc_reduceNumBlocksAndThreads(int n, int *blocks, int *threads)
{
    *threads = (n < REDUCE_LFC_MAX_THREADS) ? nextPow2(n)
                                            : REDUCE_LFC_MAX_THREADS;
    *blocks = MIN((n + (*threads - 1)) / (*threads), REDUCE_LFC_MAX_BLOCKS);
}

static void lfc_reduceNumBlocksAndThreads2(int n,int *blocks, int *threads)
{
    *threads = (n < REDUCE_LFC_MAX_THREADS2 * 2) ? nextPow2((n + 1) / 2)
                                                 : REDUCE_LFC_MAX_THREADS2;
    *blocks = MIN((n + (*threads * 2 - 1)) / (*threads * 2),
                  REDUCE_LFC_MAX_BLOCKS2);
}

#endif
#define REDUCE_LFC

#define INNAME(f) Zgpu(f ## _map512)
#define REDUCE_LFC_THREADS  512
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map256)
#define REDUCE_LFC_THREADS  256
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map128)
#define REDUCE_LFC_THREADS  128
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map64)
#define REDUCE_LFC_THREADS  64
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map32)
#define REDUCE_LFC_THREADS  32
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map16)
#define REDUCE_LFC_THREADS  16
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map8)
#define REDUCE_LFC_THREADS  8
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map4)
#define REDUCE_LFC_THREADS  4
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map2)
#define REDUCE_LFC_THREADS  2
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## _map1)
#define REDUCE_LFC_THREADS  1
#include "lfc-reduce-kernel.cpp"
#undef  REDUCE_LFC_THREADS
#undef  INNAME

#undef  INFUNC
#undef  REDUCE_THREADS

#define INFUNC(a,b) (a)
#define INNAME(f) Zgpu(f ## 512)
#define REDUCE_THREADS  512
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 256)
#define REDUCE_THREADS  256
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 128)
#define REDUCE_THREADS  128
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 64)
#define REDUCE_THREADS  64
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 32)
#define REDUCE_THREADS  32
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 16)
#define REDUCE_THREADS  16
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 8)
#define REDUCE_THREADS  8
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 4)
#define REDUCE_THREADS  4
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 2)
#define REDUCE_THREADS  2
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME

#define INNAME(f) Zgpu(f ## 1)
#define REDUCE_THREADS  1
#include "reduce-kernel.cpp"
#undef  REDUCE_THREADS
#undef  INNAME
#undef  INFUNC


void Zgpu(lfc_reducemap)(LFCObject *lfc, const Tgpu *a_G, int nG,
        Tgpu *c_xM, int nM, int nvec, int q)
{
    int blocks, threads;

    if (lfc_reduce_buffer_size < nM * REDUCE_LFC_BUFFER_SIZE) {
        lfc_reduce_dealloc_gpu();
        gpuMalloc(&lfc_reduce_buffer, nM * REDUCE_LFC_BUFFER_SIZE);
        lfc_reduce_buffer_size = nM * REDUCE_LFC_BUFFER_SIZE;
    }
    lfc_reduceNumBlocksAndThreads(lfc->max_len_A_gm, &blocks, &threads);

    int min_wsize = blocks * nM;
    int work_buffer_size = (lfc_reduce_buffer_size / sizeof(Tgpu)) / 2;

    assert(min_wsize < work_buffer_size);

    int mynvec = MAX(MIN(work_buffer_size / min_wsize, nvec), 1);

    mynvec = MIN(mynvec, (REDUCE_LFC_MAX_YBLOCKS) / nM);

    Tgpu *work_buffer1 = (Tgpu*) lfc_reduce_buffer;
    Tgpu *work_buffer2 = work_buffer1 + work_buffer_size;
    Tgpu *result_gpu = c_xM;

    int smemSize = (threads <= 32) ? 2 * threads * sizeof(Tgpu)
                                   : threads * sizeof(Tgpu);

    for (int i=0; i < nvec; i += mynvec) {
        int cunvec = MIN(mynvec, nvec - i);
        int innvec = 1;

        dim3 dimBlock(threads, 1, 1);
        dim3 dimGrid(blocks, lfc->Mcount, 1);
        int block_out = blocks;

        innvec = cunvec;

        switch (threads) {
            case 512:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map512),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case 256:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map256),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case 128:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map128),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case 64:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map64),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case 32:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map32),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case 16:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map16),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case  8:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map8),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case  4:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map4),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case  2:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map2),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG,lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            case  1:
                gpuLaunchKernel(
                        Zgpu(integrate_mul_kernel_map1),
                        dimGrid, dimBlock, smemSize, 0,
                        a_G + i * nG, nG, lfc->volume_W_gpu,
                        lfc->volume_WMi_gpu, lfc->WMi_gpu, lfc->WMimax, q,
                        (Tgpu*) work_buffer1, block_out, result_gpu + i * nM,
                        lfc->Mcount, nM, innvec);
                break;
            default:
                assert(0);
        }
        assert(!gpuCheckLastError());

        int s = blocks;
        int count = 0;
        while (s > 1) {
            int blocks2, threads2;
            int block_in = block_out;
            lfc_reduceNumBlocksAndThreads2(s, &blocks2, &threads2);
            block_out = blocks2;
            dim3 dimBlock(threads2, 1, 1);
            dim3 dimGrid(blocks2, cunvec * nM, 1);
            int smemSize = (threads2 <= 32) ? 2 * threads2 * sizeof(Tgpu)
                                            : threads2 * sizeof(Tgpu);

            Tgpu *work1 = (count % 2) ? work_buffer2 : work_buffer1;
            Tgpu *work2 = (count % 2) ? work_buffer1 : work_buffer2;
            count++;

            switch (threads2) {
                case 512:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel512),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case 256:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel256),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case 128:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel128),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case 64:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel64),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case 32:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel32),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case 16:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel16),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case  8:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel8),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case  4:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel4),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case  2:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel2),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                case  1:
                    gpuLaunchKernel(
                            Zgpu(reduce_kernel1),
                            dimGrid, dimBlock, smemSize, 0,
                            (Tgpu*) work1, NULL, (Tgpu*) work2,
                            result_gpu + i * nM, s, block_in, block_out,
                            cunvec * nM);
                    break;
                default:
                    assert(0);
            }
            assert(!gpuCheckLastError());
            s = (s + (threads2 * 2 - 1)) / (threads2 * 2);
        }
    }
}