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