blob: f3bbd2b9c61324eb8688589fffa4ad6f8332a6af [file] [log] [blame]
Thomas Schwinge41dbbb32015-01-15 21:11:12 +01001/* OpenACC Runtime Library: acc_device_host.
2
Thomas Koenigb18a97e2021-09-13 19:49:49 +02003 Copyright (C) 2013-2021 Free Software Foundation, Inc.
Thomas Schwinge41dbbb32015-01-15 21:11:12 +01004
5 Contributed by Mentor Embedded.
6
7 This file is part of the GNU Offloading and Multi Processing Library
8 (libgomp).
9
10 Libgomp is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 more details.
19
20 Under Section 7 of GPL version 3, you are granted additional
21 permissions described in the GCC Runtime Library Exception, version
22 3.1, as published by the Free Software Foundation.
23
24 You should have received a copy of the GNU General Public License and
25 a copy of the GCC Runtime Library Exception along with this program;
26 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
27 <http://www.gnu.org/licenses/>. */
28
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020029#include "libgomp.h"
30#include "oacc-int.h"
Nathan Sidwell2a21ff12015-08-24 17:10:06 +000031#include "gomp-constants.h"
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020032
33#include <stdbool.h>
34#include <stddef.h>
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020035
36static struct gomp_device_descr host_dispatch;
37
38static const char *
39host_get_name (void)
40{
41 return host_dispatch.name;
42}
43
44static unsigned int
45host_get_caps (void)
46{
47 return host_dispatch.capabilities;
48}
49
50static int
51host_get_type (void)
52{
53 return host_dispatch.type;
54}
55
56static int
57host_get_num_devices (void)
58{
59 return 1;
60}
61
Chung-Lin Tang6ce13072016-05-26 09:58:56 +000062static bool
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020063host_init_device (int n __attribute__ ((unused)))
64{
Chung-Lin Tang6ce13072016-05-26 09:58:56 +000065 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020066}
67
Chung-Lin Tang6ce13072016-05-26 09:58:56 +000068static bool
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020069host_fini_device (int n __attribute__ ((unused)))
70{
Chung-Lin Tang6ce13072016-05-26 09:58:56 +000071 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020072}
73
Nathan Sidwell2a21ff12015-08-24 17:10:06 +000074static unsigned
75host_version (void)
76{
77 return GOMP_VERSION;
78}
79
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020080static int
81host_load_image (int n __attribute__ ((unused)),
Nathan Sidwell2a21ff12015-08-24 17:10:06 +000082 unsigned v __attribute__ ((unused)),
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020083 const void *t __attribute__ ((unused)),
84 struct addr_pair **r __attribute__ ((unused)))
85{
86 return 0;
87}
88
Chung-Lin Tang6ce13072016-05-26 09:58:56 +000089static bool
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020090host_unload_image (int n __attribute__ ((unused)),
Nathan Sidwell2a21ff12015-08-24 17:10:06 +000091 unsigned v __attribute__ ((unused)),
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020092 const void *t __attribute__ ((unused)))
93{
Chung-Lin Tang6ce13072016-05-26 09:58:56 +000094 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +020095}
96
97static void *
98host_alloc (int n __attribute__ ((unused)), size_t s)
99{
100 return gomp_malloc (s);
101}
102
Chung-Lin Tang6ce13072016-05-26 09:58:56 +0000103static bool
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200104host_free (int n __attribute__ ((unused)), void *p)
105{
106 free (p);
Chung-Lin Tang6ce13072016-05-26 09:58:56 +0000107 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200108}
109
Chung-Lin Tang6ce13072016-05-26 09:58:56 +0000110static bool
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200111host_dev2host (int n __attribute__ ((unused)),
112 void *h __attribute__ ((unused)),
113 const void *d __attribute__ ((unused)),
114 size_t s __attribute__ ((unused)))
115{
Chung-Lin Tang6ce13072016-05-26 09:58:56 +0000116 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200117}
118
Chung-Lin Tang6ce13072016-05-26 09:58:56 +0000119static bool
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200120host_host2dev (int n __attribute__ ((unused)),
121 void *d __attribute__ ((unused)),
122 const void *h __attribute__ ((unused)),
123 size_t s __attribute__ ((unused)))
124{
Chung-Lin Tang6ce13072016-05-26 09:58:56 +0000125 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200126}
127
128static void
Martin Jamborb2b40052016-01-19 11:35:10 +0100129host_run (int n __attribute__ ((unused)), void *fn_ptr, void *vars,
130 void **args __attribute__((unused)))
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200131{
132 void (*fn)(void *) = (void (*)(void *)) fn_ptr;
133
134 fn (vars);
135}
136
137static void
138host_openacc_exec (void (*fn) (void *),
139 size_t mapnum __attribute__ ((unused)),
140 void **hostaddrs,
141 void **devaddrs __attribute__ ((unused)),
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000142 unsigned *dims __attribute__ ((unused)),
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200143 void *targ_mem_desc __attribute__ ((unused)))
144{
145 fn (hostaddrs);
146}
147
148static void
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000149host_openacc_async_exec (void (*fn) (void *),
150 size_t mapnum __attribute__ ((unused)),
151 void **hostaddrs,
152 void **devaddrs __attribute__ ((unused)),
153 unsigned *dims __attribute__ ((unused)),
154 void *targ_mem_desc __attribute__ ((unused)),
155 struct goacc_asyncqueue *aq __attribute__ ((unused)))
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200156{
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000157 fn (hostaddrs);
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200158}
159
160static int
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000161host_openacc_async_test (struct goacc_asyncqueue *aq __attribute__ ((unused)))
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200162{
163 return 1;
164}
165
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000166static bool
167host_openacc_async_synchronize (struct goacc_asyncqueue *aq
168 __attribute__ ((unused)))
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200169{
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000170 return true;
171}
172
173static bool
174host_openacc_async_serialize (struct goacc_asyncqueue *aq1
175 __attribute__ ((unused)),
176 struct goacc_asyncqueue *aq2
177 __attribute__ ((unused)))
178{
179 return true;
180}
181
182static bool
183host_openacc_async_host2dev (int ord __attribute__ ((unused)),
184 void *dst __attribute__ ((unused)),
185 const void *src __attribute__ ((unused)),
186 size_t n __attribute__ ((unused)),
187 struct goacc_asyncqueue *aq
188 __attribute__ ((unused)))
189{
190 return true;
191}
192
193static bool
194host_openacc_async_dev2host (int ord __attribute__ ((unused)),
195 void *dst __attribute__ ((unused)),
196 const void *src __attribute__ ((unused)),
197 size_t n __attribute__ ((unused)),
198 struct goacc_asyncqueue *aq
199 __attribute__ ((unused)))
200{
201 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200202}
203
204static void
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000205host_openacc_async_queue_callback (struct goacc_asyncqueue *aq
206 __attribute__ ((unused)),
207 void (*callback_fn)(void *)
208 __attribute__ ((unused)),
209 void *userptr __attribute__ ((unused)))
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200210{
211}
212
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000213static struct goacc_asyncqueue *
Andrew Stubbsd2903ce02019-11-13 12:37:59 +0000214host_openacc_async_construct (int device __attribute__((unused)))
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200215{
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000216 /* Non-NULL 0xffff... value as opaque dummy. */
217 return (struct goacc_asyncqueue *) -1;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200218}
219
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000220static bool
221host_openacc_async_destruct (struct goacc_asyncqueue *aq
222 __attribute__ ((unused)))
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200223{
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000224 return true;
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200225}
226
Thomas Schwinge6fc03852020-01-10 23:24:36 +0100227static union goacc_property_value
228host_openacc_get_property (int n, enum goacc_property prop)
229{
230 union goacc_property_value nullval = { .val = 0 };
231
232 if (n >= host_get_num_devices ())
233 return nullval;
234
235 switch (prop)
236 {
237 case GOACC_PROPERTY_NAME:
238 return (union goacc_property_value) { .ptr = "GOMP" };
239 case GOACC_PROPERTY_VENDOR:
240 return (union goacc_property_value) { .ptr = "GNU" };
241 case GOACC_PROPERTY_DRIVER:
242 return (union goacc_property_value) { .ptr = VERSION };
243 case GOACC_PROPERTY_MEMORY:
244 case GOACC_PROPERTY_FREE_MEMORY:
245 default:
246 return nullval;
247 }
248}
249
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200250static void *
251host_openacc_create_thread_data (int ord __attribute__ ((unused)))
252{
253 return NULL;
254}
255
256static void
257host_openacc_destroy_thread_data (void *tls_data __attribute__ ((unused)))
258{
259}
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100260
261static struct gomp_device_descr host_dispatch =
262 {
263 .name = "host",
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200264 .capabilities = (GOMP_OFFLOAD_CAP_SHARED_MEM
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100265 | GOMP_OFFLOAD_CAP_NATIVE_EXEC
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200266 | GOMP_OFFLOAD_CAP_OPENACC_200),
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100267 .target_id = 0,
268 .type = OFFLOAD_TARGET_TYPE_HOST,
269
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200270 .get_name_func = host_get_name,
271 .get_caps_func = host_get_caps,
272 .get_type_func = host_get_type,
273 .get_num_devices_func = host_get_num_devices,
274 .init_device_func = host_init_device,
275 .fini_device_func = host_fini_device,
Nathan Sidwell2a21ff12015-08-24 17:10:06 +0000276 .version_func = host_version,
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200277 .load_image_func = host_load_image,
278 .unload_image_func = host_unload_image,
279 .alloc_func = host_alloc,
280 .free_func = host_free,
281 .dev2host_func = host_dev2host,
282 .host2dev_func = host_host2dev,
283 .run_func = host_run,
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100284
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200285 .mem_map = { NULL },
Tobias Burnus93d90212019-12-11 12:45:49 +0100286 /* .lock initialized in goacc_host_init. */
Ilya Verbind84ffc02015-12-14 16:46:54 +0000287 .state = GOMP_DEVICE_UNINITIALIZED,
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100288
289 .openacc = {
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200290 .exec_func = host_openacc_exec,
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100291
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200292 .create_thread_data_func = host_openacc_create_thread_data,
293 .destroy_thread_data_func = host_openacc_destroy_thread_data,
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100294
Chung-Lin Tang1f4c5b92019-05-13 13:32:00 +0000295 .async = {
296 .construct_func = host_openacc_async_construct,
297 .destruct_func = host_openacc_async_destruct,
298 .test_func = host_openacc_async_test,
299 .synchronize_func = host_openacc_async_synchronize,
300 .serialize_func = host_openacc_async_serialize,
301 .queue_callback_func = host_openacc_async_queue_callback,
302 .exec_func = host_openacc_async_exec,
303 .dev2host_func = host_openacc_async_dev2host,
304 .host2dev_func = host_openacc_async_host2dev,
305 },
306
Thomas Schwinge6fc03852020-01-10 23:24:36 +0100307 .get_property_func = host_openacc_get_property,
308
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100309 .cuda = {
310 .get_current_device_func = NULL,
311 .get_current_context_func = NULL,
312 .get_stream_func = NULL,
313 .set_stream_func = NULL,
314 }
315 }
316 };
317
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200318/* Initialize and register this device type. */
Chung-Lin Tang6bb4c3e2015-09-22 06:45:22 +0000319void
Thomas Schwingeb97e78b2015-08-10 18:48:26 +0200320goacc_host_init (void)
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100321{
Thomas Schwinge41dbbb32015-01-15 21:11:12 +0100322 gomp_mutex_init (&host_dispatch.lock);
323 goacc_register (&host_dispatch);
324}