Debian Codegraph @

packages/aztecoo/example/azoo_iterate_hb/read_hb.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 <stdlib.h>
45 #include <stdio.h>
46 #include "az_aztec.h"
47 #include "iohb.h"
48 #include "prototypes.h"
49
50 void read_hb(char *data_file, int *proc_config,
51 int *N_global, int *n_nonzeros,
52 double **val, int **bindx,
53 double **x, double **b, double **xexact)
54 #undef DEBUG
55 /* read ASCII data file:
56 line 1: N_global, number of entries (%d,%d)
57 line 2-...: i,j,real (%d, %d, %f)
58 */
59
60 {
61 FILE *in_file ;
62 char Title[73], Key[9], Rhstype[4];
63 char Type[4];
64 char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
65 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
66
67 int i, n_entries, N_columns, Nrhs;
68 int ii, jj ;
69 int kk = 0;
70 int isym;
71 int ione = 1;
72 double value, res;
73 double *cnt;
74 int *pntr, *indx1, *pntr1;
75 double *val1;
76
77 int MAXBLOCKSIZE = 1;
78
79 if(proc_config[AZ_node] == 0)
80 {
81 in_file = fopen( data_file, "r");
82 if (in_file == NULL)
83 {
84 printf("Error: Cannot open file: %s\n",data_file);
85 exit(1);
86 }
87
88 /* Get information about the array stored in the file specified in the */
89 /* argument list: */
90
91 printf("Reading matrix info from %s...\n",data_file);
92
93 in_file = fopen( data_file, "r");
94 if (in_file == NULL)
95 {
96 printf("Error: Cannot open file: %s\n",data_file);
97 exit(1);
98 }
99
100 readHB_header(in_file, Title, Key, Type, N_global, &N_columns,
101 &n_entries, &Nrhs,
102 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
103 &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
104 fclose(in_file);
105
106 if (Nrhs < 0 ) Nrhs = 0;
107
108 printf("***************************************************************\n");
109 printf("Matrix in file %s is %d x %d, \n",data_file, *N_global, N_columns);
110 printf("with %d nonzeros with type %3s;\n", n_entries, Type);
111 printf("***************************************************************\n");
112 printf("Title: %72s\n",Title);
113 printf("***************************************************************\n");
114 /*Nrhs = 0; */
115 printf("%d right-hand-side(s) available.\n",Nrhs);
116
117 if (Type[0] != 'R') perror("Can only handle real valued matrices");
118 isym = 0;
119 if (Type[1] == 'S')
120 {
121 printf("Converting symmetric matrix to nonsymmetric storage\n");
122 n_entries = 2*n_entries - N_columns;
123 isym = 1;
124 }
125 if (Type[2] != 'A') perror("Can only handle assembled matrices");
126 if (N_columns != *N_global) perror("Matrix dimensions must be the same");
127 *n_nonzeros = n_entries;
128
129 /* Read the matrix information, generating the associated storage arrays */
130 printf("Reading the matrix from %s...\n",data_file);
131
132 /* Allocate space. Note that we add extra storage in case of zero
133 diagonals. This is necessary for conversion to MSR format. */
134
135 pntr = (int *) calloc(N_columns+1,sizeof(int)) ;
136 *bindx = (int *) calloc(n_entries+N_columns+1,sizeof(int)) ;
137 *val = (double *) calloc(n_entries+N_columns+1,sizeof(double)) ;
138
139 readHB_mat_double(data_file, pntr, *bindx, *val);
140
141 /* If a rhs is specified in the file, read one,
142 generating the associate storage */
143 if (Nrhs > 0 && Rhstype[2] =='X')
144 {
145 printf("Reading right-hand-side vector(s) from %s...\n",data_file);
146 *b = (double *) calloc(N_columns,sizeof(double));
147 readHB_aux_double(data_file, 'F', (*b));
148 printf("Reading exact solution vector(s) from %s...\n",data_file);
149 *xexact = (double *) calloc(N_columns,sizeof(double));
150 readHB_aux_double(data_file, 'X', (*xexact));
151
152 }
153 else
154 {
155
156 /* Set Xexact to a random vector */
157
158 printf("Setting random exact solution vector\n");
159 *xexact = (double *) calloc(N_columns,sizeof(double));
160
161 for (i=0;i<*N_global;i++) (*xexact)[i] = drand48();
162
163 /* Compute b to match xexact */
164
165 *b = (double *) calloc(N_columns*MAXBLOCKSIZE,sizeof(double)) ;
166 if ((*b) == NULL) perror("Error: Not enough space to create rhs");
167
168 for (i = 0; i <= *N_global; i++) pntr[i]--;
169 for (i = 0; i <= n_entries; i++) (*bindx)[i]--;
170
171 scscmv (isym, N_columns, N_columns, (*val), (*bindx), pntr, (*xexact), (*b));
172
173 for (i = 0; i <= *N_global; i++) pntr[i]++;
174 for (i = 0; i <= n_entries; i++) (*bindx)[i]++;
175 }
176
177 /* Compute residual using CSC format */
178
179 for (i = 0; i <= *N_global; i++) pntr[i]--;
180 for (i = 0; i <= n_entries; i++) (*bindx)[i]--;
181 res = scscres(isym, *N_global, *N_global, (*val), (*bindx), pntr,
182 (*xexact), (*b));
183 printf(
184 "The residual using CSC format and exact solution is %12.4g\n",
185 res);
186 for (i = 0; i <= *N_global; i++) pntr[i]++;
187 for (i = 0; i <= n_entries; i++) (*bindx)[i]++;
188
189
190 /* Set initial guess to zero */
191
192 *x = (double *) calloc((*N_global)*MAXBLOCKSIZE,sizeof(double)) ;
193
194 if ((*x) == NULL)
195 perror("Error: Not enough space to create guess");
196
197
198 /* Set RHS to a random vector, initial guess to zero */
199 for (i=0;i<*N_global;i++) (*x)[i] = 0.0;
200
201
202 /* Allocate temporary space */
203
204 pntr1 = (int *) calloc(N_columns+1,sizeof(int)) ;
205 indx1 = (int *) calloc(n_entries+N_columns+1,sizeof(int)) ;
206 val1 = (double *) calloc(n_entries+N_columns+1,sizeof(double)) ;
207
208
209 /* Convert in the following way:
210 - CSC to CSR
211 - CSR to MSR
212 */
213 csrcsc_(N_global,&ione,&ione,
214 *val,*bindx,pntr,
215 val1,indx1,pntr1);
216
217 if (Type[1] == 'S')
218 {
219 int *indu;
220 int ierr;
221 indu = indx1+n_entries; /* Use end of bindx for workspace */
222 ssrcsr_(&N_columns, val1, indx1, pntr1, &n_entries,
223 val1, indx1, pntr1, indu, &ierr);
224 if (ierr !=0 )
225 {
226 printf(" Error in converting from symmetric form\n IERR = %d\n",ierr);
227 abort();
228 }
229 }
230
231 csrmsr_(N_global,val1,indx1,pntr1,
232 *val,*bindx,
233 *val,*bindx);
234
235 /* Recompute number of nonzeros in case there were zero diagonals */
236
237 *n_nonzeros = (*bindx)[*N_global] - 2; /* Still in Fortran mode so -2 */
238
239 /* Finally, convert bindx vectors to zero base */
240
241 for (i=0;i<*n_nonzeros+1;i++) (*bindx)[i] -= 1;
242
243
244 printf("The residual using MSR format and exact solution is %12.4g\n",
245 smsrres (*N_global, *N_global, (*val), (*bindx),
246 (*xexact), (*xexact), (*b)));
247
248 /* Release unneeded space */
249
250 free((void *) val1);
251 free((void *) indx1);
252 free((void *) pntr1);
253 free((void *) pntr);
254 }
255
256 /* end read_hb */
257 }