Skip to content

Commit 313c523

Browse files
Jose M Monsalve Diazshiltian
authored andcommitted
[OpenMP][Tool] Introducing the llvm-omp-device-info tool
This patch introduces the `llvm-omp-device-info` tool, which uses the omptarget library and interface to query the device info from all the available devices as seen by OpenMP. This is inspired by PGI's `pgaccelinfo` Since omptarget usually requires a description structure with executable kernels, I split the initialization of the RTLs and Devices to be able to initialize all possible devices and query each of them. This revision relies on the patch that introduces the print device info. A limitation is that the order in which the devices are initialized, and the corresponding device ID is not necesarily the one seen by OpenMP. The changes are as follows: 1. Separate the RTL initialization that was performed in `RegisterLib` to its own `initRTLonce` function 2. Create an `initAllRTLs` method that initializes all available RTLs at runtime 3. Created the `llvm-deviceinfo.cpp` tool that uses `omptarget` to query each device and prints its information. Example Output: ``` Device (0): print_device_info not implemented Device (1): print_device_info not implemented Device (2): print_device_info not implemented Device (3): print_device_info not implemented Device (4): CUDA Driver Version: 11000 CUDA Device Number: 0 Device Name: Quadro P1000 Global Memory Size: 4236312576 bytes Number of Multiprocessors: 5 Concurrent Copy and Execution: Yes Total Constant Memory: 65536 bytes Max Shared Memory per Block: 49152 bytes Registers per Block: 65536 Warp Size: 32 Threads Maximum Threads per Block: 1024 Maximum Block Dimensions: 1024, 1024, 64 Maximum Grid Dimensions: 2147483647 x 65535 x 65535 Maximum Memory Pitch: 2147483647 bytes Texture Alignment: 512 bytes Clock Rate: 1480500 kHz Execution Timeout: Yes Integrated Device: No Can Map Host Memory: Yes Compute Mode: DEFAULT Concurrent Kernels: Yes ECC Enabled: No Memory Clock Rate: 2505000 kHz Memory Bus Width: 128 bits L2 Cache Size: 1048576 bytes Max Threads Per SMP: 2048 Async Engines: Yes (2) Unified Addressing: Yes Managed Memory: Yes Concurrent Managed Memory: Yes Preemption Supported: Yes Cooperative Launch: Yes Multi-Device Boars: No Compute Capabilities: 61 ``` Reviewed By: tianshilei1992 Differential Revision: https://reviews.llvm.org/D106752
1 parent fe7ca1a commit 313c523

File tree

10 files changed

+119
-26
lines changed

10 files changed

+119
-26
lines changed

openmp/libomptarget/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ set(LIBOMPTARGET_OPENMP_HOST_RTL_FOLDER "${LIBOMP_LIBRARY_DIR}" CACHE STRING
7979
add_subdirectory(plugins)
8080
add_subdirectory(deviceRTLs)
8181
add_subdirectory(DeviceRTL)
82+
add_subdirectory(tools)
8283

8384
# Add tests.
8485
add_subdirectory(test)

openmp/libomptarget/include/omptarget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ void __tgt_register_requires(int64_t flags);
210210
/// adds a target shared library to the target execution image
211211
void __tgt_register_lib(__tgt_bin_desc *desc);
212212

213+
/// Initialize all RTLs at once
214+
void __tgt_init_all_rtls();
215+
213216
/// removes a target shared library from the target execution image
214217
void __tgt_unregister_lib(__tgt_bin_desc *desc);
215218

openmp/libomptarget/plugins/cuda/src/rtl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
#include <list>
1717
#include <memory>
1818
#include <mutex>
19+
#include <string.h>
1920
#include <string>
2021
#include <unordered_map>
2122
#include <vector>
23+
#include <string.h>
2224

2325
#include "Debug.h"
2426
#include "omptargetplugin.h"

openmp/libomptarget/src/exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ VERS1.0 {
33
__tgt_register_requires;
44
__tgt_register_lib;
55
__tgt_unregister_lib;
6+
__tgt_init_all_rtls;
67
__tgt_target_data_begin;
78
__tgt_target_data_end;
89
__tgt_target_data_update;

openmp/libomptarget/src/interface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ EXTERN void __tgt_register_lib(__tgt_bin_desc *desc) {
4343
PM->RTLs.RegisterLib(desc);
4444
}
4545

46+
////////////////////////////////////////////////////////////////////////////////
47+
/// Initialize all available devices without registering any image
48+
EXTERN void __tgt_init_all_rtls() { PM->RTLs.initAllRTLs(); }
49+
4650
////////////////////////////////////////////////////////////////////////////////
4751
/// unloads a target shared library
4852
EXTERN void __tgt_unregister_lib(__tgt_bin_desc *desc) {

openmp/libomptarget/src/rtl.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,46 @@ void RTLsTy::RegisterRequires(int64_t flags) {
290290
flags, RequiresFlags);
291291
}
292292

293+
void RTLsTy::initRTLonce(RTLInfoTy &R) {
294+
// If this RTL is not already in use, initialize it.
295+
if (!R.isUsed && R.NumberOfDevices != 0) {
296+
// Initialize the device information for the RTL we are about to use.
297+
DeviceTy device(&R);
298+
size_t Start = PM->Devices.size();
299+
PM->Devices.resize(Start + R.NumberOfDevices, device);
300+
for (int32_t device_id = 0; device_id < R.NumberOfDevices; device_id++) {
301+
// global device ID
302+
PM->Devices[Start + device_id].DeviceID = Start + device_id;
303+
// RTL local device ID
304+
PM->Devices[Start + device_id].RTLDeviceID = device_id;
305+
}
306+
307+
// Initialize the index of this RTL and save it in the used RTLs.
308+
R.Idx = (UsedRTLs.empty())
309+
? 0
310+
: UsedRTLs.back()->Idx + UsedRTLs.back()->NumberOfDevices;
311+
assert((size_t)R.Idx == Start &&
312+
"RTL index should equal the number of devices used so far.");
313+
R.isUsed = true;
314+
UsedRTLs.push_back(&R);
315+
316+
DP("RTL " DPxMOD " has index %d!\n", DPxPTR(R.LibraryHandler), R.Idx);
317+
}
318+
}
319+
320+
void RTLsTy::initAllRTLs() {
321+
for (auto &R : AllRTLs)
322+
initRTLonce(R);
323+
}
324+
293325
void RTLsTy::RegisterLib(__tgt_bin_desc *desc) {
294326
PM->RTLsMtx.lock();
295327
// Register the images with the RTLs that understand them, if any.
296328
for (int32_t i = 0; i < desc->NumDeviceImages; ++i) {
297329
// Obtain the image.
298330
__tgt_device_image *img = &desc->DeviceImages[i];
299331

300-
RTLInfoTy *FoundRTL = NULL;
332+
RTLInfoTy *FoundRTL = nullptr;
301333

302334
// Scan the RTLs that have associated images until we find one that supports
303335
// the current image.
@@ -311,31 +343,7 @@ void RTLsTy::RegisterLib(__tgt_bin_desc *desc) {
311343
DP("Image " DPxMOD " is compatible with RTL %s!\n",
312344
DPxPTR(img->ImageStart), R.RTLName.c_str());
313345

314-
// If this RTL is not already in use, initialize it.
315-
if (!R.isUsed) {
316-
// Initialize the device information for the RTL we are about to use.
317-
DeviceTy device(&R);
318-
size_t Start = PM->Devices.size();
319-
PM->Devices.resize(Start + R.NumberOfDevices, device);
320-
for (int32_t device_id = 0; device_id < R.NumberOfDevices;
321-
device_id++) {
322-
// global device ID
323-
PM->Devices[Start + device_id].DeviceID = Start + device_id;
324-
// RTL local device ID
325-
PM->Devices[Start + device_id].RTLDeviceID = device_id;
326-
}
327-
328-
// Initialize the index of this RTL and save it in the used RTLs.
329-
R.Idx = (UsedRTLs.empty())
330-
? 0
331-
: UsedRTLs.back()->Idx + UsedRTLs.back()->NumberOfDevices;
332-
assert((size_t)R.Idx == Start &&
333-
"RTL index should equal the number of devices used so far.");
334-
R.isUsed = true;
335-
UsedRTLs.push_back(&R);
336-
337-
DP("RTL " DPxMOD " has index %d!\n", DPxPTR(R.LibraryHandler), R.Idx);
338-
}
346+
initRTLonce(R);
339347

340348
// Initialize (if necessary) translation table for this library.
341349
PM->TrlTblMtx.lock();

openmp/libomptarget/src/rtl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ struct RTLsTy {
121121
// Register the clauses of the requires directive.
122122
void RegisterRequires(int64_t flags);
123123

124+
// Initialize RTL if it has not been initialized
125+
void initRTLonce(RTLInfoTy &RTL);
126+
127+
// Initialize all RTLs
128+
void initAllRTLs();
129+
124130
// Register a shared library with all (compatible) RTLs.
125131
void RegisterLib(__tgt_bin_desc *desc);
126132

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
##===----------------------------------------------------------------------===##
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
##===----------------------------------------------------------------------===##
8+
#
9+
# Adding omptarget tools
10+
#
11+
##===----------------------------------------------------------------------===##
12+
13+
add_subdirectory(deviceinfo)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
##===----------------------------------------------------------------------===##
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
##===----------------------------------------------------------------------===##
8+
#
9+
# Build llvm-omp-device-info tool
10+
#
11+
##===----------------------------------------------------------------------===##
12+
13+
libomptarget_say("Building the llvm-omp-device-info tool")
14+
libomptarget_say("llvm-omp-device-info using plugins ${LIBOMPTARGET_TESTED_PLUGINS}")
15+
16+
add_llvm_tool(llvm-omp-device-info llvm-omp-device-info.cpp)
17+
18+
llvm_update_compile_flags(llvm-omp-device-info)
19+
20+
target_link_libraries(llvm-omp-device-info PRIVATE
21+
omp
22+
omptarget
23+
${LIBOMPTARGET_TESTED_PLUGINS}
24+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- llvm-omp-device-info.cpp - Obtain device info as seen from OpenMP --===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is a command line utility that, by using Libomptarget, and the device
10+
// plugins, list devices information as seen from the OpenMP Runtime.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "omptarget.h"
15+
#include <cstdio>
16+
17+
int main(int argc, char **argv) {
18+
__tgt_bin_desc EmptyDesc = {0, nullptr, nullptr, nullptr};
19+
__tgt_register_lib(&EmptyDesc);
20+
__tgt_init_all_rtls();
21+
22+
for (int Dev = 0; Dev < omp_get_num_devices(); Dev++) {
23+
printf("Device (%d):\n", Dev);
24+
if (!__tgt_print_device_info(Dev))
25+
printf(" print_device_info not implemented\n");
26+
printf("\n");
27+
}
28+
29+
__tgt_unregister_lib(&EmptyDesc);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)