ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
global.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include "abti.h"
7 
8 /* Must be in a critical section. */
9 ABTU_ret_err static int init_library(void);
10 ABTU_ret_err static int finailze_library(void);
11 static inline ABT_bool is_initialized_library(void);
12 
18 /* Global Data */
20 
21 /* To indicate how many times ABT_init is called. */
22 static uint32_t g_ABTI_num_inits = 0;
23 /* A global lock protecting the initialization/finalization process */
25 /* A flag whether Argobots has been initialized or not */
28 
75 int ABT_init(int argc, char **argv)
76 {
77  ABTI_UNUSED(argc);
78  ABTI_UNUSED(argv);
79 
80  int abt_errno;
81  /* Take a global lock protecting the initialization/finalization process. */
83  /* If Argobots has already been initialized, just return */
84  if (g_ABTI_num_inits > 0) {
86  abt_errno = ABT_SUCCESS;
87  } else {
88  abt_errno = init_library();
89  if (abt_errno == ABT_SUCCESS)
91  }
92  /* Unlock a global lock */
94  ABTI_CHECK_ERROR(abt_errno);
95  return ABT_SUCCESS;
96 }
97 
141 int ABT_finalize(void)
142 {
144 
145  /* Take a global lock protecting the initialization/finalization process. */
147  int abt_errno = finailze_library();
148  /* Unlock a global lock */
150  ABTI_CHECK_ERROR(abt_errno);
151  return ABT_SUCCESS;
152 }
153 
174 {
175  if (is_initialized_library()) {
176  return ABT_SUCCESS;
177  } else {
178  return ABT_ERR_UNINITIALIZED;
179  }
180 }
181 
182 /*****************************************************************************/
183 /* Private APIs */
184 /*****************************************************************************/
185 
186 /* This function must be async-signal-safe. */
188 {
189  return is_initialized_library();
190 }
191 
192 /*****************************************************************************/
193 /* Internal static functions */
194 /*****************************************************************************/
195 
196 ABTU_ret_err static int init_library(void)
197 {
198  int abt_errno, init_stage = 0;
199 
200  ABTI_global *p_global;
201  ABTI_xstream *p_local_xstream;
202 
203  abt_errno = ABTU_malloc(sizeof(ABTI_global), (void **)&p_global);
204  ABTI_CHECK_ERROR(abt_errno);
205  ABTI_global_set_global(p_global);
206 
207  /* Initialize the system environment */
208  ABTD_env_init(p_global);
209 
210  /* Initialize memory pool */
211  abt_errno = ABTI_mem_init(p_global);
212  if (abt_errno != ABT_SUCCESS)
213  goto FAILED;
214  init_stage = 1;
215 
216  /* Initialize IDs */
220 
221 #ifndef ABT_CONFIG_DISABLE_TOOL_INTERFACE
222  /* Initialize the tool interface */
223  ABTD_spinlock_clear(&p_global->tool_writer_lock);
224  p_global->tool_thread_cb_f = NULL;
225  p_global->tool_thread_user_arg = NULL;
226  ABTD_atomic_relaxed_store_uint64(&p_global->tool_thread_event_mask_tagged,
227  0);
228 #endif
229  /* Initialize a unit-to-thread hash table. */
230  ABTI_unit_init_hash_table(p_global);
231 
232  /* Initialize the ES list */
233  p_global->p_xstream_head = NULL;
234  p_global->num_xstreams = 0;
235 
236  /* Initialize a spinlock */
237  ABTD_spinlock_clear(&p_global->xstream_list_lock);
238 
239  /* Create the primary ES */
240  abt_errno = ABTI_xstream_create_primary(p_global, &p_local_xstream);
241  if (abt_errno != ABT_SUCCESS)
242  goto FAILED;
243  init_stage = 2;
244 
245  /* Init the ES local data */
246  ABTI_local_set_xstream(p_local_xstream);
247 
248  /* Create the primary ULT */
249  ABTI_ythread *p_primary_ythread;
250  abt_errno =
252  ABTI_xstream_get_local(p_local_xstream),
253  p_local_xstream, &p_primary_ythread);
254  if (abt_errno != ABT_SUCCESS)
255  goto FAILED;
256  init_stage = 3;
257 
258  /* Set as if p_local_xstream is currently running the primary ULT. */
259  ABTD_atomic_relaxed_store_int(&p_primary_ythread->thread.state,
261  p_primary_ythread->thread.p_last_xstream = p_local_xstream;
262  p_global->p_primary_ythread = p_primary_ythread;
263  p_local_xstream->p_thread = &p_primary_ythread->thread;
264 
265  /* Start the primary ES */
266  ABTI_xstream_start_primary(p_global, &p_local_xstream, p_local_xstream,
267  p_primary_ythread);
268 
269  if (p_global->print_config == ABT_TRUE) {
270  ABTI_info_print_config(p_global, stdout);
271  }
273  return ABT_SUCCESS;
274 FAILED:
275  if (init_stage >= 2) {
276  ABTI_xstream_free(p_global, ABTI_xstream_get_local(p_local_xstream),
277  p_local_xstream, ABT_TRUE);
279  }
280  if (init_stage >= 1) {
281  ABTI_mem_finalize(p_global);
282  }
283  ABTD_affinity_finalize(p_global);
284  ABTU_free(p_global);
286  ABTI_HANDLE_ERROR(abt_errno);
287 }
288 
290 {
291  ABTI_local *p_local = ABTI_local_get_local();
292 
293  /* If Argobots is not initialized, just return */
295  /* If Argobots is still referenced by others, just return */
296  if (--g_ABTI_num_inits != 0) {
297  return ABT_SUCCESS;
298  }
299 
300  ABTI_global *p_global = ABTI_global_get_global();
301  ABTI_xstream *p_local_xstream = ABTI_local_get_xstream_or_null(p_local);
302  /* If called by an external thread, return an error. */
303  ABTI_CHECK_TRUE(!ABTI_IS_EXT_THREAD_ENABLED || p_local_xstream,
305 
308  "ABT_finalize must be called by the primary ES.");
309 
310  ABTI_thread *p_self = p_local_xstream->p_thread;
313  "ABT_finalize must be called by the primary ULT.");
314  ABTI_ythread *p_ythread;
315  ABTI_CHECK_YIELDABLE(p_self, &p_ythread, ABT_ERR_INV_THREAD);
316 
317 #ifndef ABT_CONFIG_DISABLE_TOOL_INTERFACE
318  /* Turns off the tool interface */
321 #endif
322 
323  /* Finish the main scheduler of this local xstream. */
324  ABTI_sched_finish(p_local_xstream->p_main_sched);
325  /* p_self cannot join the main scheduler since p_self needs to be orphaned.
326  * Let's wait till the main scheduler finishes. This thread will be
327  * scheduled when the main root thread finishes. */
328  ABTI_ythread_yield_orphan(&p_local_xstream, p_ythread,
330  ABTI_ASSERT(p_local_xstream == ABTI_local_get_xstream(p_local));
331  ABTI_ASSERT(p_local_xstream->p_thread == p_self);
332 
333  /* Remove the primary ULT */
334  p_local_xstream->p_thread = NULL;
335  ABTI_ythread_free_primary(p_global, ABTI_xstream_get_local(p_local_xstream),
336  p_ythread);
337 
338  /* Free the primary ES */
339  ABTI_xstream_free(p_global, ABTI_xstream_get_local(p_local_xstream),
340  p_local_xstream, ABT_TRUE);
341 
342  /* Finalize the ES local data */
344 
345  /* Free the ES array */
346  ABTI_ASSERT(p_global->p_xstream_head == NULL);
347 
348  /* Finalize the memory pool */
349  ABTI_mem_finalize(p_global);
350 
351  /* Restore the affinity */
352  ABTD_affinity_finalize(p_global);
353 
354  /* Free a unit-to-thread hash table. */
356 
357  /* Free the ABTI_global structure */
358  ABTU_free(p_global);
361  return ABT_SUCCESS;
362 }
363 
364 /* This function must be async-signal-safe. */
365 static inline ABT_bool is_initialized_library(void)
366 {
368  ? ABT_TRUE
369  : ABT_FALSE;
370 }
ABT_ERR_INV_THREAD
#define ABT_ERR_INV_THREAD
Error code: invalid work unit.
Definition: abt.h:186
ABTD_SPINLOCK_STATIC_INITIALIZER
#define ABTD_SPINLOCK_STATIC_INITIALIZER()
Definition: abtd_spinlock.h:13
ABTD_atomic_relaxed_store_uint64
static void ABTD_atomic_relaxed_store_uint64(ABTD_atomic_uint64 *ptr, uint64_t val)
Definition: abtd_atomic.h:1045
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1043
ABTI_ythread_yield_orphan
static void ABTI_ythread_yield_orphan(ABTI_xstream **pp_local_xstream, ABTI_ythread *p_self, ABT_sync_event_type sync_event_type, void *p_sync)
Definition: abti_ythread.h:640
ABT_ERR_UNINITIALIZED
#define ABT_ERR_UNINITIALIZED
Error code: Argobots it not initialized.
Definition: abt.h:97
ABTI_XSTREAM_TYPE_PRIMARY
@ ABTI_XSTREAM_TYPE_PRIMARY
Definition: abti.h:66
ABTD_atomic_acquire_load_uint32
static uint32_t ABTD_atomic_acquire_load_uint32(const ABTD_atomic_uint32 *ptr)
Definition: abtd_atomic.h:928
ABT_ERR_INV_XSTREAM
#define ABT_ERR_INV_XSTREAM
Error code: invalid execution stream.
Definition: abt.h:114
init_library
static ABTU_ret_err int init_library(void)
Definition: global.c:196
ABTI_thread::type
ABTI_thread_type type
Definition: abti.h:426
ABTI_global_get_global
static ABTI_global * ABTI_global_get_global(void)
Definition: abti_global.h:9
gp_ABTI_global
ABTI_global * gp_ABTI_global
Definition: global.c:19
ABTI_pool_reset_id
void ABTI_pool_reset_id(void)
Definition: pool.c:1512
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:136
ABTI_xstream::type
ABTI_xstream_type type
Definition: abti.h:300
ABTI_CHECK_TRUE_MSG
#define ABTI_CHECK_TRUE_MSG(cond, abt_errno, msg)
Definition: abti_error.h:167
ABTI_UNUSED
#define ABTI_UNUSED(a)
Definition: abti.h:128
ABTI_ythread_create_primary
ABTU_ret_err int ABTI_ythread_create_primary(ABTI_global *p_global, ABTI_local *p_local, ABTI_xstream *p_xstream, ABTI_ythread **p_ythread)
Definition: thread.c:2520
ABTI_thread
Definition: abti.h:422
ABTI_CHECK_YIELDABLE
#define ABTI_CHECK_YIELDABLE(p_thread, pp_ythread, abt_errno)
Definition: abti_error.h:155
ABT_finalize
int ABT_finalize(void)
Finalize the Argobots execution environment.
Definition: global.c:141
ABTI_xstream
Definition: abti.h:294
ABTD_spinlock
Definition: abtd_spinlock.h:9
ABTD_ATOMIC_UINT32_STATIC_INITIALIZER
#define ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(val)
Definition: abtd_atomic.h:59
ABTI_mem_init
ABTU_ret_err int ABTI_mem_init(ABTI_global *p_global)
Definition: malloc.c:195
ABT_init
int ABT_init(int argc, char **argv)
Initialize the Argobots execution environment.
Definition: global.c:75
ABTI_THREAD_TYPE_PRIMARY
#define ABTI_THREAD_TYPE_PRIMARY
Definition: abti.h:85
abti.h
ABT_SYNC_EVENT_TYPE_OTHER
@ ABT_SYNC_EVENT_TYPE_OTHER
Definition: abt.h:702
ABTI_thread::state
ABTD_atomic_int state
Definition: abti.h:432
ABTD_spinlock_acquire
static void ABTD_spinlock_acquire(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:28
ABTI_HANDLE_ERROR
#define ABTI_HANDLE_ERROR(n)
Definition: abti_error.h:130
ABTI_xstream_free
void ABTI_xstream_free(ABTI_global *p_global, ABTI_local *p_local, ABTI_xstream *p_xstream, ABT_bool force_free)
Definition: stream.c:1639
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:235
ABTI_sched_finish
void ABTI_sched_finish(ABTI_sched *p_sched)
Definition: sched.c:700
ABTD_atomic_relaxed_store_int
static void ABTD_atomic_relaxed_store_int(ABTD_atomic_int *ptr, int val)
Definition: abtd_atomic.h:996
ABTI_mem_finalize
void ABTI_mem_finalize(ABTI_global *p_global)
Definition: malloc.c:206
ABTI_unit_finalize_hash_table
void ABTI_unit_finalize_hash_table(ABTI_global *p_global)
Definition: unit.c:101
is_initialized_library
static ABT_bool is_initialized_library(void)
Definition: global.c:365
ABTI_xstream::p_main_sched
ABTI_sched * p_main_sched
Definition: abti.h:302
ABTI_sched_reset_id
void ABTI_sched_reset_id(void)
Definition: sched.c:1072
ABTD_atomic_uint32
Definition: abtd_atomic.h:27
ABTI_ASSERT
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
ABTI_local_get_local
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
ABTI_unit_init_hash_table
void ABTI_unit_init_hash_table(ABTI_global *p_global)
Definition: unit.c:96
ABTI_local_set_xstream
static void ABTI_local_set_xstream(ABTI_xstream *p_local_xstream)
Definition: abti_local.h:60
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTD_env_init
void ABTD_env_init(ABTI_global *p_global)
Definition: abtd_env.c:45
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABTD_affinity_finalize
void ABTD_affinity_finalize(ABTI_global *p_global)
Definition: abtd_affinity.c:404
ABTI_global::p_xstream_head
ABTI_xstream * p_xstream_head
Definition: abti.h:226
ABTI_local_get_xstream_or_null
static ABTI_xstream * ABTI_local_get_xstream_or_null(ABTI_local *p_local)
Definition: abti_local.h:77
ABTI_IS_EXT_THREAD_ENABLED
#define ABTI_IS_EXT_THREAD_ENABLED
Definition: abti.h:28
g_ABTI_num_inits
static uint32_t g_ABTI_num_inits
Definition: global.c:22
ABTI_xstream_get_local
static ABTI_local * ABTI_xstream_get_local(ABTI_xstream *p_xstream)
Definition: abti_stream.h:48
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:784
ABTD_spinlock_clear
static void ABTD_spinlock_clear(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:23
ABTI_xstream_start_primary
void ABTI_xstream_start_primary(ABTI_global *p_global, ABTI_xstream **pp_local_xstream, ABTI_xstream *p_xstream, ABTI_ythread *p_ythread)
Definition: stream.c:1597
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:786
ABTI_ythread
Definition: abti.h:456
ABT_initialized
int ABT_initialized(void)
Check if the Argobots execution environment has been initialized.
Definition: global.c:173
ABTI_tool_event_thread_update_callback
static void ABTI_tool_event_thread_update_callback(ABTI_global *p_global, ABT_tool_thread_callback_fn cb_func, uint64_t event_mask, void *user_arg)
Definition: abti_tool.h:54
ABTI_initialized
ABT_bool ABTI_initialized(void)
Definition: global.c:187
ABTI_ythread_free_primary
void ABTI_ythread_free_primary(ABTI_global *p_global, ABTI_local *p_local, ABTI_ythread *p_ythread)
Definition: thread.c:2622
ABTI_UB_ASSERT
#define ABTI_UB_ASSERT(cond)
Definition: abti_error.h:19
ABT_TOOL_EVENT_THREAD_NONE
#define ABT_TOOL_EVENT_THREAD_NONE
Work-unit-event mask: none.
Definition: abt.h:725
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:228
ABTD_spinlock_release
static void ABTD_spinlock_release(ABTD_spinlock *p_lock)
Definition: abtd_spinlock.h:42
ABTI_global_set_global
static void ABTI_global_set_global(ABTI_global *p_global)
Definition: abti_global.h:20
ABTI_thread::p_last_xstream
ABTI_xstream * p_last_xstream
Definition: abti.h:428
finailze_library
static ABTU_ret_err int finailze_library(void)
Definition: global.c:289
ABTI_ythread::thread
ABTI_thread thread
Definition: abti.h:457
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:132
g_ABTI_initialized
static ABTD_atomic_uint32 g_ABTI_initialized
Definition: global.c:26
ABTI_CHECK_TRUE
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:146
ABTI_global
Definition: abti.h:223
ABT_THREAD_STATE_RUNNING
@ ABT_THREAD_STATE_RUNNING
Definition: abt.h:429
ABTI_thread_reset_id
void ABTI_thread_reset_id(void)
Definition: thread.c:2798
ABTD_atomic_release_store_uint32
static void ABTD_atomic_release_store_uint32(ABTD_atomic_uint32 *ptr, uint32_t val)
Definition: abtd_atomic.h:1100
ABTI_local_get_xstream
static ABTI_xstream * ABTI_local_get_xstream(ABTI_local *p_local)
Definition: abti_local.h:86
ABTI_xstream_create_primary
ABTU_ret_err int ABTI_xstream_create_primary(ABTI_global *p_global, ABTI_xstream **pp_xstream)
Definition: stream.c:1572
g_ABTI_init_lock
static ABTD_spinlock g_ABTI_init_lock
Definition: global.c:24
ABTI_info_print_config
void ABTI_info_print_config(ABTI_global *p_global, FILE *fp)
Definition: info.c:1110