assertOpenCL  September 19, 2018
example.cl
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file examples/kernel/example.cl
3  * \brief
4  * Simple example of use of assert*() and PRINT*() macros.
5  *
6  * Piece of assertOpenCL
7  * --- GPLv3 --- Copyright (C) 2018 Olivier Pirson
8  * --- http://www.opimedia.be/
9  * --- September 19, 2018
10  */
11 
12 #include "assert.cl" // include assert* macros
13 #include "print.cl" // include PRINT* macros
14 
15 
16 /* ***********
17  * Functions *
18  *************/
19 
20 /**
21  * Example of function without return value
22  * that contains assertions.
23  */
24 void
25 foo(ASSERT_DECLARE_UNIQUE_PARAMS) { // in debug mode add extra parameters
26  assert_n_x(false, -1, 0.5); // check assertion and set if failed
27  // This is the first assertion that it fails in this example.
28 
29  assert_n_x_r(get_global_id(0) == -1, 123, 124.0); // check assertion and set and return if failed
30  // This assertion fails, but only printing,
31  // doesn't set values because there has already a previous assertion that has failed.
32 
33  assert_n_x(false, 123, 124.0); // do nothing or never executed
34 }
35 
36 
37 /**
38  * Example of function without return value
39  * that contains assertions.
40  *
41  * @param n
42  */
43 void
44 foo2(unsigned int n // be careful, do not add comma
45  ASSERT_DECLARE_PARAMS) { // in debug mode add extra parameters
46  assert(n == 0); // check assertion
47  assert_r(n == 0); // check assertion and return if failed
48  assert(n == 0); // do nothing or never executed
49 }
50 
51 
52 /**
53  * Example of function with a return value
54  * that contains assertions.
55  *
56  * @param n
57  *
58  * @return n*2
59  */
60 unsigned int
61 bar(unsigned int n // be careful, do not add comma
62  ASSERT_DECLARE_PARAMS) { // in debug mode add extra parameters
63  assert_r0(n == 0); // check assertion and return 0 if failed
64  assert_r0(n == 0); // do nothing or never executed
65 
66  return n * 2;
67 }
68 
69 
70 
71 /* *****************
72  * Kernel function *
73  *******************/
74 
75 /**
76  * Example of kernel function that contains assertions.
77  *
78  * @param in
79  * @param outs
80  */
81 __kernel
82 void
83 example(const unsigned int in,
84  __global unsigned int* const outs // be careful, do not add comma
85  ASSERT_DECLARE_PARAMS) { // in debug mode add extra parameters
86  const unsigned int g_id = get_global_id(0);
87  const unsigned int l_id = get_local_id(0);
88 
89 
90  // Printing example (probably mixed between all work items)
91  PRINT2("global, local id: %u, %u\n", g_id, l_id);
92 
93  for (unsigned int i = 0; i < 1000000; ++i) { // artificial wait
94  barrier(CLK_GLOBAL_MEM_FENCE | CLK_LOCAL_MEM_FENCE);
95  }
96 
97 
98  // Some assertion examples that succeed
99  assert(in == 666);
100  assert_n(in == 666, 42);
101  assert_x(in == 666, 3.14);
102  assert_n_x(in == 666, 42, 3.14);
103 
104  assert_r(in == 666);
105  assert_n_r(in == 666, 42);
106  assert_x_r(in == 666, 3.14);
107  assert_n_x_r(in == 666, 42, 3.14);
108 
109  assert_n(sizeof(unsigned int) == 4, sizeof(unsigned int));
110  assert_n(sizeof(float) == 4, sizeof(float));
111 
112  if (g_id == 0) { // only the first work item, to avoid mixed printing
113  outs[0] = in;
114  outs[1] = 42;
115 
116 
117  // Some printing examples
118  PRINT1("in: %u\n", in);
119 
120  PRINT("PRINT message\n");
121  PRINT1("PRINT1 %u\n", 42);
122  PRINT2("PRINT2 %u %i\n", 42, -1);
123  PRINT3("PRINT2 %u %i %f\n", 42, -1, 3.14);
124 
125 #define NPRINT
126 #include "print.cl" // include again PRINT* macros, but with NPRINT defined
127 
128 
129  // The following printing examples are disabled
130  PRINT("PRINT message\n");
131  PRINT1("PRINT1 %u\n", 42);
132  PRINT2("PRINT2 %u %i\n", 42, -1);
133  PRINT3("PRINT2 %u %i %f\n", 42, -1, 3.14);
134 
135 
136  // Call functions that contain assertions
137  foo(ASSERT_UNIQUE_PARAMS); // in debug mode transmits extra parameters
138 
139  foo2(42 // be careful, do not add comma
140  ASSERT_PARAMS); // in debug mode transmits extra parameters
141 
142  bar(42 // be careful, do not add comma
143  ASSERT_PARAMS); // in debug mode transmits extra parameters
144 
145 
146  // Some assertions
147  assert(in == 0); // assertion failed
148  assert_n(in == 0, 42); // assertion failed
149  assert_n(in == 0, -42); // assertion failed
150  assert_x(in == 0, 3.14); // assertion failed
151  assert_n_x(in == 0, 666, 3.14); // assertion failed
152 
153 
154 #ifndef NASSERT
155 # define NASSERT_NOT_DEFINED
156 #endif
157 
158 #define NASSERT
159 #include "assert.cl" // include again assert* macros, but with NASSERT defined
160  assert(in == 0); // assertion disabled
161  assert_n(in == 0, 42); // assertion disabled
162  assert_n(in == 0, -42); // assertion disabled
163  assert_x(in == 0, 3.14); // assertion disabled
164  assert_n_x(in == 0, 666, 3.14); // assertion disabled
165 
166  assert_r(in == 0); // assertion disabled
167  assert_n_r(in == 0, 42); // assertion disabled
168  assert_n_r(in == 0, -42); // assertion disabled
169  assert_x_r(in == 0, 3.14); // assertion disabled
170  assert_n_x_r(in == 0, 666, 3.14); // assertion disabled
171 
172 
173 #ifdef NASSERT_NOT_DEFINED
174 # undef NASSERT
175 #endif
176 #include "assert.cl" // include again assert* macros, but with NASSERT not defined
177 
178  assert(in == 0); // assertion failed
179  assert_n(in == 0, 42); // assertion failed
180  assert_n(in == 0, -42); // assertion failed
181  assert_x(in == 0, 3.14); // assertion failed
182  assert_n_x(in == 0, 111, 1.23); // assertion failed
183 
184  last_assert_n_x(in == 0, -222, 2.34); // assertion failed and set new values
185 
186  assert_r(in == 0); // assertion failed and return
187  assert_n_r(in == 0, 42); // do nothing or never executed
188  assert_n_r(in == 0, -42); // do nothing or never executed
189  assert_x_r(in == 0, 3.14); // do nothing or never executed
190  assert_n_x_r(in == 0, 666, 3.14); // do nothing or never executed
191  }
192 }
#define assert_x(test, float_value)
If test is true then do nothing. Else init (if they are still null) assert_result and assert_result_f...
Definition: assert.cl:192
#define assert_n(test, integer_value)
If test is true then do nothing. Else init (if they are still null) assert_result and assert_result_f...
Definition: assert.cl:177
unsigned int bar(unsigned int n ASSERT_DECLARE_PARAMS)
Definition: example.cl:61
#define assert_n_x_r(test, integer_value, float_value)
Like assert_n_x() but if assertion failed then return.
Definition: assert.cl:272
#define assert_n_r(test, integer_value)
Like assert_n() but if assertion failed then return.
Definition: assert.cl:239
#define ASSERT_UNIQUE_PARAMS
Transmit the extra parameters in the calling of a function with ASSERT_DECLARE_UNIQUE_PARAM.
Definition: assert.cl:130
void foo2(unsigned int n ASSERT_DECLARE_PARAMS)
Definition: example.cl:44
void foo(ASSERT_DECLARE_UNIQUE_PARAMS)
Definition: example.cl:25
#define ASSERT_PARAMS
Transmit the extra parameters in the calling of a function with ASSERT_DECLARE_PARAM.
Definition: assert.cl:136
#define last_assert_n_x(test, integer_value, float_value)
If test is true then do nothing. Else init (if they are still null) assert_result and assert_result_f...
Definition: assert.cl:402
#define assert_x_r(test, float_value)
Like assert_x() but if assertion failed then return.
Definition: assert.cl:255
#define assert_n_x(test, integer_value, float_value)
If test is true then do nothing. Else init (if they are still null) assert_result and assert_result_f...
Definition: assert.cl:208
#define assert(test)
If test is true then do nothing. Else init (if they are still null) assert_result and assert_result_f...
Definition: assert.cl:162
#define assert_r(test)
Like assert() but if assertion failed then return.
Definition: assert.cl:223
#define ASSERT_DECLARE_PARAMS
Declare extra parameters in the signature (with other params) of kernel function or other functions t...
Definition: assert.cl:124
#define assert_r0(test)
Like assert() but if assertion failed then return 0.
Definition: assert.cl:288
#define ASSERT_DECLARE_UNIQUE_PARAMS
Declare extra parameters in the signature (without other params) of kernel function or other function...
Definition: assert.cl:118
C-like assert alternative (because assert doesn&#39;t exist in OpenCL), with necessary helper macros to t...
__kernel void example(const unsigned int in, __global unsigned int *const outs ASSERT_DECLARE_PARAMS)
Definition: example.cl:83