Skip to content

Commit 20f5bcf

Browse files
nicebertjhuber6
andauthored
[OpenMP] Add OpenMP extension API to dump mapping tables (#85381)
This adds an API call ompx_dump_mapping_tables. This allows users to debug the mapping tables and can be especially useful for unified shared memory applications to check if the code behaves in the way it should. The implementation reuses code already present to dump mapping tables (in a debug setting). --------- Co-authored-by: Joseph Huber <[email protected]>
1 parent 6800f42 commit 20f5bcf

File tree

8 files changed

+87
-18
lines changed

8 files changed

+87
-18
lines changed

openmp/libomptarget/include/OpenMP/Mapping.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ typedef int (*TargetDataFuncPtrTy)(ident_t *, DeviceTy &, int32_t, void **,
424424
map_var_info_t *, void **, AsyncInfoTy &,
425425
bool);
426426

427-
void dumpTargetPointerMappings(const ident_t *Loc, DeviceTy &Device);
427+
void dumpTargetPointerMappings(const ident_t *Loc, DeviceTy &Device,
428+
bool toStdOut = false);
428429

429430
int targetDataBegin(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
430431
void **ArgsBase, void **Args, int64_t *ArgSizes,

openmp/libomptarget/include/Shared/Debug.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ inline uint32_t getDebugLevel() {
136136
} while (0)
137137

138138
/// Print a generic information string used if LIBOMPTARGET_INFO=1
139-
#define INFO_MESSAGE(_num, ...) \
139+
#define INFO_MESSAGE(_num, ...) INFO_MESSAGE_TO(stderr, _num, __VA_ARGS__)
140+
141+
#define INFO_MESSAGE_TO(_stdDst, _num, ...) \
140142
do { \
141-
fprintf(stderr, GETNAME(TARGET_NAME) " device %d info: ", (int)_num); \
142-
fprintf(stderr, __VA_ARGS__); \
143+
fprintf(_stdDst, GETNAME(TARGET_NAME) " device %d info: ", (int)_num); \
144+
fprintf(_stdDst, __VA_ARGS__); \
143145
} while (0)
144146

145147
// Debugging messages
@@ -187,4 +189,13 @@ inline uint32_t getDebugLevel() {
187189
} \
188190
} while (false)
189191

192+
#define DUMP_INFO(toStdOut, _flags, _id, ...) \
193+
do { \
194+
if (toStdOut) { \
195+
INFO_MESSAGE_TO(stdout, _id, __VA_ARGS__); \
196+
} else { \
197+
INFO(_flags, _id, __VA_ARGS__); \
198+
} \
199+
} while (false)
200+
190201
#endif // OMPTARGET_SHARED_DEBUG_H

openmp/libomptarget/include/omptarget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ struct __tgt_target_non_contig {
273273
extern "C" {
274274
#endif
275275

276+
void ompx_dump_mapping_tables(void);
276277
int omp_get_num_devices(void);
277278
int omp_get_device_num(void);
278279
int omp_get_initial_device(void);

openmp/libomptarget/src/OpenMP/API.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "rtl.h"
1717

1818
#include "OpenMP/InternalTypes.h"
19+
#include "OpenMP/Mapping.h"
1920
#include "OpenMP/OMPT/Interface.h"
2021
#include "OpenMP/omp.h"
2122
#include "Shared/Profile.h"
@@ -27,6 +28,13 @@
2728
#include <cstring>
2829
#include <mutex>
2930

31+
EXTERN void ompx_dump_mapping_tables() {
32+
ident_t Loc = {0, 0, 0, 0, ";libomptarget;libomptarget;0;0;;"};
33+
auto ExclusiveDevicesAccessor = PM->getExclusiveDevicesAccessor();
34+
for (auto &Device : PM->devices(ExclusiveDevicesAccessor))
35+
dumpTargetPointerMappings(&Loc, Device, true);
36+
}
37+
3038
#ifdef OMPT_SUPPORT
3139
using namespace llvm::omp::target::ompt;
3240
#endif

openmp/libomptarget/src/OpenMP/Mapping.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@
1616
#include "device.h"
1717

1818
/// Dump a table of all the host-target pointer pairs on failure
19-
void dumpTargetPointerMappings(const ident_t *Loc, DeviceTy &Device) {
19+
void dumpTargetPointerMappings(const ident_t *Loc, DeviceTy &Device,
20+
bool toStdOut) {
2021
MappingInfoTy::HDTTMapAccessorTy HDTTMap =
2122
Device.getMappingInfo().HostDataToTargetMap.getExclusiveAccessor();
22-
if (HDTTMap->empty())
23+
if (HDTTMap->empty()) {
24+
DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,
25+
"OpenMP Host-Device pointer mappings table empty\n");
2326
return;
27+
}
2428

2529
SourceInfo Kernel(Loc);
26-
INFO(OMP_INFOTYPE_ALL, Device.DeviceID,
27-
"OpenMP Host-Device pointer mappings after block at %s:%d:%d:\n",
28-
Kernel.getFilename(), Kernel.getLine(), Kernel.getColumn());
29-
INFO(OMP_INFOTYPE_ALL, Device.DeviceID, "%-18s %-18s %s %s %s %s\n",
30-
"Host Ptr", "Target Ptr", "Size (B)", "DynRefCount", "HoldRefCount",
31-
"Declaration");
30+
DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,
31+
"OpenMP Host-Device pointer mappings after block at %s:%d:%d:\n",
32+
Kernel.getFilename(), Kernel.getLine(), Kernel.getColumn());
33+
DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,
34+
"%-18s %-18s %s %s %s %s\n", "Host Ptr", "Target Ptr", "Size (B)",
35+
"DynRefCount", "HoldRefCount", "Declaration");
3236
for (const auto &It : *HDTTMap) {
3337
HostDataToTargetTy &HDTT = *It.HDTT;
3438
SourceInfo Info(HDTT.HstPtrName);
35-
INFO(OMP_INFOTYPE_ALL, Device.DeviceID,
36-
DPxMOD " " DPxMOD " %-8" PRIuPTR " %-11s %-12s %s at %s:%d:%d\n",
37-
DPxPTR(HDTT.HstPtrBegin), DPxPTR(HDTT.TgtPtrBegin),
38-
HDTT.HstPtrEnd - HDTT.HstPtrBegin, HDTT.dynRefCountToStr().c_str(),
39-
HDTT.holdRefCountToStr().c_str(), Info.getName(), Info.getFilename(),
40-
Info.getLine(), Info.getColumn());
39+
DUMP_INFO(toStdOut, OMP_INFOTYPE_ALL, Device.DeviceID,
40+
DPxMOD " " DPxMOD " %-8" PRIuPTR " %-11s %-12s %s at %s:%d:%d\n",
41+
DPxPTR(HDTT.HstPtrBegin), DPxPTR(HDTT.TgtPtrBegin),
42+
HDTT.HstPtrEnd - HDTT.HstPtrBegin,
43+
HDTT.dynRefCountToStr().c_str(), HDTT.holdRefCountToStr().c_str(),
44+
Info.getName(), Info.getFilename(), Info.getLine(),
45+
Info.getColumn());
4146
}
4247
}
4348

openmp/libomptarget/src/exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ VERS1.0 {
3535
__tgt_push_mapper_component;
3636
__kmpc_push_target_tripcount;
3737
__kmpc_push_target_tripcount_mapper;
38+
ompx_dump_mapping_tables;
3839
omp_get_mapped_ptr;
3940
omp_get_num_devices;
4041
omp_get_device_num;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %libomptarget-compilexx-run-and-check-generic
2+
3+
#include <cstdio>
4+
#include <omp.h>
5+
6+
#define N 10
7+
8+
int main() {
9+
int *a = new int[N]; // mapped and released from device 0
10+
int *b = new int[N]; // mapped to device 2
11+
12+
// clang-format off
13+
// CHECK: Mapping tables after target enter data:
14+
// CHECK-NEXT: omptarget device 0 info: OpenMP Host-Device pointer mappings after block
15+
// CHECK-NEXT: omptarget device 0 info: Host Ptr Target Ptr Size (B) DynRefCount HoldRefCount Declaration
16+
// CHECK-NEXT: omptarget device 0 info: {{(0x[0-9a-f]{16})}} {{(0x[0-9a-f]{16})}}
17+
// CHECK-NEXT: omptarget device 1 info: OpenMP Host-Device pointer mappings table empty
18+
// CHECK-NEXT: omptarget device 2 info: OpenMP Host-Device pointer mappings after block
19+
// CHECK-NEXT: omptarget device 2 info: Host Ptr Target Ptr Size (B) DynRefCount HoldRefCount Declaration
20+
// CHECK-NEXT: omptarget device 2 info: {{(0x[0-9a-f]{16})}} {{(0x[0-9a-f]{16})}}
21+
// clang-format on
22+
#pragma omp target enter data device(0) map(to : a[ : N])
23+
#pragma omp target enter data device(2) map(to : b[ : N])
24+
printf("Mapping tables after target enter data:\n");
25+
ompx_dump_mapping_tables();
26+
27+
// clang-format off
28+
// CHECK: Mapping tables after target exit data for a:
29+
// CHECK-NEXT: omptarget device 0 info: OpenMP Host-Device pointer mappings table empty
30+
// CHECK-NEXT: omptarget device 1 info: OpenMP Host-Device pointer mappings table empty
31+
// CHECK-NEXT: omptarget device 2 info: OpenMP Host-Device pointer mappings after block
32+
// CHECK-NEXT: omptarget device 2 info: Host Ptr Target Ptr Size (B) DynRefCount HoldRefCount Declaration
33+
// CHECK-NEXT: omptarget device 2 info: {{(0x[0-9a-f]{16})}} {{(0x[0-9a-f]{16})}}
34+
// clang-format on
35+
#pragma omp target exit data device(0) map(release : a[ : N])
36+
printf("\nMapping tables after target exit data for a:\n");
37+
ompx_dump_mapping_tables();
38+
39+
return 0;
40+
}

openmp/runtime/src/include/omp.h.var

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@
156156
/* OpenMP 5.1 interop */
157157
typedef intptr_t omp_intptr_t;
158158

159+
extern void __KAI_KMPC_CONVENTION ompx_dump_mapping_tables(void);
160+
159161
/* 0..omp_get_num_interop_properties()-1 are reserved for implementation-defined properties */
160162
typedef enum omp_interop_property {
161163
omp_ipr_fr_id = -1,

0 commit comments

Comments
 (0)