1 #ifndef lint 2 static char vcid[] = "$Id: aijfact.c,v 1.65 1996/08/08 14:42:46 bsmith Exp curfman $"; 3 #endif 4 5 #include "src/mat/impls/aij/seq/aij.h" 6 /* 7 Factorization code for AIJ format. 8 */ 9 10 int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 11 { 12 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 13 IS isicol; 14 int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 15 int *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift; 16 int *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im; 17 18 PetscValidHeaderSpecific(isrow,IS_COOKIE); 19 PetscValidHeaderSpecific(iscol,IS_COOKIE); 20 ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 21 ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 22 23 /* get new row pointers */ 24 ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 25 ainew[0] = -shift; 26 /* don't know how many column pointers are needed so estimate */ 27 jmax = (int) (f*ai[n]+(!shift)); 28 ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 29 /* fill is a linked list of nonzeros in active row */ 30 fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 31 im = fill + n + 1; 32 /* idnew is location of diagonal in factor */ 33 idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew); 34 idnew[0] = -shift; 35 36 for ( i=0; i<n; i++ ) { 37 /* first copy previous fill into linked list */ 38 nnz = nz = ai[r[i]+1] - ai[r[i]]; 39 ajtmp = aj + ai[r[i]] + shift; 40 fill[n] = n; 41 while (nz--) { 42 fm = n; 43 idx = ic[*ajtmp++ + shift]; 44 do { 45 m = fm; 46 fm = fill[m]; 47 } while (fm < idx); 48 fill[m] = idx; 49 fill[idx] = fm; 50 } 51 row = fill[n]; 52 while ( row < i ) { 53 ajtmp = ajnew + idnew[row] + (!shift); 54 nzbd = 1 + idnew[row] - ainew[row]; 55 nz = im[row] - nzbd; 56 fm = row; 57 while (nz-- > 0) { 58 idx = *ajtmp++ + shift; 59 nzbd++; 60 if (idx == i) im[row] = nzbd; 61 do { 62 m = fm; 63 fm = fill[m]; 64 } while (fm < idx); 65 if (fm != idx) { 66 fill[m] = idx; 67 fill[idx] = fm; 68 fm = idx; 69 nnz++; 70 } 71 } 72 row = fill[row]; 73 } 74 /* copy new filled row into permanent storage */ 75 ainew[i+1] = ainew[i] + nnz; 76 if (ainew[i+1] > jmax) { 77 /* allocate a longer ajnew */ 78 int maxadd; 79 maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); 80 if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 81 jmax += maxadd; 82 ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp); 83 PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int)); 84 PetscFree(ajnew); 85 ajnew = ajtmp; 86 realloc++; /* count how many times we realloc */ 87 } 88 ajtmp = ajnew + ainew[i] + shift; 89 fm = fill[n]; 90 nzi = 0; 91 im[i] = nnz; 92 while (nnz--) { 93 if (fm < i) nzi++; 94 *ajtmp++ = fm - shift; 95 fm = fill[fm]; 96 } 97 idnew[i] = ainew[i] + nzi; 98 } 99 100 PLogInfo(A, 101 "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 102 realloc,f,((double)ainew[n])/((double)ai[i])); 103 104 ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 105 ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 106 107 PetscFree(fill); 108 109 /* put together the new matrix */ 110 ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr); 111 PLogObjectParent(*B,isicol); 112 ierr = ISDestroy(isicol); CHKERRQ(ierr); 113 b = (Mat_SeqAIJ *) (*B)->data; 114 PetscFree(b->imax); 115 b->singlemalloc = 0; 116 len = (ainew[n] + shift)*sizeof(Scalar); 117 /* the next line frees the default space generated by the Create() */ 118 PetscFree(b->a); PetscFree(b->ilen); 119 b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 120 b->j = ajnew; 121 b->i = ainew; 122 b->diag = idnew; 123 b->ilen = 0; 124 b->imax = 0; 125 b->row = isrow; 126 b->col = iscol; 127 b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar)); 128 CHKPTRQ(b->solve_work); 129 /* In b structure: Free imax, ilen, old a, old j. 130 Allocate idnew, solve_work, new a, new j */ 131 PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 132 b->maxnz = b->nz = ainew[n] + shift; 133 134 (*B)->info.factor_mallocs = realloc; 135 (*B)->info.fill_ratio_given = f; 136 (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]); 137 138 return 0; 139 } 140 /* ----------------------------------------------------------- */ 141 int Mat_AIJ_CheckInode(Mat); 142 143 int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 144 { 145 Mat C = *B; 146 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 147 IS iscol = b->col, isrow = b->row, isicol; 148 int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 149 int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 150 int *diag_offset = b->diag,diag,k; 151 int preserve_row_sums = (int) a->ilu_preserve_row_sums; 152 Scalar *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0; 153 double ssum; 154 /* These declarations are for optimizations. They reduce the number of 155 memory references that are made by locally storing information; the 156 word "register" used here with pointers can be viewed as "private" or 157 "known only to me" 158 */ 159 register Scalar *pv, *rtmps,*u_values; 160 register int *pj; 161 162 ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 163 PLogObjectParent(*B,isicol); 164 ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 165 ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 166 rtmp = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 167 PetscMemzero(rtmp,(n+1)*sizeof(Scalar)); 168 rtmps = rtmp + shift; ics = ic + shift; 169 170 /* precalcuate row sums */ 171 if (preserve_row_sums) { 172 rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums); 173 for ( i=0; i<n; i++ ) { 174 nz = a->i[r[i]+1] - a->i[r[i]]; 175 v = a->a + a->i[r[i]] + shift; 176 sum = 0.0; 177 for ( j=0; j<nz; j++ ) sum += v[j]; 178 rowsums[i] = sum; 179 } 180 } 181 182 for ( i=0; i<n; i++ ) { 183 nz = ai[i+1] - ai[i]; 184 ajtmp = aj + ai[i] + shift; 185 for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 186 187 /* load in initial (unfactored row) */ 188 nz = a->i[r[i]+1] - a->i[r[i]]; 189 ajtmpold = a->j + a->i[r[i]] + shift; 190 v = a->a + a->i[r[i]] + shift; 191 for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 192 193 row = *ajtmp++ + shift; 194 while (row < i) { 195 pc = rtmp + row; 196 if (*pc != 0.0) { 197 pv = b->a + diag_offset[row] + shift; 198 pj = b->j + diag_offset[row] + (!shift); 199 multiplier = *pc / *pv++; 200 *pc = multiplier; 201 nz = ai[row+1] - diag_offset[row] - 1; 202 for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 203 PLogFlops(2*nz); 204 } 205 row = *ajtmp++ + shift; 206 } 207 /* finished row so stick it into b->a */ 208 pv = b->a + ai[i] + shift; 209 pj = b->j + ai[i] + shift; 210 nz = ai[i+1] - ai[i]; 211 for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 212 diag = diag_offset[i] - ai[i]; 213 /* 214 Possibly adjust diagonal entry on current row to force 215 LU matrix to have same row sum as initial matrix. 216 */ 217 if (preserve_row_sums) { 218 pj = b->j + ai[i] + shift; 219 sum = rowsums[i]; 220 for ( j=0; j<diag; j++ ) { 221 u_values = b->a + diag_offset[pj[j]] + shift; 222 nz = ai[pj[j]+1] - diag_offset[pj[j]]; 223 inner_sum = 0.0; 224 for ( k=0; k<nz; k++ ) { 225 inner_sum += u_values[k]; 226 } 227 sum -= pv[j]*inner_sum; 228 229 } 230 nz = ai[i+1] - diag_offset[i] - 1; 231 u_values = b->a + diag_offset[i] + 1 + shift; 232 for ( k=0; k<nz; k++ ) { 233 sum -= u_values[k]; 234 } 235 ssum = PetscAbsScalar(sum/pv[diag]); 236 if (ssum < 1000. && ssum > .001) pv[diag] = sum; 237 } 238 /* check pivot entry for current row */ 239 if (pv[diag] == 0.0) { 240 SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot"); 241 } 242 } 243 244 /* invert diagonal entries for simplier triangular solves */ 245 for ( i=0; i<n; i++ ) { 246 b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 247 } 248 249 if (preserve_row_sums) PetscFree(rowsums); 250 PetscFree(rtmp); 251 ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 252 ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 253 ierr = ISDestroy(isicol); CHKERRQ(ierr); 254 C->factor = FACTOR_LU; 255 ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 256 C->assembled = PETSC_TRUE; 257 PLogFlops(b->n); 258 return 0; 259 } 260 /* ----------------------------------------------------------- */ 261 int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 262 { 263 Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 264 int ierr; 265 Mat C; 266 267 PetscValidHeaderSpecific(row,IS_COOKIE); 268 PetscValidHeaderSpecific(col,IS_COOKIE); 269 ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 270 ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 271 272 /* free all the data structures from mat */ 273 PetscFree(mat->a); 274 if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 275 if (mat->diag) PetscFree(mat->diag); 276 if (mat->ilen) PetscFree(mat->ilen); 277 if (mat->imax) PetscFree(mat->imax); 278 if (mat->solve_work) PetscFree(mat->solve_work); 279 if (mat->inode.size) PetscFree(mat->inode.size); 280 PetscFree(mat); 281 282 PetscMemcpy(A,C,sizeof(struct _Mat)); 283 PetscHeaderDestroy(C); 284 return 0; 285 } 286 /* ----------------------------------------------------------- */ 287 int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 288 { 289 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 290 IS iscol = a->col, isrow = a->row; 291 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 292 int nz,shift = a->indexshift; 293 Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 294 295 if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 296 297 ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 298 ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 299 tmp = a->solve_work; 300 301 ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 302 ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 303 304 /* forward solve the lower triangular */ 305 tmp[0] = b[*r++]; 306 tmps = tmp + shift; 307 for ( i=1; i<n; i++ ) { 308 v = aa + ai[i] + shift; 309 vi = aj + ai[i] + shift; 310 nz = a->diag[i] - ai[i]; 311 sum = b[*r++]; 312 while (nz--) sum -= *v++ * tmps[*vi++]; 313 tmp[i] = sum; 314 } 315 316 /* backward solve the upper triangular */ 317 for ( i=n-1; i>=0; i-- ){ 318 v = aa + a->diag[i] + (!shift); 319 vi = aj + a->diag[i] + (!shift); 320 nz = ai[i+1] - a->diag[i] - 1; 321 sum = tmp[i]; 322 while (nz--) sum -= *v++ * tmps[*vi++]; 323 x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 324 } 325 326 ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 327 ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 328 PLogFlops(2*a->nz - a->n); 329 return 0; 330 } 331 int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 332 { 333 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 334 IS iscol = a->col, isrow = a->row; 335 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 336 int nz, shift = a->indexshift; 337 Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 338 339 if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 340 if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 341 342 ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 343 ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 344 tmp = a->solve_work; 345 346 ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 347 ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 348 349 /* forward solve the lower triangular */ 350 tmp[0] = b[*r++]; 351 for ( i=1; i<n; i++ ) { 352 v = aa + ai[i] + shift; 353 vi = aj + ai[i] + shift; 354 nz = a->diag[i] - ai[i]; 355 sum = b[*r++]; 356 while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 357 tmp[i] = sum; 358 } 359 360 /* backward solve the upper triangular */ 361 for ( i=n-1; i>=0; i-- ){ 362 v = aa + a->diag[i] + (!shift); 363 vi = aj + a->diag[i] + (!shift); 364 nz = ai[i+1] - a->diag[i] - 1; 365 sum = tmp[i]; 366 while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 367 tmp[i] = sum*aa[a->diag[i]+shift]; 368 x[*c--] += tmp[i]; 369 } 370 371 ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 372 ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 373 PLogFlops(2*a->nz); 374 375 return 0; 376 } 377 /* -------------------------------------------------------------------*/ 378 int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 379 { 380 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 381 IS iscol = a->col, isrow = a->row, invisrow,inviscol; 382 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 383 int nz,shift = a->indexshift; 384 Scalar *x,*b,*tmp, *aa = a->a, *v; 385 386 if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 387 ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 388 ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 389 tmp = a->solve_work; 390 391 /* invert the permutations */ 392 ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 393 ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 394 395 ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 396 ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 397 398 /* copy the b into temp work space according to permutation */ 399 for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 400 401 /* forward solve the U^T */ 402 for ( i=0; i<n; i++ ) { 403 v = aa + a->diag[i] + shift; 404 vi = aj + a->diag[i] + (!shift); 405 nz = ai[i+1] - a->diag[i] - 1; 406 tmp[i] *= *v++; 407 while (nz--) { 408 tmp[*vi++ + shift] -= (*v++)*tmp[i]; 409 } 410 } 411 412 /* backward solve the L^T */ 413 for ( i=n-1; i>=0; i-- ){ 414 v = aa + a->diag[i] - 1 + shift; 415 vi = aj + a->diag[i] - 1 + shift; 416 nz = a->diag[i] - ai[i]; 417 while (nz--) { 418 tmp[*vi-- + shift] -= (*v--)*tmp[i]; 419 } 420 } 421 422 /* copy tmp into x according to permutation */ 423 for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 424 425 ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 426 ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 427 ierr = ISDestroy(invisrow); CHKERRQ(ierr); 428 ierr = ISDestroy(inviscol); CHKERRQ(ierr); 429 430 PLogFlops(2*a->nz-a->n); 431 return 0; 432 } 433 434 int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 435 { 436 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 437 IS iscol = a->col, isrow = a->row, invisrow,inviscol; 438 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 439 int nz,shift = a->indexshift; 440 Scalar *x,*b,*tmp, *aa = a->a, *v; 441 442 if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 443 if (zz != xx) VecCopy(zz,xx); 444 445 ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 446 ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 447 tmp = a->solve_work; 448 449 /* invert the permutations */ 450 ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 451 ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 452 ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 453 ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 454 455 /* copy the b into temp work space according to permutation */ 456 for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 457 458 /* forward solve the U^T */ 459 for ( i=0; i<n; i++ ) { 460 v = aa + a->diag[i] + shift; 461 vi = aj + a->diag[i] + (!shift); 462 nz = ai[i+1] - a->diag[i] - 1; 463 tmp[i] *= *v++; 464 while (nz--) { 465 tmp[*vi++ + shift] -= (*v++)*tmp[i]; 466 } 467 } 468 469 /* backward solve the L^T */ 470 for ( i=n-1; i>=0; i-- ){ 471 v = aa + a->diag[i] - 1 + shift; 472 vi = aj + a->diag[i] - 1 + shift; 473 nz = a->diag[i] - ai[i]; 474 while (nz--) { 475 tmp[*vi-- + shift] -= (*v--)*tmp[i]; 476 } 477 } 478 479 /* copy tmp into x according to permutation */ 480 for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 481 482 ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 483 ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 484 ierr = ISDestroy(invisrow); CHKERRQ(ierr); 485 ierr = ISDestroy(inviscol); CHKERRQ(ierr); 486 487 PLogFlops(2*a->nz); 488 return 0; 489 } 490 /* ----------------------------------------------------------------*/ 491 492 int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 493 { 494 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 495 IS isicol; 496 int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 497 int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 498 int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 499 int incrlev,nnz,i,shift = a->indexshift; 500 PetscTruth col_identity, row_identity; 501 502 /* special case that simply copies fill pattern */ 503 ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity); 504 if (levels == 0 && row_identity && col_identity) { 505 ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 506 (*fact)->factor = FACTOR_LU; 507 b = (Mat_SeqAIJ *) (*fact)->data; 508 if (!b->diag) { 509 ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 510 } 511 b->row = isrow; 512 b->col = iscol; 513 b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 514 return 0; 515 } 516 517 ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 518 ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 519 ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 520 521 /* get new row pointers */ 522 ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 523 ainew[0] = -shift; 524 /* don't know how many column pointers are needed so estimate */ 525 jmax = (int) (f*(ai[n]+!shift)); 526 ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 527 /* ajfill is level of fill for each fill entry */ 528 ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 529 /* fill is a linked list of nonzeros in active row */ 530 fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 531 /* im is level for each filled value */ 532 im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 533 /* dloc is location of diagonal in factor */ 534 dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 535 dloc[0] = 0; 536 for ( prow=0; prow<n; prow++ ) { 537 /* first copy previous fill into linked list */ 538 nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 539 xi = aj + ai[r[prow]] + shift; 540 fill[n] = n; 541 while (nz--) { 542 fm = n; 543 idx = ic[*xi++ + shift]; 544 do { 545 m = fm; 546 fm = fill[m]; 547 } while (fm < idx); 548 fill[m] = idx; 549 fill[idx] = fm; 550 im[idx] = 0; 551 } 552 nzi = 0; 553 row = fill[n]; 554 while ( row < prow ) { 555 incrlev = im[row] + 1; 556 nz = dloc[row]; 557 xi = ajnew + ainew[row] + shift + nz; 558 flev = ajfill + ainew[row] + shift + nz + 1; 559 nnz = ainew[row+1] - ainew[row] - nz - 1; 560 if (*xi++ + shift != row) { 561 SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 562 } 563 fm = row; 564 while (nnz-- > 0) { 565 idx = *xi++ + shift; 566 if (*flev + incrlev > levels) { 567 flev++; 568 continue; 569 } 570 do { 571 m = fm; 572 fm = fill[m]; 573 } while (fm < idx); 574 if (fm != idx) { 575 im[idx] = *flev + incrlev; 576 fill[m] = idx; 577 fill[idx] = fm; 578 fm = idx; 579 nzf++; 580 } 581 else { 582 if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 583 } 584 flev++; 585 } 586 row = fill[row]; 587 nzi++; 588 } 589 /* copy new filled row into permanent storage */ 590 ainew[prow+1] = ainew[prow] + nzf; 591 if (ainew[prow+1] > jmax-shift) { 592 /* allocate a longer ajnew */ 593 int maxadd; 594 maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 595 if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 596 jmax += maxadd; 597 xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 598 PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 599 PetscFree(ajnew); 600 ajnew = xi; 601 /* allocate a longer ajfill */ 602 xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 603 PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 604 PetscFree(ajfill); 605 ajfill = xi; 606 realloc++; 607 } 608 xi = ajnew + ainew[prow] + shift; 609 flev = ajfill + ainew[prow] + shift; 610 dloc[prow] = nzi; 611 fm = fill[n]; 612 while (nzf--) { 613 *xi++ = fm - shift; 614 *flev++ = im[fm]; 615 fm = fill[fm]; 616 } 617 } 618 PetscFree(ajfill); 619 ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 620 ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 621 ierr = ISDestroy(isicol); CHKERRQ(ierr); 622 PetscFree(fill); PetscFree(im); 623 624 PLogInfo(A, 625 "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 626 realloc,f,((double)ainew[n])/((double)ai[prow])); 627 628 /* put together the new matrix */ 629 ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr); 630 b = (Mat_SeqAIJ *) (*fact)->data; 631 PetscFree(b->imax); 632 b->singlemalloc = 0; 633 len = (ainew[n] + shift)*sizeof(Scalar); 634 /* the next line frees the default space generated by the Create() */ 635 PetscFree(b->a); PetscFree(b->ilen); 636 b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 637 b->j = ajnew; 638 b->i = ainew; 639 for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 640 b->diag = dloc; 641 b->ilen = 0; 642 b->imax = 0; 643 b->row = isrow; 644 b->col = iscol; 645 b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 646 CHKPTRQ(b->solve_work); 647 /* In b structure: Free imax, ilen, old a, old j. 648 Allocate dloc, solve_work, new a, new j */ 649 PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 650 b->maxnz = b->nz = ainew[n] + shift; 651 (*fact)->factor = FACTOR_LU; 652 653 (*fact)->info.factor_mallocs = realloc; 654 (*fact)->info.fill_ratio_given = f; 655 (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]); 656 657 return 0; 658 } 659 660 661 662 663