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