summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/library/blas/gens/legacy/trmm_lds.c
blob: d7fe8826bc34c3f8f2a243f79aaa68aaf48789b4 (plain)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* ************************************************************************
 * Copyright 2013 Advanced Micro Devices, Inc.
 *
 * 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.
 * ************************************************************************/


/*
 * LDS based generator
 */

#include <string.h>
#include <stdio.h>
#include <assert.h>

#include <clBLAS.h>
#include <blas_mempat.h>
#include <clkern.h>
#include <clblas-internal.h>
#include <matrix_dims.h>
#include <dis_warning.h>

#include "../init.h"
#include "blas_kgen_legacy.h"
#include "gen_helper_legacy.h"
#include "../gen_helper.h"
#include "../trxm_common.h"
#include "trxm_common_legacy.h"

static CLBLASMpatExtra mpatExtra;

static ssize_t
generator(
   char *buf,
   size_t buflen,
   const struct SubproblemDim *subdims,
   const struct PGranularity *pgran,
   void *extra);

static void
assignKargs(KernelArg *args, const void *params, const void *extra);

static bool
isFitToLDS(
    SubproblemDim *dim,
    DataType dtype,
    cl_ulong ldsSize,
    const void *kernelArgs);

static SolverFlags
solverFlags(void);

static int
getPerf( unsigned int kflags,
    const void *args);

static SolverOps solverOps = {
    generator,
    assignKargs,
    isFitToLDS,
    getPerf,
    NULL,
    NULL,
    NULL,
    solverFlags,
    NULL, //fixupKargs
    NULL, //getDefaultDecomp
    NULL, //getDecompList
    NULL,
    NULL
};

static void
genPrepareBlockC(
    struct KgenContext *ctx,
    const ZeroFuncs *zeroFuncs)
{
    char tmp[2048];

    sprintf(tmp, "%s((__local float4*)tempC);\n", zeroFuncs->names[MATRIX_C]);
    kgenAddStmt(ctx, tmp);
}

static void
genWriteBlockB(
    struct KgenContext *ctx,
    const SubproblemDim *dim,
    DataType dtype,
    const CopyBufFuncs *copyFuncs,
    KernelExtraFlags kflags)
{
    char tmp[1024];
    size_t pitch;
    const char *coordName[2] = {"currM", "currN"};
    int trb;

    trb = isMatrixAccessColMaj(CLBLAS_TRMM, kflags, MATRIX_C);
    pitch = matrBlockPitch(dim, MATRIX_C, dtype, clblasLeft);

    if (!(kflags & (KEXTRA_TAILS_N | KEXTRA_TAILS_M))) {
        sprintf(tmp, "%s((GPtr)B, (LPtr)tempC, %s, %s, ldb);\n",
                copyFuncs->write, coordName[trb], coordName[1 - trb]);
    }
    else {
        sprintf(tmp,
                "y = (currM + %lu <= M) ? %lu : M - currM;\n"
                "x = (currN + %lu <= N) ? %lu : N - currN;\n"
                "if ((y == %lu) && (x == %lu)) {\n"
                     // fast rwrite
                "    %s((GPtr)B, (LPtr)tempC, %s, %s, ldb);\n"
                "}\n"
                "else {\n"
                     // slow write
                "    %s((GPtr)B, (LPtr)tempC, %s, %s, y, x, ldb, %lu);\n"
                "}\n\n",
                dim->y, dim->y, dim->x, dim->x, dim->y, dim->x,
                copyFuncs->write, coordName[trb], coordName[1 - trb],
                copyFuncs->writeGeneric, coordName[trb],
                coordName[1 - trb], pitch);
    }

    kgenAddStmt(ctx, tmp);
}

static void
genInitCurrM(
    struct KgenContext *ctx,
    const SubproblemDim *dim,
    KernelExtraFlags kflags)
{
    char tmp[1024];

    if (isMatrixUpper(kflags)) {
        strcpy(tmp, "currM = 0;\n");
    }
    else {
        sprintf(tmp, "currM = (M - 1) / %lu * %lu;\n", dim->y, dim->y);
    }

    kgenAddStmt(ctx, tmp);
    kgenAddBlankLine(ctx);
}

static void
genInternalLoopCtl(
    struct KgenContext *ctx,
    const SubproblemDim *dim,
    KernelExtraFlags kflags)
{
    char tmp[1024];

    if (isMatrixUpper(kflags)) {
        if (!(kflags & KEXTRA_TAILS_M)) {
            sprintf(tmp, "for (k0 = M - %lu; (k0 + %lu > currM) && (k0 < M); "
                         "k0 -= %lu)",
                    dim->bwidth, dim->bwidth, dim->bwidth);
        }
        else {
            sprintf(tmp, "for (k0 = (M - 1) / %lu * %lu; k0 + %lu > currM; "
                              "k0 -= %lu)",
                    dim->bwidth, dim->bwidth, dim->bwidth, dim->bwidth);
        }
    }
    else {
        sprintf(tmp, "for (k0 = 0; (k0 < currM + %lu) && (k0 < M); "
                          "k0 += %lu)",
                dim->y, dim->bwidth);
    }

    kgenBeginBranch(ctx, tmp);
}

static void
initKernelVarNames(KernelVarNames *kvars,  KernelExtraFlags kflags)
{
    kvars->A = "A";
    kvars->B = "B";
    if (isMatrixAccessColMaj(CLBLAS_TRMM, kflags, MATRIX_A)) {
        kvars->coordA = "coordA.x";
    }
    else {
        kvars->coordA = "coordA.y";
    }
    if (isMatrixAccessColMaj(CLBLAS_TRMM, kflags, MATRIX_B)) {
        kvars->coordB = "coordB.x";
    }
    else {
        kvars->coordB = "coordB.y";
    }
    kvars->sizeM = "M";
    kvars->sizeN = "N";
    kvars->sizeK = "origM";
}

static ssize_t
generator(
    char *buf,
    size_t buflen,
    const struct SubproblemDim *subdims,
    const struct PGranularity *pgran,
    void *extra)
{
    struct KgenContext *ctx;
    CLBLASKernExtra *kextra = (CLBLASKernExtra*)extra;
    char tmp[2048];
    char blkmul[128];
    char updateResFn[FUNC_NAME_MAXLEN];
    char updateResGenericFn[FUNC_NAME_MAXLEN];
    CopyBufFuncs copyFuncs;
    ZeroFuncs zeroFuncs;
    DataType dtype = kextra->dtype;
    ssize_t ret;
    BlasGenSettings gset;
    BlkMulOpts mulOpts;
    size_t pitchAB, pitchC;
    bool b;
    KernelExtraFlags kflags = kextra->flags;
    const char *outTypeName;
    unsigned int nrRegs;
    bool useLocalC;
    unsigned int vecLen = sizeof(cl_float4) / dtypeSize(dtype);
    int tra, trb;
    unsigned int l1Pans;
    char vect[2] = {'y', 'x'};

    if (pgran->wgDim != 1) {
        return -EINVAL;
    }

    ctx = createKgenContext(buf, buflen, true);
    if (ctx == NULL) {
        return -ENOMEM;
    }

    /* Code that updates block of B matrix using local registers or use mad's
     * doesn't work on some GPUs. As a workaround use buffer in local memory
     * for unaligned matrix sizes */
    useLocalC = (kflags & (KEXTRA_TAILS_M | KEXTRA_TAILS_N));

    memset(&gset, 0, sizeof(gset));
    memcpy(gset.subdims, subdims, sizeof(gset.subdims));
    gset.pgran = pgran;
    gset.kextra = kextra;

    initKernelVarNames(&gset.varNames, kflags);

    // at first, generate needed declarations and auxiliary functions

    b = isDoubleBasedType(dtype);
    kgenDeclareUptrs(ctx, b);
    generateBufCopyFuncs(&copyFuncs, ctx, CLBLAS_TRMM, &gset,
                         BCHF_MATRIX_A | BCHF_MATRIX_B | BCHF_WRITE_OUTPUT);
    if (useLocalC) {
        generateZeroingFuncs(&zeroFuncs, ctx, &subdims[0], pgran, dtype,
                ZF_MATRIX_A | ZF_MATRIX_B | ZF_MATRIX_C);
    }
    else {
        generateUpresFuncs(ctx, CLBLAS_TRMM, &gset, updateResFn,
                           updateResGenericFn);
        generateZeroingFuncs(&zeroFuncs, ctx, &subdims[0], pgran, dtype,
                ZF_MATRIX_A | ZF_MATRIX_B);
    }
    kgenAddBlankLine(ctx);

    // block multiplication function
    mulOpts.aMobj = CLMEM_BUFFER;
    mulOpts.bMobj = CLMEM_BUFFER;

    if (useLocalC) {
        mulOpts.flags = BLKMUL_SKEW_COLUMN;
    }
    else {
        mulOpts.flags = BLKMUL_OUTPUT_PRIVATE | BLKMUL_SKEW_COLUMN;
    }
    // BLKMUL_MAD doesn't work here on all cards so use SEPARATE_MULADD always
    // as a workaround
    mulOpts.core = BLKMUL_SEPARATE_MULADD;
    ret = blkMulGen(ctx, subdims, dtype, &mulOpts);
    if (ret) {
        destroyKgenContext(ctx);
        return -EOVERFLOW;
    }

    kgenAddBlankLine(ctx);
    kgenGetLastFuncName(blkmul, sizeof(blkmul), ctx);

    // now, generate the kernel
    declareTrxmKernel(ctx, dtype, pgran, kflags, CLBLAS_TRMM, NULL, false,
                      false);
    ret = kgenBeginFuncBody(ctx);

    /*
     * Calculate local buffer pitches, and then insert the
     * preparative code
     */
    pitchAB = matrBlockPitch(subdims, MATRIX_A, dtype, clblasLeft);
    pitchC = matrBlockPitch(subdims, MATRIX_C, dtype, clblasLeft);

    getResultGPRsInfo(dtype, &subdims[1], vecLen, &nrRegs, &outTypeName);
    declareLdsBasedTrxmVariables(ctx, dtype, subdims, pgran, useLocalC);

    /*
    * B matrix is divided on panels, each work group
    * multiply such a panel on the whole matrix A.
    */
    sprintf(tmp, "currN = gid * %lu;\n", subdims->x);
    kgenAddStmt(ctx, tmp);
    genInitCurrM(ctx, subdims, kflags);
    if (((kflags & (KEXTRA_SIDE_RIGHT | KEXTRA_STARTM_NOT_ZERO)) ==
          KEXTRA_STARTM_NOT_ZERO) ||
        ((kflags & (KEXTRA_SIDE_RIGHT | KEXTRA_STARTN_NOT_ZERO)) ==
                   (KEXTRA_SIDE_RIGHT | KEXTRA_STARTN_NOT_ZERO))) {

        kgenAddStmt(ctx, "A += lda * offsetM + offsetM;\n");
    }
    if (kflags & KEXTRA_A_OFF_NOT_ZERO) {
        kgenAddStmt(ctx, "A += offA;\n");
    }
    genTrxmBMatrShift(ctx, kflags, false);

    tra = isMatrixAccessColMaj(CLBLAS_TRMM, kflags, MATRIX_A);
    trb = isMatrixAccessColMaj(CLBLAS_TRMM, kflags, MATRIX_B);
    l1Pans = (unsigned int)subdims[0].x / (unsigned int)subdims[1].x;

    sprintf(tmp, "coordB.%c = currN + lid %% %u * %lu;\n"
                 "coordB.%c = 0;\n\n",
            vect[trb], l1Pans, subdims[1].x, vect[1 - trb]);
    kgenAddStmt(ctx, tmp);

    // loop over M
    sprintf(tmp, "for (m0 = 0; m0 < M; m0 += %lu)", subdims->y);
    kgenBeginBranch(ctx, tmp);

    sprintf(tmp, "coordA.%c = currM + lid / %u * %lu;\n"
                 "coordA.%c = 0;\n\n",
            vect[tra], l1Pans, subdims[1].y, vect[1 - tra]);
    kgenAddStmt(ctx, tmp);

    if (useLocalC) {
        genPrepareBlockC(ctx, &zeroFuncs);
    }
    else {
        // zero work item C block
        sprintf(tmp, "for (k0 = 0; k0 < %u; k0++) {\n"
                     "    c[k0] = 0;\n"
                     "}\n\n", nrRegs);
        kgenAddStmt(ctx, tmp);
    }

    /*
     * In the first pass the part without triangle blocks is processed,
     * and in the second one only triangle blocks are processed
     */
    genInternalLoopCtl(ctx, subdims, kflags);

    genPrepareTrxmBlockA(ctx, subdims, dtype, &copyFuncs, &zeroFuncs,
                         kflags, "M");
    genPrepareTrxmBlockB(ctx, subdims, dtype, &copyFuncs, &zeroFuncs,
                         kflags);
    kgenAddBarrier(ctx, CLK_LOCAL_MEM_FENCE);
    kgenAddBlankLine(ctx);

    genTriangMatrBlock(ctx, subdims, dtype, kflags);

    // and eventually multiply the blocks and update the matrix C block
    if (useLocalC) {
        sprintf(tmp, "%s(alpha, (LPtr)(tempA + (lid / %u * %lu) * %lu), \n"
                "                    (LPtr)(tempB + (lid %% %u * %lu) * %lu),\n"
                "                    (LPtr)(tempC + (lid / %u * %lu) * %lu + \n"
                "                    (lid %% %u * %lu)), lid);\n",
                blkmul, l1Pans, subdims[1].y, pitchAB,
                l1Pans, subdims[1].x, pitchAB,
                l1Pans, subdims[1].y, pitchC, l1Pans, subdims[1].x);
    }
    else {
        sprintf(tmp, "%s((LPtr)(tempA + (lid / %u * %lu) * %lu), "
                     "(LPtr)(tempB + (lid %% %u * %lu) * %lu), c, lid);\n",
                   blkmul, l1Pans, subdims[1].y, pitchAB, l1Pans,
                   subdims[1].x, pitchAB);
    }
    kgenAddStmt(ctx, tmp);
    kgenAddBarrier(ctx, CLK_LOCAL_MEM_FENCE);

    genInternalLoopEnd(ctx);                             // loop over K
    kgenAddBlankLine(ctx);

    // write back the block, it's evaluated
    if (useLocalC) {
        genWriteBlockB(ctx, subdims, dtype, &copyFuncs, kflags);
        kgenAddBarrier(ctx, CLK_GLOBAL_MEM_FENCE);
    }
    else {
        if (kflags & (KEXTRA_TAILS_M | KEXTRA_TAILS_N)) {
            sprintf(tmp, "if ((coordA.%c < M) && (coordB.%c < N))",
                    vect[tra], vect[trb]);
            kgenBeginBranch(ctx, tmp);
        }

        generateResultUpdateOld(ctx, CLBLAS_TRMM, &gset, updateResFn,
                             updateResGenericFn);

        if (kflags & (KEXTRA_TAILS_M | KEXTRA_TAILS_N)) {
           kgenEndBranch(ctx, tmp);
        }
    }

    if (isMatrixUpper(kflags)) {
        sprintf(tmp, "currM += %lu;\n", subdims[0].y);
    }
    else {
        sprintf(tmp, "currM -= %lu;\n", subdims[0].y);
    }
    kgenAddStmt(ctx, tmp);

    kgenEndBranch(ctx, NULL);                                 // loop over M

    kgenEndFuncBody(ctx);
    ret = kgenAddBlankLine(ctx);

    if (!ret) {
        ret = (ssize_t)kgenSourceSize(ctx) + 1;
    }

    destroyKgenContext(ctx);

    return (ret < 0) ? -EOVERFLOW : ret;
}

static void
assignKargs(KernelArg *args, const void *params, const void *extra)
{
    const CLBlasKargs *blasArgs = (const CLBlasKargs*)params;
    KernelExtraFlags kflags = ((const CLBLASKernExtra*)extra)->flags;
    int idx = 7;

    initSizeKarg(&args[0], blasArgs->M);
    initSizeKarg(&args[1], blasArgs->N);
    assignScalarKarg(&args[2], &(blasArgs->alpha), blasArgs->dtype);
    initMemobjKarg(&args[3], blasArgs->A, NULL, 0, 0);
    initSizeKarg(&args[4], blasArgs->lda.matrix);
    initMemobjKarg(&args[5], blasArgs->B, NULL, 0, 0);
    initSizeKarg(&args[6], blasArgs->ldb.matrix);
    if (kflags & KEXTRA_STARTM_NOT_ZERO) {
        initSizeKarg(&args[idx++], blasArgs->offsetM);
    }
    if (kflags & KEXTRA_STARTN_NOT_ZERO) {
        initSizeKarg(&args[idx++], blasArgs->offsetN);
    }
    if (kflags & KEXTRA_A_OFF_NOT_ZERO) {
        initSizeKarg(&args[idx++], blasArgs->offA);
    }
    if (kflags & KEXTRA_BX_OFF_NOT_ZERO) {
        initSizeKarg(&args[idx++], blasArgs->offBX);
    }
}

static bool
isFitToLDS(
    SubproblemDim *dim,
    DataType dtype,
    cl_ulong ldsSize,
    const void *kernelArgs)
{
    const CLBlasKargs *kargs = (const CLBlasKargs*)kernelArgs;
    cl_ulong size;

    size = matrBlockSize(dim, MATRIX_A, dtype, kargs->side);
    size += matrBlockSize(dim, MATRIX_B, dtype, kargs->side);
    size += matrBlockSize(dim, MATRIX_C, dtype, kargs->side);

    return (size * dtypeSize(dtype) <= ldsSize);
}

static SolverFlags
solverFlags(void)
{
    return ((unsigned int)SF_WSPACE_1D);
}

void
initTrmmLdsPattern(MemoryPattern *mempat)
{
    mempat->name = "LDS based block trmm";
    mempat->nrLevels = 2;
    mempat->cuLevel = 0;
    mempat->thLevel = 1;
    mempat->sops = &solverOps;

    mpatExtra.aMset = CLMEM_LEVEL_LDS;
    mpatExtra.bMset = CLMEM_LEVEL_LDS;
    mpatExtra.mobjA = CLMEM_BUFFER;
    mpatExtra.mobjB = CLMEM_BUFFER;
    mempat->extra = &mpatExtra;
}

static int
getPerf( unsigned int kflags,
    const void *args)
{
    DUMMY_ARG_USAGE(kflags);
    DUMMY_ARG_USAGE(args);

    return PPERF_POOR;
}