configuration.h 8.63 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7 8 9
#ifndef __CONFIGURATION_H__
#define __CONFIGURATION_H__

Jonathan Jenkins's avatar
Jonathan Jenkins committed
10 11 12 13
#ifdef __cplusplus
extern "C" {
#endif

14
#include <stddef.h>
15 16
#include <inttypes.h>
#include <mpi.h>
17

18
#define CONFIGURATION_MAX_NAME 128
19 20
#define CONFIGURATION_MAX_GROUPS 10
#define CONFIGURATION_MAX_TYPES 10
21
#define CONFIGURATION_MAX_ANNOS 10
22

23 24 25 26 27 28 29 30
// NOTE: direct offsets are used when first building the structure as we're
// building up one big buffer to hold entity names. Pointers are set after the
// initialization process during configuration_load
typedef union {
    char const * ptr;
    int offset;
} config_name_u;

31 32
typedef struct config_lptype_s
{
33 34 35
    config_name_u name;
    config_name_u anno;
    int count;
36 37 38 39
} config_lptype_t;

typedef struct config_lpgroup_s
{
40 41 42
    config_name_u name;
    int repetitions;
    int lptypes_count;
43 44 45
    config_lptype_t lptypes[CONFIGURATION_MAX_TYPES];
} config_lpgroup_t;

46 47 48 49
// mapping of lp type to the list of annotations used. Used for convenience when
// models are performing configuraiton code 
typedef struct config_anno_map_s
{
50
    config_name_u lp_name;
51 52
    // only explicit annotations tracked here - use a flag to indicate a
    // non-annotated LP type
53
    int has_unanno_lp;
54 55 56 57
    // for configuration/mapping functions to be able to provide "default"
    // (annotation-ignoring) lookup semantics, provide a flag to determine if
    // the unannotated lp type is first
    int is_unanno_first;
58
    int num_annos;
59
    // maintain the number of lps that have the particular annotation 
60 61
    int num_anno_lps[CONFIGURATION_MAX_ANNOS];
    config_name_u annotations[CONFIGURATION_MAX_ANNOS];
62 63
} config_anno_map_t;

64
typedef struct config_lpgroups_s
65
{
66 67 68
    // counts for the underlying structures, not the number of string entities
    int lpgroups_count;
    int lpannos_count;
69 70
    config_lpgroup_t lpgroups[CONFIGURATION_MAX_GROUPS];
    config_anno_map_t lpannos[CONFIGURATION_MAX_TYPES];
71 72 73 74 75 76 77 78 79
    int num_uniq_lptypes;
    int num_uniq_annos;
    // ptrs into the name buffers
    char const ** group_names;
    char const ** lp_names;
    char const ** anno_names;
    char * group_names_buf;
    char * lp_names_buf;
    char * anno_names_buf;
80
} config_lpgroups_t;
81

82 83
typedef struct ConfigVTable * ConfigHandle;

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/*
 * Load a configuration on the system (collectively)
 *
 * filepath - path and name of file
 * comm     - communicator to distribute file on
 * handle   - output of configuration
 *
 * return 0 on success
 */
int configuration_load (const char * filepath,
                        MPI_Comm comm,
                        ConfigHandle *handle);

/*
 * Free any resources allocated on load.
 *
 * handle - configuration handle
 */
int configuration_free (ConfigHandle *handle);

/*
 * Get's the value for a give section/key pair.
 * Caller's responsible for transforming it to a useable type.
 * Assumes the key name is a KEY configuration type.
 * 
 * handle - configuration handle
 * section_name - name of the section the key is in
 * key_name - name of the key
112
 * annotation - optional annotation to look for (NULL -> no annotation)
113 114 115 116 117 118
 * value - pointer to string
 * length - maximum length of string
 */
int configuration_get_value(ConfigHandle *handle,
                            const char * section_name,
                            const char * key_name,
119
                            const char * annotation,
120 121 122
                            char *value,
                            size_t length);

123 124 125 126 127 128 129 130 131
/*
 * Gets the value for a given section/key pair, and interprets it as a path
 * relative to the location of the configuration file.
 * Assumes the key name is a KEY configuration type.
 * Assumes unix path conventions.
 *
 * handle - configuration handle
 * section_name - name of the section the key is in
 * key_name - name of the key
132
 * annotation - optional annotation to look for (NULL -> no annotation)
133 134 135 136 137 138
 * value - pointer to string
 * length - maximum length of string */
int configuration_get_value_relpath(
        ConfigHandle *handle,
        const char * section_name,
        const char * key_name,
139
        const char * annotation,
140 141 142
        char *value,
        size_t length);

143 144 145 146 147 148 149 150 151

/*
 * Get's the values for a give section/key pair which has multiple values.
 * Caller's responsible for transforming it to a useable type.
 * Assumes the key name is a MULTIKEY configuration type.
 * 
 * handle - configuration handle
 * section_name - name of the section the key is in
 * key_name - name of the key
152
 * annotation - optional annotation to look for (NULL -> no annotation)
153 154 155 156 157 158
 * values - array of points to values (must be freed by caller)
 * length - number of value items
 */
int configuration_get_multivalue(ConfigHandle *handle,
                                 const char * section_name,
                                 const char * key_name,
159
                                 const char * annotation,
160 161 162
                                 char ***values,
                                 size_t *length);

163 164 165 166 167 168 169
/*
 * Get's the value for a give section/key pair and converts it to an int.
 * Assumes the key name is a KEY configuration type.
 * 
 * handle - configuration handle
 * section_name - name of the section the key is in
 * key_name - name of the key
170
 * annotation - optional annotation to look for (NULL -> no annotation)
171 172 173 174 175
 * value - returned as a pointer to an integer
 */
int configuration_get_value_int (ConfigHandle *handle,
                                 const char *section_name,
                                 const char *key_name,
176
                                 const char * annotation,
177 178 179 180 181 182 183 184 185 186
                                 int *value);

/*
 * Get's the value for a give section/key pair and converts it to an
 * unsigned int.
 * Assumes the key name is a KEY configuration type.
 * 
 * handle - configuration handle
 * section_name - name of the section the key is in
 * key_name - name of the key
187
 * annotation - optional annotation to look for (NULL -> no annotation)
188 189 190 191 192
 * value - returned as a pointer to an unsigned integer
 */
int configuration_get_value_uint (ConfigHandle *handle,
                                  const char *section_name,
                                  const char *key_name,
193
                                  const char * annotation,
194 195 196 197 198 199 200 201 202
                                  unsigned int *value);

/*
 * Get's the value for a give section/key pair and converts it to a long int.
 * Assumes the key name is a KEY configuration type.
 * 
 * handle - configuration handle
 * section_name - name of the section the key is in
 * key_name - name of the key
203
 * annotation - optional annotation to look for (NULL -> no annotation)
204 205 206 207 208
 * value - returned as a pointer to a long integer
 */
int configuration_get_value_longint (ConfigHandle *handle,
                                     const char *section_name,
                                     const char *key_name,
209
                                     const char * annotation,
210 211 212 213 214 215 216 217 218
                                     long int *value);

/*
 * Get's the value for a give section/key pair and converts it to a double.
 * Assumes the key name is a KEY configuration type.
 * 
 * handle - configuration handle
 * section_name - name of the section the key is in
 * key_name - name of the key
219
 * annotation - optional annotation to look for (NULL -> no annotation)
220 221 222 223 224
 * value - returned as a pointer to a double
 */
int configuration_get_value_double (ConfigHandle *handle,
                                    const char *section_name,
                                    const char *key_name,
225
                                    const char * annotation,
226 227 228 229 230 231 232 233 234 235 236 237 238 239
                                    double *value);

/*
 * Get the LPGROUPS configuration from the config file which is stored
 * in the associated data structures.
 *
 * handle - configuration handle
 * section_name - name of section which has the lptypes
 * lpgroups - data structure to hold the lpgroup info
 */
int configuration_get_lpgroups (ConfigHandle *handle,
                                const char *section_name,
                                config_lpgroups_t *lpgroups);

240 241 242 243 244 245 246 247 248
/*
 * Helper function - get the position in the LP annotation list of the
 * given annotation. Used for configuration schemes where an array of
 * configuration values is generated based on the annotations in
 * config_anno_map_t
 * If annotation is not found, -1 is returned */
int configuration_get_annotation_index(const char *              anno,
                                       const config_anno_map_t * anno_map);

249 250 251 252
/*
 * Forward reference to the configuration handle
 */
extern ConfigHandle config;
253

254 255
extern config_lpgroups_t lpconf;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
256 257 258 259
#ifdef __cplusplus
}
#endif

260
#endif
Philip Carns's avatar
Philip Carns committed
261 262 263 264 265 266 267 268 269

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */