ARGOBOTS
config.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 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 
16 /* Global configurable parameters */
18  .type =
20 
22  .type = ABT_SCHED_CONFIG_INT };
23 
25  .type =
27 
60 {
61  int abt_errno = ABT_SUCCESS;
62  ABTI_sched_config *p_config;
63 
64  char *buffer = NULL;
65  size_t alloc_size = 8 * sizeof(size_t);
66 
67  int num_params = 0;
68  size_t offset = sizeof(num_params);
69 
70  size_t buffer_size = alloc_size;
71  buffer = (char *)ABTU_malloc(buffer_size);
72 
73  va_list varg_list;
74  va_start(varg_list, config);
75 
76  /* We read each couple (var, value) until we find ABT_sched_config_var_end
77  */
78  while (1) {
79  ABT_sched_config_var var = va_arg(varg_list, ABT_sched_config_var);
80  if (var.idx == ABT_sched_config_var_end.idx)
81  break;
82 
83  int param = var.idx;
84  ABT_sched_config_type type = var.type;
85  num_params++;
86 
87  size_t size = ABTI_sched_config_type_size(type);
88  if (offset + sizeof(param) + sizeof(type) + size > buffer_size) {
89  size_t cur_buffer_size = buffer_size;
90  buffer_size += alloc_size;
91  buffer = ABTU_realloc(buffer, cur_buffer_size, buffer_size);
92  }
93  /* Copy the parameter index */
94  memcpy(buffer + offset, (void *)&param, sizeof(param));
95  offset += sizeof(param);
96 
97  /* Copy the size of the argument */
98  memcpy(buffer + offset, (void *)&size, sizeof(size));
99  offset += sizeof(size);
100 
101  /* Copy the argument */
102  void *ptr;
103  int i;
104  double d;
105  void *p;
106  switch (type) {
108  i = va_arg(varg_list, int);
109  ptr = (void *)&i;
110  break;
112  d = va_arg(varg_list, double);
113  ptr = (void *)&d;
114  break;
116  p = va_arg(varg_list, void *);
117  ptr = (void *)&p;
118  break;
119  default:
120  abt_errno = ABT_ERR_SCHED_CONFIG;
121  goto fn_fail;
122  }
123 
124  memcpy(buffer + offset, ptr, size);
125  offset += size;
126  }
127  va_end(varg_list);
128 
129  if (num_params) {
130  memcpy(buffer, (int *)&num_params, sizeof(num_params));
131  } else {
132  ABTU_free(buffer);
133  buffer = NULL;
134  }
135 
136  p_config = (ABTI_sched_config *)buffer;
137  *config = ABTI_sched_config_get_handle(p_config);
138 
139 fn_exit:
140  return abt_errno;
141 
142 fn_fail:
143  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
144  goto fn_exit;
145 }
146 
162 int ABT_sched_config_read(ABT_sched_config config, int num_vars, ...)
163 {
164  int abt_errno = ABT_SUCCESS;
165  int v;
166 
167  /* We read all the variables and save the addresses */
168  void **variables = (void *)ABTU_malloc(num_vars * sizeof(void *));
169  va_list varg_list;
170  va_start(varg_list, num_vars);
171  for (v = 0; v < num_vars; v++) {
172  variables[v] = va_arg(varg_list, void *);
173  }
174  va_end(varg_list);
175 
176  abt_errno = ABTI_sched_config_read(config, 1, num_vars, variables);
177  ABTI_CHECK_ERROR(abt_errno);
178 
179  ABTU_free(variables);
180 
181 fn_exit:
182  return abt_errno;
183 
184 fn_fail:
185  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
186  goto fn_exit;
187 }
188 
198 {
199  ABTI_sched_config *p_config = ABTI_sched_config_get_ptr(*config);
200  ABTU_free(p_config);
201 
202  *config = ABT_SCHED_CONFIG_NULL;
203 
204  return ABT_SUCCESS;
205 }
206 
207 size_t ABTI_sched_config_type_size(ABT_sched_config_type type)
208 {
209  switch (type) {
211  return sizeof(int);
213  return sizeof(double);
215  return sizeof(void *);
216  default:
217  ABTI_ASSERT(0);
218  return 0;
219  }
220 }
221 
222 int ABTI_sched_config_read_global(ABT_sched_config config,
223  ABT_pool_access *access, ABT_bool *automatic)
224 {
225  int abt_errno = ABT_SUCCESS;
226  int num_vars = 2;
227  /* We use XXX_i variables because va_list converts these types into int */
228  int access_i = -1;
229  int automatic_i = -1;
230 
231  void **variables = (void **)ABTU_malloc(num_vars * sizeof(void *));
232  variables[(ABT_sched_config_access.idx + 2) * (-1)] = &access_i;
233  variables[(ABT_sched_config_automatic.idx + 2) * (-1)] = &automatic_i;
234 
235  abt_errno = ABTI_sched_config_read(config, 0, num_vars, variables);
236  ABTU_free(variables);
237  ABTI_CHECK_ERROR(abt_errno);
238 
239  if (access_i != -1)
240  *access = (ABT_pool_access)access_i;
241  if (automatic_i != -1)
242  *automatic = (ABT_bool)automatic_i;
243 
244 fn_exit:
245  return abt_errno;
246 
247 fn_fail:
248  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
249  goto fn_exit;
250 }
251 
252 /* type is 0 if we read the private parameters, else 1 */
253 int ABTI_sched_config_read(ABT_sched_config config, int type, int num_vars,
254  void **variables)
255 {
256  size_t offset = 0;
257  int num_params;
258 
259  if (config == ABT_SCHED_CONFIG_NULL) {
260  return ABT_SUCCESS;
261  }
262 
263  ABTI_sched_config *p_config = ABTI_sched_config_get_ptr(config);
264 
265  char *buffer = (char *)p_config;
266 
267  /* Number of parameters in buffer */
268  memcpy(&num_params, buffer, sizeof(num_params));
269  offset += sizeof(num_params);
270 
271  /* Copy the data from buffer to the right variables */
272  int p;
273  for (p = 0; p < num_params; p++) {
274  int var_idx;
275  size_t size;
276 
277  /* Get the variable index of the next parameter */
278  memcpy(&var_idx, buffer + offset, sizeof(var_idx));
279  offset += sizeof(var_idx);
280  /* Get the size of the next parameter */
281  memcpy(&size, buffer + offset, sizeof(size));
282  offset += sizeof(size);
283  /* Get the next argument */
284  /* We save it only if
285  * - the index is < 0 when type == 0
286  * - the index is >= 0 when type == 1
287  */
288  if (type == 0) {
289  if (var_idx < 0) {
290  var_idx = (var_idx + 2) * -1;
291  if (var_idx >= num_vars)
293  memcpy(variables[var_idx], buffer + offset, size);
294  }
295  } else {
296  if (var_idx >= 0) {
297  if (var_idx >= num_vars)
299  memcpy(variables[var_idx], buffer + offset, size);
300  }
301  }
302  offset += size;
303  }
304  return ABT_SUCCESS;
305 }
#define ABT_ERR_INV_SCHED_CONFIG
Definition: abt.h:75
ABT_sched_config_var ABT_sched_config_automatic
Definition: config.c:24
ABT_sched_config_var ABT_sched_config_access
Definition: config.c:21
static void * ABTU_malloc(size_t size)
Definition: abtu.h:39
int ABT_bool
Definition: abt.h:309
int ABT_sched_config_read(ABT_sched_config config, int num_vars,...)
Copy the set values from config into the variables passed in the dynamic list of arguments.
Definition: config.c:162
ABT_sched_config_type type
Definition: abt.h:367
#define HANDLE_ERROR_FUNC_WITH_CODE(n)
Definition: abti_error.h:241
ABT_sched_config_type
Definition: abt.h:359
static void * ABTU_realloc(void *ptr, size_t old_size, size_t new_size)
Definition: abtu.h:56
#define ABT_SUCCESS
Definition: abt.h:64
ABT_pool_access
Definition: abt.h:162
int ABT_sched_config_free(ABT_sched_config *config)
Free the configuration.
Definition: config.c:197
#define ABT_ERR_SCHED_CONFIG
Definition: abt.h:97
struct ABT_sched_config_opaque * ABT_sched_config
Definition: abt.h:259
int ABT_sched_config_create(ABT_sched_config *config,...)
Create a scheduler configuration.
Definition: config.c:59
#define ABT_SCHED_CONFIG_NULL
Definition: abt.h:340
ABT_sched_config_var ABT_sched_config_var_end
Definition: config.c:17
static void ABTU_free(void *ptr)
Definition: abtu.h:32