Debian Codegraph @

packages/aztecoo/example/azoo_iterate_hb/distrib_msr_matrix.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 "prototypes.h"
48 /*#define DEBUG */
49 void distrib_msr_matrix(int *proc_config, int N_global,
50 int *n_nonzeros, int *N_update, int **update,
51 double **val, int **bindx,
52 double **x, double **b, double **xexact)
53
54 {
55 int i, n_entries, N_columns, n_global_nonzeros;
56 int ii, j, row, have_xexact = 0 ;
57 int kk = 0;
58 int max_ii = 0, max_jj = 0;
59 int ione = 1;
60 double value;
61 double *cnt;
62 int *pntr, *bindx1, *pntr1;
63 double *val1, *b1, *x1, *xexact1;
64 #ifdef DEBUG
65 printf("Processor %d of %d entering distrib_matrix.\n",
66 proc_config[AZ_node],proc_config[AZ_N_procs]) ;
67 #endif
68
69 /*************** Distribute global matrix to all processors ************/
70
71 if(proc_config[AZ_node] == 0)
72 {
73 if ((*xexact) != NULL) have_xexact = 1;
74 #ifdef DEBUG
75 printf("Broadcasting exact solution\n");
76 #endif
77 }
78
79 if(proc_config[AZ_N_procs] > 1)
80 {
81
82 AZ_broadcast((char *) &N_global, sizeof(int), proc_config, AZ_PACK);
83 AZ_broadcast((char *) n_nonzeros, sizeof(int), proc_config, AZ_PACK);
84 AZ_broadcast((char *) &have_xexact, sizeof(int), proc_config, AZ_PACK);
85 AZ_broadcast(NULL, 0, proc_config, AZ_SEND);
86
87 if(proc_config[AZ_node] != 0)
88 {
89 (*bindx) = (int *) calloc(*n_nonzeros+1,sizeof(int)) ;
90 (*val) = (double *) calloc(*n_nonzeros+1,sizeof(double)) ;
91 }
92
93 AZ_broadcast((char *) (*bindx), sizeof(int) *(*n_nonzeros+1),
94 proc_config, AZ_PACK);
95 AZ_broadcast(NULL, 0, proc_config, AZ_SEND);
96 AZ_broadcast((char *) (*val), sizeof(double)*(*n_nonzeros+1),
97 proc_config, AZ_PACK);
98 AZ_broadcast(NULL, 0, proc_config, AZ_SEND);
99
100 #ifdef DEBUG
101 printf("Processor %d of %d done with matrix broadcast.\n",
102 proc_config[AZ_node],proc_config[AZ_N_procs]) ;
103 #endif
104
105 /* Set rhs and initialize guess */
106 if(proc_config[AZ_node] != 0)
107 {
108 (*b) = (double *) calloc(N_global,sizeof(double)) ;
109 (*x) = (double *) calloc(N_global,sizeof(double)) ;
110 if (have_xexact)
111 (*xexact) = (double *) calloc(N_global,sizeof(double)) ;
112 }
113
114 AZ_broadcast((char *) (*x), sizeof(double)*(N_global), proc_config, AZ_PACK);
115 AZ_broadcast((char *) (*b), sizeof(double)*(N_global), proc_config, AZ_PACK);
116 if (have_xexact)
117 AZ_broadcast((char *)
118 (*xexact), sizeof(double)*(N_global), proc_config, AZ_PACK);
119 AZ_broadcast(NULL, 0, proc_config, AZ_SEND);
120 #ifdef DEBUG
121 printf("Processor %d of %d done with rhs/guess broadcast.\n",
122 proc_config[AZ_node],proc_config[AZ_N_procs]) ;
123 #endif
124
125 }
126
127 /********************** Generate update map *************************/
128
129 AZ_read_update(N_update, update, proc_config, N_global,
130 1, AZ_linear) ;
131
132 printf("Processor %d of %d has %d rows of %d total rows.\n",
133 proc_config[AZ_node],proc_config[AZ_N_procs],*N_update,N_global) ;
134
135 /*************** Construct local matrix from global matrix ************/
136
137 /* The local matrix is a copy of the rows assigned to this processor.
138 It is stored in MSR format and still has global indices (AZ_transform
139 will complete conversion to local indices.
140 */
141
142 if(proc_config[AZ_N_procs] > 1)
143 {
144 n_global_nonzeros = *n_nonzeros;
145
146 *n_nonzeros = *N_update;
147
148 for (i=0; i<*N_update; i++)
149 *n_nonzeros += (*bindx)[(*update)[i]+1] - (*bindx)[(*update)[i]];
150
151 printf("Processor %d of %d has %d nonzeros of %d total nonzeros.\n",
152 proc_config[AZ_node],proc_config[AZ_N_procs],
153 *n_nonzeros,n_global_nonzeros) ;
154
155 #ifdef DEBUG
156 { double sum1 = 0.0;
157 for (i=0;i<N_global; i++) sum1 += (*b)[i];
158
159 printf("Processor %d of %d has sum of b = %12.4g.\n",
160 proc_config[AZ_node],proc_config[AZ_N_procs],sum1) ;
161 }
162 #endif /* DEBUG */
163
164 /* Allocate memory for local matrix */
165
166 bindx1 = (int *) calloc(*n_nonzeros+1,sizeof(int)) ;
167 val1 = (double *) calloc(*n_nonzeros+1,sizeof(double)) ;
168 b1 = (double *) calloc(*N_update,sizeof(double)) ;
169 x1 = (double *) calloc(*N_update,sizeof(double)) ;
170 if (have_xexact)
171 xexact1 = (double *) calloc(*N_update,sizeof(double)) ;
172
173 bindx1[0] = *N_update+1;
174
175 for (i=0; i<*N_update; i++)
176 {
177 row = (*update)[i];
178 b1[i] = (*b)[row];
179 x1[i] = (*x)[row];
180 if (have_xexact) xexact1[i] = (*xexact)[row];
181 val1[i] = (*val)[row];
182 bindx1[i+1] = bindx1[i];
183
184 #ifdef DEBUG
185 printf("Proc %d of %d: Global row = %d: Local row = %d:
186 b = %12.4g: x = %12.4g: bindx = %d: val = %12.4g \n",
187 proc_config[AZ_node],proc_config[AZ_N_procs],
188 row, i, b1[i], x1[i], bindx1[i], val1[i]) ;
189 #endif
190
191 for (j = (*bindx)[row]; j < (*bindx)[row+1]; j++)
192 {
193 val1[ bindx1 [i+1] ] = (*val)[j];
194 bindx1[bindx1 [i+1] ] = (*bindx)[j];
195 bindx1[i+1] ++;
196 }
197 }
198
199 #ifdef DEBUG
200 printf("Processor %d of %d done with extracting local operators.\n",
201 proc_config[AZ_node],proc_config[AZ_N_procs]) ;
202
203 if (have_xexact)
204 {
205 printf(
206 "The residual using MSR format and exact solution on processor %d is %12.4g\n",
207 proc_config[AZ_node],
208 smsrres (*N_update, N_global, val1, bindx1, xexact1, (*xexact), b1));
209 }
210
211 #endif
212 /* Release memory for global matrix, rhs and solution */
213
214 free ((void *) (*val));
215 free ((void *) (*bindx));
216 free ((void *) (*b));
217 free ((void *) (*x));
218 if (have_xexact) free((void *) *xexact);
219
220 /* Return local matrix through same pointers. */
221
222 *val = val1;
223 *bindx = bindx1;
224 *b = b1;
225 *x = x1;
226 if (have_xexact) *xexact = xexact1;
227
228 }
229 #ifdef DEBUG
230 if (have_xexact && proc_config[AZ_N_procs] == 1)
231 {
232 printf(
233 "The residual using MSR format and exact solution on processor %d is %12.4g\n",
234 proc_config[AZ_node],
235 smsrres (*N_update, N_global, (*val), (*bindx),
236 (*xexact), (*xexact), (*b)));
237 }
238
239
240 printf("Processor %d of %d leaving distrib_matrix.\n",
241 proc_config[AZ_node],proc_config[AZ_N_procs]) ;
242 #endif
243 /* end distrib_matrix */
244 }