SpM Handbook 1.2.4
Loading...
Searching...
No Matches
example_drivers.c
Go to the documentation of this file.
1/**
2 *
3 * @file example_drivers.c
4 *
5 * Example to show how to use the SpM drivers to read a sparse matrix form file,
6 * here with the Laplacian generator driver.
7 *
8 * @copyright 2020-2024 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
9 * Univ. Bordeaux. All rights reserved.
10 *
11 * @version 1.2.4
12 * @author Mathieu Faverge
13 * @author Tony Delarue
14 * @date 2024-05-29
15 *
16 * @ingroup examples_c
17 * @code
18 **/
19#include <spm.h>
20
21int main( int argc, char **argv )
22{
23 spmatrix_t spm;
24 double alpha = 2.;
25 spm_int_t size, ldb, ldx;
26 double epsilon, norm;
27 void *x, *b;
28 int rc = 0;
29
30 /*
31 * MPI need to be initialize before any call to spm library if it has been
32 * compiled wth MPI support.
33 */
34#if defined(SPM_WITH_MPI)
35 MPI_Init( &argc, &argv );
36#endif
37
38 /*
39 * Generate a sparse matrix using one of the many drivers.
40 */
41 rc = spmReadDriver( SpmDriverLaplacian, "10:10:10:2", &spm );
42 if ( rc != SPM_SUCCESS ) {
43 return rc;
44 }
45
46 /*
47 * Just for this example. If the driver do not provide values, let's create
48 * fake ones.
49 */
50 if ( spm.flttype == SpmPattern ) {
51 spmGenFakeValues( &spm );
52 }
53
54 /*
55 * Print basic information about the sparse matrix
56 */
57 spmPrintInfo( &spm, stdout );
58
59 /*
60 * Compute the frobenius norm of the spm
61 */
62 norm = spmNorm( SpmFrobeniusNorm, &spm );
63 fprintf( stdout, " || A ||_f: %e\n", norm );
64
65 /*
66 * Scale the sparse matrix.
67 */
68 if ( norm > 0. ) {
69 spmScal( 1. / norm, &spm );
70 }
71
72 /*
73 * Create a random vector x to test products.
74 * Note that you can get the size to allocate with spm_size_of() that
75 * returns the size of each value.
76 */
77 ldb = spm.nexp > 1 ? spm.nexp : 1;
78 ldx = spm.nexp > 1 ? spm.nexp : 1;
79 size = spm_size_of( spm.flttype ) * spm.nexp;
80 x = malloc( size );
81 rc = spmGenVec( SpmRhsRndX, &spm, &alpha, 24356, x, 1 );
82 if ( rc != SPM_SUCCESS ) {
83 free( x );
84 spmExit( &spm );
85 return rc;
86 }
87
88 /*
89 * Compute b = A * x
90 */
91 b = malloc( size );
92 spmMatVec( SpmNoTrans, 1., &spm, x, 0., b );
93
94 /*
95 * The following routines helps to check results obtained out of solvers by
96 * computing b = b - A * x with a given precision.
97 */
98 if ( (spm.flttype == SpmDouble ) ||
99 (spm.flttype == SpmComplex64) )
100 {
101 epsilon = 1e-15;
102 }
103 else {
104 epsilon = 1e-7;
105 }
106 epsilon = epsilon * spm.nnzexp / spm.gN;
107 rc = spmCheckAxb( epsilon, 1, &spm, NULL, 1, b, ldb, x, ldx );
108
109 free( x );
110 free( b );
111 spmExit( &spm );
112
113#if defined(SPM_WITH_MPI)
114 MPI_Finalize();
115#endif
116
117 (void)argc;
118 (void)argv;
119 return rc;
120}
121/**
122 * @endcode
123 */
static size_t spm_size_of(spm_coeftype_t type)
Double datatype that is not converted through precision generator functions.
Definition datatypes.h:209
@ SPM_SUCCESS
Definition const.h:82
@ SpmDriverLaplacian
Definition const.h:104
@ SpmNoTrans
Definition const.h:155
@ SpmComplex64
Definition const.h:66
@ SpmDouble
Definition const.h:64
@ SpmPattern
Definition const.h:62
@ SpmFrobeniusNorm
Definition const.h:200
spm_coeftype_t flttype
Definition spm.h:67
spm_int_t nexp
Definition spm.h:78
spm_int_t nnzexp
Definition spm.h:80
spm_int_t gN
Definition spm.h:72
void spmExit(spmatrix_t *spm)
Cleanup the spm structure but do not free the spm pointer.
Definition spm.c:224
int spm_int_t
The main integer datatype used in spm arrays.
Definition datatypes.h:70
int spmMatVec(spm_trans_t trans, double alpha, const spmatrix_t *spm, const void *x, double beta, void *y)
Compute a matrix-vector product.
Definition spm.c:1234
int spmCheckAxb(double eps, spm_int_t nrhs, const spmatrix_t *spm, void *opt_X0, spm_int_t opt_ldx0, void *B, spm_int_t ldb, const void *X, spm_int_t ldx)
Check the backward error, and the forward error if x0 is provided.
Definition spm.c:1423
void spmGenFakeValues(spmatrix_t *spm)
Generate the fake values array such that .
int spmReadDriver(spm_driver_t driver, const char *filename, spmatrix_t *spm)
Import a matrix file into a spm structure.
void spmScal(double alpha, spmatrix_t *spm)
Scale the spm.
Definition spm.c:1481
int spmGenVec(spm_rhstype_t type, const spmatrix_t *spm, void *alpha, unsigned long long int seed, void *x, spm_int_t incx)
Generate a vector associated to a given matrix.
Definition spm.c:1802
double spmNorm(spm_normtype_t ntype, const spmatrix_t *spm)
Compute the norm of the spm.
Definition spm.c:514
void spmPrintInfo(const spmatrix_t *spm, FILE *f)
Print basic informations about the spm matrix into a given stream.
Definition spm.c:1026
The sparse matrix data structure.
Definition spm.h:64