Improve atomic store implementation on hppa-linux.
[official-gcc.git] / libgomp / testsuite / libgomp.oacc-c-c++-common / pr92848-1-r-p.c
blob95565ba1cb2491d8a3c57d21b99f74f4b5ae0112
1 /* Verify device memory allocation/deallocation
2 { dg-additional-options "-DOPENACC_RUNTIME" } using OpenACC Runtime Library routines,
3 { dg-additional-options "-DPOINTERS" } using pointers. */
5 /* { dg-skip-if "" { *-*-* } { "-DACC_MEM_SHARED=1" } } */
7 #if OPENACC_RUNTIME
8 #elif OPENACC_DIRECTIVES
9 #else
10 # error
11 #endif
13 #if POINTERS
14 #elif ARRAYS
15 #else
16 # error
17 #endif
20 #include <openacc.h>
21 #include <acc_prof.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <stdint.h>
26 #include <stdbool.h>
29 static bool cb_ev_alloc_expected;
30 static size_t cb_ev_alloc_bytes;
31 static const void *cb_ev_alloc_device_ptr;
32 static void
33 cb_ev_alloc (acc_prof_info *prof_info, acc_event_info *event_info, acc_api_info *api_info)
35 assert (cb_ev_alloc_expected);
36 cb_ev_alloc_expected = false;
38 cb_ev_alloc_bytes = event_info->data_event.bytes;
39 cb_ev_alloc_device_ptr = event_info->data_event.device_ptr;
42 static bool cb_ev_free_expected;
43 static const void *cb_ev_free_device_ptr;
44 static void
45 cb_ev_free (acc_prof_info *prof_info, acc_event_info *event_info, acc_api_info *api_info)
47 assert (cb_ev_free_expected);
48 cb_ev_free_expected = false;
50 cb_ev_free_device_ptr = event_info->data_event.device_ptr;
54 /* Match the alignment processing that
55 'libgomp/target.c:gomp_map_vars_internal' is doing; simplified, not
56 considering special alignment requirements of certain data types. */
58 static size_t
59 aligned_size (size_t tgt_size)
61 size_t tgt_align = sizeof (void *);
62 return tgt_size + tgt_align - 1;
65 static const void *
66 aligned_address (const void *tgt_start)
68 size_t tgt_align = sizeof (void *);
69 return (void *) (((uintptr_t) tgt_start + tgt_align - 1) & ~(tgt_align - 1));
73 #define SIZE 1024
74 #define SUBSET 32
77 /* A "create", [...], "delete" sequence. */
79 static void
80 f1 (void)
82 cb_ev_alloc_expected = false;
83 cb_ev_free_expected = false;
84 acc_prof_register (acc_ev_alloc, cb_ev_alloc, acc_reg);
85 acc_prof_register (acc_ev_free, cb_ev_free, acc_reg);
87 #if POINTERS
88 char *h = (char *) malloc (SIZE);
89 #else
90 char h[SIZE];
91 #endif
93 void *d;
94 cb_ev_alloc_expected = true;
95 #if OPENACC_RUNTIME
96 d = acc_create (h, SIZE);
97 #else
98 # if POINTERS
99 # pragma acc enter data create (h[0:SIZE])
100 # else
101 # pragma acc enter data create (h)
102 # endif
103 d = acc_deviceptr (h);
104 #endif
105 assert (d);
106 assert (!cb_ev_alloc_expected);
107 assert (cb_ev_alloc_bytes == aligned_size (SIZE));
108 assert (aligned_address (cb_ev_alloc_device_ptr) == d);
109 assert (acc_is_present (h, SIZE));
111 #if OPENACC_RUNTIME
112 acc_create (h, SIZE);
113 #else
114 # if POINTERS
115 # pragma acc enter data create (h[0:SIZE])
116 # else
117 # pragma acc enter data create (h)
118 # endif
119 #endif
121 #if POINTERS
122 # pragma acc data create (h[0:SIZE])
124 #else
125 # pragma acc data create (h)
127 #endif
128 assert (acc_is_present (h, SIZE));
130 #if OPENACC_RUNTIME
131 acc_delete (h, SIZE);
132 #else
133 # if POINTERS
134 # pragma acc exit data delete (h[0:SIZE])
135 # else
136 # pragma acc exit data delete (h)
137 # endif
138 #endif
139 assert (acc_is_present (h, SIZE));
141 cb_ev_free_expected = true;
142 #if OPENACC_RUNTIME
143 acc_delete (h, SIZE);
144 #else
145 # if POINTERS
146 # pragma acc exit data delete (h[0:SIZE])
147 # else
148 # pragma acc exit data delete (h)
149 # endif
150 #endif
151 assert (!cb_ev_free_expected);
152 assert (cb_ev_free_device_ptr == cb_ev_alloc_device_ptr);
153 assert (!acc_is_present (h, SIZE));
155 #if POINTERS
156 free (h);
157 #endif
159 acc_prof_unregister (acc_ev_alloc, cb_ev_alloc, acc_reg);
160 acc_prof_unregister (acc_ev_free, cb_ev_free, acc_reg);
164 /* A "map", [...] "unmap" sequence. */
166 static void
167 f2 (void)
169 cb_ev_alloc_expected = false;
170 cb_ev_free_expected = false;
171 acc_prof_register (acc_ev_alloc, cb_ev_alloc, acc_reg);
172 acc_prof_register (acc_ev_free, cb_ev_free, acc_reg);
174 #if POINTERS
175 char *h = (char *) malloc (SIZE);
176 #else
177 char h[SIZE];
178 #endif
180 void *d;
181 cb_ev_alloc_expected = true;
182 d = acc_malloc (SIZE);
183 assert (d);
184 assert (!cb_ev_alloc_expected);
185 assert (cb_ev_alloc_bytes == SIZE);
186 assert (cb_ev_alloc_device_ptr == d);
188 acc_map_data (h, d, SIZE);
189 assert (acc_is_present (h, SIZE));
191 #if OPENACC_RUNTIME
192 acc_create (h, SIZE);
193 #else
194 # if POINTERS
195 # pragma acc enter data create (h[0:SIZE])
196 # else
197 # pragma acc enter data create (h)
198 # endif
199 #endif
201 #if POINTERS
202 # pragma acc data create (h[0:SIZE])
204 #else
205 # pragma acc data create (h)
207 #endif
208 assert (acc_is_present (h, SIZE));
210 #if OPENACC_RUNTIME
211 acc_delete (h, SIZE);
212 #else
213 # if POINTERS
214 # pragma acc exit data delete (h[0:SIZE])
215 # else
216 # pragma acc exit data delete (h)
217 # endif
218 #endif
219 assert (acc_is_present (h, SIZE));
221 acc_unmap_data (h);
222 assert (!acc_is_present (h, SIZE));
224 cb_ev_free_expected = true;
225 acc_free (d);
226 assert (!cb_ev_free_expected);
227 assert (cb_ev_free_device_ptr == cb_ev_alloc_device_ptr);
229 #if POINTERS
230 free (h);
231 #endif
233 acc_prof_unregister (acc_ev_alloc, cb_ev_alloc, acc_reg);
234 acc_prof_unregister (acc_ev_free, cb_ev_free, acc_reg);
238 /* A structured 'data' construct. */
240 static void
241 f3 (void)
243 cb_ev_alloc_expected = false;
244 cb_ev_free_expected = false;
245 acc_prof_register (acc_ev_alloc, cb_ev_alloc, acc_reg);
246 acc_prof_register (acc_ev_free, cb_ev_free, acc_reg);
248 #if POINTERS
249 char *h = (char *) malloc (SIZE);
250 #else
251 char h[SIZE];
252 #endif
254 cb_ev_alloc_expected = true;
255 #if POINTERS
256 # pragma acc data create (h[0:SIZE])
257 #else
258 # pragma acc data create (h)
259 #endif
261 void *d = acc_deviceptr (h);
262 assert (d);
263 assert (!cb_ev_alloc_expected);
264 assert (cb_ev_alloc_bytes == aligned_size (SIZE));
265 assert (aligned_address (cb_ev_alloc_device_ptr) == d);
266 assert (acc_is_present (h, SIZE));
268 #if OPENACC_RUNTIME
269 acc_create (h, SIZE);
270 #else
271 # if POINTERS
272 # pragma acc enter data create (h[0:SIZE])
273 # else
274 # pragma acc enter data create (h)
275 # endif
276 #endif
278 #if POINTERS
279 # pragma acc data create (h[0:SIZE])
281 #else
282 # pragma acc data create (h)
284 #endif
285 assert (acc_is_present (h, SIZE));
287 #if OPENACC_RUNTIME
288 acc_delete (h, SIZE);
289 #else
290 # if POINTERS
291 # pragma acc exit data delete (h[0:SIZE])
292 # else
293 # pragma acc exit data delete (h)
294 # endif
295 #endif
296 assert (acc_is_present (h, SIZE));
298 cb_ev_free_expected = true;
300 assert (!cb_ev_free_expected);
301 assert (cb_ev_free_device_ptr == cb_ev_alloc_device_ptr);
302 assert (!acc_is_present (h, SIZE));
304 #if POINTERS
305 free (h);
306 #endif
308 acc_prof_unregister (acc_ev_alloc, cb_ev_alloc, acc_reg);
309 acc_prof_unregister (acc_ev_free, cb_ev_free, acc_reg);
314 main ()
316 f1 ();
317 f2 ();
318 f3 ();
320 return 0;