ARGOBOTS  1059a7c2eb7e3f99f736a9c3a4f6ea476ac1b804
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
basic_wait.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 
12 static int sched_init(ABT_sched sched, ABT_sched_config config);
13 static void sched_run(ABT_sched sched);
14 static int sched_free(ABT_sched);
15 static void sched_sort_pools(int num_pools, ABT_pool *pools);
16 
19  .init = sched_init,
20  .run = sched_run,
21  .free = sched_free,
22  .get_migr_pool = NULL,
23 };
24 
25 typedef struct {
26  uint32_t event_freq;
27  int num_pools;
28  ABT_pool *pools;
29 } sched_data;
30 
32  .type =
34 
35 ABT_sched_def *ABTI_sched_get_basic_wait_def(void)
36 {
37  return &sched_basic_wait_def;
38 }
39 
40 static inline sched_data *sched_data_get_ptr(void *data)
41 {
42  return (sched_data *)data;
43 }
44 
45 static int sched_init(ABT_sched sched, ABT_sched_config config)
46 {
47  int abt_errno;
48  int num_pools;
49 
50  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
51  ABTI_CHECK_NULL_SCHED_PTR(p_sched);
52 
53  /* Default settings */
54  sched_data *p_data;
55  abt_errno = ABTU_malloc(sizeof(sched_data), (void **)&p_data);
56  ABTI_CHECK_ERROR(abt_errno);
57 
58  p_data->event_freq = gp_ABTI_global->sched_event_freq;
59 
60  /* Set the variables from the config */
61  void *p_event_freq = &p_data->event_freq;
62  abt_errno = ABTI_sched_config_read(config, 1, 1, &p_event_freq);
63  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
64  ABTU_free(p_data);
65  ABTI_CHECK_ERROR(abt_errno);
66  }
67 
68  /* Save the list of pools */
69  num_pools = p_sched->num_pools;
70  p_data->num_pools = num_pools;
71  abt_errno =
72  ABTU_malloc(num_pools * sizeof(ABT_pool), (void **)&p_data->pools);
73  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
74  ABTU_free(p_data);
75  ABTI_CHECK_ERROR(abt_errno);
76  }
77  memcpy(p_data->pools, p_sched->pools, sizeof(ABT_pool) * num_pools);
78 
79  /* Sort pools according to their access mode so the scheduler can execute
80  work units from the private pools. */
81  if (num_pools > 1) {
82  sched_sort_pools(num_pools, p_data->pools);
83  }
84 
85  p_sched->data = p_data;
86  return ABT_SUCCESS;
87 }
88 
89 static void sched_run(ABT_sched sched)
90 {
91  ABTI_xstream *p_local_xstream =
92  ABTI_local_get_xstream(ABTI_local_get_local());
93  uint32_t work_count = 0;
94  sched_data *p_data;
95  uint32_t event_freq;
96  int num_pools;
97  ABT_pool *pools;
98  int i;
99  int run_cnt_nowait;
100 
101  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
102  ABTI_ASSERT(p_sched);
103 
104  p_data = sched_data_get_ptr(p_sched->data);
105  event_freq = p_data->event_freq;
106  num_pools = p_data->num_pools;
107  pools = p_data->pools;
108 
109  while (1) {
110  run_cnt_nowait = 0;
111 
112  /* Execute one work unit from the scheduler's pool */
113  for (i = 0; i < num_pools; i++) {
114  ABT_pool pool = pools[i];
115  ABTI_pool *p_pool = ABTI_pool_get_ptr(pool);
116  /* Pop one work unit */
117  ABT_unit unit = ABTI_pool_pop(p_pool);
118  if (unit != ABT_UNIT_NULL) {
119  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
120  run_cnt_nowait++;
121  break;
122  }
123  }
124 
125  /* Block briefly on pop_wait() if we didn't find work to do in main loop
126  * above. */
127  if (!run_cnt_nowait) {
128  ABT_unit unit =
129  ABTI_pool_pop_wait(ABTI_pool_get_ptr(pools[0]), 0.1);
130  if (unit != ABT_UNIT_NULL) {
131  ABTI_xstream_run_unit(&p_local_xstream, unit,
132  ABTI_pool_get_ptr(pools[0]));
133  break;
134  }
135  }
136 
137  /* If run_cnt_nowait is zero, that means that no units were found in
138  * first pass through pools and we must have called pop_wait above. We
139  * should check events regardless of work_count in that case for them to
140  * be processed in a timely manner. */
141  if (!run_cnt_nowait || (++work_count >= event_freq)) {
142  ABTI_xstream_check_events(p_local_xstream, p_sched);
143  ABTI_local *p_local = ABTI_xstream_get_local(p_local_xstream);
144  if (ABTI_sched_has_to_stop(&p_local, p_sched) == ABT_TRUE)
145  break;
146  p_local_xstream = ABTI_local_get_xstream(p_local);
147  work_count = 0;
148  }
149  }
150 }
151 
152 static int sched_free(ABT_sched sched)
153 {
154  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
155  ABTI_ASSERT(p_sched);
156 
157  sched_data *p_data = sched_data_get_ptr(p_sched->data);
158  ABTU_free(p_data->pools);
159  ABTU_free(p_data);
160  return ABT_SUCCESS;
161 }
162 
163 static int pool_get_access_num(ABT_pool *p_pool)
164 {
165  ABT_pool_access access;
166  int num = 0;
167 
168  access = ABTI_pool_get_ptr(*p_pool)->access;
169  switch (access) {
171  num = 0;
172  break;
175  num = 1;
176  break;
179  num = 2;
180  break;
181  default:
182  ABTI_ASSERT(0);
184  }
185 
186  return num;
187 }
188 
189 static int sched_cmp_pools(const void *p1, const void *p2)
190 {
191  int p1_access, p2_access;
192 
193  p1_access = pool_get_access_num((ABT_pool *)p1);
194  p2_access = pool_get_access_num((ABT_pool *)p2);
195 
196  if (p1_access > p2_access) {
197  return 1;
198  } else if (p1_access < p2_access) {
199  return -1;
200  } else {
201  return 0;
202  }
203 }
204 
205 static void sched_sort_pools(int num_pools, ABT_pool *pools)
206 {
207  qsort(pools, num_pools, sizeof(ABT_pool), sched_cmp_pools);
208 }
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:337
struct ABT_sched_opaque * ABT_sched
Definition: abt.h:319
#define ABTU_unreachable()
Definition: abtu.h:25
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: basic_wait.c:45
#define ABT_UNIT_NULL
Definition: abt.h:415
struct ABT_pool_opaque * ABT_pool
Definition: abt.h:329
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:129
ABT_sched_config_var ABT_sched_basic_wait_freq
Definition: basic_wait.c:31
static int sched_free(ABT_sched)
Definition: basic_wait.c:152
static sched_data * sched_data_get_ptr(void *data)
Definition: basic_wait.c:40
static int sched_cmp_pools(const void *p1, const void *p2)
Definition: basic_wait.c:189
ABTI_global * gp_ABTI_global
Definition: global.c:18
#define ABT_SUCCESS
Definition: abt.h:64
ABT_pool_access
Definition: abt.h:161
#define ABT_TRUE
Definition: abt.h:284
static void sched_run(ABT_sched sched)
Definition: basic_wait.c:89
static ABT_sched_def sched_basic_wait_def
Definition: basic_wait.c:17
ABT_sched_type type
Definition: abt.h:460
static void sched_sort_pools(int num_pools, ABT_pool *pools)
Definition: basic_wait.c:205
struct ABT_sched_config_opaque * ABT_sched_config
Definition: abt.h:321
static void ABTU_free(void *ptr)
Definition: abtu.h:122
static int pool_get_access_num(ABT_pool *p_pool)
Definition: basic_wait.c:163