Frederik Harwath | 2e5ea57 | 2020-01-29 10:19:50 +0100 | [diff] [blame] | 1 | /* Test the `acc_get_property' and `acc_get_property_string' library |
| 2 | functions on amdgcn devices by comparing property values with |
| 3 | those obtained through the HSA API. */ |
| 4 | /* { dg-additional-sources acc_get_property-aux.c } */ |
| 5 | /* { dg-additional-options "-ldl" } */ |
Thomas Schwinge | 4912a04 | 2020-04-21 14:16:24 +0200 | [diff] [blame] | 6 | /* { dg-do run { target openacc_radeon_accel_selected } } */ |
Frederik Harwath | 2e5ea57 | 2020-01-29 10:19:50 +0100 | [diff] [blame] | 7 | |
| 8 | #include <dlfcn.h> |
| 9 | #include <stdint.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <openacc.h> |
| 14 | |
| 15 | #ifndef __cplusplus |
| 16 | typedef int bool; |
| 17 | #endif |
| 18 | #include <hsa.h> |
| 19 | |
| 20 | |
| 21 | void expect_device_string_properties (acc_device_t dev_type, int dev_num, |
| 22 | const char* expected_vendor, |
| 23 | const char* expected_name, |
| 24 | const char* expected_driver); |
| 25 | |
| 26 | hsa_status_t (*hsa_agent_get_info_fn) (hsa_agent_t agent, |
| 27 | hsa_agent_info_t attribute, |
| 28 | void *value); |
| 29 | hsa_status_t (*hsa_system_get_info_fn) (hsa_system_info_t attribute, |
| 30 | void *value); |
| 31 | hsa_status_t (*hsa_iterate_agents_fn) |
| 32 | (hsa_status_t (*callback)(hsa_agent_t agent, void *data), void *data); |
| 33 | hsa_status_t (*hsa_init_fn) (void); |
| 34 | |
| 35 | char* support_cpu_devices; |
| 36 | |
Frederik Harwath | 87c3fcf | 2020-01-29 10:21:18 +0100 | [diff] [blame] | 37 | void |
| 38 | test_setup () |
Frederik Harwath | 2e5ea57 | 2020-01-29 10:19:50 +0100 | [diff] [blame] | 39 | { |
| 40 | char* env_runtime; |
| 41 | char* hsa_runtime_lib; |
| 42 | void *handle; |
| 43 | |
| 44 | #define DLSYM_FN(function) \ |
| 45 | function##_fn = (typeof(function##_fn))dlsym (handle, #function); \ |
| 46 | if (function##_fn == NULL) \ |
| 47 | { \ |
| 48 | fprintf (stderr, "Could not get symbol " #function ".\n"); \ |
| 49 | abort (); \ |
| 50 | } |
| 51 | |
| 52 | env_runtime = getenv ("HSA_RUNTIME_LIB"); |
| 53 | hsa_runtime_lib = env_runtime ? env_runtime : (char*)"libhsa-runtime64.so"; |
| 54 | |
| 55 | handle = dlopen (hsa_runtime_lib, RTLD_LAZY); |
| 56 | if (!handle) |
| 57 | { |
| 58 | fprintf (stderr, "Could not load %s.\n", hsa_runtime_lib); |
| 59 | abort (); |
| 60 | } |
| 61 | |
| 62 | DLSYM_FN (hsa_agent_get_info) |
| 63 | DLSYM_FN (hsa_system_get_info) |
| 64 | DLSYM_FN (hsa_iterate_agents) |
| 65 | DLSYM_FN (hsa_init) |
| 66 | |
| 67 | hsa_init_fn (); |
| 68 | |
| 69 | support_cpu_devices = getenv ("GCN_SUPPORT_CPU_DEVICES"); |
| 70 | } |
| 71 | |
Frederik Harwath | 87c3fcf | 2020-01-29 10:21:18 +0100 | [diff] [blame] | 72 | static hsa_status_t |
| 73 | check_agent_properties (hsa_agent_t agent, void *dev_num_arg) |
Frederik Harwath | 2e5ea57 | 2020-01-29 10:19:50 +0100 | [diff] [blame] | 74 | { |
| 75 | |
| 76 | char name[64]; |
| 77 | char vendor_name[64]; |
| 78 | uint16_t minor; |
| 79 | uint16_t major; |
| 80 | char driver[60]; |
| 81 | |
| 82 | hsa_status_t status; |
| 83 | hsa_device_type_t device_type; |
| 84 | int* dev_num = (int*)dev_num_arg; |
| 85 | |
| 86 | #define AGENT_GET_INFO(info_type, val) \ |
| 87 | status = hsa_agent_get_info_fn (agent, info_type, &val); \ |
| 88 | if (status != HSA_STATUS_SUCCESS) \ |
| 89 | { \ |
| 90 | fprintf (stderr, "Failed to obtain " #info_type ".\n"); \ |
| 91 | abort (); \ |
| 92 | } |
| 93 | #define SYSTEM_GET_INFO(info_type, val) \ |
| 94 | status = hsa_system_get_info_fn (info_type, &val); \ |
| 95 | if (status != HSA_STATUS_SUCCESS) \ |
| 96 | { \ |
| 97 | fprintf (stderr, "Failed to obtain " #info_type ".\n"); \ |
| 98 | abort (); \ |
| 99 | } |
| 100 | |
| 101 | AGENT_GET_INFO (HSA_AGENT_INFO_DEVICE, device_type) |
| 102 | |
| 103 | /* Skip unsupported device types. Mimic the GCN plugin's behavior. */ |
| 104 | if (!(device_type == HSA_DEVICE_TYPE_GPU |
| 105 | || (support_cpu_devices && device_type == HSA_DEVICE_TYPE_CPU))) |
| 106 | return HSA_STATUS_SUCCESS; |
| 107 | |
| 108 | AGENT_GET_INFO (HSA_AGENT_INFO_NAME, name) |
| 109 | AGENT_GET_INFO (HSA_AGENT_INFO_VENDOR_NAME, vendor_name) |
| 110 | |
| 111 | SYSTEM_GET_INFO (HSA_SYSTEM_INFO_VERSION_MINOR, minor) |
| 112 | SYSTEM_GET_INFO (HSA_SYSTEM_INFO_VERSION_MAJOR, major) |
| 113 | |
| 114 | snprintf (driver, sizeof driver, "HSA Runtime %hu.%hu", |
| 115 | (unsigned short int)major, (unsigned short int)minor); |
| 116 | |
| 117 | expect_device_string_properties(acc_device_radeon, *dev_num, |
| 118 | vendor_name, name, driver); |
| 119 | |
| 120 | (*dev_num)++; |
| 121 | |
| 122 | return status; |
| 123 | } |
| 124 | |
Frederik Harwath | 87c3fcf | 2020-01-29 10:21:18 +0100 | [diff] [blame] | 125 | int |
| 126 | main () |
Frederik Harwath | 2e5ea57 | 2020-01-29 10:19:50 +0100 | [diff] [blame] | 127 | { |
| 128 | int dev_num = 0; |
| 129 | test_setup (); |
| 130 | |
| 131 | hsa_status_t status = |
| 132 | hsa_iterate_agents_fn (&check_agent_properties, &dev_num); |
| 133 | |
| 134 | return status; |
| 135 | } |