1 #ifdef PETSC_RCS_HEADER 2 static char vcid[] = "$Id: aijfact.c,v 1.92 1997/11/03 04:45:22 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 ierr = ISDestroy(isicol); CHKERRQ(ierr); 138 b = (Mat_SeqAIJ *) (*B)->data; 139 PetscFree(b->imax); 140 b->singlemalloc = 0; 141 /* the next line frees the default space generated by the Create() */ 142 PetscFree(b->a); PetscFree(b->ilen); 143 b->a = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a); 144 b->j = ajnew; 145 b->i = ainew; 146 b->diag = idnew; 147 b->ilen = 0; 148 b->imax = 0; 149 b->row = isrow; 150 b->col = iscol; 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 iscol = b->col, isrow = b->row, isicol; 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 ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 188 PLogObjectParent(*B,isicol); 189 ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 190 ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 191 rtmp = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 192 PetscMemzero(rtmp,(n+1)*sizeof(Scalar)); 193 rtmps = rtmp + shift; ics = ic + shift; 194 195 /* precalcuate row sums */ 196 if (preserve_row_sums) { 197 rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums); 198 for ( i=0; i<n; i++ ) { 199 nz = a->i[r[i]+1] - a->i[r[i]]; 200 v = a->a + a->i[r[i]] + shift; 201 sum = 0.0; 202 for ( j=0; j<nz; j++ ) sum += v[j]; 203 rowsums[i] = sum; 204 } 205 } 206 207 for ( i=0; i<n; i++ ) { 208 nz = ai[i+1] - ai[i]; 209 ajtmp = aj + ai[i] + shift; 210 for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 211 212 /* load in initial (unfactored row) */ 213 nz = a->i[r[i]+1] - a->i[r[i]]; 214 ajtmpold = a->j + a->i[r[i]] + shift; 215 v = a->a + a->i[r[i]] + shift; 216 for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 217 218 row = *ajtmp++ + shift; 219 while (row < i ) { 220 pc = rtmp + row; 221 if (*pc != 0.0) { 222 pv = b->a + diag_offset[row] + shift; 223 pj = b->j + diag_offset[row] + (!shift); 224 multiplier = *pc / *pv++; 225 *pc = multiplier; 226 nz = ai[row+1] - diag_offset[row] - 1; 227 for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 228 PLogFlops(2*nz); 229 } 230 row = *ajtmp++ + shift; 231 } 232 /* finished row so stick it into b->a */ 233 pv = b->a + ai[i] + shift; 234 pj = b->j + ai[i] + shift; 235 nz = ai[i+1] - ai[i]; 236 for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 237 diag = diag_offset[i] - ai[i]; 238 /* 239 Possibly adjust diagonal entry on current row to force 240 LU matrix to have same row sum as initial matrix. 241 */ 242 if (pv[diag] == 0.0) { 243 SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot"); 244 } 245 if (preserve_row_sums) { 246 pj = b->j + ai[i] + shift; 247 sum = rowsums[i]; 248 for ( j=0; j<diag; j++ ) { 249 u_values = b->a + diag_offset[pj[j]] + shift; 250 nz = ai[pj[j]+1] - diag_offset[pj[j]]; 251 inner_sum = 0.0; 252 for ( k=0; k<nz; k++ ) { 253 inner_sum += u_values[k]; 254 } 255 sum -= pv[j]*inner_sum; 256 257 } 258 nz = ai[i+1] - diag_offset[i] - 1; 259 u_values = b->a + diag_offset[i] + 1 + shift; 260 for ( k=0; k<nz; k++ ) { 261 sum -= u_values[k]; 262 } 263 ssum = PetscAbsScalar(sum/pv[diag]); 264 if (ssum < 1000. && ssum > .001) pv[diag] = sum; 265 } 266 /* check pivot entry for current row */ 267 } 268 269 /* invert diagonal entries for simplier triangular solves */ 270 for ( i=0; i<n; i++ ) { 271 b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 272 } 273 274 if (preserve_row_sums) PetscFree(rowsums); 275 PetscFree(rtmp); 276 ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 277 ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 278 ierr = ISDestroy(isicol); CHKERRQ(ierr); 279 C->factor = FACTOR_LU; 280 ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 281 C->assembled = PETSC_TRUE; 282 PLogFlops(b->n); 283 PetscFunctionReturn(0); 284 } 285 /* ----------------------------------------------------------- */ 286 #undef __FUNC__ 287 #define __FUNC__ "MatLUFactor_SeqAIJ" 288 int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 289 { 290 Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 291 int ierr; 292 Mat C; 293 294 PetscFunctionBegin; 295 ierr = MatLUFactorSymbolic(A,row,col,f,&C); CHKERRQ(ierr); 296 ierr = MatLUFactorNumeric(A,&C); CHKERRQ(ierr); 297 298 /* free all the data structures from mat */ 299 PetscFree(mat->a); 300 if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 301 if (mat->diag) PetscFree(mat->diag); 302 if (mat->ilen) PetscFree(mat->ilen); 303 if (mat->imax) PetscFree(mat->imax); 304 if (mat->solve_work) PetscFree(mat->solve_work); 305 if (mat->inode.size) PetscFree(mat->inode.size); 306 PetscFree(mat); 307 308 PetscMemcpy(A,C,sizeof(struct _p_Mat)); 309 PetscHeaderDestroy(C); 310 PetscFunctionReturn(0); 311 } 312 /* ----------------------------------------------------------- */ 313 #undef __FUNC__ 314 #define __FUNC__ "MatSolve_SeqAIJ" 315 int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 316 { 317 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 318 IS iscol = a->col, isrow = a->row; 319 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 320 int nz,shift = a->indexshift,*rout,*cout; 321 Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 322 323 PetscFunctionBegin; 324 if (!n) PetscFunctionReturn(0); 325 326 VecGetArray_Fast(bb,b); 327 VecGetArray_Fast(xx,x); 328 tmp = a->solve_work; 329 330 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 331 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 332 333 /* forward solve the lower triangular */ 334 tmp[0] = b[*r++]; 335 tmps = tmp + shift; 336 for ( i=1; i<n; i++ ) { 337 v = aa + ai[i] + shift; 338 vi = aj + ai[i] + shift; 339 nz = a->diag[i] - ai[i]; 340 sum = b[*r++]; 341 while (nz--) sum -= *v++ * tmps[*vi++]; 342 tmp[i] = sum; 343 } 344 345 /* backward solve the upper triangular */ 346 for ( i=n-1; i>=0; i-- ){ 347 v = aa + a->diag[i] + (!shift); 348 vi = aj + a->diag[i] + (!shift); 349 nz = ai[i+1] - a->diag[i] - 1; 350 sum = tmp[i]; 351 while (nz--) sum -= *v++ * tmps[*vi++]; 352 x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 353 } 354 355 ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 356 ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 357 PLogFlops(2*a->nz - a->n); 358 PetscFunctionReturn(0); 359 } 360 361 /* ----------------------------------------------------------- */ 362 #undef __FUNC__ 363 #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering" 364 int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx) 365 { 366 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 367 int i, n = a->m, *vi, *ai = a->i, *aj = a->j,nz, *adiag = a->diag; 368 int ai_i, adiag_i,ierr; 369 Scalar *x,*b, *aa = a->a, sum, *v; 370 371 PetscFunctionBegin; 372 if (!n) PetscFunctionReturn(0); 373 if (a->indexshift) { 374 ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr); 375 PetscFunctionReturn(0); 376 } 377 378 VecGetArray_Fast(bb,b); 379 VecGetArray_Fast(xx,x); 380 381 #if defined(USE_FORTRAN_KERNELS) 382 fortransolveaij_(&n,x,ai,aj,adiag,aa,b); 383 #else 384 /* forward solve the lower triangular */ 385 x[0] = b[0]; 386 for ( i=1; i<n; i++ ) { 387 ai_i = ai[i]; 388 v = aa + ai_i; 389 vi = aj + ai_i; 390 nz = adiag[i] - ai_i; 391 sum = b[i]; 392 while (nz--) sum -= *v++ * x[*vi++]; 393 x[i] = sum; 394 } 395 396 /* backward solve the upper triangular */ 397 for ( i=n-1; i>=0; i-- ){ 398 adiag_i = adiag[i]; 399 v = aa + adiag_i + 1; 400 vi = aj + adiag_i + 1; 401 nz = ai[i+1] - adiag_i - 1; 402 sum = x[i]; 403 while (nz--) sum -= *v++ * x[*vi++]; 404 x[i] = sum*aa[adiag_i]; 405 } 406 #endif 407 PLogFlops(2*a->nz - a->n); 408 PetscFunctionReturn(0); 409 } 410 411 #undef __FUNC__ 412 #define __FUNC__ "MatSolveAdd_SeqAIJ" 413 int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 414 { 415 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 416 IS iscol = a->col, isrow = a->row; 417 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 418 int nz, shift = a->indexshift,*rout,*cout; 419 Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 420 421 PetscFunctionBegin; 422 if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 423 424 VecGetArray_Fast(bb,b); 425 VecGetArray_Fast(xx,x); 426 tmp = a->solve_work; 427 428 ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout; 429 ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1); 430 431 /* forward solve the lower triangular */ 432 tmp[0] = b[*r++]; 433 for ( i=1; i<n; i++ ) { 434 v = aa + ai[i] + shift; 435 vi = aj + ai[i] + shift; 436 nz = a->diag[i] - ai[i]; 437 sum = b[*r++]; 438 while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 439 tmp[i] = sum; 440 } 441 442 /* backward solve the upper triangular */ 443 for ( i=n-1; i>=0; i-- ){ 444 v = aa + a->diag[i] + (!shift); 445 vi = aj + a->diag[i] + (!shift); 446 nz = ai[i+1] - a->diag[i] - 1; 447 sum = tmp[i]; 448 while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 449 tmp[i] = sum*aa[a->diag[i]+shift]; 450 x[*c--] += tmp[i]; 451 } 452 453 ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 454 ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 455 PLogFlops(2*a->nz); 456 457 PetscFunctionReturn(0); 458 } 459 /* -------------------------------------------------------------------*/ 460 #undef __FUNC__ 461 #define __FUNC__ "MatSolveTrans_SeqAIJ" 462 int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 463 { 464 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 465 IS iscol = a->col, isrow = a->row, invisrow,inviscol; 466 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 467 int nz,shift = a->indexshift,*rout,*cout; 468 Scalar *x,*b,*tmp, *aa = a->a, *v; 469 470 PetscFunctionBegin; 471 VecGetArray_Fast(bb,b); 472 VecGetArray_Fast(xx,x); 473 tmp = a->solve_work; 474 475 /* invert the permutations */ 476 ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 477 ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 478 479 ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout; 480 ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout; 481 482 /* copy the b into temp work space according to permutation */ 483 for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 484 485 /* forward solve the U^T */ 486 for ( i=0; i<n; i++ ) { 487 v = aa + a->diag[i] + shift; 488 vi = aj + a->diag[i] + (!shift); 489 nz = ai[i+1] - a->diag[i] - 1; 490 tmp[i] *= *v++; 491 while (nz--) { 492 tmp[*vi++ + shift] -= (*v++)*tmp[i]; 493 } 494 } 495 496 /* backward solve the L^T */ 497 for ( i=n-1; i>=0; i-- ){ 498 v = aa + a->diag[i] - 1 + shift; 499 vi = aj + a->diag[i] - 1 + shift; 500 nz = a->diag[i] - ai[i]; 501 while (nz--) { 502 tmp[*vi-- + shift] -= (*v--)*tmp[i]; 503 } 504 } 505 506 /* copy tmp into x according to permutation */ 507 for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 508 509 ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr); 510 ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr); 511 ierr = ISDestroy(invisrow); CHKERRQ(ierr); 512 ierr = ISDestroy(inviscol); CHKERRQ(ierr); 513 514 PLogFlops(2*a->nz-a->n); 515 PetscFunctionReturn(0); 516 } 517 518 #undef __FUNC__ 519 #define __FUNC__ "MatSolveTransAdd_SeqAIJ" 520 int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 521 { 522 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 523 IS iscol = a->col, isrow = a->row, invisrow,inviscol; 524 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 525 int nz,shift = a->indexshift, *rout, *cout; 526 Scalar *x,*b,*tmp, *aa = a->a, *v; 527 528 PetscFunctionBegin; 529 if (zz != xx) VecCopy(zz,xx); 530 531 VecGetArray_Fast(bb,b); 532 VecGetArray_Fast(xx,x); 533 tmp = a->solve_work; 534 535 /* invert the permutations */ 536 ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 537 ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 538 ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout; 539 ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout; 540 541 /* copy the b into temp work space according to permutation */ 542 for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 543 544 /* forward solve the U^T */ 545 for ( i=0; i<n; i++ ) { 546 v = aa + a->diag[i] + shift; 547 vi = aj + a->diag[i] + (!shift); 548 nz = ai[i+1] - a->diag[i] - 1; 549 tmp[i] *= *v++; 550 while (nz--) { 551 tmp[*vi++ + shift] -= (*v++)*tmp[i]; 552 } 553 } 554 555 /* backward solve the L^T */ 556 for ( i=n-1; i>=0; i-- ){ 557 v = aa + a->diag[i] - 1 + shift; 558 vi = aj + a->diag[i] - 1 + shift; 559 nz = a->diag[i] - ai[i]; 560 while (nz--) { 561 tmp[*vi-- + shift] -= (*v--)*tmp[i]; 562 } 563 } 564 565 /* copy tmp into x according to permutation */ 566 for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 567 568 ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr); 569 ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr); 570 ierr = ISDestroy(invisrow); CHKERRQ(ierr); 571 ierr = ISDestroy(inviscol); CHKERRQ(ierr); 572 573 PLogFlops(2*a->nz); 574 PetscFunctionReturn(0); 575 } 576 /* ----------------------------------------------------------------*/ 577 578 #undef __FUNC__ 579 #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ" 580 int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 581 { 582 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 583 IS isicol; 584 int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 585 int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 586 int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 587 int incrlev,nnz,i,shift = a->indexshift; 588 PetscTruth col_identity, row_identity; 589 590 PetscFunctionBegin; 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->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 603 (*fact)->ops.solve = MatSolve_SeqAIJ_NaturalOrdering; 604 PetscFunctionReturn(0); 605 } 606 607 ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 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 ierr = ISDestroy(isicol); CHKERRQ(ierr); 713 PetscFree(fill); PetscFree(im); 714 715 { 716 double af = ((double)ainew[n])/((double)ai[n]); 717 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 718 realloc,f,af); 719 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af); 720 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af); 721 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n"); 722 } 723 724 /* put together the new matrix */ 725 ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr); 726 b = (Mat_SeqAIJ *) (*fact)->data; 727 PetscFree(b->imax); 728 b->singlemalloc = 0; 729 len = (ainew[n] + shift)*sizeof(Scalar); 730 /* the next line frees the default space generated by the Create() */ 731 PetscFree(b->a); PetscFree(b->ilen); 732 b->a = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a); 733 b->j = ajnew; 734 b->i = ainew; 735 for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 736 b->diag = dloc; 737 b->ilen = 0; 738 b->imax = 0; 739 b->row = isrow; 740 b->col = iscol; 741 b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 742 CHKPTRQ(b->solve_work); 743 /* In b structure: Free imax, ilen, old a, old j. 744 Allocate dloc, solve_work, new a, new j */ 745 PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 746 b->maxnz = b->nz = ainew[n] + shift; 747 (*fact)->factor = FACTOR_LU; 748 749 (*fact)->info.factor_mallocs = realloc; 750 (*fact)->info.fill_ratio_given = f; 751 (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]); 752 753 PetscFunctionReturn(0); 754 } 755 756 757 758 759