ARGOBOTS  1227c643f7a7f974f1f1778a9ffebd29d7dafecf
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
basic.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;
29 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
30  struct timespec sleep_time;
31 #endif
32 } sched_data;
33 
35  .type = ABT_SCHED_CONFIG_INT };
36 
38 {
39  return &sched_basic_def;
40 }
41 
42 static inline sched_data *sched_data_get_ptr(void *data)
43 {
44  return (sched_data *)data;
45 }
46 
47 static int sched_init(ABT_sched sched, ABT_sched_config config)
48 {
49  int abt_errno = ABT_SUCCESS;
50  int num_pools;
51 
52  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
54 
55  /* Default settings */
56  sched_data *p_data;
57  abt_errno = ABTU_malloc(sizeof(sched_data), (void **)&p_data);
58  ABTI_CHECK_ERROR(abt_errno);
59 
61 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
62  p_data->sleep_time.tv_sec = 0;
63  p_data->sleep_time.tv_nsec = gp_ABTI_global->sched_sleep_nsec;
64 #endif
65 
66  /* Set the variables from the config */
67  void *p_event_freq = &p_data->event_freq;
68  abt_errno = ABTI_sched_config_read(config, 1, 1, &p_event_freq);
69  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
70  ABTU_free(p_data);
71  goto fn_fail;
72  }
73 
74  /* Save the list of pools */
75  num_pools = p_sched->num_pools;
76  p_data->num_pools = num_pools;
77  abt_errno =
78  ABTU_malloc(num_pools * sizeof(ABT_pool), (void **)&p_data->pools);
79  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
80  ABTU_free(p_data);
81  goto fn_fail;
82  }
83  memcpy(p_data->pools, p_sched->pools, sizeof(ABT_pool) * num_pools);
84 
85  /* Sort pools according to their access mode so the scheduler can execute
86  work units from the private pools. */
87  if (num_pools > 1) {
88  sched_sort_pools(num_pools, p_data->pools);
89  }
90 
91  p_sched->data = p_data;
92 
93 fn_exit:
94  return abt_errno;
95 
96 fn_fail:
97  HANDLE_ERROR_WITH_CODE("basic: sched_init", abt_errno);
98  goto fn_exit;
99 }
100 
101 static void sched_run(ABT_sched sched)
102 {
103  ABTI_xstream *p_local_xstream =
105  ABT_unit unit = ABT_UNIT_NULL;
106  uint32_t pop_count = 0;
107  sched_data *p_data;
108  uint32_t event_freq;
109  int num_pools;
110  ABT_pool *pools;
111  int i;
112 
113  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
114  ABTI_ASSERT(p_sched);
115 
116  p_data = sched_data_get_ptr(p_sched->data);
117  event_freq = p_data->event_freq;
118  num_pools = p_data->num_pools;
119  pools = p_data->pools;
120 
121  while (1) {
122  for (i = 0; i < num_pools; i++) {
123  ABTI_pool *p_pool = ABTI_pool_get_ptr(pools[i]);
124  ++pop_count;
125  if ((unit = ABTI_pool_pop(p_pool)) != ABT_UNIT_NULL) {
126  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
127  break;
128  }
129  }
130  /* if we attempted event_freq pops, check for events */
131  if (pop_count >= event_freq) {
132  ABTI_xstream_check_events(p_local_xstream, p_sched);
133  ABTI_local *p_local = ABTI_xstream_get_local(p_local_xstream);
134  if (ABTI_sched_has_to_stop(&p_local, p_sched) == ABT_TRUE)
135  break;
136  p_local_xstream = ABTI_local_get_xstream(p_local);
137  SCHED_SLEEP(unit != ABT_UNIT_NULL, p_data->sleep_time);
138  pop_count = 0;
139  }
140  }
141 }
142 
143 static int sched_free(ABT_sched sched)
144 {
145  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
146  ABTI_ASSERT(p_sched);
147 
148  sched_data *p_data = sched_data_get_ptr(p_sched->data);
149  ABTU_free(p_data->pools);
150  ABTU_free(p_data);
151  return ABT_SUCCESS;
152 }
153 
154 static int pool_get_access_num(ABT_pool *p_pool)
155 {
156  ABT_pool_access access;
157  int num = 0;
158 
159  access = ABTI_pool_get_ptr(*p_pool)->access;
160  switch (access) {
162  num = 0;
163  break;
166  num = 1;
167  break;
170  num = 2;
171  break;
172  default:
173  ABTI_ASSERT(0);
175  }
176 
177  return num;
178 }
179 
180 static int sched_cmp_pools(const void *p1, const void *p2)
181 {
182  int p1_access, p2_access;
183 
184  p1_access = pool_get_access_num((ABT_pool *)p1);
185  p2_access = pool_get_access_num((ABT_pool *)p2);
186 
187  if (p1_access > p2_access) {
188  return 1;
189  } else if (p1_access < p2_access) {
190  return -1;
191  } else {
192  return 0;
193  }
194 }
195 
196 static void sched_sort_pools(int num_pools, ABT_pool *pools)
197 {
198  qsort(pools, num_pools, sizeof(ABT_pool), sched_cmp_pools);
199 }
int num_pools
Definition: basic.c:27
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:337
static int pool_get_access_num(ABT_pool *p_pool)
Definition: basic.c:154
#define ABTI_CHECK_NULL_SCHED_PTR(p)
Definition: abti_error.h:182
static ABT_unit ABTI_pool_pop(ABTI_pool *p_pool)
Definition: abti_pool.h:108
struct ABT_sched_opaque * ABT_sched
Definition: abt.h:319
ABT_pool * pools
Definition: abti.h:265
#define ABTU_unreachable()
Definition: abtu.h:25
struct ABTI_local ABTI_local
Definition: abti.h:101
ABTU_ret_err int ABTI_sched_config_read(ABT_sched_config config, int type, int num_vars, void **variables)
Definition: config.c:252
#define ABT_UNIT_NULL
Definition: abt.h:415
void ABTI_xstream_check_events(ABTI_xstream *p_xstream, ABTI_sched *p_sched)
Definition: stream.c:1104
static int sched_free(ABT_sched)
Definition: basic.c:143
long sched_sleep_nsec
Definition: abti.h:185
ABT_pool * pools
Definition: basic.c:28
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:142
int num_pools
Definition: abti.h:266
#define HANDLE_ERROR_WITH_CODE(msg, n)
Definition: abti_error.h:346
static int sched_cmp_pools(const void *p1, const void *p2)
Definition: basic.c:180
static ABTI_pool * ABTI_pool_get_ptr(ABT_pool pool)
Definition: abti_pool.h:11
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: basic.c:47
ABTI_global * gp_ABTI_global
Definition: global.c:14
#define ABT_SUCCESS
Definition: abt.h:64
void ABTI_xstream_run_unit(ABTI_xstream **pp_local_xstream, ABT_unit unit, ABTI_pool *p_pool)
Definition: stream.c:1085
ABT_bool ABTI_sched_has_to_stop(ABTI_local **pp_local, ABTI_sched *p_sched)
Definition: sched.c:616
ABT_pool_access
Definition: abt.h:161
static void sched_run(ABT_sched sched)
Definition: basic.c:101
#define ABT_TRUE
Definition: abt.h:284
static sched_data * sched_data_get_ptr(void *data)
Definition: basic.c:42
ABT_pool_access access
Definition: abti.h:282
ABT_sched_type type
Definition: abt.h:460
static ABT_sched_def sched_basic_def
Definition: basic.c:17
ABT_sched_config_var ABT_sched_basic_freq
Definition: basic.c:34
#define SCHED_SLEEP(c, t)
Definition: abti_sched.h:78
ABT_sched_def * ABTI_sched_get_basic_def(void)
Definition: basic.c:37
static ABTI_sched * ABTI_sched_get_ptr(ABT_sched sched)
Definition: abti_sched.h:11
#define ABTI_ASSERT(cond)
Definition: abti_error.h:12
struct ABT_sched_config_opaque * ABT_sched_config
Definition: abt.h:321
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:86
static ABTI_xstream * ABTI_local_get_xstream(ABTI_local *p_local)
Definition: abti_local.h:86
static void sched_sort_pools(int num_pools, ABT_pool *pools)
Definition: basic.c:196
uint32_t sched_event_freq
Definition: abti.h:184
uint32_t event_freq
Definition: basic.c:26
static ABTI_local * ABTI_xstream_get_local(ABTI_xstream *p_xstream)
Definition: abti_stream.h:67
void * data
Definition: abti.h:268
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
#define ABTI_IS_ERROR_CHECK_ENABLED
Definition: abti.h:20
static void ABTU_free(void *ptr)
Definition: abtu.h:135
Definition: fifo.c:33