summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/compute/test/test_program.cpp
blob: 7172c5e25ef271cb0532b29c4ee50e1bb5a268f6 (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
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestProgram
#include <boost/test/unit_test.hpp>

// disable the automatic kernel compilation debug messages. this allows the
// test for program to check that compilation error exceptions are properly
// thrown when invalid kernel code is passed to program::build().
#undef BOOST_COMPUTE_DEBUG_KERNEL_COMPILATION

#include <boost/compute/exception/program_build_failure.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/program.hpp>
#include <boost/compute/utility/source.hpp>

#include "quirks.hpp"
#include "context_setup.hpp"

namespace compute = boost::compute;

const char source[] =
    "__kernel void foo(__global float *x, const uint n) { }\n"
    "__kernel void bar(__global int *x, __global int *y) { }\n";


BOOST_AUTO_TEST_CASE(get_program_info)
{
    // create program
    boost::compute::program program =
        boost::compute::program::create_with_source(source, context);

    // build program
    program.build();

    // check program info
#ifndef BOOST_COMPUTE_USE_OFFLINE_CACHE
    BOOST_CHECK(program.source().empty() == false);
#endif
    BOOST_CHECK(program.get_context() == context);
}

BOOST_AUTO_TEST_CASE(program_source)
{
    // create program from source
    boost::compute::program program =
        boost::compute::program::create_with_source(source, context);

    BOOST_CHECK_EQUAL(std::string(source), program.source());
}

BOOST_AUTO_TEST_CASE(program_multiple_sources)
{
    std::vector<std::string> sources;
    sources.push_back("__kernel void foo(__global int* x) { }\n");
    sources.push_back("__kernel void bar(__global float* y) { }\n");

    // create program from sources
    boost::compute::program program =
        boost::compute::program::create_with_source(sources, context);
    program.build();

    boost::compute::kernel foo = program.create_kernel("foo");
    boost::compute::kernel bar = program.create_kernel("bar");
}

BOOST_AUTO_TEST_CASE(program_source_no_file)
{
    // create program from a non-existant source file
    // and verifies it throws.
    BOOST_CHECK_THROW(boost::compute::program program =
                      boost::compute::program::create_with_source_file
                      (std::string(), context),
                      std::ios_base::failure);
}

BOOST_AUTO_TEST_CASE(create_kernel)
{
    boost::compute::program program =
        boost::compute::program::create_with_source(source, context);
    program.build();

    boost::compute::kernel foo = program.create_kernel("foo");
    boost::compute::kernel bar = program.create_kernel("bar");

    // try to create a kernel that doesn't exist
    BOOST_CHECK_THROW(program.create_kernel("baz"), boost::compute::opencl_error);
}

BOOST_AUTO_TEST_CASE(create_with_binary)
{
    // create program from source
    boost::compute::program source_program =
        boost::compute::program::create_with_source(source, context);
    source_program.build();

    // create kernels in source program
    boost::compute::kernel source_foo_kernel = source_program.create_kernel("foo");
    boost::compute::kernel source_bar_kernel = source_program.create_kernel("bar");

    // check source kernels
    BOOST_CHECK_EQUAL(source_foo_kernel.name(), std::string("foo"));
    BOOST_CHECK_EQUAL(source_bar_kernel.name(), std::string("bar"));

    // get binary
    std::vector<unsigned char> binary = source_program.binary();

    // create program from binary
    boost::compute::program binary_program =
        boost::compute::program::create_with_binary(binary, context);
    binary_program.build();

    // create kernels in binary program
    boost::compute::kernel binary_foo_kernel = binary_program.create_kernel("foo");
    boost::compute::kernel binary_bar_kernel = binary_program.create_kernel("bar");

    // check binary kernels
    BOOST_CHECK_EQUAL(binary_foo_kernel.name(), std::string("foo"));
    BOOST_CHECK_EQUAL(binary_bar_kernel.name(), std::string("bar"));
}

#ifdef BOOST_COMPUTE_CL_VERSION_2_1
BOOST_AUTO_TEST_CASE(create_with_il)
{
    REQUIRES_OPENCL_VERSION(2, 1);

    size_t device_address_space_size = device.address_bits();
    std::string file_path(BOOST_COMPUTE_TEST_DATA_PATH);
    if(device_address_space_size == 64)
    {
        file_path += "/program.spirv64";
    }
    else
    {
        file_path += "/program.spirv32";
    }

    // create program from il
    boost::compute::program il_program;
    BOOST_CHECK_NO_THROW(
        il_program = boost::compute::program::create_with_il_file(
            file_path, context
        )
    );
    BOOST_CHECK_NO_THROW(il_program.build());

    // create kernel (to check if program was loaded correctly)
    BOOST_CHECK_NO_THROW(il_program.create_kernel("foobar"));
}

BOOST_AUTO_TEST_CASE(get_program_il_binary)
{
    REQUIRES_OPENCL_VERSION(2, 1);

    size_t device_address_space_size = device.address_bits();
    std::string file_path(BOOST_COMPUTE_TEST_DATA_PATH);
    if(device_address_space_size == 64)
    {
        file_path += "/program.spirv64";
    }
    else
    {
        file_path += "/program.spirv32";
    }

    // create program from il
    boost::compute::program il_program;
    BOOST_CHECK_NO_THROW(
        il_program = boost::compute::program::create_with_il_file(
            file_path, context
        )
    );
    BOOST_CHECK_NO_THROW(il_program.build());

    std::vector<unsigned char> il_binary;
    BOOST_CHECK_NO_THROW(il_binary = il_program.il_binary());

    // create program from loaded il binary
    BOOST_CHECK_NO_THROW(
        il_program = boost::compute::program::create_with_il(il_binary, context)
    );
    BOOST_CHECK_NO_THROW(il_program.build());

    // create kernel (to check if program was loaded correctly)
    BOOST_CHECK_NO_THROW(il_program.create_kernel("foobar"));
}

BOOST_AUTO_TEST_CASE(get_program_il_binary_empty)
{
    REQUIRES_OPENCL_VERSION(2, 1);

    boost::compute::program program;
    BOOST_CHECK_NO_THROW(
        program = boost::compute::program::create_with_source(source, context)
    );
    BOOST_CHECK_NO_THROW(program.build());

    std::vector<unsigned char> il_binary;
    il_binary = program.il_binary();
    BOOST_CHECK(il_binary.empty());
}
#endif // BOOST_COMPUTE_CL_VERSION_2_1

BOOST_AUTO_TEST_CASE(create_with_source_doctest)
{
//! [create_with_source]
std::string source = "__kernel void foo(__global int *data) { }";

boost::compute::program foo_program =
    boost::compute::program::create_with_source(source, context);
//! [create_with_source]

    foo_program.build();
}

#ifdef BOOST_COMPUTE_CL_VERSION_1_2
BOOST_AUTO_TEST_CASE(compile_and_link)
{
    REQUIRES_OPENCL_VERSION(1,2);

    if(!supports_compile_program(device) || !supports_link_program(device)) {
        return;
    }

    // create the library program
    const char library_source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
        // for some reason the apple opencl compilers complains if a prototype
        // for the square() function is not available, so we add it here
        T square(T);

        // generic square function definition
        T square(T x) { return x * x; }
    );

    compute::program library_program =
        compute::program::create_with_source(library_source, context);

    library_program.compile("-DT=int");

    // create the kernel program
    const char kernel_source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
        // forward declare square function
        extern int square(int);

        // square kernel definition
        __kernel void square_kernel(__global int *x)
        {
            x[0] = square(x[0]);
        }
    );

    compute::program square_program =
        compute::program::create_with_source(kernel_source, context);

    square_program.compile();

    // link the programs
    std::vector<compute::program> programs;
    programs.push_back(library_program);
    programs.push_back(square_program);

    compute::program linked_program =
        compute::program::link(programs, context);

    // create the square kernel
    compute::kernel square_kernel =
        linked_program.create_kernel("square_kernel");
    BOOST_CHECK_EQUAL(square_kernel.name(), "square_kernel");
}

BOOST_AUTO_TEST_CASE(compile_and_link_with_headers)
{
    REQUIRES_OPENCL_VERSION(1,2);

    if(!supports_compile_program(device) || !supports_link_program(device)) {
        return;
    }

    // create the header programs
    const char square_header_source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
        T square(T x) { return x * x; }
    );
    const char div2_header_source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
        T div2(T x) { return x / 2; }
    );

    compute::program square_header_program =
        compute::program::create_with_source(square_header_source, context);
    compute::program div2_header_program =
        compute::program::create_with_source(div2_header_source, context);

    // create the kernel program
    const char kernel_source[] =
        "#include \"square.h\"\n"
        "#include \"div2.h\"\n"
        "__kernel void squareby2_kernel(__global int *x)"
        "{"
        "    x[0] = div2(square(x[0]));"
        "}";

    compute::program square_program =
        compute::program::create_with_source(kernel_source, context);

    std::vector<std::pair<std::string, compute::program> > header_programs;
    header_programs.push_back(std::make_pair("square.h", square_header_program));
    header_programs.push_back(std::make_pair("div2.h", div2_header_program));

    square_program.compile("-DT=int", header_programs);

    // link program
    std::vector<compute::program> programs;
    programs.push_back(square_program);

    compute::program linked_program =
        compute::program::link(programs, context);

    // create the square kernel
    compute::kernel square_kernel =
        linked_program.create_kernel("squareby2_kernel");
    BOOST_CHECK_EQUAL(square_kernel.name(), "squareby2_kernel");
}
#endif // BOOST_COMPUTE_CL_VERSION_1_2

BOOST_AUTO_TEST_CASE(build_log)
{
    const char invalid_source[] =
        "__kernel void foo(__global int *input) { !@#$%^&*() }";

    compute::program invalid_program =
        compute::program::create_with_source(invalid_source, context);

    try {
        invalid_program.build();

        // should not get here
        BOOST_CHECK(false);
    }
    catch(compute::opencl_error&){
        std::string log = invalid_program.build_log();
        BOOST_CHECK(!log.empty());
    }
}

BOOST_AUTO_TEST_CASE(program_build_exception)
{
    const char invalid_source[] =
        "__kernel void foo(__global int *input) { !@#$%^&*() }";

    compute::program invalid_program =
        compute::program::create_with_source(invalid_source, context);

    BOOST_CHECK_THROW(invalid_program.build(),
                      compute::program_build_failure);

    try {
        // POCL bug: https://github.com/pocl/pocl/issues/577
        if(pocl_bug_issue_577(device))
        {
            invalid_program =
                compute::program::create_with_source(invalid_source, context);
        }
        invalid_program.build();

        // should not get here
        BOOST_CHECK(false);
    }
    catch(compute::program_build_failure& e){
        BOOST_CHECK(e.build_log() == invalid_program.build_log());
    }
    catch(...)
    {
        // should not get here
        BOOST_CHECK(false);
    }
}

BOOST_AUTO_TEST_CASE(build_with_source_exception)
{
    const char invalid_source[] =
        "__kernel void foo(__global int *input) { !@#$%^&*() }";

    BOOST_CHECK_THROW(compute::program::build_with_source(invalid_source, context),
        compute::program_build_failure);
}

BOOST_AUTO_TEST_CASE(build_with_source_file_exception)
{
    std::string file_path(BOOST_COMPUTE_TEST_DATA_PATH "/invalid_program.cl");
    BOOST_CHECK_THROW(compute::program::build_with_source_file(file_path, context),
        compute::program_build_failure);
}

BOOST_AUTO_TEST_SUITE_END()