|
| 1 | +// RUN: %libomptarget-compile-run-and-check-generic |
| 2 | + |
| 3 | +#include <omp.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +// Note that the device UIDs for the "fake" host devices used by libomptarget |
| 8 | +// will always be the same as the UID for the initial device (since it *is* the |
| 9 | +// same device). The other way round, the device number returned for this UID |
| 10 | +// will always be the initial device. |
| 11 | + |
| 12 | +int is_host_device_uid(const char *device_uid) { |
| 13 | + return strcmp(device_uid, |
| 14 | + omp_get_uid_from_device(omp_get_initial_device())) == 0; |
| 15 | +} |
| 16 | + |
| 17 | +int test_omp_device_uid(int device_num) { |
| 18 | + const char *device_uid = omp_get_uid_from_device(device_num); |
| 19 | + if (device_uid == NULL) { |
| 20 | + printf("FAIL for device %d: omp_get_uid_from_device returned NULL\n", |
| 21 | + device_num); |
| 22 | + return 0; |
| 23 | + } |
| 24 | + |
| 25 | + int device_num_from_uid = omp_get_device_from_uid(device_uid); |
| 26 | + if (device_num_from_uid != (is_host_device_uid(device_uid) |
| 27 | + ? omp_get_initial_device() |
| 28 | + : device_num)) { |
| 29 | + printf( |
| 30 | + "FAIL for device %d: omp_get_device_from_uid returned %d (UID: %s)\n", |
| 31 | + device_num, device_num_from_uid, device_uid); |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + if (device_num == omp_get_initial_device()) |
| 36 | + return 1; |
| 37 | + |
| 38 | + int success = 1; |
| 39 | + |
| 40 | +// Note that the following code may be executed on the host if the host is the |
| 41 | +// device |
| 42 | +#pragma omp target map(tofrom : success) device(device_num) |
| 43 | + { |
| 44 | + int device_num = omp_get_device_num(); |
| 45 | + |
| 46 | + // omp_get_uid_from_device() in the device runtime is a dummy function |
| 47 | + // returning NULL |
| 48 | + const char *device_uid_target = omp_get_uid_from_device(device_num); |
| 49 | + |
| 50 | + // omp_get_device_from_uid() in the device runtime is a dummy function |
| 51 | + // returning omp_invalid_device. |
| 52 | + device_num_from_uid = omp_get_device_from_uid(device_uid_target); |
| 53 | + |
| 54 | + // Depending on whether we're executing on the device or the host, we either |
| 55 | + // got NULL as the device UID or the correct device UID. Consequently, |
| 56 | + // omp_get_device_from_uid() either returned omp_invalid_device or the |
| 57 | + // correct device number (aka omp_get_initial_device()). |
| 58 | + if (device_uid_target ? device_num_from_uid != omp_get_initial_device() |
| 59 | + : device_num_from_uid != omp_invalid_device) { |
| 60 | + printf("FAIL for device %d (target): omp_get_device_from_uid returned %d " |
| 61 | + "(UID: %s)\n", |
| 62 | + device_num, device_num_from_uid, device_uid_target); |
| 63 | + success = 0; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return success; |
| 68 | +} |
| 69 | + |
| 70 | +int main() { |
| 71 | + int num_devices = omp_get_num_devices(); |
| 72 | + int num_failed = 0; |
| 73 | + // (also test initial device aka num_devices) |
| 74 | + for (int i = 0; i < num_devices + 1; i++) { |
| 75 | + if (!test_omp_device_uid(i)) { |
| 76 | + printf("FAIL for device %d\n", i); |
| 77 | + num_failed++; |
| 78 | + } |
| 79 | + } |
| 80 | + if (num_failed) { |
| 81 | + printf("FAIL\n"); |
| 82 | + return 1; |
| 83 | + } |
| 84 | + printf("PASS\n"); |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +// CHECK: PASS |
0 commit comments