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