compiler_if_host.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "compiler_if_host.h"
  27. #include <malloc.h>
  28. #ifndef TARGET_WINNT
  29. #include <alloca.h>
  30. #endif // TARGET_WINNT
  31. // Global counter on host.
  32. // This variable is used if P2OPT_offload_do_data_persistence == 2.
  33. // The variable used to identify offload constructs contained in one procedure.
  34. // Increment of OFFLOAD_CALL_COUNT is inserted at entries of HOST routines with
  35. // offload constructs.
  36. static int offload_call_count = 0;
  37. extern "C" OFFLOAD OFFLOAD_TARGET_ACQUIRE(
  38. TARGET_TYPE target_type,
  39. int target_number,
  40. int is_optional,
  41. _Offload_status* status,
  42. const char* file,
  43. uint64_t line
  44. )
  45. {
  46. bool retval;
  47. OFFLOAD ofld;
  48. // initialize status
  49. if (status != 0) {
  50. status->result = OFFLOAD_UNAVAILABLE;
  51. status->device_number = -1;
  52. status->data_sent = 0;
  53. status->data_received = 0;
  54. }
  55. // make sure libray is initialized
  56. retval = __offload_init_library();
  57. // OFFLOAD_TIMER_INIT must follow call to __offload_init_library
  58. OffloadHostTimerData * timer_data = OFFLOAD_TIMER_INIT(file, line);
  59. OFFLOAD_TIMER_START(timer_data, c_offload_host_total_offload);
  60. OFFLOAD_TIMER_START(timer_data, c_offload_host_initialize);
  61. // initalize all devices is init_type is on_offload_all
  62. if (retval && __offload_init_type == c_init_on_offload_all) {
  63. for (int i = 0; i < mic_engines_total; i++) {
  64. mic_engines[i].init();
  65. }
  66. }
  67. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_initialize);
  68. OFFLOAD_TIMER_START(timer_data, c_offload_host_target_acquire);
  69. if (target_type == TARGET_HOST) {
  70. // Host always available
  71. retval = true;
  72. }
  73. else if (target_type == TARGET_MIC) {
  74. if (target_number >= -1) {
  75. if (retval) {
  76. if (target_number >= 0) {
  77. // User provided the device number
  78. target_number = target_number % mic_engines_total;
  79. }
  80. else {
  81. // use device 0
  82. target_number = 0;
  83. }
  84. // reserve device in ORSL
  85. if (is_optional) {
  86. if (!ORSL::try_reserve(target_number)) {
  87. target_number = -1;
  88. }
  89. }
  90. else {
  91. if (!ORSL::reserve(target_number)) {
  92. target_number = -1;
  93. }
  94. }
  95. // initialize device
  96. if (target_number >= 0 &&
  97. __offload_init_type == c_init_on_offload) {
  98. OFFLOAD_TIMER_START(timer_data, c_offload_host_initialize);
  99. mic_engines[target_number].init();
  100. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_initialize);
  101. }
  102. }
  103. else {
  104. // fallback to CPU
  105. target_number = -1;
  106. }
  107. if (target_number < 0 || !retval) {
  108. if (!is_optional && status == 0) {
  109. LIBOFFLOAD_ERROR(c_device_is_not_available);
  110. exit(1);
  111. }
  112. retval = false;
  113. }
  114. }
  115. else {
  116. LIBOFFLOAD_ERROR(c_invalid_device_number);
  117. exit(1);
  118. }
  119. }
  120. if (retval) {
  121. ofld = new OffloadDescriptor(target_number, status,
  122. !is_optional, false, timer_data);
  123. OFFLOAD_TIMER_HOST_MIC_NUM(timer_data, target_number);
  124. Offload_Report_Prolog(timer_data);
  125. OFFLOAD_DEBUG_TRACE_1(2, timer_data->offload_number, c_offload_start,
  126. "Starting offload: target_type = %d, "
  127. "number = %d, is_optional = %d\n",
  128. target_type, target_number, is_optional);
  129. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_target_acquire);
  130. }
  131. else {
  132. ofld = NULL;
  133. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_target_acquire);
  134. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_total_offload);
  135. offload_report_free_data(timer_data);
  136. }
  137. return ofld;
  138. }
  139. extern "C" OFFLOAD OFFLOAD_TARGET_ACQUIRE1(
  140. const int* device_num,
  141. const char* file,
  142. uint64_t line
  143. )
  144. {
  145. int target_number;
  146. // make sure libray is initialized and at least one device is available
  147. if (!__offload_init_library()) {
  148. LIBOFFLOAD_ERROR(c_device_is_not_available);
  149. exit(1);
  150. }
  151. // OFFLOAD_TIMER_INIT must follow call to __offload_init_library
  152. OffloadHostTimerData * timer_data = OFFLOAD_TIMER_INIT(file, line);
  153. OFFLOAD_TIMER_START(timer_data, c_offload_host_total_offload);
  154. OFFLOAD_TIMER_START(timer_data, c_offload_host_initialize);
  155. if (__offload_init_type == c_init_on_offload_all) {
  156. for (int i = 0; i < mic_engines_total; i++) {
  157. mic_engines[i].init();
  158. }
  159. }
  160. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_initialize);
  161. OFFLOAD_TIMER_START(timer_data, c_offload_host_target_acquire);
  162. // use default device number if it is not provided
  163. if (device_num != 0) {
  164. target_number = *device_num;
  165. }
  166. else {
  167. target_number = __omp_device_num;
  168. }
  169. // device number should be a non-negative integer value
  170. if (target_number < 0) {
  171. LIBOFFLOAD_ERROR(c_omp_invalid_device_num);
  172. exit(1);
  173. }
  174. // should we do this for OpenMP?
  175. target_number %= mic_engines_total;
  176. // reserve device in ORSL
  177. if (!ORSL::reserve(target_number)) {
  178. LIBOFFLOAD_ERROR(c_device_is_not_available);
  179. exit(1);
  180. }
  181. // initialize device(s)
  182. OFFLOAD_TIMER_START(timer_data, c_offload_host_initialize);
  183. if (__offload_init_type == c_init_on_offload) {
  184. mic_engines[target_number].init();
  185. }
  186. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_initialize);
  187. OFFLOAD ofld =
  188. new OffloadDescriptor(target_number, 0, true, true, timer_data);
  189. OFFLOAD_TIMER_HOST_MIC_NUM(timer_data, target_number);
  190. Offload_Report_Prolog(timer_data);
  191. OFFLOAD_DEBUG_TRACE_1(2, timer_data->offload_number, c_offload_start,
  192. "Starting OpenMP offload, device = %d\n",
  193. target_number);
  194. OFFLOAD_TIMER_STOP(timer_data, c_offload_host_target_acquire);
  195. return ofld;
  196. }
  197. int offload_offload_wrap(
  198. OFFLOAD ofld,
  199. const char *name,
  200. int is_empty,
  201. int num_vars,
  202. VarDesc *vars,
  203. VarDesc2 *vars2,
  204. int num_waits,
  205. const void **waits,
  206. const void **signal,
  207. int entry_id,
  208. const void *stack_addr
  209. )
  210. {
  211. bool ret = ofld->offload(name, is_empty, vars, vars2, num_vars,
  212. waits, num_waits, signal, entry_id, stack_addr);
  213. if (!ret || signal == 0) {
  214. delete ofld;
  215. }
  216. return ret;
  217. }
  218. extern "C" int OFFLOAD_OFFLOAD1(
  219. OFFLOAD ofld,
  220. const char *name,
  221. int is_empty,
  222. int num_vars,
  223. VarDesc *vars,
  224. VarDesc2 *vars2,
  225. int num_waits,
  226. const void **waits,
  227. const void **signal
  228. )
  229. {
  230. return offload_offload_wrap(ofld, name, is_empty,
  231. num_vars, vars, vars2,
  232. num_waits, waits,
  233. signal, NULL, NULL);
  234. }
  235. extern "C" int OFFLOAD_OFFLOAD2(
  236. OFFLOAD ofld,
  237. const char *name,
  238. int is_empty,
  239. int num_vars,
  240. VarDesc *vars,
  241. VarDesc2 *vars2,
  242. int num_waits,
  243. const void** waits,
  244. const void** signal,
  245. int entry_id,
  246. const void *stack_addr
  247. )
  248. {
  249. return offload_offload_wrap(ofld, name, is_empty,
  250. num_vars, vars, vars2,
  251. num_waits, waits,
  252. signal, entry_id, stack_addr);
  253. }
  254. extern "C" int OFFLOAD_OFFLOAD(
  255. OFFLOAD ofld,
  256. const char *name,
  257. int is_empty,
  258. int num_vars,
  259. VarDesc *vars,
  260. VarDesc2 *vars2,
  261. int num_waits,
  262. const void **waits,
  263. const void *signal,
  264. int entry_id,
  265. const void *stack_addr
  266. )
  267. {
  268. // signal is passed by reference now
  269. const void **signal_new = (signal != 0) ? &signal : 0;
  270. const void **waits_new = 0;
  271. int num_waits_new = 0;
  272. // remove NULL values from the list of signals to wait for
  273. if (num_waits > 0) {
  274. waits_new = (const void**) alloca(sizeof(void*) * num_waits);
  275. for (int i = 0; i < num_waits; i++) {
  276. if (waits[i] != 0) {
  277. waits_new[num_waits_new++] = waits[i];
  278. }
  279. }
  280. }
  281. return OFFLOAD_OFFLOAD1(ofld, name, is_empty,
  282. num_vars, vars, vars2,
  283. num_waits_new, waits_new,
  284. signal_new);
  285. }
  286. extern "C" int OFFLOAD_CALL_COUNT()
  287. {
  288. offload_call_count++;
  289. return offload_call_count;
  290. }