/* Addresses for all images and all devices. */
typedef std::map<const void *, DevAddrVect> ImgDevAddrMap;
+/* Image descriptor needed by __offload_[un]register_image. */
+struct TargetImageDesc {
+ int64_t size;
+ /* 10 characters is enough for max int value. */
+ char name[sizeof ("lib0000000000.so")];
+ char data[];
+};
+
+/* Image descriptors, indexed by a pointer obtained from libgomp. */
+typedef std::map<const void *, TargetImageDesc *> ImgDescMap;
+
/* Total number of available devices. */
static int num_devices;
second key is number of device. Contains a vector of pointer pairs. */
static ImgDevAddrMap *address_table;
+/* Descriptors of all images, registered in liboffloadmic. */
+static ImgDescMap *image_descriptors;
+
/* Thread-safe registration of the main image. */
static pthread_once_t main_image_is_registered = PTHREAD_ONCE_INIT;
out:
address_table = new ImgDevAddrMap;
+ image_descriptors = new ImgDescMap;
num_devices = _Offload_number_of_devices ();
}
static void
offload (const char *file, uint64_t line, int device, const char *name,
- int num_vars, VarDesc *vars, VarDesc2 *vars2, const void **async_data)
+ int num_vars, VarDesc *vars, const void **async_data)
{
OFFLOAD ofld = __offload_target_acquire1 (&device, file, line);
if (ofld)
{
if (async_data == NULL)
- __offload_offload1 (ofld, name, 0, num_vars, vars, vars2, 0, NULL,
- NULL);
+ __offload_offload1 (ofld, name, 0, num_vars, vars, NULL, 0, NULL, NULL);
else
{
OffloadFlags flags;
}
}
+static void
+unregister_main_image ()
+{
+ __offload_unregister_image (&main_target_image);
+}
+
static void
register_main_image ()
{
+ /* Do not check the return value, because old versions of liboffloadmic did
+ not have return values. */
__offload_register_image (&main_target_image);
/* liboffloadmic will call GOMP_PLUGIN_target_task_completion when
asynchronous task on target is completed. */
__offload_register_task_callback (GOMP_PLUGIN_target_task_completion);
+
+ if (atexit (unregister_main_image) != 0)
+ {
+ fprintf (stderr, "%s: atexit failed\n", __FILE__);
+ exit (1);
+ }
}
/* liboffloadmic loads and runs offload_target_main on all available devices
extern "C" void
GOMP_OFFLOAD_init_device (int device)
{
- TRACE ("");
+ TRACE ("(device = %d)", device);
pthread_once (&main_image_is_registered, register_main_image);
- offload (__FILE__, __LINE__, device, "__offload_target_init_proc", 0,
- NULL, NULL, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_init_proc", 0, NULL,
+ NULL);
}
extern "C" void
GOMP_OFFLOAD_fini_device (int device)
{
- TRACE ("");
+ TRACE ("(device = %d)", device);
/* Unreachable for GOMP_OFFLOAD_CAP_OPENMP_400. */
abort ();
}
vd1[0].size = sizeof (num_funcs);
vd1[1].ptr = &num_vars;
vd1[1].size = sizeof (num_vars);
- VarDesc2 vd1g[2] = { { "num_funcs", 0 }, { "num_vars", 0 } };
- offload (__FILE__, __LINE__, device, "__offload_target_table_p1", 2,
- vd1, vd1g, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_table_p1", 2, vd1,
+ NULL);
int table_size = num_funcs + 2 * num_vars;
if (table_size > 0)
vd2 = vd_tgt2host;
vd2.ptr = table;
vd2.size = table_size * sizeof (void *);
- VarDesc2 vd2g = { "table", 0 };
- offload (__FILE__, __LINE__, device, "__offload_target_table_p2", 1,
- &vd2, &vd2g, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_table_p2", 1, &vd2,
+ NULL);
}
}
static void
offload_image (const void *target_image)
{
- struct TargetImage {
- int64_t size;
- /* 10 characters is enough for max int value. */
- char name[sizeof ("lib0000000000.so")];
- char data[];
- } __attribute__ ((packed));
-
void *image_start = ((void **) target_image)[0];
void *image_end = ((void **) target_image)[1];
target_image, image_start, image_end);
int64_t image_size = (uintptr_t) image_end - (uintptr_t) image_start;
- TargetImage *image
- = (TargetImage *) malloc (sizeof (int64_t) + sizeof ("lib0000000000.so")
- + image_size);
+ TargetImageDesc *image = (TargetImageDesc *) malloc (offsetof (TargetImageDesc, data)
+ + image_size);
if (!image)
{
fprintf (stderr, "%s: Can't allocate memory\n", __FILE__);
TRACE ("() __offload_register_image %s { %p, %d }",
image->name, image_start, image->size);
+ /* Do not check the return value, because old versions of liboffloadmic did
+ not have return values. */
__offload_register_image (image);
/* Receive tables for target_image from all devices. */
}
dev_table.push_back (curr_dev_table);
+ delete [] table;
}
address_table->insert (std::make_pair (target_image, dev_table));
-
- free (image);
+ image_descriptors->insert (std::make_pair (target_image, image));
}
/* Return the libgomp version number we're compatible with. There is
TRACE ("(device = %d, target_image = %p)", device, target_image);
- /* TODO: Currently liboffloadmic doesn't support __offload_unregister_image
- for libraries. */
+ /* liboffloadmic unloads the image from all available devices. */
+ if (image_descriptors->count (target_image) > 0)
+ {
+ TargetImageDesc *image_desc = (*image_descriptors)[target_image];
+ __offload_unregister_image (image_desc);
+ free (image_desc);
- address_table->erase (target_image);
+ address_table->erase (target_image);
+ image_descriptors->erase (target_image);
+ }
}
extern "C" void *
GOMP_OFFLOAD_alloc (int device, size_t size)
{
- TRACE ("(size = %d)", size);
+ TRACE ("(device = %d, size = %d)", device, size);
void *tgt_ptr;
- VarDesc vd1[2] = { vd_host2tgt, vd_tgt2host };
- vd1[0].ptr = &size;
- vd1[0].size = sizeof (size);
- vd1[1].ptr = &tgt_ptr;
- vd1[1].size = sizeof (void *);
- VarDesc2 vd1g[2] = { { "size", 0 }, { "tgt_ptr", 0 } };
-
- offload (__FILE__, __LINE__, device, "__offload_target_alloc", 2, vd1, vd1g,
- NULL);
+ VarDesc vd[2] = { vd_host2tgt, vd_tgt2host };
+ vd[0].ptr = &size;
+ vd[0].size = sizeof (size);
+ vd[1].ptr = &tgt_ptr;
+ vd[1].size = sizeof (void *);
+
+ offload (__FILE__, __LINE__, device, "__offload_target_alloc", 2, vd, NULL);
+
return tgt_ptr;
}
extern "C" void
GOMP_OFFLOAD_free (int device, void *tgt_ptr)
{
- TRACE ("(tgt_ptr = %p)", tgt_ptr);
+ TRACE ("(device = %d, tgt_ptr = %p)", device, tgt_ptr);
- VarDesc vd1 = vd_host2tgt;
- vd1.ptr = &tgt_ptr;
- vd1.size = sizeof (void *);
- VarDesc2 vd1g = { "tgt_ptr", 0 };
+ VarDesc vd = vd_host2tgt;
+ vd.ptr = &tgt_ptr;
+ vd.size = sizeof (void *);
- offload (__FILE__, __LINE__, device, "__offload_target_free", 1, &vd1, &vd1g,
- NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_free", 1, &vd, NULL);
}
extern "C" void *
GOMP_OFFLOAD_host2dev (int device, void *tgt_ptr, const void *host_ptr,
size_t size)
{
- TRACE ("(tgt_ptr = %p, host_ptr = %p, size = %d)", tgt_ptr, host_ptr, size);
+ TRACE ("(device = %d, tgt_ptr = %p, host_ptr = %p, size = %d)",
+ device, tgt_ptr, host_ptr, size);
if (!size)
return tgt_ptr;
vd1[0].size = sizeof (void *);
vd1[1].ptr = &size;
vd1[1].size = sizeof (size);
- VarDesc2 vd1g[2] = { { "tgt_ptr", 0 }, { "size", 0 } };
- offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p1", 2,
- vd1, vd1g, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p1", 2, vd1,
+ NULL);
VarDesc vd2 = vd_host2tgt;
vd2.ptr = (void *) host_ptr;
vd2.size = size;
- VarDesc2 vd2g = { "var", 0 };
- offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p2", 1,
- &vd2, &vd2g, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_host2tgt_p2", 1, &vd2,
+ NULL);
return tgt_ptr;
}
GOMP_OFFLOAD_dev2host (int device, void *host_ptr, const void *tgt_ptr,
size_t size)
{
- TRACE ("(host_ptr = %p, tgt_ptr = %p, size = %d)", host_ptr, tgt_ptr, size);
+ TRACE ("(device = %d, host_ptr = %p, tgt_ptr = %p, size = %d)",
+ device, host_ptr, tgt_ptr, size);
if (!size)
return host_ptr;
vd1[0].size = sizeof (void *);
vd1[1].ptr = &size;
vd1[1].size = sizeof (size);
- VarDesc2 vd1g[2] = { { "tgt_ptr", 0 }, { "size", 0 } };
- offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p1", 2,
- vd1, vd1g, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p1", 2, vd1,
+ NULL);
VarDesc vd2 = vd_tgt2host;
vd2.ptr = (void *) host_ptr;
vd2.size = size;
- VarDesc2 vd2g = { "var", 0 };
- offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p2", 1,
- &vd2, &vd2g, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_tgt2host_p2", 1, &vd2,
+ NULL);
return host_ptr;
}
GOMP_OFFLOAD_dev2dev (int device, void *dst_ptr, const void *src_ptr,
size_t size)
{
- TRACE ("(dst_ptr = %p, src_ptr = %p, size = %d)", dst_ptr, src_ptr, size);
+ TRACE ("(device = %d, dst_ptr = %p, src_ptr = %p, size = %d)",
+ device, dst_ptr, src_ptr, size);
if (!size)
return dst_ptr;
- VarDesc vd1[3] = { vd_host2tgt, vd_host2tgt, vd_host2tgt };
- vd1[0].ptr = &dst_ptr;
- vd1[0].size = sizeof (void *);
- vd1[1].ptr = &src_ptr;
- vd1[1].size = sizeof (void *);
- vd1[2].ptr = &size;
- vd1[2].size = sizeof (size);
- VarDesc2 vd1g[3] = { { "dst_ptr", 0 }, { "src_ptr", 0 }, { "size", 0 } };
+ VarDesc vd[3] = { vd_host2tgt, vd_host2tgt, vd_host2tgt };
+ vd[0].ptr = &dst_ptr;
+ vd[0].size = sizeof (void *);
+ vd[1].ptr = &src_ptr;
+ vd[1].size = sizeof (void *);
+ vd[2].ptr = &size;
+ vd[2].size = sizeof (size);
- offload (__FILE__, __LINE__, device, "__offload_target_tgt2tgt", 3, vd1,
- vd1g, NULL);
+ offload (__FILE__, __LINE__, device, "__offload_target_tgt2tgt", 3, vd, NULL);
return dst_ptr;
}
vd[1].ptr = &tgt_vars;
vd[1].size = sizeof (void *);
- offload (__FILE__, __LINE__, device, "__offload_target_run", 2, vd, NULL,
+ offload (__FILE__, __LINE__, device, "__offload_target_run", 2, vd,
(const void **) async_data);
}
/* Plugin for offload execution on Intel MIC devices.
- Copyright (C) 2014 Free Software Foundation, Inc.
+ Copyright (C) 2014-2015 Free Software Foundation, Inc.
Contributed by Ilya Verbin <ilya.verbin@intel.com>.
int num_vars = (var_table_end - var_table_begin) / 2;
TRACE ("(num_funcs = %d, num_vars = %d)", num_funcs, num_vars);
- VarDesc vd1[2] = { vd_tgt2host, vd_tgt2host };
- vd1[0].ptr = &num_funcs;
- vd1[0].size = sizeof (num_funcs);
- vd1[1].ptr = &num_vars;
- vd1[1].size = sizeof (num_vars);
- VarDesc2 vd2[2] = { { "num_funcs", 0 }, { "num_vars", 0 } };
+ VarDesc vd[2] = { vd_tgt2host, vd_tgt2host };
+ vd[0].ptr = &num_funcs;
+ vd[0].size = sizeof (num_funcs);
+ vd[1].ptr = &num_vars;
+ vd[1].size = sizeof (num_vars);
- __offload_target_enter (ofldt, 2, vd1, vd2);
+ __offload_target_enter (ofldt, 2, vd, NULL);
__offload_target_leave (ofldt);
}
void **table = (void **) malloc (table_size);
TRACE ("(table_size = %d)", table_size);
- VarDesc vd1;
- vd1 = vd_tgt2host;
- vd1.ptr = table;
- vd1.size = table_size;
- VarDesc2 vd2 = { "table", 0 };
+ VarDesc vd = vd_tgt2host;
+ vd.ptr = table;
+ vd.size = table_size;
- __offload_target_enter (ofldt, 1, &vd1, &vd2);
+ __offload_target_enter (ofldt, 1, &vd, NULL);
void **p;
int i = 0;
size_t size = 0;
void *ptr = NULL;
- VarDesc vd1[2] = { vd_host2tgt, vd_tgt2host };
- vd1[0].ptr = &size;
- vd1[0].size = sizeof (size);
- vd1[1].ptr = &ptr;
- vd1[1].size = sizeof (void *);
- VarDesc2 vd2[2] = { { "size", 0 }, { "ptr", 0 } };
+ VarDesc vd[2] = { vd_host2tgt, vd_tgt2host };
+ vd[0].ptr = &size;
+ vd[0].size = sizeof (size);
+ vd[1].ptr = &ptr;
+ vd[1].size = sizeof (void *);
- __offload_target_enter (ofldt, 2, vd1, vd2);
+ __offload_target_enter (ofldt, 2, vd, NULL);
ptr = malloc (size);
TRACE ("(size = %d): ptr = %p", size, ptr);
__offload_target_leave (ofldt);
{
void *ptr = 0;
- VarDesc vd1 = vd_host2tgt;
- vd1.ptr = &ptr;
- vd1.size = sizeof (void *);
- VarDesc2 vd2 = { "ptr", 0 };
+ VarDesc vd = vd_host2tgt;
+ vd.ptr = &ptr;
+ vd.size = sizeof (void *);
- __offload_target_enter (ofldt, 1, &vd1, &vd2);
+ __offload_target_enter (ofldt, 1, &vd, NULL);
TRACE ("(ptr = %p)", ptr);
free (ptr);
__offload_target_leave (ofldt);
void *var_ptr = NULL;
size_t var_size = 0;
- VarDesc vd1[2] = { vd_host2tgt, vd_host2tgt };
- vd1[0].ptr = &var_ptr;
- vd1[0].size = sizeof (void *);
- vd1[1].ptr = &var_size;
- vd1[1].size = sizeof (var_size);
- VarDesc2 vd2[2] = { { "var_ptr", 0 }, { "var_size", 0 } };
+ VarDesc vd[2] = { vd_host2tgt, vd_host2tgt };
+ vd[0].ptr = &var_ptr;
+ vd[0].size = sizeof (void *);
+ vd[1].ptr = &var_size;
+ vd[1].size = sizeof (var_size);
- __offload_target_enter (ofldt, 2, vd1, vd2);
+ __offload_target_enter (ofldt, 2, vd, NULL);
TRACE ("(var_ptr = %p, var_size = %d)", var_ptr, var_size);
last_var_ptr = var_ptr;
last_var_size = var_size;
TRACE ("(last_var_ptr = %p, last_var_size = %d)",
last_var_ptr, last_var_size);
- VarDesc vd1 = vd_host2tgt;
- vd1.ptr = last_var_ptr;
- vd1.size = last_var_size;
- VarDesc2 vd2 = { "var", 0 };
+ VarDesc vd = vd_host2tgt;
+ vd.ptr = last_var_ptr;
+ vd.size = last_var_size;
- __offload_target_enter (ofldt, 1, &vd1, &vd2);
+ __offload_target_enter (ofldt, 1, &vd, NULL);
__offload_target_leave (ofldt);
}
void *var_ptr = NULL;
size_t var_size = 0;
- VarDesc vd1[2] = { vd_host2tgt, vd_host2tgt };
- vd1[0].ptr = &var_ptr;
- vd1[0].size = sizeof (void *);
- vd1[1].ptr = &var_size;
- vd1[1].size = sizeof (var_size);
- VarDesc2 vd2[2] = { { "var_ptr", 0 }, { "var_size", 0 } };
+ VarDesc vd[2] = { vd_host2tgt, vd_host2tgt };
+ vd[0].ptr = &var_ptr;
+ vd[0].size = sizeof (void *);
+ vd[1].ptr = &var_size;
+ vd[1].size = sizeof (var_size);
- __offload_target_enter (ofldt, 2, vd1, vd2);
+ __offload_target_enter (ofldt, 2, vd, NULL);
TRACE ("(var_ptr = %p, var_size = %d)", var_ptr, var_size);
last_var_ptr = var_ptr;
last_var_size = var_size;
TRACE ("(last_var_ptr = %p, last_var_size = %d)",
last_var_ptr, last_var_size);
- VarDesc vd1 = vd_tgt2host;
- vd1.ptr = last_var_ptr;
- vd1.size = last_var_size;
- VarDesc2 vd2 = { "var", 0 };
+ VarDesc vd = vd_tgt2host;
+ vd.ptr = last_var_ptr;
+ vd.size = last_var_size;
- __offload_target_enter (ofldt, 1, &vd1, &vd2);
+ __offload_target_enter (ofldt, 1, &vd, NULL);
__offload_target_leave (ofldt);
}
void *dst_ptr = NULL;
size_t size = 0;
- VarDesc vd1[3] = { vd_host2tgt, vd_host2tgt, vd_host2tgt };
- vd1[0].ptr = &dst_ptr;
- vd1[0].size = sizeof (void *);
- vd1[1].ptr = &src_ptr;
- vd1[1].size = sizeof (void *);
- vd1[2].ptr = &size;
- vd1[2].size = sizeof (size);
- VarDesc2 vd1g[3] = { { "dst_ptr", 0 }, { "src_ptr", 0 }, { "size", 0 } };
+ VarDesc vd[3] = { vd_host2tgt, vd_host2tgt, vd_host2tgt };
+ vd[0].ptr = &dst_ptr;
+ vd[0].size = sizeof (void *);
+ vd[1].ptr = &src_ptr;
+ vd[1].size = sizeof (void *);
+ vd[2].ptr = &size;
+ vd[2].size = sizeof (size);
- __offload_target_enter (ofldt, 3, vd1, vd1g);
+ __offload_target_enter (ofldt, 3, vd, NULL);
TRACE ("(dst_ptr = %p, src_ptr = %p, size = %d)", dst_ptr, src_ptr, size);
memcpy (dst_ptr, src_ptr, size);
__offload_target_leave (ofldt);
void *fn_ptr;
void *vars_ptr;
- VarDesc vd1[2] = { vd_host2tgt, vd_host2tgt };
- vd1[0].ptr = &fn_ptr;
- vd1[0].size = sizeof (void *);
- vd1[1].ptr = &vars_ptr;
- vd1[1].size = sizeof (void *);
- VarDesc2 vd2[2] = { { "fn_ptr", 0 }, { "vars_ptr", 0 } };
+ VarDesc vd[2] = { vd_host2tgt, vd_host2tgt };
+ vd[0].ptr = &fn_ptr;
+ vd[0].size = sizeof (void *);
+ vd[1].ptr = &vars_ptr;
+ vd[1].size = sizeof (void *);
- __offload_target_enter (ofldt, 2, vd1, vd2);
+ __offload_target_enter (ofldt, 2, vd, NULL);
TRACE ("(fn_ptr = %p, vars_ptr = %p)", fn_ptr, vars_ptr);
void (*fn)(void *) = (void (*)(void *)) fn_ptr;
fn (vars_ptr);