SpM Handbook 1.2.4
Loading...
Searching...
No Matches
example_lap1.c
Go to the documentation of this file.
1/**
2 *
3 * @file example_lap1.c
4 *
5 * Example to show how to use the SPM library with an spm matrix allocated
6 * through the library but initialized by the user.
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-06-26
15 *
16 * @ingroup examples_c
17 * @code
18 **/
19#include <spm.h>
20
21/**
22 * @brief Create an IJV symmetric laplacian matrix for a regular cube of
23 * dimensions (dim1 x dim2 x dim3) and with 1-based values as in Fortran arrays.
24 */
25static inline void
26spm_example_fill_ijv_spm( spm_int_t *colptr,
27 spm_int_t *rowptr,
28 double *values,
29 spm_int_t dim1,
30 spm_int_t dim2,
31 spm_int_t dim3 )
32{
33 spm_int_t l, i, j, k;
34 spm_int_t mdim1, mdim2, mdim3;
35 double alpha = 8.;
36 double beta = 0.5;
37
38 mdim1 = dim1 - 1;
39 mdim2 = dim2 - 1;
40 mdim3 = dim3 - 1;
41 l = 0;
42 for( i = 0; i < dim1; i++ )
43 {
44 for( j = 0; j < dim2; j++ )
45 {
46 for( k = 0; k < dim3; k++ )
47 {
48 /*
49 * Start with the diagonal element connected with its two
50 * neigbours in the three directions
51 */
52 rowptr[l] = i + dim1 * j + dim1 * dim2 * k + 1;
53 colptr[l] = i + dim1 * j + dim1 * dim2 * k + 1;
54 values[l] = 6.;
55
56 /*
57 * Handle limit cases
58 */
59 if ( i == 0 ) { values[l] -= 1.; }
60 if ( i == mdim1 ) { values[l] -= 1.; }
61 if ( j == 0 ) { values[l] -= 1.; }
62 if ( j == mdim2 ) { values[l] -= 1.; }
63 if ( k == 0 ) { values[l] -= 1.; }
64 if ( k == mdim3 ) { values[l] -= 1.; }
65
66 /* Scale by alpha */
67 values[l] *= alpha;
68 l++;
69
70 /* Add connexions */
71 if ( i < mdim1 ) {
72 rowptr[l] = (i+1) + dim1 * j + dim1 * dim2 * k + 1;
73 colptr[l] = i + dim1 * j + dim1 * dim2 * k + 1;
74 values[l] = - beta;
75 l++;
76 }
77
78 if ( j < mdim2 ) {
79 rowptr[l] = i + dim1 * (j+1) + dim1 * dim2 * k + 1;
80 colptr[l] = i + dim1 * j + dim1 * dim2 * k + 1;
81 values[l] = - beta;
82 l++;
83 }
84
85 if ( k < mdim3 ) {
86 rowptr[l] = i + dim1 * j + dim1 * dim2 * (k+1) + 1;
87 colptr[l] = i + dim1 * j + dim1 * dim2 * k + 1;
88 values[l] = - beta;
89 l++;
90 }
91 }
92 }
93 }
94
95 assert( l == ((2*(dim1)-1) * dim2 * dim3 + (dim2-1)*dim1*dim3 + dim2*dim1*(dim3-1)) );
96}
97
98/**
99 * @brief Create a laplacian manually
100 */
101void
102spm_example_create_laplacian( spmatrix_t *spm )
103{
104 spm_int_t dim1, dim2, dim3;
105 spm_int_t n, nnz;
106
107 dim1 = 10;
108 dim2 = 10;
109 dim3 = 10;
110 n = dim1 * dim2 * dim3;
111 nnz = (2*(dim1)-1) * dim2 * dim3 + (dim2-1)*dim1*dim3 + dim2*dim1*(dim3-1);
112
113 /*
114 * Initialize all non computed fields.
115 */
116 spmInit( spm );
117
118 /*
119 * Set the fields according to what we want to generate.
120 * All non computed fields must be set.
121 */
122 spm->baseval = 1;
123 spm->mtxtype = SpmSymmetric;
124 spm->flttype = SpmDouble;
125 spm->fmttype = SpmIJV;
126 spm->n = n;
127 spm->nnz = nnz;
128 spm->dof = 1;
129
130 /* The full matrix is replicated on all nodes */
131 spm->replicated = 1;
132
133 /*
134 * Update the computed fields.
135 * If dof < 0, please see example_user2.c
136 */
138
139 /*
140 * Allocate the arrays of the spm.
141 */
142 spmAlloc( spm );
143
144 /*
145 * Fill the arrays with the generator.
146 */
147 spm_example_fill_ijv_spm( spm->colptr,
148 spm->rowptr,
149 spm->values,
150 dim1, dim2, dim3 );
151}
152
153int main( int argc, char **argv )
154{
155 spmatrix_t spm;
156 spm_int_t nrhs = 10;
157 double alpha = 2.;
158 spm_int_t size, ldb, ldx;
159 double epsilon, norm;
160 void *x, *b;
161 int rc = 0;
162
163 /*
164 * MPI need to be initialize before any call to spm library if it has been
165 * compiled wth MPI support.
166 */
167#if defined(SPM_WITH_MPI)
168 MPI_Init( &argc, &argv );
169#endif
170
171 /*
172 * Create a user made sparse matrix.
173 */
174 spm_example_create_laplacian( &spm );
175
176 /*
177 * Print basic information about the sparse matrix
178 */
179 spmPrintInfo( &spm, stdout );
180
181 /*
182 * Possibly convert the SPM into another format (CSC may be needed for a few routines)
183 */
184 spmConvert( SpmCSC, &spm );
185
186 /*
187 * Compute the frobenius norm of the spm
188 */
189 norm = spmNorm( SpmFrobeniusNorm, &spm );
190 fprintf( stdout, " || A ||_f: %e\n", norm );
191
192 /*
193 * Scale the sparse matrix.
194 */
195 if ( norm > 0. ) {
196 spmScal( 1. / norm, &spm );
197 }
198
199 /*
200 * Create a random matrix x to test products (multiple right hand side).
201 * Note that you can get the size to allocate with spm_size_of() that
202 * returns the size of each value.
203 */
204 ldb = spm.nexp > 1 ? spm.nexp : 1;
205 ldx = spm.nexp > 1 ? spm.nexp : 1;
206 size = spm_size_of( spm.flttype ) * spm.n * nrhs;
207 x = malloc( size );
208 rc = spmGenMat( SpmRhsRndX, nrhs, &spm, &alpha, 24356, x, ldx );
209 if ( rc != SPM_SUCCESS ) {
210 free( x );
211 spmExit( &spm );
212 return rc;
213 }
214
215 /*
216 * Compute b = A * x
217 */
218 b = malloc( size );
219 spmMatMat( SpmNoTrans, nrhs, 1., &spm, x, ldx, 0., b, ldb );
220
221 /*
222 * The following routines helps to check results obtained out of solvers by
223 * computing b = b - A * x with a given precision.
224 */
225 if ( (spm.flttype == SpmDouble ) ||
226 (spm.flttype == SpmComplex64) )
227 {
228 epsilon = 1e-15;
229 }
230 else {
231 epsilon = 1e-7;
232 }
233 epsilon = epsilon * spm.nnzexp / spm.gN;
234 rc = spmCheckAxb( epsilon, nrhs, &spm, NULL, 1, b, ldb, x, ldx );
235
236 free( x );
237 free( b );
238 spmExit( &spm );
239
240#if defined(SPM_WITH_MPI)
241 MPI_Finalize();
242#endif
243
244 (void)argc;
245 (void)argv;
246 return rc;
247}
248/**
249 * @endcode
250 */
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
@ SpmNoTrans
Definition const.h:155
@ SpmComplex64
Definition const.h:66
@ SpmDouble
Definition const.h:64
@ SpmSymmetric
Definition const.h:166
@ SpmCSC
Definition const.h:73
@ SpmIJV
Definition const.h:75
@ SpmFrobeniusNorm
Definition const.h:200
spm_coeftype_t flttype
Definition spm.h:67
spm_int_t nexp
Definition spm.h:78
void * values
Definition spm.h:92
spm_int_t nnz
Definition spm.h:75
spm_int_t nnzexp
Definition spm.h:80
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_int_t baseval
Definition spm.h:71
spm_int_t * colptr
Definition spm.h:89
spm_int_t gN
Definition spm.h:72
int replicated
Definition spm.h:98
void spmAlloc(spmatrix_t *spm)
Allocate the arrays of an spm structure.
Definition spm.c:175
void spmExit(spmatrix_t *spm)
Cleanup the spm structure but do not free the spm pointer.
Definition spm.c:224
void spmUpdateComputedFields(spmatrix_t *spm)
Update all the computed fields based on the static values stored.
int spm_int_t
The main integer datatype used in spm arrays.
Definition datatypes.h:70
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
int spmConvert(int ofmttype, spmatrix_t *ospm)
Convert the storage format of the spm.
Definition spm.c:418
int spmMatMat(spm_trans_t trans, spm_int_t n, double alpha, const spmatrix_t *A, const void *B, spm_int_t ldb, double beta, void *C, spm_int_t ldc)
Compute a matrix-matrix product.
Definition spm.c:1327
void spmScal(double alpha, spmatrix_t *spm)
Scale the spm.
Definition spm.c:1481
double spmNorm(spm_normtype_t ntype, const spmatrix_t *spm)
Compute the norm of the spm.
Definition spm.c:514
void spmInit(spmatrix_t *spm)
Init the spm structure.
Definition spm.c:152
int spmGenMat(spm_rhstype_t type, spm_int_t nrhs, const spmatrix_t *spm, void *alpha, unsigned long long int seed, void *A, spm_int_t lda)
Generate a set of vectors associated to a given matrix.
Definition spm.c:1732
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