myoimpl.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright 2010-2013 Intel Corporation.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, version 2.1.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA.
  17. *
  18. * Disclaimer: The codes contained in these modules may be specific
  19. * to the Intel Software Development Platform codenamed Knights Ferry,
  20. * and the Intel product codenamed Knights Corner, and are not backward
  21. * compatible with other Intel products. Additionally, Intel will NOT
  22. * support the codes or instruction set in future products.
  23. *
  24. * Intel offers no warranty of any kind regarding the code. This code is
  25. * licensed on an "AS IS" basis and Intel is not obligated to provide
  26. * any support, assistance, installation, training, or other services
  27. * of any kind. Intel is also not obligated to provide any updates,
  28. * enhancements or extensions. Intel specifically disclaims any warranty
  29. * of merchantability, non-infringement, fitness for any particular
  30. * purpose, and any other warranty.
  31. *
  32. * Further, Intel disclaims all liability of any kind, including but
  33. * not limited to liability for infringement of any proprietary rights,
  34. * relating to the use of the code, even if Intel is notified of the
  35. * possibility of such liability. Except as expressly stated in an Intel
  36. * license agreement provided with this code and agreed upon with Intel,
  37. * no license, express or implied, by estoppel or otherwise, to any
  38. * intellectual property rights is granted herein.
  39. */
  40. /**
  41. Description:
  42. Define APIs of MYO for compiler or pre-processor to transfer original programs.
  43. */
  44. #ifndef _MYO_IMPL_H_
  45. #define _MYO_IMPL_H_
  46. /** @ingroup MYO
  47. * @addtogroup MYOIMPL_API
  48. @{
  49. * @file myoimpl.h
  50. */
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #define EXTERN_C extern "C"
  54. #else
  55. #define EXTERN_C /* nothing */
  56. #endif
  57. # define MYOACCESSAPI /* nothing */
  58. #ifdef DEFINE_ARENA_API_CILK_SHARED
  59. #define CILK_SHARED _Cilk_shared
  60. #else
  61. #define CILK_SHARED /* nothing */
  62. #endif
  63. /* **************************************************************************** *\
  64. APIs to enable functions being remotely called
  65. \* **************************************************************************** */
  66. typedef void *(*MyoiRemoteFuncType)(void *);
  67. /** @fn extern MyoError myoiRemoteFuncRegister(MyoiRemoteFuncType in_pFuncAddr,
  68. * const char *in_pFuncName)
  69. * @brief Register a function so that it can be remotely called. This should be
  70. * done in myoiUserInit or before calling myoiLibInit. After myoiLibInit,
  71. * there will be a table on all peers, which contains the information for
  72. * all remotely callable functions.
  73. *
  74. * @param in_pWrapFuncAddr address of the wrapper function.
  75. * @param in_pFuncName name of the function.
  76. * @return
  77. * MYO_SUCCESS; or
  78. * an error number to indicate the error.
  79. **/
  80. MYOACCESSAPI
  81. MyoError myoiRemoteFuncRegister(MyoiRemoteFuncType in_pFuncAddr,
  82. const char *in_pFuncName);
  83. /** @fn extern MyoError myoiRemoteFuncLookupByName(char *in_pFuncName,
  84. * MyoiRemoteFuncType *out_pWrapFuncAddr)
  85. * @brief Get the address of the wrapper function by looking up the table
  86. * by name. This API can be used when assigning a function pointer to
  87. * remotely callable functions.
  88. *
  89. * @param in_pFuncName name of the function.
  90. * @param out_pWrapFuncAddr address of the wrapper function.
  91. * @return
  92. * MYO_SUCCESS; or
  93. * an error number to indicate the error.
  94. **/
  95. MYOACCESSAPI MyoError myoiRemoteFuncLookupByName(char *in_pFuncName,
  96. MyoiRemoteFuncType *out_pWrapFuncAddr);
  97. /** @fn extern MyoError myoiRemoteFuncLookupByAddr(MyoiRemoteFuncType
  98. * in_pWrapFuncAddr,char **out_pFuncName)
  99. * @brief Get the name of a remote function by looking up the table by
  100. * the address. This API can be used when calling a remotely callable
  101. * function by a function pointer.
  102. *
  103. * @param in_pWrapFuncAddr address of the function.
  104. * @param out_pFuncName name of the function.
  105. * @return
  106. * MYO_SUCCESS; or
  107. * an error number to indicate the error.
  108. **/
  109. MYOACCESSAPI MyoError myoiRemoteFuncLookupByAddr(MyoiRemoteFuncType in_pWrapFuncAddr,
  110. char **out_pFuncName);
  111. //! Host Side Shared Function Pointer Entry Struct
  112. typedef struct {
  113. //! Function Name
  114. const char *funcName;
  115. //! Function Address
  116. void *funcAddr;
  117. //! Local Thunk Address
  118. void *localThunkAddr;
  119. } MyoiHostSharedFptrEntry;
  120. //! Target Side Shared Function Pointer Entry Struct
  121. typedef struct {
  122. //! Function Name
  123. const char *funcName;
  124. //! Function Address
  125. void *funcAddr;
  126. //! Wrap Function Address
  127. void *wrapFuncAddr;
  128. //! Locak Thunk Address
  129. void *localThunkAddr;
  130. } MyoiTargetSharedFptrEntry;
  131. /**
  132. * @cond INCLUDE_MYO_INTERNAL_DOCUMENTATION
  133. * @fn extern MyoError myoiHostFptrTableRegister(void *in_pAddrOfFptrTable,
  134. * int in_NumEntry, int in_Ordered)
  135. * @brief Register shared functions on host side. A 16 byte thunk will be
  136. * allocated for each function entry in non-coherent shared memory. The
  137. * thunk will contain a jump instruction to the local version of the
  138. * shared function, which is provided by the second item of the function
  139. * entry. Also, the address of the thunk will be stored to the 3rd item
  140. * of the function entry for Compiler usage.
  141. *
  142. * @param in_pAddrOfFptrTable start address of the shared function
  143. * table, assuming it follows the format of MyoiHostSharedFptrEntry.
  144. * @param in_NumEntry number of entry in the table.
  145. * @param in_Ordered whether the table is ordered by function name.
  146. * @return
  147. * MYO_SUCCESS; or
  148. * an error number to indicate the error.
  149. **/
  150. MYOACCESSAPI
  151. MyoError myoiHostFptrTableRegister(
  152. void *in_pAddrOfFptrTable, int in_NumEntry, int in_Ordered);
  153. /** @fn extern MyoError myoiTargetFptrTableRegister(void *in_pAddrOfFptrTable,
  154. * int in_NumEntry, int in_Ordered)
  155. * @brief Register shared functions on target side. This function is the
  156. * same as myoiHostFptrTableRegister, except it does not need to allocate
  157. * thunks from non-coherent shared memory for each function entry, but
  158. * instead looks up this information from a table retrieved from the
  159. * Host side.
  160. *
  161. * @param in_pAddrOfFptrTable start address of the shared function
  162. * table, assuming it follows the format of MyoiTargetSharedFptrEntry.
  163. * @param in_NumEntry number of entry in the table.
  164. * @param in_Ordered whether the table is ordered by function name.
  165. * @return
  166. * MYO_SUCCESS; or
  167. * an error number to indicate the error.
  168. **/
  169. extern MyoError myoiTargetFptrTableRegister(
  170. void *in_pAddrOfFptrTable, int in_NumEntry, int in_Ordered);
  171. /**
  172. * @endcond
  173. **/
  174. /* *************************************************************************** *\
  175. APIs for remote function calls
  176. \* *************************************************************************** */
  177. typedef void * MyoiRFuncCallHandle;
  178. /** @fn extern MyoiRFuncCallHandle myoiRemoteCall(char *in_pFuncName,
  179. * void *in_pArgs, int in_deviceNum)
  180. * @brief Call a remote callable function. If there are multiple arguments
  181. * for the function, pack them to a shared buffer beforehand and take the
  182. * address of the shared buffer as this function. After receiving the call
  183. * requests from other peers, the arguments should be unpacked from the
  184. * shared buffer before calling the target function. The shared buffer
  185. * can also be used to store the return value of the function.
  186. *
  187. * @param in_pFuncName name of the function.
  188. * @param in_pArgs address of the shared buffer.
  189. * @return
  190. * Handle used to check the result.
  191. **/
  192. MYOACCESSAPI
  193. MyoiRFuncCallHandle myoiRemoteCall(const char *in_pFuncName, void *in_pArgs, int in_deviceNum);
  194. /**
  195. * @cond INCLUDE_MYO_INTERNAL_DOCUMENTATION
  196. * @fn extern MyoError myoiRemoteThunkCall(void *in_funcThunkAddr, void *in_pArgs, int in_deviceNum)
  197. * @brief Call a remote callable function. If there are multiple arguments for
  198. * the function, pack them to a shared buffer beforehand and take the address
  199. * of the shared buffer as this function. After receiving the call requests
  200. * from other peers, the arguments should be unpacked from the shared buffer
  201. * before calling the target function. The shared buffer can also be used to
  202. * store the return value of the function.
  203. *
  204. * @param in_funcThunkAddr pointer to function thunk in the non-coherent
  205. * shared memory.
  206. * @param in_pArgs address of the shared buffer.
  207. * @PARAM in_deviceNum: device ID (0-N-1) for the MIC device to run
  208. * function call. -1 request causes MYO to schedule an available device.
  209. * For RPC from device to host, in_deviceNum should always be -1.
  210. * @return
  211. * MYO_SUCCESS; or
  212. * an error number to indicate the error.
  213. **/
  214. MYOACCESSAPI
  215. MyoError myoiRemoteThunkCall(void *in_funcThunkAddr, void *in_pArgs, int in_deviceNum);
  216. /**
  217. * @endcond
  218. **/
  219. /** @fn extern MyoError myoiCheckResult(MyoiRFuncCallHandle in_Handle)
  220. * @brief Check whether the remote call is done.
  221. *
  222. * @param in_Handle handle of the remote call.
  223. * @return
  224. * MYO_SUCCESS (done); or
  225. * an error number to indicate the error.
  226. **/
  227. extern MyoError myoiCheckResult(MyoiRFuncCallHandle in_Handle);
  228. /** @fn extern MyoError myoiGetResult(MyoiRFuncCallHandle in_Handle)
  229. * @brief Wait till the remote call is done.
  230. *
  231. * @param in_Handle handle of the remote call.
  232. * @return
  233. * MYO_SUCCESS; or
  234. * an error number to indicate the error.
  235. **/
  236. MYOACCESSAPI
  237. MyoError myoiGetResult(MyoiRFuncCallHandle in_Handle);
  238. /* **************************************************************************** *\
  239. APIs related with shared variables.
  240. \* **************************************************************************** */
  241. /*
  242. * It is Compiler's responsibility to make sure all shared variables
  243. * located in shared memory space and have the same address in all sides.
  244. * However, it is hard for Compiler to do this. So we propose a solution
  245. * which is changing the definition of all shared variables and accessing
  246. * them indirectly, and making sure all shared variables pointing
  247. * to the same shared space on all sides. For example,
  248. *
  249. * "shared int a;" is changed to "shared int *a";
  250. * Also all the accesses to "a" is change to "*a".
  251. *
  252. * We suppose the shared memory for each shared variable is allocated on
  253. * host side by Compiler. For the upper example, Compiler can call:
  254. *
  255. * a = (shared int *) myoSharedMalloc(sizeof(shared int));
  256. *
  257. * Now the issue is how to make "a" on other sides also pointing to the
  258. * same shared memory on other sides. We provide two methods to do this.
  259. * They can be used in a hybrid way.
  260. */
  261. /*
  262. * The first method is calling myoiVarRegister for each shared variable
  263. * on all sides in myoiUserInit. On host side, we will get a table containing
  264. * a table containing the shared address and name of each shared variable.
  265. * After calling myoiUserInit, we will propagate the table to other sides.
  266. * On card side, after getting the table from host, myoiUserInit is called.
  267. * When calling myoiVarRegister in myoiUserInit, we will make local pointer
  268. * of each shared variable pointing to the same shared memory with the local
  269. * pointer on host side of the same shared variable pointing to.
  270. */
  271. /*
  272. * The second method suppose that Compiler already have a table on all sides.
  273. * On host side, the table contains the name and the shared address of each
  274. * shared variable. On card side, the table contains the name of each shared
  275. * variable and the address of the local pointer which will pointing to shared
  276. * memory space.
  277. *
  278. * On host side, Compiler generates a call to myoiHostVarTablePropagate
  279. * after initializing MYO runtime and making the host side table ready.
  280. * On card side, Compiler uses myoiMicVarTableRegister to tell
  281. * the runtime where the card side table is.
  282. *
  283. * Since there may be multiple libraries on card side for the same application,
  284. * myoiHostVarTablePropagate and myoiMicVarTableRegister can be called multiple
  285. * times and called simultaneously.
  286. *
  287. * Inside runtime, the local pointer of the same shared variable on all sides
  288. * will be make sure pointing to the same shared space by using the information
  289. * of the Compiler provided tables.
  290. */
  291. /*
  292. * Comipler knows the following two structures to make sure the var table
  293. * has the following format.
  294. */
  295. /*
  296. * This is structure of the Shared var table entry. This table contains
  297. * the shared address and name of each shared variable
  298. */
  299. /** @fn extern MyoError myoiVarRegister(void *in_pAddrOfLocalPtrToShared, char *in_pSVarName)
  300. * @brief Register shared variables. Call it on all sides in myoiUserInit.
  301. * On host side, make sure calling it after allocating shared memory for
  302. * the shared variables by calling myoSharedMalloc.
  303. *
  304. * @param in_pAddrOfLocalPtrToShared the address assigned by the compiler
  305. * for the shared variable, which is the address of a local pointer,
  306. * pointing to shared memory space.
  307. * @param in_pSVarName name of shared variable.
  308. * @return
  309. * MYO_SUCCESS; or
  310. * an error number to indicate the error.
  311. **/
  312. MYOACCESSAPI
  313. MyoError myoiVarRegister(
  314. void *in_pAddrOfLocalPtrToShared, const char *in_pSVarName);
  315. /*
  316. * Compiler knows the following two structures to make sure the var table
  317. * has the following format.
  318. */
  319. /**
  320. * This is structure of the Shared var table entry. This table contains
  321. * the shared address and name of each shared variable
  322. **/
  323. typedef struct {
  324. //! Variable Name
  325. const char *varName;
  326. //! Shared Address
  327. void *sharedAddr;
  328. } MyoiSharedVarEntry;
  329. //! Structure of the var table entry on host
  330. typedef struct {
  331. //! Variable Name
  332. const char *varName;
  333. //! Variable Size
  334. int size;
  335. //! Local pointer to Shared var
  336. void *ptrToLocalPtrToShared;
  337. } MyoiHostSharedVarEntry;
  338. //! Structure of the var table entry on card
  339. typedef struct {
  340. //! Variable Name
  341. const char *varName;
  342. //! Local pointer to Shared var
  343. void *ptrToLocalPtrToShared;
  344. } MyoiMicSharedVarEntry;
  345. /** @fn extern MyoError myoiHostVarTablePropagate(void *in_pAddrOfSVarTable, int in_NumEntry)
  346. * @brief Send the host side var table to the card side. Card side will also
  347. * have a copy of the host side var table after this propagation, although it
  348. * is in an internal format different than the original host side var table,
  349. * due to implementation convenience.
  350. *
  351. * @param in_pAddrOfSVarTable start address of the host side var table,
  352. * assuming it follows the format of MyoiSharedVarEntry.
  353. * @param in_NumEntry number of entry in the table.
  354. * @return
  355. * MYO_SUCCESS; or
  356. * an error number to indicate the error.
  357. **/
  358. MYOACCESSAPI MyoError myoiHostVarTablePropagate(
  359. void *in_pAddrOfSVarTable, int in_NumEntry);
  360. /**
  361. * @cond INCLUDE_MYO_INTERNAL_DOCUMENTATION
  362. * @fn extern MyoError myoiMicVarTableRegister(void *in_pAddrOfSVarTable, int in_NumEntry)
  363. * @brief Tell the runtime where the card side table is.
  364. *
  365. * @param in_pAddrOfSVarTable start address of the card side var
  366. * table, assuming it follows the format of MyoiMicSharedVarEntry.
  367. * @param in_NumEntry number of entry in the table.
  368. * @return
  369. * MYO_SUCCESS; or
  370. * an error number to indicate the error.
  371. **/
  372. extern MyoError myoiMicVarTableRegister(
  373. void *in_pAddrOfSVarTable, int in_NumEntry);
  374. /**
  375. * @endcond
  376. **/
  377. /** @fn MyoError myoiHostSharedMallocTableRegister(void *in_pAddrOfSVarTable, int in_NumEntry, int in_Ordered)
  378. * @brief Allocate shared memory for all shared variables in the table.
  379. * Also update local address of the shared variable with new shared address.
  380. *
  381. * @param in_pAddrOfSVarTable start address of the shared variable table,
  382. * assuming it follows the format of MyoiHostSharedVarEntry.
  383. * @param in_NumEntry number of entry in the table.
  384. * @param in_Ordered whether the table ordered by name.
  385. * @return
  386. * MYO_SUCCESS; or
  387. * an error number to indicate the error.
  388. **/
  389. MYOACCESSAPI
  390. MyoError myoiHostSharedMallocTableRegister(
  391. void *in_pAddrOfSVarTable, int in_NumEntry, int in_Ordered);
  392. /** @fn extern MyoError myoiTargetSharedMallocTableRegister(void *in_pAddrOfSVarTable, int in_NumEntry, int in_Ordered)
  393. * @brief Register the shared variables on the target side.
  394. *
  395. * @param in_pAddrOfSVarTable start address of the shared varaible table,
  396. * assuming it follows the format of MyoiMicSharedVarEntry.
  397. * @param in_NumEntry number of entry in the table.
  398. * @param in_Ordered whether the table ordered by name.
  399. * @return
  400. * MYO_SUCCESS; or
  401. * an error number to indicate the error.
  402. **/
  403. extern MyoError myoiTargetSharedMallocTableRegister(
  404. void *in_pAddrOfSVarTable, int in_NumEntry, int in_Ordered);
  405. /** @fn MyoError myoiLibInit(void * in_args, void (*userInitFunc))
  406. * @brief Init entry of the MYO library responsible for initializing
  407. * the runtime.
  408. *
  409. * @param in_args mechanism to pass arguments to the Initialization
  410. * routine. The default value of NULL would mean the host is blocked
  411. * on the completion of myoiLibInit() on all nodes. A subset of the
  412. * installed cards can be intialized by passing an array of
  413. * MyoiUserParams. For example, in a system with two cards, to run a
  414. * MYO application only on the second card, intialize the array as
  415. * follows:
  416. * @code
  417. * MyoiUserParams UserParas[64];
  418. * UserParas[0].type = MYOI_USERPARAMS_DEVID;
  419. * UserParas[0].nodeid = 2;
  420. * UserParas[1].type = MYOI_USERPARAMS_LAST_MSG;
  421. * if(MYO_SUCCESS != myoiLibInit(&UserParas, (void*)&myoiUserInit)) {
  422. * printf("Failed to initialize MYO runtime\n");
  423. * return -1;
  424. * }
  425. * @endcode
  426. * This intialization is required only in the client/host side
  427. * of the application. The server/card side executable should be
  428. * executed only on the second card in this case.
  429. *
  430. * @param userInitFunc Shared variables and remote funtions are
  431. * registered in this routine, which is called by the runtime during
  432. * library initialization.
  433. * @return
  434. * MYO_SUCCESS;
  435. * MYO_ERROR;
  436. **/
  437. MYOACCESSAPI
  438. MyoError myoiLibInit(void * in_args, void *userInitFunc /*userInitFunc must be: MyoError (*userInitFunc)(void) */);
  439. /** @fn void myoiLibFini()
  440. * @brief Finalize the MYO library, all resources held by the runtime are
  441. * released by this routine.
  442. *
  443. * @return
  444. **/
  445. MYOACCESSAPI
  446. void myoiLibFini();
  447. /* *************************************************************************** *\
  448. APIs to set shared memory space consistent/non-consistent.
  449. \* *************************************************************************** */
  450. /** @fn extern MyoError myoiSetMemNonConsistent(void *in_pAddr, size_t in_Size)
  451. * @brief Set part of the shared memory space to be non-consistent, which
  452. * means that the consistency of this part of shared memory space does not
  453. * need to be maintained between HOST and cards.
  454. *
  455. * @param in_pAddr The start address of the specified shared memory space;
  456. * @param in_Size The size of the specified shared memory space;
  457. * @return
  458. * MYO_SUCCESS; or
  459. * an error number to indicate the error.
  460. **/
  461. MYOACCESSAPI
  462. MyoError myoiSetMemNonConsistent(void *in_pAddr, size_t in_Size);
  463. /** @fn extern MyoError myoiSetMemConsistent(void *in_pAddr, size_t in_Size)
  464. * @brief Set part of the shared memory space to be consistent, which
  465. * means that the consistency of this part of shared memory space needs
  466. * to be maintained between HOST and cards.
  467. *
  468. * @param in_pAddr The start address of the specified shared
  469. * memory space.
  470. * @param in_size The size of the specified shared memory space.
  471. * @return
  472. * MYO_SUCCESS; or
  473. * an error number to indicate the error.
  474. **/
  475. MYOACCESSAPI
  476. MyoError myoiSetMemConsistent(void *in_pAddr, size_t in_Size);
  477. /* A collection of external data symbols */
  478. EXTERN_C MYOACCESSAPI unsigned int myoiMyId; /* MYO_MYID if on accelerators */
  479. EXTERN_C MYOACCESSAPI volatile int myoiInitFlag;
  480. //! Structure of the array element that is passed to myoiLibInit() to initialize a subset of the available cards.
  481. typedef struct{
  482. //!type = MYOI_USERPARAMS_DEVID for each element in the array except the last element ; type = MYOI_USERPARAMS_LAST_MSG for the last element in the array.
  483. int type;
  484. //!nodeid refers to the card index.
  485. int nodeid;
  486. }MyoiUserParams;
  487. #define MYOI_USERPARAMS_DEVID 1
  488. #define MYOI_USERPARAMS_LAST_MSG -1
  489. #ifdef __cplusplus
  490. }
  491. #endif
  492. #endif // _MYO_IMPL_H_
  493. /*! @} */