77 lines · c
1// RUN: %libomptarget-compile-run-and-check-generic2 3#include <omp.h>4#include <stdio.h>5#include <string.h>6 7int test_omp_device_uid(int device_num) {8 const char *device_uid = omp_get_uid_from_device(device_num);9 if (device_uid == NULL) {10 printf("FAIL for device %d: omp_get_uid_from_device returned NULL\n",11 device_num);12 return 0;13 }14 15 int device_num_from_uid = omp_get_device_from_uid(device_uid);16 if (device_num_from_uid != device_num) {17 printf(18 "FAIL for device %d: omp_get_device_from_uid returned %d (UID: %s)\n",19 device_num, device_num_from_uid, device_uid);20 return 0;21 }22 23 if (device_num == omp_get_initial_device())24 return 1;25 26 int success = 1;27 28// Note that the following code may be executed on the host if the host is the29// device30#pragma omp target map(tofrom : success) device(device_num)31 {32 int device_num = omp_get_device_num();33 34 // omp_get_uid_from_device() in the device runtime is a dummy function35 // returning NULL36 const char *device_uid = omp_get_uid_from_device(device_num);37 38 // omp_get_device_from_uid() in the device runtime is a dummy function39 // returning omp_invalid_device.40 int device_num_from_uid = omp_get_device_from_uid(device_uid);41 42 // Depending on whether we're executing on the device or the host, we either43 // got NULL as the device UID or the correct device UID. Consequently,44 // omp_get_device_from_uid() either returned omp_invalid_device or the45 // correct device number (aka omp_get_initial_device()).46 if (device_uid ? device_num_from_uid != device_num47 : device_num_from_uid != omp_invalid_device) {48 printf("FAIL for device %d (target): omp_get_device_from_uid returned %d "49 "(UID: %s)\n",50 device_num, device_num_from_uid, device_uid);51 success = 0;52 }53 }54 55 return success;56}57 58int main() {59 int num_devices = omp_get_num_devices();60 int num_failed = 0;61 // (also test initial device aka num_devices)62 for (int i = 0; i < num_devices + 1; i++) {63 if (!test_omp_device_uid(i)) {64 printf("FAIL for device %d\n", i);65 num_failed++;66 }67 }68 if (num_failed) {69 printf("FAIL\n");70 return 1;71 }72 printf("PASS\n");73 return 0;74}75 76// CHECK: PASS77