3 *************************************************************************
5 * Copyright (C) 2009-2016, Intel Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
32 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 * *********************************************************************
37 * PLEASE NOTE: This file is a downstream copy of a file mainitained in
38 * a repository at cilkplus.org. Changes made to this file that are not
39 * submitted through the contribution process detailed at
40 * http://www.cilkplus.org/submit-cilk-contribution will be lost the next
41 * time that a new version is released. Changes only submitted to the
42 * GNU compiler collection or posted to the git repository at
43 * https://bitbucket.org/intelcilkruntime/intel-cilk-runtime.git are
46 * We welcome your contributions to this open source project. Thank you
47 * for your assistance in helping us improve Cilk Plus.
48 **************************************************************************/
51 * Implementation of functions declared in cilk_api.h
55 * Define the COMPILING_CILK_ABI_FUNCTIONS macro, so that
56 * compilation of this file generates non-inlined definitions for the
57 * functions marked as CILK_EXPORT_AND_INLINE in cilk_api.h.
59 * We must deal with these functions differently because we need to
60 * continue to ship nonlined versions of these functions.
62 * CILK_EXPORT_AND_INLINE int __cilkrts_get_worker_rank(uint64_t *rank);
63 * CILK_EXPORT_AND_INLINE int __cilkrts_bump_worker_rank();
64 * CILK_EXPORT_AND_INLINE int __cilkrts_bump_loop_rank();
66 #define COMPILING_CILK_API_FUNCTIONS
68 #include <internal/abi.h>
69 #include <cilk/cilk_api.h>
74 #include "global_state.h"
75 #include "local_state.h"
76 #include "scheduler.h"
79 CILK_API_VOID
__cilkrts_init(void)
81 // Initialize, but don't start, the cilk runtime.
82 __cilkrts_init_internal(0);
85 CILK_API_VOID
__cilkrts_end_cilk(void)
87 // Take out the global OS mutex while we do this to protect against
88 // another thread attempting to bind while we do this
89 global_os_mutex_lock();
91 if (cilkg_is_published()) {
92 global_state_t
*g
= cilkg_get_global_state();
93 if (g
->Q
|| __cilkrts_get_tls_worker())
94 __cilkrts_bug("Attempt to shut down Cilk while Cilk is still "
96 __cilkrts_stop_workers(g
);
97 __cilkrts_deinit_internal(g
);
100 global_os_mutex_unlock();
104 __cilkrts_get_nworkers()
106 return cilkg_get_nworkers();
110 __cilkrts_get_total_workers()
112 return cilkg_get_total_workers();
115 CILK_API_INT
__cilkrts_get_force_reduce(void)
117 return cilkg_get_force_reduce();
120 CILK_API_INT
__cilkrts_set_param(const char* param
, const char* value
)
122 return cilkg_set_param(param
, value
);
126 CILK_API_INT
__cilkrts_set_param_w(const wchar_t* param
, const wchar_t* value
)
128 return cilkg_set_param_w(param
, value
);
132 /* Return a small integer indicating which Cilk worker the function is
133 * currently running on. Each thread started by the Cilk runtime library
134 * (system worker) has a unique worker number in the range 1..P-1, where P is
135 * the valued returned by __cilkrts_get_nworkers(). All threads started by
136 * the user or by other libraries (user workers) share the worker number 0.
137 * Therefore, the worker number is not unique across multiple user threads.
139 * Implementor's note: The value returned from this function is different from
140 * the value, w->self, used in most debug messages.
143 __cilkrts_get_worker_number(void)
145 __cilkrts_worker
*w
= __cilkrts_get_tls_worker();
148 /* A non-worker always has a worker number of zero. */
150 else if (WORKER_USER
== w
->l
->type
)
151 /* User worker was once a non-worker, so its number should still be
155 /* w->self for a system worker is in range 0..(P-1); adjust to 1..P
156 * to avoid conflicting with the user thread's worker number. */
161 * Internal definition of the pedigree context. The size of the
162 * structure must match __cilkrts_pedigree_context_t defined in abi.i
164 typedef struct pedigree_context_t
166 /** Size of the structure, in bytes */
169 /** Next __cilkrts_pedigree to return */
170 const __cilkrts_pedigree
*pedigree
;
172 /** Unused. Left over from previous implementation */
175 /** Unused. Left over from previous implementation */
178 // // Debugging aid for pedigree-test:
179 // __cilkrts_stack_frame *expected_sf;
180 } pedigree_context_t
;
183 * __cilkrts_get_pedigree_info
185 * Fetch the birthrank for a stack frame. To initialize the walk, both sf_in
186 * and frame_in should be NULL. parent_sf_ptr and parent_frame_ptr provide
187 * context for the stackwalk and should be returned as sf_in and frame_in on
191 * 0 - Success - birthrank, parent_sf_out and parent_frame_out are valid
192 * >1 - Pedigree walk completed
193 * <1 - Failure - -1: No worker bound to thread, -2: Sanity check failed
196 #define PEDIGREE_WALK_COMPLETE (__cilkrts_pedigree *)-1
199 __cilkrts_get_pedigree_info(__cilkrts_pedigree_context_t
*external_context
,
200 uint64_t *sf_birthrank
)
202 pedigree_context_t
*context
= (pedigree_context_t
*)external_context
;
204 CILK_ASSERT(sizeof(__cilkrts_pedigree_context_t
) ==
205 sizeof(pedigree_context_t
));
206 if (context
->size
!= sizeof(pedigree_context_t
))
207 return -3; // Invalid size
209 // If the pointer to the last __cilkrts_pedigree is -1, we've
210 // finished the walk. We're still done.
211 if (PEDIGREE_WALK_COMPLETE
== context
->pedigree
)
214 // The passed in context value contains a pointer to the last
215 // __cilkrts_pedigree returned, or NULL if we're starting a
217 if (NULL
== context
->pedigree
)
219 __cilkrts_worker
*w
= __cilkrts_get_tls_worker();
220 __cilkrts_pedigree
* pedigree_node
;
222 pedigree_node
= &w
->pedigree
;
225 pedigree_node
= __cilkrts_get_tls_pedigree_leaf(1);
227 context
->pedigree
= pedigree_node
->parent
;
230 context
->pedigree
= context
->pedigree
->parent
;
232 // Note: If we want to omit the user root node,
233 // stop at context->pedigree->parent instead.
234 if (NULL
== context
->pedigree
)
236 context
->pedigree
= PEDIGREE_WALK_COMPLETE
;
240 *sf_birthrank
= context
->pedigree
->rank
;
245 __cilkrts_get_pedigree_internal(__cilkrts_worker
*w
)
251 const __cilkrts_pedigree
*pedigree
=
252 __cilkrts_get_tls_pedigree_leaf(1);
258 CILK_API_INT
__cilkrts_bump_worker_rank_internal(__cilkrts_worker
*w
)
260 __cilkrts_pedigree
*pedigree
;
261 pedigree
= (w
? &w
->pedigree
: __cilkrts_get_tls_pedigree_leaf(1));