Skip to content

Commit c073357

Browse files
committed
[OFFLOAD] Stricter enforcement of user offload disable
If user specifies offload is disabled (e.g., OMP_TARGET_OFFLOAD=disable), disable library almost completely. This reduces resources spent to a minimum and ensures all APIs behave as if the only available device is the host device. Currently some of the APIs behave as if there were devices avaible for offload even when under OMP_TARGET_OFFLOAD=disable.
1 parent 71f43a7 commit c073357

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

offload/include/OffloadPolicy.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern "C" int __kmpc_get_target_offload(void) __attribute__((weak));
2626

2727
class OffloadPolicy {
2828

29-
OffloadPolicy(PluginManager &PM) {
29+
OffloadPolicy() {
3030
// TODO: Check for OpenMP.
3131
switch ((kmp_target_offload_kind_t)__kmpc_get_target_offload()) {
3232
case tgt_disabled:
@@ -36,6 +36,17 @@ class OffloadPolicy {
3636
Kind = MANDATORY;
3737
return;
3838
default:
39+
// delay DEFAULT policy until PluginManager is ready
40+
UserValue = false;
41+
return;
42+
};
43+
}
44+
45+
OffloadPolicy(PluginManager &PM) {
46+
const OffloadPolicy &OP = get();
47+
if (!OP.UserValue) {
48+
// User didn't specify a policy, decide
49+
// based on number of devices discovered
3950
if (PM.getNumDevices()) {
4051
DP("Default TARGET OFFLOAD policy is now mandatory "
4152
"(devices were found)\n");
@@ -46,10 +57,16 @@ class OffloadPolicy {
4657
Kind = DISABLED;
4758
}
4859
return;
49-
};
60+
}
61+
Kind = OP.Kind;
5062
}
5163

5264
public:
65+
static const OffloadPolicy &get() {
66+
static OffloadPolicy OP;
67+
return OP;
68+
}
69+
5370
static const OffloadPolicy &get(PluginManager &PM) {
5471
static OffloadPolicy OP(PM);
5572
return OP;
@@ -58,6 +75,7 @@ class OffloadPolicy {
5875
enum OffloadPolicyKind { DISABLED, MANDATORY };
5976

6077
OffloadPolicyKind Kind = MANDATORY;
78+
bool UserValue = true;
6179
};
6280

6381
#endif // OMPTARGET_OFFLOAD_POLICY_H

offload/libomptarget/OffloadRTL.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "OffloadPolicy.h"
1314
#include "OpenMP/OMPT/Callback.h"
1415
#include "PluginManager.h"
1516

@@ -31,6 +32,12 @@ void initRuntime() {
3132
if (PM == nullptr)
3233
PM = new PluginManager();
3334

35+
if (OffloadPolicy::get().Kind == OffloadPolicy::DISABLED) {
36+
DP("Offload is disabled. Skipping library initialization\n");
37+
// Do only absolutely needed initialization
38+
return;
39+
}
40+
3441
RefCount++;
3542
if (RefCount == 1) {
3643
DP("Init offload library!\n");

0 commit comments

Comments
 (0)