1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/* Plugin for offload execution on Intel MIC devices.
Copyright (C) 2014 Free Software Foundation, Inc.
Contributed by Ilya Verbin <ilya.verbin@intel.com>.
This file is part of the GNU Offloading and Multi Processing Library
(libgomp).
Libgomp is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
/* Target side part of a libgomp plugin. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "compiler_if_target.h"
#ifdef DEBUG
#define TRACE(...) \
{ \
fprintf (stderr, "TARGET:\t%s:%s ", __FILE__, __FUNCTION__); \
fprintf (stderr, __VA_ARGS__); \
fprintf (stderr, "\n"); \
}
#else
#define TRACE { }
#endif
static VarDesc vd_host2tgt = {
{ 1, 1 }, /* dst, src */
{ 1, 0 }, /* in, out */
1, /* alloc_if */
1, /* free_if */
4, /* align */
0, /* mic_offset */
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* is_static, is_static_dstn, has_length,
is_stack_buf, sink_addr, alloc_disp,
is_noncont_src, is_noncont_dst */
0, /* offset */
0, /* size */
1, /* count */
0, /* alloc */
0, /* into */
0 /* ptr */
};
static VarDesc vd_tgt2host = {
{ 1, 1 }, /* dst, src */
{ 0, 1 }, /* in, out */
1, /* alloc_if */
1, /* free_if */
4, /* align */
0, /* mic_offset */
{ 0, 0, 0, 0, 0, 0, 0, 0 }, /* is_static, is_static_dstn, has_length,
is_stack_buf, sink_addr, alloc_disp,
is_noncont_src, is_noncont_dst */
0, /* offset */
0, /* size */
1, /* count */
0, /* alloc */
0, /* into */
0 /* ptr */
};
/* Pointer to the descriptor of the last loaded shared library. */
static void *last_loaded_library = NULL;
/* Pointer and size of the variable, used in __offload_target_host2tgt_p[12]
and __offload_target_tgt2host_p[12]. */
static void *last_var_ptr = NULL;
static int last_var_size = 0;
/* Override the corresponding functions from libgomp. */
extern "C" int
omp_is_initial_device (void) __GOMP_NOTHROW
{
return 0;
}
extern "C" int32_t
omp_is_initial_device_ (void)
{
return omp_is_initial_device ();
}
/* Dummy function needed for the initialization of target process during the
first call to __offload_offload1. */
static void
__offload_target_init_proc (OFFLOAD ofldt)
{
TRACE ("");
}
/* Collect addresses of the offload functions and of the global variables from
the library descriptor and send them to host.
Part 1: Send num_funcs and num_vars to host. */
static void
__offload_target_table_p1 (OFFLOAD ofldt)
{
void ***lib_descr = (void ***) last_loaded_library;
if (lib_descr == NULL)
{
TRACE ("");
fprintf (stderr, "Error! No shared libraries loaded on target.\n");
return;
}
void **func_table_begin = lib_descr[0];
void **func_table_end = lib_descr[1];
void **var_table_begin = lib_descr[2];
void **var_table_end = lib_descr[3];
/* The func table contains only addresses, the var table contains addresses
and corresponding sizes. */
int num_funcs = func_table_end - func_table_begin;
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 } };
__offload_target_enter (ofldt, 2, vd1, vd2);
__offload_target_leave (ofldt);
}
/* Part 2: Send the table with addresses to host. */
static void
__offload_target_table_p2 (OFFLOAD ofldt)
{
void ***lib_descr = (void ***) last_loaded_library;
void **func_table_begin = lib_descr[0];
void **func_table_end = lib_descr[1];
void **var_table_begin = lib_descr[2];
void **var_table_end = lib_descr[3];
int num_funcs = func_table_end - func_table_begin;
int num_vars = (var_table_end - var_table_begin) / 2;
int table_size = (num_funcs + 2 * num_vars) * sizeof (void *);
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 };
__offload_target_enter (ofldt, 1, &vd1, &vd2);
void **p;
int i = 0;
for (p = func_table_begin; p < func_table_end; p++, i++)
table[i] = *p;
for (p = var_table_begin; p < var_table_end; p++, i++)
table[i] = *p;
__offload_target_leave (ofldt);
free (table);
}
/* Allocate size bytes and send a pointer to the allocated memory to host. */
static void
__offload_target_alloc (OFFLOAD ofldt)
{
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 } };
__offload_target_enter (ofldt, 2, vd1, vd2);
ptr = malloc (size);
TRACE ("(size = %d): ptr = %p", size, ptr);
__offload_target_leave (ofldt);
}
/* Free the memory space pointed to by ptr. */
static void
__offload_target_free (OFFLOAD ofldt)
{
void *ptr = 0;
VarDesc vd1 = vd_host2tgt;
vd1.ptr = &ptr;
vd1.size = sizeof (void *);
VarDesc2 vd2 = { "ptr", 0 };
__offload_target_enter (ofldt, 1, &vd1, &vd2);
TRACE ("(ptr = %p)", ptr);
free (ptr);
__offload_target_leave (ofldt);
}
/* Receive var_size bytes from host and store to var_ptr.
Part 1: Receive var_ptr and var_size from host. */
static void
__offload_target_host2tgt_p1 (OFFLOAD 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 } };
__offload_target_enter (ofldt, 2, vd1, vd2);
TRACE ("(var_ptr = %p, var_size = %d)", var_ptr, var_size);
last_var_ptr = var_ptr;
last_var_size = var_size;
__offload_target_leave (ofldt);
}
/* Part 2: Receive the data from host. */
static void
__offload_target_host2tgt_p2 (OFFLOAD ofldt)
{
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 };
__offload_target_enter (ofldt, 1, &vd1, &vd2);
__offload_target_leave (ofldt);
}
/* Send var_size bytes from var_ptr to host.
Part 1: Receive var_ptr and var_size from host. */
static void
__offload_target_tgt2host_p1 (OFFLOAD 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 } };
__offload_target_enter (ofldt, 2, vd1, vd2);
TRACE ("(var_ptr = %p, var_size = %d)", var_ptr, var_size);
last_var_ptr = var_ptr;
last_var_size = var_size;
__offload_target_leave (ofldt);
}
/* Part 2: Send the data to host. */
static void
__offload_target_tgt2host_p2 (OFFLOAD ofldt)
{
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 };
__offload_target_enter (ofldt, 1, &vd1, &vd2);
__offload_target_leave (ofldt);
}
/* Call offload function by the address fn_ptr and pass vars_ptr to it. */
static void
__offload_target_run (OFFLOAD 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 } };
__offload_target_enter (ofldt, 2, vd1, vd2);
TRACE ("(fn_ptr = %p, vars_ptr = %p)", fn_ptr, vars_ptr);
void (*fn)(void *) = (void (*)(void *)) fn_ptr;
fn (vars_ptr);
__offload_target_leave (ofldt);
}
/* This should be called from every library with offloading. */
extern "C" void
target_register_lib (const void *target_table)
{
TRACE ("(target_table = %p { %p, %p, %p, %p })", target_table,
((void **) target_table)[0], ((void **) target_table)[1],
((void **) target_table)[2], ((void **) target_table)[3]);
last_loaded_library = (void *) target_table;
}
/* Use __offload_target_main from liboffload. */
int
main (int argc, char **argv)
{
__offload_target_main ();
return 0;
}
/* Register offload_target_main's functions in the liboffload. */
struct Entry {
const char *name;
void *func;
};
#define REGISTER(f) \
extern "C" const Entry __offload_target_##f##_$entry \
__attribute__ ((section(".OffloadEntryTable."))) = { \
"__offload_target_"#f, \
(void *) __offload_target_##f \
}
REGISTER (init_proc);
REGISTER (table_p1);
REGISTER (table_p2);
REGISTER (alloc);
REGISTER (free);
REGISTER (host2tgt_p1);
REGISTER (host2tgt_p2);
REGISTER (tgt2host_p1);
REGISTER (tgt2host_p2);
REGISTER (run);
#undef REGISTER
|