SpM Handbook 1.3.0
Loading...
Searching...
No Matches
example_drivers.c File Reference

Go to the source code of this file.

Detailed Description

Example to show how to use the SpM drivers to read a sparse matrix form file, here with the Laplacian generator driver.

Version
1.3.0
Author
Mathieu Faverge
Tony Delarue
Date
2024-07-09
/
#include <spm.h>
int main( int argc, char **argv )
{
double alpha = 2.;
spm_int_t size, ldb, ldx;
double epsilon, norm;
void *x, *b;
int rc = 0;
/*
* MPI need to be initialize before any call to spm library if it has been
* compiled wth MPI support.
*/
#if defined(SPM_WITH_MPI)
MPI_Init( &argc, &argv );
#endif
/*
* Generate a sparse matrix using one of the many drivers.
*/
rc = spmReadDriver( SpmDriverLaplacian, "10:10:10:2", &spm );
if ( rc != SPM_SUCCESS ) {
return rc;
}
/*
* Just for this example. If the driver do not provide values, let's create
* fake ones.
*/
if ( spm.flttype == SpmPattern ) {
}
/*
* Print basic information about the sparse matrix
*/
spmPrintInfo( &spm, stdout );
/*
* Compute the frobenius norm of the spm
*/
norm = spmNorm( SpmFrobeniusNorm, &spm );
fprintf( stdout, " || A ||_f: %e\n", norm );
/*
* Scale the sparse matrix.
*/
if ( norm > 0. ) {
spmScal( 1. / norm, &spm );
}
/*
* Create a random vector x to test products.
* Note that you can get the size to allocate with spm_size_of() that
* returns the size of each value.
*/
ldb = spm.nexp > 1 ? spm.nexp : 1;
ldx = spm.nexp > 1 ? spm.nexp : 1;
size = spm_size_of( spm.flttype ) * spm.nexp;
x = malloc( size );
rc = spmGenVec( SpmRhsRndX, &spm, &alpha, 24356, x, 1 );
if ( rc != SPM_SUCCESS ) {
free( x );
spmExit( &spm );
return rc;
}
/*
* Compute b = A * x
*/
b = malloc( size );
spmMatVec( SpmNoTrans, 1., &spm, x, 0., b );
/*
* The following routines helps to check results obtained out of solvers by
* computing b = b - A * x with a given precision.
*/
if ( (spm.flttype == SpmDouble ) ||
(spm.flttype == SpmComplex64) )
{
epsilon = 1e-15;
}
else {
epsilon = 1e-7;
}
epsilon = epsilon * spm.nnzexp / spm.gN;
rc = spmCheckAxb( epsilon, 1, &spm, NULL, 1, b, ldb, x, ldx );
free( x );
free( b );
spmExit( &spm );
#if defined(SPM_WITH_MPI)
MPI_Finalize();
#endif
(void)argc;
(void)argv;
return rc;
}
/**
*
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:68
spm_int_t nexp
Definition spm.h:79
spm_int_t nnzexp
Definition spm.h:81
spm_int_t gN
Definition spm.h:73
void spmExit(spmatrix_t *spm)
Cleanup the spm structure but do not free the spm pointer.
Definition spm.c:234
struct spmatrix_s spmatrix_t
The sparse matrix data structure.
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:1245
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:1434
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:1492
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:1813
double spmNorm(spm_normtype_t ntype, const spmatrix_t *spm)
Compute the norm of the spm.
Definition spm.c:525
int spm_int_t
The main integer datatype used in spm arrays.
Definition datatypes.h:70
void spmPrintInfo(const spmatrix_t *spm, FILE *f)
Print basic informations about the spm matrix into a given stream.
Definition spm.c:1037

Definition in file example_drivers.c.