annotate libcilkrts/runtime/cilk_api.c @ 143:76e1cf5455ef

add cbc_gc test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 19:24:05 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* cilk_api.c -*-C-*-
kono
parents:
diff changeset
2 *
kono
parents:
diff changeset
3 *************************************************************************
kono
parents:
diff changeset
4 *
kono
parents:
diff changeset
5 * Copyright (C) 2009-2016, Intel Corporation
kono
parents:
diff changeset
6 * All rights reserved.
kono
parents:
diff changeset
7 *
kono
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
kono
parents:
diff changeset
10 * are met:
kono
parents:
diff changeset
11 *
kono
parents:
diff changeset
12 * * Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
14 * * Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
16 * the documentation and/or other materials provided with the
kono
parents:
diff changeset
17 * distribution.
kono
parents:
diff changeset
18 * * Neither the name of Intel Corporation nor the names of its
kono
parents:
diff changeset
19 * contributors may be used to endorse or promote products derived
kono
parents:
diff changeset
20 * from this software without specific prior written permission.
kono
parents:
diff changeset
21 *
kono
parents:
diff changeset
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
kono
parents:
diff changeset
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
kono
parents:
diff changeset
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
kono
parents:
diff changeset
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
kono
parents:
diff changeset
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
kono
parents:
diff changeset
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
kono
parents:
diff changeset
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
kono
parents:
diff changeset
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
kono
parents:
diff changeset
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
kono
parents:
diff changeset
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
kono
parents:
diff changeset
32 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
33 * POSSIBILITY OF SUCH DAMAGE.
kono
parents:
diff changeset
34 *
kono
parents:
diff changeset
35 * *********************************************************************
kono
parents:
diff changeset
36 *
kono
parents:
diff changeset
37 * PLEASE NOTE: This file is a downstream copy of a file mainitained in
kono
parents:
diff changeset
38 * a repository at cilkplus.org. Changes made to this file that are not
kono
parents:
diff changeset
39 * submitted through the contribution process detailed at
kono
parents:
diff changeset
40 * http://www.cilkplus.org/submit-cilk-contribution will be lost the next
kono
parents:
diff changeset
41 * time that a new version is released. Changes only submitted to the
kono
parents:
diff changeset
42 * GNU compiler collection or posted to the git repository at
kono
parents:
diff changeset
43 * https://bitbucket.org/intelcilkruntime/intel-cilk-runtime.git are
kono
parents:
diff changeset
44 * not tracked.
kono
parents:
diff changeset
45 *
kono
parents:
diff changeset
46 * We welcome your contributions to this open source project. Thank you
kono
parents:
diff changeset
47 * for your assistance in helping us improve Cilk Plus.
kono
parents:
diff changeset
48 **************************************************************************/
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 /*
kono
parents:
diff changeset
51 * Implementation of functions declared in cilk_api.h
kono
parents:
diff changeset
52 */
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 /*
kono
parents:
diff changeset
55 * Define the COMPILING_CILK_ABI_FUNCTIONS macro, so that
kono
parents:
diff changeset
56 * compilation of this file generates non-inlined definitions for the
kono
parents:
diff changeset
57 * functions marked as CILK_EXPORT_AND_INLINE in cilk_api.h.
kono
parents:
diff changeset
58 *
kono
parents:
diff changeset
59 * We must deal with these functions differently because we need to
kono
parents:
diff changeset
60 * continue to ship nonlined versions of these functions.
kono
parents:
diff changeset
61 *
kono
parents:
diff changeset
62 * CILK_EXPORT_AND_INLINE int __cilkrts_get_worker_rank(uint64_t *rank);
kono
parents:
diff changeset
63 * CILK_EXPORT_AND_INLINE int __cilkrts_bump_worker_rank();
kono
parents:
diff changeset
64 * CILK_EXPORT_AND_INLINE int __cilkrts_bump_loop_rank();
kono
parents:
diff changeset
65 */
kono
parents:
diff changeset
66 #define COMPILING_CILK_API_FUNCTIONS
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 #include <internal/abi.h>
kono
parents:
diff changeset
69 #include <cilk/cilk_api.h>
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 #include "os.h"
kono
parents:
diff changeset
72 #include "os_mutex.h"
kono
parents:
diff changeset
73 #include "bug.h"
kono
parents:
diff changeset
74 #include "global_state.h"
kono
parents:
diff changeset
75 #include "local_state.h"
kono
parents:
diff changeset
76 #include "scheduler.h"
kono
parents:
diff changeset
77 #include "sysdep.h"
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 CILK_API_VOID __cilkrts_init(void)
kono
parents:
diff changeset
80 {
kono
parents:
diff changeset
81 // Initialize, but don't start, the cilk runtime.
kono
parents:
diff changeset
82 __cilkrts_init_internal(0);
kono
parents:
diff changeset
83 }
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 CILK_API_VOID __cilkrts_end_cilk(void)
kono
parents:
diff changeset
86 {
kono
parents:
diff changeset
87 // Take out the global OS mutex while we do this to protect against
kono
parents:
diff changeset
88 // another thread attempting to bind while we do this
kono
parents:
diff changeset
89 global_os_mutex_lock();
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 if (cilkg_is_published()) {
kono
parents:
diff changeset
92 global_state_t *g = cilkg_get_global_state();
kono
parents:
diff changeset
93 if (g->Q || __cilkrts_get_tls_worker())
kono
parents:
diff changeset
94 __cilkrts_bug("Attempt to shut down Cilk while Cilk is still "
kono
parents:
diff changeset
95 "running");
kono
parents:
diff changeset
96 __cilkrts_stop_workers(g);
kono
parents:
diff changeset
97 __cilkrts_deinit_internal(g);
kono
parents:
diff changeset
98 }
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 global_os_mutex_unlock();
kono
parents:
diff changeset
101 }
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 CILK_API_INT
kono
parents:
diff changeset
104 __cilkrts_get_nworkers()
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 return cilkg_get_nworkers();
kono
parents:
diff changeset
107 }
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 CILK_API_INT
kono
parents:
diff changeset
110 __cilkrts_get_total_workers()
kono
parents:
diff changeset
111 {
kono
parents:
diff changeset
112 return cilkg_get_total_workers();
kono
parents:
diff changeset
113 }
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 CILK_API_INT __cilkrts_get_force_reduce(void)
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 return cilkg_get_force_reduce();
kono
parents:
diff changeset
118 }
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 CILK_API_INT __cilkrts_set_param(const char* param, const char* value)
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 return cilkg_set_param(param, value);
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 #ifdef _WIN32
kono
parents:
diff changeset
126 CILK_API_INT __cilkrts_set_param_w(const wchar_t* param, const wchar_t* value)
kono
parents:
diff changeset
127 {
kono
parents:
diff changeset
128 return cilkg_set_param_w(param, value);
kono
parents:
diff changeset
129 }
kono
parents:
diff changeset
130 #endif // _WIN32
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 /* Return a small integer indicating which Cilk worker the function is
kono
parents:
diff changeset
133 * currently running on. Each thread started by the Cilk runtime library
kono
parents:
diff changeset
134 * (system worker) has a unique worker number in the range 1..P-1, where P is
kono
parents:
diff changeset
135 * the valued returned by __cilkrts_get_nworkers(). All threads started by
kono
parents:
diff changeset
136 * the user or by other libraries (user workers) share the worker number 0.
kono
parents:
diff changeset
137 * Therefore, the worker number is not unique across multiple user threads.
kono
parents:
diff changeset
138 *
kono
parents:
diff changeset
139 * Implementor's note: The value returned from this function is different from
kono
parents:
diff changeset
140 * the value, w->self, used in most debug messages.
kono
parents:
diff changeset
141 */
kono
parents:
diff changeset
142 CILK_API_INT
kono
parents:
diff changeset
143 __cilkrts_get_worker_number(void)
kono
parents:
diff changeset
144 {
kono
parents:
diff changeset
145 __cilkrts_worker *w = __cilkrts_get_tls_worker();
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 if (0 == w)
kono
parents:
diff changeset
148 /* A non-worker always has a worker number of zero. */
kono
parents:
diff changeset
149 return 0;
kono
parents:
diff changeset
150 else if (WORKER_USER == w->l->type)
kono
parents:
diff changeset
151 /* User worker was once a non-worker, so its number should still be
kono
parents:
diff changeset
152 * zero. */
kono
parents:
diff changeset
153 return 0;
kono
parents:
diff changeset
154 else
kono
parents:
diff changeset
155 /* w->self for a system worker is in range 0..(P-1); adjust to 1..P
kono
parents:
diff changeset
156 * to avoid conflicting with the user thread's worker number. */
kono
parents:
diff changeset
157 return w->self + 1;
kono
parents:
diff changeset
158 }
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 /**
kono
parents:
diff changeset
161 * Internal definition of the pedigree context. The size of the
kono
parents:
diff changeset
162 * structure must match __cilkrts_pedigree_context_t defined in abi.i
kono
parents:
diff changeset
163 */
kono
parents:
diff changeset
164 typedef struct pedigree_context_t
kono
parents:
diff changeset
165 {
kono
parents:
diff changeset
166 /** Size of the structure, in bytes */
kono
parents:
diff changeset
167 size_t size;
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 /** Next __cilkrts_pedigree to return */
kono
parents:
diff changeset
170 const __cilkrts_pedigree *pedigree;
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 /** Unused. Left over from previous implementation */
kono
parents:
diff changeset
173 void *unused1;
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 /** Unused. Left over from previous implementation */
kono
parents:
diff changeset
176 void *unused2;
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 // // Debugging aid for pedigree-test:
kono
parents:
diff changeset
179 // __cilkrts_stack_frame *expected_sf;
kono
parents:
diff changeset
180 } pedigree_context_t;
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 /*
kono
parents:
diff changeset
183 * __cilkrts_get_pedigree_info
kono
parents:
diff changeset
184 *
kono
parents:
diff changeset
185 * Fetch the birthrank for a stack frame. To initialize the walk, both sf_in
kono
parents:
diff changeset
186 * and frame_in should be NULL. parent_sf_ptr and parent_frame_ptr provide
kono
parents:
diff changeset
187 * context for the stackwalk and should be returned as sf_in and frame_in on
kono
parents:
diff changeset
188 * the next call.
kono
parents:
diff changeset
189 *
kono
parents:
diff changeset
190 * Returns:
kono
parents:
diff changeset
191 * 0 - Success - birthrank, parent_sf_out and parent_frame_out are valid
kono
parents:
diff changeset
192 * >1 - Pedigree walk completed
kono
parents:
diff changeset
193 * <1 - Failure - -1: No worker bound to thread, -2: Sanity check failed
kono
parents:
diff changeset
194 */
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 #define PEDIGREE_WALK_COMPLETE (__cilkrts_pedigree *)-1
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 CILK_API_INT
kono
parents:
diff changeset
199 __cilkrts_get_pedigree_info(__cilkrts_pedigree_context_t *external_context,
kono
parents:
diff changeset
200 uint64_t *sf_birthrank)
kono
parents:
diff changeset
201 {
kono
parents:
diff changeset
202 pedigree_context_t *context = (pedigree_context_t *)external_context;
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 CILK_ASSERT(sizeof(__cilkrts_pedigree_context_t) ==
kono
parents:
diff changeset
205 sizeof(pedigree_context_t));
kono
parents:
diff changeset
206 if (context->size != sizeof(pedigree_context_t))
kono
parents:
diff changeset
207 return -3; // Invalid size
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 // If the pointer to the last __cilkrts_pedigree is -1, we've
kono
parents:
diff changeset
210 // finished the walk. We're still done.
kono
parents:
diff changeset
211 if (PEDIGREE_WALK_COMPLETE == context->pedigree)
kono
parents:
diff changeset
212 return 1;
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 // The passed in context value contains a pointer to the last
kono
parents:
diff changeset
215 // __cilkrts_pedigree returned, or NULL if we're starting a
kono
parents:
diff changeset
216 // new walk
kono
parents:
diff changeset
217 if (NULL == context->pedigree)
kono
parents:
diff changeset
218 {
kono
parents:
diff changeset
219 __cilkrts_worker *w = __cilkrts_get_tls_worker();
kono
parents:
diff changeset
220 __cilkrts_pedigree* pedigree_node;
kono
parents:
diff changeset
221 if (NULL != w) {
kono
parents:
diff changeset
222 pedigree_node = &w->pedigree;
kono
parents:
diff changeset
223 }
kono
parents:
diff changeset
224 else {
kono
parents:
diff changeset
225 pedigree_node = __cilkrts_get_tls_pedigree_leaf(1);
kono
parents:
diff changeset
226 }
kono
parents:
diff changeset
227 context->pedigree = pedigree_node->parent;
kono
parents:
diff changeset
228 }
kono
parents:
diff changeset
229 else
kono
parents:
diff changeset
230 context->pedigree = context->pedigree->parent;
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 // Note: If we want to omit the user root node,
kono
parents:
diff changeset
233 // stop at context->pedigree->parent instead.
kono
parents:
diff changeset
234 if (NULL == context->pedigree)
kono
parents:
diff changeset
235 {
kono
parents:
diff changeset
236 context->pedigree = PEDIGREE_WALK_COMPLETE;
kono
parents:
diff changeset
237 return 1;
kono
parents:
diff changeset
238 }
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 *sf_birthrank = context->pedigree->rank;
kono
parents:
diff changeset
241 return 0;
kono
parents:
diff changeset
242 }
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 CILK_API_PEDIGREE
kono
parents:
diff changeset
245 __cilkrts_get_pedigree_internal(__cilkrts_worker *w)
kono
parents:
diff changeset
246 {
kono
parents:
diff changeset
247 if (NULL != w) {
kono
parents:
diff changeset
248 return w->pedigree;
kono
parents:
diff changeset
249 }
kono
parents:
diff changeset
250 else {
kono
parents:
diff changeset
251 const __cilkrts_pedigree *pedigree =
kono
parents:
diff changeset
252 __cilkrts_get_tls_pedigree_leaf(1);
kono
parents:
diff changeset
253 return *pedigree;
kono
parents:
diff changeset
254 }
kono
parents:
diff changeset
255 }
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 CILK_API_INT __cilkrts_bump_worker_rank_internal(__cilkrts_worker *w)
kono
parents:
diff changeset
259 {
kono
parents:
diff changeset
260 __cilkrts_pedigree *pedigree;
kono
parents:
diff changeset
261 pedigree = (w ? &w->pedigree : __cilkrts_get_tls_pedigree_leaf(1));
kono
parents:
diff changeset
262 pedigree->rank++;
kono
parents:
diff changeset
263 return 0;
kono
parents:
diff changeset
264 }
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 /* End cilk_api.c */