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