ARGOBOTS
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 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
23  struct timespec sleep_time;
24 #endif
25 } sched_data;
26 
27 ABT_sched_def *ABTI_sched_get_prio_def(void)
28 {
29  return &sched_prio_def;
30 }
31 
32 static inline sched_data *sched_data_get_ptr(void *data)
33 {
34  return (sched_data *)data;
35 }
36 
37 static int sched_init(ABT_sched sched, ABT_sched_config config)
38 {
39  int abt_errno = ABT_SUCCESS;
40 
41  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
42  ABTI_CHECK_NULL_SCHED_PTR(p_sched);
43 
44  /* Default settings */
45  sched_data *p_data = (sched_data *)ABTU_malloc(sizeof(sched_data));
46  p_data->event_freq = ABTI_global_get_sched_event_freq();
47 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
48  p_data->sleep_time.tv_sec = 0;
49  p_data->sleep_time.tv_nsec = ABTI_global_get_sched_sleep_nsec();
50 #endif
51 
52  /* Set the variables from the config */
53  void *p_event_freq = &p_data->event_freq;
54  ABTI_sched_config_read(config, 1, 1, &p_event_freq);
55 
56  p_sched->data = p_data;
57 
58 fn_exit:
59  return abt_errno;
60 
61 fn_fail:
62  HANDLE_ERROR_WITH_CODE("prio: sched_init", abt_errno);
63  goto fn_exit;
64 }
65 
66 static void sched_run(ABT_sched sched)
67 {
68  ABTI_local *p_local = ABTI_local_get_local();
69  uint32_t work_count = 0;
70  sched_data *p_data;
71  uint32_t event_freq;
72  int num_pools;
73  ABT_pool *p_pools;
74  int i;
75  CNT_DECL(run_cnt);
76 
77  ABTI_xstream *p_xstream = p_local->p_xstream;
78  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
79  ABTI_ASSERT(p_sched);
80 
81  p_data = sched_data_get_ptr(p_sched->data);
82  event_freq = p_data->event_freq;
83 
84  /* Get the list of pools */
85  num_pools = p_sched->num_pools;
86  p_pools = (ABT_pool *)ABTU_malloc(num_pools * sizeof(ABT_pool));
87  memcpy(p_pools, p_sched->pools, sizeof(ABT_pool) * num_pools);
88 
89  while (1) {
90  CNT_INIT(run_cnt, 0);
91 
92  /* Execute one work unit from the scheduler's pool */
93  /* The pool with lower index has higher priority. */
94  for (i = 0; i < num_pools; i++) {
95  ABT_pool pool = p_pools[i];
96  ABTI_pool *p_pool = ABTI_pool_get_ptr(pool);
97  ABT_unit unit = ABTI_pool_pop(p_pool);
98  if (unit != ABT_UNIT_NULL) {
99  ABTI_xstream_run_unit(&p_local, p_xstream, unit, p_pool);
100  CNT_INC(run_cnt);
101  break;
102  }
103  }
104 
105  if (++work_count >= event_freq) {
106  ABT_bool stop =
107  ABTI_sched_has_to_stop(&p_local, p_sched, p_xstream);
108  if (stop == ABT_TRUE)
109  break;
110  work_count = 0;
111  ABTI_xstream_check_events(p_xstream, sched);
112  SCHED_SLEEP(run_cnt, p_data->sleep_time);
113  }
114  }
115 
116  ABTU_free(p_pools);
117 }
118 
119 static int sched_free(ABT_sched sched)
120 {
121  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
122  ABTI_ASSERT(p_sched);
123 
124  sched_data *p_data = sched_data_get_ptr(p_sched->data);
125  ABTU_free(p_data);
126 
127  return ABT_SUCCESS;
128 }
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:275
struct ABT_sched_opaque * ABT_sched
Definition: abt.h:257
static ABT_sched_def sched_prio_def
Definition: prio.c:14
static sched_data * sched_data_get_ptr(void *data)
Definition: prio.c:32
static void sched_run(ABT_sched sched)
Definition: prio.c:66
#define ABT_UNIT_NULL
Definition: abt.h:343
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: prio.c:37
static void * ABTU_malloc(size_t size)
Definition: abtu.h:39
int ABT_bool
Definition: abt.h:309
struct ABT_pool_opaque * ABT_pool
Definition: abt.h:267
#define CNT_INIT(c, v)
Definition: abti_sched.h:89
#define HANDLE_ERROR_WITH_CODE(msg, n)
Definition: abti_error.h:234
#define ABT_SUCCESS
Definition: abt.h:64
#define ABT_TRUE
Definition: abt.h:223
ABT_sched_type type
Definition: abt.h:387
#define CNT_DECL(c)
Definition: abti_sched.h:88
static int sched_free(ABT_sched)
Definition: prio.c:119
#define CNT_INC(c)
Definition: abti_sched.h:90
#define SCHED_SLEEP(c, t)
Definition: abti_sched.h:91
struct ABT_sched_config_opaque * ABT_sched_config
Definition: abt.h:259
static void ABTU_free(void *ptr)
Definition: abtu.h:32