summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/library/common/tests/t_gens_cache.c
blob: 5a2b98231887a745840057d3e7437a151c791581 (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
/* ************************************************************************
 * 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.
 * ************************************************************************/


/*
 * test generator and cache infrastructure
 */

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

#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif

#include <kerngen.h>
#include <kern_cache.h>

enum {
    NR_TEST_PATTERNS = 5,
    KERNELS_PER_PATTERN = 10,
    KCACHE_SIZE_LIMIT = 1048576
};

const char *strcpyImpl =
    "char\n"
    "*strcpy(char *dst, char *src)\n"
    "{\n"
    "   do {\n"
    "       *dst++ = *src++;\n"
    "   } while (*(dst - 1) != 0);\n"
    "}";

static int
testGenFunc(struct KgenContext *ctx)
{
    kgenDeclareFunction(ctx, "char\n"
                             "*strcpy(char *dst, char *src)\n");
    kgenBeginFuncBody(ctx);
    kgenAddStmt(ctx, "char *ret = dst;\n\n");
    kgenBeginBranch(ctx, "do");
    kgenAddStmt(ctx, "*dst = *src;\n"
                     "src++;\n"
                     "dst++;\n");
    kgenEndBranch(ctx, "while (*(dst - 1) != 0)");
    kgenAddBlankLine(ctx);
    kgenAddStmt(ctx, "return ret;\n");

    return kgenEndFuncBody(ctx);
}

static int
kernExtraCmp(const void *extra, const void *extraKey)
{
    unsigned long u1 = *(unsigned long*)extra;
    unsigned long u2 = *(unsigned long*)extraKey;

    return !(u1 == u2);
}


static int
testGen(void)
{
    char buf[4096];
    char name[64];
    int r;
    struct KgenContext *ctx;
    size_t s;

    ctx = createKgenContext(buf, sizeof(buf), true);
    if (ctx == NULL) {
        printf("Context creation failed\n");
        printf("FAIL\n\n");
        return -1;
    }

    printf("Test normal kernel generation\n");
    if (!testGenFunc(ctx)) {
        printf("Generated code:\n\n");
        printf("%s", buf);
        printf("\n\nPASS\n\n");
    }
    else {
        printf("FAIL\n\n");
    }

    printf("Test function name extracting from the generated code\n");
    r = kgenGetLastFuncName(name, sizeof(name), ctx);
    if (r) {
        printf("FAIL\n");
    }
    else {
        if (strcmp((const char*)name, "strcpy")) {
            printf("Extracted names is %s must be strcpy\n", name);
            printf("FAIL\n\n");
            r = -1;
        }
        else {
            printf("PASS\n\n");
        }
    }

    destroyKgenContext(ctx);

    printf("Test source size calculating without actual source "
           "adding to any buffer\n");
    ctx = createKgenContext(NULL, 0, true);
    r = kgenAddStmt(ctx, strcpyImpl);
    if (!r) {
        s = kgenSourceSize(ctx);
        if (s != strlen(strcpyImpl)) {
            r = -1;
        }
    }
    if (r) {
        printf("FAIL\n\n");
    }
    else {
        printf("PASS\n\n");
    }
    destroyKgenContext(ctx);

    ctx = createKgenContext(buf, 5, true);

    if (!r) {
        printf("Test generation with insufficient buffer\n");
        if (testGenFunc(ctx)) {
            printf("PASS\n");
        }
        else {
            printf("FAIL\n");
            r = -1;
        }
    }

    return r;
}

// test case for kache error functionality
static int
errorCacheTestCase(
    const char *msg,
    struct KernelCache *kcache,
    solver_id_t sid,
    SubproblemDim *dims,
    unsigned int nrDims,
    cl_context context,
    cl_device_id device,
    unsigned long extra,
    Kernel *kern)
{
    KernelKey key;
    Kernel* krn1;
    int r;
    bool fail;

    key.device = device;
    key.context = context;
    key.nrDims = nrDims;
    memset(key.subdims, 0, sizeof(key.subdims));
    r = nrDims;
    if (nrDims > MAX_SUBDIMS)
        r = MAX_SUBDIMS;
    memcpy(key.subdims, dims, sizeof(SubproblemDim) * r);

    printf("%s", msg);
    if (kern == NULL) {
        krn1 = findKernel(kcache, sid, &key, &extra);
        fail = (krn1 != NULL);
    }
    else {
        r = addKernelToCache(kcache, sid, kern, &key, kernExtraCmp);
        fail = (r == 0);
    }

    if (fail) {
        printf("FAIL\n");
        r = -1;
    }
    else {
        printf("PASS\n");
        r = 0;
    }

    return r;
}

static int
testCache(cl_context context, cl_device_id device)
{
    int r = 0;
    int i, j;
    unsigned int k;
    const solver_id_t wrongSID = 15;
    struct KernelCache *kcache;
    KernelKey key;
    Kernel *kern[NR_TEST_PATTERNS][KERNELS_PER_PATTERN], *krn1;
    SubproblemDim dims[NR_TEST_PATTERNS][KERNELS_PER_PATTERN][MAX_SUBDIMS];
    unsigned int nrDims[NR_TEST_PATTERNS] = {1, 3, 2, 2, 1};
    unsigned long extra = 7, extra1;

    printf("Testing inserting and normal searching of kernels\n");
    kcache = createKernelCache(10, KCACHE_SIZE_LIMIT);

    key.device = device;
    key.context = context;

    for (i = 0; (i < NR_TEST_PATTERNS) && !r; i++) {
        for (j = 0; (j < KERNELS_PER_PATTERN) && !r; j++) {
            for (k = 0; k < nrDims[i]; k++) {
                dims[i][j][k].x = random() % 1000;
                if (k == 2) {
                    dims[i][j][k].y = SUBDIM_UNUSED;
                    dims[i][j][k].itemX = SUBDIM_UNUSED;
                }
                else {
                    dims[i][j][k].y = random() % 1000;
                    dims[i][j][k].itemX = random() % 1000;
                }
                dims[i][j][k].bwidth = random() % 1000;
                dims[i][j][k].itemY = random() % 1000;
            }

            kern[i][j] = allocKernel();
            kern[i][j]->extra = &extra;
            kern[i][j]->extraSize = sizeof(extra);
            key.nrDims = nrDims[i];
            memset(key.subdims, 0, sizeof(key.subdims));
            memcpy(key.subdims, dims[i][j], sizeof(SubproblemDim) * key.nrDims);
            r = addKernelToCache(kcache, i, kern[i][j], &key, kernExtraCmp);
        }
    }

    if (r) {
        printf("Error at addition to the cache, i = %d, j = %d\n", i, j);
        printf("FAIL\n");
    }
    else {
        // Now try to find each cached kernel
        extra1 = extra;
        for (i = 0; (i < NR_TEST_PATTERNS) && !r; i++) {
            for (j = 0; j < KERNELS_PER_PATTERN; j++) {
                key.nrDims = nrDims[i];
                memset(key.subdims, 0, sizeof(key.subdims));
                memcpy(key.subdims, dims[i][j], sizeof(SubproblemDim) * key.nrDims);
                krn1 = findKernel(kcache, i, &key, &extra1);
                if (krn1 != kern[i][j]) {
                    r = -1;
                    break;
                }
            }
        }
        if (r) {
            printf("First error occurred at pattern %d, kernel %d: ", i, j);
            if (krn1 == NULL) {
                printf("the kernel is not found\n");
            }
            else {
                printf("the kernel mismatch\n");
            }
        }
        else {
            printf("PASS\n");
        }
    }

    // cases for search error functionality
    dims[0][0][0].x = 1001;

    if (!r) {
        r = errorCacheTestCase("Try to search a kernel not being in "
                               "the cache\n",
                               kcache, 0, dims[0][0],
                               nrDims[0], context, device, extra, NULL);
    }

    if (!r) {
        r = errorCacheTestCase("Try To search a kernel with a wrong extra "
                               "information\n", kcache, 0, dims[0][1],
                               nrDims[0], context, device, extra - 2, NULL);
    }

    if (!r) {
        r = errorCacheTestCase("Try to search a kernel with a solver "
                               "ID\n", kcache, wrongSID,
                               dims[0][1], nrDims[0], context, device,
                               extra, NULL);
    }

    if (!r) {
        r = errorCacheTestCase("Try to search a kernel with a wrong number "
                               "of subproblem dimensions\n",
                               kcache, 0, dims[0][1], 500, context, device,
                               extra, NULL);
    }
    if (!r) {
        r = errorCacheTestCase("Try to search a kernel with bad OpenCL context\n",
                               kcache, 0, dims[0][1], 500, (cl_context)-1, device,
                               extra, NULL);
    }
    if (!r) {
        r = errorCacheTestCase("Try to search a kernel with bad OpenCL device\n",
                               kcache, 0, dims[0][1], 500, context,
                               (cl_device_id)-1, extra, NULL);
    }

    // error test cases for inserting to cache
    krn1 = allocKernel();
    krn1->extra = &extra;
    krn1->extraSize = sizeof(extra);

    if (!r) {
        r = errorCacheTestCase("Try to insert a kernel with a wrong solver "
                               "ID\n", kcache, wrongSID,
                               dims[0][0], nrDims[0], context, device,
                               extra, krn1);
    }

    if (!r) {
        r = errorCacheTestCase("Try to insert a kernel with a wrong number "
                               "of subproblem dimensions\n",
                               kcache, 0, dims[0][0],
                               500, context, device, extra, krn1);
    }

    return r;
}


int
main(void)
{
    cl_int err;
    cl_platform_id platform;
    cl_device_id device;
    cl_context_properties props[] = { CL_CONTEXT_PLATFORM, 0, 0 };
    cl_context context;

    err = clGetPlatformIDs(1, &platform, NULL);
    if (err != CL_SUCCESS) {
        fprintf(stderr, "clGetPlatformIDs() failed with %d\n", err);
        return 1;
    }
    err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
    if (err != CL_SUCCESS) {
        fprintf(stderr, "clGetDeviceIDs() failed with %d\n", err);
        return 1;
    }
    props[1] = (cl_context_properties)platform;
    context = clCreateContext(props, 1, &device, NULL, NULL, &err);
    if (err != CL_SUCCESS) {
        fprintf(stderr, "clCreateContext() failed with %d\n", err);
        return 1;
    }

    printf("Launch tests for kernel generators\n");
    printf("-----------------------------------------\n");
    if (!testGen()) {
        printf("-----------------------------------------\n\n");
        printf("Launch tests for kernel cache\n");
        printf("-----------------------------------------\n");
        testCache(context, device);
    }

    clReleaseContext(context);
    return 0;
}