annotate liboffloadmic/plugin/libgomp-plugin-intelmic.cpp @ 118:fd00160c1b76

ifdef TARGET_64BIT
author mir3636
date Tue, 27 Feb 2018 15:01:35 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Plugin for offload execution on Intel MIC devices.
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 Copyright (C) 2014-2016 Free Software Foundation, Inc.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 Contributed by Ilya Verbin <ilya.verbin@intel.com>.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 This file is part of the GNU Offloading and Multi Processing Library
kono
parents:
diff changeset
8 (libgomp).
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 Libgomp is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
11 under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
12 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
13 any later version.
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
kono
parents:
diff changeset
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kono
parents:
diff changeset
18 more details.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
21 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
22 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
25 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
26 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
27 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 /* Host side part of a libgomp plugin. */
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #include <stdint.h>
kono
parents:
diff changeset
32 #include <stdio.h>
kono
parents:
diff changeset
33 #include <stdlib.h>
kono
parents:
diff changeset
34 #include <string.h>
kono
parents:
diff changeset
35 #include <utility>
kono
parents:
diff changeset
36 #include <vector>
kono
parents:
diff changeset
37 #include <map>
kono
parents:
diff changeset
38 #include "libgomp-plugin.h"
kono
parents:
diff changeset
39 #include "compiler_if_host.h"
kono
parents:
diff changeset
40 #include "main_target_image.h"
kono
parents:
diff changeset
41 #include "gomp-constants.h"
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 #define OFFLOAD_ACTIVE_WAIT_ENV "OFFLOAD_ACTIVE_WAIT"
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 #ifdef DEBUG
kono
parents:
diff changeset
46 #define TRACE(...) \
kono
parents:
diff changeset
47 { \
kono
parents:
diff changeset
48 fprintf (stderr, "HOST:\t%s:%s ", __FILE__, __FUNCTION__); \
kono
parents:
diff changeset
49 fprintf (stderr, __VA_ARGS__); \
kono
parents:
diff changeset
50 fprintf (stderr, "\n"); \
kono
parents:
diff changeset
51 }
kono
parents:
diff changeset
52 #else
kono
parents:
diff changeset
53 #define TRACE { }
kono
parents:
diff changeset
54 #endif
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 /* Start/end addresses of functions and global variables on a device. */
kono
parents:
diff changeset
58 typedef std::vector<addr_pair> AddrVect;
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 /* Addresses for one image and all devices. */
kono
parents:
diff changeset
61 typedef std::vector<AddrVect> DevAddrVect;
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 /* Addresses for all images and all devices. */
kono
parents:
diff changeset
64 typedef std::map<const void *, DevAddrVect> ImgDevAddrMap;
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 /* Image descriptor needed by __offload_[un]register_image. */
kono
parents:
diff changeset
67 struct TargetImageDesc {
kono
parents:
diff changeset
68 int64_t size;
kono
parents:
diff changeset
69 /* 10 characters is enough for max int value. */
kono
parents:
diff changeset
70 char name[sizeof ("lib0000000000.so")];
kono
parents:
diff changeset
71 char data[];
kono
parents:
diff changeset
72 };
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 /* Image descriptors, indexed by a pointer obtained from libgomp. */
kono
parents:
diff changeset
75 typedef std::map<const void *, TargetImageDesc *> ImgDescMap;
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 /* Total number of available devices. */
kono
parents:
diff changeset
79 static int num_devices;
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 /* Total number of shared libraries with offloading to Intel MIC. */
kono
parents:
diff changeset
82 static int num_images;
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 /* Two dimensional array: one key is a pointer to image,
kono
parents:
diff changeset
85 second key is number of device. Contains a vector of pointer pairs. */
kono
parents:
diff changeset
86 static ImgDevAddrMap *address_table;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 /* Descriptors of all images, registered in liboffloadmic. */
kono
parents:
diff changeset
89 static ImgDescMap *image_descriptors;
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 /* Thread-safe registration of the main image. */
kono
parents:
diff changeset
92 static pthread_once_t main_image_is_registered = PTHREAD_ONCE_INIT;
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 static VarDesc vd_host2tgt = {
kono
parents:
diff changeset
95 { 1, 1 }, /* dst, src */
kono
parents:
diff changeset
96 { 1, 0 }, /* in, out */
kono
parents:
diff changeset
97 1, /* alloc_if */
kono
parents:
diff changeset
98 1, /* free_if */
kono
parents:
diff changeset
99 4, /* align */
kono
parents:
diff changeset
100 0, /* mic_offset */
kono
parents:
diff changeset
101 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* is_static, is_static_dstn, has_length,
kono
parents:
diff changeset
102 is_stack_buf, sink_addr, alloc_disp,
kono
parents:
diff changeset
103 is_noncont_src, is_noncont_dst */
kono
parents:
diff changeset
104 0, /* offset */
kono
parents:
diff changeset
105 0, /* size */
kono
parents:
diff changeset
106 1, /* count */
kono
parents:
diff changeset
107 0, /* alloc */
kono
parents:
diff changeset
108 0, /* into */
kono
parents:
diff changeset
109 0 /* ptr */
kono
parents:
diff changeset
110 };
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 static VarDesc vd_tgt2host = {
kono
parents:
diff changeset
113 { 1, 1 }, /* dst, src */
kono
parents:
diff changeset
114 { 0, 1 }, /* in, out */
kono
parents:
diff changeset
115 1, /* alloc_if */
kono
parents:
diff changeset
116 1, /* free_if */
kono
parents:
diff changeset
117 4, /* align */
kono
parents:
diff changeset
118 0, /* mic_offset */
kono
parents:
diff changeset
119 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* is_static, is_static_dstn, has_length,
kono
parents:
diff changeset
120 is_stack_buf, sink_addr, alloc_disp,
kono
parents:
diff changeset
121 is_noncont_src, is_noncont_dst */
kono
parents:
diff changeset
122 0, /* offset */
kono
parents:
diff changeset
123 0, /* size */
kono
parents:
diff changeset
124 1, /* count */
kono
parents:
diff changeset
125 0, /* alloc */
kono
parents:
diff changeset
126 0, /* into */
kono
parents:
diff changeset
127 0 /* ptr */
kono
parents:
diff changeset
128 };
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 __attribute__((constructor))
kono
parents:
diff changeset
132 static void
kono
parents:
diff changeset
133 init (void)
kono
parents:
diff changeset
134 {
kono
parents:
diff changeset
135 const char *active_wait = getenv (OFFLOAD_ACTIVE_WAIT_ENV);
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 /* Disable active wait by default to avoid useless CPU usage. */
kono
parents:
diff changeset
138 if (!active_wait)
kono
parents:
diff changeset
139 setenv (OFFLOAD_ACTIVE_WAIT_ENV, "0", 0);
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 address_table = new ImgDevAddrMap;
kono
parents:
diff changeset
142 image_descriptors = new ImgDescMap;
kono
parents:
diff changeset
143 num_devices = _Offload_number_of_devices ();
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 extern "C" const char *
kono
parents:
diff changeset
147 GOMP_OFFLOAD_get_name (void)
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 const char *res = "intelmic";
kono
parents:
diff changeset
150 TRACE ("(): return %s", res);
kono
parents:
diff changeset
151 return res;
kono
parents:
diff changeset
152 }
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 extern "C" unsigned int
kono
parents:
diff changeset
155 GOMP_OFFLOAD_get_caps (void)
kono
parents:
diff changeset
156 {
kono
parents:
diff changeset
157 unsigned int res = GOMP_OFFLOAD_CAP_OPENMP_400;
kono
parents:
diff changeset
158 TRACE ("(): return %x", res);
kono
parents:
diff changeset
159 return res;
kono
parents:
diff changeset
160 }
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 extern "C" int
kono
parents:
diff changeset
163 GOMP_OFFLOAD_get_type (void)
kono
parents:
diff changeset
164 {
kono
parents:
diff changeset
165 enum offload_target_type res = OFFLOAD_TARGET_TYPE_INTEL_MIC;
kono
parents:
diff changeset
166 TRACE ("(): return %d", res);
kono
parents:
diff changeset
167 return res;
kono
parents:
diff changeset
168 }
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 extern "C" int
kono
parents:
diff changeset
171 GOMP_OFFLOAD_get_num_devices (void)
kono
parents:
diff changeset
172 {
kono
parents:
diff changeset
173 TRACE ("(): return %d", num_devices);
kono
parents:
diff changeset
174 return num_devices;
kono
parents:
diff changeset
175 }
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 static bool
kono
parents:
diff changeset
178 offload (const char *file, uint64_t line, int device, const char *name,
kono
parents:
diff changeset
179 int num_vars, VarDesc *vars, const void **async_data)
kono
parents:
diff changeset
180 {
kono
parents:
diff changeset
181 OFFLOAD ofld = __offload_target_acquire1 (&device, file, line);
kono
parents:
diff changeset
182 if (ofld)
kono
parents:
diff changeset
183 {
kono
parents:
diff changeset
184 if (async_data == NULL)
kono
parents:
diff changeset
185 return __offload_offload1 (ofld, name, 0, num_vars, vars, NULL, 0,
kono
parents:
diff changeset
186 NULL, NULL);
kono
parents:
diff changeset
187 else
kono
parents:
diff changeset
188 {
kono
parents:
diff changeset
189 OffloadFlags flags;
kono
parents:
diff changeset
190 flags.flags = 0;
kono
parents:
diff changeset
191 flags.bits.omp_async = 1;
kono
parents:
diff changeset
192 return __offload_offload3 (ofld, name, 0, num_vars, vars, NULL, 0,
kono
parents:
diff changeset
193 NULL, async_data, 0, NULL, flags, NULL);
kono
parents:
diff changeset
194 }
kono
parents:
diff changeset
195 }
kono
parents:
diff changeset
196 else
kono
parents:
diff changeset
197 {
kono
parents:
diff changeset
198 GOMP_PLUGIN_error ("%s:%d: Offload target acquire failed\n", file, line);
kono
parents:
diff changeset
199 return false;
kono
parents:
diff changeset
200 }
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 static void
kono
parents:
diff changeset
204 register_main_image ()
kono
parents:
diff changeset
205 {
kono
parents:
diff changeset
206 /* Do not check the return value, because old versions of liboffloadmic did
kono
parents:
diff changeset
207 not have return values. */
kono
parents:
diff changeset
208 __offload_register_image (&main_target_image);
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 /* liboffloadmic will call GOMP_PLUGIN_target_task_completion when
kono
parents:
diff changeset
211 asynchronous task on target is completed. */
kono
parents:
diff changeset
212 __offload_register_task_callback (GOMP_PLUGIN_target_task_completion);
kono
parents:
diff changeset
213 }
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 /* liboffloadmic loads and runs offload_target_main on all available devices
kono
parents:
diff changeset
216 during a first call to offload (). */
kono
parents:
diff changeset
217 extern "C" bool
kono
parents:
diff changeset
218 GOMP_OFFLOAD_init_device (int device)
kono
parents:
diff changeset
219 {
kono
parents:
diff changeset
220 TRACE ("(device = %d)", device);
kono
parents:
diff changeset
221 pthread_once (&main_image_is_registered, register_main_image);
kono
parents:
diff changeset
222 return offload (__FILE__, __LINE__, device, "__offload_target_init_proc", 0,
kono
parents:
diff changeset
223 NULL, NULL);
kono
parents:
diff changeset
224 }
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 extern "C" bool
kono
parents:
diff changeset
227 GOMP_OFFLOAD_fini_device (int device)
kono
parents:
diff changeset
228 {
kono
parents:
diff changeset
229 TRACE ("(device = %d)", device);
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 /* liboffloadmic will finalize target processes on all available devices. */
kono
parents:
diff changeset
232 __offload_unregister_image (&main_target_image);
kono
parents:
diff changeset
233 return true;
kono
parents:
diff changeset
234 }
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 static bool
kono
parents:
diff changeset
237 get_target_table (int device, int &num_funcs, int &num_vars, void **&table)
kono
parents:
diff changeset
238 {
kono
parents:
diff changeset
239 VarDesc vd1[2] = { vd_tgt2host, vd_tgt2host };
kono
parents:
diff changeset
240 vd1[0].ptr = &num_funcs;
kono
parents:
diff changeset
241 vd1[0].size = sizeof (num_funcs);
kono
parents:
diff changeset
242 vd1[1].ptr = &num_vars;
kono
parents:
diff changeset
243 vd1[1].size = sizeof (num_vars);
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 if (!offload (__FILE__, __LINE__, device, "__offload_target_table_p1", 2,
kono
parents:
diff changeset
246 vd1, NULL))
kono
parents:
diff changeset
247 return false;
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 int table_size = num_funcs + 2 * num_vars;
kono
parents:
diff changeset
250 if (table_size > 0)
kono
parents:
diff changeset
251 {
kono
parents:
diff changeset
252 table = new void * [table_size];
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 VarDesc vd2;
kono
parents:
diff changeset
255 vd2 = vd_tgt2host;
kono
parents:
diff changeset
256 vd2.ptr = table;
kono
parents:
diff changeset
257 vd2.size = table_size * sizeof (void *);
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 return offload (__FILE__, __LINE__, device, "__offload_target_table_p2",
kono
parents:
diff changeset
260 1, &vd2, NULL);
kono
parents:
diff changeset
261 }
kono
parents:
diff changeset
262 return true;
kono
parents:
diff changeset
263 }
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 /* Offload TARGET_IMAGE to all available devices and fill address_table with
kono
parents:
diff changeset
266 corresponding target addresses. */
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 static bool
kono
parents:
diff changeset
269 offload_image (const void *target_image)
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 void *image_start = ((void **) target_image)[0];
kono
parents:
diff changeset
272 void *image_end = ((void **) target_image)[1];
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 TRACE ("(target_image = %p { %p, %p })",
kono
parents:
diff changeset
275 target_image, image_start, image_end);
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 int64_t image_size = (uintptr_t) image_end - (uintptr_t) image_start;
kono
parents:
diff changeset
278 TargetImageDesc *image = (TargetImageDesc *) malloc (offsetof (TargetImageDesc, data)
kono
parents:
diff changeset
279 + image_size);
kono
parents:
diff changeset
280 if (!image)
kono
parents:
diff changeset
281 {
kono
parents:
diff changeset
282 GOMP_PLUGIN_error ("%s: Can't allocate memory\n", __FILE__);
kono
parents:
diff changeset
283 return false;
kono
parents:
diff changeset
284 }
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 image->size = image_size;
kono
parents:
diff changeset
287 sprintf (image->name, "lib%010d.so", num_images++);
kono
parents:
diff changeset
288 memcpy (image->data, image_start, image->size);
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 TRACE ("() __offload_register_image %s { %p, %d }",
kono
parents:
diff changeset
291 image->name, image_start, image->size);
kono
parents:
diff changeset
292 /* Do not check the return value, because old versions of liboffloadmic did
kono
parents:
diff changeset
293 not have return values. */
kono
parents:
diff changeset
294 __offload_register_image (image);
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 /* Receive tables for target_image from all devices. */
kono
parents:
diff changeset
297 DevAddrVect dev_table;
kono
parents:
diff changeset
298 bool ret = true;
kono
parents:
diff changeset
299 for (int dev = 0; dev < num_devices; dev++)
kono
parents:
diff changeset
300 {
kono
parents:
diff changeset
301 int num_funcs = 0;
kono
parents:
diff changeset
302 int num_vars = 0;
kono
parents:
diff changeset
303 void **table = NULL;
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 ret &= get_target_table (dev, num_funcs, num_vars, table);
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 AddrVect curr_dev_table;
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 for (int i = 0; i < num_funcs; i++)
kono
parents:
diff changeset
310 {
kono
parents:
diff changeset
311 addr_pair tgt_addr;
kono
parents:
diff changeset
312 tgt_addr.start = (uintptr_t) table[i];
kono
parents:
diff changeset
313 tgt_addr.end = tgt_addr.start + 1;
kono
parents:
diff changeset
314 TRACE ("() func %d:\t0x%llx..0x%llx", i,
kono
parents:
diff changeset
315 tgt_addr.start, tgt_addr.end);
kono
parents:
diff changeset
316 curr_dev_table.push_back (tgt_addr);
kono
parents:
diff changeset
317 }
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 for (int i = 0; i < num_vars; i++)
kono
parents:
diff changeset
320 {
kono
parents:
diff changeset
321 addr_pair tgt_addr;
kono
parents:
diff changeset
322 tgt_addr.start = (uintptr_t) table[num_funcs+i*2];
kono
parents:
diff changeset
323 tgt_addr.end = tgt_addr.start + (uintptr_t) table[num_funcs+i*2+1];
kono
parents:
diff changeset
324 TRACE ("() var %d:\t0x%llx..0x%llx", i, tgt_addr.start, tgt_addr.end);
kono
parents:
diff changeset
325 curr_dev_table.push_back (tgt_addr);
kono
parents:
diff changeset
326 }
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 dev_table.push_back (curr_dev_table);
kono
parents:
diff changeset
329 delete [] table;
kono
parents:
diff changeset
330 }
kono
parents:
diff changeset
331
kono
parents:
diff changeset
332 address_table->insert (std::make_pair (target_image, dev_table));
kono
parents:
diff changeset
333 image_descriptors->insert (std::make_pair (target_image, image));
kono
parents:
diff changeset
334 return ret;
kono
parents:
diff changeset
335 }
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 /* Return the libgomp version number we're compatible with. There is
kono
parents:
diff changeset
338 no requirement for cross-version compatibility. */
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 extern "C" unsigned
kono
parents:
diff changeset
341 GOMP_OFFLOAD_version (void)
kono
parents:
diff changeset
342 {
kono
parents:
diff changeset
343 return GOMP_VERSION;
kono
parents:
diff changeset
344 }
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 extern "C" int
kono
parents:
diff changeset
347 GOMP_OFFLOAD_load_image (int device, const unsigned version,
kono
parents:
diff changeset
348 const void *target_image, addr_pair **result)
kono
parents:
diff changeset
349 {
kono
parents:
diff changeset
350 TRACE ("(device = %d, target_image = %p)", device, target_image);
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 if (GOMP_VERSION_DEV (version) > GOMP_VERSION_INTEL_MIC)
kono
parents:
diff changeset
353 {
kono
parents:
diff changeset
354 GOMP_PLUGIN_error ("Offload data incompatible with intelmic plugin"
kono
parents:
diff changeset
355 " (expected %u, received %u)",
kono
parents:
diff changeset
356 GOMP_VERSION_INTEL_MIC, GOMP_VERSION_DEV (version));
kono
parents:
diff changeset
357 return -1;
kono
parents:
diff changeset
358 }
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 /* If target_image is already present in address_table, then there is no need
kono
parents:
diff changeset
361 to offload it. */
kono
parents:
diff changeset
362 if (address_table->count (target_image) == 0)
kono
parents:
diff changeset
363 {
kono
parents:
diff changeset
364 /* If fail, return -1 as error code. */
kono
parents:
diff changeset
365 if (!offload_image (target_image))
kono
parents:
diff changeset
366 return -1;
kono
parents:
diff changeset
367 }
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 AddrVect *curr_dev_table = &(*address_table)[target_image][device];
kono
parents:
diff changeset
370 int table_size = curr_dev_table->size ();
kono
parents:
diff changeset
371 addr_pair *table = (addr_pair *) malloc (table_size * sizeof (addr_pair));
kono
parents:
diff changeset
372 if (table == NULL)
kono
parents:
diff changeset
373 {
kono
parents:
diff changeset
374 GOMP_PLUGIN_error ("%s: Can't allocate memory\n", __FILE__);
kono
parents:
diff changeset
375 return -1;
kono
parents:
diff changeset
376 }
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 std::copy (curr_dev_table->begin (), curr_dev_table->end (), table);
kono
parents:
diff changeset
379 *result = table;
kono
parents:
diff changeset
380 return table_size;
kono
parents:
diff changeset
381 }
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 extern "C" bool
kono
parents:
diff changeset
384 GOMP_OFFLOAD_unload_image (int device, unsigned version,
kono
parents:
diff changeset
385 const void *target_image)
kono
parents:
diff changeset
386 {
kono
parents:
diff changeset
387 if (GOMP_VERSION_DEV (version) > GOMP_VERSION_INTEL_MIC)
kono
parents:
diff changeset
388 {
kono
parents:
diff changeset
389 GOMP_PLUGIN_error ("Offload data incompatible with intelmic plugin"
kono
parents:
diff changeset
390 " (expected %u, received %u)",
kono
parents:
diff changeset
391 GOMP_VERSION_INTEL_MIC, GOMP_VERSION_DEV (version));
kono
parents:
diff changeset
392 return false;
kono
parents:
diff changeset
393 }
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 TRACE ("(device = %d, target_image = %p)", device, target_image);
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 /* liboffloadmic unloads the image from all available devices. */
kono
parents:
diff changeset
398 if (image_descriptors->count (target_image) > 0)
kono
parents:
diff changeset
399 {
kono
parents:
diff changeset
400 TargetImageDesc *image_desc = (*image_descriptors)[target_image];
kono
parents:
diff changeset
401 __offload_unregister_image (image_desc);
kono
parents:
diff changeset
402 free (image_desc);
kono
parents:
diff changeset
403
kono
parents:
diff changeset
404 address_table->erase (target_image);
kono
parents:
diff changeset
405 image_descriptors->erase (target_image);
kono
parents:
diff changeset
406 }
kono
parents:
diff changeset
407 return true;
kono
parents:
diff changeset
408 }
kono
parents:
diff changeset
409
kono
parents:
diff changeset
410 extern "C" void *
kono
parents:
diff changeset
411 GOMP_OFFLOAD_alloc (int device, size_t size)
kono
parents:
diff changeset
412 {
kono
parents:
diff changeset
413 TRACE ("(device = %d, size = %d)", device, size);
kono
parents:
diff changeset
414
kono
parents:
diff changeset
415 void *tgt_ptr;
kono
parents:
diff changeset
416 VarDesc vd[2] = { vd_host2tgt, vd_tgt2host };
kono
parents:
diff changeset
417 vd[0].ptr = &size;
kono
parents:
diff changeset
418 vd[0].size = sizeof (size);
kono
parents:
diff changeset
419 vd[1].ptr = &tgt_ptr;
kono
parents:
diff changeset
420 vd[1].size = sizeof (void *);
kono
parents:
diff changeset
421
kono
parents:
diff changeset
422 if (!offload (__FILE__, __LINE__, device, "__offload_target_alloc", 2,
kono
parents:
diff changeset
423 vd, NULL))
kono
parents:
diff changeset
424 return NULL;
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 return tgt_ptr;
kono
parents:
diff changeset
427 }
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 extern "C" bool
kono
parents:
diff changeset
430 GOMP_OFFLOAD_free (int device, void *tgt_ptr)
kono
parents:
diff changeset
431 {
kono
parents:
diff changeset
432 TRACE ("(device = %d, tgt_ptr = %p)", device, tgt_ptr);
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 VarDesc vd = vd_host2tgt;
kono
parents:
diff changeset
435 vd.ptr = &tgt_ptr;
kono
parents:
diff changeset
436 vd.size = sizeof (void *);
kono
parents:
diff changeset
437
kono
parents:
diff changeset
438 return offload (__FILE__, __LINE__, device, "__offload_target_free", 1,
kono
parents:
diff changeset
439 &vd, NULL);
kono
parents:
diff changeset
440 }
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 extern "C" bool
kono
parents:
diff changeset
443 GOMP_OFFLOAD_host2dev (int device, void *tgt_ptr, const void *host_ptr,
kono
parents:
diff changeset
444 size_t size)
kono
parents:
diff changeset
445 {
kono
parents:
diff changeset
446 TRACE ("(device = %d, tgt_ptr = %p, host_ptr = %p, size = %d)",
kono
parents:
diff changeset
447 device, tgt_ptr, host_ptr, size);
kono
parents:
diff changeset
448 if (!size)
kono
parents:
diff changeset
449 return true;
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 VarDesc vd1[2] = { vd_host2tgt, vd_host2tgt };
kono
parents:
diff changeset
452 vd1[0].ptr = &tgt_ptr;
kono
parents:
diff changeset
453 vd1[0].size = sizeof (void *);
kono
parents:
diff changeset
454 vd1[1].ptr = &size;
kono
parents:
diff changeset
455 vd1[1].size = sizeof (size);
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 if (!offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p1", 2,
kono
parents:
diff changeset
458 vd1, NULL))
kono
parents:
diff changeset
459 return false;
kono
parents:
diff changeset
460
kono
parents:
diff changeset
461 VarDesc vd2 = vd_host2tgt;
kono
parents:
diff changeset
462 vd2.ptr = (void *) host_ptr;
kono
parents:
diff changeset
463 vd2.size = size;
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 return offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p2", 1,
kono
parents:
diff changeset
466 &vd2, NULL);
kono
parents:
diff changeset
467 }
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 extern "C" bool
kono
parents:
diff changeset
470 GOMP_OFFLOAD_dev2host (int device, void *host_ptr, const void *tgt_ptr,
kono
parents:
diff changeset
471 size_t size)
kono
parents:
diff changeset
472 {
kono
parents:
diff changeset
473 TRACE ("(device = %d, host_ptr = %p, tgt_ptr = %p, size = %d)",
kono
parents:
diff changeset
474 device, host_ptr, tgt_ptr, size);
kono
parents:
diff changeset
475 if (!size)
kono
parents:
diff changeset
476 return true;
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 VarDesc vd1[2] = { vd_host2tgt, vd_host2tgt };
kono
parents:
diff changeset
479 vd1[0].ptr = &tgt_ptr;
kono
parents:
diff changeset
480 vd1[0].size = sizeof (void *);
kono
parents:
diff changeset
481 vd1[1].ptr = &size;
kono
parents:
diff changeset
482 vd1[1].size = sizeof (size);
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 if (!offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p1", 2,
kono
parents:
diff changeset
485 vd1, NULL))
kono
parents:
diff changeset
486 return false;
kono
parents:
diff changeset
487
kono
parents:
diff changeset
488 VarDesc vd2 = vd_tgt2host;
kono
parents:
diff changeset
489 vd2.ptr = (void *) host_ptr;
kono
parents:
diff changeset
490 vd2.size = size;
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 return offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p2", 1,
kono
parents:
diff changeset
493 &vd2, NULL);
kono
parents:
diff changeset
494 }
kono
parents:
diff changeset
495
kono
parents:
diff changeset
496 extern "C" bool
kono
parents:
diff changeset
497 GOMP_OFFLOAD_dev2dev (int device, void *dst_ptr, const void *src_ptr,
kono
parents:
diff changeset
498 size_t size)
kono
parents:
diff changeset
499 {
kono
parents:
diff changeset
500 TRACE ("(device = %d, dst_ptr = %p, src_ptr = %p, size = %d)",
kono
parents:
diff changeset
501 device, dst_ptr, src_ptr, size);
kono
parents:
diff changeset
502 if (!size)
kono
parents:
diff changeset
503 return true;
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505 VarDesc vd[3] = { vd_host2tgt, vd_host2tgt, vd_host2tgt };
kono
parents:
diff changeset
506 vd[0].ptr = &dst_ptr;
kono
parents:
diff changeset
507 vd[0].size = sizeof (void *);
kono
parents:
diff changeset
508 vd[1].ptr = &src_ptr;
kono
parents:
diff changeset
509 vd[1].size = sizeof (void *);
kono
parents:
diff changeset
510 vd[2].ptr = &size;
kono
parents:
diff changeset
511 vd[2].size = sizeof (size);
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 return offload (__FILE__, __LINE__, device, "__offload_target_tgt2tgt", 3,
kono
parents:
diff changeset
514 vd, NULL);
kono
parents:
diff changeset
515 }
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 extern "C" void
kono
parents:
diff changeset
518 GOMP_OFFLOAD_async_run (int device, void *tgt_fn, void *tgt_vars,
kono
parents:
diff changeset
519 void **, void *async_data)
kono
parents:
diff changeset
520 {
kono
parents:
diff changeset
521 TRACE ("(device = %d, tgt_fn = %p, tgt_vars = %p, async_data = %p)", device,
kono
parents:
diff changeset
522 tgt_fn, tgt_vars, async_data);
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 VarDesc vd[2] = { vd_host2tgt, vd_host2tgt };
kono
parents:
diff changeset
525 vd[0].ptr = &tgt_fn;
kono
parents:
diff changeset
526 vd[0].size = sizeof (void *);
kono
parents:
diff changeset
527 vd[1].ptr = &tgt_vars;
kono
parents:
diff changeset
528 vd[1].size = sizeof (void *);
kono
parents:
diff changeset
529
kono
parents:
diff changeset
530 offload (__FILE__, __LINE__, device, "__offload_target_run", 2, vd,
kono
parents:
diff changeset
531 (const void **) async_data);
kono
parents:
diff changeset
532 }
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 extern "C" void
kono
parents:
diff changeset
535 GOMP_OFFLOAD_run (int device, void *tgt_fn, void *tgt_vars, void **)
kono
parents:
diff changeset
536 {
kono
parents:
diff changeset
537 TRACE ("(device = %d, tgt_fn = %p, tgt_vars = %p)", device, tgt_fn, tgt_vars);
kono
parents:
diff changeset
538
kono
parents:
diff changeset
539 GOMP_OFFLOAD_async_run (device, tgt_fn, tgt_vars, NULL, NULL);
kono
parents:
diff changeset
540 }