Files
Jan André Reuter 083d2c4b1f [Offload][OpenMP] Accept omp_initial_device for runtime calls (#205290)
In OpenMP v5.2, the constant `omp_initial_device` was introduced, which
was defined as a value of `-1`.
Support for this value was added to Clang and Flang in
09cd294482 and
3c14034c55.

However, runtime calls are not aware of this constant. As such,
execution of a runtime function, passing `omp_initial_device` as the
`device_num`, would abort with e.g.,:

```
omptarget fatal error -1: "invalid value" device number '-1' out of range, only 1 devices available
```

To fix this issue, also extend the host device checks in the API
functions to also check for `omp_initial_device`.
For testing, extend API tests using `omp_get_initial_device()` to also
test `omp_initial_device` and extend `omp_device_uid.c` to check if the
UID for `omp_initial_device` and `omp_get_initial_device()` matches.

Partially addresses https://github.com/llvm/llvm-project/issues/192980
(see
https://github.com/llvm/llvm-project/issues/192980#issuecomment-4404241994)

---------

Signed-off-by: Jan André Reuter <j.reuter@fz-juelich.de>
Signed-off-by: Jan André Reuter <jan@zyten.de>
2026-06-30 17:01:01 +02:00

85 lines
2.5 KiB
C

// RUN: %libomptarget-compile-run-and-check-generic
#include <omp.h>
#include <stdio.h>
#include <string.h>
int test_omp_device_uid(int device_num) {
const char *device_uid = omp_get_uid_from_device(device_num);
if (device_uid == NULL) {
printf("FAIL for device %d: omp_get_uid_from_device returned NULL\n",
device_num);
return 0;
}
int device_num_from_uid = omp_get_device_from_uid(device_uid);
if (device_num_from_uid != device_num) {
printf(
"FAIL for device %d: omp_get_device_from_uid returned %d (UID: %s)\n",
device_num, device_num_from_uid, device_uid);
return 0;
}
if (device_num == omp_get_initial_device())
return 1;
int success = 1;
// Note that the following code may be executed on the host if the host is the
// device
#pragma omp target map(tofrom : success) device(device_num)
{
int device_num = omp_get_device_num();
// omp_get_uid_from_device() in the device runtime is a dummy function
// returning NULL
const char *device_uid = omp_get_uid_from_device(device_num);
// omp_get_device_from_uid() in the device runtime is a dummy function
// returning omp_invalid_device.
int device_num_from_uid = omp_get_device_from_uid(device_uid);
// Depending on whether we're executing on the device or the host, we either
// got NULL as the device UID or the correct device UID. Consequently,
// omp_get_device_from_uid() either returned omp_invalid_device or the
// correct device number (aka omp_get_initial_device()).
if (device_uid ? device_num_from_uid != device_num
: device_num_from_uid != omp_invalid_device) {
printf("FAIL for device %d (target): omp_get_device_from_uid returned %d "
"(UID: %s)\n",
device_num, device_num_from_uid, device_uid);
success = 0;
}
}
return success;
}
int main() {
int num_devices = omp_get_num_devices();
int num_failed = 0;
// (also test initial device aka num_devices)
for (int i = 0; i < num_devices + 1; i++) {
if (!test_omp_device_uid(i)) {
printf("FAIL for device %d\n", i);
num_failed++;
}
}
// test that uid for omp_initial_device matches uid
// for omp_get_num_devices(), i.e. the host device
if (omp_get_uid_from_device(omp_initial_device) !=
omp_get_uid_from_device(num_devices)) {
printf("FAIL for matching uid for omp_initial_device and "
"omp_get_initial_device()");
num_failed++;
}
if (num_failed) {
printf("FAIL\n");
return 1;
}
printf("PASS\n");
return 0;
}
// CHECK: PASS