[gcc/devel/omp/gcc-10] openmp: Change omp_get_initial_device () to match OpenMP 5.1 requirements

Kwok Yeung kcy@gcc.gnu.org
Fri Nov 13 11:53:45 GMT 2020


https://gcc.gnu.org/g:99b827f8cb1b1d084828dedc5269fc61c69bd827

commit 99b827f8cb1b1d084828dedc5269fc61c69bd827
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Oct 22 09:31:01 2020 +0200

    openmp: Change omp_get_initial_device () to match OpenMP 5.1 requirements
    
    > Therefore, I think until omp_get_initial_device () value is changed, we
    
    The following so far untested patch implements that change.
    
    OpenMP 4.5 said for omp_get_initial_device:
    The value of the device number is implementation defined. If it is between 0 and one less than
    omp_get_num_devices() then it is valid for use with all device constructs and routines; if it is
    outside that range, then it is only valid for use with the device memory routines and not in the
    device clause.
    and OpenMP 5.0 similarly, but OpenMP 5.1 says:
    The value of the device number is the value returned by the omp_get_num_devices routine.
    
    As the new value is compatible with what has been required earlier, I think
    we can change it already now.
    
    This is a backport from mainline (commit
    74c9882b80bda50b37c9555498de7123c6bdb9e4).
    
    2020-10-22  Jakub Jelinek  <jakub@redhat.com>
    
            * icv.c (omp_get_initial_device): Remove including corresponding
            ialias.
            * icv-device.c (omp_get_initial_device): New function.  Return
            gomp_get_num_devices ().  Add ialias.
            * target.c (resolve_device): Don't fail with
            OMP_TARGET_OFFLOAD=mandatory if device_id is equal to
            gomp_get_num_devices ().
            (omp_target_alloc, omp_target_free, omp_target_is_present,
            omp_target_memcpy, omp_target_memcpy_rect, omp_target_associate_ptr,
            omp_target_disassociate_ptr, omp_pause_resource): Use
            gomp_get_num_devices () instead of GOMP_DEVICE_HOST_FALLBACK on the
            first use in the functions, in uses dominated by the
            gomp_get_num_devices call use num_devices_openmp instead.
            * libgomp.texi (omp_get_initial_device): Document.
            * config/gcn/icv-device.c (omp_get_initial_device): New function.
            Add ialias.
            * config/nvptx/icv-device.c (omp_get_initial_device): Likewise.
            * testsuite/libgomp.c/target-40.c: New test.

Diff:
---
 libgomp/ChangeLog.omp                   | 24 ++++++++++++++++++++++++
 libgomp/config/gcn/icv-device.c         |  7 +++++++
 libgomp/config/nvptx/icv-device.c       |  7 +++++++
 libgomp/icv-device.c                    |  7 +++++++
 libgomp/icv.c                           |  7 -------
 libgomp/libgomp.texi                    | 28 ++++++++++++++++++++++++++++
 libgomp/target.c                        | 28 ++++++++++++++--------------
 libgomp/testsuite/libgomp.c/target-40.c | 10 ++++++++++
 8 files changed, 97 insertions(+), 21 deletions(-)

diff --git a/libgomp/ChangeLog.omp b/libgomp/ChangeLog.omp
index 9b50729bd46..6dc9187181c 100644
--- a/libgomp/ChangeLog.omp
+++ b/libgomp/ChangeLog.omp
@@ -1,3 +1,27 @@
+2020-11-11  Kwok Cheung Yeung  <kcy@codesourcery.com>
+
+	Backport from mainline
+	2020-10-22  Jakub Jelinek  <jakub@redhat.com>
+
+	* icv.c (omp_get_initial_device): Remove including corresponding
+	ialias.
+	* icv-device.c (omp_get_initial_device): New function.  Return
+	gomp_get_num_devices ().  Add ialias.
+	* target.c (resolve_device): Don't fail with
+	OMP_TARGET_OFFLOAD=mandatory if device_id is equal to
+	gomp_get_num_devices ().
+	(omp_target_alloc, omp_target_free, omp_target_is_present,
+	omp_target_memcpy, omp_target_memcpy_rect, omp_target_associate_ptr,
+	omp_target_disassociate_ptr, omp_pause_resource): Use
+	gomp_get_num_devices () instead of GOMP_DEVICE_HOST_FALLBACK on the
+	first use in the functions, in uses dominated by the
+	gomp_get_num_devices call use num_devices_openmp instead.
+	* libgomp.texi (omp_get_initial_device): Document.
+	* config/gcn/icv-device.c (omp_get_initial_device): New function.
+	Add ialias.
+	* config/nvptx/icv-device.c (omp_get_initial_device): Likewise.
+	* testsuite/libgomp.c/target-40.c: New test.
+
 2020-11-11  Kwok Cheung Yeung  <kcy@codesourcery.com>
 
 	Backport from mainline
diff --git a/libgomp/config/gcn/icv-device.c b/libgomp/config/gcn/icv-device.c
index 1b09f9738f1..ba3a446a6ea 100644
--- a/libgomp/config/gcn/icv-device.c
+++ b/libgomp/config/gcn/icv-device.c
@@ -39,6 +39,12 @@ omp_get_default_device (void)
   return 0;
 }
 
+int
+omp_get_initial_device (void)
+{
+  return GOMP_DEVICE_HOST_FALLBACK;
+}
+
 int
 omp_get_num_devices (void)
 {
@@ -66,6 +72,7 @@ omp_is_initial_device (void)
 
 ialias (omp_set_default_device)
 ialias (omp_get_default_device)
+ialias (omp_get_initial_device)
 ialias (omp_get_num_devices)
 ialias (omp_get_num_teams)
 ialias (omp_get_team_num)
diff --git a/libgomp/config/nvptx/icv-device.c b/libgomp/config/nvptx/icv-device.c
index 6a70329c032..795da7db757 100644
--- a/libgomp/config/nvptx/icv-device.c
+++ b/libgomp/config/nvptx/icv-device.c
@@ -39,6 +39,12 @@ omp_get_default_device (void)
   return 0;
 }
 
+int
+omp_get_initial_device (void)
+{
+  return GOMP_DEVICE_HOST_FALLBACK;
+}
+
 int
 omp_get_num_devices (void)
 {
@@ -54,5 +60,6 @@ omp_is_initial_device (void)
 
 ialias (omp_set_default_device)
 ialias (omp_get_default_device)
+ialias (omp_get_initial_device)
 ialias (omp_get_num_devices)
 ialias (omp_is_initial_device)
diff --git a/libgomp/icv-device.c b/libgomp/icv-device.c
index 414960c3821..af7972eae67 100644
--- a/libgomp/icv-device.c
+++ b/libgomp/icv-device.c
@@ -42,6 +42,12 @@ omp_get_default_device (void)
   return icv->default_device_var;
 }
 
+int
+omp_get_initial_device (void)
+{
+  return gomp_get_num_devices ();
+}
+
 int
 omp_get_num_devices (void)
 {
@@ -57,5 +63,6 @@ omp_is_initial_device (void)
 
 ialias (omp_set_default_device)
 ialias (omp_get_default_device)
+ialias (omp_get_initial_device)
 ialias (omp_get_num_devices)
 ialias (omp_is_initial_device)
diff --git a/libgomp/icv.c b/libgomp/icv.c
index 1bb46abac43..4da6527c9b8 100644
--- a/libgomp/icv.c
+++ b/libgomp/icv.c
@@ -155,12 +155,6 @@ omp_get_proc_bind (void)
   return icv->bind_var;
 }
 
-int
-omp_get_initial_device (void)
-{
-  return GOMP_DEVICE_HOST_FALLBACK;
-}
-
 int
 omp_get_num_places (void)
 {
@@ -241,7 +235,6 @@ ialias (omp_get_max_active_levels)
 ialias (omp_get_supported_active_levels)
 ialias (omp_get_cancellation)
 ialias (omp_get_proc_bind)
-ialias (omp_get_initial_device)
 ialias (omp_get_max_task_priority)
 ialias (omp_get_num_places)
 ialias (omp_get_place_num)
diff --git a/libgomp/libgomp.texi b/libgomp/libgomp.texi
index 92168960ae8..347f84e7c5e 100644
--- a/libgomp/libgomp.texi
+++ b/libgomp/libgomp.texi
@@ -166,6 +166,7 @@ linkage, and do not throw exceptions.
 * omp_get_cancellation::        Whether cancellation support is enabled
 * omp_get_default_device::      Get the default device for target regions
 * omp_get_dynamic::             Dynamic teams setting
+* omp_get_initial_device::      Device number of host device
 * omp_get_level::               Number of parallel regions
 * omp_get_max_active_levels::   Current maximum number of active regions
 * omp_get_max_task_priority::   Maximum task priority value that can be set
@@ -353,6 +354,33 @@ disabled by default.
 
 
 
+@node omp_get_initial_device
+@section @code{omp_get_initial_device} -- Return device number of initial device
+@table @asis
+@item @emph{Description}:
+This function returns a device number that represents the host device.
+For OpenMP 5.1, this must be equal to the value returned by the
+@code{omp_get_num_devices} function.
+
+@item @emph{C/C++}
+@multitable @columnfractions .20 .80
+@item @emph{Prototype}: @tab @code{int omp_get_initial_device(void);}
+@end multitable
+
+@item @emph{Fortran}:
+@multitable @columnfractions .20 .80
+@item @emph{Interface}: @tab @code{integer function omp_get_initial_device()}
+@end multitable
+
+@item @emph{See also}:
+@ref{omp_get_num_devices}
+
+@item @emph{Reference}:
+@uref{https://www.openmp.org, OpenMP specification v4.5}, Section 3.2.35.
+@end table
+
+
+
 @node omp_get_level
 @section @code{omp_get_level} -- Obtain the current nesting level
 @table @asis
diff --git a/libgomp/target.c b/libgomp/target.c
index 95c094105b3..6bf36afb353 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -118,7 +118,8 @@ resolve_device (int device_id)
   if (device_id < 0 || device_id >= gomp_get_num_devices ())
     {
       if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY
-	  && device_id != GOMP_DEVICE_HOST_FALLBACK)
+	  && device_id != GOMP_DEVICE_HOST_FALLBACK
+	  && device_id != num_devices_openmp)
 	gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
 		    "but device not found");
 
@@ -132,8 +133,7 @@ resolve_device (int device_id)
     {
       gomp_mutex_unlock (&devices[device_id].lock);
 
-      if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY
-	  && device_id != GOMP_DEVICE_HOST_FALLBACK)
+      if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY)
 	gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
 		    "but device is finalized");
 
@@ -2904,7 +2904,7 @@ GOMP_teams (unsigned int num_teams, unsigned int thread_limit)
 void *
 omp_target_alloc (size_t size, int device_num)
 {
-  if (device_num == GOMP_DEVICE_HOST_FALLBACK)
+  if (device_num == gomp_get_num_devices ())
     return malloc (size);
 
   if (device_num < 0)
@@ -2930,7 +2930,7 @@ omp_target_free (void *device_ptr, int device_num)
   if (device_ptr == NULL)
     return;
 
-  if (device_num == GOMP_DEVICE_HOST_FALLBACK)
+  if (device_num == gomp_get_num_devices ())
     {
       free (device_ptr);
       return;
@@ -2961,7 +2961,7 @@ omp_target_is_present (const void *ptr, int device_num)
   if (ptr == NULL)
     return 1;
 
-  if (device_num == GOMP_DEVICE_HOST_FALLBACK)
+  if (device_num == gomp_get_num_devices ())
     return 1;
 
   if (device_num < 0)
@@ -2995,7 +2995,7 @@ omp_target_memcpy (void *dst, const void *src, size_t length,
   struct gomp_device_descr *dst_devicep = NULL, *src_devicep = NULL;
   bool ret;
 
-  if (dst_device_num != GOMP_DEVICE_HOST_FALLBACK)
+  if (dst_device_num != gomp_get_num_devices ())
     {
       if (dst_device_num < 0)
 	return EINVAL;
@@ -3008,7 +3008,7 @@ omp_target_memcpy (void *dst, const void *src, size_t length,
 	  || dst_devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
 	dst_devicep = NULL;
     }
-  if (src_device_num != GOMP_DEVICE_HOST_FALLBACK)
+  if (src_device_num != num_devices_openmp)
     {
       if (src_device_num < 0)
 	return EINVAL;
@@ -3146,7 +3146,7 @@ omp_target_memcpy_rect (void *dst, const void *src, size_t element_size,
   if (!dst && !src)
     return INT_MAX;
 
-  if (dst_device_num != GOMP_DEVICE_HOST_FALLBACK)
+  if (dst_device_num != gomp_get_num_devices ())
     {
       if (dst_device_num < 0)
 	return EINVAL;
@@ -3159,7 +3159,7 @@ omp_target_memcpy_rect (void *dst, const void *src, size_t element_size,
 	  || dst_devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
 	dst_devicep = NULL;
     }
-  if (src_device_num != GOMP_DEVICE_HOST_FALLBACK)
+  if (src_device_num != num_devices_openmp)
     {
       if (src_device_num < 0)
 	return EINVAL;
@@ -3195,7 +3195,7 @@ int
 omp_target_associate_ptr (const void *host_ptr, const void *device_ptr,
 			  size_t size, size_t device_offset, int device_num)
 {
-  if (device_num == GOMP_DEVICE_HOST_FALLBACK)
+  if (device_num == gomp_get_num_devices ())
     return EINVAL;
 
   if (device_num < 0)
@@ -3258,7 +3258,7 @@ omp_target_associate_ptr (const void *host_ptr, const void *device_ptr,
 int
 omp_target_disassociate_ptr (const void *ptr, int device_num)
 {
-  if (device_num == GOMP_DEVICE_HOST_FALLBACK)
+  if (device_num == gomp_get_num_devices ())
     return EINVAL;
 
   if (device_num < 0)
@@ -3301,9 +3301,9 @@ int
 omp_pause_resource (omp_pause_resource_t kind, int device_num)
 {
   (void) kind;
-  if (device_num == GOMP_DEVICE_HOST_FALLBACK)
+  if (device_num == gomp_get_num_devices ())
     return gomp_pause_host ();
-  if (device_num < 0 || device_num >= gomp_get_num_devices ())
+  if (device_num < 0 || device_num >= num_devices_openmp)
     return -1;
   /* Do nothing for target devices for now.  */
   return 0;
diff --git a/libgomp/testsuite/libgomp.c/target-40.c b/libgomp/testsuite/libgomp.c/target-40.c
new file mode 100644
index 00000000000..138e16298d5
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/target-40.c
@@ -0,0 +1,10 @@
+#include <omp.h>
+#include <stdlib.h>
+
+int
+main ()
+{
+  if (omp_get_initial_device () != omp_get_num_devices ())
+    abort ();
+  return 0;
+}


More information about the Gcc-cvs mailing list