Debian Codegraph @

libgomp/testsuite/libgomp.c/interop-hsa.c

raw
1 /* { dg-additional-options "-ldl" } */
2 /* { dg-require-effective-target offload_device_gcn }
3 The 'asm' insert is valid for GCN only:
4 { dg-additional-options -foffload=amdgcn-amdhsa } */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <omp.h>
10 #include <assert.h>
11 #include <dlfcn.h>
12 #include "../../../include/hsa.h"
13 #include "../../config/gcn/libgomp-gcn.h"
14
15 #define STACKSIZE (100 * 1024)
16 #define HEAPSIZE (10 * 1024 * 1024)
17 #define ARENASIZE HEAPSIZE
18
19 /* This code fragment must be optimized or else the host-fallback kernel has
20 * invalid ASM inserts. The rest of the file can be compiled safely at -O0. */
21 #pragma omp declare target
22 uintptr_t __attribute__((optimize("O1")))
23 get_kernel_ptr ()
24 {
25 uintptr_t val;
26 if (!omp_is_initial_device ())
27 /* "main._omp_fn.0" is the name GCC gives the first OpenMP target
28 * region in the "main" function.
29 * The ".kd" suffix is added by the LLVM assembler when it creates the
30 * kernel meta-data, and this is what we need to launch a kernel. */
31 asm ("s_getpc_b64 %0\n\t"
32 "s_add_u32 %L0, %L0, main._omp_fn.0.kd@rel32@lo+4\n\t"
33 "s_addc_u32 %H0, %H0, main._omp_fn.0.kd@rel32@hi+4"
34 : "=Sg"(val));
35 return val;
36 }
37 #pragma omp end declare target
38
39 int
40 main(int argc, char** argv)
41 {
42
43 /* Load the HSA runtime DLL. */
44 void *hsalib = dlopen ("libhsa-runtime64.so.1", RTLD_LAZY);
45 assert (hsalib);
46
47 hsa_status_t (*hsa_signal_create) (hsa_signal_value_t initial_value,
48 uint32_t num_consumers,
49 const hsa_agent_t *consumers,
50 hsa_signal_t *signal)
51 = dlsym (hsalib, "hsa_signal_create");
52 assert (hsa_signal_create);
53
54 uint64_t (*hsa_queue_load_write_index_relaxed) (const hsa_queue_t *queue)
55 = dlsym (hsalib, "hsa_queue_load_write_index_relaxed");
56 assert (hsa_queue_load_write_index_relaxed);
57
58 void (*hsa_signal_store_relaxed) (hsa_signal_t signal,
59 hsa_signal_value_t value)
60 = dlsym (hsalib, "hsa_signal_store_relaxed");
61 assert (hsa_signal_store_relaxed);
62
63 hsa_signal_value_t (*hsa_signal_wait_relaxed) (hsa_signal_t signal,
64 hsa_signal_condition_t condition,
65 hsa_signal_value_t compare_value,
66 uint64_t timeout_hint,
67 hsa_wait_state_t wait_state_hint)
68 = dlsym (hsalib, "hsa_signal_wait_relaxed");
69 assert (hsa_signal_wait_relaxed);
70
71 void (*hsa_queue_store_write_index_relaxed) (const hsa_queue_t *queue,
72 uint64_t value)
73 = dlsym (hsalib, "hsa_queue_store_write_index_relaxed");
74 assert (hsa_queue_store_write_index_relaxed);
75
76 hsa_status_t (*hsa_signal_destroy) (hsa_signal_t signal)
77 = dlsym (hsalib, "hsa_signal_destroy");
78 assert (hsa_signal_destroy);
79
80 /* Set up the device data environment. */
81 int test_data_value = 0;
82 #pragma omp target enter data map(test_data_value)
83
84 /* Get the interop details. */
85 int device_num = omp_get_default_device();
86 hsa_agent_t *gpu_agent;
87 hsa_queue_t *hsa_queue = NULL;
88
89 omp_interop_t interop = omp_interop_none;
90 #pragma omp interop init(target, targetsync, prefer_type("hsa"): interop) device(device_num)
91 assert (interop != omp_interop_none);
92
93 omp_interop_rc_t retcode;
94 omp_interop_fr_t fr = omp_get_interop_int (interop, omp_ipr_fr_id, &retcode);
95 assert (retcode == omp_irc_success);
96 assert (fr == omp_ifr_hsa);
97
98 gpu_agent = omp_get_interop_ptr(interop, omp_ipr_device, &retcode);
99 assert (retcode == omp_irc_success);
100
101 hsa_queue = omp_get_interop_ptr(interop, omp_ipr_targetsync, &retcode);
102 assert (retcode == omp_irc_success);
103 assert (hsa_queue);
104
105 /* Call an offload kernel via OpenMP/libgomp.
106 *
107 * This kernel serves two purposes:
108 * 1) Lookup the device-side load-address of itself (thus avoiding the
109 * need to access the libgomp internals).
110 * 2) Count how many times it is called.
111 * We then call it once using OpenMP, and once manually, and check
112 * the counter reads "2". */
113 uint64_t kernel_object = 0;
114 #pragma omp target map(from:kernel_object) map(present,alloc:test_data_value)
115 {
116 kernel_object = get_kernel_ptr ();
117 ++test_data_value;
118 }
119
120 assert (kernel_object != 0);
121
122 /* Configure the same kernel to run again, using HSA manually this time. */
123 hsa_status_t status;
124 hsa_signal_t signal;
125 status = hsa_signal_create(1, 0, NULL, &signal);
126 assert (status == HSA_STATUS_SUCCESS);
127
128 /* The kernel is built by GCC for OpenMP, so we need to pass the same
129 * data pointers that libgomp would pass in. */
130 struct {
131 uintptr_t test_data_value;
132 uintptr_t kernel_object;
133 } tgtaddrs;
134
135 #pragma omp target data use_device_addr(test_data_value)
136 {
137 tgtaddrs.test_data_value = (uintptr_t)&test_data_value;
138 tgtaddrs.kernel_object = (uintptr_t)omp_target_alloc (8, device_num);
139 }
140
141 /* We also need to duplicate the launch ABI used by plugin-gcn.c. */
142 struct kernargs_abi args; /* From libgomp-gcn.h. */
143 args.dummy1 = (int64_t)&tgtaddrs;
144 args.out_ptr = (int64_t)malloc (sizeof (struct output)); /* Host side. */
145 args.heap_ptr = (int64_t)omp_target_alloc (HEAPSIZE, device_num);
146 args.arena_ptr = (int64_t)omp_target_alloc (ARENASIZE, device_num);
147 args.stack_ptr = (int64_t)omp_target_alloc (STACKSIZE, device_num);
148 args.arena_size_per_team = ARENASIZE;
149 args.stack_size_per_thread = STACKSIZE;
150
151 /* Build the HSA dispatch packet, and insert it into the queue. */
152 uint64_t packet_id = hsa_queue_load_write_index_relaxed (hsa_queue);
153 const uint32_t queueMask = hsa_queue->size - 1;
154 hsa_kernel_dispatch_packet_t *dispatch_packet =
155 &(((hsa_kernel_dispatch_packet_t *)
156 (hsa_queue->base_address))[packet_id & queueMask]);
157
158 dispatch_packet->setup = 3 << HSA_KERNEL_DISPATCH_PACKET_SETUP_DIMENSIONS;
159 dispatch_packet->workgroup_size_x = 1;
160 dispatch_packet->workgroup_size_y = 64;
161 dispatch_packet->workgroup_size_z = 1;
162 dispatch_packet->grid_size_x = 1;
163 dispatch_packet->grid_size_y = 64;
164 dispatch_packet->grid_size_z = 1;
165 dispatch_packet->completion_signal = signal;
166 dispatch_packet->kernel_object = kernel_object;
167 dispatch_packet->kernarg_address = &args;
168 dispatch_packet->private_segment_size = 0;
169 dispatch_packet->group_segment_size = 1536;
170
171 uint16_t header = 0;
172 header |= HSA_PACKET_TYPE_KERNEL_DISPATCH << HSA_PACKET_HEADER_TYPE;
173 header |= HSA_FENCE_SCOPE_SYSTEM << HSA_PACKET_HEADER_ACQUIRE_FENCE_SCOPE;
174 header |= HSA_FENCE_SCOPE_SYSTEM << HSA_PACKET_HEADER_RELEASE_FENCE_SCOPE;
175
176 /* Finish writing the packet header with an atomic release. */
177 __atomic_store_n((uint16_t*)dispatch_packet, header, __ATOMIC_RELEASE);
178
179 hsa_queue_store_write_index_relaxed (hsa_queue, packet_id + 1);
180
181 ;/* Run the kernel and wait for it to complete. */
182 hsa_signal_store_relaxed(hsa_queue->doorbell_signal, packet_id);
183 while (hsa_signal_wait_relaxed(signal, HSA_SIGNAL_CONDITION_LT, 1,
184 UINT64_MAX, HSA_WAIT_STATE_ACTIVE) != 0)
185 ;
186
187 /* Clean up HSA. */
188 hsa_signal_destroy(signal);
189 free ((void*)args.out_ptr);
190 omp_target_free ((void*)args.heap_ptr, device_num);
191 omp_target_free ((void*)args.arena_ptr, device_num);
192 omp_target_free ((void*)args.stack_ptr, device_num);
193 omp_target_free ((void*)tgtaddrs.kernel_object, device_num);
194
195 /* Clean up OpenMP. */
196 #pragma omp interop destroy(interop)
197
198 /* Bring the data back from the device. */
199 #pragma omp target exit data map(test_data_value)
200
201 /* Ensure the kernel was called twice. Once by OpenMP, once by HSA. */
202 assert (test_data_value == 2);
203
204 return 0;
205 }