File: carto_file_module.c

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (333 lines) | stat: -rw-r--r-- 12,706 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * Copyright (c) 2004-2008 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2006-2007 Cisco Systems, Inc.  All rights reserved.
 *
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 */

#include "opal_config.h"

/* This component will only be compiled on File, where we are
   guaranteed to have <unistd.h> and friends */
#include <stdio.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif  /* HAVE_UNISTD_H */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif  /* HAVE_STDLIB_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif  /* HAVE_STRING_H */
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif  /* HAVE_ERRNO_H */

#include "opal/constants.h"
#include "opal/mca/base/mca_base_param.h"
#include "opal/util/argv.h"
#include "opal/util/show_help.h"
#include "opal/mca/carto/carto.h"
#include "opal/mca/carto/base/base.h"
#include "opal/mca/carto/base/carto_base_graph.h"
#include "carto_file.h"
#include "carto_file_lex.h"



char *carto_file_path;

static int opal_carto_file_init(void);
static int opal_carto_file_finalize(void);
static int opal_carto_file_parse(const char *cartofile);


/*
 * File carto module
 */
static const opal_carto_base_module_1_0_0_t loc_module = {
    opal_carto_file_init,
    opal_carto_base_graph_get_host_graph_fn,
    opal_carto_base_free_graph_fn,
    opal_carto_base_get_nodes_distance_fn,
    opal_carto_base_graph_spf_fn,
    opal_carto_base_graph_find_node_fn,
    opal_carto_file_finalize,
};


int opal_carto_file_component_query(mca_base_module_t **module, int *priority)
{
    int param;

    param = mca_base_param_find("carto", "file", "priority");
    mca_base_param_lookup_int(param, priority);

    param = mca_base_param_find("carto", "file", "path");
    mca_base_param_lookup_string(param, &carto_file_path);

    *module = (mca_base_module_t *)&loc_module;

    return OPAL_SUCCESS;
}


/**
 * Init the carto file module.
 * 
 * @return int success or error
 */
static int opal_carto_file_init(void)
{
    int rc;

    /* create an empty graph */
    if (NULL == opal_carto_base_common_host_graph) {
        opal_carto_base_graph_create_fn(&opal_carto_base_common_host_graph);
    }
    if (NULL == carto_file_path) {
        return OPAL_ERROR;
    }
    /* parse the carto file and add nodes and connections to the graph */
    rc = opal_carto_file_parse(carto_file_path);
    return rc;
}

/**
 * finalize the carto file module.
 * 
 * @return int success or error
 */
static int opal_carto_file_finalize(void)
{
    /* free the host cartography graph. */
    if (NULL != opal_carto_base_common_host_graph) {
        opal_carto_base_free_graph_fn(opal_carto_base_common_host_graph);
    }
    return OPAL_SUCCESS;
}


/**
 * Parse the carto file and file the host graph with nodes and
 * connections.
 * 
 * @param cartofile the path to the carto file.
 * 
 * @return int success or error.
 */
static int opal_carto_file_parse(const char *cartofile)
{
    int token;
    opal_carto_base_node_t *node, *end_node;
    uint32_t weight;
    char *node1_name, *node2_name, *value, **argv;
    int cnt, line_number = 1;
    char *token_to_string[] = {
        "New-line",
        "File-error",
        "Node deceleration",
        "Connection deceleration",
        "bi directional connection",
        "Integer",
        "Name",
        "Node connection",
        "Undefined"
        "Undefined"
        "Undefined"
    };

    /* set the done flag to false. at the end of file the the lexical analyzer will set it to true */
    carto_file_done = false;
    carto_file_in = fopen(cartofile, "r"); /* open the carto file */
    /* if the file not found, return an error */
    if (NULL == carto_file_in) {
        opal_show_help("help-opal-carto-file.txt", "file not found", true, cartofile);
        return OPAL_ERR_NOT_FOUND;
    }
    while (!carto_file_done) {
        token = carto_file_lex();
        switch (token) {
        case OPAL_CARTO_FILE_NEWLINE:
            line_number++;
            break;
        case OPAL_CARTO_FILE_NODE_DECELERATION:
                token = carto_file_lex();
                switch (token) {
                case OPAL_CARTO_FILE_NAME:
                    node = (opal_carto_base_node_t *)malloc(sizeof(opal_carto_base_node_t));
                    node->node_type = strdup(carto_file_value.sval);
                    if (0 == strcmp("socket",node->node_type)) {
                        node->is_cpu = true;
                    }
                    else {
                        node->is_cpu = false;
                    }
                    token = carto_file_lex();
                    switch (token) {
                    case OPAL_CARTO_FILE_NAME:
                        node->node_name = strdup(carto_file_value.sval);
                        opal_carto_base_graph_add_node_fn(opal_carto_base_common_host_graph, node);
                        break;
                    default:
                        free(node);
                        opal_show_help("help-opal-carto-file.txt", "expected node name", 
                                       true, cartofile, line_number, token_to_string[token]);
                        goto error;
                    }
                    break;
                default:
                    opal_show_help("help-opal-carto-file.txt", "expected node type", 
                                   true, cartofile, line_number, token_to_string[token]);
                    goto error;
                }
            break;
        case OPAL_CARTO_FILE_CONNECTION_DECELERATION:
            token = carto_file_lex();
            switch (token) {
            case OPAL_CARTO_FILE_NAME:
                node1_name = strdup(carto_file_value.sval);
                while (OPAL_CARTO_FILE_NEWLINE != token) {
                    token = carto_file_lex();
                    switch (token) {
                    case OPAL_CARTO_FILE_NODE_CONNECTION:
                        value = carto_file_value.sval;
                        argv = opal_argv_split (value, ':');
                        cnt = opal_argv_count (argv);
                        if (2 == cnt) {
                            node2_name = strdup(argv[0]);
                            weight = atoi(argv[1]);
                        } else {
                            opal_show_help("help-opal-carto-file.txt", "incorrect connection", true, cartofile, line_number, value);
                            opal_argv_free (argv);
                            free(node1_name);
                            goto error;
                        }
                        opal_argv_free (argv);
                        /* find the start node of the connection */
                        node = opal_carto_base_graph_find_node_fn(opal_carto_base_common_host_graph,node1_name);
                        if (NULL == node) {
                            opal_show_help("help-opal-carto-file.txt", "vertex not found", true, cartofile, line_number, node1_name);
                            free(node1_name);
                            free(node2_name);
                            goto error;
                        }
                        /* find the end node of the connection */
                        end_node = opal_carto_base_graph_find_node_fn(opal_carto_base_common_host_graph,node2_name);
                        if (NULL == end_node) {
                            opal_show_help("help-opal-carto-file.txt", "vertex not found", true, cartofile, line_number, node2_name);
                            free(node1_name);
                            free(node2_name);
                            goto error;
                        }
                        opal_carto_base_connect_nodes_fn(opal_carto_base_common_host_graph, node, end_node, weight);
                        free(node2_name);
                        break;
                    case OPAL_CARTO_FILE_NEWLINE:
                        line_number++;
                        break;
                    default:
                        opal_show_help("help-opal-carto-file.txt", "expected Connection", 
                                       true, cartofile, line_number, token_to_string[token]);
                        free(node1_name);
                        goto error;
                    }
                }
                free(node1_name);
                break;
            default:
                opal_show_help("help-opal-carto-file.txt", "expected node name", 
                               true, cartofile, line_number, token_to_string[token]);
                goto error;
            }
            break;
        case OPAL_CARTO_FILE_BIDIRECTION_CONNECTION:
            token = carto_file_lex();
            switch (token) {
            case OPAL_CARTO_FILE_NAME:
                node1_name = strdup(carto_file_value.sval);
                while (OPAL_CARTO_FILE_NEWLINE != token) {
                    token = carto_file_lex();
                    switch (token) {
                    case OPAL_CARTO_FILE_NODE_CONNECTION:
                        value = carto_file_value.sval;
                        argv = opal_argv_split (value, ':');
                        cnt = opal_argv_count (argv);
                        if (2 == cnt) {
                            node2_name = strdup(argv[0]);
                            weight = atoi(argv[1]);
                        } else {
                            opal_show_help("help-opal-carto-file.txt", "incorrect connection", true, cartofile, line_number, value);
                            opal_argv_free (argv);
                            free(node1_name);
                            goto error;
                        }
                        opal_argv_free (argv);
                        /* find the start node of the connection */
                        node = opal_carto_base_graph_find_node_fn(opal_carto_base_common_host_graph,node1_name);
                        if (NULL == node) {
                            opal_show_help("help-opal-carto-file.txt", "vertex not found", true, cartofile, line_number, node1_name);
                            free(node1_name);
                            free(node2_name);
                            goto error;
                        }
                        /* find the end node of the connection */
                        end_node = opal_carto_base_graph_find_node_fn(opal_carto_base_common_host_graph,node2_name);
                        if (NULL == end_node) {
                            opal_show_help("help-opal-carto-file.txt", "vertex not found", true, cartofile, line_number, node2_name);
                            free(node1_name);
                            free(node2_name);
                            goto error;
                        }
                        opal_carto_base_connect_nodes_fn(opal_carto_base_common_host_graph, node, end_node, weight);
                        opal_carto_base_connect_nodes_fn(opal_carto_base_common_host_graph, end_node, node, weight);
                        free(node2_name);
                        break;
                    case OPAL_CARTO_FILE_NEWLINE:
                        line_number++;
                        break;
                    default:
                        opal_show_help("help-opal-carto-file.txt", "expected Connection", 
                                       true, cartofile, line_number, token_to_string[token]);
                        free(node1_name);
                        goto error;
                    }
                }
                free(node1_name);
                break;
            default:
                opal_show_help("help-opal-carto-file.txt", "expected node name", 
                               true, cartofile, line_number, token_to_string[token]);
                goto error;
            }
            break;
        default:
            opal_show_help("help-opal-carto-file.txt", "expected deceleration", 
                           true, cartofile, line_number, token_to_string[token]);
            goto error;

        }
    }
    return OPAL_SUCCESS;
error:
    fclose(carto_file_in);
    return OPAL_ERR_BAD_PARAM;
}