[Openmp-commits] [openmp] 616dd9a - [OpenMP] Implementing omp_get_device_num()

Jose M Monsalve Diaz via Openmp-commits openmp-commits at lists.llvm.org
Wed Jun 29 00:18:52 PDT 2022


Author: Jose M Monsalve Diaz
Date: 2022-06-29T02:18:21-05:00
New Revision: 616dd9ae143172472bde793683d6e4df79554ac8

URL: https://github.com/llvm/llvm-project/commit/616dd9ae143172472bde793683d6e4df79554ac8
DIFF: https://github.com/llvm/llvm-project/commit/616dd9ae143172472bde793683d6e4df79554ac8.diff

LOG: [OpenMP] Implementing omp_get_device_num()

This patch implements omp_get_device_num() in the host and the device.

It uses the already existing getDeviceNum in the device config for the device.
And in the host it uses the omp_get_num_devices().

Two simple tests added

Differential Revision: https://reviews.llvm.org/D128347

Added: 
    openmp/libomptarget/test/api/omp_get_device_num.c
    openmp/runtime/test/api/omp_get_device_num.c

Modified: 
    openmp/libomptarget/DeviceRTL/include/Configuration.h
    openmp/libomptarget/DeviceRTL/include/Interface.h
    openmp/libomptarget/DeviceRTL/src/State.cpp
    openmp/libomptarget/include/omptarget.h
    openmp/libomptarget/src/api.cpp
    openmp/libomptarget/src/exports

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/DeviceRTL/include/Configuration.h b/openmp/libomptarget/DeviceRTL/include/Configuration.h
index 94f11b6066a20..368a7c35ac4a8 100644
--- a/openmp/libomptarget/DeviceRTL/include/Configuration.h
+++ b/openmp/libomptarget/DeviceRTL/include/Configuration.h
@@ -28,8 +28,7 @@ enum DebugKind : uint32_t {
 /// host by omp_get_num_devices.
 uint32_t getNumDevices();
 
-/// Return the number of devices in the system, same number as returned on the
-/// host by omp_get_num_devices.
+/// Return the device number in the system for omp_get_device_num.
 uint32_t getDeviceNum();
 
 /// Return the user choosen debug level.

diff  --git a/openmp/libomptarget/DeviceRTL/include/Interface.h b/openmp/libomptarget/DeviceRTL/include/Interface.h
index cb79fd44eee76..2ebe08e4084f5 100644
--- a/openmp/libomptarget/DeviceRTL/include/Interface.h
+++ b/openmp/libomptarget/DeviceRTL/include/Interface.h
@@ -126,6 +126,8 @@ int omp_get_default_device(void);
 
 int omp_get_num_devices(void);
 
+int omp_get_device_num(void);
+
 int omp_get_num_teams(void);
 
 int omp_get_team_num();

diff  --git a/openmp/libomptarget/DeviceRTL/src/State.cpp b/openmp/libomptarget/DeviceRTL/src/State.cpp
index c8910635278ff..312600062813a 100644
--- a/openmp/libomptarget/DeviceRTL/src/State.cpp
+++ b/openmp/libomptarget/DeviceRTL/src/State.cpp
@@ -504,6 +504,8 @@ int omp_get_default_device(void) { return -1; }
 
 int omp_get_num_devices(void) { return config::getNumDevices(); }
 
+int omp_get_device_num(void) { return config::getDeviceNum(); }
+
 int omp_get_num_teams(void) { return mapping::getNumberOfBlocks(); }
 
 int omp_get_team_num() { return mapping::getBlockId(); }

diff  --git a/openmp/libomptarget/include/omptarget.h b/openmp/libomptarget/include/omptarget.h
index 5217f40a5eccf..f104c5671787b 100644
--- a/openmp/libomptarget/include/omptarget.h
+++ b/openmp/libomptarget/include/omptarget.h
@@ -202,6 +202,7 @@ extern "C" {
 #endif
 
 int omp_get_num_devices(void);
+int omp_get_device_num(void);
 int omp_get_initial_device(void);
 void *omp_target_alloc(size_t size, int device_num);
 void omp_target_free(void *device_ptr, int device_num);

diff  --git a/openmp/libomptarget/src/api.cpp b/openmp/libomptarget/src/api.cpp
index 29af0167e1f3d..8f6a32fe1540f 100644
--- a/openmp/libomptarget/src/api.cpp
+++ b/openmp/libomptarget/src/api.cpp
@@ -30,6 +30,15 @@ EXTERN int omp_get_num_devices(void) {
   return DevicesSize;
 }
 
+EXTERN int omp_get_device_num(void) {
+  TIMESCOPE();
+  int HostDevice = omp_get_initial_device();
+
+  DP("Call to omp_get_device_num returning %d\n", HostDevice);
+
+  return HostDevice;
+}
+
 EXTERN int omp_get_initial_device(void) {
   TIMESCOPE();
   int hostDevice = omp_get_num_devices();

diff  --git a/openmp/libomptarget/src/exports b/openmp/libomptarget/src/exports
index d4911addf3c81..fe27885dfb23e 100644
--- a/openmp/libomptarget/src/exports
+++ b/openmp/libomptarget/src/exports
@@ -29,6 +29,7 @@ VERS1.0 {
     __kmpc_push_target_tripcount;
     __kmpc_push_target_tripcount_mapper;
     omp_get_num_devices;
+    omp_get_device_num;
     omp_get_initial_device;
     omp_target_alloc;
     omp_target_free;

diff  --git a/openmp/libomptarget/test/api/omp_get_device_num.c b/openmp/libomptarget/test/api/omp_get_device_num.c
new file mode 100644
index 0000000000000..a0a897a204793
--- /dev/null
+++ b/openmp/libomptarget/test/api/omp_get_device_num.c
@@ -0,0 +1,33 @@
+// RUN: %libomptarget-compile-run-and-check-generic
+
+#include <stdio.h>
+#include <omp.h>
+
+int test_omp_get_device_num()
+{
+  /* checks that omp_get_device_num() == omp_get_num_devices() in the host */
+  int device_num = omp_get_device_num();
+  printf("device_num = %d\n", device_num);
+
+  #pragma omp target
+  {}
+
+  return (device_num == omp_get_num_devices());
+}
+
+int main()
+{
+  int i;
+  int failed=0;
+
+  if (!test_omp_get_device_num()) {
+    failed++;
+  }
+  if (failed)
+    printf("FAIL\n");
+  else
+    printf("PASS\n");
+  return failed;
+}
+
+// CHECK: PASS

diff  --git a/openmp/runtime/test/api/omp_get_device_num.c b/openmp/runtime/test/api/omp_get_device_num.c
new file mode 100644
index 0000000000000..3b511157e031e
--- /dev/null
+++ b/openmp/runtime/test/api/omp_get_device_num.c
@@ -0,0 +1,27 @@
+// RUN: %libomp-compile-and-run
+// Linking fails for icc 18
+// UNSUPPORTED: icc-18
+
+#include <stdio.h>
+#include "omp_testsuite.h"
+
+int test_omp_get_device_num()
+{
+  /* checks that omp_get_device_num */
+  int device_num = omp_get_device_num();
+
+  return (device_num == omp_get_num_devices());
+}
+
+int main()
+{
+  int i;
+  int num_failed=0;
+
+  for(i = 0; i < REPETITIONS; i++) {
+    if(!test_omp_get_device_num()) {
+      num_failed++;
+    }
+  }
+  return num_failed;
+}


        


More information about the Openmp-commits mailing list