| 1 |
/* Verify device memory allocation/deallocation |
| 2 |
{ dg-additional-options "-DOPENACC_RUNTIME" } using OpenACC Runtime Library routines, |
| 3 |
{ dg-additional-options "-DPOINTERS" } using pointers. */ |
| 4 |
|
| 5 |
/* { dg-skip-if "" { *-*-* } { "-DACC_MEM_SHARED=1" } } */ |
| 6 |
|
| 7 |
#if OPENACC_RUNTIME |
| 8 |
#elif OPENACC_DIRECTIVES |
| 9 |
#else |
| 10 |
# error |
| 11 |
#endif |
| 12 |
|
| 13 |
#if POINTERS |
| 14 |
#elif ARRAYS |
| 15 |
#else |
| 16 |
# error |
| 17 |
#endif |
| 18 |
|
| 19 |
|
| 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> |
| 27 |
|
| 28 |
|
| 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) |
| 34 |
{ |
| 35 |
assert (cb_ev_alloc_expected); |
| 36 |
cb_ev_alloc_expected = false; |
| 37 |
|
| 38 |
cb_ev_alloc_bytes = event_info->data_event.bytes; |
| 39 |
cb_ev_alloc_device_ptr = event_info->data_event.device_ptr; |
| 40 |
} |
| 41 |
|
| 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) |
| 46 |
{ |
| 47 |
assert (cb_ev_free_expected); |
| 48 |
cb_ev_free_expected = false; |
| 49 |
|
| 50 |
cb_ev_free_device_ptr = event_info->data_event.device_ptr; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 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. */ |
| 57 |
|
| 58 |
static size_t |
| 59 |
aligned_size (size_t tgt_size) |
| 60 |
{ |
| 61 |
size_t tgt_align = sizeof (void *); |
| 62 |
return tgt_size + tgt_align - 1; |
| 63 |
} |
| 64 |
|
| 65 |
static const void * |
| 66 |
aligned_address (const void *tgt_start) |
| 67 |
{ |
| 68 |
size_t tgt_align = sizeof (void *); |
| 69 |
return (void *) (((uintptr_t) tgt_start + tgt_align - 1) & ~(tgt_align - 1)); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
#define SIZE 1024 |
| 74 |
#define SUBSET 32 |
| 75 |
|
| 76 |
|
| 77 |
/* A "create", [...], "delete" sequence. */ |
| 78 |
|
| 79 |
static void |
| 80 |
f1 (void) |
| 81 |
{ |
| 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); |
| 86 |
|
| 87 |
#if POINTERS |
| 88 |
char *h = (char *) malloc (SIZE); |
| 89 |
#else |
| 90 |
char h[SIZE]; |
| 91 |
#endif |
| 92 |
|
| 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)); |
| 110 |
|
| 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 |
| 120 |
|
| 121 |
#if POINTERS |
| 122 |
# pragma acc data create (h[0:SIZE]) |
| 123 |
; |
| 124 |
#else |
| 125 |
# pragma acc data create (h) |
| 126 |
; |
| 127 |
#endif |
| 128 |
assert (acc_is_present (h, SIZE)); |
| 129 |
|
| 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)); |
| 140 |
|
| 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)); |
| 154 |
|
| 155 |
#if POINTERS |
| 156 |
free (h); |
| 157 |
#endif |
| 158 |
|
| 159 |
acc_prof_unregister (acc_ev_alloc, cb_ev_alloc, acc_reg); |
| 160 |
acc_prof_unregister (acc_ev_free, cb_ev_free, acc_reg); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/* A "map", [...] "unmap" sequence. */ |
| 165 |
|
| 166 |
static void |
| 167 |
f2 (void) |
| 168 |
{ |
| 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); |
| 173 |
|
| 174 |
#if POINTERS |
| 175 |
char *h = (char *) malloc (SIZE); |
| 176 |
#else |
| 177 |
char h[SIZE]; |
| 178 |
#endif |
| 179 |
|
| 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); |
| 187 |
|
| 188 |
acc_map_data (h, d, SIZE); |
| 189 |
assert (acc_is_present (h, SIZE)); |
| 190 |
|
| 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 |
| 200 |
|
| 201 |
#if POINTERS |
| 202 |
# pragma acc data create (h[0:SIZE]) |
| 203 |
; |
| 204 |
#else |
| 205 |
# pragma acc data create (h) |
| 206 |
; |
| 207 |
#endif |
| 208 |
assert (acc_is_present (h, SIZE)); |
| 209 |
|
| 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)); |
| 220 |
|
| 221 |
acc_unmap_data (h); |
| 222 |
assert (!acc_is_present (h, SIZE)); |
| 223 |
|
| 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); |
| 228 |
|
| 229 |
#if POINTERS |
| 230 |
free (h); |
| 231 |
#endif |
| 232 |
|
| 233 |
acc_prof_unregister (acc_ev_alloc, cb_ev_alloc, acc_reg); |
| 234 |
acc_prof_unregister (acc_ev_free, cb_ev_free, acc_reg); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/* A structured 'data' construct. */ |
| 239 |
|
| 240 |
static void |
| 241 |
f3 (void) |
| 242 |
{ |
| 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); |
| 247 |
|
| 248 |
#if POINTERS |
| 249 |
char *h = (char *) malloc (SIZE); |
| 250 |
#else |
| 251 |
char h[SIZE]; |
| 252 |
#endif |
| 253 |
|
| 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 |
| 260 |
{ |
| 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)); |
| 267 |
|
| 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 |
| 277 |
|
| 278 |
#if POINTERS |
| 279 |
# pragma acc data create (h[0:SIZE]) |
| 280 |
; |
| 281 |
#else |
| 282 |
# pragma acc data create (h) |
| 283 |
; |
| 284 |
#endif |
| 285 |
assert (acc_is_present (h, SIZE)); |
| 286 |
|
| 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)); |
| 297 |
|
| 298 |
cb_ev_free_expected = true; |
| 299 |
} |
| 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)); |
| 303 |
|
| 304 |
#if POINTERS |
| 305 |
free (h); |
| 306 |
#endif |
| 307 |
|
| 308 |
acc_prof_unregister (acc_ev_alloc, cb_ev_alloc, acc_reg); |
| 309 |
acc_prof_unregister (acc_ev_free, cb_ev_free, acc_reg); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
int |
| 314 |
main () |
| 315 |
{ |
| 316 |
f1 (); |
| 317 |
f2 (); |
| 318 |
f3 (); |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |