SpM Handbook 1.2.4
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.2.4
Author
Mathieu Faverge
Tony Delarue
Date
2024-05-29
/
#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 ) {
spmGenFakeValues( &spm );
}
/*
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;
}
/**
int spm_int_t
The main integer datatype used in spm arrays.
Definition datatypes.h:70
The sparse matrix data structure.
Definition spm.h:64

Definition in file example_drivers.c.