ARGOBOTS  66b1c39742507d8df30e8d28c54839b961a14814
 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 
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;
42  int num_pools;
43 
44  ABTI_sched *p_sched = ABTI_sched_get_ptr(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);
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  ABTI_CHECK_ERROR(abt_errno);
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  ABTI_CHECK_ERROR(abt_errno);
73  }
74  memcpy(p_data->pools, p_sched->pools, sizeof(ABT_pool) * num_pools);
75 
76  p_sched->data = p_data;
77  return ABT_SUCCESS;
78 }
79 
80 static void sched_run(ABT_sched sched)
81 {
82  ABTI_xstream *p_local_xstream =
84  uint32_t work_count = 0;
85  sched_data *p_data;
86  uint32_t event_freq;
87  int num_pools;
88  ABT_pool *pools;
89  int i;
90  CNT_DECL(run_cnt);
91 
92  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
93  ABTI_ASSERT(p_sched);
94 
95  p_data = sched_data_get_ptr(p_sched->data);
96  event_freq = p_data->event_freq;
97  num_pools = p_sched->num_pools;
98  pools = p_data->pools;
99 
100  while (1) {
101  CNT_INIT(run_cnt, 0);
102 
103  /* Execute one work unit from the scheduler's pool */
104  /* The pool with lower index has higher priority. */
105  for (i = 0; i < num_pools; i++) {
106  ABT_pool pool = pools[i];
107  ABTI_pool *p_pool = ABTI_pool_get_ptr(pool);
108  ABT_unit unit = ABTI_pool_pop(p_pool);
109  if (unit != ABT_UNIT_NULL) {
110  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
111  CNT_INC(run_cnt);
112  break;
113  }
114  }
115 
116  if (++work_count >= event_freq) {
117  ABTI_xstream_check_events(p_local_xstream, p_sched);
118  ABTI_local *p_local = ABTI_xstream_get_local(p_local_xstream);
119  if (ABTI_sched_has_to_stop(&p_local, p_sched) == ABT_TRUE)
120  break;
121  p_local_xstream = ABTI_local_get_xstream(p_local);
122  work_count = 0;
123  SCHED_SLEEP(run_cnt, p_data->sleep_time);
124  }
125  }
126 }
127 
128 static int sched_free(ABT_sched sched)
129 {
130  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
131  ABTI_ASSERT(p_sched);
132 
133  sched_data *p_data = sched_data_get_ptr(p_sched->data);
134  ABTU_free(p_data->pools);
135  ABTU_free(p_data);
136  return ABT_SUCCESS;
137 }
int num_pools
Definition: basic.c:27
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:337
#define ABTI_CHECK_NULL_SCHED_PTR(p)
Definition: abti_error.h:184
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
static ABT_sched_def sched_prio_def
Definition: prio.c:14
struct ABTI_local ABTI_local
Definition: abti.h:101
static sched_data * sched_data_get_ptr(void *data)
Definition: prio.c:34
static void sched_run(ABT_sched sched)
Definition: prio.c:80
ABTU_ret_err int ABTI_sched_config_read(ABT_sched_config config, int type, int num_vars, void **variables)
Definition: config.c:239
#define ABT_UNIT_NULL
Definition: abt.h:415
void ABTI_xstream_check_events(ABTI_xstream *p_xstream, ABTI_sched *p_sched)
Definition: stream.c:922
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: prio.c:39
long sched_sleep_nsec
Definition: abti.h:185
ABT_sched_def * ABTI_sched_get_prio_def(void)
Definition: prio.c:29
ABT_pool * pools
Definition: basic.c:28
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
int num_pools
Definition: abti.h:266
static ABTI_pool * ABTI_pool_get_ptr(ABT_pool pool)
Definition: abti_pool.h:11
ABTI_global * gp_ABTI_global
Definition: global.c:18
#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:903
ABT_bool ABTI_sched_has_to_stop(ABTI_local **pp_local, ABTI_sched *p_sched)
Definition: sched.c:526
#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:128
#define CNT_INC(c)
Definition: abti_sched.h:77
#define SCHED_SLEEP(c, t)
Definition: abti_sched.h:78
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:127
static ABTI_xstream * ABTI_local_get_xstream(ABTI_local *p_local)
Definition: abti_local.h:86
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