File: mpicomm.c

package info (click to toggle)
hwloc 2.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,032 kB
  • sloc: ansic: 58,129; xml: 12,064; sh: 6,822; makefile: 2,200; javascript: 1,623; perl: 380; cpp: 93; php: 8; sed: 4
file content (101 lines) | stat: -rw-r--r-- 2,247 bytes parent folder | download | duplicates (16)
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
/*
 * Copyright © 2016-2017 Inria.  All rights reserved.
 *
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 * See COPYING in top-level directory.
 *
 * $HEADER$
 */


#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <stdio.h>
#include <stdlib.h>

#include <netloc.h>
#include <private/netloc.h>

int netloc_build_comm_mat(char *filename, int *pn, double ***pmat)
{
    FILE *input = fopen(filename, "r");

    if (!input ) {
        perror("fopen");
        return NETLOC_ERROR;
    }
    char *line = NULL;
    size_t linesize = 0;

    char *ptr= NULL;
    int i,j;

    j = -1;
    i = 0;

    /* Get the number of elements in a line to find the size of the matrix */
    netloc_line_get(&line, &linesize, input);
    int n = 0;
    char *remain_line = line;
    while ((ptr = netloc_line_get_next_token(&remain_line, ' '))) {
        if (!strlen(ptr))
            break;
        n++;
    }
    rewind(input);

    if (!n) {
        goto error;
    }

    double *mat_values = (double *)malloc(sizeof(double[n*n]));
    double **mat = (double **)malloc(sizeof(double *[n]));
    for (int i = 0; i < n; i++) {
        mat[i] = &mat_values[i*n];
    }

    while (netloc_line_get(&line, &linesize, input) != -1) {
        char *remain_line = line;
        j = 0;
        while ((ptr = netloc_line_get_next_token(&remain_line, ' '))){
            if (!strlen(ptr))
                break;
            mat[i][j] = atof(ptr);
            if (mat[i][j] < 0) {
                fprintf(stderr, "Warning: negative value in comm matrix "
                        "(mat[%d][%d] = %f)\n", i, j, mat[i][j]);
            }
            j++;
        }
        if (j != n) {
            fprintf(stderr, "Error at %d %d (%d!=%d). "
                    "Too many columns for %s\n", i, j, j, n, filename);
            goto error;
        }
        i++;
    }

    if (i != n) {
        fprintf(stderr,"Error at %d %d. Too many rows for %s\n",
                i, j, filename);
        goto error;
    }

    fclose (input);

    *pn = n;
    *pmat = mat;

    free(line);
    return NETLOC_SUCCESS;

error:
    free(line);
    free(mat_values);
    free(mat);
    *pmat = NULL;
    *pn = 0;
    fclose (input);
    return NETLOC_ERROR;
}