ARGOBOTS  1227c643f7a7f974f1f1778a9ffebd29d7dafecf
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
prio.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 /* Priority Scheduler Implementation */
9 
10 static int sched_init(ABT_sched sched, ABT_sched_config config);
11 static void sched_run(ABT_sched sched);
12 static int sched_free(ABT_sched);
13 
15  .init = sched_init,
16  .run = sched_run,
17  .free = sched_free,
18  .get_migr_pool = NULL };
19 
20 typedef struct {
21  uint32_t event_freq;
22  int num_pools;
23  ABT_pool *pools;
24 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
25  struct timespec sleep_time;
26 #endif
27 } sched_data;
28 
29 ABT_sched_def *ABTI_sched_get_prio_def(void)
30 {
31  return &sched_prio_def;
32 }
33 
34 static inline sched_data *sched_data_get_ptr(void *data)
35 {
36  return (sched_data *)data;
37 }
38 
39 static int sched_init(ABT_sched sched, ABT_sched_config config)
40 {
41  int abt_errno = ABT_SUCCESS;
42  int num_pools;
43 
44  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
45  ABTI_CHECK_NULL_SCHED_PTR(p_sched);
46 
47  /* Default settings */
48  sched_data *p_data;
49  abt_errno = ABTU_malloc(sizeof(sched_data), (void **)&p_data);
50  ABTI_CHECK_ERROR(abt_errno);
51  p_data->event_freq = gp_ABTI_global->sched_event_freq;
52 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
53  p_data->sleep_time.tv_sec = 0;
54  p_data->sleep_time.tv_nsec = gp_ABTI_global->sched_sleep_nsec;
55 #endif
56 
57  /* Set the variables from the config */
58  void *p_event_freq = &p_data->event_freq;
59  abt_errno = ABTI_sched_config_read(config, 1, 1, &p_event_freq);
60  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
61  ABTU_free(p_data);
62  goto fn_fail;
63  }
64 
65  /* Save the list of pools */
66  num_pools = p_sched->num_pools;
67  p_data->num_pools = num_pools;
68  abt_errno =
69  ABTU_malloc(num_pools * sizeof(ABT_pool), (void **)&p_data->pools);
70  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
71  ABTU_free(p_data);
72  goto fn_fail;
73  }
74  memcpy(p_data->pools, p_sched->pools, sizeof(ABT_pool) * num_pools);
75 
76  p_sched->data = p_data;
77 
78 fn_exit:
79  return abt_errno;
80 
81 fn_fail:
82  HANDLE_ERROR_WITH_CODE("prio: sched_init", abt_errno);
83  goto fn_exit;
84 }
85 
86 static void sched_run(ABT_sched sched)
87 {
88  ABTI_xstream *p_local_xstream =
89  ABTI_local_get_xstream(ABTI_local_get_local());
90  uint32_t work_count = 0;
91  sched_data *p_data;
92  uint32_t event_freq;
93  int num_pools;
94  ABT_pool *pools;
95  int i;
96  CNT_DECL(run_cnt);
97 
98  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
99  ABTI_ASSERT(p_sched);
100 
101  p_data = sched_data_get_ptr(p_sched->data);
102  event_freq = p_data->event_freq;
103  num_pools = p_sched->num_pools;
104  pools = p_data->pools;
105 
106  while (1) {
107  CNT_INIT(run_cnt, 0);
108 
109  /* Execute one work unit from the scheduler's pool */
110  /* The pool with lower index has higher priority. */
111  for (i = 0; i < num_pools; i++) {
112  ABT_pool pool = pools[i];
113  ABTI_pool *p_pool = ABTI_pool_get_ptr(pool);
114  ABT_unit unit = ABTI_pool_pop(p_pool);
115  if (unit != ABT_UNIT_NULL) {
116  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
117  CNT_INC(run_cnt);
118  break;
119  }
120  }
121 
122  if (++work_count >= event_freq) {
123  ABTI_xstream_check_events(p_local_xstream, p_sched);
124  ABTI_local *p_local = ABTI_xstream_get_local(p_local_xstream);
125  if (ABTI_sched_has_to_stop(&p_local, p_sched) == ABT_TRUE)
126  break;
127  p_local_xstream = ABTI_local_get_xstream(p_local);
128  work_count = 0;
129  SCHED_SLEEP(run_cnt, p_data->sleep_time);
130  }
131  }
132 }
133 
134 static int sched_free(ABT_sched sched)
135 {
136  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
137  ABTI_ASSERT(p_sched);
138 
139  sched_data *p_data = sched_data_get_ptr(p_sched->data);
140  ABTU_free(p_data->pools);
141  ABTU_free(p_data);
142  return ABT_SUCCESS;
143 }
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:337
struct ABT_sched_opaque * ABT_sched
Definition: abt.h:319
static ABT_sched_def sched_prio_def
Definition: prio.c:14
static sched_data * sched_data_get_ptr(void *data)
Definition: prio.c:34
static void sched_run(ABT_sched sched)
Definition: prio.c:86
#define ABT_UNIT_NULL
Definition: abt.h:415
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: prio.c:39
struct ABT_pool_opaque * ABT_pool
Definition: abt.h:329
#define CNT_INIT(c, v)
Definition: abti_sched.h:76
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:142
#define HANDLE_ERROR_WITH_CODE(msg, n)
Definition: abti_error.h:346
ABTI_global * gp_ABTI_global
Definition: global.c:14
#define ABT_SUCCESS
Definition: abt.h:64
#define ABT_TRUE
Definition: abt.h:284
ABT_sched_type type
Definition: abt.h:460
#define CNT_DECL(c)
Definition: abti_sched.h:75
static int sched_free(ABT_sched)
Definition: prio.c:134
#define CNT_INC(c)
Definition: abti_sched.h:77
#define SCHED_SLEEP(c, t)
Definition: abti_sched.h:78
struct ABT_sched_config_opaque * ABT_sched_config
Definition: abt.h:321
static void ABTU_free(void *ptr)
Definition: abtu.h:135