offload_omp_host.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <omp.h>
  27. #include "offload.h"
  28. #include "compiler_if_host.h"
  29. // OpenMP API
  30. void omp_set_default_device(int num) __GOMP_NOTHROW
  31. {
  32. if (num >= 0) {
  33. __omp_device_num = num;
  34. }
  35. }
  36. int omp_get_default_device(void) __GOMP_NOTHROW
  37. {
  38. return __omp_device_num;
  39. }
  40. int omp_get_num_devices() __GOMP_NOTHROW
  41. {
  42. __offload_init_library();
  43. return mic_engines_total;
  44. }
  45. // OpenMP API wrappers
  46. static void omp_set_int_target(
  47. TARGET_TYPE target_type,
  48. int target_number,
  49. int setting,
  50. const char* f_name
  51. )
  52. {
  53. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  54. f_name, 0);
  55. if (ofld) {
  56. VarDesc vars[1] = {0};
  57. vars[0].type.src = c_data;
  58. vars[0].type.dst = c_data;
  59. vars[0].direction.bits = c_parameter_in;
  60. vars[0].size = sizeof(int);
  61. vars[0].count = 1;
  62. vars[0].ptr = &setting;
  63. OFFLOAD_OFFLOAD(ofld, f_name, 0, 1, vars, NULL, 0, 0, 0);
  64. }
  65. }
  66. static int omp_get_int_target(
  67. TARGET_TYPE target_type,
  68. int target_number,
  69. const char * f_name
  70. )
  71. {
  72. int setting = 0;
  73. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  74. f_name, 0);
  75. if (ofld) {
  76. VarDesc vars[1] = {0};
  77. vars[0].type.src = c_data;
  78. vars[0].type.dst = c_data;
  79. vars[0].direction.bits = c_parameter_out;
  80. vars[0].size = sizeof(int);
  81. vars[0].count = 1;
  82. vars[0].ptr = &setting;
  83. OFFLOAD_OFFLOAD(ofld, f_name, 0, 1, vars, NULL, 0, 0, 0);
  84. }
  85. return setting;
  86. }
  87. void omp_set_num_threads_target(
  88. TARGET_TYPE target_type,
  89. int target_number,
  90. int num_threads
  91. )
  92. {
  93. omp_set_int_target(target_type, target_number, num_threads,
  94. "omp_set_num_threads_target");
  95. }
  96. int omp_get_max_threads_target(
  97. TARGET_TYPE target_type,
  98. int target_number
  99. )
  100. {
  101. return omp_get_int_target(target_type, target_number,
  102. "omp_get_max_threads_target");
  103. }
  104. int omp_get_num_procs_target(
  105. TARGET_TYPE target_type,
  106. int target_number
  107. )
  108. {
  109. return omp_get_int_target(target_type, target_number,
  110. "omp_get_num_procs_target");
  111. }
  112. void omp_set_dynamic_target(
  113. TARGET_TYPE target_type,
  114. int target_number,
  115. int num_threads
  116. )
  117. {
  118. omp_set_int_target(target_type, target_number, num_threads,
  119. "omp_set_dynamic_target");
  120. }
  121. int omp_get_dynamic_target(
  122. TARGET_TYPE target_type,
  123. int target_number
  124. )
  125. {
  126. return omp_get_int_target(target_type, target_number,
  127. "omp_get_dynamic_target");
  128. }
  129. void omp_set_nested_target(
  130. TARGET_TYPE target_type,
  131. int target_number,
  132. int nested
  133. )
  134. {
  135. omp_set_int_target(target_type, target_number, nested,
  136. "omp_set_nested_target");
  137. }
  138. int omp_get_nested_target(
  139. TARGET_TYPE target_type,
  140. int target_number
  141. )
  142. {
  143. return omp_get_int_target(target_type, target_number,
  144. "omp_get_nested_target");
  145. }
  146. void omp_set_schedule_target(
  147. TARGET_TYPE target_type,
  148. int target_number,
  149. omp_sched_t kind,
  150. int modifier
  151. )
  152. {
  153. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  154. __func__, 0);
  155. if (ofld != 0) {
  156. VarDesc vars[2] = {0};
  157. vars[0].type.src = c_data;
  158. vars[0].type.dst = c_data;
  159. vars[0].direction.bits = c_parameter_in;
  160. vars[0].size = sizeof(omp_sched_t);
  161. vars[0].count = 1;
  162. vars[0].ptr = &kind;
  163. vars[1].type.src = c_data;
  164. vars[1].type.dst = c_data;
  165. vars[1].direction.bits = c_parameter_in;
  166. vars[1].size = sizeof(int);
  167. vars[1].count = 1;
  168. vars[1].ptr = &modifier;
  169. OFFLOAD_OFFLOAD(ofld, "omp_set_schedule_target",
  170. 0, 2, vars, NULL, 0, 0, 0);
  171. }
  172. }
  173. void omp_get_schedule_target(
  174. TARGET_TYPE target_type,
  175. int target_number,
  176. omp_sched_t *kind,
  177. int *modifier
  178. )
  179. {
  180. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  181. __func__, 0);
  182. if (ofld != 0) {
  183. VarDesc vars[2] = {0};
  184. vars[0].type.src = c_data;
  185. vars[0].type.dst = c_data;
  186. vars[0].direction.bits = c_parameter_out;
  187. vars[0].size = sizeof(omp_sched_t);
  188. vars[0].count = 1;
  189. vars[0].ptr = kind;
  190. vars[1].type.src = c_data;
  191. vars[1].type.dst = c_data;
  192. vars[1].direction.bits = c_parameter_out;
  193. vars[1].size = sizeof(int);
  194. vars[1].count = 1;
  195. vars[1].ptr = modifier;
  196. OFFLOAD_OFFLOAD(ofld, "omp_get_schedule_target",
  197. 0, 2, vars, NULL, 0, 0, 0);
  198. }
  199. }
  200. // lock API functions
  201. void omp_init_lock_target(
  202. TARGET_TYPE target_type,
  203. int target_number,
  204. omp_lock_target_t *lock
  205. )
  206. {
  207. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  208. __func__, 0);
  209. if (ofld != 0) {
  210. VarDesc vars[1] = {0};
  211. vars[0].type.src = c_data;
  212. vars[0].type.dst = c_data;
  213. vars[0].direction.bits = c_parameter_out;
  214. vars[0].size = sizeof(omp_lock_target_t);
  215. vars[0].count = 1;
  216. vars[0].ptr = lock;
  217. OFFLOAD_OFFLOAD(ofld, "omp_init_lock_target",
  218. 0, 1, vars, NULL, 0, 0, 0);
  219. }
  220. }
  221. void omp_destroy_lock_target(
  222. TARGET_TYPE target_type,
  223. int target_number,
  224. omp_lock_target_t *lock
  225. )
  226. {
  227. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  228. __func__, 0);
  229. if (ofld != 0) {
  230. VarDesc vars[1] = {0};
  231. vars[0].type.src = c_data;
  232. vars[0].type.dst = c_data;
  233. vars[0].direction.bits = c_parameter_in;
  234. vars[0].size = sizeof(omp_lock_target_t);
  235. vars[0].count = 1;
  236. vars[0].ptr = lock;
  237. OFFLOAD_OFFLOAD(ofld, "omp_destroy_lock_target",
  238. 0, 1, vars, NULL, 0, 0, 0);
  239. }
  240. }
  241. void omp_set_lock_target(
  242. TARGET_TYPE target_type,
  243. int target_number,
  244. omp_lock_target_t *lock
  245. )
  246. {
  247. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  248. __func__, 0);
  249. if (ofld != 0) {
  250. VarDesc vars[1] = {0};
  251. vars[0].type.src = c_data;
  252. vars[0].type.dst = c_data;
  253. vars[0].direction.bits = c_parameter_inout;
  254. vars[0].size = sizeof(omp_lock_target_t);
  255. vars[0].count = 1;
  256. vars[0].ptr = lock;
  257. OFFLOAD_OFFLOAD(ofld, "omp_set_lock_target",
  258. 0, 1, vars, NULL, 0, 0, 0);
  259. }
  260. }
  261. void omp_unset_lock_target(
  262. TARGET_TYPE target_type,
  263. int target_number,
  264. omp_lock_target_t *lock
  265. )
  266. {
  267. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  268. __func__, 0);
  269. if (ofld != 0) {
  270. VarDesc vars[1] = {0};
  271. vars[0].type.src = c_data;
  272. vars[0].type.dst = c_data;
  273. vars[0].direction.bits = c_parameter_inout;
  274. vars[0].size = sizeof(omp_lock_target_t);
  275. vars[0].count = 1;
  276. vars[0].ptr = lock;
  277. OFFLOAD_OFFLOAD(ofld, "omp_unset_lock_target",
  278. 0, 1, vars, NULL, 0, 0, 0);
  279. }
  280. }
  281. int omp_test_lock_target(
  282. TARGET_TYPE target_type,
  283. int target_number,
  284. omp_lock_target_t *lock
  285. )
  286. {
  287. int result = 0;
  288. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  289. __func__, 0);
  290. if (ofld != 0) {
  291. VarDesc vars[2] = {0};
  292. vars[0].type.src = c_data;
  293. vars[0].type.dst = c_data;
  294. vars[0].direction.bits = c_parameter_inout;
  295. vars[0].size = sizeof(omp_lock_target_t);
  296. vars[0].count = 1;
  297. vars[0].ptr = lock;
  298. vars[1].type.src = c_data;
  299. vars[1].type.dst = c_data;
  300. vars[1].direction.bits = c_parameter_out;
  301. vars[1].size = sizeof(int);
  302. vars[1].count = 1;
  303. vars[1].ptr = &result;
  304. OFFLOAD_OFFLOAD(ofld, "omp_test_lock_target",
  305. 0, 2, vars, NULL, 0, 0, 0);
  306. }
  307. return result;
  308. }
  309. // nested lock API functions
  310. void omp_init_nest_lock_target(
  311. TARGET_TYPE target_type,
  312. int target_number,
  313. omp_nest_lock_target_t *lock
  314. )
  315. {
  316. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  317. __func__, 0);
  318. if (ofld != 0) {
  319. VarDesc vars[1] = {0};
  320. vars[0].type.src = c_data;
  321. vars[0].type.dst = c_data;
  322. vars[0].direction.bits = c_parameter_out;
  323. vars[0].size = sizeof(omp_nest_lock_target_t);
  324. vars[0].count = 1;
  325. vars[0].ptr = lock;
  326. OFFLOAD_OFFLOAD(ofld, "omp_init_nest_lock_target",
  327. 0, 1, vars, NULL, 0, 0, 0);
  328. }
  329. }
  330. void omp_destroy_nest_lock_target(
  331. TARGET_TYPE target_type,
  332. int target_number,
  333. omp_nest_lock_target_t *lock
  334. )
  335. {
  336. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  337. __func__, 0);
  338. if (ofld != 0) {
  339. VarDesc vars[1] = {0};
  340. vars[0].type.src = c_data;
  341. vars[0].type.dst = c_data;
  342. vars[0].direction.bits = c_parameter_in;
  343. vars[0].size = sizeof(omp_nest_lock_target_t);
  344. vars[0].count = 1;
  345. vars[0].ptr = lock;
  346. OFFLOAD_OFFLOAD(ofld, "omp_destroy_nest_lock_target",
  347. 0, 1, vars, NULL, 0, 0, 0);
  348. }
  349. }
  350. void omp_set_nest_lock_target(
  351. TARGET_TYPE target_type,
  352. int target_number,
  353. omp_nest_lock_target_t *lock
  354. )
  355. {
  356. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  357. __func__, 0);
  358. if (ofld != 0) {
  359. VarDesc vars[1] = {0};
  360. vars[0].type.src = c_data;
  361. vars[0].type.dst = c_data;
  362. vars[0].direction.bits = c_parameter_inout;
  363. vars[0].size = sizeof(omp_nest_lock_target_t);
  364. vars[0].count = 1;
  365. vars[0].ptr = lock;
  366. OFFLOAD_OFFLOAD(ofld, "omp_set_nest_lock_target",
  367. 0, 1, vars, NULL, 0, 0, 0);
  368. }
  369. }
  370. void omp_unset_nest_lock_target(
  371. TARGET_TYPE target_type,
  372. int target_number,
  373. omp_nest_lock_target_t *lock
  374. )
  375. {
  376. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  377. __func__, 0);
  378. if (ofld != 0) {
  379. VarDesc vars[1] = {0};
  380. vars[0].type.src = c_data;
  381. vars[0].type.dst = c_data;
  382. vars[0].direction.bits = c_parameter_inout;
  383. vars[0].size = sizeof(omp_nest_lock_target_t);
  384. vars[0].count = 1;
  385. vars[0].ptr = lock;
  386. OFFLOAD_OFFLOAD(ofld, "omp_unset_nest_lock_target",
  387. 0, 1, vars, NULL, 0, 0, 0);
  388. }
  389. }
  390. int omp_test_nest_lock_target(
  391. TARGET_TYPE target_type,
  392. int target_number,
  393. omp_nest_lock_target_t *lock
  394. )
  395. {
  396. int result = 0;
  397. OFFLOAD ofld = OFFLOAD_TARGET_ACQUIRE(target_type, target_number, 0, NULL,
  398. __func__, 0);
  399. if (ofld != 0) {
  400. VarDesc vars[2] = {0};
  401. vars[0].type.src = c_data;
  402. vars[0].type.dst = c_data;
  403. vars[0].direction.bits = c_parameter_inout;
  404. vars[0].size = sizeof(omp_nest_lock_target_t);
  405. vars[0].count = 1;
  406. vars[0].ptr = lock;
  407. vars[1].type.src = c_data;
  408. vars[1].type.dst = c_data;
  409. vars[1].direction.bits = c_parameter_out;
  410. vars[1].size = sizeof(int);
  411. vars[1].count = 1;
  412. vars[1].ptr = &result;
  413. OFFLOAD_OFFLOAD(ofld, "omp_test_nest_lock_target",
  414. 0, 2, vars, NULL, 0, 0, 0);
  415. }
  416. return result;
  417. }