summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/library/blas/gens/tile.c
blob: 2ffdebc92fe18b7d998b31c6ec5bc90d4a741a05 (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
515
516
517
/* ************************************************************************
 * 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.
 * ************************************************************************/


#include <sys/types.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#include <defbool.h>
#include <clblas_stddef.h>

#include "blas_kgen.h"

// assign tile's base name to 'name' if it is assigned to zero pointer
static __inline void
selectTileBaseName(Tile *tile, const char *name)
{
    if (tile->baseName == NULL) {
        tile->baseName = name;
    }
}

static void
selectDefaultTileVecLen(
    Tile *tile,
    TileCreationFlags tflags,
    const BlasGenSettings *gset,
    BlasFunctionID funcID,
    MatrixRole mrole)
{
    if (tflags & TILE_WITH_FETCH_VECLEN) {
        tile->vecLen = getVecLen(gset, funcID, mrole);
    }
    else {
        size_t w;

        w = (tile->trans) ? tile->nrRows : tile->nrCols;
        if (tile->packed) {
            size_t wpad, height;

            wpad = roundUpPow2(w);
            height = (tile->trans) ? tile->nrCols : tile->nrRows;
            tile->vecLen = (unsigned int)szmin(height * wpad, MAX_TILE_VECLEN);
        }
        else {
            tile->vecLen = (unsigned int)roundUpPow2(w);
            tile->vecLen = (unsigned int)szmin(tile->vecLen, MAX_TILE_VECLEN);
        }
    }
}

// physical tile pitch, can be less than one vector in case of packed mode
static unsigned int
tilePitch(const Tile *tile)
{
    unsigned int pitch;

    if (!tile->trans) {
        if (tile->packed) {
            pitch = (unsigned int)roundUpPow2(tile->nrCols);
        }
        else {
            pitch = (unsigned int)roundUp(tile->nrCols, tile->vecLen);
        }
    }
    else {
        if (tile->packed) {
            pitch = (unsigned int)roundUpPow2(tile->nrRows);
        }
        else {
            pitch = (unsigned int)roundUp(tile->nrRows, tile->vecLen);
        }
    }

    return pitch;
}

void
initTile(
    Tile *tile,
    const char *baseName,
    unsigned int nrRows,
    unsigned int nrCols,
    unsigned int vecLen,
    DataType dtype,
    PrivateStorageType storType,
    bool trans,
    bool packed)
{
    assert(baseName == NULL || strlen(baseName) <= MAX_TILE_BASE_NAMELEN);

    tile->baseName = baseName;
    tile->nrRows = nrRows;
    tile->nrCols = nrCols;
    tile->vecLen = umin(MAX_TILE_VECLEN, vecLen);
    tile->dtype = dtype;
    tile->storType = storType;
    tile->trans = trans;
    tile->packed = packed;
}

void
initDefaultTiles(
    BlasGenSettings *gset,
    BlasFunctionID funcID,
    TileCreationFlags flags,
    PrivateStorageType storType)
{
    const SubproblemDim *dim = &gset->subdims[1];
    KernelExtraFlags kflags = gset->kextra->flags;
    DataType dtype = gset->kextra->dtype;
    Tile *tile;
    const char *name;
    int level;
    bool packed;

    level = funcBlasLevel(funcID);
    packed = ((flags & TILE_PACKED) != 0);

    tile = &gset->tileA;
    selectTileBaseName(tile, "a");
    initTile(tile, tile->baseName, (unsigned int)dim->y,
             (unsigned int)dim->bwidth, 1, dtype, storType, false, packed);

    tile->trans = isMatrixAccessColMaj(funcID, kflags, MATRIX_A);
    if (!(gset->flags & BGF_WHOLE_A)) {
        if (tile->trans) {
            tile->nrCols = 1;
        }
        else {
            tile->nrRows = 1;
        }
    }
    selectDefaultTileVecLen(tile, flags, gset, funcID, MATRIX_A);

    tile = &gset->tileBX;
    name = (level == 2) ? "x" : "b";
    selectTileBaseName(tile, name);
    initTile(tile, tile->baseName, (unsigned int)dim->bwidth,
             (unsigned int)dim->x, 1, dtype, storType, false, packed);

    /*
     * NOTE: Tiles for the level 2 functions are forced to be transposed
     *       in order to allow user to fetch elements belonging to different
     *       rows which is very useful in case of unit increment between
     *       elements because provides faster access to the global memory.
     */
    if (level == 2) {
        tile->trans = true;
    }
    else {
        tile->trans = !isMatrixAccessColMaj(funcID, kflags, MATRIX_B);
    }
    selectDefaultTileVecLen(tile, flags, gset, funcID, MATRIX_B);

    tile = &gset->tileCY;
    name = (level == 2) ? "y" : "c";
    selectTileBaseName(tile, name);

    initTile(tile, tile->baseName, (unsigned int)dim->y,
             (unsigned int)dim->x, 1, dtype, storType, false,
             packed);

    if (level == 2) {
        tile->trans = true;
    }
    else if (!(flags & TILE_C_FORCE_NOTRANS)) {
        tile->trans = isMatrixAccessColMaj(funcID, kflags, MATRIX_C);
    }
    selectDefaultTileVecLen(tile, flags, gset, funcID, MATRIX_C);

    // FIXME: remove the restriction
    /*if (isComplexType(tile->dtype)) {
        tile->vecLen = 1;
    }*/
}

unsigned int
tileVectorsNum(const Tile *tile)
{
    size_t pitch, height;

    pitch = tilePitch(tile);
    height = (tile->trans) ? tile->nrCols : tile->nrRows;

    return (unsigned int)divRoundUp(height * pitch, tile->vecLen);
}

unsigned int
tileStorageSize(const Tile *tile)
{
    unsigned int u;

    u = tileVectorsNum(tile) * tile->vecLen;

    return u;
}

unsigned int
tileLineSegmentLen(const Tile *tile)
{
    unsigned int pitch;
    unsigned int len;

    pitch = tilePitch(tile);
    len = umin(pitch, tile->vecLen);
    if (tile->trans) {
        len = umin(len, tile->nrRows);
    }
    else {
        len = umin(len, tile->nrCols);
    }

    return len;
}

int
declareOneTileStorage(struct KgenContext *ctx, const Tile *tile)
{
    char tmp[1024];
    const char *tname;
    int r;
    size_t size;

    getVectorTypeName(tile->dtype, tile->vecLen, &tname, NULL);
    size = tileVectorsNum(tile);
    if (tile->storType == PRIV_STORAGE_ARRAY) {
        sprintf(tmp, "%s %s[%lu];\n", tname, tile->baseName, size);
    }
    else {
        size_t i;
        char *p;

        sprintf(tmp, "%s %s0", tname, tile->baseName);
        p = tmp + strlen(tmp);
        for (i = 1; i < size; i++) {
            sprintf(p, ", %s%lu", tile->baseName, i);
            p += strlen(p);
        }
        strcpy(p, ";\n");
    }

    r = kgenAddStmt(ctx, tmp);

    return (r) ? -EOVERFLOW : 0;
}

int
declareTileStorages(struct KgenContext *ctx, const BlasGenSettings *gset)
{
    int ret;

    ret = declareOneTileStorage(ctx, &gset->tileA);
    if (!ret) {
        ret = declareOneTileStorage(ctx, &gset->tileBX);
    }
    if (!ret) {
        declareOneTileStorage(ctx, &gset->tileCY);
    }

    return ret;
}

void
sprintfTileElement(
    Kstring *str,
    const Tile *tile,
    unsigned int row,
    unsigned int col,
    unsigned int len)
{
    unsigned int pitch;
    unsigned int elemLen;
    unsigned int off;
    unsigned int vecLen = tile->vecLen;
    char vchunk[24];

    if (len == 0) {
        len = vecLen;
    }

    pitch = tilePitch(tile);
    elemLen = isComplexType(tile->dtype) ? 2 : 1;
    if (!tile->trans) {
        assert((row < tile->nrRows) && (col + len <= tile->nrCols));
        off = (row * pitch + col) * elemLen;
    }
    else {
        assert((row + len <= tile->nrRows) && (col < tile->nrCols));
        off = (col * pitch + row) * elemLen;
    }

    vecLen *= elemLen;
    sprintfVecChunk(vchunk, vecLen, len * elemLen, off % vecLen);

    if (tile->storType == PRIV_STORAGE_ARRAY) {
        sprintf(str->buf, "%s[%u]%s", tile->baseName, off / vecLen, vchunk);
    }
    else {
        sprintf(str->buf, "%s%u%s", tile->baseName, off / vecLen, vchunk);
    }
}

void
sprintfTileElementHalf(
    Kstring *str,
    const Tile *tile,
    unsigned int row,
    unsigned int col,
    TileElementHalf half)
{
    int len;

    assert(isComplexType(tile->dtype));

    // sprintf the full element and the drop an unneded half
    sprintfTileElement(str, tile, row, col, 1);
    len = (int)strlen(str->buf);
    if (half == TE_HALF_HIGH) {
        str->buf[len - 2] = str->buf[len - 1];
    }
    str->buf[len - 1] = '\0';
}

int
forEachTile(Kstring *kstr, unsigned int row, unsigned int col,
            unsigned int num, Tile *first, ...)
{
   unsigned int minVecLen = first->vecLen;
   unsigned int valRow = first->nrRows;
   unsigned int valCol = first->nrCols;
   va_list argptr;
   unsigned int i;

   va_start(argptr, first);
   for (i = 1; i < num; i++) {
       Tile * cur = va_arg( argptr, Tile * );
       minVecLen = umin(minVecLen, cur->vecLen);
   }
   va_end(argptr);

   if (first->trans) {
       valRow /= minVecLen;
   }
   else {
       valCol /= minVecLen;
   }

   if (row >= valRow || col >= valCol /*|| row < 0 || col < 0*/) { //would be signed
       return 0;
   }
   if (kstr) {
       va_start(argptr, first);
       for (i = 0; i < num; i++) {
           Tile * cur = i ? va_arg( argptr, Tile * ) : first;
           if (cur->baseName) {
               unsigned int vRow = (cur->trans ? row * minVecLen : row);
               unsigned int vCol = (cur->trans ? col : col * minVecLen);
               sprintfTileElement(&kstr[i], cur, vRow, vCol, minVecLen);
           }
       }
       va_end(argptr);
   }
   return first->trans ? valRow : valCol;
}

void
genSetZeroInTile(
    struct KgenContext *ctx,
    const Tile *tile,
    unsigned int row,
    unsigned int col,
    unsigned int len)
{
    char tmp[1024];
    Kstring elem;

    sprintfTileElement(&elem, tile, row, col, len);
    sprintf(tmp, "%s = 0;\n", elem.buf);
    kgenAddStmt(ctx, tmp);
}

void
genSetUnitInTile(
    struct KgenContext *ctx,
    const Tile *tile,
    unsigned int row,
    unsigned int col)
{
    char tmp[1024];
    Kstring elem;
    const char *s;

    sprintfTileElement(&elem, tile, row, col, 1);
    s = strOne(tile->dtype);
    sprintf(tmp, "%s = %s;\n", elem.buf, s);
    kgenAddStmt(ctx, tmp);
}

void
genZeroTile(struct KgenContext *ctx, const Tile *tile)
{
    char tmp[1024];
    Kstring elem;
    unsigned int incRows, incCols;
    unsigned int i, j, v;

    v = tileLineSegmentLen(tile);
    if (!tile->trans) {
        incRows = 1;
        incCols = v;
    }
    else {
        incRows = v;
        incCols = 1;
    }

    for (i = 0; i < tile->nrRows; i += incRows) {
        for (j = 0; j < tile->nrCols; j += incCols) {
            sprintfTileElement(&elem, tile, i, j, v);
            sprintf(tmp, "%s = 0;\n", elem.buf);
            kgenAddStmt(ctx, tmp);
        }
    }

    kgenAddBlankLine(ctx);
}

void
genTileCopy(
    struct KgenContext *ctx,
    const Tile *dst,
    const Tile *src,
    TileCopyOps op)
{
    char tmp[1024];
    Kstring el1, el2;
    unsigned int nrRows, nrCols;
    unsigned int incRows, incCols;
    unsigned int vlen;
    unsigned int i, j;

    nrRows = umin(dst->nrRows, src->nrRows);
    nrCols = umin(dst->nrCols, src->nrCols);
    if (dst->trans != src->trans) {
        vlen = 1;
        incRows = incCols = 1;
    }
    else {
        vlen = umin(dst->vecLen, src->vecLen);
        if (!dst->trans) {
            incRows = 1;
            incCols = umin(dst->nrCols, src->nrCols);
            incCols = umin(incCols, vlen);
        }
        else {
            incRows = umin(dst->nrRows, src->nrRows);
            incRows = umin(incRows, vlen);
            incCols = 1;
        }
    }

    for (i = 0; i < nrRows; i += incRows) {
        for (j = 0; j < nrCols; j += incCols) {
            sprintfTileElement(&el1, dst, i, j, vlen);
            sprintfTileElement(&el2, src, i, j, vlen);
            switch( op )
            {
                case TILECOPY_ASSIGN:
                    sprintf(tmp, "%s = %s;\n", el1.buf, el2.buf);
                    break;

                case TILECOPY_ADD_ASSIGN:
                    sprintf(tmp, "%s += %s;\n", el1.buf, el2.buf);
                    break;

                case TILECOPY_SUB_ASSIGN:
                    sprintf(tmp, "%s -= %s;\n", el1.buf, el2.buf);
                    break;

                case TILECOPY_MUL_ASSIGN:
                    sprintf(tmp, "%s *= %s;\n", el1.buf, el2.buf);
                    break;

                case TILECOPY_DIV_ASSIGN:
                    sprintf(tmp, "%s /= %s;\n", el1.buf, el2.buf);
                    break;

                case TILECOPY_MOD_ASSIGN:
                    sprintf(tmp, "%s %%= %s;\n", el1.buf, el2.buf);
                    break;

                default:
                    break;
            }
            kgenAddStmt(ctx, tmp);
        }
    }

    kgenAddBlankLine(ctx);
}