ARGOBOTS  1227c643f7a7f974f1f1778a9ffebd29d7dafecf
 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 
31 ABT_sched_def *ABTI_sched_get_randws_def(void)
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 = ABT_SUCCESS;
39  int num_pools;
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;
46  abt_errno = ABTU_malloc(sizeof(sched_data), (void **)&p_data);
47  ABTI_CHECK_ERROR(abt_errno);
48  p_data->event_freq = gp_ABTI_global->sched_event_freq;
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  goto fn_fail;
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  goto fn_fail;
70  }
71  memcpy(p_data->pools, p_sched->pools, sizeof(ABT_pool) * num_pools);
72 
73  p_sched->data = p_data;
74 
75 fn_exit:
76  return abt_errno;
77 
78 fn_fail:
79  HANDLE_ERROR_WITH_CODE("randws: sched_init", abt_errno);
80  goto fn_exit;
81 }
82 
83 static void sched_run(ABT_sched sched)
84 {
85  ABTI_xstream *p_local_xstream =
86  ABTI_local_get_xstream(ABTI_local_get_local());
87  uint32_t work_count = 0;
88  sched_data *p_data;
89  int num_pools;
90  ABT_pool *pools;
91  ABT_unit unit;
92  int target;
93  unsigned seed = time(NULL);
94  CNT_DECL(run_cnt);
95 
96  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
97  ABTI_ASSERT(p_sched);
98 
99  p_data = (sched_data *)p_sched->data;
100  num_pools = p_sched->num_pools;
101  pools = p_data->pools;
102 
103  while (1) {
104  CNT_INIT(run_cnt, 0);
105 
106  /* Execute one work unit from the scheduler's pool */
107  ABT_pool pool = pools[0];
108  ABTI_pool *p_pool = ABTI_pool_get_ptr(pool);
109  unit = ABTI_pool_pop(p_pool);
110  if (unit != ABT_UNIT_NULL) {
111  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
112  CNT_INC(run_cnt);
113  } else if (num_pools > 1) {
114  /* Steal a work unit from other pools */
115  target =
116  (num_pools == 2) ? 1 : (rand_r(&seed) % (num_pools - 1) + 1);
117  pool = pools[target];
118  p_pool = ABTI_pool_get_ptr(pool);
119  unit = ABTI_pool_pop(p_pool);
120  LOG_DEBUG_POOL_POP(p_pool, unit);
121  if (unit != ABT_UNIT_NULL) {
122  ABTI_unit_set_associated_pool(unit, p_pool);
123  ABTI_xstream_run_unit(&p_local_xstream, unit, p_pool);
124  CNT_INC(run_cnt);
125  }
126  }
127 
128  if (++work_count >= p_data->event_freq) {
129  ABTI_xstream_check_events(p_local_xstream, p_sched);
130  ABTI_local *p_local = ABTI_xstream_get_local(p_local_xstream);
131  if (ABTI_sched_has_to_stop(&p_local, p_sched) == ABT_TRUE)
132  break;
133  p_local_xstream = ABTI_local_get_xstream(p_local);
134  work_count = 0;
135  SCHED_SLEEP(run_cnt, p_data->sleep_time);
136  }
137  }
138 }
139 
140 static int sched_free(ABT_sched sched)
141 {
142  ABTI_sched *p_sched = ABTI_sched_get_ptr(sched);
143  ABTI_ASSERT(p_sched);
144 
145  sched_data *p_data = (sched_data *)p_sched->data;
146  ABTU_free(p_data->pools);
147  ABTU_free(p_data);
148  return ABT_SUCCESS;
149 }
struct ABT_unit_opaque * ABT_unit
Definition: abt.h:337
struct ABT_sched_opaque * ABT_sched
Definition: abt.h:319
#define ABT_UNIT_NULL
Definition: abt.h:415
static void sched_run(ABT_sched sched)
Definition: randws.c:83
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:140
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
#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
struct ABT_sched_config_opaque * ABT_sched_config
Definition: abt.h:321
static ABT_sched_def sched_randws_def
Definition: randws.c:14
static void ABTU_free(void *ptr)
Definition: abtu.h:135
static int sched_init(ABT_sched sched, ABT_sched_config config)
Definition: randws.c:36