diff --git a/openmp/libomptarget/CMakeLists.txt b/openmp/libomptarget/CMakeLists.txt --- a/openmp/libomptarget/CMakeLists.txt +++ b/openmp/libomptarget/CMakeLists.txt @@ -78,6 +78,7 @@ # Build offloading plugins and device RTLs if they are available. add_subdirectory(plugins) add_subdirectory(deviceRTLs) +add_subdirectory(tools) # Add tests. add_subdirectory(test) diff --git a/openmp/libomptarget/include/omptarget.h b/openmp/libomptarget/include/omptarget.h --- a/openmp/libomptarget/include/omptarget.h +++ b/openmp/libomptarget/include/omptarget.h @@ -210,6 +210,9 @@ /// adds a target shared library to the target execution image void __tgt_register_lib(__tgt_bin_desc *desc); +/// Initialize all RTLs at once +void __tgt_init_all_rtls(); + /// removes a target shared library from the target execution image void __tgt_unregister_lib(__tgt_bin_desc *desc); diff --git a/openmp/libomptarget/plugins/cuda/CMakeLists.txt b/openmp/libomptarget/plugins/cuda/CMakeLists.txt --- a/openmp/libomptarget/plugins/cuda/CMakeLists.txt +++ b/openmp/libomptarget/plugins/cuda/CMakeLists.txt @@ -63,6 +63,7 @@ if (LIBOMPTARGET_CAN_LINK_LIBCUDA OR LIBOMPTARGET_FORCE_DLOPEN_LIBCUDA) libomptarget_say("Enable tests using CUDA plugin") set(LIBOMPTARGET_SYSTEM_TARGETS "${LIBOMPTARGET_SYSTEM_TARGETS} nvptx64-nvidia-cuda" PARENT_SCOPE) + set(LIBOMPTARGET_TESTED_PLUGINS "omptarget.rtl.cuda" PARENT_SCOPE) else() libomptarget_say("Disabling tests using CUDA plugin as cuda may not be available") endif() diff --git a/openmp/libomptarget/src/exports b/openmp/libomptarget/src/exports --- a/openmp/libomptarget/src/exports +++ b/openmp/libomptarget/src/exports @@ -3,6 +3,7 @@ __tgt_register_requires; __tgt_register_lib; __tgt_unregister_lib; + __tgt_init_all_rtls; __tgt_target_data_begin; __tgt_target_data_end; __tgt_target_data_update; diff --git a/openmp/libomptarget/src/interface.cpp b/openmp/libomptarget/src/interface.cpp --- a/openmp/libomptarget/src/interface.cpp +++ b/openmp/libomptarget/src/interface.cpp @@ -43,6 +43,10 @@ PM->RTLs.RegisterLib(desc); } +//////////////////////////////////////////////////////////////////////////////// +/// Initialize all available devices without registering any image +EXTERN void __tgt_init_all_rtls() { PM->RTLs.initAllRTLs(); } + //////////////////////////////////////////////////////////////////////////////// /// unloads a target shared library EXTERN void __tgt_unregister_lib(__tgt_bin_desc *desc) { diff --git a/openmp/libomptarget/src/rtl.h b/openmp/libomptarget/src/rtl.h --- a/openmp/libomptarget/src/rtl.h +++ b/openmp/libomptarget/src/rtl.h @@ -121,6 +121,12 @@ // Register the clauses of the requires directive. void RegisterRequires(int64_t flags); + // Initialize RTL if it has not been initialized + void initRTLonce(RTLInfoTy &RTL); + + // Initialize all RTLs + void initAllRTLs(); + // Register a shared library with all (compatible) RTLs. void RegisterLib(__tgt_bin_desc *desc); diff --git a/openmp/libomptarget/src/rtl.cpp b/openmp/libomptarget/src/rtl.cpp --- a/openmp/libomptarget/src/rtl.cpp +++ b/openmp/libomptarget/src/rtl.cpp @@ -290,6 +290,38 @@ flags, RequiresFlags); } +void RTLsTy::initRTLonce(RTLInfoTy &R) { + // If this RTL is not already in use, initialize it. + if (!R.isUsed && R.NumberOfDevices != 0) { + // Initialize the device information for the RTL we are about to use. + DeviceTy device(&R); + size_t Start = PM->Devices.size(); + PM->Devices.resize(Start + R.NumberOfDevices, device); + for (int32_t device_id = 0; device_id < R.NumberOfDevices; device_id++) { + // global device ID + PM->Devices[Start + device_id].DeviceID = Start + device_id; + // RTL local device ID + PM->Devices[Start + device_id].RTLDeviceID = device_id; + } + + // Initialize the index of this RTL and save it in the used RTLs. + R.Idx = (UsedRTLs.empty()) + ? 0 + : UsedRTLs.back()->Idx + UsedRTLs.back()->NumberOfDevices; + assert((size_t)R.Idx == Start && + "RTL index should equal the number of devices used so far."); + R.isUsed = true; + UsedRTLs.push_back(&R); + + DP("RTL " DPxMOD " has index %d!\n", DPxPTR(R.LibraryHandler), R.Idx); + } +} + +void RTLsTy::initAllRTLs() { + for (auto &R : AllRTLs) + initRTLonce(R); +} + void RTLsTy::RegisterLib(__tgt_bin_desc *desc) { PM->RTLsMtx.lock(); // Register the images with the RTLs that understand them, if any. @@ -297,7 +329,7 @@ // Obtain the image. __tgt_device_image *img = &desc->DeviceImages[i]; - RTLInfoTy *FoundRTL = NULL; + RTLInfoTy *FoundRTL = nullptr; // Scan the RTLs that have associated images until we find one that supports // the current image. @@ -311,31 +343,7 @@ DP("Image " DPxMOD " is compatible with RTL %s!\n", DPxPTR(img->ImageStart), R.RTLName.c_str()); - // If this RTL is not already in use, initialize it. - if (!R.isUsed) { - // Initialize the device information for the RTL we are about to use. - DeviceTy device(&R); - size_t Start = PM->Devices.size(); - PM->Devices.resize(Start + R.NumberOfDevices, device); - for (int32_t device_id = 0; device_id < R.NumberOfDevices; - device_id++) { - // global device ID - PM->Devices[Start + device_id].DeviceID = Start + device_id; - // RTL local device ID - PM->Devices[Start + device_id].RTLDeviceID = device_id; - } - - // Initialize the index of this RTL and save it in the used RTLs. - R.Idx = (UsedRTLs.empty()) - ? 0 - : UsedRTLs.back()->Idx + UsedRTLs.back()->NumberOfDevices; - assert((size_t)R.Idx == Start && - "RTL index should equal the number of devices used so far."); - R.isUsed = true; - UsedRTLs.push_back(&R); - - DP("RTL " DPxMOD " has index %d!\n", DPxPTR(R.LibraryHandler), R.Idx); - } + initRTLonce(R); // Initialize (if necessary) translation table for this library. PM->TrlTblMtx.lock(); diff --git a/openmp/libomptarget/tools/CMakeLists.txt b/openmp/libomptarget/tools/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/openmp/libomptarget/tools/CMakeLists.txt @@ -0,0 +1,13 @@ +##===----------------------------------------------------------------------===## +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +##===----------------------------------------------------------------------===## +# +# Adding omptarget tools +# +##===----------------------------------------------------------------------===## + +add_subdirectory(deviceinfo) \ No newline at end of file diff --git a/openmp/libomptarget/tools/deviceinfo/CMakeLists.txt b/openmp/libomptarget/tools/deviceinfo/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/openmp/libomptarget/tools/deviceinfo/CMakeLists.txt @@ -0,0 +1,30 @@ +##===----------------------------------------------------------------------===## +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +##===----------------------------------------------------------------------===## +# +# Build llvm-omp-device-info tool +# +##===----------------------------------------------------------------------===## + +libomptarget_say("Building the llvm-omp-device-info tool") +libomptarget_say("llvm-omp-device-info using plugins ${LIBOMPTARGET_TESTED_PLUGINS}") + +add_llvm_tool(llvm-omp-device-info + llvm-omp-device-info.cpp + ) + + + + llvm_update_compile_flags(llvm-omp-device-info) + + target_link_libraries(llvm-omp-device-info + PRIVATE + omp + omptarget + ${LIBOMPTARGET_TESTED_PLUGINS} + ) + diff --git a/openmp/libomptarget/tools/deviceinfo/llvm-omp-device-info.cpp b/openmp/libomptarget/tools/deviceinfo/llvm-omp-device-info.cpp new file mode 100644 --- /dev/null +++ b/openmp/libomptarget/tools/deviceinfo/llvm-omp-device-info.cpp @@ -0,0 +1,32 @@ +//===- llvm-omp-device-info.cpp - Obtain device info as seen from OpenMP --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This is a command line utility that, by using Libomptarget, and the device +// plugins, list devices information as seen from the OpenMP Runtime. +// +//===----------------------------------------------------------------------===// + +#include "omptarget.h" +#include + +int main(int argc, char **argv) { + __tgt_bin_desc EmptyDesc = {0, nullptr, nullptr, nullptr}; + __tgt_register_lib(&EmptyDesc); + __tgt_init_all_rtls(); + + for (int Dev = 0; Dev < omp_get_num_devices(); Dev++) { + printf("Device (%d):\n", Dev); + if (!__tgt_print_device_info(Dev)) { + printf(" print_device_info not implemented\n"); + } + printf("\n"); + } + + __tgt_unregister_lib(&EmptyDesc); + return 0; +}