SpM Handbook 1.2.4
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
example_lap2.c
Go to the documentation of this file.
1/**
2 *
3 * @file example_lap2.c
4 *
5 * Example to show how to define an spm matrix out of a matrix allocated by the
6 * 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 *colptr;
106 spm_int_t *rowptr;
107 double *values;
108 spm_int_t n, nnz;
109
110 dim1 = 10;
111 dim2 = 10;
112 dim3 = 10;
113 n = dim1 * dim2 * dim3;
114 nnz = (2*(dim1)-1) * dim2 * dim3 + (dim2-1)*dim1*dim3 + dim2*dim1*(dim3-1);
115
116 /*
117 * Allocate the pointers of the spm.
118 */
119 colptr = malloc( nnz * sizeof(spm_int_t) );
120 rowptr = malloc( nnz * sizeof(spm_int_t) );
121 values = malloc( nnz * sizeof(double) );
122
123 /*
124 * Generate the user matrix.
125 */
126 spm_example_fill_ijv_spm( colptr, rowptr, values,
127 dim1, dim2, dim3 );
128
129 /*
130 * Create the associated spm data structure.
131 */
132 spmInit( spm );
133
134 /*
135 * Set the fields according to what we want to generate.
136 * All non computed fields must be set.
137 */
138 spm->baseval = 1;
139 spm->mtxtype = SpmSymmetric;
140 spm->flttype = SpmDouble;
141 spm->fmttype = SpmIJV;
142 spm->n = n;
143 spm->nnz = nnz;
144 spm->dof = 1;
145 spm->colptr = colptr;
146 spm->rowptr = rowptr;
147 spm->values = values;
148
149 /* The full matrix is replicated on all nodes */
150 spm->replicated = 1;
151
152 /*
153 * Update the computed fields.
154 */
156}
157
158int main (int argc, char **argv)
159{
160 spmatrix_t spm;
161 spm_int_t nrhs = 10;
162 double alpha = 2.;
163 spm_int_t size, ldb, ldx;
164 double epsilon, norm;
165 void *x, *b;
166 int rc = 0;
167
168#if defined(SPM_WITH_MPI)
169 MPI_Init( &argc, &argv );
170#endif
171
172 /*
173 * Create a user made sparse matrix.
174 */
175 spm_example_create_laplacian( &spm );
176
177 /*
178 * Print basic information about the sparse matrix
179 */
180 spmPrintInfo( &spm, stdout );
181
182 /*
183 * Possibly convert the SPM into another format (CSC may be needed for a few routines)
184 */
185 spmConvert( SpmCSC, &spm );
186
187 /*
188 * Compute the frobenius norm of the spm
189 */
190 norm = spmNorm( SpmFrobeniusNorm, &spm );
191 fprintf( stdout, " || A ||_f: %e\n", norm );
192
193 /*
194 * Scale the sparse matrix.
195 */
196 if ( norm > 0. ) {
197 spmScal( 1. / norm, &spm );
198 }
199
200 /*
201 * Create a random matrix x to test products (multiple right hand side).
202 * Note that you can get the size to allocate with spm_size_of() that
203 * returns the size of each value.
204 */
205 ldb = spm.nexp > 1 ? spm.nexp : 1;
206 ldx = spm.nexp > 1 ? spm.nexp : 1;
207 size = spm_size_of( spm.flttype ) * spm.n * nrhs;
208 x = malloc( size );
209 rc = spmGenMat( SpmRhsRndX, nrhs, &spm, &alpha, 24356, x, ldx );
210 if ( rc != SPM_SUCCESS ) {
211 free( x );
212 spmExit( &spm );
213 return rc;
214 }
215
216 /*
217 * Compute b = A * x
218 */
219 b = malloc( size );
220 spmMatMat( SpmNoTrans, nrhs, 1., &spm, x, ldx, 0., b, ldb );
221
222 /*
223 * The following routines helps to check results obtained out of solvers by
224 * computing b = b - A * x with a given precision.
225 */
226 if ( (spm.flttype == SpmDouble ) ||
227 (spm.flttype == SpmComplex64) )
228 {
229 epsilon = 1e-15;
230 }
231 else {
232 epsilon = 1e-7;
233 }
234 epsilon = epsilon * spm.nnzexp / spm.gN;
235 rc = spmCheckAxb( epsilon, nrhs, &spm, NULL, 1, b, ldb, x, ldx );
236
237 free( x );
238 free( b );
239 spmExit( &spm );
240
241#if defined(SPM_WITH_MPI)
242 MPI_Finalize();
243#endif
244
245 (void)argc;
246 (void)argv;
247 return rc;
248}
249/**
250 * @endcode
251 */
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 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