blob: 83625ba8a8e33c083d41b84687fb37cce3a1190c [file] [log] [blame]
Thomas Koenigb18a97e2021-09-13 19:49:49 +02001/* Copyright (C) 2013-2021 Free Software Foundation, Inc.
Thomas Schwinge41dbbb32015-01-15 21:11:12 +01002
3 Contributed by Mentor Embedded.
4
5 This file is part of the GNU Offloading and Multi Processing Library
6 (libgomp).
7
8 Libgomp is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27/* This file handles OpenACC constructs. */
28
29#include "openacc.h"
30#include "libgomp.h"
Thomas Schwinge41dbbb32015-01-15 21:11:12 +010031#include "gomp-constants.h"
32#include "oacc-int.h"
Kai Tietz01c0b3b2015-03-25 16:05:02 +010033#ifdef HAVE_INTTYPES_H
34# include <inttypes.h> /* For PRIu64. */
35#endif
Thomas Schwinge41dbbb32015-01-15 21:11:12 +010036#include <string.h>
37#include <stdarg.h>
38#include <assert.h>
Thomas Schwinge41dbbb32015-01-15 21:11:12 +010039
Thomas Schwinge59d59602018-12-28 12:34:14 +010040
41/* In the ABI, the GOACC_FLAGs are encoded as an inverted bitmask, so that we
42 continue to support the following two legacy values. */
43_Static_assert (GOACC_FLAGS_UNMARSHAL (GOMP_DEVICE_ICV) == 0,
44 "legacy GOMP_DEVICE_ICV broken");
45_Static_assert (GOACC_FLAGS_UNMARSHAL (GOMP_DEVICE_HOST_FALLBACK)
46 == GOACC_FLAG_HOST_FALLBACK,
47 "legacy GOMP_DEVICE_HOST_FALLBACK broken");
48
49
Chung-Lin Tang829c6342018-06-20 16:35:15 +000050/* Handle the mapping pair that are presented when a
51 deviceptr clause is used with Fortran. */
52
53static void
54handle_ftn_pointers (size_t mapnum, void **hostaddrs, size_t *sizes,
55 unsigned short *kinds)
56{
57 int i;
58
59 for (i = 0; i < mapnum; i++)
60 {
61 unsigned short kind1 = kinds[i] & 0xff;
62
63 /* Handle Fortran deviceptr clause. */
64 if (kind1 == GOMP_MAP_FORCE_DEVICEPTR)
65 {
66 unsigned short kind2;
67
68 if (i < (signed)mapnum - 1)
69 kind2 = kinds[i + 1] & 0xff;
70 else
71 kind2 = 0xffff;
72
73 if (sizes[i] == sizeof (void *))
74 continue;
75
76 /* At this point, we're dealing with a Fortran deviceptr.
77 If the next element is not what we're expecting, then
78 this is an instance of where the deviceptr variable was
79 not used within the region and the pointer was removed
80 by the gimplifier. */
81 if (kind2 == GOMP_MAP_POINTER
82 && sizes[i + 1] == 0
83 && hostaddrs[i] == *(void **)hostaddrs[i + 1])
84 {
85 kinds[i+1] = kinds[i];
86 sizes[i+1] = sizeof (void *);
87 }
88
89 /* Invalidate the entry. */
90 hostaddrs[i] = NULL;
91 }
92 }
Thomas Schwinge41dbbb32015-01-15 21:11:12 +010093}
94
Nathan Sidwell3e32ee12015-09-28 19:37:33 +000095
Thomas Schwinge59d59602018-12-28 12:34:14 +010096/* Launch a possibly offloaded function with FLAGS. FN is the host fn
Nathan Sidwell3e32ee12015-09-28 19:37:33 +000097 address. MAPNUM, HOSTADDRS, SIZES & KINDS describe the memory
98 blocks to be copied to/from the device. Varadic arguments are
99 keyed optional parameters terminated with a zero. */
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100100
101void
Thomas Schwinge59d59602018-12-28 12:34:14 +0100102GOACC_parallel_keyed (int flags_m, void (*fn) (void *),
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000103 size_t mapnum, void **hostaddrs, size_t *sizes,
104 unsigned short *kinds, ...)
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100105{
Thomas Schwinge59d59602018-12-28 12:34:14 +0100106 int flags = GOACC_FLAGS_UNMARSHAL (flags_m);
107
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100108 va_list ap;
109 struct goacc_thread *thr;
110 struct gomp_device_descr *acc_dev;
111 struct target_mem_desc *tgt;
112 void **devaddrs;
113 unsigned int i;
114 struct splay_tree_key_s k;
115 splay_tree_key tgt_fn_key;
116 void (*tgt_fn);
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000117 int async = GOMP_ASYNC_SYNC;
118 unsigned dims[GOMP_DIM_MAX];
119 unsigned tag;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100120
Kai Tietz01c0b3b2015-03-25 16:05:02 +0100121#ifdef HAVE_INTTYPES_H
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000122 gomp_debug (0, "%s: mapnum=%"PRIu64", hostaddrs=%p, size=%p, kinds=%p\n",
123 __FUNCTION__, (uint64_t) mapnum, hostaddrs, sizes, kinds);
Kai Tietz01c0b3b2015-03-25 16:05:02 +0100124#else
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000125 gomp_debug (0, "%s: mapnum=%lu, hostaddrs=%p, sizes=%p, kinds=%p\n",
126 __FUNCTION__, (unsigned long) mapnum, hostaddrs, sizes, kinds);
Kai Tietz01c0b3b2015-03-25 16:05:02 +0100127#endif
Julian Brownd93bdab2015-04-08 15:58:33 +0000128 goacc_lazy_initialize ();
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100129
130 thr = goacc_thread ();
131 acc_dev = thr->dev;
132
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200133 bool profiling_p = GOACC_PROFILING_DISPATCH_P (true);
134
135 acc_prof_info prof_info;
136 if (profiling_p)
137 {
138 thr->prof_info = &prof_info;
139
140 prof_info.event_type = acc_ev_compute_construct_start;
141 prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
142 prof_info.version = _ACC_PROF_INFO_VERSION;
143 prof_info.device_type = acc_device_type (acc_dev->type);
144 prof_info.device_number = acc_dev->target_id;
145 prof_info.thread_id = -1;
146 prof_info.async = async;
147 prof_info.async_queue = prof_info.async;
148 prof_info.src_file = NULL;
149 prof_info.func_name = NULL;
150 prof_info.line_no = -1;
151 prof_info.end_line_no = -1;
152 prof_info.func_line_no = -1;
153 prof_info.func_end_line_no = -1;
154 }
155 acc_event_info compute_construct_event_info;
156 if (profiling_p)
157 {
158 compute_construct_event_info.other_event.event_type
159 = prof_info.event_type;
160 compute_construct_event_info.other_event.valid_bytes
161 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
162 compute_construct_event_info.other_event.parent_construct
163 = acc_construct_parallel;
164 compute_construct_event_info.other_event.implicit = 0;
165 compute_construct_event_info.other_event.tool_info = NULL;
166 }
167 acc_api_info api_info;
168 if (profiling_p)
169 {
170 thr->api_info = &api_info;
171
172 api_info.device_api = acc_device_api_none;
173 api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
174 api_info.device_type = prof_info.device_type;
175 api_info.vendor = -1;
176 api_info.device_handle = NULL;
177 api_info.context_handle = NULL;
178 api_info.async_handle = NULL;
179 }
180
181 if (profiling_p)
182 goacc_profiling_dispatch (&prof_info, &compute_construct_event_info,
183 &api_info);
184
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000185 handle_ftn_pointers (mapnum, hostaddrs, sizes, kinds);
186
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100187 /* Host fallback if "if" clause is false or if the current device is set to
188 the host. */
Thomas Schwinge59d59602018-12-28 12:34:14 +0100189 if (flags & GOACC_FLAG_HOST_FALLBACK)
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100190 {
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200191 prof_info.device_type = acc_device_host;
192 api_info.device_type = prof_info.device_type;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100193 goacc_save_and_set_bind (acc_device_host);
194 fn (hostaddrs);
195 goacc_restore_bind ();
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200196 goto out_prof;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100197 }
198 else if (acc_device_type (acc_dev->type) == acc_device_host)
199 {
200 fn (hostaddrs);
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200201 goto out_prof;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100202 }
203
Thomas Schwingef99c3552016-02-23 16:07:54 +0100204 /* Default: let the runtime choose. */
205 for (i = 0; i != GOMP_DIM_MAX; i++)
206 dims[i] = 0;
207
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000208 va_start (ap, kinds);
209 /* TODO: This will need amending when device_type is implemented. */
210 while ((tag = va_arg (ap, unsigned)) != 0)
Nathan Sidwella0911182015-07-20 17:31:46 +0000211 {
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000212 if (GOMP_LAUNCH_DEVICE (tag))
213 gomp_fatal ("device_type '%d' offload parameters, libgomp is too old",
214 GOMP_LAUNCH_DEVICE (tag));
215
216 switch (GOMP_LAUNCH_CODE (tag))
217 {
218 case GOMP_LAUNCH_DIM:
219 {
220 unsigned mask = GOMP_LAUNCH_OP (tag);
221
222 for (i = 0; i != GOMP_DIM_MAX; i++)
223 if (mask & GOMP_DIM_MASK (i))
224 dims[i] = va_arg (ap, unsigned);
225 }
226 break;
227
228 case GOMP_LAUNCH_ASYNC:
229 {
230 /* Small constant values are encoded in the operand. */
231 async = GOMP_LAUNCH_OP (tag);
232
233 if (async == GOMP_LAUNCH_OP_MAX)
234 async = va_arg (ap, unsigned);
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200235
236 if (profiling_p)
237 {
238 prof_info.async = async;
239 prof_info.async_queue = prof_info.async;
240 }
241
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000242 break;
243 }
244
245 case GOMP_LAUNCH_WAIT:
246 {
247 unsigned num_waits = GOMP_LAUNCH_OP (tag);
Chung-Lin Tang19695f42019-02-19 14:10:15 +0000248 goacc_wait (async, num_waits, &ap);
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000249 break;
250 }
251
252 default:
253 gomp_fatal ("unrecognized offload code '%d',"
254 " libgomp is too old", GOMP_LAUNCH_CODE (tag));
255 }
Nathan Sidwella0911182015-07-20 17:31:46 +0000256 }
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000257 va_end (ap);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100258
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100259 if (!(acc_dev->capabilities & GOMP_OFFLOAD_CAP_NATIVE_EXEC))
260 {
261 k.host_start = (uintptr_t) fn;
262 k.host_end = k.host_start + 1;
Ilya Verbina51df542015-04-06 12:40:28 +0000263 gomp_mutex_lock (&acc_dev->lock);
264 tgt_fn_key = splay_tree_lookup (&acc_dev->mem_map, &k);
265 gomp_mutex_unlock (&acc_dev->lock);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100266
267 if (tgt_fn_key == NULL)
268 gomp_fatal ("target function wasn't mapped");
269
Julian Brownd93bdab2015-04-08 15:58:33 +0000270 tgt_fn = (void (*)) tgt_fn_key->tgt_offset;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100271 }
272 else
273 tgt_fn = (void (*)) fn;
274
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200275 acc_event_info enter_exit_data_event_info;
276 if (profiling_p)
277 {
278 prof_info.event_type = acc_ev_enter_data_start;
279 enter_exit_data_event_info.other_event.event_type
280 = prof_info.event_type;
281 enter_exit_data_event_info.other_event.valid_bytes
282 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
283 enter_exit_data_event_info.other_event.parent_construct
284 = compute_construct_event_info.other_event.parent_construct;
285 enter_exit_data_event_info.other_event.implicit = 1;
286 enter_exit_data_event_info.other_event.tool_info = NULL;
287 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
288 &api_info);
289 }
290
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000291 goacc_aq aq = get_goacc_asyncqueue (async);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100292
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200293 tgt = goacc_map_vars (acc_dev, aq, mapnum, hostaddrs, NULL, sizes, kinds,
294 true, 0);
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200295 if (profiling_p)
296 {
297 prof_info.event_type = acc_ev_enter_data_end;
298 enter_exit_data_event_info.other_event.event_type
299 = prof_info.event_type;
300 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
301 &api_info);
302 }
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200303
Thomas Schwinge6e361142015-01-16 21:05:21 +0100304 devaddrs = gomp_alloca (sizeof (void *) * mapnum);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100305 for (i = 0; i < mapnum; i++)
Julian Brown5bcd4702019-12-20 01:20:19 +0000306 devaddrs[i] = (void *) gomp_map_val (tgt, hostaddrs, i);
307
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000308 if (aq == NULL)
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200309 acc_dev->openacc.exec_func (tgt_fn, mapnum, hostaddrs, devaddrs, dims,
310 tgt);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100311 else
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200312 acc_dev->openacc.async.exec_func (tgt_fn, mapnum, hostaddrs, devaddrs,
313 dims, tgt, aq);
314
315 if (profiling_p)
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000316 {
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200317 prof_info.event_type = acc_ev_exit_data_start;
318 enter_exit_data_event_info.other_event.event_type = prof_info.event_type;
319 enter_exit_data_event_info.other_event.tool_info = NULL;
320 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
321 &api_info);
322 }
323
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200324 /* If running synchronously (aq == NULL), this will unmap immediately. */
325 goacc_unmap_vars (tgt, true, aq);
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200326
327 if (profiling_p)
328 {
329 prof_info.event_type = acc_ev_exit_data_end;
330 enter_exit_data_event_info.other_event.event_type = prof_info.event_type;
331 goacc_profiling_dispatch (&prof_info, &enter_exit_data_event_info,
332 &api_info);
333 }
334
335 out_prof:
336 if (profiling_p)
337 {
338 prof_info.event_type = acc_ev_compute_construct_end;
339 compute_construct_event_info.other_event.event_type
340 = prof_info.event_type;
341 goacc_profiling_dispatch (&prof_info, &compute_construct_event_info,
342 &api_info);
343
344 thr->prof_info = NULL;
345 thr->api_info = NULL;
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000346 }
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100347}
348
Thomas Schwinge2bbbfa42019-05-06 10:49:55 +0200349/* Legacy entry point (GCC 5). Only provide host fallback execution. */
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000350
351void
Thomas Schwinge59d59602018-12-28 12:34:14 +0100352GOACC_parallel (int flags_m, void (*fn) (void *),
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000353 size_t mapnum, void **hostaddrs, size_t *sizes,
354 unsigned short *kinds,
355 int num_gangs, int num_workers, int vector_length,
356 int async, int num_waits, ...)
357{
358 goacc_save_and_set_bind (acc_device_host);
359 fn (hostaddrs);
360 goacc_restore_bind ();
361}
362
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100363void
Thomas Schwinge59d59602018-12-28 12:34:14 +0100364GOACC_data_start (int flags_m, size_t mapnum,
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100365 void **hostaddrs, size_t *sizes, unsigned short *kinds)
366{
Thomas Schwinge59d59602018-12-28 12:34:14 +0100367 int flags = GOACC_FLAGS_UNMARSHAL (flags_m);
368
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100369 struct target_mem_desc *tgt;
370
Kai Tietz01c0b3b2015-03-25 16:05:02 +0100371#ifdef HAVE_INTTYPES_H
372 gomp_debug (0, "%s: mapnum=%"PRIu64", hostaddrs=%p, size=%p, kinds=%p\n",
373 __FUNCTION__, (uint64_t) mapnum, hostaddrs, sizes, kinds);
374#else
375 gomp_debug (0, "%s: mapnum=%lu, hostaddrs=%p, sizes=%p, kinds=%p\n",
376 __FUNCTION__, (unsigned long) mapnum, hostaddrs, sizes, kinds);
377#endif
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100378
Julian Brownd93bdab2015-04-08 15:58:33 +0000379 goacc_lazy_initialize ();
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100380
381 struct goacc_thread *thr = goacc_thread ();
382 struct gomp_device_descr *acc_dev = thr->dev;
383
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200384 bool profiling_p = GOACC_PROFILING_DISPATCH_P (true);
385
386 acc_prof_info prof_info;
387 if (profiling_p)
388 {
389 thr->prof_info = &prof_info;
390
391 prof_info.event_type = acc_ev_enter_data_start;
392 prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
393 prof_info.version = _ACC_PROF_INFO_VERSION;
394 prof_info.device_type = acc_device_type (acc_dev->type);
395 prof_info.device_number = acc_dev->target_id;
396 prof_info.thread_id = -1;
397 prof_info.async = acc_async_sync; /* Always synchronous. */
398 prof_info.async_queue = prof_info.async;
399 prof_info.src_file = NULL;
400 prof_info.func_name = NULL;
401 prof_info.line_no = -1;
402 prof_info.end_line_no = -1;
403 prof_info.func_line_no = -1;
404 prof_info.func_end_line_no = -1;
405 }
406 acc_event_info enter_data_event_info;
407 if (profiling_p)
408 {
409 enter_data_event_info.other_event.event_type
410 = prof_info.event_type;
411 enter_data_event_info.other_event.valid_bytes
412 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
413 enter_data_event_info.other_event.parent_construct = acc_construct_data;
414 for (int i = 0; i < mapnum; ++i)
Tobias Burnusd5c23c62020-01-10 16:08:41 +0100415 if ((kinds[i] & 0xff) == GOMP_MAP_USE_DEVICE_PTR
416 || (kinds[i] & 0xff) == GOMP_MAP_USE_DEVICE_PTR_IF_PRESENT)
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200417 {
418 /* If there is one such data mapping kind, then this is actually an
419 OpenACC 'host_data' construct. (GCC maps the OpenACC
420 'host_data' construct to the OpenACC 'data' construct.) Apart
421 from artificial test cases (such as an OpenACC 'host_data'
422 construct's (implicit) device initialization when there hasn't
423 been any device data be set up before...), there can't really
424 any meaningful events be generated from OpenACC 'host_data'
425 constructs, though. */
426 enter_data_event_info.other_event.parent_construct
427 = acc_construct_host_data;
428 break;
429 }
430 enter_data_event_info.other_event.implicit = 0;
431 enter_data_event_info.other_event.tool_info = NULL;
432 }
433 acc_api_info api_info;
434 if (profiling_p)
435 {
436 thr->api_info = &api_info;
437
438 api_info.device_api = acc_device_api_none;
439 api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
440 api_info.device_type = prof_info.device_type;
441 api_info.vendor = -1;
442 api_info.device_handle = NULL;
443 api_info.context_handle = NULL;
444 api_info.async_handle = NULL;
445 }
446
447 if (profiling_p)
448 goacc_profiling_dispatch (&prof_info, &enter_data_event_info, &api_info);
449
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100450 /* Host fallback or 'do nothing'. */
451 if ((acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
Thomas Schwinge59d59602018-12-28 12:34:14 +0100452 || (flags & GOACC_FLAG_HOST_FALLBACK))
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100453 {
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200454 prof_info.device_type = acc_device_host;
455 api_info.device_type = prof_info.device_type;
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200456 tgt = goacc_map_vars (NULL, NULL, 0, NULL, NULL, NULL, NULL, true, 0);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100457 tgt->prev = thr->mapped_data;
458 thr->mapped_data = tgt;
459
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200460 goto out_prof;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100461 }
462
463 gomp_debug (0, " %s: prepare mappings\n", __FUNCTION__);
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200464 tgt = goacc_map_vars (acc_dev, NULL, mapnum, hostaddrs, NULL, sizes, kinds,
465 true, 0);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100466 gomp_debug (0, " %s: mappings prepared\n", __FUNCTION__);
467 tgt->prev = thr->mapped_data;
468 thr->mapped_data = tgt;
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200469
470 out_prof:
471 if (profiling_p)
472 {
473 prof_info.event_type = acc_ev_enter_data_end;
474 enter_data_event_info.other_event.event_type = prof_info.event_type;
475 goacc_profiling_dispatch (&prof_info, &enter_data_event_info, &api_info);
476
477 thr->prof_info = NULL;
478 thr->api_info = NULL;
479 }
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100480}
481
482void
483GOACC_data_end (void)
484{
485 struct goacc_thread *thr = goacc_thread ();
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200486 struct gomp_device_descr *acc_dev = thr->dev;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100487 struct target_mem_desc *tgt = thr->mapped_data;
488
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200489 bool profiling_p = GOACC_PROFILING_DISPATCH_P (true);
490
491 acc_prof_info prof_info;
492 if (profiling_p)
493 {
494 thr->prof_info = &prof_info;
495
496 prof_info.event_type = acc_ev_exit_data_start;
497 prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
498 prof_info.version = _ACC_PROF_INFO_VERSION;
499 prof_info.device_type = acc_device_type (acc_dev->type);
500 prof_info.device_number = acc_dev->target_id;
501 prof_info.thread_id = -1;
502 prof_info.async = acc_async_sync; /* Always synchronous. */
503 prof_info.async_queue = prof_info.async;
504 prof_info.src_file = NULL;
505 prof_info.func_name = NULL;
506 prof_info.line_no = -1;
507 prof_info.end_line_no = -1;
508 prof_info.func_line_no = -1;
509 prof_info.func_end_line_no = -1;
510 }
511 acc_event_info exit_data_event_info;
512 if (profiling_p)
513 {
514 exit_data_event_info.other_event.event_type
515 = prof_info.event_type;
516 exit_data_event_info.other_event.valid_bytes
517 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
518 exit_data_event_info.other_event.parent_construct = acc_construct_data;
519 exit_data_event_info.other_event.implicit = 0;
520 exit_data_event_info.other_event.tool_info = NULL;
521 }
522 acc_api_info api_info;
523 if (profiling_p)
524 {
525 thr->api_info = &api_info;
526
527 api_info.device_api = acc_device_api_none;
528 api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
529 api_info.device_type = prof_info.device_type;
530 api_info.vendor = -1;
531 api_info.device_handle = NULL;
532 api_info.context_handle = NULL;
533 api_info.async_handle = NULL;
534 }
535
536 if (profiling_p)
537 goacc_profiling_dispatch (&prof_info, &exit_data_event_info, &api_info);
538
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100539 gomp_debug (0, " %s: restore mappings\n", __FUNCTION__);
540 thr->mapped_data = tgt->prev;
Thomas Koenigb18a97e2021-09-13 19:49:49 +0200541 goacc_unmap_vars (tgt, true, NULL);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100542 gomp_debug (0, " %s: mappings restored\n", __FUNCTION__);
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200543
544 if (profiling_p)
545 {
546 prof_info.event_type = acc_ev_exit_data_end;
547 exit_data_event_info.other_event.event_type = prof_info.event_type;
548 goacc_profiling_dispatch (&prof_info, &exit_data_event_info, &api_info);
549
550 thr->prof_info = NULL;
551 thr->api_info = NULL;
552 }
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100553}
554
555void
Thomas Schwinge59d59602018-12-28 12:34:14 +0100556GOACC_update (int flags_m, size_t mapnum,
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100557 void **hostaddrs, size_t *sizes, unsigned short *kinds,
558 int async, int num_waits, ...)
559{
Thomas Schwinge59d59602018-12-28 12:34:14 +0100560 int flags = GOACC_FLAGS_UNMARSHAL (flags_m);
561
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100562 size_t i;
563
Julian Brownd93bdab2015-04-08 15:58:33 +0000564 goacc_lazy_initialize ();
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100565
566 struct goacc_thread *thr = goacc_thread ();
567 struct gomp_device_descr *acc_dev = thr->dev;
568
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200569 bool profiling_p = GOACC_PROFILING_DISPATCH_P (true);
570
571 acc_prof_info prof_info;
572 if (profiling_p)
573 {
574 thr->prof_info = &prof_info;
575
576 prof_info.event_type = acc_ev_update_start;
577 prof_info.valid_bytes = _ACC_PROF_INFO_VALID_BYTES;
578 prof_info.version = _ACC_PROF_INFO_VERSION;
579 prof_info.device_type = acc_device_type (acc_dev->type);
580 prof_info.device_number = acc_dev->target_id;
581 prof_info.thread_id = -1;
582 prof_info.async = async;
583 prof_info.async_queue = prof_info.async;
584 prof_info.src_file = NULL;
585 prof_info.func_name = NULL;
586 prof_info.line_no = -1;
587 prof_info.end_line_no = -1;
588 prof_info.func_line_no = -1;
589 prof_info.func_end_line_no = -1;
590 }
591 acc_event_info update_event_info;
592 if (profiling_p)
593 {
594 update_event_info.other_event.event_type
595 = prof_info.event_type;
596 update_event_info.other_event.valid_bytes
597 = _ACC_OTHER_EVENT_INFO_VALID_BYTES;
598 update_event_info.other_event.parent_construct = acc_construct_update;
599 update_event_info.other_event.implicit = 0;
600 update_event_info.other_event.tool_info = NULL;
601 }
602 acc_api_info api_info;
603 if (profiling_p)
604 {
605 thr->api_info = &api_info;
606
607 api_info.device_api = acc_device_api_none;
608 api_info.valid_bytes = _ACC_API_INFO_VALID_BYTES;
609 api_info.device_type = prof_info.device_type;
610 api_info.vendor = -1;
611 api_info.device_handle = NULL;
612 api_info.context_handle = NULL;
613 api_info.async_handle = NULL;
614 }
615
616 if (profiling_p)
617 goacc_profiling_dispatch (&prof_info, &update_event_info, &api_info);
618
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100619 if ((acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
Thomas Schwinge59d59602018-12-28 12:34:14 +0100620 || (flags & GOACC_FLAG_HOST_FALLBACK))
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200621 {
622 prof_info.device_type = acc_device_host;
623 api_info.device_type = prof_info.device_type;
624
625 goto out_prof;
626 }
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100627
Nathan Sidwella0911182015-07-20 17:31:46 +0000628 if (num_waits)
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100629 {
630 va_list ap;
631
632 va_start (ap, num_waits);
Nathan Sidwell3e32ee12015-09-28 19:37:33 +0000633 goacc_wait (async, num_waits, &ap);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100634 va_end (ap);
635 }
636
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000637 bool update_device = false;
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100638 for (i = 0; i < mapnum; ++i)
639 {
640 unsigned char kind = kinds[i] & 0xff;
641
642 switch (kind)
643 {
644 case GOMP_MAP_POINTER:
645 case GOMP_MAP_TO_PSET:
646 break;
647
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000648 case GOMP_MAP_ALWAYS_POINTER:
649 if (update_device)
650 {
651 /* Save the contents of the host pointer. */
652 void *dptr = acc_deviceptr (hostaddrs[i-1]);
653 uintptr_t t = *(uintptr_t *) hostaddrs[i];
654
655 /* Update the contents of the host pointer to reflect
656 the value of the allocated device memory in the
657 previous pointer. */
658 *(uintptr_t *) hostaddrs[i] = (uintptr_t)dptr;
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000659 /* TODO: verify that we really cannot use acc_update_device_async
660 here. */
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000661 acc_update_device (hostaddrs[i], sizeof (uintptr_t));
662
663 /* Restore the host pointer. */
664 *(uintptr_t *) hostaddrs[i] = t;
665 update_device = false;
666 }
667 break;
668
669 case GOMP_MAP_TO:
670 if (!acc_is_present (hostaddrs[i], sizes[i]))
671 {
672 update_device = false;
673 break;
674 }
675 /* Fallthru */
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100676 case GOMP_MAP_FORCE_TO:
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000677 update_device = true;
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000678 acc_update_device_async (hostaddrs[i], sizes[i], async);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100679 break;
680
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000681 case GOMP_MAP_FROM:
682 if (!acc_is_present (hostaddrs[i], sizes[i]))
683 {
684 update_device = false;
685 break;
686 }
687 /* Fallthru */
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100688 case GOMP_MAP_FORCE_FROM:
Chung-Lin Tang829c6342018-06-20 16:35:15 +0000689 update_device = false;
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000690 acc_update_self_async (hostaddrs[i], sizes[i], async);
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100691 break;
692
693 default:
694 gomp_fatal (">>>> GOACC_update UNHANDLED kind 0x%.2x", kind);
695 break;
696 }
697 }
Thomas Schwinge5fae0492019-05-17 21:13:36 +0200698
699 out_prof:
700 if (profiling_p)
701 {
702 prof_info.event_type = acc_ev_update_end;
703 update_event_info.other_event.event_type = prof_info.event_type;
704 goacc_profiling_dispatch (&prof_info, &update_event_info, &api_info);
705
706 thr->prof_info = NULL;
707 thr->api_info = NULL;
708 }
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100709}
710
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100711
Thomas Schwinge2bbbfa42019-05-06 10:49:55 +0200712/* Legacy entry point (GCC 5). */
713
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100714int
715GOACC_get_num_threads (void)
716{
717 return 1;
718}
719
Thomas Schwinge2bbbfa42019-05-06 10:49:55 +0200720/* Legacy entry point (GCC 5). */
721
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100722int
723GOACC_get_thread_num (void)
724{
725 return 0;
726}