offload_myo_host.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "offload_myo_host.h"
  27. #include <errno.h>
  28. #include <malloc.h>
  29. #include "offload_host.h"
  30. #if defined(LINUX) || defined(FREEBSD)
  31. #include <mm_malloc.h>
  32. #endif
  33. #define MYO_VERSION1 "MYO_1.0"
  34. extern "C" void __cilkrts_cilk_for_32(void*, void*, uint32_t, int32_t);
  35. extern "C" void __cilkrts_cilk_for_64(void*, void*, uint64_t, int32_t);
  36. #ifndef TARGET_WINNT
  37. #pragma weak __cilkrts_cilk_for_32
  38. #pragma weak __cilkrts_cilk_for_64
  39. #endif // TARGET_WINNT
  40. #ifdef TARGET_WINNT
  41. #define MYO_TABLE_END_MARKER() reinterpret_cast<const char*>(-1)
  42. #else // TARGET_WINNT
  43. #define MYO_TABLE_END_MARKER() reinterpret_cast<const char*>(0)
  44. #endif // TARGET_WINNT
  45. class MyoWrapper {
  46. public:
  47. MyoWrapper() : m_lib_handle(0), m_is_available(false)
  48. {}
  49. bool is_available() const {
  50. return m_is_available;
  51. }
  52. bool LoadLibrary(void);
  53. // unloads the library
  54. void UnloadLibrary(void) {
  55. // if (m_lib_handle != 0) {
  56. // DL_close(m_lib_handle);
  57. // m_lib_handle = 0;
  58. // }
  59. }
  60. // Wrappers for MYO client functions
  61. void LibInit(void *arg, void *func) const {
  62. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myoinit,
  63. "%s(%p, %p)\n", __func__, arg, func);
  64. CheckResult(__func__, m_lib_init(arg, func));
  65. }
  66. void LibFini(void) const {
  67. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myofini, "%s()\n", __func__);
  68. m_lib_fini();
  69. }
  70. void* SharedMalloc(size_t size) const {
  71. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myosharedmalloc,
  72. "%s(%lld)\n", __func__, size);
  73. return m_shared_malloc(size);
  74. }
  75. void SharedFree(void *ptr) const {
  76. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myosharedfree,
  77. "%s(%p)\n", __func__, ptr);
  78. m_shared_free(ptr);
  79. }
  80. void* SharedAlignedMalloc(size_t size, size_t align) const {
  81. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myosharedalignedmalloc,
  82. "%s(%lld, %lld)\n", __func__, size, align);
  83. return m_shared_aligned_malloc(size, align);
  84. }
  85. void SharedAlignedFree(void *ptr) const {
  86. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myosharedalignedfree,
  87. "%s(%p)\n", __func__, ptr);
  88. m_shared_aligned_free(ptr);
  89. }
  90. void Acquire(void) const {
  91. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myoacquire,
  92. "%s()\n", __func__);
  93. CheckResult(__func__, m_acquire());
  94. }
  95. void Release(void) const {
  96. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myorelease,
  97. "%s()\n", __func__);
  98. CheckResult(__func__, m_release());
  99. }
  100. void HostVarTablePropagate(void *table, int num_entries) const {
  101. OFFLOAD_DEBUG_TRACE(4, "%s(%p, %d)\n", __func__, table, num_entries);
  102. CheckResult(__func__, m_host_var_table_propagate(table, num_entries));
  103. }
  104. void HostFptrTableRegister(void *table, int num_entries,
  105. int ordered) const {
  106. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_myoregister,
  107. "%s(%p, %d, %d)\n", __func__, table,
  108. num_entries, ordered);
  109. CheckResult(__func__,
  110. m_host_fptr_table_register(table, num_entries, ordered));
  111. }
  112. void RemoteThunkCall(void *thunk, void *args, int device) {
  113. OFFLOAD_DEBUG_TRACE(4, "%s(%p, %p, %d)\n", __func__, thunk, args,
  114. device);
  115. CheckResult(__func__, m_remote_thunk_call(thunk, args, device));
  116. }
  117. MyoiRFuncCallHandle RemoteCall(char *func, void *args, int device) const {
  118. OFFLOAD_DEBUG_TRACE(4, "%s(%s, %p, %d)\n", __func__, func, args,
  119. device);
  120. return m_remote_call(func, args, device);
  121. }
  122. void GetResult(MyoiRFuncCallHandle handle) const {
  123. OFFLOAD_DEBUG_TRACE(4, "%s(%p)\n", __func__, handle);
  124. CheckResult(__func__, m_get_result(handle));
  125. }
  126. private:
  127. void CheckResult(const char *func, MyoError error) const {
  128. if (error != MYO_SUCCESS) {
  129. LIBOFFLOAD_ERROR(c_myowrapper_checkresult, func, error);
  130. exit(1);
  131. }
  132. }
  133. private:
  134. void* m_lib_handle;
  135. bool m_is_available;
  136. // pointers to functions from myo library
  137. MyoError (*m_lib_init)(void*, void*);
  138. void (*m_lib_fini)(void);
  139. void* (*m_shared_malloc)(size_t);
  140. void (*m_shared_free)(void*);
  141. void* (*m_shared_aligned_malloc)(size_t, size_t);
  142. void (*m_shared_aligned_free)(void*);
  143. MyoError (*m_acquire)(void);
  144. MyoError (*m_release)(void);
  145. MyoError (*m_host_var_table_propagate)(void*, int);
  146. MyoError (*m_host_fptr_table_register)(void*, int, int);
  147. MyoError (*m_remote_thunk_call)(void*, void*, int);
  148. MyoiRFuncCallHandle (*m_remote_call)(char*, void*, int);
  149. MyoError (*m_get_result)(MyoiRFuncCallHandle);
  150. };
  151. bool MyoWrapper::LoadLibrary(void)
  152. {
  153. #ifndef TARGET_WINNT
  154. const char *lib_name = "libmyo-client.so";
  155. #else // TARGET_WINNT
  156. const char *lib_name = "myo-client.dll";
  157. #endif // TARGET_WINNT
  158. OFFLOAD_DEBUG_TRACE(2, "Loading MYO library %s ...\n", lib_name);
  159. m_lib_handle = DL_open(lib_name);
  160. if (m_lib_handle == 0) {
  161. OFFLOAD_DEBUG_TRACE(2, "Failed to load the library. errno = %d\n",
  162. errno);
  163. return false;
  164. }
  165. m_lib_init = (MyoError (*)(void*, void*))
  166. DL_sym(m_lib_handle, "myoiLibInit", MYO_VERSION1);
  167. if (m_lib_init == 0) {
  168. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  169. "myoiLibInit");
  170. UnloadLibrary();
  171. return false;
  172. }
  173. m_lib_fini = (void (*)(void))
  174. DL_sym(m_lib_handle, "myoiLibFini", MYO_VERSION1);
  175. if (m_lib_fini == 0) {
  176. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  177. "myoiLibFini");
  178. UnloadLibrary();
  179. return false;
  180. }
  181. m_shared_malloc = (void* (*)(size_t))
  182. DL_sym(m_lib_handle, "myoSharedMalloc", MYO_VERSION1);
  183. if (m_shared_malloc == 0) {
  184. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  185. "myoSharedMalloc");
  186. UnloadLibrary();
  187. return false;
  188. }
  189. m_shared_free = (void (*)(void*))
  190. DL_sym(m_lib_handle, "myoSharedFree", MYO_VERSION1);
  191. if (m_shared_free == 0) {
  192. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  193. "myoSharedFree");
  194. UnloadLibrary();
  195. return false;
  196. }
  197. m_shared_aligned_malloc = (void* (*)(size_t, size_t))
  198. DL_sym(m_lib_handle, "myoSharedAlignedMalloc", MYO_VERSION1);
  199. if (m_shared_aligned_malloc == 0) {
  200. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  201. "myoSharedAlignedMalloc");
  202. UnloadLibrary();
  203. return false;
  204. }
  205. m_shared_aligned_free = (void (*)(void*))
  206. DL_sym(m_lib_handle, "myoSharedAlignedFree", MYO_VERSION1);
  207. if (m_shared_aligned_free == 0) {
  208. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  209. "myoSharedAlignedFree");
  210. UnloadLibrary();
  211. return false;
  212. }
  213. m_acquire = (MyoError (*)(void))
  214. DL_sym(m_lib_handle, "myoAcquire", MYO_VERSION1);
  215. if (m_acquire == 0) {
  216. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  217. "myoAcquire");
  218. UnloadLibrary();
  219. return false;
  220. }
  221. m_release = (MyoError (*)(void))
  222. DL_sym(m_lib_handle, "myoRelease", MYO_VERSION1);
  223. if (m_release == 0) {
  224. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  225. "myoRelease");
  226. UnloadLibrary();
  227. return false;
  228. }
  229. m_host_var_table_propagate = (MyoError (*)(void*, int))
  230. DL_sym(m_lib_handle, "myoiHostVarTablePropagate", MYO_VERSION1);
  231. if (m_host_var_table_propagate == 0) {
  232. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  233. "myoiHostVarTablePropagate");
  234. UnloadLibrary();
  235. return false;
  236. }
  237. m_host_fptr_table_register = (MyoError (*)(void*, int, int))
  238. DL_sym(m_lib_handle, "myoiHostFptrTableRegister", MYO_VERSION1);
  239. if (m_host_fptr_table_register == 0) {
  240. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  241. "myoiHostFptrTableRegister");
  242. UnloadLibrary();
  243. return false;
  244. }
  245. m_remote_thunk_call = (MyoError (*)(void*, void*, int))
  246. DL_sym(m_lib_handle, "myoiRemoteThunkCall", MYO_VERSION1);
  247. if (m_remote_thunk_call == 0) {
  248. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  249. "myoiRemoteThunkCall");
  250. UnloadLibrary();
  251. return false;
  252. }
  253. m_remote_call = (MyoiRFuncCallHandle (*)(char*, void*, int))
  254. DL_sym(m_lib_handle, "myoiRemoteCall", MYO_VERSION1);
  255. if (m_remote_call == 0) {
  256. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  257. "myoiRemoteCall");
  258. UnloadLibrary();
  259. return false;
  260. }
  261. m_get_result = (MyoError (*)(MyoiRFuncCallHandle))
  262. DL_sym(m_lib_handle, "myoiGetResult", MYO_VERSION1);
  263. if (m_get_result == 0) {
  264. OFFLOAD_DEBUG_TRACE(2, "Failed to find %s in MYO library\n",
  265. "myoiGetResult");
  266. UnloadLibrary();
  267. return false;
  268. }
  269. OFFLOAD_DEBUG_TRACE(2, "The library was successfully loaded\n");
  270. m_is_available = true;
  271. return true;
  272. }
  273. static bool myo_is_available;
  274. static MyoWrapper myo_wrapper;
  275. struct MyoTable
  276. {
  277. MyoTable(SharedTableEntry *tab, int len) : var_tab(tab), var_tab_len(len)
  278. {}
  279. SharedTableEntry* var_tab;
  280. int var_tab_len;
  281. };
  282. typedef std::list<MyoTable> MyoTableList;
  283. static MyoTableList __myo_table_list;
  284. static mutex_t __myo_table_lock;
  285. static bool __myo_tables = false;
  286. static void __offload_myo_shared_table_register(SharedTableEntry *entry);
  287. static void __offload_myo_shared_init_table_register(InitTableEntry* entry);
  288. static void __offload_myo_fptr_table_register(FptrTableEntry *entry);
  289. static void __offload_myoLoadLibrary_once(void)
  290. {
  291. if (__offload_init_library()) {
  292. myo_wrapper.LoadLibrary();
  293. }
  294. }
  295. static bool __offload_myoLoadLibrary(void)
  296. {
  297. static OffloadOnceControl ctrl = OFFLOAD_ONCE_CONTROL_INIT;
  298. __offload_run_once(&ctrl, __offload_myoLoadLibrary_once);
  299. return myo_wrapper.is_available();
  300. }
  301. static void __offload_myoInit_once(void)
  302. {
  303. if (!__offload_myoLoadLibrary()) {
  304. return;
  305. }
  306. // initialize all devices
  307. for (int i = 0; i < mic_engines_total; i++) {
  308. mic_engines[i].init();
  309. }
  310. // load and initialize MYO library
  311. OFFLOAD_DEBUG_TRACE(2, "Initializing MYO library ...\n");
  312. COIEVENT events[MIC_ENGINES_MAX];
  313. MyoiUserParams params[MIC_ENGINES_MAX+1];
  314. // load target library to all devices
  315. for (int i = 0; i < mic_engines_total; i++) {
  316. mic_engines[i].init_myo(&events[i]);
  317. params[i].type = MYOI_USERPARAMS_DEVID;
  318. params[i].nodeid = mic_engines[i].get_physical_index() + 1;
  319. }
  320. params[mic_engines_total].type = MYOI_USERPARAMS_LAST_MSG;
  321. // initialize myo runtime on host
  322. myo_wrapper.LibInit(params, 0);
  323. // wait for the target init calls to finish
  324. COIRESULT res;
  325. res = COI::EventWait(mic_engines_total, events, -1, 1, 0, 0);
  326. if (res != COI_SUCCESS) {
  327. LIBOFFLOAD_ERROR(c_event_wait, res);
  328. exit(1);
  329. }
  330. myo_is_available = true;
  331. OFFLOAD_DEBUG_TRACE(2, "Initializing MYO library ... done\n");
  332. }
  333. static bool __offload_myoInit(void)
  334. {
  335. static OffloadOnceControl ctrl = OFFLOAD_ONCE_CONTROL_INIT;
  336. __offload_run_once(&ctrl, __offload_myoInit_once);
  337. // register pending shared var tables
  338. if (myo_is_available && __myo_tables) {
  339. mutex_locker_t locker(__myo_table_lock);
  340. if (__myo_tables) {
  341. // Register tables with MYO so it can propagate to target.
  342. for(MyoTableList::const_iterator it = __myo_table_list.begin();
  343. it != __myo_table_list.end(); ++it) {
  344. #ifdef TARGET_WINNT
  345. for (SharedTableEntry *entry = it->var_tab;
  346. entry->varName != MYO_TABLE_END_MARKER(); entry++) {
  347. if (entry->varName == 0) {
  348. continue;
  349. }
  350. myo_wrapper.HostVarTablePropagate(entry, 1);
  351. }
  352. #else // TARGET_WINNT
  353. myo_wrapper.HostVarTablePropagate(it->var_tab,
  354. it->var_tab_len);
  355. #endif // TARGET_WINNT
  356. }
  357. __myo_table_list.clear();
  358. __myo_tables = false;
  359. }
  360. }
  361. return myo_is_available;
  362. }
  363. static bool shared_table_entries(
  364. SharedTableEntry *entry
  365. )
  366. {
  367. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, entry);
  368. for (; entry->varName != MYO_TABLE_END_MARKER(); entry++) {
  369. #ifdef TARGET_WINNT
  370. if (entry->varName == 0) {
  371. continue;
  372. }
  373. #endif // TARGET_WINNT
  374. return true;
  375. }
  376. return false;
  377. }
  378. static bool fptr_table_entries(
  379. FptrTableEntry *entry
  380. )
  381. {
  382. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, entry);
  383. for (; entry->funcName != MYO_TABLE_END_MARKER(); entry++) {
  384. #ifdef TARGET_WINNT
  385. if (entry->funcName == 0) {
  386. continue;
  387. }
  388. #endif // TARGET_WINNT
  389. return true;
  390. }
  391. return false;
  392. }
  393. extern "C" void __offload_myoRegisterTables(
  394. InitTableEntry* init_table,
  395. SharedTableEntry *shared_table,
  396. FptrTableEntry *fptr_table
  397. )
  398. {
  399. // check whether we need to initialize MYO library. It is
  400. // initialized only if at least one myo table is not empty
  401. if (shared_table_entries(shared_table) || fptr_table_entries(fptr_table)) {
  402. // make sure myo library is loaded
  403. __offload_myoLoadLibrary();
  404. // register tables
  405. __offload_myo_shared_table_register(shared_table);
  406. __offload_myo_fptr_table_register(fptr_table);
  407. __offload_myo_shared_init_table_register(init_table);
  408. }
  409. }
  410. void __offload_myoFini(void)
  411. {
  412. if (myo_is_available) {
  413. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  414. COIEVENT events[MIC_ENGINES_MAX];
  415. // kick off myoiLibFini calls on all devices
  416. for (int i = 0; i < mic_engines_total; i++) {
  417. mic_engines[i].fini_myo(&events[i]);
  418. }
  419. // cleanup myo runtime on host
  420. myo_wrapper.LibFini();
  421. // wait for the target fini calls to finish
  422. COIRESULT res;
  423. res = COI::EventWait(mic_engines_total, events, -1, 1, 0, 0);
  424. if (res != COI_SUCCESS) {
  425. LIBOFFLOAD_ERROR(c_event_wait, res);
  426. exit(1);
  427. }
  428. }
  429. }
  430. static void __offload_myo_shared_table_register(
  431. SharedTableEntry *entry
  432. )
  433. {
  434. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, entry);
  435. SharedTableEntry *start = entry;
  436. int entries = 0;
  437. // allocate shared memory for vars
  438. for (; entry->varName != MYO_TABLE_END_MARKER(); entry++) {
  439. #ifdef TARGET_WINNT
  440. if (entry->varName == 0) {
  441. OFFLOAD_DEBUG_TRACE(4, "skip registering a NULL MyoSharedTable entry\n");
  442. continue;
  443. }
  444. #endif // TARGET_WINNT
  445. OFFLOAD_DEBUG_TRACE(4, "registering MyoSharedTable entry for %s @%p\n",
  446. entry->varName, entry);
  447. // Invoke the function to create shared memory
  448. reinterpret_cast<void(*)(void)>(entry->sharedAddr)();
  449. entries++;
  450. }
  451. // and table to the list if it is not empty
  452. if (entries > 0) {
  453. mutex_locker_t locker(__myo_table_lock);
  454. __myo_table_list.push_back(MyoTable(start, entries));
  455. __myo_tables = true;
  456. }
  457. }
  458. static void __offload_myo_shared_init_table_register(InitTableEntry* entry)
  459. {
  460. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, entry);
  461. #ifdef TARGET_WINNT
  462. for (; entry->funcName != MYO_TABLE_END_MARKER(); entry++) {
  463. if (entry->funcName == 0) {
  464. OFFLOAD_DEBUG_TRACE(4, "skip registering a NULL MyoSharedInit entry\n");
  465. continue;
  466. }
  467. // Invoke the function to init the shared memory
  468. entry->func();
  469. }
  470. #else // TARGET_WINNT
  471. for (; entry->func != 0; entry++) {
  472. // Invoke the function to init the shared memory
  473. entry->func();
  474. }
  475. #endif // TARGET_WINNT
  476. }
  477. static void __offload_myo_fptr_table_register(
  478. FptrTableEntry *entry
  479. )
  480. {
  481. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, entry);
  482. FptrTableEntry *start = entry;
  483. int entries = 0;
  484. for (; entry->funcName != MYO_TABLE_END_MARKER(); entry++) {
  485. #ifdef TARGET_WINNT
  486. if (entry->funcName == 0) {
  487. OFFLOAD_DEBUG_TRACE(4, "skip registering a NULL MyoFptrTable entry\n");
  488. continue;
  489. }
  490. #endif // TARGET_WINNT
  491. if (!myo_wrapper.is_available()) {
  492. *(static_cast<void**>(entry->localThunkAddr)) = entry->funcAddr;
  493. }
  494. OFFLOAD_DEBUG_TRACE(4, "registering MyoFptrTable entry for %s @%p\n",
  495. entry->funcName, entry);
  496. #ifdef TARGET_WINNT
  497. if (myo_wrapper.is_available()) {
  498. myo_wrapper.HostFptrTableRegister(entry, 1, false);
  499. }
  500. #endif // TARGET_WINNT
  501. entries++;
  502. }
  503. #ifndef TARGET_WINNT
  504. if (myo_wrapper.is_available() && entries > 0) {
  505. myo_wrapper.HostFptrTableRegister(start, entries, false);
  506. }
  507. #endif // TARGET_WINNT
  508. }
  509. extern "C" int __offload_myoIsAvailable(int target_number)
  510. {
  511. OFFLOAD_DEBUG_TRACE(3, "%s(%d)\n", __func__, target_number);
  512. if (target_number >= -2) {
  513. bool is_default_number = (target_number == -2);
  514. if (__offload_myoInit()) {
  515. if (target_number >= 0) {
  516. // User provided the device number
  517. int num = target_number % mic_engines_total;
  518. // reserve device in ORSL
  519. target_number = ORSL::reserve(num) ? num : -1;
  520. }
  521. else {
  522. // try to use device 0
  523. target_number = ORSL::reserve(0) ? 0 : -1;
  524. }
  525. // make sure device is initialized
  526. if (target_number >= 0) {
  527. mic_engines[target_number].init();
  528. }
  529. }
  530. else {
  531. // fallback to CPU
  532. target_number = -1;
  533. }
  534. if (target_number < 0 && !is_default_number) {
  535. LIBOFFLOAD_ERROR(c_device_is_not_available);
  536. exit(1);
  537. }
  538. }
  539. else {
  540. LIBOFFLOAD_ERROR(c_invalid_device_number);
  541. exit(1);
  542. }
  543. return target_number;
  544. }
  545. extern "C" void __offload_myoiRemoteIThunkCall(
  546. void *thunk,
  547. void *arg,
  548. int target_number
  549. )
  550. {
  551. OFFLOAD_DEBUG_TRACE(3, "%s(%p, %p, %d)\n", __func__, thunk, arg,
  552. target_number);
  553. myo_wrapper.Release();
  554. myo_wrapper.RemoteThunkCall(thunk, arg, target_number);
  555. myo_wrapper.Acquire();
  556. ORSL::release(target_number);
  557. }
  558. extern "C" void* _Offload_shared_malloc(size_t size)
  559. {
  560. OFFLOAD_DEBUG_TRACE(3, "%s(%lld)\n", __func__, size);
  561. if (__offload_myoLoadLibrary()) {
  562. return myo_wrapper.SharedMalloc(size);
  563. }
  564. else {
  565. return malloc(size);
  566. }
  567. }
  568. extern "C" void _Offload_shared_free(void *ptr)
  569. {
  570. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, ptr);
  571. if (__offload_myoLoadLibrary()) {
  572. myo_wrapper.SharedFree(ptr);
  573. }
  574. else {
  575. free(ptr);
  576. }
  577. }
  578. extern "C" void* _Offload_shared_aligned_malloc(size_t size, size_t align)
  579. {
  580. OFFLOAD_DEBUG_TRACE(3, "%s(%lld, %lld)\n", __func__, size, align);
  581. if (__offload_myoLoadLibrary()) {
  582. return myo_wrapper.SharedAlignedMalloc(size, align);
  583. }
  584. else {
  585. if (align < sizeof(void*)) {
  586. align = sizeof(void*);
  587. }
  588. return _mm_malloc(size, align);
  589. }
  590. }
  591. extern "C" void _Offload_shared_aligned_free(void *ptr)
  592. {
  593. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, ptr);
  594. if (__offload_myoLoadLibrary()) {
  595. myo_wrapper.SharedAlignedFree(ptr);
  596. }
  597. else {
  598. _mm_free(ptr);
  599. }
  600. }
  601. extern "C" void __intel_cilk_for_32_offload(
  602. int size,
  603. void (*copy_constructor)(void*, void*),
  604. int target_number,
  605. void *raddr,
  606. void *closure_object,
  607. unsigned int iters,
  608. unsigned int grain_size)
  609. {
  610. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  611. target_number = __offload_myoIsAvailable(target_number);
  612. if (target_number >= 0) {
  613. struct S {
  614. void *M1;
  615. unsigned int M2;
  616. unsigned int M3;
  617. char closure[];
  618. } *args;
  619. args = (struct S*) _Offload_shared_malloc(sizeof(struct S) + size);
  620. if (args == NULL)
  621. LIBOFFLOAD_ERROR(c_malloc);
  622. args->M1 = raddr;
  623. args->M2 = iters;
  624. args->M3 = grain_size;
  625. if (copy_constructor == 0) {
  626. memcpy(args->closure, closure_object, size);
  627. }
  628. else {
  629. copy_constructor(args->closure, closure_object);
  630. }
  631. myo_wrapper.Release();
  632. myo_wrapper.GetResult(
  633. myo_wrapper.RemoteCall("__intel_cilk_for_32_offload",
  634. args, target_number)
  635. );
  636. myo_wrapper.Acquire();
  637. _Offload_shared_free(args);
  638. ORSL::release(target_number);
  639. }
  640. else {
  641. __cilkrts_cilk_for_32(raddr,
  642. closure_object,
  643. iters,
  644. grain_size);
  645. }
  646. }
  647. extern "C" void __intel_cilk_for_64_offload(
  648. int size,
  649. void (*copy_constructor)(void*, void*),
  650. int target_number,
  651. void *raddr,
  652. void *closure_object,
  653. uint64_t iters,
  654. uint64_t grain_size)
  655. {
  656. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  657. target_number = __offload_myoIsAvailable(target_number);
  658. if (target_number >= 0) {
  659. struct S {
  660. void *M1;
  661. uint64_t M2;
  662. uint64_t M3;
  663. char closure[];
  664. } *args;
  665. args = (struct S*) _Offload_shared_malloc(sizeof(struct S) + size);
  666. if (args == NULL)
  667. LIBOFFLOAD_ERROR(c_malloc);
  668. args->M1 = raddr;
  669. args->M2 = iters;
  670. args->M3 = grain_size;
  671. if (copy_constructor == 0) {
  672. memcpy(args->closure, closure_object, size);
  673. }
  674. else {
  675. copy_constructor(args->closure, closure_object);
  676. }
  677. myo_wrapper.Release();
  678. myo_wrapper.GetResult(
  679. myo_wrapper.RemoteCall("__intel_cilk_for_64_offload", args,
  680. target_number)
  681. );
  682. myo_wrapper.Acquire();
  683. _Offload_shared_free(args);
  684. ORSL::release(target_number);
  685. }
  686. else {
  687. __cilkrts_cilk_for_64(raddr,
  688. closure_object,
  689. iters,
  690. grain_size);
  691. }
  692. }