Debian Codegraph @

packages/aztecoo/example/azoo_iterate_hb/main.c

raw
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // AztecOO: An Object-Oriented Aztec Linear Solver Package
6 // Copyright (2002) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux ([email protected])
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <ctype.h>
47 #include <assert.h>
48 #include <string.h>
49 #include <math.h>
50 #include "az_aztec.h"
51 #ifdef EPETRA_MPI
52 #define AZTEC_MPI
53 #define AZ_MPI
54 #endif
55 #ifdef AZTEC_MPI
56 #include "mpi.h"
57 #endif
58 #include "prototypes.h"
59
60 #define perror(str) { fprintf(stderr,"%s\n",str); exit(-1); }
61 #define double_quote '"'
62
63 #ifdef __cplusplus
64 extern "C" void AZOO_iterate(double * xsolve, double * b,
65 int * options, double * params,
66 double * status, int *proc_config,
67 AZ_MATRIX * Amat);
68 #endif
69 int main(int argc, char *argv[])
70 {
71 int proc_config[AZ_PROC_SIZE];/* Processor information. */
72 int options[AZ_OPTIONS_SIZE]; /* Array used to select solver options. */
73 double params[AZ_PARAMS_SIZE]; /* User selected solver paramters. */
74 int *data_org;
75 /* Array to specify data layout */
76 double status[AZ_STATUS_SIZE]; /* Information returned from AZ_solve(). */
77 int *update; /* vector elements updated on this node. */
78 int *external;
79 /* vector elements needed by this node. */
80 int *update_index;
81 /* ordering of update[] and external[] */
82 int *extern_index;
83 /* locally on this processor. */
84 int *indx; /* MSR format of real and imag parts */
85 int *bindx;
86 int *bpntr;
87 int *rpntr;
88 int *cpntr;
89 AZ_MATRIX *Amat;
90 AZ_PRECOND *Prec;
91 double *val;
92 double *x, *b, *xexact, *xsolve;
93 int n_nonzeros, n_blk_nonzeros;
94 int N_update; /* # of block unknowns updated on this node */
95 int N_local;
96 /* Number scalar equations on this node */
97 int N_global, N_blk_global; /* Total number of equations */
98 int N_external, N_blk_eqns;
99
100 double *val_msr;
101 int *bindx_msr;
102
103 double norm, d ;
104
105 int matrix_type;
106
107 int has_global_indices, option;
108 int i, j, m, mp ;
109 int ione = 1;
110 int rank, nproc;
111
112 #ifdef AZTEC_MPI
113 MPI_Init(&argc,&argv);
114 #endif
115
116 /* get number of processors and the name of this processor */
117
118 #ifdef AZTEC_MPI
119 AZ_set_proc_config(proc_config,MPI_COMM_WORLD);
120 #else
121 AZ_set_proc_config(proc_config,0);
122 #endif
123
124 rank = proc_config[AZ_node];
125 nproc = proc_config[AZ_N_procs];
126 printf("proc %d of %d is alive\n",rank, nproc) ;
127
128 #ifdef VBR
129 if(argc != 3)
130 perror("error: enter name of data and partition file on command line") ;
131 #else
132 if(argc != 2) perror("error: enter name of data file on command line") ;
133 #endif
134 /* Set exact solution to NULL */
135 xexact = NULL;
136
137 #ifdef AZTEC_MPI
138 /* Uncomment this code to debug in parallel */
139 /* if (rank==0) {
140 printf("Hit any key to continue...\n");
141 i = getchar();
142 }
143 MPI_Barrier(MPI_COMM_WORLD); */
144 #endif
145
146 /* Read matrix file and distribute among processors.
147 Returns with this processor's set of rows */
148
149 #ifdef VBR
150 read_hb(argv[1], proc_config, &N_global, &n_nonzeros,
151 &val_msr, &bindx_msr, &x, &b, &xexact);
152
153 create_vbr(argv[2], proc_config, &N_global, &N_blk_global,
154 &n_nonzeros, &n_blk_nonzeros, &N_update, &update,
155 bindx_msr, val_msr, &val, &indx,
156 &rpntr, &cpntr, &bpntr, &bindx);
157
158 if(proc_config[AZ_node] == 0)
159 {
160 free ((void *) val_msr);
161 free ((void *) bindx_msr);
162 free ((void *) cpntr);
163 }
164 matrix_type = AZ_VBR_MATRIX;
165
166
167 distrib_vbr_matrix( proc_config, N_global, N_blk_global,
168 &n_nonzeros, &n_blk_nonzeros,
169 &N_update, &update,
170 &val, &indx, &rpntr, &cpntr, &bpntr, &bindx,
171 &x, &b, &xexact);
172
173 #else
174 read_hb(argv[1], proc_config, &N_global, &n_nonzeros,
175 &val, &bindx, &x, &b, &xexact);
176
177 distrib_msr_matrix(proc_config, N_global, &n_nonzeros, &N_update,
178 &update, &val, &bindx, &x, &b, &xexact);
179
180 matrix_type = AZ_MSR_MATRIX;
181 #endif
182 /* convert matrix to a local distributed matrix */
183 AZ_transform(proc_config, &external, bindx, val, update,
184 &update_index, &extern_index, &data_org,
185 N_update, indx, bpntr, rpntr, &cpntr,
186 matrix_type);
187
188 printf("Processor %d: Completed AZ_transform\n",proc_config[AZ_node]) ;
189 has_global_indices = 0;
190 option = AZ_LOCAL;
191
192 #ifdef VBR
193 N_local = rpntr[N_update];
194 #else
195 N_local = N_update;
196 #endif
197
198 Amat = AZ_matrix_create(N_local);
199
200 #ifdef VBR
201 AZ_set_VBR(Amat, rpntr, cpntr, bpntr, indx, bindx, val, data_org,
202 N_update, update, option);
203 #else
204 AZ_set_MSR(Amat, bindx, val, data_org, N_update, update, option);
205 #endif
206
207
208 printf("proc %d Completed AZ_create_matrix\n",proc_config[AZ_node]) ;
209
210
211 if (has_global_indices)
212 {
213 N_external = 0;
214 }
215 else
216 {
217 N_external = data_org[AZ_N_external];
218 }
219
220 xsolve = (double *) calloc(N_local + N_external,
221 sizeof(double)) ;
222
223 for (i=0; i<N_local; i++) xsolve[i] = x[i];
224
225 /* Reorder rhs and xsolve to match matrix ordering from AZ_transform */
226 if (!has_global_indices)
227 {
228 AZ_reorder_vec(b, data_org, update_index, rpntr) ;
229 AZ_reorder_vec(xsolve, data_org, update_index, rpntr) ;
230 }
231
232 #ifdef VBR
233 AZ_check_vbr(N_update, data_org[AZ_N_ext_blk], AZ_LOCAL,
234 bindx, bpntr, cpntr, rpntr, proc_config);
235 #else
236 AZ_check_msr(bindx, N_update, N_external, AZ_LOCAL, proc_config);
237 #endif
238
239 printf("Processor %d of %d N_local = %d N_external = %d NNZ = %d\n",
240 proc_config[AZ_node],proc_config[AZ_N_procs],N_local,N_external,
241 n_nonzeros);
242
243 AZ_defaults(options, params);
244
245 options[AZ_solver] = AZ_gmres;
246 options[AZ_precond] = AZ_dom_decomp;
247 options[AZ_subdomain_solve] = AZ_ilut;
248 /*
249 params[AZ_ilut_fill] = 2.0;
250 params[AZ_drop] = 0.01;
251 options[AZ_overlap] = 0;
252 options[AZ_reorder] = 0;
253 params[AZ_rthresh] = 1.0E-1;
254 params[AZ_athresh] = 1.0E-1;
255 options[AZ_precond] = AZ_dom_decomp ;
256 options[AZ_subdomain_solve] = AZ_bilu_ifp;
257 options[AZ_reorder] = 0;
258 options[AZ_graph_fill] = 0;
259 params[AZ_rthresh] = 1.0E-7;
260 params[AZ_athresh] = 1.0E-7;
261 options[AZ_poly_ord] = 1;
262 options[AZ_precond] = AZ_Jacobi;
263 params[AZ_omega] = 1.0;
264 options[AZ_precond] = AZ_none ;
265
266 options[AZ_poly_ord] = 1;
267 options[AZ_precond] = AZ_Jacobi ;
268 options[AZ_scaling] = AZ_sym_row_sum ;
269 options[AZ_scaling] = AZ_sym_diag;
270
271
272 options[AZ_conv] = AZ_noscaled;
273 options[AZ_scaling] = AZ_Jacobi ;
274
275 options[AZ_precond] = AZ_dom_decomp ;
276 options[AZ_subdomain_solve] = AZ_icc ;
277 options[AZ_subdomain_solve] = AZ_ilut ;
278 params[AZ_omega] = 1.2;
279 params[AZ_ilut_fill] = 2.0;
280 params[AZ_drop] = 0.01;
281 options[AZ_reorder] = 0;
282 options[AZ_overlap] = 0;
283 options[AZ_type_overlap] = AZ_symmetric;
284
285 options[AZ_precond] = AZ_dom_decomp ;
286 options[AZ_subdomain_solve] = AZ_bilu ;
287 options[AZ_graph_fill] = 0;
288 options[AZ_overlap] = 0;
289
290 options[AZ_precond] = AZ_dom_decomp ;
291 options[AZ_subdomain_solve] = AZ_bilu_ifp ;
292 options[AZ_graph_fill] = 0;
293 options[AZ_overlap] = 0;
294 params[AZ_rthresh] = 1.0E-3;
295 params[AZ_athresh] = 1.0E-3;
296
297 options[AZ_poly_ord] = 1;
298 options[AZ_precond] = AZ_Jacobi ;
299 */
300
301
302 options[AZ_kspace] = 1000;
303
304 options[AZ_max_iter] = 1000;
305 params[AZ_tol] = 1.0e-14;
306 Prec = AZ_precond_create(Amat,AZ_precondition, NULL);
307 AZOO_iterate(xsolve, b, options, params, status, proc_config, Amat,
308 Prec, NULL);
309
310 if (proc_config[AZ_node]==0)
311 {
312 printf("True residual norm = %22.16g\n",status[AZ_r]);
313 printf("True scaled res = %22.16g\n",status[AZ_scaled_r]);
314 printf("Computed res norm = %22.16g\n",status[AZ_rec_r]);
315 }
316
317 /* Get solution back into original ordering */
318 if (!has_global_indices) {
319 AZ_invorder_vec(xsolve, data_org, update_index, rpntr, x);
320 free ((void *) xsolve);
321 }
322 else {
323 free ((void *) x);
324 x = xsolve;
325 }
326
327
328 if (xexact != NULL)
329 {
330 double sum = 0.0;
331 double largest = 0.0;
332 for (i=0; i<N_local; i++) sum += fabs(x[i]-xexact[i]);
333 printf("Processor %d: Difference between exact and computed solution = %12.4g\n",
334 proc_config[AZ_node],sum);
335 for (i=0; i<N_local; i++) largest = AZ_MAX(largest,fabs(xexact[i]));
336 printf("Processor %d: Difference divided by max abs value of exact = %12.4g\n",
337 proc_config[AZ_node],sum/largest);
338 free((void *) xexact);
339 }
340
341
342 AZ_precond_destroy(&Prec);
343 AZ_matrix_destroy(&Amat);
344 AZ_free((void *) update);
345 AZ_free((void *) update_index);
346 AZ_free((void *) external);
347 AZ_free((void *) extern_index);
348 AZ_free((void *) data_org);
349 AZ_free((void *) cpntr) ;
350
351 free((void *) b) ;
352 free((void *) x) ;
353 free((void *) val) ;
354 free((void *) rpntr) ;
355 free((void *) bpntr) ;
356 free((void *) bindx) ;
357 free((void *) indx) ;
358
359 #ifdef AZTEC_MPI
360 MPI_Finalize() ;
361 #endif
362
363 /* end main
364 */
365 return 0 ;
366 }