1 /* Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 Contributed by Mentor Embedded.
5 This file is part of the GNU Offloading and Multi Processing Library
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)
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
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.
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/>. */
27 /* This file handles OpenACC constructs. */
31 #include "libgomp_g.h"
32 #include "gomp-constants.h"
34 #ifdef HAVE_INTTYPES_H
35 # include <inttypes.h> /* For PRIu64. */
41 /* Returns the number of mappings associated with the pointer or pset. PSET
42 have three mappings, whereas pointer have two. */
45 find_pointer (int pos
, size_t mapnum
, unsigned short *kinds
)
47 if (pos
+ 1 >= mapnum
)
50 unsigned char kind
= kinds
[pos
+1] & 0xff;
52 if (kind
== GOMP_MAP_TO_PSET
)
54 else if (kind
== GOMP_MAP_POINTER
)
60 static void *__goacc_host_ganglocal_ptr
;
63 GOACC_get_ganglocal_ptr (void)
65 return __goacc_host_ganglocal_ptr
;
69 alloc_host_shared_mem (size_t shared_size
)
72 __goacc_host_ganglocal_ptr
= malloc (shared_size
);
76 free_host_shared_mem (void)
78 if (__goacc_host_ganglocal_ptr
)
80 free (__goacc_host_ganglocal_ptr
);
81 __goacc_host_ganglocal_ptr
= NULL
;
86 alloc_ganglocal_addrs (size_t mapnum
, void **hostaddrs
, size_t *sizes
,
87 unsigned short *kinds
)
90 const int typemask
= 0xff;
93 for (i
= 0; i
< mapnum
; i
++)
95 if ((kinds
[i
] & typemask
) == GOMP_MAP_FORCE_TO_GANGLOCAL
)
97 t
= malloc (sizes
[i
]);
98 memcpy (t
, hostaddrs
[i
], sizes
[i
]);
101 if (i
+ 1 < mapnum
&& GOMP_MAP_POINTER_P (kinds
[i
+1] & typemask
))
103 size_t *ptr
= (size_t *) malloc (sizeof (size_t *));
105 hostaddrs
[i
+1] = ptr
;
112 static struct oacc_static
118 struct oacc_static
*next
;
121 static bool alloc_done
= false;
124 goacc_allocate_static (acc_device_t d
)
126 struct oacc_static
*s
;
131 for (s
= oacc_statics
; s
; s
= s
->next
)
137 case GOMP_MAP_FORCE_ALLOC
:
140 case GOMP_MAP_FORCE_TO
:
141 d
= acc_deviceptr (s
->addr
);
142 acc_memcpy_to_device (d
, s
->addr
, s
->size
);
145 case GOMP_MAP_FORCE_DEVICEPTR
:
146 case GOMP_MAP_DEVICE_RESIDENT
:
160 goacc_deallocate_static (acc_device_t d
)
162 struct oacc_static
*s
;
163 unsigned short mask
= GOMP_MAP_FORCE_DEALLOC
;
168 for (s
= oacc_statics
; s
; s
= s
->next
)
169 GOACC_enter_exit_data (d
, 1, &s
->addr
, &s
->size
, &mask
, 0, 0);
174 static void goacc_wait (int async
, int num_waits
, va_list ap
);
177 GOACC_parallel (int device
, void (*fn
) (void *),
178 size_t mapnum
, void **hostaddrs
, size_t *sizes
,
179 unsigned short *kinds
,
180 int num_gangs
, int num_workers
, int vector_length
,
182 int async
, int num_waits
, ...)
184 bool host_fallback
= device
== GOMP_DEVICE_HOST_FALLBACK
;
186 struct goacc_thread
*thr
;
187 struct gomp_device_descr
*acc_dev
;
188 struct target_mem_desc
*tgt
;
191 struct splay_tree_key_s k
;
192 splay_tree_key tgt_fn_key
;
195 #ifdef HAVE_INTTYPES_H
196 gomp_debug (0, "%s: mapnum=%"PRIu64
", hostaddrs=%p, sizes=%p, kinds=%p, "
197 "shared_size=%"PRIu64
", async = %d\n",
198 __FUNCTION__
, (uint64_t) mapnum
, hostaddrs
, sizes
, kinds
,
199 (uint64_t) shared_size
, async
);
201 gomp_debug (0, "%s: mapnum=%lu, hostaddrs=%p, sizes=%p, kinds=%p, "
202 "shared_size=%lu, async=%d\n",
203 __FUNCTION__
, (unsigned long) mapnum
, hostaddrs
, sizes
, kinds
,
204 (unsigned long) shared_size
, async
);
207 alloc_ganglocal_addrs (mapnum
, hostaddrs
, sizes
, kinds
);
209 goacc_lazy_initialize ();
211 thr
= goacc_thread ();
214 /* Host fallback if "if" clause is false or if the current device is set to
218 goacc_save_and_set_bind (acc_device_host
);
219 alloc_host_shared_mem (shared_size
);
221 free_host_shared_mem ();
222 goacc_restore_bind ();
225 else if (acc_device_type (acc_dev
->type
) == acc_device_host
)
227 alloc_host_shared_mem (shared_size
);
229 free_host_shared_mem ();
233 if (acc_device_type (acc_dev
->type
) == acc_device_host_nonshm
)
234 alloc_host_shared_mem (shared_size
);
236 va_start (ap
, num_waits
);
239 goacc_wait (async
, num_waits
, ap
);
243 acc_dev
->openacc
.async_set_async_func (async
);
245 if (!(acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_NATIVE_EXEC
))
247 k
.host_start
= (uintptr_t) fn
;
248 k
.host_end
= k
.host_start
+ 1;
249 gomp_mutex_lock (&acc_dev
->lock
);
250 tgt_fn_key
= splay_tree_lookup (&acc_dev
->mem_map
, &k
);
251 gomp_mutex_unlock (&acc_dev
->lock
);
253 if (tgt_fn_key
== NULL
)
254 gomp_fatal ("target function wasn't mapped");
256 tgt_fn
= (void (*)) tgt_fn_key
->tgt_offset
;
259 tgt_fn
= (void (*)) fn
;
261 tgt
= gomp_map_vars (acc_dev
, mapnum
, hostaddrs
, NULL
, sizes
, kinds
, true,
264 devaddrs
= gomp_alloca (sizeof (void *) * mapnum
);
265 for (i
= 0; i
< mapnum
; i
++)
266 devaddrs
[i
] = (void *) (tgt
->list
[i
]->tgt
->tgt_start
267 + tgt
->list
[i
]->tgt_offset
);
269 acc_dev
->openacc
.exec_func (tgt_fn
, mapnum
, hostaddrs
, devaddrs
, sizes
, kinds
,
270 num_gangs
, num_workers
, vector_length
, async
,
273 /* If running synchronously, unmap immediately. */
274 if (async
< acc_async_noval
)
275 gomp_unmap_vars (tgt
, true);
278 gomp_copy_from_async (tgt
);
279 acc_dev
->openacc
.register_async_cleanup_func (tgt
);
282 acc_dev
->openacc
.async_set_async_func (acc_async_sync
);
284 if (acc_device_type (acc_dev
->type
) == acc_device_host_nonshm
)
285 free_host_shared_mem ();
289 GOACC_data_start (int device
, size_t mapnum
,
290 void **hostaddrs
, size_t *sizes
, unsigned short *kinds
)
292 bool host_fallback
= device
== GOMP_DEVICE_HOST_FALLBACK
;
293 struct target_mem_desc
*tgt
;
295 #ifdef HAVE_INTTYPES_H
296 gomp_debug (0, "%s: mapnum=%"PRIu64
", hostaddrs=%p, sizes=%p, kinds=%p\n",
297 __FUNCTION__
, (uint64_t) mapnum
, hostaddrs
, sizes
, kinds
);
299 gomp_debug (0, "%s: mapnum=%lu, hostaddrs=%p, sizes=%p, kinds=%p\n",
300 __FUNCTION__
, (unsigned long) mapnum
, hostaddrs
, sizes
, kinds
);
303 goacc_lazy_initialize ();
305 struct goacc_thread
*thr
= goacc_thread ();
306 struct gomp_device_descr
*acc_dev
= thr
->dev
;
308 /* Host fallback or 'do nothing'. */
309 if ((acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
312 tgt
= gomp_map_vars (NULL
, 0, NULL
, NULL
, NULL
, NULL
, true, false);
313 tgt
->prev
= thr
->mapped_data
;
314 thr
->mapped_data
= tgt
;
319 gomp_debug (0, " %s: prepare mappings\n", __FUNCTION__
);
320 tgt
= gomp_map_vars (acc_dev
, mapnum
, hostaddrs
, NULL
, sizes
, kinds
, true,
322 gomp_debug (0, " %s: mappings prepared\n", __FUNCTION__
);
323 tgt
->prev
= thr
->mapped_data
;
324 thr
->mapped_data
= tgt
;
328 GOACC_data_end (void)
330 struct goacc_thread
*thr
= goacc_thread ();
331 struct target_mem_desc
*tgt
= thr
->mapped_data
;
333 gomp_debug (0, " %s: restore mappings\n", __FUNCTION__
);
334 thr
->mapped_data
= tgt
->prev
;
335 gomp_unmap_vars (tgt
, true);
336 gomp_debug (0, " %s: mappings restored\n", __FUNCTION__
);
340 GOACC_enter_exit_data (int device
, size_t mapnum
,
341 void **hostaddrs
, size_t *sizes
, unsigned short *kinds
,
342 int async
, int num_waits
, ...)
344 struct goacc_thread
*thr
;
345 struct gomp_device_descr
*acc_dev
;
346 bool host_fallback
= device
== GOMP_DEVICE_HOST_FALLBACK
;
347 bool data_enter
= false;
350 goacc_lazy_initialize ();
352 thr
= goacc_thread ();
355 if ((acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
363 va_start (ap
, num_waits
);
365 goacc_wait (async
, num_waits
, ap
);
370 acc_dev
->openacc
.async_set_async_func (async
);
372 /* Determine if this is an "acc enter data". */
373 for (i
= 0; i
< mapnum
; ++i
)
375 unsigned char kind
= kinds
[i
] & 0xff;
377 if (kind
== GOMP_MAP_POINTER
|| kind
== GOMP_MAP_TO_PSET
)
380 if (kind
== GOMP_MAP_FORCE_ALLOC
381 || kind
== GOMP_MAP_FORCE_PRESENT
382 || kind
== GOMP_MAP_FORCE_TO
383 || kind
== GOMP_MAP_TO
384 || kind
== GOMP_MAP_ALLOC
)
390 if (kind
== GOMP_MAP_FORCE_DEALLOC
391 || kind
== GOMP_MAP_FORCE_FROM
)
394 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
398 /* In c, non-pointers and arrays are represented by a single data clause.
399 Dynamically allocated arrays and subarrays are represented by a data
400 clause followed by an internal GOMP_MAP_POINTER.
402 In fortran, scalars and not allocated arrays are represented by a
403 single data clause. Allocated arrays and subarrays have three mappings:
404 1) the original data clause, 2) a PSET 3) a pointer to the array data.
409 for (i
= 0; i
< mapnum
; i
++)
411 unsigned char kind
= kinds
[i
] & 0xff;
413 /* Scan for pointers and PSETs. */
414 int pointer
= find_pointer (i
, mapnum
, kinds
);
421 acc_present_or_create (hostaddrs
[i
], sizes
[i
]);
423 case GOMP_MAP_FORCE_ALLOC
:
424 acc_create (hostaddrs
[i
], sizes
[i
]);
427 acc_present_or_copyin (hostaddrs
[i
], sizes
[i
]);
429 case GOMP_MAP_FORCE_TO
:
430 acc_copyin (hostaddrs
[i
], sizes
[i
]);
433 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
440 if (!acc_is_present (hostaddrs
[i
], sizes
[i
]))
442 gomp_acc_insert_pointer (pointer
, &hostaddrs
[i
],
443 &sizes
[i
], &kinds
[i
]);
445 /* Increment 'i' by two because OpenACC requires fortran
446 arrays to be contiguous, so each PSET is associated with
447 one of MAP_FORCE_ALLOC/MAP_FORCE_PRESET/MAP_FORCE_TO, and
454 for (i
= 0; i
< mapnum
; ++i
)
456 unsigned char kind
= kinds
[i
] & 0xff;
458 int pointer
= find_pointer (i
, mapnum
, kinds
);
464 case GOMP_MAP_FORCE_DEALLOC
:
465 if (acc_is_present (hostaddrs
[i
], sizes
[i
]))
466 acc_delete (hostaddrs
[i
], sizes
[i
]);
470 case GOMP_MAP_FORCE_FROM
:
471 acc_copyout (hostaddrs
[i
], sizes
[i
]);
474 gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
481 if (acc_is_present (hostaddrs
[i
], sizes
[i
]))
483 gomp_acc_remove_pointer (hostaddrs
[i
], (kinds
[i
] & 0xff)
484 == GOMP_MAP_FORCE_FROM
, async
,
486 /* See the above comment. */
492 acc_dev
->openacc
.async_set_async_func (acc_async_sync
);
496 GOACC_kernels (int device
, void (*fn
) (void *),
497 size_t mapnum
, void **hostaddrs
, size_t *sizes
,
498 unsigned short *kinds
,
499 int num_gangs
, int num_workers
, int vector_length
,
500 size_t shared_size
, int async
, int num_waits
, ...)
502 #ifdef HAVE_INTTYPES_H
503 gomp_debug (0, "%s: mapnum=%"PRIu64
", hostaddrs=%p, sizes=%p, kinds=%p\n",
504 __FUNCTION__
, (uint64_t) mapnum
, hostaddrs
, sizes
, kinds
);
506 gomp_debug (0, "%s: mapnum=%lu, hostaddrs=%p, sizes=%p, kinds=%p\n",
507 __FUNCTION__
, (unsigned long) mapnum
, hostaddrs
, sizes
, kinds
);
512 goacc_lazy_initialize ();
514 va_start (ap
, num_waits
);
517 goacc_wait (async
, num_waits
, ap
);
521 GOACC_parallel (device
, fn
, mapnum
, hostaddrs
, sizes
, kinds
,
522 num_gangs
, num_workers
, vector_length
, shared_size
,
527 goacc_wait (int async
, int num_waits
, va_list ap
)
529 struct goacc_thread
*thr
= goacc_thread ();
530 struct gomp_device_descr
*acc_dev
= thr
->dev
;
533 assert (num_waits
>= 0);
535 if (async
== acc_async_sync
&& num_waits
== 0)
541 if (async
== acc_async_sync
&& num_waits
)
543 for (i
= 0; i
< num_waits
; i
++)
545 int qid
= va_arg (ap
, int);
547 if (acc_async_test (qid
))
555 if (async
== acc_async_noval
&& num_waits
== 0)
557 acc_dev
->openacc
.async_wait_all_async_func (acc_async_noval
);
561 for (i
= 0; i
< num_waits
; i
++)
563 int qid
= va_arg (ap
, int);
565 if (acc_async_test (qid
))
568 /* If we're waiting on the same asynchronous queue as we're launching on,
569 the queue itself will order work as required, so there's no need to
572 acc_dev
->openacc
.async_wait_async_func (qid
, async
);
577 GOACC_update (int device
, size_t mapnum
,
578 void **hostaddrs
, size_t *sizes
, unsigned short *kinds
,
579 int async
, int num_waits
, ...)
581 bool host_fallback
= device
== GOMP_DEVICE_HOST_FALLBACK
;
584 goacc_lazy_initialize ();
586 struct goacc_thread
*thr
= goacc_thread ();
587 struct gomp_device_descr
*acc_dev
= thr
->dev
;
589 if ((acc_dev
->capabilities
& GOMP_OFFLOAD_CAP_SHARED_MEM
)
597 va_start (ap
, num_waits
);
599 goacc_wait (async
, num_waits
, ap
);
604 acc_dev
->openacc
.async_set_async_func (async
);
606 for (i
= 0; i
< mapnum
; ++i
)
608 unsigned char kind
= kinds
[i
] & 0xff;
612 case GOMP_MAP_POINTER
:
613 case GOMP_MAP_TO_PSET
:
616 case GOMP_MAP_FORCE_TO
:
617 case GOMP_MAP_FORCE_TO_GANGLOCAL
:
618 acc_update_device (hostaddrs
[i
], sizes
[i
]);
621 case GOMP_MAP_FORCE_FROM
:
622 acc_update_self (hostaddrs
[i
], sizes
[i
]);
626 gomp_fatal (">>>> GOACC_update UNHANDLED kind 0x%.2x", kind
);
631 acc_dev
->openacc
.async_set_async_func (acc_async_sync
);
635 GOACC_wait (int async
, int num_waits
, ...)
639 va_start (ap
, num_waits
);
641 goacc_wait (async
, num_waits
, ap
);
647 GOACC_get_num_threads (int gang
, int worker
, int vector
)
653 GOACC_get_thread_num (int gang
, int worker
, int vector
)
659 GOACC_register_static (void *addr
, int size
, unsigned int mask
)
661 struct oacc_static
*s
;
663 s
= (struct oacc_static
*) malloc (sizeof (struct oacc_static
));
665 s
->size
= (size_t) size
;
671 s
->next
= oacc_statics
;
679 GOACC_declare (int device
, size_t mapnum
,
680 void **hostaddrs
, size_t *sizes
, unsigned short *kinds
)
684 for (i
= 0; i
< mapnum
; i
++)
686 unsigned char kind
= kinds
[i
] & 0xff;
688 if (kind
== GOMP_MAP_POINTER
|| kind
== GOMP_MAP_TO_PSET
)
693 case GOMP_MAP_FORCE_ALLOC
:
694 case GOMP_MAP_FORCE_DEALLOC
:
695 case GOMP_MAP_FORCE_FROM
:
696 case GOMP_MAP_FORCE_TO
:
697 case GOMP_MAP_POINTER
:
698 GOACC_enter_exit_data (device
, 1, &hostaddrs
[i
], &sizes
[i
],
702 case GOMP_MAP_FORCE_DEVICEPTR
:
706 if (!acc_is_present (hostaddrs
[i
], sizes
[i
]))
708 GOACC_enter_exit_data (device
, 1, &hostaddrs
[i
], &sizes
[i
],
714 GOACC_enter_exit_data (device
, 1, &hostaddrs
[i
], &sizes
[i
],
720 kinds
[i
] = GOMP_MAP_FORCE_FROM
;
721 GOACC_enter_exit_data (device
, 1, &hostaddrs
[i
], &sizes
[i
],
725 case GOMP_MAP_FORCE_PRESENT
:
726 if (!acc_is_present (hostaddrs
[i
], sizes
[i
]))
727 gomp_fatal ("[%p,%zd] is not mapped", hostaddrs
[i
], sizes
[i
]);