1 /* Plugin for offload execution on Intel MIC devices.
3 Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 Contributed by Ilya Verbin <ilya.verbin@intel.com>.
7 This file is part of the GNU Offloading and Multi Processing Library
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)
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
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.
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/>. */
29 /* Host side part of a libgomp plugin. */
38 #include "libgomp-plugin.h"
39 #include "compiler_if_host.h"
40 #include "main_target_image.h"
41 #include "gomp-constants.h"
43 #define OFFLOAD_ACTIVE_WAIT_ENV "OFFLOAD_ACTIVE_WAIT"
48 fprintf (stderr, "HOST:\t%s:%s ", __FILE__, __FUNCTION__); \
49 fprintf (stderr, __VA_ARGS__); \
50 fprintf (stderr, "\n"); \
57 /* Start/end addresses of functions and global variables on a device. */
58 typedef std::vector
<addr_pair
> AddrVect
;
60 /* Addresses for one image and all devices. */
61 typedef std::vector
<AddrVect
> DevAddrVect
;
63 /* Addresses for all images and all devices. */
64 typedef std::map
<const void *, DevAddrVect
> ImgDevAddrMap
;
66 /* Image descriptor needed by __offload_[un]register_image. */
67 struct TargetImageDesc
{
69 /* 10 characters is enough for max int value. */
70 char name
[sizeof ("lib0000000000.so")];
74 /* Image descriptors, indexed by a pointer obtained from libgomp. */
75 typedef std::map
<const void *, TargetImageDesc
*> ImgDescMap
;
78 /* Total number of available devices. */
79 static int num_devices
;
81 /* Total number of shared libraries with offloading to Intel MIC. */
82 static int num_images
;
84 /* Two dimensional array: one key is a pointer to image,
85 second key is number of device. Contains a vector of pointer pairs. */
86 static ImgDevAddrMap
*address_table
;
88 /* Descriptors of all images, registered in liboffloadmic. */
89 static ImgDescMap
*image_descriptors
;
91 /* Thread-safe registration of the main image. */
92 static pthread_once_t main_image_is_registered
= PTHREAD_ONCE_INIT
;
94 static VarDesc vd_host2tgt
= {
95 { 1, 1 }, /* dst, src */
96 { 1, 0 }, /* in, out */
101 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* is_static, is_static_dstn, has_length,
102 is_stack_buf, sink_addr, alloc_disp,
103 is_noncont_src, is_noncont_dst */
112 static VarDesc vd_tgt2host
= {
113 { 1, 1 }, /* dst, src */
114 { 0, 1 }, /* in, out */
119 { 0, 0, 0, 0, 0, 0, 0, 0 }, /* is_static, is_static_dstn, has_length,
120 is_stack_buf, sink_addr, alloc_disp,
121 is_noncont_src, is_noncont_dst */
131 __attribute__((constructor
))
135 const char *active_wait
= getenv (OFFLOAD_ACTIVE_WAIT_ENV
);
137 /* Disable active wait by default to avoid useless CPU usage. */
139 setenv (OFFLOAD_ACTIVE_WAIT_ENV
, "0", 0);
141 address_table
= new ImgDevAddrMap
;
142 image_descriptors
= new ImgDescMap
;
143 num_devices
= _Offload_number_of_devices ();
146 extern "C" const char *
147 GOMP_OFFLOAD_get_name (void)
149 const char *res
= "intelmic";
150 TRACE ("(): return %s", res
);
154 extern "C" unsigned int
155 GOMP_OFFLOAD_get_caps (void)
157 unsigned int res
= GOMP_OFFLOAD_CAP_OPENMP_400
;
158 TRACE ("(): return %x", res
);
163 GOMP_OFFLOAD_get_type (void)
165 enum offload_target_type res
= OFFLOAD_TARGET_TYPE_INTEL_MIC
;
166 TRACE ("(): return %d", res
);
171 GOMP_OFFLOAD_get_num_devices (void)
173 TRACE ("(): return %d", num_devices
);
177 extern "C" union gomp_device_property_value
178 GOMP_OFFLOAD_get_property (int n
, int prop
)
180 union gomp_device_property_value nullval
= { .val
= 0 };
182 if (n
>= num_devices
)
185 ("Request for a property of a non-existing Intel MIC device %i", n
);
191 case GOMP_DEVICE_PROPERTY_VENDOR
:
192 return (union gomp_device_property_value
) { .ptr
= "Intel" };
199 offload (const char *file
, uint64_t line
, int device
, const char *name
,
200 int num_vars
, VarDesc
*vars
, const void **async_data
)
202 OFFLOAD ofld
= __offload_target_acquire1 (&device
, file
, line
);
205 if (async_data
== NULL
)
206 return __offload_offload1 (ofld
, name
, 0, num_vars
, vars
, NULL
, 0,
212 flags
.bits
.omp_async
= 1;
213 return __offload_offload3 (ofld
, name
, 0, num_vars
, vars
, NULL
, 0,
214 NULL
, async_data
, 0, NULL
, flags
, NULL
);
219 GOMP_PLUGIN_error ("%s:%d: Offload target acquire failed\n", file
, line
);
225 register_main_image ()
227 /* Do not check the return value, because old versions of liboffloadmic did
228 not have return values. */
229 __offload_register_image (&main_target_image
);
231 /* liboffloadmic will call GOMP_PLUGIN_target_task_completion when
232 asynchronous task on target is completed. */
233 __offload_register_task_callback (GOMP_PLUGIN_target_task_completion
);
236 /* liboffloadmic loads and runs offload_target_main on all available devices
237 during a first call to offload (). */
239 GOMP_OFFLOAD_init_device (int device
)
241 TRACE ("(device = %d)", device
);
242 pthread_once (&main_image_is_registered
, register_main_image
);
243 return offload (__FILE__
, __LINE__
, device
, "__offload_target_init_proc", 0,
248 GOMP_OFFLOAD_fini_device (int device
)
250 TRACE ("(device = %d)", device
);
252 /* liboffloadmic will finalize target processes on all available devices. */
253 __offload_unregister_image (&main_target_image
);
258 get_target_table (int device
, int &num_funcs
, int &num_vars
, void **&table
)
260 VarDesc vd1
[2] = { vd_tgt2host
, vd_tgt2host
};
261 vd1
[0].ptr
= &num_funcs
;
262 vd1
[0].size
= sizeof (num_funcs
);
263 vd1
[1].ptr
= &num_vars
;
264 vd1
[1].size
= sizeof (num_vars
);
266 if (!offload (__FILE__
, __LINE__
, device
, "__offload_target_table_p1", 2,
270 int table_size
= num_funcs
+ 2 * num_vars
;
273 table
= new void * [table_size
];
278 vd2
.size
= table_size
* sizeof (void *);
280 return offload (__FILE__
, __LINE__
, device
, "__offload_target_table_p2",
286 /* Offload TARGET_IMAGE to all available devices and fill address_table with
287 corresponding target addresses. */
290 offload_image (const void *target_image
)
292 void *image_start
= ((void **) target_image
)[0];
293 void *image_end
= ((void **) target_image
)[1];
295 TRACE ("(target_image = %p { %p, %p })",
296 target_image
, image_start
, image_end
);
298 int64_t image_size
= (uintptr_t) image_end
- (uintptr_t) image_start
;
299 TargetImageDesc
*image
= (TargetImageDesc
*) malloc (offsetof (TargetImageDesc
, data
)
303 GOMP_PLUGIN_error ("%s: Can't allocate memory\n", __FILE__
);
307 image
->size
= image_size
;
308 sprintf (image
->name
, "lib%010d.so", num_images
++);
309 memcpy (image
->data
, image_start
, image
->size
);
311 TRACE ("() __offload_register_image %s { %p, %d }",
312 image
->name
, image_start
, image
->size
);
313 /* Do not check the return value, because old versions of liboffloadmic did
314 not have return values. */
315 __offload_register_image (image
);
317 /* Receive tables for target_image from all devices. */
318 DevAddrVect dev_table
;
320 for (int dev
= 0; dev
< num_devices
; dev
++)
326 ret
&= get_target_table (dev
, num_funcs
, num_vars
, table
);
328 AddrVect curr_dev_table
;
330 for (int i
= 0; i
< num_funcs
; i
++)
333 tgt_addr
.start
= (uintptr_t) table
[i
];
334 tgt_addr
.end
= tgt_addr
.start
+ 1;
335 TRACE ("() func %d:\t0x%llx..0x%llx", i
,
336 tgt_addr
.start
, tgt_addr
.end
);
337 curr_dev_table
.push_back (tgt_addr
);
340 for (int i
= 0; i
< num_vars
; i
++)
343 tgt_addr
.start
= (uintptr_t) table
[num_funcs
+i
*2];
344 tgt_addr
.end
= tgt_addr
.start
+ (uintptr_t) table
[num_funcs
+i
*2+1];
345 TRACE ("() var %d:\t0x%llx..0x%llx", i
, tgt_addr
.start
, tgt_addr
.end
);
346 curr_dev_table
.push_back (tgt_addr
);
349 dev_table
.push_back (curr_dev_table
);
353 address_table
->insert (std::make_pair (target_image
, dev_table
));
354 image_descriptors
->insert (std::make_pair (target_image
, image
));
358 /* Return the libgomp version number we're compatible with. There is
359 no requirement for cross-version compatibility. */
362 GOMP_OFFLOAD_version (void)
368 GOMP_OFFLOAD_load_image (int device
, const unsigned version
,
369 const void *target_image
, addr_pair
**result
)
371 TRACE ("(device = %d, target_image = %p)", device
, target_image
);
373 if (GOMP_VERSION_DEV (version
) > GOMP_VERSION_INTEL_MIC
)
375 GOMP_PLUGIN_error ("Offload data incompatible with intelmic plugin"
376 " (expected %u, received %u)",
377 GOMP_VERSION_INTEL_MIC
, GOMP_VERSION_DEV (version
));
381 /* If target_image is already present in address_table, then there is no need
383 if (address_table
->count (target_image
) == 0)
385 /* If fail, return -1 as error code. */
386 if (!offload_image (target_image
))
390 AddrVect
*curr_dev_table
= &(*address_table
)[target_image
][device
];
391 int table_size
= curr_dev_table
->size ();
392 addr_pair
*table
= (addr_pair
*) malloc (table_size
* sizeof (addr_pair
));
395 GOMP_PLUGIN_error ("%s: Can't allocate memory\n", __FILE__
);
399 std::copy (curr_dev_table
->begin (), curr_dev_table
->end (), table
);
405 GOMP_OFFLOAD_unload_image (int device
, unsigned version
,
406 const void *target_image
)
408 if (GOMP_VERSION_DEV (version
) > GOMP_VERSION_INTEL_MIC
)
410 GOMP_PLUGIN_error ("Offload data incompatible with intelmic plugin"
411 " (expected %u, received %u)",
412 GOMP_VERSION_INTEL_MIC
, GOMP_VERSION_DEV (version
));
416 TRACE ("(device = %d, target_image = %p)", device
, target_image
);
418 /* liboffloadmic unloads the image from all available devices. */
419 if (image_descriptors
->count (target_image
) > 0)
421 TargetImageDesc
*image_desc
= (*image_descriptors
)[target_image
];
422 __offload_unregister_image (image_desc
);
425 address_table
->erase (target_image
);
426 image_descriptors
->erase (target_image
);
432 GOMP_OFFLOAD_alloc (int device
, size_t size
)
434 TRACE ("(device = %d, size = %d)", device
, size
);
437 VarDesc vd
[2] = { vd_host2tgt
, vd_tgt2host
};
439 vd
[0].size
= sizeof (size
);
440 vd
[1].ptr
= &tgt_ptr
;
441 vd
[1].size
= sizeof (void *);
443 if (!offload (__FILE__
, __LINE__
, device
, "__offload_target_alloc", 2,
451 GOMP_OFFLOAD_free (int device
, void *tgt_ptr
)
453 TRACE ("(device = %d, tgt_ptr = %p)", device
, tgt_ptr
);
455 VarDesc vd
= vd_host2tgt
;
457 vd
.size
= sizeof (void *);
459 return offload (__FILE__
, __LINE__
, device
, "__offload_target_free", 1,
464 GOMP_OFFLOAD_host2dev (int device
, void *tgt_ptr
, const void *host_ptr
,
467 TRACE ("(device = %d, tgt_ptr = %p, host_ptr = %p, size = %d)",
468 device
, tgt_ptr
, host_ptr
, size
);
472 VarDesc vd1
[2] = { vd_host2tgt
, vd_host2tgt
};
473 vd1
[0].ptr
= &tgt_ptr
;
474 vd1
[0].size
= sizeof (void *);
476 vd1
[1].size
= sizeof (size
);
478 if (!offload (__FILE__
, __LINE__
, device
, "__offload_target_host2tgt_p1", 2,
482 VarDesc vd2
= vd_host2tgt
;
483 vd2
.ptr
= (void *) host_ptr
;
486 return offload (__FILE__
, __LINE__
, device
, "__offload_target_host2tgt_p2", 1,
491 GOMP_OFFLOAD_dev2host (int device
, void *host_ptr
, const void *tgt_ptr
,
494 TRACE ("(device = %d, host_ptr = %p, tgt_ptr = %p, size = %d)",
495 device
, host_ptr
, tgt_ptr
, size
);
499 VarDesc vd1
[2] = { vd_host2tgt
, vd_host2tgt
};
500 vd1
[0].ptr
= &tgt_ptr
;
501 vd1
[0].size
= sizeof (void *);
503 vd1
[1].size
= sizeof (size
);
505 if (!offload (__FILE__
, __LINE__
, device
, "__offload_target_tgt2host_p1", 2,
509 VarDesc vd2
= vd_tgt2host
;
510 vd2
.ptr
= (void *) host_ptr
;
513 return offload (__FILE__
, __LINE__
, device
, "__offload_target_tgt2host_p2", 1,
518 GOMP_OFFLOAD_dev2dev (int device
, void *dst_ptr
, const void *src_ptr
,
521 TRACE ("(device = %d, dst_ptr = %p, src_ptr = %p, size = %d)",
522 device
, dst_ptr
, src_ptr
, size
);
526 VarDesc vd
[3] = { vd_host2tgt
, vd_host2tgt
, vd_host2tgt
};
527 vd
[0].ptr
= &dst_ptr
;
528 vd
[0].size
= sizeof (void *);
529 vd
[1].ptr
= &src_ptr
;
530 vd
[1].size
= sizeof (void *);
532 vd
[2].size
= sizeof (size
);
534 return offload (__FILE__
, __LINE__
, device
, "__offload_target_tgt2tgt", 3,
539 GOMP_OFFLOAD_async_run (int device
, void *tgt_fn
, void *tgt_vars
,
540 void **, void *async_data
)
542 TRACE ("(device = %d, tgt_fn = %p, tgt_vars = %p, async_data = %p)", device
,
543 tgt_fn
, tgt_vars
, async_data
);
545 VarDesc vd
[2] = { vd_host2tgt
, vd_host2tgt
};
547 vd
[0].size
= sizeof (void *);
548 vd
[1].ptr
= &tgt_vars
;
549 vd
[1].size
= sizeof (void *);
551 offload (__FILE__
, __LINE__
, device
, "__offload_target_run", 2, vd
,
552 (const void **) async_data
);
556 GOMP_OFFLOAD_run (int device
, void *tgt_fn
, void *tgt_vars
, void **)
558 TRACE ("(device = %d, tgt_fn = %p, tgt_vars = %p)", device
, tgt_fn
, tgt_vars
);
560 GOMP_OFFLOAD_async_run (device
, tgt_fn
, tgt_vars
, NULL
, NULL
);