| 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_vbr_matrix(int *proc_config, |
| 50 |
int N_global, int N_blk_global, |
| 51 |
int *n_nonzeros, int *n_blk_nonzeros, |
| 52 |
int *N_update, int **update, |
| 53 |
double **val, int **indx, int **rpntr, int **cpntr, |
| 54 |
int **bpntr, int **bindx, |
| 55 |
double **x, double **b, double **xexact) |
| 56 |
|
| 57 |
{ |
| 58 |
int i, n_entries, N_columns, n_global_nonzeros, n_global_blk_nonzeros; |
| 59 |
int N_local; |
| 60 |
int ii, j, row, have_xexact = 0 ; |
| 61 |
int kk = 0; |
| 62 |
int max_ii = 0, max_jj = 0; |
| 63 |
int ione = 1; |
| 64 |
double value; |
| 65 |
double *cnt; |
| 66 |
int *rpntr1, *bindx1, *bpntr1, *indx1; |
| 67 |
double *val1, *b1, *x1, *xexact1; |
| 68 |
#ifdef DEBUG |
| 69 |
printf("Processor %d of %d entering distrib_matrix.\n", |
| 70 |
proc_config[AZ_node],proc_config[AZ_N_procs]) ; |
| 71 |
#endif |
| 72 |
|
| 73 |
/*************** Distribute global matrix to all processors ************/ |
| 74 |
|
| 75 |
if(proc_config[AZ_node] == 0) |
| 76 |
{ |
| 77 |
if ((*xexact) != NULL) have_xexact = 1; |
| 78 |
#ifdef DEBUG |
| 79 |
printf("Broadcasting exact solution\n"); |
| 80 |
#endif |
| 81 |
} |
| 82 |
|
| 83 |
if(proc_config[AZ_N_procs] > 1) |
| 84 |
{ |
| 85 |
|
| 86 |
AZ_broadcast((char *) &N_global, sizeof(int), proc_config, AZ_PACK); |
| 87 |
AZ_broadcast((char *) &N_blk_global, sizeof(int), proc_config, AZ_PACK); |
| 88 |
AZ_broadcast((char *) n_nonzeros, sizeof(int), proc_config, AZ_PACK); |
| 89 |
AZ_broadcast((char *) n_blk_nonzeros, sizeof(int), proc_config, AZ_PACK); |
| 90 |
AZ_broadcast((char *) &have_xexact, sizeof(int), proc_config, AZ_PACK); |
| 91 |
AZ_broadcast(NULL, 0, proc_config, AZ_SEND); |
| 92 |
|
| 93 |
#ifdef DEBUG |
| 94 |
printf("Processor %d of %d done with global parameter broadcast.\n", |
| 95 |
proc_config[AZ_node],proc_config[AZ_N_procs]) ; |
| 96 |
#endif |
| 97 |
|
| 98 |
if(proc_config[AZ_node] != 0) |
| 99 |
{ |
| 100 |
*bpntr = (int *) calloc(N_blk_global+1,sizeof(int)) ; |
| 101 |
*rpntr = (int *) calloc(N_blk_global+1,sizeof(int)) ; |
| 102 |
*bindx = (int *) calloc(*n_blk_nonzeros+1,sizeof(int)) ; |
| 103 |
*indx = (int *) calloc(*n_blk_nonzeros+1,sizeof(int)) ; |
| 104 |
*val = (double *) calloc(*n_nonzeros+1,sizeof(double)) ; |
| 105 |
|
| 106 |
#ifdef DEBUG |
| 107 |
printf("Processor %d of %d done with global calloc.\n", |
| 108 |
proc_config[AZ_node],proc_config[AZ_N_procs]) ; |
| 109 |
#endif |
| 110 |
} |
| 111 |
|
| 112 |
AZ_broadcast((char *) (*bpntr), sizeof(int) *(N_blk_global+1), |
| 113 |
proc_config, AZ_PACK); |
| 114 |
AZ_broadcast((char *) (*rpntr), sizeof(int) *(N_blk_global+1), |
| 115 |
proc_config, AZ_PACK); |
| 116 |
AZ_broadcast((char *) (*bindx), sizeof(int) *(*n_blk_nonzeros+1), |
| 117 |
proc_config, AZ_PACK); |
| 118 |
AZ_broadcast((char *) (*indx), sizeof(int) *(*n_blk_nonzeros+1), |
| 119 |
proc_config, AZ_PACK); |
| 120 |
AZ_broadcast(NULL, 0, proc_config, AZ_SEND); |
| 121 |
AZ_broadcast((char *) (*val), sizeof(double)*(*n_nonzeros+1), |
| 122 |
proc_config, AZ_PACK); |
| 123 |
AZ_broadcast(NULL, 0, proc_config, AZ_SEND); |
| 124 |
|
| 125 |
#ifdef DEBUG |
| 126 |
printf("Processor %d of %d done with matrix broadcast.\n", |
| 127 |
proc_config[AZ_node],proc_config[AZ_N_procs]) ; |
| 128 |
#endif |
| 129 |
|
| 130 |
/* Set rhs and initialize guess */ |
| 131 |
if(proc_config[AZ_node] != 0) |
| 132 |
{ |
| 133 |
(*b) = (double *) calloc(N_global,sizeof(double)) ; |
| 134 |
(*x) = (double *) calloc(N_global,sizeof(double)) ; |
| 135 |
if (have_xexact) |
| 136 |
(*xexact) = (double *) calloc(N_global,sizeof(double)) ; |
| 137 |
} |
| 138 |
|
| 139 |
AZ_broadcast((char *) (*x), sizeof(double)*(N_global), proc_config, AZ_PACK); |
| 140 |
AZ_broadcast((char *) (*b), sizeof(double)*(N_global), proc_config, AZ_PACK); |
| 141 |
if (have_xexact) |
| 142 |
AZ_broadcast((char *) |
| 143 |
(*xexact), sizeof(double)*(N_global), proc_config, AZ_PACK); |
| 144 |
AZ_broadcast(NULL, 0, proc_config, AZ_SEND); |
| 145 |
|
| 146 |
#ifdef DEBUG |
| 147 |
printf("Processor %d of %d done with rhs/guess broadcast.\n", |
| 148 |
proc_config[AZ_node],proc_config[AZ_N_procs]) ; |
| 149 |
#endif |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/********************** Generate update map *************************/ |
| 154 |
|
| 155 |
AZ_read_update(N_update, update, proc_config, N_blk_global, |
| 156 |
1, AZ_linear) ; |
| 157 |
|
| 158 |
printf("Processor %d of %d has %d rows of %d total block rows.\n", |
| 159 |
proc_config[AZ_node],proc_config[AZ_N_procs],*N_update,N_blk_global) ; |
| 160 |
|
| 161 |
/*************** Construct local matrix from global matrix ************/ |
| 162 |
|
| 163 |
/* The local matrix is a copy of the rows assigned to this processor. |
| 164 |
It is stored in MSR format and still has global indices (AZ_transform |
| 165 |
will complete conversion to local indices. |
| 166 |
*/ |
| 167 |
|
| 168 |
if(proc_config[AZ_N_procs] > 1) |
| 169 |
{ |
| 170 |
n_global_nonzeros = *n_nonzeros; |
| 171 |
n_global_blk_nonzeros = *n_blk_nonzeros; |
| 172 |
|
| 173 |
*n_nonzeros = 0; |
| 174 |
*n_blk_nonzeros = 0; |
| 175 |
N_local = 0; |
| 176 |
|
| 177 |
for (i=0; i<*N_update; i++) |
| 178 |
{ |
| 179 |
row = (*update)[i]; |
| 180 |
*n_nonzeros += (*indx)[(*bpntr)[row+1]] - (*indx)[(*bpntr)[row]]; |
| 181 |
*n_blk_nonzeros += (*bpntr)[row+1] - (*bpntr)[row]; |
| 182 |
N_local += (*rpntr)[row+1] - (*rpntr)[row]; |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
printf("Processor %d of %d has %d nonzeros of %d total nonzeros.\n", |
| 187 |
proc_config[AZ_node],proc_config[AZ_N_procs], |
| 188 |
*n_nonzeros,n_global_nonzeros) ; |
| 189 |
|
| 190 |
printf("Processor %d of %d has %d block nonzeros of %d total block nonzeros.\n", |
| 191 |
proc_config[AZ_node],proc_config[AZ_N_procs], |
| 192 |
*n_blk_nonzeros,n_global_blk_nonzeros) ; |
| 193 |
|
| 194 |
printf("Processor %d of %d has %d equations of %d total equations.\n", |
| 195 |
proc_config[AZ_node],proc_config[AZ_N_procs], |
| 196 |
N_local,N_global) ; |
| 197 |
|
| 198 |
#ifdef DEBUG |
| 199 |
{ double sum1 = 0.0; |
| 200 |
for (i=0;i<N_global; i++) sum1 += (*b)[i]; |
| 201 |
|
| 202 |
printf("Processor %d of %d has sum of b = %12.4g.\n", |
| 203 |
proc_config[AZ_node],proc_config[AZ_N_procs],sum1) ; |
| 204 |
} |
| 205 |
#endif /* DEBUG */ |
| 206 |
|
| 207 |
/* Allocate memory for local matrix */ |
| 208 |
|
| 209 |
bpntr1 = (int *) calloc(*N_update+1,sizeof(int)) ; |
| 210 |
rpntr1 = (int *) calloc(*N_update+1,sizeof(int)) ; |
| 211 |
bindx1 = (int *) calloc(*n_blk_nonzeros+1,sizeof(int)) ; |
| 212 |
indx1 = (int *) calloc(*n_blk_nonzeros+1,sizeof(int)) ; |
| 213 |
val1 = (double *) calloc(*n_nonzeros+1,sizeof(double)) ; |
| 214 |
b1 = (double *) calloc(N_local,sizeof(double)) ; |
| 215 |
x1 = (double *) calloc(N_local,sizeof(double)) ; |
| 216 |
if (have_xexact) |
| 217 |
xexact1 = (double *) calloc(N_local,sizeof(double)) ; |
| 218 |
|
| 219 |
{ |
| 220 |
int cur_blk_size, indx_offset, len_val, row_offset, row_offset1; |
| 221 |
double *val_ptr, *val1_ptr; |
| 222 |
|
| 223 |
bpntr1[0] = 0; |
| 224 |
indx1[0] = 0; |
| 225 |
rpntr1[0] = 0; |
| 226 |
for (i=0; i<*N_update; i++) |
| 227 |
{ |
| 228 |
row = (*update)[i]; |
| 229 |
cur_blk_size = (*rpntr)[row+1] - (*rpntr)[row]; |
| 230 |
rpntr1[i+1] = rpntr1[i] + cur_blk_size; |
| 231 |
row_offset = (*rpntr)[row]; |
| 232 |
row_offset1 = rpntr1[i]; |
| 233 |
for (j = 0; j<cur_blk_size; j++) |
| 234 |
{ |
| 235 |
b1[row_offset1+j] = (*b)[row_offset+j]; |
| 236 |
x1[row_offset1+j] = (*x)[row_offset+j]; |
| 237 |
if (have_xexact) xexact1[row_offset1+j] = (*xexact)[row_offset+j]; |
| 238 |
} |
| 239 |
bpntr1[i+1] = bpntr1[i]; |
| 240 |
|
| 241 |
#ifdef DEBUG |
| 242 |
printf("Proc %d of %d: Global row = %d: Local row = %d: |
| 243 |
b = %12.4g: x = %12.4g: bindx = %d: val = %12.4g \n", |
| 244 |
proc_config[AZ_node],proc_config[AZ_N_procs], |
| 245 |
row, i, b1[i], x1[i], bindx1[i], val1[i]) ; |
| 246 |
#endif |
| 247 |
indx_offset = (*indx)[(*bpntr)[row]] - indx1[bpntr1[i]]; |
| 248 |
for (j = (*bpntr)[row]; j < (*bpntr)[row+1]; j++) |
| 249 |
{ |
| 250 |
indx1[bpntr1 [i+1] + 1] = (*indx)[j+1] - indx_offset; |
| 251 |
bindx1[bpntr1 [i+1] ] = (*bindx)[j]; |
| 252 |
bpntr1[i+1] ++; |
| 253 |
} |
| 254 |
len_val = indx1[bpntr1[i+1]] - indx1[bpntr1[i]]; |
| 255 |
val_ptr = (*val)+(*indx)[(*bpntr)[row]]; |
| 256 |
val1_ptr = val1+indx1[bpntr1[i]]; |
| 257 |
for (j = 0; j<len_val; j++) |
| 258 |
{ |
| 259 |
*val1_ptr = *val_ptr; |
| 260 |
val_ptr++; val1_ptr++; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
/* #ifdef DEBUG |
| 265 |
printf("Processor %d of %d done with extracting local operators.\n", |
| 266 |
proc_config[AZ_node],proc_config[AZ_N_procs]) ; |
| 267 |
if (have_xexact) |
| 268 |
{ |
| 269 |
printf( |
| 270 |
"The residual using VBR format and exact solution on processor %d is %12.4g\n", |
| 271 |
proc_config[AZ_node], |
| 272 |
svbrres (N_local, N_global, *N_update, val1, indx1, bindx1, |
| 273 |
rpntr1, (*rpntr), bpntr1, bpntr1+1, |
| 274 |
(*xexact), b1)); |
| 275 |
} |
| 276 |
#endif */ |
| 277 |
/* Release memory for global matrix, rhs and solution */ |
| 278 |
|
| 279 |
free ((void *) (*val)); |
| 280 |
free ((void *) (*indx)); |
| 281 |
free ((void *) (*bindx)); |
| 282 |
free ((void *) (*bpntr)); |
| 283 |
free ((void *) (*rpntr)); |
| 284 |
free ((void *) (*b)); |
| 285 |
free ((void *) (*x)); |
| 286 |
if (have_xexact) free((void *) *xexact); |
| 287 |
|
| 288 |
/* Return local matrix through same pointers. */ |
| 289 |
|
| 290 |
*val = val1; |
| 291 |
*indx = indx1; |
| 292 |
*bindx = bindx1; |
| 293 |
*bpntr = bpntr1; |
| 294 |
*rpntr = rpntr1; |
| 295 |
*b = b1; |
| 296 |
*x = x1; |
| 297 |
if (have_xexact) *xexact = xexact1; |
| 298 |
|
| 299 |
} |
| 300 |
/* #ifdef DEBUG |
| 301 |
if (have_xexact && proc_config[AZ_N_procs] == 1) |
| 302 |
{ |
| 303 |
printf( |
| 304 |
"The residual using VBR format and exact solution on processor %d is %12.4g\n", |
| 305 |
proc_config[AZ_node], |
| 306 |
svbrres (N_global, N_global, *N_update, (*val), (*indx), (*bindx), |
| 307 |
(*rpntr), (*rpntr), (*bpntr), (*bpntr)+1, |
| 308 |
(*xexact), (*b))); |
| 309 |
} |
| 310 |
|
| 311 |
printf("Processor %d of %d leaving distrib_matrix.\n", |
| 312 |
proc_config[AZ_node],proc_config[AZ_N_procs]) ; |
| 313 |
#endif */ |
| 314 |
|
| 315 |
/* end distrib_vbr_matrix */ |
| 316 |
} |