SpM Handbook 1.2.4
Loading...
Searching...
No Matches
spm_read_driver.c
Go to the documentation of this file.
1/**
2 * @file spm_read_driver.c
3 *
4 * SParse Matrix package file driver.
5 *
6 * @copyright 2016-2024 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
7 * Univ. Bordeaux. All rights reserved.
8 *
9 * @version 1.2.4
10 * @author Mathieu Faverge
11 * @author Pierre Ramet
12 * @author Matias Hastaran
13 * @author Tony Delarue
14 * @author Alycia Lisito
15 * @date 2024-06-26
16 *
17 **/
18#include "common.h"
19#include "spm_drivers.h"
20#if defined(SPM_WITH_SCOTCH)
21#include <scotch.h>
22#endif
23
24/**
25 *******************************************************************************
26 *
27 * @ingroup spm_dev_driver
28 * @brief Import a sparse matrix from a Scotch data file.
29 *
30 *******************************************************************************
31 *
32 * @param[in] filename
33 * The name of the file that stores the matrix (see driver).
34 *
35 * @param[inout] spm
36 * On entry, an allocated sparse matrix structure.
37 * On exit, the filled sparse matrix structure with the matrix from the
38 * file.
39 *
40 *******************************************************************************
41 *
42 * @retval SPM_SUCCESS if the file reading happened successfully,
43 * @retval SPM_ERR_BADPARAMETER if one the parameter is incorrect.
44 *
45 *******************************************************************************/
46static inline int
47spm_read_scotch( const char *filename,
48 spmatrix_t *spm )
49{
50#if !defined(SPM_WITH_SCOTCH)
51 (void)filename;
52 (void)spm;
53
54 fprintf( stderr, "Scotch driver to read graph file unavailable.\n"
55 "Compile with Scotch support to provide it\n" );
57
58#else
59
60 SCOTCH_Graph sgraph;
61 FILE *file;
62 SCOTCH_Num baseval = 1;
63
64 file = fopen( filename, "r" );
65 if ( file == NULL ) {
66 fprintf( stderr,"spmReadDriver: impossible to open the file %s\n", filename );
67 return SPM_ERR_FILE;
68 }
69
70 /* Check integer compatibility */
71 if (sizeof(spm_int_t) != sizeof(SCOTCH_Num)) {
72 fprintf( stderr,"Inconsistent integer type\n");
73 fclose(file);
75 }
76
77 SCOTCH_graphLoad( &sgraph, file, -1, 0 );
78 SCOTCH_graphData( &sgraph, &baseval, &(spm->n), &(spm->colptr), NULL, NULL, NULL,
79 &(spm->nnz), &(spm->rowptr), NULL );
80 fclose(file);
81
82 spm->replicated = 1;
83 spm->baseval = baseval;
84 spm->mtxtype = SpmGeneral;
85 spm->flttype = SpmPattern;
86 spm->fmttype = SpmCSC;
87 spm->dof = 1;
88
90 return SPM_SUCCESS;
91#endif
92}
93
94/**
95 *******************************************************************************
96 *
97 * @ingroup spm
98 *
99 * @brief Import a matrix file into a spm structure for a specific communicator.
100 *
101 * This function read or generate a sparse matrix from a file to store it into a
102 * spm structure. The different formats accepted by this driver are described by
103 * the driver field.
104 *
105 *******************************************************************************
106 *
107 * @param[in] scatter
108 * Boolean to specify if the final spm must be scattered or not.
109 *
110 * @param[in] driver
111 * This defines the driver to use to create the spm structure:
112 * - SpmDriverRSA
113 * - SpmDriverHB
114 * - SpmDriverIJV
115 * - SpmDriverMM
116 * - SpmDriverLaplacian
117 * - SpmDriverXLaplacian
118 * - SpmDriverGraph
119 * - SpmDriverSPM
120 *
121 * @param[in] filename
122 * The name of the file that stores the matrix (see driver).
123 *
124 * @param[inout] spm
125 * On entry, an allocated sparse matrix structure.
126 * On exit, the filled sparse matrix structure with the matrix from the
127 * file.
128 *
129 * @param[in] comm
130 * The MPI communicator of the problem
131 *
132 ********************************************************************************
133 *
134 * @retval SPM_SUCCESS if the file reading happened successfully,
135 * @retval SPM_ERR_BADPARAMETER if one the parameter is incorrect.
136 *
137 *******************************************************************************/
138static inline int
139spm_read_driver( int scatter,
140 spm_driver_t driver,
141 const char *filename,
142 spmatrix_t *spm,
143 SPM_Comm comm )
144{
145 int is_centralized = 1;
146 int rc = SPM_SUCCESS;
147
148 if ( filename == NULL ) {
149 fprintf( stderr, "spmReadDriver[Dist]: invalid filename parameter\n" );
151 }
152
153 if ( spm == NULL ) {
154 fprintf( stderr, "spmReadDriver[Dist]: invalide spm parameter\n" );
156 }
157
158 spmInitDist( spm, comm );
159
160 switch(driver)
161 {
162 case SpmDriverRSA:
163 /* The RSA driver is no longer supported in fortran */
164 fprintf(stderr, "RSA driver is no longer supported and is replaced by the HB driver\n");
165 spm_attr_fallthrough;
166
167 case SpmDriverHB:
168 /* TODO: Possible to read the RHS, the solution or a guess of the solution */
169 spm->replicated = 1;
170 rc = readHB( filename, spm );
171 break;
172
173 case SpmDriverIJV:
174 spm->replicated = 1;
175 rc = readIJV( filename, spm );
176 break;
177
178 case SpmDriverMM:
179 spm->replicated = 1;
180 rc = readMM( filename, spm );
181 break;
182
184 rc = genLaplacian( filename, spm );
185 is_centralized = 0;
186 break;
187
189 rc = genExtendedLaplacian( filename, spm );
190 is_centralized = 0;
191 break;
192
193 case SpmDriverSPM:
194 rc = spmLoad( spm, filename );
195 break;
196
197 case SpmDriverGraph:
198 rc = spm_read_scotch( filename, spm );
199 break;
200
201 default:
202 fprintf(stderr, "spmReadDriver: Driver not implemented\n");
203 return SPM_ERR_UNKNOWN;
204 }
205
206#if defined(SPM_WITH_MPI)
207 MPI_Allreduce( MPI_IN_PLACE, &rc, 1, MPI_INT,
208 MPI_MAX, comm );
209#endif
210 if ( rc != SPM_SUCCESS ) {
211 fprintf( stderr,"spmReadDriver[Dist]: error while reading the input %s\n", filename );
212 return rc;
213 }
214
215#if defined(SPM_WITH_MPI)
216 if ( spm->clustnbr > 1 ) {
217
218 if ( is_centralized && scatter )
219 {
220 /* Scatter the spm among the processes */
221 spmatrix_t spm_dist;
222
223 spmScatter( &spm_dist, -1, spm, 0, NULL, 1, spm->comm );
224
225 /* Switch the data structure */
226 spmExit( spm );
227 memcpy( spm, &spm_dist, sizeof(spmatrix_t) );
228 }
229
230 if ( !is_centralized && !scatter )
231 {
232 /* Gather the spm to replicate it on each node */
233 spmatrix_t spm_glob;
234
235 spmGather( spm, -1, &spm_glob );
236
237 /* Switch the data structure */
238 spmExit( spm );
239 memcpy( spm, &spm_glob, sizeof(spmatrix_t) );
240 }
241 }
242#endif
243
244 (void)is_centralized;
245 (void)scatter;
246 return SPM_SUCCESS;
247}
248
249/**
250 *******************************************************************************
251 *
252 * @ingroup spm
253 *
254 * @brief Import a matrix file into an spm structure for a specific communicator.
255 *
256 * This function read or generate a sparse matrix from a file to store it into
257 * an spm structure. The different formats accepted by this driver are described
258 * by the driver field.
259 *
260 *******************************************************************************
261 *
262 * @param[in] driver
263 * This defines the driver to use to create the spm structure:
264 * - SpmDriverRSA
265 * - SpmDriverHB
266 * - SpmDriverIJV
267 * - SpmDriverMM
268 * - SpmDriverLaplacian
269 * - SpmDriverXLaplacian
270 * - SpmDriverGraph
271 * - SpmDriverSPM
272 *
273 * @param[in] filename
274 * The name of the file that stores the matrix (see driver).
275 *
276 * @param[inout] spm
277 * On entry, an allocated sparse matrix structure.
278 * On exit, the filled sparse matrix structure with the matrix from the
279 * file.
280 *
281 * @param[in] comm
282 * The MPI communicator of the problem
283 *
284 ********************************************************************************
285 *
286 * @retval SPM_SUCCESS if the file reading happened successfully,
287 * @retval SPM_ERR_BADPARAMETER if one the parameter is incorrect.
288 *
289 *******************************************************************************/
290int
292 const char *filename,
293 spmatrix_t *spm,
294 SPM_Comm comm )
295{
296 return spm_read_driver( 1, driver, filename,
297 spm, comm );
298}
299
300/**
301 *******************************************************************************
302 *
303 * @ingroup spm
304 *
305 * @brief Import a matrix file into a spm structure.
306 *
307 * This function read or generate a sparse matrix from a file to store it into
308 * an spm structure. The different formats accepted by this driver are described
309 * by the driver field.
310 *
311 *******************************************************************************
312 *
313 * @param[in] driver
314 * This defines the driver to use to create the spm structure:
315 * - SpmDriverRSA
316 * - SpmDriverHB
317 * - SpmDriverIJV
318 * - SpmDriverMM
319 * - SpmDriverLaplacian
320 * - SpmDriverXLaplacian
321 * - SpmDriverGraph
322 * - SpmDriverSPM
323 *
324 * @param[in] filename
325 * The name of the file that stores the matrix (see driver).
326 *
327 * @param[inout] spm
328 * On entry, an allocated sparse matrix structure.
329 * On exit, the filled sparse matrix structure with the matrix from the
330 * file.
331 *
332 ********************************************************************************
333 *
334 * @retval SPM_SUCCESS if the file reading happened successfully,
335 * @retval SPM_ERR_BADPARAMETER if one the parameter is incorrect.
336 *
337 *******************************************************************************/
338int
340 const char *filename,
341 spmatrix_t *spm )
342{
343 return spm_read_driver( 0, driver, filename,
344 spm, MPI_COMM_WORLD );
345}
enum spm_driver_e spm_driver_t
The list of matrix driver readers and generators.
@ SPM_ERR_INTEGER_TYPE
Definition const.h:91
@ SPM_ERR_BADPARAMETER
Definition const.h:89
@ SPM_SUCCESS
Definition const.h:82
@ SPM_ERR_FILE
Definition const.h:90
@ SPM_ERR_UNKNOWN
Definition const.h:83
@ SpmDriverSPM
Definition const.h:107
@ SpmDriverHB
Definition const.h:101
@ SpmDriverRSA
Definition const.h:100
@ SpmDriverLaplacian
Definition const.h:104
@ SpmDriverXLaplacian
Definition const.h:105
@ SpmDriverMM
Definition const.h:103
@ SpmDriverIJV
Definition const.h:102
@ SpmDriverGraph
Definition const.h:106
@ SpmPattern
Definition const.h:62
@ SpmGeneral
Definition const.h:165
@ SpmCSC
Definition const.h:73
spm_coeftype_t flttype
Definition spm.h:67
int clustnbr
Definition spm.h:96
spm_int_t nnz
Definition spm.h:75
spm_int_t * rowptr
Definition spm.h:90
spm_fmttype_t fmttype
Definition spm.h:69
spm_int_t dof
Definition spm.h:82
spm_int_t n
Definition spm.h:73
spm_mtxtype_t mtxtype
Definition spm.h:65
SPM_Comm comm
Definition spm.h:97
spm_int_t baseval
Definition spm.h:71
spm_int_t * colptr
Definition spm.h:89
int replicated
Definition spm.h:98
int spmGather(const spmatrix_t *spm_scattered, int root, spmatrix_t *opt_spm_gathered)
Gather a distributed Sparse Matrix on a single node.
Definition spm_gather.c:436
void spmExit(spmatrix_t *spm)
Cleanup the spm structure but do not free the spm pointer.
Definition spm.c:224
int spmLoad(spmatrix_t *spm, const char *filename)
Load the spm structure from a file (internal format).
Definition spm_io.c:749
void spmUpdateComputedFields(spmatrix_t *spm)
Update all the computed fields based on the static values stored.
int spmScatter(spmatrix_t *spm_scattered, int root, const spmatrix_t *opt_spm_gathered, spm_int_t opt_n, const spm_int_t *opt_loc2glob, int opt_distByColumn, SPM_Comm opt_comm)
Scatter the SPM thanks to loc2glob.
int spm_int_t
The main integer datatype used in spm arrays.
Definition datatypes.h:70
void spmInitDist(spmatrix_t *spm, SPM_Comm comm)
Init the spm structure with a specific communicator.
Definition spm.c:101
int spmReadDriver(spm_driver_t driver, const char *filename, spmatrix_t *spm)
Import a matrix file into a spm structure.
static int spm_read_driver(int scatter, spm_driver_t driver, const char *filename, spmatrix_t *spm, SPM_Comm comm)
Import a matrix file into a spm structure for a specific communicator.
int spmReadDriverDist(spm_driver_t driver, const char *filename, spmatrix_t *spm, SPM_Comm comm)
Import a matrix file into an spm structure for a specific communicator.
The sparse matrix data structure.
Definition spm.h:64
int genExtendedLaplacian(const char *filename, spmatrix_t *spm)
Generate a extended Laplacian of size spm->n.
Definition laplacian.c:367
int genLaplacian(const char *filename, spmatrix_t *spm)
Generate a Laplacian of size spm->n.
Definition laplacian.c:291
static int spm_read_scotch(const char *filename, spmatrix_t *spm)
Import a sparse matrix from a Scotch data file.