[RS6000] PR72802 part 2, reload ICE
[official-gcc.git] / libcilkrts / runtime / global_state.h
blob527a4b54e4504b50d68bdb669b39fe1143d4134e
1 /* global_state.h -*-C++-*-
3 *************************************************************************
5 * Copyright (C) 2009-2016, Intel Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
17 * distribution.
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
44 * not tracked.
46 * We welcome your contributions to this open source project. Thank you
47 * for your assistance in helping us improve Cilk Plus.
48 **************************************************************************/
50 /**
51 * @file global_state.h
53 * @brief The global_state_t structure contains most of the global context
54 * maintained by the Intel Cilk runtime.
57 #ifndef INCLUDED_GLOBAL_STATE_DOT_H
58 #define INCLUDED_GLOBAL_STATE_DOT_H
60 #include <cilk/common.h>
62 #include "frame_malloc.h"
63 #include "stats.h"
64 #include "bug.h"
65 #include "cilk_fiber.h"
67 __CILKRTS_BEGIN_EXTERN_C
69 /**
70 * Non-null place-holder for a stack handle that has no meaningful value.
72 #define PLACEHOLDER_FIBER ((cilk_fiber *) -2)
74 /**
75 * States for record_or_replay
77 enum record_replay_t {
78 RECORD_REPLAY_NONE,
79 RECORD_LOG,
80 REPLAY_LOG
83 /**
84 * @brief Global state structure version.
86 * Since the global state is exposed for debugger access, we need a version
87 * number to let it know that the version of the structure is what it expects
88 * to see. If any of the fields marked as (fixed) below are changed, the
89 * version number needs to be bumped.
91 #define GLOBAL_STATE_VERSION 0
93 /**
94 * @brief The global state is a structure that is shared by all workers in
95 * Cilk.
97 * Make the structure ready for use by calling
98 * cilkg_init_global_state() and then cilkg_publish_global_state().
100 * The same global lock should be held while both of these methods are
101 * called. These methods are split because it is useful to execute
102 * other runtime initialization code in between.
104 * After cilkg_publish_global_state() has completed, Cilk runtime
105 * methods may call cilkg_get_global_state() to look at the published
106 * value without holding the global lock.
108 * Finally, clean up the global state by calling
109 * cilkg_deinit_global_state(). This method should be called only
110 * after all calls to cilkg_get_global_state() have completed, and
111 * while holding the global lock.
113 * Before initialization and after deinitialization, the fields in the
114 * global state have unspecified values, except for a few special
115 * fields labeled "USER SETTING", which can be read and written before
116 * initialization and after deinitialization.
119 struct global_state_t { /* COMMON_PORTABLE */
121 /* Fields described as "(fixed)" should not be changed after
122 * initialization.
125 /*************************************************************************
126 * Note that debugger integration must reach into the
127 * global state! The debugger integration is depending on the
128 * offsets of the addr_size, system_workers, total_workers,
129 * stealing_disabled, sysdep, and workers. If these offsets change, the
130 * debugger integration library will need to be changed to match!!!
131 *************************************************************************/
133 uint16_t addr_size; ///< Number of bytes for an address, used by debugger (fixed)
135 uint16_t version; ///< Version of this structure (fixed)
137 int system_workers; ///< Number of system workers (fixed)
140 * @brief USER SETTING: Maximum number of user workers that can be
141 * bound to cilk workers.
143 * 0 unless set by user. Call cilkg_calc_max_user_workers to get
144 * the value.
146 int max_user_workers;
148 int total_workers; ///< Total number of worker threads allocated (fixed)
150 int workers_running; ///< True when system workers have beens started */
152 /// Set by debugger to disable stealing (fixed)
153 int stealing_disabled;
155 /// System-dependent part of the global state
156 struct global_sysdep_state *sysdep;
158 /// Array of worker structures.
159 __cilkrts_worker **workers;
161 /******* END OF DEBUGGER-INTEGRATION FIELDS ***************/
163 /// Number of frames in each worker's lazy task queue
164 __STDNS size_t ltqsize;
167 * @brief USER SETTING: Force all possible reductions.
169 * TRUE if running a p-tool that requires reducers to call the reduce()
170 * method even if no actual stealing occurs.
172 * When set to TRUE, runtime will simulate steals, forcing calls to the
173 * the reduce() methods of reducers.
176 int force_reduce;
178 /// USER SETTING: Per-worker fiber pool size
179 int fiber_pool_size;
181 /// USER SETTING: Global fiber pool size
182 int global_fiber_pool_size;
185 * @brief TRUE when workers should exit scheduling loop so we can
186 * shut down the runtime and free the global state.
188 * @note @c work_done will be checked *FREQUENTLY* in the scheduling loop
189 * by idle workers. We need to ensure that it's not in a cache line which
190 * may be invalidated by other cores. The surrounding fields are either
191 * constant after initialization or not used until shutdown (stats) so we
192 * should be OK.
194 volatile int work_done;
196 int under_ptool; ///< True when running under a serial PIN tool
198 statistics stats; ///< Statistics on use of runtime
201 * @brief USER SETTING: Maximum number of stacks the runtime will
202 * allocate (apart from those created by the OS when worker
203 * threads are created).
205 * If max_stacks == 0,there is no pre-defined maximum.
207 unsigned max_stacks;
209 /// Size of each stack
210 size_t stack_size;
212 /// Global cache for per-worker memory
213 struct __cilkrts_frame_cache frame_malloc;
215 /// Global fiber pool
216 cilk_fiber_pool fiber_pool;
220 * @brief Track whether the runtime has failed to allocate a
221 * stack.
223 * Setting this flag prevents multiple warnings from being
224 * issued.
226 int failure_to_allocate_stack;
229 * @brief USER SETTING: indicate record or replay log.
230 * Set to NULL if not used in this run.
232 char *record_replay_file_name;
235 * @brief Record/replay state.
236 * Valid states are:
237 * RECORD_REPLAY_NONE - Not recording or replaying a log
238 * RECORD_LOG - Recording a log for replay later
239 * REPLAY_LOG - Replay a log recorded earlier
241 enum record_replay_t record_or_replay;
244 * @brief Buffer to force max_steal_failures to appear on a
245 * different cache line from the previous member variables.
247 * This padding is needed because max_steal_failures is read
248 * constantly and other modified values in the global state will
249 * cause thrashing.
251 char cache_buf[64];
254 * @brief Maximum number of times a thread should fail to steal
255 * before checking if Cilk is shutting down.
257 unsigned int max_steal_failures;
259 /// Pointer to scheduler entry point
260 void (*scheduler)(__cilkrts_worker *w);
263 * @brief Buffer to force P and Q to appear on a different cache
264 * line from the previous member variables.
266 char cache_buf_2[64];
268 int P; ///< USER SETTING: number of system workers + 1 (fixed)
269 int Q; ///< Number of user threads currently bound to workers
273 * @brief Initialize the global state object. This method must both
274 * complete before referencing any fields in the global state, except
275 * those specified as "user-settable values".
277 global_state_t* cilkg_init_global_state();
280 * @brief Publish the global state object, so that
281 * cilkg_is_published can return true.
283 * @param g - the global state created by cilkg_init_global_state() to
284 * publish.
286 * After the global state object has been published, a thread should
287 * not modify this state unless it has exclusive access (i.e., holds
288 * the global lock).
290 void cilkg_publish_global_state(global_state_t* g);
293 * @brief Return true if the global state has been fully initialized
294 * and published, and has not been deinitialized.
296 int cilkg_is_published(void);
299 * @brief De-initializes the global state object. Must be called to free
300 * resources when the global state is no longer needed.
302 void cilkg_deinit_global_state(void);
305 * @brief Returns the global state object. Result is valid only if the
306 * global state has been published (see cilkg_publish_global_state()).
308 static inline
309 global_state_t* cilkg_get_global_state(void)
311 // "private" extern declaration:
312 extern global_state_t *cilkg_singleton_ptr;
314 __CILKRTS_ASSERT(cilkg_singleton_ptr); // Debug only
315 return cilkg_singleton_ptr;
320 * @brief Implementation of __cilkrts_set_params.
322 * Set user controllable parameters
323 * @param param - string specifying parameter to be set
324 * @param value - string specifying new value
325 * @returns One of: CILKG_SET_PARAM_SUCCESS ( = 0),
326 * CILKG_SET_PARAM_UNIMP, CILKG_SET_PARAM_XRANGE,
327 * CILKG_SET_PARAM_INVALID, or CILKG_SET_PARAM_LATE.
329 * @attention The wide character version __cilkrts_set_param_w() is available
330 * only on Windows.
332 * Allowable parameter names:
334 * - "nworkers" - number of processors that should run Cilk code.
335 * The value is a string of digits to be parsed by strtol.
337 * - "force reduce" - test reducer callbacks by allocating new views
338 * for every spawn within which a reducer is accessed. This can
339 * significantly reduce performance. The value is "1" or "true"
340 * to enable, "0" or "false" to disable.
341 * @warning Enabling "force reduce" when running with more than a single
342 * worker is currently broken.
344 * - "max user workers" - (Not publicly documented) Sets the number of slots
345 * allocated for user worker threads
347 * - "local stacks" - (Not publicly documented) Number of stacks we'll hold in
348 * the per-worker stack cache. Range 1 .. 42. See
349 * cilkg_init_global_state for details.
351 * - "shared stacks" - (Not publicly documented) Maximum number of stacks
352 * we'll hold in the global stack cache. Maximum value is 42. See
353 * __cilkrts_make_global_state for details
355 * - "nstacks" - (Not publicly documented at this time, though it may be
356 * exposed in the future) Sets the maximum number of stacks permitted at one
357 * time. If the runtime reaches this maximum, it will cease to allocate
358 * stacks and the app will lose parallelism. 0 means unlimited. Default is
359 * unlimited. Minimum is twice the number of worker threads, though that
360 * cannot be tested at this time.
362 int cilkg_set_param(const char* param, const char* value);
363 #ifdef _WIN32
365 * @brief Implementation of __cilkrts_set_params for Unicode characters on
366 * Windows. See the documentation on @ref cilkg_set_param for more details.
368 * Set user controllable parameters
369 * @param param - string specifying parameter to be set
370 * @param value - string specifying new value
371 * @returns One of: CILKG_SET_PARAM_SUCCESS ( = 0),
372 * CILKG_SET_PARAM_UNIMP, CILKG_SET_PARAM_XRANGE,
373 * CILKG_SET_PARAM_INVALID, or CILKG_SET_PARAM_LATE.
375 int cilkg_set_param_w(const wchar_t* param, const wchar_t* value);
376 #endif
379 * @brief implementation of __cilkrts_get_nworkers()
381 static inline
382 int cilkg_get_nworkers(void)
384 // "private" extern declaration
385 extern global_state_t* cilkg_get_user_settable_values(void);
386 return cilkg_get_user_settable_values()->P;
390 * @brief implementation of __cilkrts_get_total_workers()
392 static inline
393 int cilkg_get_total_workers(void)
395 // "private" extern declaration
396 extern int cilkg_calc_total_workers(void);
398 // This number can fluctate until initialization so we
399 // compute it from scratch
400 return cilkg_calc_total_workers();
404 * @brief implementation of __cilkrts_get_force_reduce()
406 static inline
407 int cilkg_get_force_reduce(void)
409 // "private" extern declaration
410 extern global_state_t* cilkg_get_user_settable_values(void);
411 return cilkg_get_user_settable_values()->force_reduce;
415 * @brief implementation of __cilkrts_get_stack_size()
417 static inline
418 size_t cilkg_get_stack_size(void)
420 // "private" extern declaration
421 extern global_state_t* cilkg_get_user_settable_values(void);
422 return cilkg_get_user_settable_values()->stack_size;
426 * @brief Run the scheduler function stored in the global_state
428 * Look up the scheduler function in global_state and run it. Report a fatal
429 * error if an exception escapes the scheduler function.
431 * @param w - Worker structure to associate with the current thread.
433 * @attention The scheduler field of the global state must be set before this
434 * function is called.
436 void __cilkrts_run_scheduler_with_exceptions(__cilkrts_worker *w);
438 __CILKRTS_END_EXTERN_C
440 #endif // ! defined(INCLUDED_GLOBAL_STATE_DOT_H)