ARGOBOTS  66b1c39742507d8df30e8d28c54839b961a14814
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
randws.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 /* Random Work-stealing 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 
16  .init = sched_init,
17  .run = sched_run,
18  .free = sched_free,
19  .get_migr_pool = NULL,
20 };
21 
22 typedef struct {
23  uint32_t event_freq;
24  int num_pools;
25  ABT_pool *pools;
26 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
27  struct timespec sleep_time;
28 #endif
29 } sched_data;
30 
32 {
33  return &sched_randws_def;
34 }
35 
36 static int sched_init(ABT_sched sched, ABT_sched_config config)
37 {
38  int abt_errno;
39  int num_pools;
40 
41  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
43 
44  /* Default settings */
45  sched_data *p_data;
46  abt_errno = ABTU_malloc(sizeof(sched_data), (void **)&p_data);
47  ABTI_CHECK_ERROR(abt_errno);
49 #ifdef ABT_CONFIG_USE_SCHED_SLEEP
50  p_data->sleep_time.tv_sec = 0;
51  p_data->sleep_time.tv_nsec = gp_ABTI_global->sched_sleep_nsec;
52 #endif
53 
54  /* Set the variables from the config */
55  void *p_event_freq = &p_data->event_freq;
56  abt_errno = ABTI_sched_config_read(config, 1, 1, &p_event_freq);
57  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
58  ABTU_free(p_data);
59  ABTI_HANDLE_ERROR(abt_errno);
60  }
61 
62  /* Save the list of pools */
63  num_pools = p_sched->num_pools;
64  p_data->num_pools = num_pools;
65  abt_errno =
66  ABTU_malloc(num_pools * sizeof(ABT_pool), (void **)&p_data->pools);
67  if (ABTI_IS_ERROR_CHECK_ENABLED && abt_errno != ABT_SUCCESS) {
68  ABTU_free(p_data);
69  ABTI_HANDLE_ERROR(abt_errno);
70  }
71  memcpy(p_data->pools, p_sched->pools, sizeof(ABT_pool) * num_pools);
72 
73  p_sched->data = p_data;
74  return ABT_SUCCESS;
75 }
76 
77 static void sched_run(ABT_sched sched)
78 {
79  ABTI_xstream *p_local_xstream =
81  uint32_t work_count = 0;
82  sched_data *p_data;
83  int num_pools;
84  ABT_pool *pools;
85  ABT_unit unit;
86  int target;
87  unsigned seed = time(NULL);
88  CNT_DECL(run_cnt);
89 
90  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
91  ABTI_ASSERT(p_sched);
92 
93  p_data = (sched_data *)p_sched->data;
94  num_pools = p_sched->num_pools;
95  pools = p_data->pools;
96 
97  while (1) {
98  CNT_INIT(run_cnt, 0);
99 
100  /* Execute one work unit from the scheduler's pool */
101  ABT_pool pool = pools[0];
102  ABTI_pool *p_pool = ABTI_pool_get_ptr(pool);
103  unit = ABTI_pool_pop(p_pool);
104  if (unit != ABT_UNIT_NULL) {
105  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
106  CNT_INC(run_cnt);
107  } else if (num_pools > 1) {
108  /* Steal a work unit from other pools */
109  target =
110  (num_pools == 2) ? 1 : (rand_r(&seed) % (num_pools - 1) + 1);
111  pool = pools[target];
112  p_pool = ABTI_pool_get_ptr(pool);
113  unit = ABTI_pool_pop(p_pool);
114  LOG_DEBUG_POOL_POP(p_pool, unit);
115  if (unit != ABT_UNIT_NULL) {
116  ABTI_unit_set_associated_pool(unit, p_pool);
117  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
118  CNT_INC(run_cnt);
119  }
120  }
121 
122  if (++work_count >= p_data->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 *)p_sched->data;
140  ABTU_free(p_data->pools);
141  ABTU_free(p_data);
142  return ABT_SUCCESS;
143 }
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
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:239
#define ABT_UNIT_NULL
Definition: abt.h:415
static void sched_run(ABT_sched sched)
Definition: randws.c:77
void ABTI_xstream_check_events(ABTI_xstream *p_xstream, ABTI_sched *p_sched)
Definition: stream.c:922
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
#define CNT_INIT(c, v)
Definition: abti_sched.h:76
static int sched_free(ABT_sched)
Definition: randws.c:134
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
void ABTI_unit_set_associated_pool(ABT_unit unit, ABTI_pool *p_pool)
Definition: unit.c:40
#define CNT_INC(c)
Definition: abti_sched.h:77
#define LOG_DEBUG_POOL_POP(p_pool, unit)
Definition: abti_log.h:30
#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
ABT_sched_def * ABTI_sched_get_randws_def(void)
Definition: randws.c:31
static ABT_sched_def sched_randws_def
Definition: randws.c:14
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
#define ABTI_HANDLE_ERROR(n)
Definition: abti_error.h:121
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: randws.c:36