graph_io.c 4.1 KB
Newer Older
Mathieu Faverge's avatar
Mathieu Faverge committed
1
2
3
4
/**
 *
 * @file graph_io.c
 *
5
 * PaStiX graph IO routines
Mathieu Faverge's avatar
Mathieu Faverge committed
6
 *
Pierre Ramet's avatar
Pierre Ramet committed
7
8
9
10
 * @copyright 2004-2017 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
 *                      Univ. Bordeaux. All rights reserved.
 *
 * @version 6.0.0
Mathieu Faverge's avatar
Mathieu Faverge committed
11
12
13
14
15
16
17
 * @author Xavier Lacoste
 * @author Pierre Ramet
 * @author Mathieu Faverge
 * @date 2013-06-24
 *
 **/
#include "common.h"
Mathieu Faverge's avatar
Mathieu Faverge committed
18
#include "graph.h"
19
#include "spm.h"
Mathieu Faverge's avatar
Mathieu Faverge committed
20
21
22
23
24
25

/**
 *******************************************************************************
 *
 * @ingroup pastix_graph
 *
Pierre Ramet's avatar
Pierre Ramet committed
26
27
28
 * @brief Load a graph from a file
 *
 * The file is named 'graphname' in the current directory.
Mathieu Faverge's avatar
Mathieu Faverge committed
29
30
31
32
33
34
35
 *
 *******************************************************************************
 *
 * @param[in] pastix_data
 *          The pointer to the solver instance to get options as rank,
 *          communicators, ...
 *
Pierre Ramet's avatar
Pierre Ramet committed
36
 * @param[inout] graph
Mathieu Faverge's avatar
Mathieu Faverge committed
37
 *          The graph structure to store the loaded graph.
38
39
40
 *          The graph is read from the file named by the environment variable
 *          PASTIX_FILE_GRAPH, and if PASTIX_FILE_GRAPH is not defined, the
 *          default filename "graphname" in the current directory is used.
Mathieu Faverge's avatar
Mathieu Faverge committed
41
42
 *
 *******************************************************************************/
43
void
44
45
graphLoad( const pastix_data_t *pastix_data,
           pastix_graph_t      *graph )
Mathieu Faverge's avatar
Mathieu Faverge committed
46
{
Mathieu Faverge's avatar
Mathieu Faverge committed
47
    pastix_spm_t spm;
48
49
50
    FILE *stream = NULL;
    char *filename = NULL;
    int env = 1;
Mathieu Faverge's avatar
Mathieu Faverge committed
51

Mathieu Faverge's avatar
Mathieu Faverge committed
52
    assert( pastix_data->procnbr == 1 );
Mathieu Faverge's avatar
Mathieu Faverge committed
53

54
55
56
57
    /* Parameter checks */
    if ( graph == NULL ) {
        return;
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
    /*
     * Get the environment variable as second option
     */
    filename = pastix_getenv( "PASTIX_FILE_GRAPH" );
    env = 1;

    /*
     * Get the default name as third option
     */
    if ( filename == NULL ) {
        filename = "graphname";
        env = 0;
    }

73
    stream = pastix_fopen( filename );
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    if ( stream ) {
        spmLoad( &spm, stream );
        fclose(stream);

        spmConvert( PastixCSC, &spm );
        graph->gN       = spm.gN;
        graph->n        = spm.n;
        graph->dof      = spm.dof;
        assert( spm.dof == 1 );
        graph->colptr   = spm.colptr;
        graph->rows     = spm.rowptr;
        graph->loc2glob = spm.loc2glob;
    }

    if (env) {
        pastix_cleanenv( filename );
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
91
    (void)pastix_data;
Mathieu Faverge's avatar
Mathieu Faverge committed
92
93
94
95
96
97
98
}

/**
 *******************************************************************************
 *
 * @ingroup pastix_graph
 *
Pierre Ramet's avatar
Pierre Ramet committed
99
100
101
 * @brief Save a graph to file.
 *
 * The file is named 'graphgen' in the current directory.
Mathieu Faverge's avatar
Mathieu Faverge committed
102
103
104
105
106
107
108
109
110
 *
 *******************************************************************************
 *
 * @param[in] pastix_data
 *          The pointer to the solver instance to get options as rank,
 *          communicators, ...
 *
 * @param[in] graph
 *          The graph structure to store the loaded graph.
111
112
113
 *          The graph is written to the file named by the environment variable
 *          PASTIX_FILE_GRAPH, and if PASTIX_FILE_GRAPH is not defined, the
 *          default filename "graphname" in the current directory is used.
Mathieu Faverge's avatar
Mathieu Faverge committed
114
115
 *
 *******************************************************************************/
116
117
118
void
graphSave( pastix_data_t        *pastix_data,
           const pastix_graph_t *graph )
Mathieu Faverge's avatar
Mathieu Faverge committed
119
{
Mathieu Faverge's avatar
Mathieu Faverge committed
120
    pastix_spm_t spm;
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    FILE *stream   = NULL;
    int   env      = 1;
    char *filename = NULL;

    /* Parameter checks */
    if ( graph == NULL ) {
        return;
    }

    /*
     * Get the environment variable as first option
     */
    filename = pastix_getenv( "PASTIX_FILE_GRAPH" );

    /*
     * Get the default name as second option
     */
    if ( filename == NULL ) {
        filename = "graphgen";
        env = 0;
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
142

Mathieu Faverge's avatar
Mathieu Faverge committed
143
144
145
146
147
148
149
150
151
152
    assert( pastix_data->procnbr == 1 );
    spm.n   = graph->n;
    spm.nnz = graph->colptr[ graph->n ] - graph->colptr[ 0 ];
    spm.dof = graph->dof;
    assert( spm.dof == 1 );
    spm.colptr   = graph->colptr;
    spm.rowptr   = graph->rows;
    spm.loc2glob = graph->loc2glob;
    spm.dofs     = NULL;

Mathieu Faverge's avatar
Mathieu Faverge committed
153
    spmUpdateComputedFields( &spm );
Mathieu Faverge's avatar
Mathieu Faverge committed
154

155
    stream = pastix_fopenw( &(pastix_data->dirtemp), filename, "w" );
156
157
158
159
    if ( stream ) {
        spmSave( &spm, stream );
        fclose(stream);
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
160

161
162
163
    if (env) {
        pastix_cleanenv( filename );
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
164
}