ARGOBOTS
abtd_env.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 #include <unistd.h>
8 #include <strings.h>
9 
10 #define ABTD_KEY_TABLE_DEFAULT_SIZE 4
11 #define ABTD_THREAD_DEFAULT_STACKSIZE 16384
12 #define ABTD_SCHED_DEFAULT_STACKSIZE (4 * 1024 * 1024)
13 #define ABTD_SCHED_EVENT_FREQ 50
14 #define ABTD_SCHED_SLEEP_NSEC 100
15 
16 #define ABTD_OS_PAGE_SIZE (4 * 1024)
17 #define ABTD_HUGE_PAGE_SIZE (2 * 1024 * 1024)
18 #define ABTD_MEM_PAGE_SIZE (2 * 1024 * 1024)
19 #define ABTD_MEM_STACK_PAGE_SIZE (8 * 1024 * 1024)
20 #define ABTD_MEM_MAX_NUM_STACKS 65536
21 
22 void ABTD_env_init(ABTI_global *p_global)
23 {
24  char *env;
25 
26  /* Get the number of available cores in the system */
27  p_global->num_cores = sysconf(_SC_NPROCESSORS_ONLN);
28 
29  /* By default, we use the CPU affinity */
30  p_global->set_affinity = ABT_TRUE;
31  env = getenv("ABT_SET_AFFINITY");
32  if (env == NULL)
33  env = getenv("ABT_ENV_SET_AFFINITY");
34  if (env != NULL) {
35  if (strcmp(env, "0") == 0 || strcasecmp(env, "n") == 0 ||
36  strcasecmp(env, "no") == 0) {
37  p_global->set_affinity = ABT_FALSE;
38  }
39  }
40  if (p_global->set_affinity == ABT_TRUE) {
41  ABTD_affinity_init();
42  }
43 
44 #ifdef ABT_CONFIG_USE_DEBUG_LOG_PRINT
45  /* If the debug log printing is set in configure, logging is turned on by
46  * default. */
47  p_global->use_logging = ABT_TRUE;
48  p_global->use_debug = ABT_TRUE;
49 #else
50  /* Otherwise, logging is not turned on by default. */
51  p_global->use_logging = ABT_FALSE;
52  p_global->use_debug = ABT_FALSE;
53 #endif
54  env = getenv("ABT_USE_LOG");
55  if (env == NULL)
56  env = getenv("ABT_ENV_USE_LOG");
57  if (env != NULL) {
58  if (strcmp(env, "0") == 0 || strcasecmp(env, "n") == 0 ||
59  strcasecmp(env, "no") == 0) {
60  p_global->use_logging = ABT_FALSE;
61  } else {
62  p_global->use_logging = ABT_TRUE;
63  }
64  }
65  env = getenv("ABT_USE_DEBUG");
66  if (env == NULL)
67  env = getenv("ABT_ENV_USE_DEBUG");
68  if (env != NULL) {
69  if (strcmp(env, "0") == 0 || strcasecmp(env, "n") == 0 ||
70  strcasecmp(env, "no") == 0) {
71  p_global->use_debug = ABT_FALSE;
72  } else {
73  p_global->use_debug = ABT_TRUE;
74  }
75  }
76 
77  /* Maximum size of the internal ES array */
78  env = getenv("ABT_MAX_NUM_XSTREAMS");
79  if (env == NULL)
80  env = getenv("ABT_ENV_MAX_NUM_XSTREAMS");
81  if (env != NULL) {
82  p_global->max_xstreams = atoi(env);
83  } else {
84  p_global->max_xstreams = p_global->num_cores;
85  }
86 
87  /* Default key table size */
88  env = getenv("ABT_KEY_TABLE_SIZE");
89  if (env == NULL)
90  env = getenv("ABT_ENV_KEY_TABLE_SIZE");
91  if (env != NULL) {
92  p_global->key_table_size = (int)atoi(env);
93  } else {
94  p_global->key_table_size = ABTD_KEY_TABLE_DEFAULT_SIZE;
95  }
96 
97  /* Default stack size for ULT */
98  env = getenv("ABT_THREAD_STACKSIZE");
99  if (env == NULL)
100  env = getenv("ABT_ENV_THREAD_STACKSIZE");
101  if (env != NULL) {
102  p_global->thread_stacksize = (size_t)atol(env);
103  ABTI_ASSERT(p_global->thread_stacksize >= 512);
104  } else {
105  p_global->thread_stacksize = ABTD_THREAD_DEFAULT_STACKSIZE;
106  }
107 
108  /* Default stack size for scheduler */
109  env = getenv("ABT_SCHED_STACKSIZE");
110  if (env == NULL)
111  env = getenv("ABT_ENV_SCHED_STACKSIZE");
112  if (env != NULL) {
113  p_global->sched_stacksize = (size_t)atol(env);
114  ABTI_ASSERT(p_global->sched_stacksize >= 512);
115  } else {
116  p_global->sched_stacksize = ABTD_SCHED_DEFAULT_STACKSIZE;
117  }
118 
119  /* Default frequency for event checking by the scheduler */
120  env = getenv("ABT_SCHED_EVENT_FREQ");
121  if (env == NULL)
122  env = getenv("ABT_ENV_SCHED_EVENT_FREQ");
123  if (env != NULL) {
124  p_global->sched_event_freq = (uint32_t)atol(env);
125  ABTI_ASSERT(p_global->sched_event_freq >= 1);
126  } else {
127  p_global->sched_event_freq = ABTD_SCHED_EVENT_FREQ;
128  }
129 
130  /* Default nanoseconds for scheduler sleep */
131  env = getenv("ABT_SCHED_SLEEP_NSEC");
132  if (env == NULL)
133  env = getenv("ABT_ENV_SCHED_SLEEP_NSEC");
134  if (env != NULL) {
135  p_global->sched_sleep_nsec = atol(env);
136  ABTI_ASSERT(p_global->sched_sleep_nsec >= 0);
137  } else {
138  p_global->sched_sleep_nsec = ABTD_SCHED_SLEEP_NSEC;
139  }
140 
141  /* Mutex attributes */
142  env = getenv("ABT_MUTEX_MAX_HANDOVERS");
143  if (env == NULL)
144  env = getenv("ABT_ENV_MUTEX_MAX_HANDOVERS");
145  if (env != NULL) {
146  p_global->mutex_max_handovers = (uint32_t)atoi(env);
147  ABTI_ASSERT(p_global->mutex_max_handovers >= 1);
148  } else {
149  p_global->mutex_max_handovers = 64;
150  }
151 
152  env = getenv("ABT_MUTEX_MAX_WAKEUPS");
153  if (env == NULL)
154  env = getenv("ABT_ENV_MUTEX_MAX_WAKEUPS");
155  if (env != NULL) {
156  p_global->mutex_max_wakeups = (uint32_t)atoi(env);
157  ABTI_ASSERT(p_global->mutex_max_wakeups >= 1);
158  } else {
159  p_global->mutex_max_wakeups = 1;
160  }
161 
162  /* OS page size */
163  env = getenv("ABT_OS_PAGE_SIZE");
164  if (env == NULL)
165  env = getenv("ABT_ENV_OS_PAGE_SIZE");
166  if (env != NULL) {
167  p_global->os_page_size = (uint32_t)atol(env);
168  } else {
169  p_global->os_page_size = ABTD_OS_PAGE_SIZE;
170  }
171 
172  /* Huge page size */
173  env = getenv("ABT_HUGE_PAGE_SIZE");
174  if (env == NULL)
175  env = getenv("ABT_ENV_HUGE_PAGE_SIZE");
176  if (env != NULL) {
177  p_global->huge_page_size = (uint32_t)atol(env);
178  } else {
179  p_global->huge_page_size = ABTD_HUGE_PAGE_SIZE;
180  }
181 
182 #ifdef ABT_CONFIG_USE_MEM_POOL
183  /* Page size for memory allocation */
184  env = getenv("ABT_MEM_PAGE_SIZE");
185  if (env == NULL)
186  env = getenv("ABT_ENV_MEM_PAGE_SIZE");
187  if (env != NULL) {
188  p_global->mem_page_size = (uint32_t)atol(env);
189  } else {
190  p_global->mem_page_size = ABTD_MEM_PAGE_SIZE;
191  }
192 
193  /* Stack page size for memory allocation */
194  env = getenv("ABT_MEM_STACK_PAGE_SIZE");
195  if (env == NULL)
196  env = getenv("ABT_ENV_MEM_STACK_PAGE_SIZE");
197  if (env != NULL) {
198  p_global->mem_sp_size = (size_t)atol(env);
199  } else {
200  p_global->mem_sp_size = ABTD_MEM_STACK_PAGE_SIZE;
201  }
202 
203  /* Maximum number of stacks that each ES can keep during execution */
204  env = getenv("ABT_MEM_MAX_NUM_STACKS");
205  if (env == NULL)
206  env = getenv("ABT_ENV_MEM_MAX_NUM_STACKS");
207  if (env != NULL) {
208  p_global->mem_max_stacks = (uint32_t)atol(env);
209  } else {
210  p_global->mem_max_stacks = ABTD_MEM_MAX_NUM_STACKS;
211  }
212 
213  /* How to allocate large pages. The default is to use mmap() for huge
214  * pages and then to fall back to allocate regular pages using mmap() when
215  * huge pages are run out of. */
216  env = getenv("ABT_MEM_LP_ALLOC");
217  if (env == NULL)
218  env = getenv("ABT_ENV_MEM_LP_ALLOC");
219 #if defined(HAVE_MAP_ANONYMOUS) || defined(HAVE_MAP_ANON)
220 #if defined(__x86_64__)
221  int lp_alloc = ABTI_MEM_LP_MMAP_HP_RP;
222 #else
223  /*
224  * If hugepage is used, mmap() needs a correct size of hugepage; otherwise,
225  * error happens on munmap(). However, the default size is for typical
226  * x86/64 machines, not for other architectures, so the error happens when
227  * the hugepage size is different. To run Argobots with default settings
228  * on these architectures, we disable hugepage allocation by default on
229  * non-x86/64 architectures; on such a machine, hugepage settings should
230  * be explicitly enabled via an environmental variable.
231  *
232  * TODO: fix this issue by detecting and setting a correct hugepage size.
233  */
234  int lp_alloc = ABTI_MEM_LP_MALLOC;
235 #endif
236 #else
237  int lp_alloc = ABTI_MEM_LP_MALLOC;
238 #endif
239  if (env != NULL) {
240  if (strcasecmp(env, "malloc") == 0) {
241  lp_alloc = ABTI_MEM_LP_MALLOC;
242 #if defined(HAVE_MAP_ANONYMOUS) || defined(HAVE_MAP_ANON)
243  } else if (strcasecmp(env, "mmap_rp") == 0) {
244  lp_alloc = ABTI_MEM_LP_MMAP_RP;
245  } else if (strcasecmp(env, "mmap_hp_rp") == 0) {
246  lp_alloc = ABTI_MEM_LP_MMAP_HP_RP;
247  } else if (strcasecmp(env, "mmap_hp_thp") == 0) {
248  lp_alloc = ABTI_MEM_LP_MMAP_HP_THP;
249 #endif
250  } else if (strcasecmp(env, "thp") == 0) {
251  lp_alloc = ABTI_MEM_LP_THP;
252  }
253  }
254 
255  /* Check if the requested allocation method is really possible. */
256  if (lp_alloc != ABTI_MEM_LP_MALLOC) {
257  p_global->mem_lp_alloc = ABTI_mem_check_lp_alloc(lp_alloc);
258  } else {
259  p_global->mem_lp_alloc = lp_alloc;
260  }
261 #endif
262 
263  /* Whether to print the configuration on ABT_init() */
264  env = getenv("ABT_PRINT_CONFIG");
265  if (env == NULL)
266  env = getenv("ABT_ENV_PRINT_CONFIG");
267  if (env != NULL) {
268  if (strcmp(env, "1") == 0 || strcasecmp(env, "yes") == 0 ||
269  strcasecmp(env, "y") == 0) {
270  p_global->print_config = ABT_TRUE;
271  } else {
272  p_global->print_config = ABT_FALSE;
273  }
274  } else {
275  p_global->print_config = ABT_FALSE;
276  }
277 
278  /* Init timer */
279  ABTD_time_init();
280 }
#define ABT_FALSE
Definition: abt.h:224
#define ABT_TRUE
Definition: abt.h:223