offload_omp_target.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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_target.h"
  29. // OpenMP API
  30. void omp_set_default_device(int num) __GOMP_NOTHROW
  31. {
  32. }
  33. int omp_get_default_device(void) __GOMP_NOTHROW
  34. {
  35. return mic_index;
  36. }
  37. int omp_get_num_devices() __GOMP_NOTHROW
  38. {
  39. return mic_engines_total;
  40. }
  41. // OpenMP API wrappers
  42. static void omp_send_int_to_host(
  43. void *ofld_,
  44. int setting
  45. )
  46. {
  47. OFFLOAD ofld = (OFFLOAD) ofld_;
  48. VarDesc vars[1] = {0};
  49. vars[0].type.src = c_data;
  50. vars[0].type.dst = c_data;
  51. vars[0].direction.bits = c_parameter_out;
  52. vars[0].ptr = &setting;
  53. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  54. OFFLOAD_TARGET_LEAVE(ofld);
  55. }
  56. static int omp_get_int_from_host(
  57. void *ofld_
  58. )
  59. {
  60. OFFLOAD ofld = (OFFLOAD) ofld_;
  61. VarDesc vars[1] = {0};
  62. int setting;
  63. vars[0].type.src = c_data;
  64. vars[0].type.dst = c_data;
  65. vars[0].direction.bits = c_parameter_in;
  66. vars[0].ptr = &setting;
  67. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  68. OFFLOAD_TARGET_LEAVE(ofld);
  69. return setting;
  70. }
  71. void omp_set_num_threads_lrb(
  72. void *ofld
  73. )
  74. {
  75. int num_threads;
  76. num_threads = omp_get_int_from_host(ofld);
  77. omp_set_num_threads(num_threads);
  78. }
  79. void omp_get_max_threads_lrb(
  80. void *ofld
  81. )
  82. {
  83. int num_threads;
  84. num_threads = omp_get_max_threads();
  85. omp_send_int_to_host(ofld, num_threads);
  86. }
  87. void omp_get_num_procs_lrb(
  88. void *ofld
  89. )
  90. {
  91. int num_procs;
  92. num_procs = omp_get_num_procs();
  93. omp_send_int_to_host(ofld, num_procs);
  94. }
  95. void omp_set_dynamic_lrb(
  96. void *ofld
  97. )
  98. {
  99. int dynamic;
  100. dynamic = omp_get_int_from_host(ofld);
  101. omp_set_dynamic(dynamic);
  102. }
  103. void omp_get_dynamic_lrb(
  104. void *ofld
  105. )
  106. {
  107. int dynamic;
  108. dynamic = omp_get_dynamic();
  109. omp_send_int_to_host(ofld, dynamic);
  110. }
  111. void omp_set_nested_lrb(
  112. void *ofld
  113. )
  114. {
  115. int nested;
  116. nested = omp_get_int_from_host(ofld);
  117. omp_set_nested(nested);
  118. }
  119. void omp_get_nested_lrb(
  120. void *ofld
  121. )
  122. {
  123. int nested;
  124. nested = omp_get_nested();
  125. omp_send_int_to_host(ofld, nested);
  126. }
  127. void omp_set_schedule_lrb(
  128. void *ofld_
  129. )
  130. {
  131. OFFLOAD ofld = (OFFLOAD) ofld_;
  132. VarDesc vars[2] = {0};
  133. omp_sched_t kind;
  134. int modifier;
  135. vars[0].type.src = c_data;
  136. vars[0].type.dst = c_data;
  137. vars[0].direction.bits = c_parameter_in;
  138. vars[0].ptr = &kind;
  139. vars[1].type.src = c_data;
  140. vars[1].type.dst = c_data;
  141. vars[1].direction.bits = c_parameter_in;
  142. vars[1].ptr = &modifier;
  143. OFFLOAD_TARGET_ENTER(ofld, 2, vars, NULL);
  144. omp_set_schedule(kind, modifier);
  145. OFFLOAD_TARGET_LEAVE(ofld);
  146. }
  147. void omp_get_schedule_lrb(
  148. void *ofld_
  149. )
  150. {
  151. OFFLOAD ofld = (OFFLOAD) ofld_;
  152. VarDesc vars[2] = {0};
  153. omp_sched_t kind;
  154. int modifier;
  155. vars[0].type.src = c_data;
  156. vars[0].type.dst = c_data;
  157. vars[0].direction.bits = c_parameter_out;
  158. vars[0].ptr = &kind;
  159. vars[1].type.src = c_data;
  160. vars[1].type.dst = c_data;
  161. vars[1].direction.bits = c_parameter_out;
  162. vars[1].ptr = &modifier;
  163. OFFLOAD_TARGET_ENTER(ofld, 2, vars, NULL);
  164. omp_get_schedule(&kind, &modifier);
  165. OFFLOAD_TARGET_LEAVE(ofld);
  166. }
  167. // lock API functions
  168. void omp_init_lock_lrb(
  169. void *ofld_
  170. )
  171. {
  172. OFFLOAD ofld = (OFFLOAD) ofld_;
  173. VarDesc vars[1] = {0};
  174. omp_lock_target_t lock;
  175. vars[0].type.src = c_data;
  176. vars[0].type.dst = c_data;
  177. vars[0].direction.bits = c_parameter_out;
  178. vars[0].ptr = &lock;
  179. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  180. omp_init_lock(&lock.lock);
  181. OFFLOAD_TARGET_LEAVE(ofld);
  182. }
  183. void omp_destroy_lock_lrb(
  184. void *ofld_
  185. )
  186. {
  187. OFFLOAD ofld = (OFFLOAD) ofld_;
  188. VarDesc vars[1] = {0};
  189. omp_lock_target_t lock;
  190. vars[0].type.src = c_data;
  191. vars[0].type.dst = c_data;
  192. vars[0].direction.bits = c_parameter_in;
  193. vars[0].ptr = &lock;
  194. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  195. omp_destroy_lock(&lock.lock);
  196. OFFLOAD_TARGET_LEAVE(ofld);
  197. }
  198. void omp_set_lock_lrb(
  199. void *ofld_
  200. )
  201. {
  202. OFFLOAD ofld = (OFFLOAD) ofld_;
  203. VarDesc vars[1] = {0};
  204. omp_lock_target_t lock;
  205. vars[0].type.src = c_data;
  206. vars[0].type.dst = c_data;
  207. vars[0].direction.bits = c_parameter_inout;
  208. vars[0].ptr = &lock;
  209. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  210. omp_set_lock(&lock.lock);
  211. OFFLOAD_TARGET_LEAVE(ofld);
  212. }
  213. void omp_unset_lock_lrb(
  214. void *ofld_
  215. )
  216. {
  217. OFFLOAD ofld = (OFFLOAD) ofld_;
  218. VarDesc vars[1] = {0};
  219. omp_lock_target_t lock;
  220. vars[0].type.src = c_data;
  221. vars[0].type.dst = c_data;
  222. vars[0].direction.bits = c_parameter_inout;
  223. vars[0].ptr = &lock;
  224. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  225. omp_unset_lock(&lock.lock);
  226. OFFLOAD_TARGET_LEAVE(ofld);
  227. }
  228. void omp_test_lock_lrb(
  229. void *ofld_
  230. )
  231. {
  232. OFFLOAD ofld = (OFFLOAD) ofld_;
  233. VarDesc vars[2] = {0};
  234. omp_lock_target_t lock;
  235. int result;
  236. vars[0].type.src = c_data;
  237. vars[0].type.dst = c_data;
  238. vars[0].direction.bits = c_parameter_inout;
  239. vars[0].ptr = &lock;
  240. vars[1].type.src = c_data;
  241. vars[1].type.dst = c_data;
  242. vars[1].direction.bits = c_parameter_out;
  243. vars[1].ptr = &result;
  244. OFFLOAD_TARGET_ENTER(ofld, 2, vars, NULL);
  245. result = omp_test_lock(&lock.lock);
  246. OFFLOAD_TARGET_LEAVE(ofld);
  247. }
  248. // nested lock API functions
  249. void omp_init_nest_lock_lrb(
  250. void *ofld_
  251. )
  252. {
  253. OFFLOAD ofld = (OFFLOAD) ofld_;
  254. VarDesc vars[1] = {0};
  255. omp_nest_lock_target_t lock;
  256. vars[0].type.src = c_data;
  257. vars[0].type.dst = c_data;
  258. vars[0].direction.bits = c_parameter_out;
  259. vars[0].ptr = &lock;
  260. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  261. omp_init_nest_lock(&lock.lock);
  262. OFFLOAD_TARGET_LEAVE(ofld);
  263. }
  264. void omp_destroy_nest_lock_lrb(
  265. void *ofld_
  266. )
  267. {
  268. OFFLOAD ofld = (OFFLOAD) ofld_;
  269. VarDesc vars[1] = {0};
  270. omp_nest_lock_target_t lock;
  271. vars[0].type.src = c_data;
  272. vars[0].type.dst = c_data;
  273. vars[0].direction.bits = c_parameter_in;
  274. vars[0].ptr = &lock;
  275. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  276. omp_destroy_nest_lock(&lock.lock);
  277. OFFLOAD_TARGET_LEAVE(ofld);
  278. }
  279. void omp_set_nest_lock_lrb(
  280. void *ofld_
  281. )
  282. {
  283. OFFLOAD ofld = (OFFLOAD) ofld_;
  284. VarDesc vars[1] = {0};
  285. omp_nest_lock_target_t lock;
  286. vars[0].type.src = c_data;
  287. vars[0].type.dst = c_data;
  288. vars[0].direction.bits = c_parameter_inout;
  289. vars[0].ptr = &lock;
  290. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  291. omp_set_nest_lock(&lock.lock);
  292. OFFLOAD_TARGET_LEAVE(ofld);
  293. }
  294. void omp_unset_nest_lock_lrb(
  295. void *ofld_
  296. )
  297. {
  298. OFFLOAD ofld = (OFFLOAD) ofld_;
  299. VarDesc vars[1] = {0};
  300. omp_nest_lock_target_t lock;
  301. vars[0].type.src = c_data;
  302. vars[0].type.dst = c_data;
  303. vars[0].direction.bits = c_parameter_inout;
  304. vars[0].ptr = &lock;
  305. OFFLOAD_TARGET_ENTER(ofld, 1, vars, NULL);
  306. omp_unset_nest_lock(&lock.lock);
  307. OFFLOAD_TARGET_LEAVE(ofld);
  308. }
  309. void omp_test_nest_lock_lrb(
  310. void *ofld_
  311. )
  312. {
  313. OFFLOAD ofld = (OFFLOAD) ofld_;
  314. VarDesc vars[2] = {0};
  315. omp_nest_lock_target_t lock;
  316. int result;
  317. vars[0].type.src = c_data;
  318. vars[0].type.dst = c_data;
  319. vars[0].direction.bits = c_parameter_inout;
  320. vars[0].ptr = &lock;
  321. vars[1].type.src = c_data;
  322. vars[1].type.dst = c_data;
  323. vars[1].direction.bits = c_parameter_out;
  324. vars[1].ptr = &result;
  325. OFFLOAD_TARGET_ENTER(ofld, 2, vars, NULL);
  326. result = omp_test_nest_lock(&lock.lock);
  327. OFFLOAD_TARGET_LEAVE(ofld);
  328. }
  329. // Target-side stubs for the host functions (to avoid unresolveds)
  330. // These are needed for the offloadm table
  331. void omp_set_num_threads_target(
  332. TARGET_TYPE target_type,
  333. int target_number,
  334. int num_threads
  335. )
  336. {
  337. }
  338. int omp_get_max_threads_target(
  339. TARGET_TYPE target_type,
  340. int target_number
  341. )
  342. {
  343. return 0;
  344. }
  345. int omp_get_num_procs_target(
  346. TARGET_TYPE target_type,
  347. int target_number
  348. )
  349. {
  350. return 0;
  351. }
  352. void omp_set_dynamic_target(
  353. TARGET_TYPE target_type,
  354. int target_number,
  355. int num_threads
  356. )
  357. {
  358. }
  359. int omp_get_dynamic_target(
  360. TARGET_TYPE target_type,
  361. int target_number
  362. )
  363. {
  364. return 0;
  365. }
  366. void omp_set_nested_target(
  367. TARGET_TYPE target_type,
  368. int target_number,
  369. int num_threads
  370. )
  371. {
  372. }
  373. int omp_get_nested_target(
  374. TARGET_TYPE target_type,
  375. int target_number
  376. )
  377. {
  378. return 0;
  379. }
  380. void omp_set_schedule_target(
  381. TARGET_TYPE target_type,
  382. int target_number,
  383. omp_sched_t kind,
  384. int modifier
  385. )
  386. {
  387. }
  388. void omp_get_schedule_target(
  389. TARGET_TYPE target_type,
  390. int target_number,
  391. omp_sched_t *kind,
  392. int *modifier
  393. )
  394. {
  395. }
  396. void omp_init_lock_target(
  397. TARGET_TYPE target_type,
  398. int target_number,
  399. omp_lock_target_t *lock
  400. )
  401. {
  402. }
  403. void omp_destroy_lock_target(
  404. TARGET_TYPE target_type,
  405. int target_number,
  406. omp_lock_target_t *lock
  407. )
  408. {
  409. }
  410. void omp_set_lock_target(
  411. TARGET_TYPE target_type,
  412. int target_number,
  413. omp_lock_target_t *lock
  414. )
  415. {
  416. }
  417. void omp_unset_lock_target(
  418. TARGET_TYPE target_type,
  419. int target_number,
  420. omp_lock_target_t *lock
  421. )
  422. {
  423. }
  424. int omp_test_lock_target(
  425. TARGET_TYPE target_type,
  426. int target_number,
  427. omp_lock_target_t *lock
  428. )
  429. {
  430. return 0;
  431. }
  432. void omp_init_nest_lock_target(
  433. TARGET_TYPE target_type,
  434. int target_number,
  435. omp_nest_lock_target_t *lock
  436. )
  437. {
  438. }
  439. void omp_destroy_nest_lock_target(
  440. TARGET_TYPE target_type,
  441. int target_number,
  442. omp_nest_lock_target_t *lock
  443. )
  444. {
  445. }
  446. void omp_set_nest_lock_target(
  447. TARGET_TYPE target_type,
  448. int target_number,
  449. omp_nest_lock_target_t *lock
  450. )
  451. {
  452. }
  453. void omp_unset_nest_lock_target(
  454. TARGET_TYPE target_type,
  455. int target_number,
  456. omp_nest_lock_target_t *lock
  457. )
  458. {
  459. }
  460. int omp_test_nest_lock_target(
  461. TARGET_TYPE target_type,
  462. int target_number,
  463. omp_nest_lock_target_t *lock
  464. )
  465. {
  466. return 0;
  467. }