1 #ifdef PETSC_RCS_HEADER 2 static char vcid[] = "$Id: aijfact.c,v 1.115 1999/01/24 19:56:37 bsmith Exp balay $"; 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 mat = (Mat_SeqAIJ *) A->data; 329 PLogObjectParent(A,mat->icol); 330 331 A->bops = Abops; 332 A->ops = Aops; 333 A->qlist = 0; 334 335 PetscHeaderDestroy(C); 336 PetscFunctionReturn(0); 337 } 338 /* ----------------------------------------------------------- */ 339 #undef __FUNC__ 340 #define __FUNC__ "MatSolve_SeqAIJ" 341 int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 342 { 343 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 344 IS iscol = a->col, isrow = a->row; 345 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 346 int nz,shift = a->indexshift,*rout,*cout; 347 Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 348 349 PetscFunctionBegin; 350 if (!n) PetscFunctionReturn(0); 351 352 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 353 ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 354 tmp = a->solve_work; 355 356 ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 357 ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 358 359 /* forward solve the lower triangular */ 360 tmp[0] = b[*r++]; 361 tmps = tmp + shift; 362 for ( i=1; i<n; i++ ) { 363 v = aa + ai[i] + shift; 364 vi = aj + ai[i] + shift; 365 nz = a->diag[i] - ai[i]; 366 sum = b[*r++]; 367 while (nz--) sum -= *v++ * tmps[*vi++]; 368 tmp[i] = sum; 369 } 370 371 /* backward solve the upper triangular */ 372 for ( i=n-1; i>=0; i-- ){ 373 v = aa + a->diag[i] + (!shift); 374 vi = aj + a->diag[i] + (!shift); 375 nz = ai[i+1] - a->diag[i] - 1; 376 sum = tmp[i]; 377 while (nz--) sum -= *v++ * tmps[*vi++]; 378 x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 379 } 380 381 ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 382 ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 383 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 384 ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 385 PLogFlops(2*a->nz - a->n); 386 PetscFunctionReturn(0); 387 } 388 389 /* ----------------------------------------------------------- */ 390 #undef __FUNC__ 391 #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering" 392 int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx) 393 { 394 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 395 int n = a->m, *ai = a->i, *aj = a->j, *adiag = a->diag,ierr; 396 Scalar *x,*b, *aa = a->a, sum; 397 #if !defined(USE_FORTRAN_KERNEL_SOLVEAIJ) 398 int adiag_i,i,*vi,nz,ai_i; 399 Scalar *v; 400 #endif 401 402 PetscFunctionBegin; 403 if (!n) PetscFunctionReturn(0); 404 if (a->indexshift) { 405 ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr); 406 PetscFunctionReturn(0); 407 } 408 409 ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 410 ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 411 412 #if defined(USE_FORTRAN_KERNEL_SOLVEAIJ) 413 fortransolveaij_(&n,x,ai,aj,adiag,aa,b); 414 #else 415 /* forward solve the lower triangular */ 416 x[0] = b[0]; 417 for ( i=1; i<n; i++ ) { 418 ai_i = ai[i]; 419 v = aa + ai_i; 420 vi = aj + ai_i; 421 nz = adiag[i] - ai_i; 422 sum = b[i]; 423 while (nz--) sum -= *v++ * x[*vi++]; 424 x[i] = sum; 425 } 426 427 /* backward solve the upper triangular */ 428 for ( i=n-1; i>=0; i-- ){ 429 adiag_i = adiag[i]; 430 v = aa + adiag_i + 1; 431 vi = aj + adiag_i + 1; 432 nz = ai[i+1] - adiag_i - 1; 433 sum = x[i]; 434 while (nz--) sum -= *v++ * x[*vi++]; 435 x[i] = sum*aa[adiag_i]; 436 } 437 #endif 438 PLogFlops(2*a->nz - a->n); 439 ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr); 440 ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 441 PetscFunctionReturn(0); 442 } 443 444 #undef __FUNC__ 445 #define __FUNC__ "MatSolveAdd_SeqAIJ" 446 int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 447 { 448 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 449 IS iscol = a->col, isrow = a->row; 450 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 451 int nz, shift = a->indexshift,*rout,*cout; 452 Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 453 454 PetscFunctionBegin; 455 if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 456 457 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 458 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 459 tmp = a->solve_work; 460 461 ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout; 462 ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1); 463 464 /* forward solve the lower triangular */ 465 tmp[0] = b[*r++]; 466 for ( i=1; i<n; i++ ) { 467 v = aa + ai[i] + shift; 468 vi = aj + ai[i] + shift; 469 nz = a->diag[i] - ai[i]; 470 sum = b[*r++]; 471 while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 472 tmp[i] = sum; 473 } 474 475 /* backward solve the upper triangular */ 476 for ( i=n-1; i>=0; i-- ){ 477 v = aa + a->diag[i] + (!shift); 478 vi = aj + a->diag[i] + (!shift); 479 nz = ai[i+1] - a->diag[i] - 1; 480 sum = tmp[i]; 481 while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 482 tmp[i] = sum*aa[a->diag[i]+shift]; 483 x[*c--] += tmp[i]; 484 } 485 486 ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 487 ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 488 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 489 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 490 PLogFlops(2*a->nz); 491 492 PetscFunctionReturn(0); 493 } 494 /* -------------------------------------------------------------------*/ 495 #undef __FUNC__ 496 #define __FUNC__ "MatSolveTrans_SeqAIJ" 497 int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 498 { 499 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 500 IS iscol = a->col, isrow = a->row; 501 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 502 int nz,shift = a->indexshift,*rout,*cout; 503 Scalar *x,*b,*tmp, *aa = a->a, *v; 504 505 PetscFunctionBegin; 506 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 507 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 508 tmp = a->solve_work; 509 510 ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout; 511 ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout; 512 513 /* copy the b into temp work space according to permutation */ 514 for ( i=0; i<n; i++ ) tmp[i] = b[c[i]]; 515 516 /* forward solve the U^T */ 517 for ( i=0; i<n; i++ ) { 518 v = aa + a->diag[i] + shift; 519 vi = aj + a->diag[i] + (!shift); 520 nz = ai[i+1] - a->diag[i] - 1; 521 tmp[i] *= *v++; 522 while (nz--) { 523 tmp[*vi++ + shift] -= (*v++)*tmp[i]; 524 } 525 } 526 527 /* backward solve the L^T */ 528 for ( i=n-1; i>=0; i-- ){ 529 v = aa + a->diag[i] - 1 + shift; 530 vi = aj + a->diag[i] - 1 + shift; 531 nz = a->diag[i] - ai[i]; 532 while (nz--) { 533 tmp[*vi-- + shift] -= (*v--)*tmp[i]; 534 } 535 } 536 537 /* copy tmp into x according to permutation */ 538 for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 539 540 ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 541 ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 542 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 543 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 544 545 PLogFlops(2*a->nz-a->n); 546 PetscFunctionReturn(0); 547 } 548 549 #undef __FUNC__ 550 #define __FUNC__ "MatSolveTransAdd_SeqAIJ" 551 int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 552 { 553 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 554 IS iscol = a->col, isrow = a->row; 555 int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 556 int nz,shift = a->indexshift, *rout, *cout; 557 Scalar *x,*b,*tmp, *aa = a->a, *v; 558 559 PetscFunctionBegin; 560 if (zz != xx) VecCopy(zz,xx); 561 562 ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 563 ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 564 tmp = a->solve_work; 565 566 ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout; 567 ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout; 568 569 /* copy the b into temp work space according to permutation */ 570 for ( i=0; i<n; i++ ) tmp[i] = b[c[i]]; 571 572 /* forward solve the U^T */ 573 for ( i=0; i<n; i++ ) { 574 v = aa + a->diag[i] + shift; 575 vi = aj + a->diag[i] + (!shift); 576 nz = ai[i+1] - a->diag[i] - 1; 577 tmp[i] *= *v++; 578 while (nz--) { 579 tmp[*vi++ + shift] -= (*v++)*tmp[i]; 580 } 581 } 582 583 /* backward solve the L^T */ 584 for ( i=n-1; i>=0; i-- ){ 585 v = aa + a->diag[i] - 1 + shift; 586 vi = aj + a->diag[i] - 1 + shift; 587 nz = a->diag[i] - ai[i]; 588 while (nz--) { 589 tmp[*vi-- + shift] -= (*v--)*tmp[i]; 590 } 591 } 592 593 /* copy tmp into x according to permutation */ 594 for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 595 596 ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 597 ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 598 ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); 599 ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 600 601 PLogFlops(2*a->nz); 602 PetscFunctionReturn(0); 603 } 604 /* ----------------------------------------------------------------*/ 605 extern int MatMissingDiag_SeqAIJ(Mat); 606 607 #undef __FUNC__ 608 #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ" 609 int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,MatILUInfo *info,Mat *fact) 610 { 611 Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 612 IS isicol; 613 int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 614 int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 615 int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0, dcount = 0; 616 int incrlev,nnz,i,shift = a->indexshift,levels,diagonal_fill; 617 PetscTruth col_identity, row_identity; 618 double f; 619 620 PetscFunctionBegin; 621 if (info) { 622 f = info->fill; 623 levels = (int) info->levels; 624 diagonal_fill = (int) info->diagonal_fill; 625 } else { 626 f = 1.0; 627 levels = 0; 628 diagonal_fill = 0; 629 } 630 ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 631 632 /* special case that simply copies fill pattern */ 633 ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity); 634 if (levels == 0 && row_identity && col_identity) { 635 ierr = MatDuplicate_SeqAIJ(A,MAT_DO_NOT_COPY_VALUES,fact); CHKERRQ(ierr); 636 (*fact)->factor = FACTOR_LU; 637 b = (Mat_SeqAIJ *) (*fact)->data; 638 if (!b->diag) { 639 ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 640 } 641 ierr = MatMissingDiag_SeqAIJ(*fact); CHKERRQ(ierr); 642 b->row = isrow; 643 b->col = iscol; 644 b->icol = isicol; 645 b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 646 (*fact)->ops->solve = MatSolve_SeqAIJ_NaturalOrdering; 647 PetscFunctionReturn(0); 648 } 649 650 ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 651 ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 652 653 /* get new row pointers */ 654 ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 655 ainew[0] = -shift; 656 /* don't know how many column pointers are needed so estimate */ 657 jmax = (int) (f*(ai[n]+!shift)); 658 ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 659 /* ajfill is level of fill for each fill entry */ 660 ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 661 /* fill is a linked list of nonzeros in active row */ 662 fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 663 /* im is level for each filled value */ 664 im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 665 /* dloc is location of diagonal in factor */ 666 dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 667 dloc[0] = 0; 668 for ( prow=0; prow<n; prow++ ) { 669 670 /* copy current row into linked list */ 671 nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 672 if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix"); 673 xi = aj + ai[r[prow]] + shift; 674 fill[n] = n; 675 fill[prow] = -1; /* marker to indicate if diagonal exists */ 676 while (nz--) { 677 fm = n; 678 idx = ic[*xi++ + shift]; 679 do { 680 m = fm; 681 fm = fill[m]; 682 } while (fm < idx); 683 fill[m] = idx; 684 fill[idx] = fm; 685 im[idx] = 0; 686 } 687 688 /* make sure diagonal entry is included */ 689 if (diagonal_fill && fill[prow] == -1) { 690 fm = n; 691 while (fill[fm] < prow) fm = fill[fm]; 692 fill[prow] = fill[fm]; /* insert diagonal into linked list */ 693 fill[fm] = prow; 694 im[prow] = 0; 695 nzf++; 696 dcount++; 697 } 698 699 nzi = 0; 700 row = fill[n]; 701 while ( row < prow ) { 702 incrlev = im[row] + 1; 703 nz = dloc[row]; 704 xi = ajnew + ainew[row] + shift + nz + 1; 705 flev = ajfill + ainew[row] + shift + nz + 1; 706 nnz = ainew[row+1] - ainew[row] - nz - 1; 707 fm = row; 708 while (nnz-- > 0) { 709 idx = *xi++ + shift; 710 if (*flev + incrlev > levels) { 711 flev++; 712 continue; 713 } 714 do { 715 m = fm; 716 fm = fill[m]; 717 } while (fm < idx); 718 if (fm != idx) { 719 im[idx] = *flev + incrlev; 720 fill[m] = idx; 721 fill[idx] = fm; 722 fm = idx; 723 nzf++; 724 } else { 725 if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 726 } 727 flev++; 728 } 729 row = fill[row]; 730 nzi++; 731 } 732 /* copy new filled row into permanent storage */ 733 ainew[prow+1] = ainew[prow] + nzf; 734 if (ainew[prow+1] > jmax-shift) { 735 736 /* estimate how much additional space we will need */ 737 /* use the strategy suggested by David Hysom <hysom@perch-t.icase.edu> */ 738 /* just double the memory each time */ 739 /* maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); */ 740 int maxadd = jmax; 741 if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 742 jmax += maxadd; 743 744 /* allocate a longer ajnew and ajfill */ 745 xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 746 PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 747 PetscFree(ajnew); 748 ajnew = xi; 749 xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 750 PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 751 PetscFree(ajfill); 752 ajfill = xi; 753 realloc++; /* count how many times we realloc */ 754 } 755 xi = ajnew + ainew[prow] + shift; 756 flev = ajfill + ainew[prow] + shift; 757 dloc[prow] = nzi; 758 fm = fill[n]; 759 while (nzf--) { 760 *xi++ = fm - shift; 761 *flev++ = im[fm]; 762 fm = fill[fm]; 763 } 764 /* make sure row has diagonal entry */ 765 if (ajnew[ainew[prow]+shift+dloc[prow]]+shift != prow) { 766 SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,1,"Row %d has missing diagonal in factored matrix\n\ 767 try running with -pc_ilu_nonzeros_along_diagonal or -pc_ilu_diagonal_fill",prow); 768 } 769 } 770 PetscFree(ajfill); 771 ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 772 ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 773 PetscFree(fill); PetscFree(im); 774 775 { 776 double af = ((double)ainew[n])/((double)ai[n]); 777 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 778 realloc,f,af); 779 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af); 780 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af); 781 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:for best performance.\n"); 782 if (diagonal_fill) { 783 PLogInfo(A,"MatILUFactorSymbolic_SeqAIJ:Detected and replace %d missing diagonals",dcount); 784 } 785 } 786 787 /* put together the new matrix */ 788 ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr); 789 PLogObjectParent(*fact,isicol); 790 b = (Mat_SeqAIJ *) (*fact)->data; 791 PetscFree(b->imax); 792 b->singlemalloc = 0; 793 len = (ainew[n] + shift)*sizeof(Scalar); 794 /* the next line frees the default space generated by the Create() */ 795 PetscFree(b->a); PetscFree(b->ilen); 796 b->a = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a); 797 b->j = ajnew; 798 b->i = ainew; 799 for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 800 b->diag = dloc; 801 b->ilen = 0; 802 b->imax = 0; 803 b->row = isrow; 804 b->col = iscol; 805 b->icol = isicol; 806 b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); CHKPTRQ(b->solve_work); 807 /* In b structure: Free imax, ilen, old a, old j. 808 Allocate dloc, solve_work, new a, new j */ 809 PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 810 b->maxnz = b->nz = ainew[n] + shift; 811 (*fact)->factor = FACTOR_LU; 812 813 (*fact)->info.factor_mallocs = realloc; 814 (*fact)->info.fill_ratio_given = f; 815 (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]); 816 (*fact)->factor = FACTOR_LU;; 817 818 PetscFunctionReturn(0); 819 } 820 821 822 823 824