Skip to content

Commit e2f9aa5

Browse files
authored
[OpenMP][MI-300][Runtime] Handling crash with OMP_TARGET_OFFLOAD=DISABLED and invoking omp_get_default_device() (#809)
Fix a crash in `omp_get_default_device()` when `OMP_TARGET_OFFLOAD=DISABLED` is set together with a non-zero `OMP_DEFAULT_DEVICE` value. When users set both: `OMP_TARGET_OFFLOAD=DISABLED` (to disable GPU offloading) `OMP_DEFAULT_DEVICE=N` (where N > 0) The application would crash with: `omptarget fatal error 2: "invalid value" device number 'N' out of range, only 0 devices available` Reason: This occurs because `omp_get_default_device()` returned the value from `OMP_DEFAULT_DEVICE` (or from `omp_set_default_device()`) even when offloading was disabled, causing program to execute the code associated with the non-existent devices. The solution ensures that when offloading is disabled, all device-related functions return values consistent with host-only execution by returning the initial device value. Testing: testing with the smoke test with PR [1787](ROCm/aomp#1787)
1 parent 69e3fa1 commit e2f9aa5

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

openmp/runtime/src/kmp_ftn_entry.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,14 +1213,6 @@ int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_TEAM_NUM)(void) {
12131213
#endif
12141214
}
12151215

1216-
int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_DEFAULT_DEVICE)(void) {
1217-
#if KMP_MIC || KMP_OS_DARWIN || defined(KMP_STUB)
1218-
return 0;
1219-
#else
1220-
return __kmp_entry_thread()->th.th_current_task->td_icvs.default_device;
1221-
#endif
1222-
}
1223-
12241216
void FTN_STDCALL KMP_EXPAND_NAME(FTN_SET_DEFAULT_DEVICE)(int KMP_DEREF arg) {
12251217
#if KMP_MIC || KMP_OS_DARWIN || defined(KMP_STUB)
12261218
// Nothing.
@@ -1267,6 +1259,17 @@ int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)(void) {
12671259
return KMP_EXPAND_NAME(FTN_GET_NUM_DEVICES)();
12681260
}
12691261

1262+
int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_DEFAULT_DEVICE)(void) {
1263+
#if KMP_MIC || KMP_OS_DARWIN || defined(KMP_STUB)
1264+
return 0;
1265+
#else
1266+
// When offloading is disabled, return the initial device (host)
1267+
if (__kmp_target_offload == tgt_disabled)
1268+
return KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)();
1269+
return __kmp_entry_thread()->th.th_current_task->td_icvs.default_device;
1270+
#endif
1271+
}
1272+
12701273
#if defined(KMP_STUB)
12711274
// Entries for stubs library
12721275
// As all *target* functions are C-only parameters always passed by value

0 commit comments

Comments
 (0)