summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/opencl/opencl-test.cpp
blob: 17bc5ccfa639fe8262d2d92047ff77c74326e941 (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/* $Rev$ $Date$ */

/**
 * Test OpenCL kernel evaluator.
 */

#include <assert.h>
#include "stream.hpp"
#include "string.hpp"
#include "driver.hpp"
#include "parallel.hpp"
#include "perf.hpp"

namespace tuscany {
namespace opencl {

const string testFloatsCPU =
        "kernel void add(const float x, const float y, global float* r) {\n"
        "    float l_x;\n"
        "    float l_y;\n"
        "    l_x = x;\n"
        "    l_y = y;\n"
        "    *r = l_x + l_y;\n"
        "}\n"
        "kernel void float_add(const float x, const float y, global float* r) {\n"
        "    add(x, y, r);\n"
        "}\n";

const string testFloatsGPU =
        "kernel void add(const float x, const float y, global float* r) {\n"
        "    local float l_x;\n"
        "    local float l_y;\n"
        "    l_x = x;\n"
        "    l_y = y;\n"
        "    barrier(CLK_LOCAL_MEM_FENCE);\n"
        "    *r = l_x + l_y;\n"
        "}\n"
        "kernel void float_add(const float x, const float y, global float* r) {\n"
        "    add(x, y, r);\n"
        "}\n";

const string testBoolsCPU =
        "kernel void int_or(const int x, const int y, global int* r) {\n"
        "    int l_x;\n"
        "    int l_y;\n"
        "    l_x = x;\n"
        "    l_y = y;\n"
        "    *r = l_x || l_y;\n"
        "}\n";

const string testBoolsGPU =
        "kernel void int_or(const int x, const int y, global int* r) {\n"
        "    local int l_x;\n"
        "    local int l_y;\n"
        "    l_x = x;\n"
        "    l_y = y;\n"
        "    barrier(CLK_LOCAL_MEM_FENCE);\n"
        "    *r = l_x || l_y;\n"
        "}\n";

const string testCharsCPU =
        "kernel void add(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    const int ixl = (int)xl;\n"
        "    const int iyl = (int)yl;\n"
        "    for (int i = 0; i < ixl; i++)\n"
        "        r[i] = x[i];\n"
        "    for (int i = 0; i < iyl; i++)\n"
        "        r[ixl + i] = y[i];\n"
        "    r[ixl + iyl] = '\\0';\n"
        "}\n"
        "kernel void char128_add(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    add(xl, x, yl, y, r);\n"
        "}\n";

const string testCharsCopyGPU =
        "kernel void add(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    const int ixl = (int)xl;\n"
        "    const int iyl = (int)yl;\n"
        "    local char l_x[128];\n"
        "    local char l_y[128];\n"
        "    event_t re = async_work_group_copy(l_x, x, ixl, 0);\n"
        "    async_work_group_copy(l_y, y, iyl, re);\n"
        "    wait_group_events(1, &re);\n"
        "    local char l_r[128];\n"
        "    for (int i = 0; i < ixl; i++)\n"
        "        l_r[i] = l_x[i];\n"
        "    for (int i = 0; i < iyl; i++)\n"
        "        l_r[ixl + i] = l_y[i];\n"
        "    l_r[ixl + iyl] = '\\0';\n"
        "    event_t we = async_work_group_copy(r, l_r, ixl + iyl + 1, 0);\n"
        "    wait_group_events(1, &we);\n"
        "}\n"
        "kernel void char128(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    add(xl, x, yl, y, r);\n"
        "}\n";

const string testCharsGPU =
        "kernel void add(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    const int ixl = (int)xl;\n"
        "    const int iyl = (int)yl;\n"
        "    for (int i = 0; i < ixl; i++)\n"
        "        r[i] = x[i];\n"
        "    for (int i = 0; i < iyl; i++)\n"
        "        r[ixl + i] = y[i];\n"
        "    r[ixl + iyl] = '\\0';\n"
        "}\n"
        "kernel void char128(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    add(xl, x, yl, y, r);\n"
        "}\n";

const string testCharsParallelCPU =
        "kernel void add(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    const int i = get_global_id(0);\n"
        "    const int ixl = (int)xl;\n"
        "    const int iyl = (int)yl;\n"
        "    r[i] = i < ixl? x[i] : i < ixl + iyl? y[i - ixl] : '\\0';\n"
        "}\n";

const string testCharsParallelCopyGPU =
        "kernel void add(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    const int i = get_global_id(0);\n"
        "    const int ixl = (int)xl;\n"
        "    const int iyl = (int)yl;\n"
        "    local char l_x[128];\n"
        "    local char l_y[128];\n"
        "    event_t re = async_work_group_copy(l_x, x, ixl, 0);\n"
        "    async_work_group_copy(l_y, y, iyl, re);\n"
        "    wait_group_events(1, &re);\n"
        "    local char l_r[128];\n"
        "    l_r[i] = i < ixl? l_x[i] : i < ixl + iyl? l_y[i - ixl] : '\\0';\n"
        "    event_t we = async_work_group_copy(r, l_r, ixl + iyl + 1, 0);\n"
        "    wait_group_events(1, &we);\n"
        "}\n";

const string testCharsParallelGPU =
        "kernel void add(const float xl, global const char* x, const float yl, global const char* y, global char* r) {\n"
        "    const int i = get_global_id(0);\n"
        "    const int ixl = (int)xl;\n"
        "    const int iyl = (int)yl;\n"
        "    r[i] = i < ixl? x[i] : i < ixl + iyl? y[i - ixl] : '\\0';\n"
        "}\n";

bool testTaskParallel(const OpenCLContext::DeviceType dev) {
    gc_scoped_pool pool;
    OpenCLContext cl(dev);
    if (!devices(cl) != 0)
        return true;

    {
        istringstream is(dev == OpenCLContext::CPU? testFloatsCPU : testFloatsGPU);
        failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
        assert(hasContent(clprog));
        const value exp = mklist<value>("float_add", 2, 3);
        const failable<value> r = evalKernel(createKernel(exp, content(clprog)), exp, cl);
        assert(hasContent(r));
        assert(content(r) == value(5));
    }
    if (true) return true;
    {
        istringstream is(dev == OpenCLContext::CPU? testFloatsCPU : testFloatsGPU);
        failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
        assert(hasContent(clprog));
        const value exp = mklist<value>("add", 2, 3);
        const failable<value> r = evalKernel(createKernel(exp, content(clprog)), exp, 1, value::Number, 1, cl);
        assert(hasContent(r));
        assert(content(r) == value(5));
    }
    {
        istringstream is(dev == OpenCLContext::CPU? testBoolsCPU : testBoolsGPU);
        failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
        assert(hasContent(clprog));

        const value exp = mklist<value>("int_or", true, false);
        const failable<value> r = evalKernel(createKernel(exp, content(clprog)), exp, cl);
        assert(hasContent(r));
        assert(content(r) == value(true));

        const value exp2 = mklist<value>("int_or", false, false);
        const failable<value> r2 = evalKernel(createKernel(exp2, content(clprog)), exp2, cl);
        assert(hasContent(r2));
        assert(content(r2) == value(false));
    }
    {
        istringstream is(dev == OpenCLContext::CPU? testCharsCPU : testCharsGPU);
        failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
        assert(hasContent(clprog));

        const value exp = mklist<value>("char128", 60, string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello "), 60, string("World World World World World World World World World World "));
        const failable<value> r = evalKernel(createKernel(exp, content(clprog)), exp, cl);
        assert(hasContent(r));
        assert(content(r) == value(string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello World World World World World World World World World World ")));
    }
    {
        istringstream is(dev == OpenCLContext::CPU? testCharsCPU : testCharsGPU);
        failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
        assert(hasContent(clprog));

        const value exp = mklist<value>("add", 60, string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello "), 60, string("World World World World World World World World World World "));
        const failable<value> r = evalKernel(createKernel(exp, content(clprog)), exp, 1, value::String, 128, cl);
        assert(hasContent(r));
        assert(content(r) == value(string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello World World World World World World World World World World ")));
    }

    return true;
}

struct evalTaskParallelLoop {
    evalTaskParallelLoop(const OpenCLContext& cl, const OpenCLProgram& clprog) : cl(cl), clprog(clprog), exp(mklist<value>("add", 60, string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello "), 60, string("World World World World World World World World World World "))) {
    }
    const bool operator()() const {
        const failable<value> r = evalKernel(createKernel(exp, clprog), exp, 1, value::String, 128, cl);
        assert(hasContent(r));
        assert(content(r) == value(string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello World World World World World World World World World World ")));
        return true;
    }

    const OpenCLContext& cl;
    const OpenCLProgram& clprog;
    const value exp;
};

const bool testTaskParallelPerf(const OpenCLContext::DeviceType dev, const bool copy) {
    gc_scoped_pool pool;
    OpenCLContext cl(dev);
    if (!devices(cl) != 0)
        return true;

    istringstream is(dev == OpenCLContext::CPU? testCharsCPU : copy? testCharsCopyGPU : testCharsGPU);
    failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
    assert(hasContent(clprog));

    resetOpenCLCounters();
    const lambda<bool()> el = evalTaskParallelLoop(cl, content(clprog));
    cout << "OpenCL task-parallel eval " << (dev == OpenCLContext::CPU? "CPU" : "GPU") << (copy? " copy" : "") << " test " << time(el, 5, 500) << " ms";
    printOpenCLCounters(500);
    cout << endl;
    return true;
}

bool testDataParallel(const OpenCLContext::DeviceType dev) {
    gc_scoped_pool pool;
    OpenCLContext cl(dev);
    if (!devices(cl) != 0)
        return true;

    {
        istringstream is(dev == OpenCLContext::CPU? testCharsParallelCPU : testCharsParallelGPU);
        failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
        assert(hasContent(clprog));

        const value exp = mklist<value>("add", 6, string("Hello "), 5, string("World"));
        const failable<value> r = evalKernel(createKernel(exp, content(clprog)), exp, 121, value::String, 128, cl);
        assert(hasContent(r));
        assert(content(r) == value(string("Hello World")));
    }

    return true;
}

struct evalDataParallelLoop {
    evalDataParallelLoop(const OpenCLContext& cl, const OpenCLProgram& clprog) : cl(cl), clprog(clprog), exp(mklist<value>("add", 60, string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello "), 60, string("World World World World World World World World World World "))) {
    }
    const bool operator()() const {
        const failable<value> r = evalKernel(createKernel(exp, clprog), exp, 121, value::String, 128, cl);
        assert(hasContent(r));
        assert(content(r) == value(string("Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello World World World World World World World World World World ")));
        return true;
    }

    const OpenCLContext& cl;
    const OpenCLProgram& clprog;
    const value exp;
};

const bool testDataParallelPerf(const OpenCLContext::DeviceType dev, const bool copy) {
    gc_scoped_pool pool;
    OpenCLContext cl(dev);
    if (!devices(cl) != 0)
        return true;

    istringstream is(dev == OpenCLContext::CPU? testCharsParallelCPU : copy? testCharsParallelCopyGPU : testCharsParallelGPU);
    failable<OpenCLProgram> clprog = readProgram("kernel.cl", is, cl);
    assert(hasContent(clprog));

    resetOpenCLCounters();
    const lambda<bool()> el = evalDataParallelLoop(cl, content(clprog));
    cout << "OpenCL data-parallel eval " << (dev == OpenCLContext::CPU? "CPU" : "GPU") << (copy? " copy" : "") << " test " << time(el, 5, 500) << " ms";
    printOpenCLCounters(500);
    cout << endl;
    return true;
}

}
}

int main() {
    tuscany::cout << "Testing..." << tuscany::endl;

    tuscany::opencl::testTaskParallel(tuscany::opencl::OpenCLContext::CPU);
    tuscany::opencl::testTaskParallelPerf(tuscany::opencl::OpenCLContext::CPU, false);
    tuscany::opencl::testDataParallel(tuscany::opencl::OpenCLContext::CPU);
    tuscany::opencl::testDataParallelPerf(tuscany::opencl::OpenCLContext::CPU, false);

    tuscany::opencl::testTaskParallel(tuscany::opencl::OpenCLContext::GPU);
    tuscany::opencl::testTaskParallelPerf(tuscany::opencl::OpenCLContext::GPU, false);
    tuscany::opencl::testTaskParallelPerf(tuscany::opencl::OpenCLContext::GPU, true);
    tuscany::opencl::testDataParallel(tuscany::opencl::OpenCLContext::GPU);
    tuscany::opencl::testDataParallelPerf(tuscany::opencl::OpenCLContext::GPU, false);
    tuscany::opencl::testDataParallelPerf(tuscany::opencl::OpenCLContext::GPU, true);

    tuscany::cout << "OK" << tuscany::endl;
    return 0;
}