ARGOBOTS  422b8312b78bcc3a312ddd8ee0ecaf6aa618bbbc
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 
17 /* Global Data */
19 
20 /* To indicate how many times ABT_init is called. */
21 static uint32_t g_ABTI_num_inits = 0;
22 /* A global lock protecting the initialization/finalization process */
24 /* A flag whether Argobots has been initialized or not */
27 
44 int ABT_init(int argc, char **argv)
45 {
46  ABTI_UNUSED(argc);
47  ABTI_UNUSED(argv);
48  /* Take a global lock protecting the initialization/finalization process. */
49  ABTI_spinlock_acquire(&g_ABTI_init_lock);
50  int abt_errno = init_library();
51  /* Unlock a global lock */
52  ABTI_spinlock_release(&g_ABTI_init_lock);
53  ABTI_CHECK_ERROR(abt_errno);
54  return ABT_SUCCESS;
55 }
56 
73 int ABT_finalize(void)
74 {
75  /* Take a global lock protecting the initialization/finalization process. */
76  ABTI_spinlock_acquire(&g_ABTI_init_lock);
77  int abt_errno = finailze_library();
78  /* Unlock a global lock */
79  ABTI_spinlock_release(&g_ABTI_init_lock);
80  ABTI_CHECK_ERROR(abt_errno);
81  return ABT_SUCCESS;
82 }
83 
96 int ABT_initialized(void)
97 {
98  if (ABTD_atomic_acquire_load_uint32(&g_ABTI_initialized) == 0) {
99  return ABT_ERR_UNINITIALIZED;
100  } else {
101  return ABT_SUCCESS;
102  }
103 }
104 
105 /*****************************************************************************/
106 /* Internal static functions */
107 /*****************************************************************************/
108 
109 ABTU_ret_err static int init_library(void)
110 {
111  int abt_errno;
112  /* If Argobots has already been initialized, just return */
113  if (g_ABTI_num_inits++ > 0) {
114  return ABT_SUCCESS;
115  }
116 
117  abt_errno = ABTU_malloc(sizeof(ABTI_global), (void **)&gp_ABTI_global);
118  ABTI_CHECK_ERROR(abt_errno);
119 
120  /* Initialize the system environment */
121  ABTD_env_init(gp_ABTI_global);
122 
123  /* Initialize memory pool */
124  ABTI_mem_init(gp_ABTI_global);
125 
126  /* Initialize IDs */
130 
131 #ifndef ABT_CONFIG_DISABLE_TOOL_INTERFACE
132  /* Initialize the tool interface */
133  ABTI_spinlock_clear(&gp_ABTI_global->tool_writer_lock);
134  gp_ABTI_global->tool_thread_cb_f = NULL;
135  gp_ABTI_global->tool_thread_user_arg = NULL;
136  gp_ABTI_global->tool_task_cb_f = NULL;
137  gp_ABTI_global->tool_task_user_arg = NULL;
138  ABTD_atomic_relaxed_store_uint64(&gp_ABTI_global
139  ->tool_thread_event_mask_tagged,
140  0);
141 #endif
142 
143  /* Initialize the ES list */
144  gp_ABTI_global->p_xstream_head = NULL;
145  gp_ABTI_global->num_xstreams = 0;
146 
147  /* Initialize a spinlock */
148  ABTI_spinlock_clear(&gp_ABTI_global->xstream_list_lock);
149 
150  /* Create the primary ES */
151  ABTI_xstream *p_local_xstream;
152  abt_errno = ABTI_xstream_create_primary(&p_local_xstream);
153  ABTI_CHECK_ERROR(abt_errno);
154 
155  /* Init the ES local data */
156  ABTI_local_set_xstream(p_local_xstream);
157 
158  /* Create the primary ULT, i.e., the main thread */
159  ABTI_ythread *p_main_ythread;
160  abt_errno =
162  p_local_xstream, &p_main_ythread);
163  /* Set as if p_local_xstream is currently running the main thread. */
164  ABTD_atomic_relaxed_store_int(&p_main_ythread->thread.state,
166  p_main_ythread->thread.p_last_xstream = p_local_xstream;
167  ABTI_CHECK_ERROR(abt_errno);
168  gp_ABTI_global->p_main_ythread = p_main_ythread;
169  p_local_xstream->p_thread = &p_main_ythread->thread;
170 
171  /* Start the primary ES */
172  ABTI_xstream_start_primary(&p_local_xstream, p_local_xstream,
173  p_main_ythread);
174 
175  if (gp_ABTI_global->print_config == ABT_TRUE) {
176  ABTI_info_print_config(stdout);
177  }
178  ABTD_atomic_release_store_uint32(&g_ABTI_initialized, 1);
179  return ABT_SUCCESS;
180 }
181 
183 {
184  ABTI_local *p_local = ABTI_local_get_local();
185 
186  /* If Argobots is not initialized, just return */
188  /* If Argobots is still referenced by others, just return */
189  if (--g_ABTI_num_inits != 0) {
190  return ABT_SUCCESS;
191  }
192 
193  ABTI_xstream *p_local_xstream = ABTI_local_get_xstream_or_null(p_local);
194  /* If called by an external thread, return an error. */
195  ABTI_CHECK_TRUE(!ABTI_IS_EXT_THREAD_ENABLED || p_local_xstream,
197 
200  "ABT_finalize must be called by the primary ES.");
201 
202  ABTI_thread *p_self = p_local_xstream->p_thread;
205  "ABT_finalize must be called by the primary ULT.");
206  ABTI_ythread *p_ythread;
207  ABTI_CHECK_YIELDABLE(p_self, &p_ythread, ABT_ERR_INV_THREAD);
208 
209 #ifndef ABT_CONFIG_DISABLE_TOOL_INTERFACE
210  /* Turns off the tool interface */
212  NULL);
214 #endif
215 
216  /* Set the orphan request for the primary ULT */
218  /* Finish the main scheduler of this local xstream. */
219  ABTI_sched_finish(p_local_xstream->p_main_sched);
220  /* p_self cannot join the main scheduler since p_self needs to be orphaned.
221  * Let's wait till the main scheduler finishes. This thread will be
222  * scheduled when the main root thread finishes. */
223  ABTI_ythread_yield(&p_local_xstream, p_ythread, ABT_SYNC_EVENT_TYPE_OTHER,
224  NULL);
225  ABTI_ASSERT(p_local_xstream == ABTI_local_get_xstream(p_local));
226  ABTI_ASSERT(p_local_xstream->p_thread == p_self);
227 
228  /* Remove the primary ULT */
229  p_local_xstream->p_thread = NULL;
230  ABTI_ythread_free_main(ABTI_xstream_get_local(p_local_xstream), p_ythread);
231 
232  /* Free the primary ES */
233  ABTI_xstream_free(ABTI_xstream_get_local(p_local_xstream), p_local_xstream,
234  ABT_TRUE);
235 
236  /* Finalize the ES local data */
238 
239  /* Free the ES array */
240  ABTI_ASSERT(gp_ABTI_global->p_xstream_head == NULL);
241 
242  /* Finalize the memory pool */
243  ABTI_mem_finalize(gp_ABTI_global);
244 
245  /* Restore the affinity */
246  if (gp_ABTI_global->set_affinity == ABT_TRUE) {
248  }
249 
250  /* Free the ABTI_global structure */
251  ABTU_free(gp_ABTI_global);
252  gp_ABTI_global = NULL;
253  ABTD_atomic_release_store_uint32(&g_ABTI_initialized, 0);
254  return ABT_SUCCESS;
255 }
int ABT_init(int argc, char **argv) ABT_API_PUBLIC
Initialize the Argobots execution environment.
Definition: global.c:44
static ABTU_ret_err int init_library(void)
Definition: global.c:109
void ABTD_affinity_finalize(void)
void ABTI_mem_init(ABTI_global *p_global)
Definition: malloc.c:160
static ABTD_atomic_uint32 g_ABTI_initialized
Definition: global.c:25
static void ABTI_tool_event_task_update_callback(ABT_tool_task_callback_fn cb_func, uint64_t event_mask_task, void *user_arg)
Definition: abti_tool.h:100
struct ABTI_local ABTI_local
Definition: abti.h:101
static ABTI_spinlock g_ABTI_init_lock
Definition: global.c:23
ABTI_thread_type type
Definition: abti.h:319
void ABTI_sched_reset_id(void)
Definition: sched.c:720
#define ABT_ERR_INV_THREAD
Definition: abt.h:80
ABTI_thread * p_thread
Definition: abti.h:254
void ABTI_thread_reset_id(void)
Definition: thread.c:1627
#define ABT_TOOL_EVENT_THREAD_NONE
Definition: abt.h:264
static void ABTI_thread_set_request(ABTI_thread *p_thread, uint32_t req)
Definition: abti_thread.h:68
static void ABTD_atomic_release_store_uint32(ABTD_atomic_uint32 *ptr, uint32_t val)
Definition: abtd_atomic.h:1100
static void ABTI_tool_event_thread_update_callback(ABT_tool_thread_callback_fn cb_func, uint64_t event_mask_thread, void *user_arg)
Definition: abti_tool.h:54
static void ABTI_spinlock_clear(ABTI_spinlock *p_lock)
Definition: abti_spinlock.h:23
ABTI_xstream_type type
Definition: abti.h:243
void ABTI_ythread_free_main(ABTI_local *p_local, ABTI_ythread *p_ythread)
Definition: thread.c:1484
ABTU_ret_err int ABTI_ythread_create_main(ABTI_local *p_local, ABTI_xstream *p_xstream, ABTI_ythread **p_ythread)
Definition: thread.c:1378
void ABTD_env_init(ABTI_global *p_global)
Definition: abtd_env.c:44
#define ABTI_CHECK_YIELDABLE(p_thread, pp_ythread, abt_errno)
Definition: abti_error.h:146
#define ABTI_SPINLOCK_STATIC_INITIALIZER()
Definition: abti_spinlock.h:13
ABTI_sched * p_main_sched
Definition: abti.h:245
void ABTI_xstream_start_primary(ABTI_xstream **pp_local_xstream, ABTI_xstream *p_xstream, ABTI_ythread *p_ythread)
Definition: stream.c:875
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:218
#define ABTI_CHECK_TRUE_MSG(cond, abt_errno, msg)
Definition: abti_error.h:158
ABT_bool set_affinity
Definition: abti.h:181
ABTI_spinlock xstream_list_lock
Definition: abti.h:177
#define ABTI_THREAD_TYPE_MAIN
Definition: abti.h:75
ABTI_xstream * p_last_xstream
Definition: abti.h:321
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:137
#define ABTI_UNUSED(a)
Definition: abti.h:97
static uint32_t ABTD_atomic_acquire_load_uint32(const ABTD_atomic_uint32 *ptr)
Definition: abtd_atomic.h:928
ABTD_atomic_int state
Definition: abti.h:325
#define ABTI_IS_EXT_THREAD_ENABLED
Definition: abti.h:28
static uint32_t g_ABTI_num_inits
Definition: global.c:21
static void ABTI_spinlock_release(ABTI_spinlock *p_lock)
Definition: abti_spinlock.h:42
int ABT_finalize(void) ABT_API_PUBLIC
Terminate the Argobots execution environment.
Definition: global.c:73
ABTI_global * gp_ABTI_global
Definition: global.c:18
ABTI_thread thread
Definition: abti.h:351
#define ABT_SUCCESS
Definition: abt.h:64
ABTI_xstream * p_xstream_head
Definition: abti.h:175
#define ABT_TRUE
Definition: abt.h:288
static ABTU_ret_err int finailze_library(void)
Definition: global.c:182
void ABTI_mem_finalize(ABTI_global *p_global)
Definition: malloc.c:168
#define ABT_ERR_UNINITIALIZED
Definition: abt.h:65
ABT_bool print_config
Definition: abti.h:214
static void ABTI_spinlock_acquire(ABTI_spinlock *p_lock)
Definition: abti_spinlock.h:28
void ABTI_info_print_config(FILE *fp)
Definition: info.c:618
static void ABTD_atomic_relaxed_store_uint64(ABTD_atomic_uint64 *ptr, uint64_t val)
Definition: abtd_atomic.h:1045
ABTU_ret_err int ABTI_xstream_create_primary(ABTI_xstream **pp_xstream)
Definition: stream.c:855
#define ABT_TOOL_EVENT_TASK_NONE
Definition: abt.h:277
static void ABTI_local_set_xstream(ABTI_xstream *p_local_xstream)
Definition: abti_local.h:60
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:127
static void ABTD_atomic_relaxed_store_int(ABTD_atomic_int *ptr, int val)
Definition: abtd_atomic.h:996
void ABTI_xstream_free(ABTI_local *p_local, ABTI_xstream *p_xstream, ABT_bool force_free)
Definition: stream.c:938
static ABTI_xstream * ABTI_local_get_xstream(ABTI_local *p_local)
Definition: abti_local.h:86
void ABTI_sched_finish(ABTI_sched *p_sched)
Definition: sched.c:344
ABTI_ythread * p_main_ythread
Definition: abti.h:189
static void ABTI_ythread_yield(ABTI_xstream **pp_local_xstream, ABTI_ythread *p_ythread, ABT_sync_event_type sync_event_type, void *p_sync)
Definition: abti_ythread.h:350
void ABTI_pool_reset_id(void)
Definition: pool.c:467
#define ABT_ERR_INV_XSTREAM
Definition: abt.h:68
static ABTI_local * ABTI_xstream_get_local(ABTI_xstream *p_xstream)
Definition: abti_stream.h:67
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
static void ABTU_free(void *ptr)
Definition: abtu.h:211
#define ABTI_THREAD_REQ_ORPHAN
Definition: abti.h:46
int num_xstreams
Definition: abti.h:174
#define ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(val)
Definition: abtd_atomic.h:59
#define ABTU_ret_err
Definition: abtu.h:138
static ABTI_xstream * ABTI_local_get_xstream_or_null(ABTI_local *p_local)
Definition: abti_local.h:77
int ABT_initialized(void) ABT_API_PUBLIC
Check whether ABT_init() has been called.
Definition: global.c:96