summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/library/blas/gens/legacy/trsm_kgen_legacy.c
blob: 2aa91cc8ecb980a405197f4b3cb2fc240d571526 (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
/* ************************************************************************
 * 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 <stdio.h>

#include "../blas_kgen.h"
#include "trsm_kgen_legacy.h"

void
genUpdateIntermTrsmResult(
    struct KgenContext *ctx,
    const BlasGenSettings *gset,
    const char *optFuncName,
    const char *genericFuncName,
    bool withMhitCond)
{
    char tmp[1024];
    const char *coordY, *coordX;
    char *revAlp, *alp;
    DataType dtype = gset->kextra->dtype;
    KernelExtraFlags kflags = gset->kextra->flags;
    const SubproblemDim *dim = &gset->subdims[1];
    const KernelVarNames *kvarNames = &gset->varNames;

    if (isComplexType(dtype)) {
        if (dtype == TYPE_COMPLEX_FLOAT) {
            revAlp = "div((float2)(-1.f, 0), alpha)";
            alp = "(float2)(1.f, 0)";
        }
        else {
            revAlp = "div((double2)(-1., 0), alpha)";
            alp = "(double2)(1., 0)";
        }
    }
    else {
        revAlp = "-1. / alpha";
        alp = "1.";
    }

    coordY = kvarNames->coordA;
    coordX = kvarNames->coordB;

    if (!(kflags & (KEXTRA_TAILS_M | KEXTRA_TAILS_N))) {
        sprintf(tmp, "%s(B, c, %s, %s, %s, ldb, %s);\n",
                optFuncName, alp, coordY, coordX, revAlp);
        kgenAddStmt(ctx, tmp);
    }
    else {
        if (withMhitCond) {
            sprintf(tmp, "if ((%s < %s) && (%s < %s))",
                    coordY, kvarNames->sizeM, coordX, kvarNames->sizeN);
            kgenBeginBranch(ctx, tmp);
        }
        else {
            /* for x, y variables scope */
            kgenBeginBranch(ctx, NULL);
        }

        sprintf(tmp, "uint y = min(%luu, %s - (uint)%s);\n"
                     "uint x = min(%luu, %s - (uint)%s);\n"
                     "if ((y == %luu) && (x == %luu)) {\n"
                     "    %s(B, c, %s, %s, %s, ldb, %s);\n"
                     "}\n"
                     "else {\n"
                     "    %s(B, c, %s, %s, %s, ldb, %s, y, x);\n"
                     "}\n",
                dim->y, kvarNames->sizeM, coordY,
                dim->x, kvarNames->sizeN, coordX,
                dim->y, dim->x,
                optFuncName, alp, coordY, coordX, revAlp,
                genericFuncName, alp, coordY, coordX, revAlp);

        kgenAddStmt(ctx, tmp);

        kgenEndBranch(ctx, NULL);
    }
}

void
genHeapTrsmResultToLDS(
    struct KgenContext *ctx,
    const BlasGenSettings *gset,
    const char *funcName,
    const char *dstName)
{
    char tmp[1024];
    char *alp;
    unsigned int l1Pans;
    DataType dtype = gset->kextra->dtype;
    const SubproblemDim *dims = gset->subdims;

    if(isComplexType(dtype)) {
        if (dtype == TYPE_COMPLEX_FLOAT) {
            alp = "(float2)(1.f, 0)";
        }
        else {
            alp = "(double2)(1., 0)";
        }
    }
    else {
        alp = "1.";
    }

    l1Pans = (unsigned int)dims[0].x / (unsigned int)dims[1].x;
    sprintf(tmp, "%s(%s, c, %s, (lid / %u * %lu), (lid %% %u * %lu), %lu);\n",
            funcName, dstName, alp, l1Pans, dims[1].y, l1Pans, dims[1].x,
            dims[0].bwidth);
    kgenAddStmt(ctx, tmp);
}

void
genInvertingBlockFunc(
    struct KgenContext *ctx,
    size_t pitch,
    DataType dtype,
    KernelExtraFlags kflags)
{
    char tmp[1024];
    const char *ctype;
    ctype = dtypeBuiltinType(dtype);

    sprintf(tmp, "void\ninvert(__local %s *src, __local %s *dst, int lid, "
                              "int lastRow)\n", ctype, ctype);
    kgenDeclareFunction(ctx, tmp);
    kgenBeginFuncBody(ctx);
    kgenAddStmt(ctx, "int i, k;\n");

    if (isComplexType(dtype)) {
        sprintf(tmp, "dst[lid * %lu + lid].x = 1.f;\n", pitch);
    }
    else {
        sprintf(tmp, "dst[lid * %lu + lid] = 1.f;\n", pitch);
    }
    kgenAddStmt(ctx, tmp);

    if (isMatrixUpper(kflags)) {
        sprintf(tmp, "for (i = lastRow - 1; i >= 0; i--)");
    }
    else {
        sprintf(tmp, "for (i = 0; i < lastRow; i++)");
    }
    kgenBeginBranch(ctx, tmp);

    if (isComplexType(dtype)) {
        sprintf(tmp, "dst[i * %lu + lid] = div(dst[i * %lu + lid], "
                     "src[i * %lu + i]);\n", pitch, pitch, pitch);
    }
    else {
        sprintf(tmp, "dst[i * %lu + lid] = dst[i * %lu + lid] / "
                     "src[i * %lu + i];\n", pitch, pitch, pitch);
    }
    kgenAddStmt(ctx, tmp);

    if (isMatrixUpper(kflags)) {
        sprintf(tmp, "for (k = 0; k < i; k++)");
    }
    else {
        sprintf(tmp, "for (k = i + 1; k < %lu; k++)", pitch);
    }
    kgenBeginBranch(ctx, tmp);
    if (isComplexType(dtype)) {
        sprintf(tmp, "dst[k * %lu + lid] = dst[k * %lu + lid] - "
                     "mul(src[k * %lu + i], dst[i * %lu + lid]);\n",
                pitch, pitch, pitch, pitch);
    }
    else {
        sprintf(tmp, "dst[k * %lu + lid] = dst[k * %lu + lid] - "
                      "dst[i * %lu + lid] * src[k * %lu + i];\n",
                pitch, pitch, pitch, pitch);
    }
    kgenAddStmt(ctx, tmp);
    kgenEndBranch(ctx, NULL);
    kgenEndBranch(ctx, NULL);
    kgenEndFuncBody(ctx);
}