1/* OpenACC Runtime Library: acc_device_host, acc_device_host_nonshm.
2
3   Copyright (C) 2013-2015 Free Software Foundation, Inc.
4
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
29/* Simple implementation of support routines for a shared-memory
30   acc_device_host, and a non-shared memory acc_device_host_nonshm, with the
31   latter built as a plugin.  */
32
33#include "openacc.h"
34#include "config.h"
35#ifdef HOST_NONSHM_PLUGIN
36#include "libgomp-plugin.h"
37#include "oacc-plugin.h"
38#else
39#include "libgomp.h"
40#include "oacc-int.h"
41#endif
42
43#include <stdint.h>
44#include <stdlib.h>
45#include <string.h>
46#include <stdio.h>
47#include <stdbool.h>
48
49#ifdef HOST_NONSHM_PLUGIN
50#define STATIC
51#define GOMP(X) GOMP_PLUGIN_##X
52#define SELF "host_nonshm plugin: "
53#else
54#define STATIC static
55#define GOMP(X) gomp_##X
56#define SELF "host: "
57#endif
58
59#ifdef HOST_NONSHM_PLUGIN
60#include "plugin-host.h"
61#endif
62
63STATIC const char *
64GOMP_OFFLOAD_get_name (void)
65{
66#ifdef HOST_NONSHM_PLUGIN
67  return "host_nonshm";
68#else
69  return "host";
70#endif
71}
72
73STATIC unsigned int
74GOMP_OFFLOAD_get_caps (void)
75{
76  unsigned int caps = (GOMP_OFFLOAD_CAP_OPENACC_200
77		       | GOMP_OFFLOAD_CAP_NATIVE_EXEC);
78
79#ifndef HOST_NONSHM_PLUGIN
80  caps |= GOMP_OFFLOAD_CAP_SHARED_MEM;
81#endif
82
83  return caps;
84}
85
86STATIC int
87GOMP_OFFLOAD_get_type (void)
88{
89#ifdef HOST_NONSHM_PLUGIN
90  return OFFLOAD_TARGET_TYPE_HOST_NONSHM;
91#else
92  return OFFLOAD_TARGET_TYPE_HOST;
93#endif
94}
95
96STATIC int
97GOMP_OFFLOAD_get_num_devices (void)
98{
99  return 1;
100}
101
102STATIC void
103GOMP_OFFLOAD_init_device (int n __attribute__ ((unused)))
104{
105}
106
107STATIC void
108GOMP_OFFLOAD_fini_device (int n __attribute__ ((unused)))
109{
110}
111
112STATIC int
113GOMP_OFFLOAD_load_image (int n __attribute__ ((unused)),
114			 void *i __attribute__ ((unused)),
115			 struct addr_pair **r __attribute__ ((unused)))
116{
117  return 0;
118}
119
120STATIC void
121GOMP_OFFLOAD_unload_image (int n __attribute__ ((unused)),
122			   void *i __attribute__ ((unused)))
123{
124}
125
126STATIC void *
127GOMP_OFFLOAD_alloc (int n __attribute__ ((unused)), size_t s)
128{
129  return GOMP (malloc) (s);
130}
131
132STATIC void
133GOMP_OFFLOAD_free (int n __attribute__ ((unused)), void *p)
134{
135  free (p);
136}
137
138STATIC void *
139GOMP_OFFLOAD_host2dev (int n __attribute__ ((unused)), void *d, const void *h,
140		       size_t s)
141{
142#ifdef HOST_NONSHM_PLUGIN
143  memcpy (d, h, s);
144#endif
145
146  return 0;
147}
148
149STATIC void *
150GOMP_OFFLOAD_dev2host (int n __attribute__ ((unused)), void *h, const void *d,
151		       size_t s)
152{
153#ifdef HOST_NONSHM_PLUGIN
154  memcpy (h, d, s);
155#endif
156
157  return 0;
158}
159
160STATIC void
161GOMP_OFFLOAD_run (int n __attribute__ ((unused)), void *fn_ptr, void *vars)
162{
163  void (*fn)(void *) = (void (*)(void *)) fn_ptr;
164
165  fn (vars);
166}
167
168STATIC void
169GOMP_OFFLOAD_openacc_parallel (void (*fn) (void *),
170			       size_t mapnum __attribute__ ((unused)),
171			       void **hostaddrs __attribute__ ((unused)),
172			       void **devaddrs __attribute__ ((unused)),
173			       size_t *sizes __attribute__ ((unused)),
174			       unsigned short *kinds __attribute__ ((unused)),
175			       int num_gangs __attribute__ ((unused)),
176			       int num_workers __attribute__ ((unused)),
177			       int vector_length __attribute__ ((unused)),
178			       int async __attribute__ ((unused)),
179			       void *targ_mem_desc __attribute__ ((unused)))
180{
181#ifdef HOST_NONSHM_PLUGIN
182  struct nonshm_thread *thd = GOMP_PLUGIN_acc_thread ();
183  thd->nonshm_exec = true;
184  fn (devaddrs);
185  thd->nonshm_exec = false;
186#else
187  fn (hostaddrs);
188#endif
189}
190
191STATIC void
192GOMP_OFFLOAD_openacc_register_async_cleanup (void *targ_mem_desc)
193{
194#ifdef HOST_NONSHM_PLUGIN
195  /* "Asynchronous" launches are executed synchronously on the (non-SHM) host,
196     so there's no point in delaying host-side cleanup -- just do it now.  */
197  GOMP_PLUGIN_async_unmap_vars (targ_mem_desc);
198#endif
199}
200
201STATIC void
202GOMP_OFFLOAD_openacc_async_set_async (int async __attribute__ ((unused)))
203{
204}
205
206STATIC int
207GOMP_OFFLOAD_openacc_async_test (int async __attribute__ ((unused)))
208{
209  return 1;
210}
211
212STATIC int
213GOMP_OFFLOAD_openacc_async_test_all (void)
214{
215  return 1;
216}
217
218STATIC void
219GOMP_OFFLOAD_openacc_async_wait (int async __attribute__ ((unused)))
220{
221}
222
223STATIC void
224GOMP_OFFLOAD_openacc_async_wait_all (void)
225{
226}
227
228STATIC void
229GOMP_OFFLOAD_openacc_async_wait_async (int async1 __attribute__ ((unused)),
230				       int async2 __attribute__ ((unused)))
231{
232}
233
234STATIC void
235GOMP_OFFLOAD_openacc_async_wait_all_async (int async __attribute__ ((unused)))
236{
237}
238
239STATIC void *
240GOMP_OFFLOAD_openacc_create_thread_data (int ord
241					 __attribute__ ((unused)))
242{
243#ifdef HOST_NONSHM_PLUGIN
244  struct nonshm_thread *thd
245    = GOMP_PLUGIN_malloc (sizeof (struct nonshm_thread));
246  thd->nonshm_exec = false;
247  return thd;
248#else
249  return NULL;
250#endif
251}
252
253STATIC void
254GOMP_OFFLOAD_openacc_destroy_thread_data (void *tls_data)
255{
256#ifdef HOST_NONSHM_PLUGIN
257  free (tls_data);
258#endif
259}
260