From 616dd9ae143172472bde793683d6e4df79554ac8 Mon Sep 17 00:00:00 2001 From: Jose M Monsalve Diaz Date: Wed, 22 Jun 2022 10:05:34 -0500 Subject: [PATCH] [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 --- .../libomptarget/DeviceRTL/include/Configuration.h | 3 +- openmp/libomptarget/DeviceRTL/include/Interface.h | 2 ++ openmp/libomptarget/DeviceRTL/src/State.cpp | 2 ++ openmp/libomptarget/include/omptarget.h | 1 + openmp/libomptarget/src/api.cpp | 9 ++++++ openmp/libomptarget/src/exports | 1 + openmp/libomptarget/test/api/omp_get_device_num.c | 33 ++++++++++++++++++++++ openmp/runtime/test/api/omp_get_device_num.c | 27 ++++++++++++++++++ 8 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 openmp/libomptarget/test/api/omp_get_device_num.c create mode 100644 openmp/runtime/test/api/omp_get_device_num.c diff --git a/openmp/libomptarget/DeviceRTL/include/Configuration.h b/openmp/libomptarget/DeviceRTL/include/Configuration.h index 94f11b6..368a7c3 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 cb79fd4..2ebe08e 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 c891063..3126000 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 5217f40..f104c56 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 29af016..8f6a32f 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 d4911ad..fe27885 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 0000000..a0a897a --- /dev/null +++ b/openmp/libomptarget/test/api/omp_get_device_num.c @@ -0,0 +1,33 @@ +// RUN: %libomptarget-compile-run-and-check-generic + +#include +#include + +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 0000000..3b51115 --- /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 +#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; +} -- 2.7.4