2 Copyright (c) 2014-2015 Intel Corporation. All Rights Reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "offload_myo_target.h"
32 #include "offload_target.h"
34 extern "C" void __cilkrts_cilk_for_32(void*, void*, uint32_t, int32_t);
35 extern "C" void __cilkrts_cilk_for_64(void*, void*, uint64_t, int32_t);
37 #pragma weak __cilkrts_cilk_for_32
38 #pragma weak __cilkrts_cilk_for_64
40 static void CheckResult(const char *func
, MyoError error
) {
41 if (error
!= MYO_SUCCESS
) {
42 LIBOFFLOAD_ERROR(c_myotarget_checkresult
, func
, error
);
47 static void __offload_myo_shared_table_process(SharedTableEntry
*entry
)
50 SharedTableEntry
*t_start
;
52 OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__
, entry
);
55 while (t_start
->varName
!= 0) {
56 OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_mic_myo_shared
,
57 "myo shared entry name = \"%s\" addr = %p\n",
58 t_start
->varName
, t_start
->sharedAddr
);
64 OFFLOAD_DEBUG_TRACE(3, "myoiMicVarTableRegister(%p, %d)\n", entry
,
66 CheckResult("myoiMicVarTableRegister",
67 myoiMicVarTableRegister(entry
, entries
));
71 static void __offload_myo_shared_vtable_process(SharedTableEntry
*entry
)
74 SharedTableEntry
*t_start
;
76 OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__
, entry
);
79 while (t_start
->varName
!= 0) {
80 OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_mic_myo_shared
,
81 "myo shared vtable entry name"
82 " = \"%s\" addr = %p\n",
83 t_start
->varName
, t_start
->sharedAddr
);
89 OFFLOAD_DEBUG_TRACE(3, "myoiMicVarTableRegister(%p, %d)\n", entry
,
91 CheckResult("myoiMicVarTableRegister",
92 myoiMicVarTableRegister(entry
, entries
));
96 static void __offload_myo_fptr_table_process(
101 FptrTableEntry
*t_start
;
103 OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__
, entry
);
106 while (t_start
->funcName
!= 0) {
107 OFFLOAD_DEBUG_TRACE_1(4, 0, c_offload_mic_myo_fptr
,
108 "myo fptr entry name = \"%s\" addr = %p\n",
109 t_start
->funcName
, t_start
->funcAddr
);
115 OFFLOAD_DEBUG_TRACE(3, "myoiTargetFptrTableRegister(%p, %d, 0)\n",
117 CheckResult("myoiTargetFptrTableRegister",
118 myoiTargetFptrTableRegister(entry
, entries
, 0));
122 void __offload_myo_shared_init_table_process(InitTableEntry
* entry
)
124 OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__
, entry
);
126 for (; entry
->func
!= 0; entry
++) {
127 // Invoke the function to init the shared memory
128 OFFLOAD_DEBUG_TRACE(3, "Invoked a shared init function @%p\n",
129 (void *)(entry
->func
));
134 extern "C" void __offload_myoAcquire(void)
136 OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__
);
138 CheckResult("myoAcquire", myoAcquire());
141 extern "C" void __offload_myoRelease(void)
143 OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__
);
144 CheckResult("myoRelease", myoRelease());
147 extern "C" void __intel_cilk_for_32_offload_wrapper(void *args_
)
149 OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__
);
156 } *args
= (struct S
*) args_
;
158 __cilkrts_cilk_for_32(args
->M1
, args
->closure
, args
->M2
, args
->M3
);
161 extern "C" void __intel_cilk_for_64_offload_wrapper(void *args_
)
163 OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__
);
170 } *args
= (struct S
*) args_
;
172 __cilkrts_cilk_for_64(args
->M1
, args
->closure
, args
->M2
, args
->M3
);
175 static void __offload_myo_once_init(void)
177 CheckResult("myoiRemoteFuncRegister",
178 myoiRemoteFuncRegister(
179 (MyoiRemoteFuncType
) __intel_cilk_for_32_offload_wrapper
,
180 "__intel_cilk_for_32_offload"));
181 CheckResult("myoiRemoteFuncRegister",
182 myoiRemoteFuncRegister(
183 (MyoiRemoteFuncType
) __intel_cilk_for_64_offload_wrapper
,
184 "__intel_cilk_for_64_offload"));
187 extern "C" void __offload_myoRegisterTables(
188 SharedTableEntry
*shared_table
,
189 FptrTableEntry
*fptr_table
192 OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__
);
194 // one time registration of Intel(R) Cilk(TM) language entries
195 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
196 pthread_once(&once_control
, __offload_myo_once_init
);
198 // register module's tables
199 if (shared_table
->varName
== 0 && fptr_table
->funcName
== 0) {
203 __offload_myo_shared_table_process(shared_table
);
204 __offload_myo_fptr_table_process(fptr_table
);
207 extern "C" void __offload_myoProcessTables(
208 InitTableEntry
* init_table
,
209 SharedTableEntry
*shared_table
,
210 SharedTableEntry
*shared_vtable
,
211 FptrTableEntry
*fptr_table
214 OFFLOAD_DEBUG_TRACE(3, "%s\n", __func__
);
216 // one time registration of Intel(R) Cilk(TM) language entries
217 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
218 pthread_once(&once_control
, __offload_myo_once_init
);
220 // register module's tables
221 // check slot-1 of the function table because
222 // slot-0 is predefined with --vtable_initializer--
223 if (shared_table
->varName
== 0 &&
224 shared_vtable
->varName
== 0 &&
225 fptr_table
[1].funcName
== 0) {
229 __offload_myo_shared_table_process(shared_table
);
230 __offload_myo_shared_vtable_process(shared_vtable
);
231 __offload_myo_fptr_table_process(fptr_table
);
234 extern "C" void* _Offload_shared_malloc(size_t size
)
236 OFFLOAD_DEBUG_TRACE(3, "%s(%lld)\n", __func__
, size
);
237 return myoSharedMalloc(size
);
240 extern "C" void _Offload_shared_free(void *ptr
)
242 OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__
, ptr
);
246 extern "C" void* _Offload_shared_aligned_malloc(size_t size
, size_t align
)
248 OFFLOAD_DEBUG_TRACE(3, "%s(%lld, %lld)\n", __func__
, size
, align
);
249 return myoSharedAlignedMalloc(size
, align
);
252 extern "C" void _Offload_shared_aligned_free(void *ptr
)
254 OFFLOAD_DEBUG_TRACE(3, "%s(%p)\n", __func__
, ptr
);
255 myoSharedAlignedFree(ptr
);
258 extern "C" void* _Offload_shared_aligned_arena_malloc(
265 3, "%s(%u, %lld, %lld)\n", __func__
, arena
, size
, align
);
267 return myoArenaAlignedMalloc(arena
, size
, align
);
270 extern "C" void _Offload_shared_aligned_arena_free(
275 OFFLOAD_DEBUG_TRACE(3, "%s(%u, %p)\n", __func__
, arena
, ptr
);
277 myoArenaAlignedFree(arena
, ptr
);
280 extern "C" void _Offload_shared_arena_acquire(
284 OFFLOAD_DEBUG_TRACE(3, "%s(%u)\n", __func__
, arena
);
286 myoArenaAcquire(arena
);
289 extern "C" void _Offload_shared_arena_release(
293 OFFLOAD_DEBUG_TRACE(3, "%s(%u)\n", __func__
, arena
);
295 myoArenaRelease(arena
);
298 // temporary workaround for blocking behavior of myoiLibInit/Fini calls
299 extern "C" void __offload_myoLibInit()
301 OFFLOAD_DEBUG_TRACE(3, "%s()\n", __func__
);
302 CheckResult("myoiLibInit", myoiLibInit(0, 0));
305 extern "C" void __offload_myoLibFini()
307 OFFLOAD_DEBUG_TRACE(3, "%s()\n", __func__
);