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