Debian Codegraph @

libgomp/testsuite/libgomp.oacc-c-c++-common/acc_get_property-gcn.c

raw
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" } */
6 /* { dg-do run { target openacc_radeon_accel_selected } } */
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 #include <hsa.h>
16
17
18 void expect_device_string_properties (acc_device_t dev_type, int dev_num,
19 const char* expected_vendor,
20 const char* expected_name,
21 const char* expected_driver);
22
23 hsa_status_t (*hsa_agent_get_info_fn) (hsa_agent_t agent,
24 hsa_agent_info_t attribute,
25 void *value);
26 hsa_status_t (*hsa_system_get_info_fn) (hsa_system_info_t attribute,
27 void *value);
28 hsa_status_t (*hsa_iterate_agents_fn)
29 (hsa_status_t (*callback)(hsa_agent_t agent, void *data), void *data);
30 hsa_status_t (*hsa_init_fn) (void);
31
32 char* support_cpu_devices;
33
34 void
35 test_setup ()
36 {
37 char* env_runtime;
38 char* hsa_runtime_lib;
39 void *handle;
40
41 #define DLSYM_FN(function) \
42 function##_fn = (typeof(function##_fn))dlsym (handle, #function); \
43 if (function##_fn == NULL) \
44 { \
45 fprintf (stderr, "Could not get symbol " #function ".\n"); \
46 abort (); \
47 }
48
49 env_runtime = getenv ("HSA_RUNTIME_LIB");
50 hsa_runtime_lib = env_runtime ? env_runtime : (char*)"libhsa-runtime64.so";
51
52 handle = dlopen (hsa_runtime_lib, RTLD_LAZY);
53 if (!handle)
54 {
55 fprintf (stderr, "Could not load %s.\n", hsa_runtime_lib);
56 abort ();
57 }
58
59 DLSYM_FN (hsa_agent_get_info)
60 DLSYM_FN (hsa_system_get_info)
61 DLSYM_FN (hsa_iterate_agents)
62 DLSYM_FN (hsa_init)
63
64 hsa_init_fn ();
65
66 support_cpu_devices = getenv ("GCN_SUPPORT_CPU_DEVICES");
67 }
68
69 static hsa_status_t
70 check_agent_properties (hsa_agent_t agent, void *dev_num_arg)
71 {
72
73 char name[64];
74 char vendor_name[64];
75 uint16_t minor;
76 uint16_t major;
77 char driver[60];
78
79 hsa_status_t status;
80 hsa_device_type_t device_type;
81 int* dev_num = (int*)dev_num_arg;
82
83 #define AGENT_GET_INFO(info_type, val) \
84 status = hsa_agent_get_info_fn (agent, info_type, &val); \
85 if (status != HSA_STATUS_SUCCESS) \
86 { \
87 fprintf (stderr, "Failed to obtain " #info_type ".\n"); \
88 abort (); \
89 }
90 #define SYSTEM_GET_INFO(info_type, val) \
91 status = hsa_system_get_info_fn (info_type, &val); \
92 if (status != HSA_STATUS_SUCCESS) \
93 { \
94 fprintf (stderr, "Failed to obtain " #info_type ".\n"); \
95 abort (); \
96 }
97
98 AGENT_GET_INFO (HSA_AGENT_INFO_DEVICE, device_type)
99
100 /* Skip unsupported device types. Mimic the GCN plugin's behavior. */
101 if (!(device_type == HSA_DEVICE_TYPE_GPU
102 || (support_cpu_devices && device_type == HSA_DEVICE_TYPE_CPU)))
103 return HSA_STATUS_SUCCESS;
104
105 AGENT_GET_INFO (HSA_AGENT_INFO_NAME, name)
106 AGENT_GET_INFO (HSA_AGENT_INFO_VENDOR_NAME, vendor_name)
107
108 SYSTEM_GET_INFO (HSA_SYSTEM_INFO_VERSION_MINOR, minor)
109 SYSTEM_GET_INFO (HSA_SYSTEM_INFO_VERSION_MAJOR, major)
110
111 snprintf (driver, sizeof driver, "HSA Runtime %hu.%hu",
112 (unsigned short int)major, (unsigned short int)minor);
113
114 expect_device_string_properties(acc_device_radeon, *dev_num,
115 vendor_name, name, driver);
116
117 (*dev_num)++;
118
119 return status;
120 }
121
122 int
123 main ()
124 {
125 int dev_num = 0;
126 test_setup ();
127
128 hsa_status_t status =
129 hsa_iterate_agents_fn (&check_agent_properties, &dev_num);
130
131 return status;
132 }