ARGOBOTS  66b1c39742507d8df30e8d28c54839b961a14814
 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;
28  ABT_pool *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 
37 ABT_sched_def *ABTI_sched_get_basic_def(void)
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;
50  int num_pools;
51 
52  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
53  ABTI_CHECK_NULL_SCHED_PTR(p_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 
60  p_data->event_freq = gp_ABTI_global->sched_event_freq;
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  ABTI_CHECK_ERROR(abt_errno);
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  ABTI_CHECK_ERROR(abt_errno);
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  return ABT_SUCCESS;
93 }
94 
95 static void sched_run(ABT_sched sched)
96 {
97  ABTI_xstream *p_local_xstream =
98  ABTI_local_get_xstream(ABTI_local_get_local());
99  ABT_unit unit = ABT_UNIT_NULL;
100  uint32_t pop_count = 0;
101  sched_data *p_data;
102  uint32_t event_freq;
103  int num_pools;
104  ABT_pool *pools;
105  int i;
106 
107  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
108  ABTI_ASSERT(p_sched);
109 
110  p_data = sched_data_get_ptr(p_sched->data);
111  event_freq = p_data->event_freq;
112  num_pools = p_data->num_pools;
113  pools = p_data->pools;
114 
115  while (1) {
116  for (i = 0; i < num_pools; i++) {
117  ABTI_pool *p_pool = ABTI_pool_get_ptr(pools[i]);
118  ++pop_count;
119  if ((unit = ABTI_pool_pop(p_pool)) != ABT_UNIT_NULL) {
120  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
121  break;
122  }
123  }
124  /* if we attempted event_freq pops, check for events */
125  if (pop_count >= event_freq) {
126  ABTI_xstream_check_events(p_local_xstream, p_sched);
127  ABTI_local *p_local = ABTI_xstream_get_local(p_local_xstream);
128  if (ABTI_sched_has_to_stop(&p_local, p_sched) == ABT_TRUE)
129  break;
130  p_local_xstream = ABTI_local_get_xstream(p_local);
131  SCHED_SLEEP(unit != ABT_UNIT_NULL, p_data->sleep_time);
132  pop_count = 0;
133  }
134  }
135 }
136 
137 static int sched_free(ABT_sched sched)
138 {
139  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
140  ABTI_ASSERT(p_sched);
141 
142  sched_data *p_data = sched_data_get_ptr(p_sched->data);
143  ABTU_free(p_data->pools);
144  ABTU_free(p_data);
145  return ABT_SUCCESS;
146 }
147 
148 static int pool_get_access_num(ABT_pool *p_pool)
149 {
150  ABT_pool_access access;
151  int num = 0;
152 
153  access = ABTI_pool_get_ptr(*p_pool)->access;
154  switch (access) {
156  num = 0;
157  break;
160  num = 1;
161  break;
164  num = 2;
165  break;
166  default:
167  ABTI_ASSERT(0);
169  }
170 
171  return num;
172 }
173 
174 static int sched_cmp_pools(const void *p1, const void *p2)
175 {
176  int p1_access, p2_access;
177 
178  p1_access = pool_get_access_num((ABT_pool *)p1);
179  p2_access = pool_get_access_num((ABT_pool *)p2);
180 
181  if (p1_access > p2_access) {
182  return 1;
183  } else if (p1_access < p2_access) {
184  return -1;
185  } else {
186  return 0;
187  }
188 }
189 
190 static void sched_sort_pools(int num_pools, ABT_pool *pools)
191 {
192  qsort(pools, num_pools, sizeof(ABT_pool), sched_cmp_pools);
193 }
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:337
static int pool_get_access_num(ABT_pool *p_pool)
Definition: basic.c:148
struct ABT_sched_opaque * ABT_sched
Definition: abt.h:319
#define ABTU_unreachable()
Definition: abtu.h:25
#define ABT_UNIT_NULL
Definition: abt.h:415
static int sched_free(ABT_sched)
Definition: basic.c:137
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
static int sched_cmp_pools(const void *p1, const void *p2)
Definition: basic.c:174
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: basic.c:47
ABTI_global * gp_ABTI_global
Definition: global.c:18
#define ABT_SUCCESS
Definition: abt.h:64
ABT_pool_access
Definition: abt.h:161
static void sched_run(ABT_sched sched)
Definition: basic.c:95
#define ABT_TRUE
Definition: abt.h:284
static sched_data * sched_data_get_ptr(void *data)
Definition: basic.c:42
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
struct ABT_sched_config_opaque * ABT_sched_config
Definition: abt.h:321
static void sched_sort_pools(int num_pools, ABT_pool *pools)
Definition: basic.c:190
static void ABTU_free(void *ptr)
Definition: abtu.h:135