offload_myo_target.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_target.h"
  27. #include "offload_target.h"
  28. extern "C" void __cilkrts_cilk_for_32(void*, void*, uint32_t, int32_t);
  29. extern "C" void __cilkrts_cilk_for_64(void*, void*, uint64_t, int32_t);
  30. #pragma weak __cilkrts_cilk_for_32
  31. #pragma weak __cilkrts_cilk_for_64
  32. static void CheckResult(const char *func, MyoError error) {
  33. if (error != MYO_SUCCESS) {
  34. LIBOFFLOAD_ERROR(c_myotarget_checkresult, func, error);
  35. exit(1);
  36. }
  37. }
  38. static void __offload_myo_shared_table_register(SharedTableEntry *entry)
  39. {
  40. int entries = 0;
  41. SharedTableEntry *t_start;
  42. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, entry);
  43. t_start = entry;
  44. while (t_start->varName != 0) {
  45. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_mic_myo_shared,
  46. "myo shared entry name = \"%s\" addr = %p\n",
  47. t_start->varName, t_start->sharedAddr);
  48. t_start++;
  49. entries++;
  50. }
  51. if (entries > 0) {
  52. OFFLOAD_DEBUG_TRACE(3, "myoiMicVarTableRegister(%p, %d)\n", entry,
  53. entries);
  54. CheckResult("myoiMicVarTableRegister",
  55. myoiMicVarTableRegister(entry, entries));
  56. }
  57. }
  58. static void __offload_myo_fptr_table_register(
  59. FptrTableEntry *entry
  60. )
  61. {
  62. int entries = 0;
  63. FptrTableEntry *t_start;
  64. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, entry);
  65. t_start = entry;
  66. while (t_start->funcName != 0) {
  67. OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_mic_myo_fptr,
  68. "myo fptr entry name = \"%s\" addr = %p\n",
  69. t_start->funcName, t_start->funcAddr);
  70. t_start++;
  71. entries++;
  72. }
  73. if (entries > 0) {
  74. OFFLOAD_DEBUG_TRACE(3, "myoiTargetFptrTableRegister(%p, %d, 0)\n",
  75. entry, entries);
  76. CheckResult("myoiTargetFptrTableRegister",
  77. myoiTargetFptrTableRegister(entry, entries, 0));
  78. }
  79. }
  80. extern "C" void __offload_myoAcquire(void)
  81. {
  82. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  83. CheckResult("myoAcquire", myoAcquire());
  84. }
  85. extern "C" void __offload_myoRelease(void)
  86. {
  87. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  88. CheckResult("myoRelease", myoRelease());
  89. }
  90. extern "C" void __intel_cilk_for_32_offload_wrapper(void *args_)
  91. {
  92. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  93. struct S {
  94. void *M1;
  95. unsigned int M2;
  96. unsigned int M3;
  97. char closure[];
  98. } *args = (struct S*) args_;
  99. __cilkrts_cilk_for_32(args->M1, args->closure, args->M2, args->M3);
  100. }
  101. extern "C" void __intel_cilk_for_64_offload_wrapper(void *args_)
  102. {
  103. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  104. struct S {
  105. void *M1;
  106. uint64_t M2;
  107. uint64_t M3;
  108. char closure[];
  109. } *args = (struct S*) args_;
  110. __cilkrts_cilk_for_64(args->M1, args->closure, args->M2, args->M3);
  111. }
  112. static void __offload_myo_once_init(void)
  113. {
  114. CheckResult("myoiRemoteFuncRegister",
  115. myoiRemoteFuncRegister(
  116. (MyoiRemoteFuncType) __intel_cilk_for_32_offload_wrapper,
  117. "__intel_cilk_for_32_offload"));
  118. CheckResult("myoiRemoteFuncRegister",
  119. myoiRemoteFuncRegister(
  120. (MyoiRemoteFuncType) __intel_cilk_for_64_offload_wrapper,
  121. "__intel_cilk_for_64_offload"));
  122. }
  123. extern "C" void __offload_myoRegisterTables(
  124. SharedTableEntry *shared_table,
  125. FptrTableEntry *fptr_table
  126. )
  127. {
  128. OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__);
  129. // one time registration of Intel(R) Cilk(TM) language entries
  130. static pthread_once_t once_control = PTHREAD_ONCE_INIT;
  131. pthread_once(&once_control, __offload_myo_once_init);
  132. // register module's tables
  133. if (shared_table->varName == 0 && fptr_table->funcName == 0) {
  134. return;
  135. }
  136. __offload_myo_shared_table_register(shared_table);
  137. __offload_myo_fptr_table_register(fptr_table);
  138. }
  139. extern "C" void* _Offload_shared_malloc(size_t size)
  140. {
  141. OFFLOAD_DEBUG_TRACE(3, "%s(%lld)\n", __func__, size);
  142. return myoSharedMalloc(size);
  143. }
  144. extern "C" void _Offload_shared_free(void *ptr)
  145. {
  146. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, ptr);
  147. myoSharedFree(ptr);
  148. }
  149. extern "C" void* _Offload_shared_aligned_malloc(size_t size, size_t align)
  150. {
  151. OFFLOAD_DEBUG_TRACE(3, "%s(%lld, %lld)\n", __func__, size, align);
  152. return myoSharedAlignedMalloc(size, align);
  153. }
  154. extern "C" void _Offload_shared_aligned_free(void *ptr)
  155. {
  156. OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__, ptr);
  157. myoSharedAlignedFree(ptr);
  158. }
  159. // temporary workaround for blocking behavior of myoiLibInit/Fini calls
  160. extern "C" void __offload_myoLibInit()
  161. {
  162. OFFLOAD_DEBUG_TRACE(3, "%s()\n", __func__);
  163. CheckResult("myoiLibInit", myoiLibInit(0, 0));
  164. }
  165. extern "C" void __offload_myoLibFini()
  166. {
  167. OFFLOAD_DEBUG_TRACE(3, "%s()\n", __func__);
  168. myoiLibFini();
  169. }