Debian Codegraph @

libgomp/testsuite/libgomp.c-c++-common/interop-routines-2.c

raw
1 /* { dg-do run { target { offload_device } } } */
2
3 /* OpenMP permits using the interop functions on the device,
4 but it is not really supported. Hence, check that the stubs
5 are working. */
6
7
8 #include <stddef.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <omp.h>
12
13 /* Assert is not available with newlib on the device side, hence... */
14 #define assert(cond) \
15 do { \
16 if (!(cond)) \
17 { \
18 __builtin_printf ("assert failed: " #cond "\n"); \
19 abort (); \
20 } \
21 } \
22 while (0)
23
24 #pragma omp begin declare target
25 void
26 target_test ()
27 {
28 if (omp_is_initial_device ())
29 return; /* Already tested in interop-routines-1.c. */
30
31 omp_interop_t interop = omp_interop_none;
32
33 assert (omp_irc_no_value == 1);
34 assert (omp_irc_success == 0);
35 assert (omp_irc_empty == -1);
36 assert (omp_irc_out_of_range == -2);
37 assert (omp_irc_type_int == -3);
38 assert (omp_irc_type_ptr == -4);
39 assert (omp_irc_type_str == -5);
40 assert (omp_irc_other == -6);
41
42 /* Check values, including invalid values. */
43 for (omp_interop_rc_t ret_code3 = (omp_interop_rc_t) ((int) omp_irc_other - 1);
44 ret_code3 <= omp_irc_no_value + 1;
45 ret_code3 = (omp_interop_rc_t) ((int) ret_code3 + 1))
46 {
47 const char *msg = omp_get_interop_rc_desc (interop, ret_code3);
48 if (ret_code3 < omp_irc_other || ret_code3 > omp_irc_no_value)
49 /* Assume NULL for an invalid value. */
50 assert (msg == NULL);
51 else if (ret_code3 == omp_irc_other)
52 /* In GCC, this is used on the device side, only, if the
53 omp_get_interop_{int,ptr,str} were invoked on the host.
54 see below for a check. */
55 assert (msg != NULL && strlen (msg) > 5); /* Some sensible message. */
56 else
57 /* Assume that omp_get_interop_rc_desc handles all of those and
58 not only omp_irc_empty (and possibly omp_irc_out_of_range),
59 which do occur for omp_interop_none. */
60 assert (msg != NULL && strlen (msg) > 5); /* Some sensible message. */
61 }
62
63 assert (omp_ifr_last >= omp_ifr_hsa);
64
65 for (omp_interop_fr_t fr = omp_ifr_cuda; fr <= omp_ifr_last;
66 fr = (omp_interop_fr_t) ((int) fr + 1))
67 {
68 switch (fr)
69 {
70 /* Expect the id values from the additional-definition document. */
71 case omp_ifr_cuda:
72 if (fr != 1)
73 abort ();
74 break;
75 case omp_ifr_cuda_driver:
76 if (fr != 2)
77 abort ();
78 break;
79 case omp_ifr_opencl:
80 if (fr != 3)
81 abort ();
82 break;
83 case omp_ifr_sycl:
84 if (fr != 4)
85 abort ();
86 break;
87 case omp_ifr_hip:
88 if (fr != 5)
89 abort ();
90 break;
91 case omp_ifr_level_zero:
92 if (fr != 6)
93 abort ();
94 break;
95 case omp_ifr_hsa:
96 if (fr != 7)
97 abort ();
98 break;
99 default:
100 /* Valid, but unexpected to have more interop types. */
101 abort ();
102 }
103 }
104
105 assert (omp_ipr_first <= omp_ipr_targetsync
106 && omp_get_num_interop_properties (interop) > omp_ipr_fr_id);
107
108 for (omp_interop_property_t ipr = omp_ipr_first;
109 ipr < omp_get_num_interop_properties (interop);
110 ipr = (omp_interop_property_t) ((int) ipr + 1))
111 {
112 /* As interop == omp_interop_none, NULL is permissible;
113 nonetheless, require != NULL for the GCC implementation. */
114 const char *name = omp_get_interop_name (interop, ipr);
115 if (name == NULL)
116 abort ();
117 switch (ipr)
118 {
119 case omp_ipr_fr_id:
120 if (ipr != -1 || !!strcmp (name, "fr_id"))
121 abort ();
122 break;
123 case omp_ipr_fr_name:
124 if (ipr != -2 || !!strcmp (name, "fr_name"))
125 abort ();
126 break;
127 case omp_ipr_vendor:
128 if (ipr != -3 || !!strcmp (name, "vendor"))
129 abort ();
130 break;
131 case omp_ipr_vendor_name:
132 if (ipr != -4 || !!strcmp (name, "vendor_name"))
133 abort ();
134 break;
135 case omp_ipr_device_num:
136 if (ipr != -5 || !!strcmp (name, "device_num"))
137 abort ();
138 break;
139 case omp_ipr_platform:
140 if (ipr != -6 || !!strcmp (name, "platform"))
141 abort ();
142 break;
143 case omp_ipr_device:
144 if (ipr != -7 || !!strcmp (name, "device"))
145 abort ();
146 break;
147 case omp_ipr_device_context:
148 if (ipr != -8 || !!strcmp (name, "device_context"))
149 abort ();
150 break;
151 case omp_ipr_targetsync:
152 if (ipr != -9 || !!strcmp (name, "targetsync"))
153 abort ();
154 break;
155 default:
156 /* Valid, but unexpected to have more interop types,
157 especially not for interop == omp_interop_none. */
158 abort ();
159 }
160
161 /* As interop == omp_interop_none, expect NULL. */
162 if (omp_get_interop_type_desc (interop, ipr) != NULL)
163 abort ();
164
165 omp_interop_rc_t ret_code;
166 const char *err;
167
168 ret_code = omp_irc_success;
169 omp_intptr_t ival = omp_get_interop_int (interop, ipr, &ret_code);
170 assert (ret_code == omp_irc_empty); /* As interop == omp_interop_none. */
171 assert (ival == 0); /* Implementation choice. */
172 err = omp_get_interop_rc_desc (interop, ret_code);
173 assert (err != NULL && strlen (err) > 5); /* Some sensible message. */
174 assert (!strcmp (err, "provided interoperability object is equal to "
175 "omp_interop_none")); /* GCC implementation choice. */
176 ival = omp_get_interop_int (interop, ipr, NULL);
177 assert (ival == 0); /* Implementation choice. */
178 #ifdef __cplusplus
179 ival = omp_get_interop_int (interop, ipr);
180 assert (ival == 0); /* Implementation choice. */
181 #endif
182
183 ret_code = omp_irc_success;
184 void *ptr = omp_get_interop_ptr (interop, ipr, &ret_code);
185 assert (ret_code == omp_irc_empty); /* As interop == omp_interop_none. */
186 assert (ptr == NULL); /* Obvious implementation choice. */
187 err = omp_get_interop_rc_desc (interop, ret_code);
188 assert (err != NULL && strlen (err) > 5); /* Some sensible message. */
189 assert (!strcmp (err, "provided interoperability object is equal to "
190 "omp_interop_none")); /* GCC implementation choice. */
191 ptr = omp_get_interop_ptr (interop, ipr, NULL);
192 assert (ptr == NULL); /* Obvious implementation choice. */
193 #ifdef __cplusplus
194 ptr = omp_get_interop_ptr (interop, ipr);
195 assert (ptr == NULL); /* Obvious implementation choice. */
196 #endif
197
198 ret_code = omp_irc_success;
199 const char *str = omp_get_interop_str (interop, ipr, &ret_code);
200 assert (ret_code == omp_irc_empty); /* As interop == omp_interop_none. */
201 assert (str == NULL); /* Obvious implementation choice. */
202 err = omp_get_interop_rc_desc (interop, ret_code);
203 assert (err != NULL && strlen (err) > 5); /* Some sensible message. */
204 assert (!strcmp (err, "provided interoperability object is equal to "
205 "omp_interop_none")); /* GCC implementation choice. */
206 str = omp_get_interop_str (interop, ipr, NULL);
207 assert (str == NULL); /* Obvious implementation choice. */
208 #ifdef __cplusplus
209 str = omp_get_interop_str (interop, ipr);
210 assert (str == NULL); /* Obvious implementation choice. */
211 #endif
212
213 /* Special case of GCC: For any non-'omp_interop_none' valued interop,
214 a device-side call to omp_get_interop_{int,ptr,src} will yield
215 omp_irc_other - with the error message as checked below. */
216
217 omp_interop_t interop_not_none_invalid = (omp_interop_t) 0xDEADBEEF;
218
219 ret_code = omp_irc_success;
220 ival = omp_get_interop_int (interop_not_none_invalid, ipr, &ret_code);
221 assert (ret_code == omp_irc_other);
222 assert (ival == 0); /* Implementation choice. */
223 err = omp_get_interop_rc_desc (interop, ret_code);
224 assert (err != NULL && strlen (err) > 5); /* Some sensible message. */
225
226 ret_code = omp_irc_success;
227 ptr = omp_get_interop_ptr (interop_not_none_invalid, ipr, &ret_code);
228 assert (ret_code == omp_irc_other);
229 assert (ptr == NULL); /* Implementation choice. */
230 err = omp_get_interop_rc_desc (interop, ret_code);
231 assert (err != NULL && strlen (err) > 5); /* Some sensible message. */
232 assert (!strcmp (err, "obtaining properties is only supported on the "
233 "initial device")); /* GCC special case. */
234
235 ret_code = omp_irc_success;
236 str = omp_get_interop_str (interop_not_none_invalid, ipr, &ret_code);
237 assert (ret_code == omp_irc_other);
238 assert (str == NULL); /* Implementation choice. */
239 err = omp_get_interop_rc_desc (interop, ret_code);
240 assert (!strcmp (err, "obtaining properties is only supported on the "
241 "initial device")); /* GCC special case. */
242 }
243
244 /* Invalid ipr. */
245 /* Valid are either omp_irc_empty (due to omp_interop_none) or
246 omp_irc_out_of_range; assume omp_irc_out_of_range with GCC. */
247
248 omp_interop_rc_t ret_code2;
249 const char *err2;
250 omp_intptr_t ival2;
251 void *ptr2;
252 const char *str2;
253
254 /* omp_ipr_targetsync-1, i.e < lower bound. */
255
256 ret_code2 = omp_irc_success;
257 ival2 = omp_get_interop_int (interop,
258 (omp_interop_property_t) ((int) omp_ipr_targetsync-1), &ret_code2);
259 assert (ret_code2 == omp_irc_out_of_range);
260 assert (ival2 == 0); /* Implementation choice. */
261 err2 = omp_get_interop_rc_desc (interop, ret_code2);
262 assert (err2 != NULL && strlen (err2) > 5); /* Some sensible message. */
263 /* GCC implementation choice. */
264 assert (!strcmp (err2, "property ID is out of range"));
265 ival2 = omp_get_interop_int (interop,
266 (omp_interop_property_t) ((int) omp_ipr_targetsync-1), NULL);
267 assert (ival2 == 0); /* Implementation choice. */
268 #ifdef __cplusplus
269 ival2 = omp_get_interop_int (interop,
270 (omp_interop_property_t) ((int) omp_ipr_targetsync-1));
271 assert (ival2 == 0); /* Implementation choice. */
272 #endif
273
274 ret_code2 = omp_irc_success;
275 ptr2 = omp_get_interop_ptr (interop,
276 (omp_interop_property_t) ((int) omp_ipr_targetsync-1), &ret_code2);
277 assert (ret_code2 == omp_irc_out_of_range);
278 assert (ptr2 == NULL); /* Obvious implementation choice. */
279 err2 = omp_get_interop_rc_desc (interop, ret_code2);
280 assert (err2 != NULL && strlen (err2) > 5); /* Some sensible message. */
281 /* GCC implementation choice. */
282 assert (!strcmp (err2, "property ID is out of range"));
283 ptr2 = omp_get_interop_ptr (interop,
284 (omp_interop_property_t) ((int) omp_ipr_targetsync-1), NULL);
285 assert (ptr2 == NULL); /* Obvious implementation choice. */
286 #ifdef __cplusplus
287 ptr2 = omp_get_interop_ptr (interop,
288 (omp_interop_property_t) ((int) omp_ipr_targetsync-1));
289 assert (ptr2 == NULL); /* Obvious implementation choice. */
290 #endif
291
292 ret_code2 = omp_irc_success;
293 str2 = omp_get_interop_str (interop,
294 (omp_interop_property_t) ((int) omp_ipr_targetsync-1), &ret_code2);
295 assert (ret_code2 == omp_irc_out_of_range);
296 assert (str2 == NULL); /* Obvious implementation choice. */
297 err2 = omp_get_interop_rc_desc (interop, ret_code2);
298 assert (err2 != NULL && strlen (err2) > 5); /* Some sensible message. */
299 /* GCC implementation choice. */
300 assert (!strcmp (err2, "property ID is out of range"));
301 str2 = omp_get_interop_str (interop,
302 (omp_interop_property_t) ((int) omp_ipr_targetsync-1), NULL);
303 assert (str2 == NULL); /* Obvious implementation choice. */
304 #ifdef __cplusplus
305 str2 = omp_get_interop_str (interop,
306 (omp_interop_property_t) ((int) omp_ipr_targetsync-1));
307 assert (str2 == NULL); /* Obvious implementation choice. */
308 #endif
309
310 /* omp_get_num_interop_properties (), i.e > upper bound. */
311
312 ret_code2 = omp_irc_success;
313 ival2 = omp_get_interop_int (interop,
314 (omp_interop_property_t) omp_get_num_interop_properties (interop),
315 &ret_code2);
316 assert (ret_code2 == omp_irc_out_of_range);
317 assert (ival2 == 0); /* Implementation choice. */
318 err2 = omp_get_interop_rc_desc (interop, ret_code2);
319 assert (err2 != NULL && strlen (err2) > 5); /* Some sensible message. */
320 /* GCC implementation choice. */
321 assert (!strcmp (err2, "property ID is out of range"));
322
323 ret_code2 = omp_irc_success;
324 ptr2 = omp_get_interop_ptr (interop,
325 (omp_interop_property_t) omp_get_num_interop_properties (interop),
326 &ret_code2);
327 assert (ret_code2 == omp_irc_out_of_range);
328 assert (ptr2 == NULL); /* Obvious implementation choice. */
329 err2 = omp_get_interop_rc_desc (interop, ret_code2);
330 assert (err2 != NULL && strlen (err2) > 5); /* Some sensible message. */
331 /* GCC implementation choice. */
332 assert (!strcmp (err2, "property ID is out of range"));
333
334 ret_code2 = omp_irc_success;
335 str2 = omp_get_interop_str (interop,
336 (omp_interop_property_t) omp_get_num_interop_properties (interop),
337 &ret_code2);
338 assert (ret_code2 == omp_irc_out_of_range);
339 assert (str2 == NULL); /* Obvious implementation choice. */
340 err2 = omp_get_interop_rc_desc (interop, ret_code2);
341 assert (err2 != NULL && strlen (err2) > 5); /* Some sensible message. */
342 /* GCC implementation choice. */
343 assert (!strcmp (err2, "property ID is out of range"));
344 }
345 #pragma omp end declare target
346
347 int
348 main ()
349 {
350 for (int dev = 0; dev < omp_get_num_devices (); dev++)
351 #pragma omp target device(device_num : dev)
352 target_test ();
353 return 0;
354 }