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