1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*3a40ed3dSBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.89 1997/09/18 16:47:24 balay Exp bsmith $"; 3cb512458SBarry Smith #endif 4289bc588SBarry Smith 570f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h" 690f02eecSBarry Smith #include "src/vec/vecimpl.h" 71c4feecaSSatish Balay #include "src/inline/dot.h" 83b2fbd54SBarry Smith 95615d1e5SSatish Balay #undef __FUNC__ 10d4bb536fSBarry Smith #define __FUNC__ "MatOrder_Flow_SeqAIJ" 113b2fbd54SBarry Smith int MatOrder_Flow_SeqAIJ(Mat mat,MatReordering type,IS *irow,IS *icol) 123b2fbd54SBarry Smith { 13*3a40ed3dSBarry Smith PetscFunctionBegin; 14*3a40ed3dSBarry Smith 15e3372554SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Code not written"); 163b2fbd54SBarry Smith } 173b2fbd54SBarry Smith 18289bc588SBarry Smith /* 19289bc588SBarry Smith Factorization code for AIJ format. 20289bc588SBarry Smith */ 215615d1e5SSatish Balay #undef __FUNC__ 225615d1e5SSatish Balay #define __FUNC__ "MatLUFactorSymbolic_SeqAIJ" 23416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 24289bc588SBarry Smith { 25416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 26289bc588SBarry Smith IS isicol; 27416022c9SBarry Smith int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 28416022c9SBarry Smith int *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift; 2991bf9adeSBarry Smith int *idnew, idx, row,m,fm, nnz, nzi, realloc = 0,nzbd,*im; 30289bc588SBarry Smith 31*3a40ed3dSBarry Smith PetscFunctionBegin; 32d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(isrow,IS_COOKIE); 33d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(iscol,IS_COOKIE); 343b2fbd54SBarry Smith 3578b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 36289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 37289bc588SBarry Smith 38289bc588SBarry Smith /* get new row pointers */ 390452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 40dbb450caSBarry Smith ainew[0] = -shift; 41289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 42dbb450caSBarry Smith jmax = (int) (f*ai[n]+(!shift)); 430452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 44289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 450452661fSBarry Smith fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 462fb3ed76SBarry Smith im = fill + n + 1; 47289bc588SBarry Smith /* idnew is location of diagonal in factor */ 480452661fSBarry Smith idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew); 49dbb450caSBarry Smith idnew[0] = -shift; 50289bc588SBarry Smith 51289bc588SBarry Smith for ( i=0; i<n; i++ ) { 52289bc588SBarry Smith /* first copy previous fill into linked list */ 53289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 546b96ef82SSatish Balay if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix"); 55dbb450caSBarry Smith ajtmp = aj + ai[r[i]] + shift; 56289bc588SBarry Smith fill[n] = n; 57289bc588SBarry Smith while (nz--) { 58289bc588SBarry Smith fm = n; 59dbb450caSBarry Smith idx = ic[*ajtmp++ + shift]; 60289bc588SBarry Smith do { 61289bc588SBarry Smith m = fm; 62289bc588SBarry Smith fm = fill[m]; 63289bc588SBarry Smith } while (fm < idx); 64289bc588SBarry Smith fill[m] = idx; 65289bc588SBarry Smith fill[idx] = fm; 66289bc588SBarry Smith } 67289bc588SBarry Smith row = fill[n]; 68289bc588SBarry Smith while ( row < i ) { 69dbb450caSBarry Smith ajtmp = ajnew + idnew[row] + (!shift); 702fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 712fb3ed76SBarry Smith nz = im[row] - nzbd; 72289bc588SBarry Smith fm = row; 732fb3ed76SBarry Smith while (nz-- > 0) { 74dbb450caSBarry Smith idx = *ajtmp++ + shift; 752fb3ed76SBarry Smith nzbd++; 762fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 77289bc588SBarry Smith do { 78289bc588SBarry Smith m = fm; 79289bc588SBarry Smith fm = fill[m]; 80289bc588SBarry Smith } while (fm < idx); 81289bc588SBarry Smith if (fm != idx) { 82289bc588SBarry Smith fill[m] = idx; 83289bc588SBarry Smith fill[idx] = fm; 84289bc588SBarry Smith fm = idx; 85289bc588SBarry Smith nnz++; 86289bc588SBarry Smith } 87289bc588SBarry Smith } 88289bc588SBarry Smith row = fill[row]; 89289bc588SBarry Smith } 90289bc588SBarry Smith /* copy new filled row into permanent storage */ 91289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 92496e697eSBarry Smith if (ainew[i+1] > jmax) { 93289bc588SBarry Smith /* allocate a longer ajnew */ 942fb3ed76SBarry Smith int maxadd; 95dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); 96bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 972fb3ed76SBarry Smith jmax += maxadd; 980452661fSBarry Smith ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp); 99416022c9SBarry Smith PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int)); 1000452661fSBarry Smith PetscFree(ajnew); 101289bc588SBarry Smith ajnew = ajtmp; 1022fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 103289bc588SBarry Smith } 104dbb450caSBarry Smith ajtmp = ajnew + ainew[i] + shift; 105289bc588SBarry Smith fm = fill[n]; 106289bc588SBarry Smith nzi = 0; 1072fb3ed76SBarry Smith im[i] = nnz; 108289bc588SBarry Smith while (nnz--) { 109289bc588SBarry Smith if (fm < i) nzi++; 110dbb450caSBarry Smith *ajtmp++ = fm - shift; 111289bc588SBarry Smith fm = fill[fm]; 112289bc588SBarry Smith } 113289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 114289bc588SBarry Smith } 115464e8d44SSatish Balay if (ai[n] != 0) { 116464e8d44SSatish Balay double af = ((double)ainew[n])/((double)ai[n]); 117f64065bbSBarry Smith PLogInfo(A,"Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 118f64065bbSBarry Smith realloc,f,af); 119f64065bbSBarry Smith PLogInfo(A,"Info:MatLUFactorSymbolic_SeqAIJ:Run with -pc_lu_fill %g or use \n",af); 120f64065bbSBarry Smith PLogInfo(A,"Info:MatLUFactorSymbolic_SeqAIJ:PCLUSetFill(pc,%g);\n",af); 121f64065bbSBarry Smith PLogInfo(A,"Info:MatLUFactorSymbolic_SeqAIJ:for best performance.\n"); 12205bf559cSSatish Balay } else { 123f64065bbSBarry Smith PLogInfo(A,"Info:MatLUFactorSymbolic_SeqAIJ: Empty matrix\n"); 12405bf559cSSatish Balay } 1252fb3ed76SBarry Smith 126898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 127898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 1281987afe7SBarry Smith 1290452661fSBarry Smith PetscFree(fill); 130289bc588SBarry Smith 131289bc588SBarry Smith /* put together the new matrix */ 132b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr); 1331987afe7SBarry Smith PLogObjectParent(*B,isicol); 1341987afe7SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 135416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 1360452661fSBarry Smith PetscFree(b->imax); 137416022c9SBarry Smith b->singlemalloc = 0; 138e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 1390452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 14091bf9adeSBarry Smith b->a = (Scalar *) PetscMalloc((ainew[n]+shift+1)*sizeof(Scalar));CHKPTRQ(b->a); 141416022c9SBarry Smith b->j = ajnew; 142416022c9SBarry Smith b->i = ainew; 143416022c9SBarry Smith b->diag = idnew; 144416022c9SBarry Smith b->ilen = 0; 145416022c9SBarry Smith b->imax = 0; 146416022c9SBarry Smith b->row = isrow; 147416022c9SBarry Smith b->col = iscol; 14891bf9adeSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 149416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 1507fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 151416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 152416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 1537fd98bd6SLois Curfman McInnes 154ae068f58SLois Curfman McInnes (*B)->info.factor_mallocs = realloc; 155ae068f58SLois Curfman McInnes (*B)->info.fill_ratio_given = f; 156eea2913aSSatish Balay if (ai[i] != 0) { 157ae068f58SLois Curfman McInnes (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]); 158eea2913aSSatish Balay } else { 159eea2913aSSatish Balay (*B)->info.fill_ratio_needed = 0.0; 160eea2913aSSatish Balay } 161ae068f58SLois Curfman McInnes 162*3a40ed3dSBarry Smith PetscFunctionReturn(0); 163289bc588SBarry Smith } 1640a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 16541c01911SSatish Balay int Mat_AIJ_CheckInode(Mat); 16641c01911SSatish Balay 1675615d1e5SSatish Balay #undef __FUNC__ 1685615d1e5SSatish Balay #define __FUNC__ "MatLUFactorNumeric_SeqAIJ" 169416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 170289bc588SBarry Smith { 171416022c9SBarry Smith Mat C = *B; 172416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 173416022c9SBarry Smith IS iscol = b->col, isrow = b->row, isicol; 174416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 1751987afe7SBarry Smith int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 176f2582111SSatish Balay int *diag_offset = b->diag,diag,k; 17735aab85fSBarry Smith int preserve_row_sums = (int) a->ilu_preserve_row_sums; 178*3a40ed3dSBarry Smith register int *pj; 1798ecf7165SBarry Smith Scalar *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0; 18035aab85fSBarry Smith double ssum; 18135aab85fSBarry Smith register Scalar *pv, *rtmps,*u_values; 182289bc588SBarry Smith 183*3a40ed3dSBarry Smith PetscFunctionBegin; 18478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 185416022c9SBarry Smith PLogObjectParent(*B,isicol); 18678b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 18778b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 1880452661fSBarry Smith rtmp = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 18913ae1565SBarry Smith PetscMemzero(rtmp,(n+1)*sizeof(Scalar)); 1900cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 191289bc588SBarry Smith 19235aab85fSBarry Smith /* precalcuate row sums */ 19335aab85fSBarry Smith if (preserve_row_sums) { 19435aab85fSBarry Smith rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums); 19535aab85fSBarry Smith for ( i=0; i<n; i++ ) { 19635aab85fSBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 19735aab85fSBarry Smith v = a->a + a->i[r[i]] + shift; 19835aab85fSBarry Smith sum = 0.0; 19935aab85fSBarry Smith for ( j=0; j<nz; j++ ) sum += v[j]; 20035aab85fSBarry Smith rowsums[i] = sum; 20135aab85fSBarry Smith } 20235aab85fSBarry Smith } 20335aab85fSBarry Smith 204289bc588SBarry Smith for ( i=0; i<n; i++ ) { 205289bc588SBarry Smith nz = ai[i+1] - ai[i]; 206dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 20748e94559SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 208289bc588SBarry Smith 209289bc588SBarry Smith /* load in initial (unfactored row) */ 210416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 211416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 212416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 2130cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 214289bc588SBarry Smith 215dbb450caSBarry Smith row = *ajtmp++ + shift; 216f2582111SSatish Balay while (row < i ) { 2178c37ef55SBarry Smith pc = rtmp + row; 2188c37ef55SBarry Smith if (*pc != 0.0) { 2191987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 2201987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 22135aab85fSBarry Smith multiplier = *pc / *pv++; 2228c37ef55SBarry Smith *pc = multiplier; 223f2582111SSatish Balay nz = ai[row+1] - diag_offset[row] - 1; 224f2582111SSatish Balay for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 225f2582111SSatish Balay PLogFlops(2*nz); 226289bc588SBarry Smith } 227dbb450caSBarry Smith row = *ajtmp++ + shift; 228289bc588SBarry Smith } 229416022c9SBarry Smith /* finished row so stick it into b->a */ 230416022c9SBarry Smith pv = b->a + ai[i] + shift; 231416022c9SBarry Smith pj = b->j + ai[i] + shift; 2328c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 233416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 23435aab85fSBarry Smith diag = diag_offset[i] - ai[i]; 23535aab85fSBarry Smith /* 23635aab85fSBarry Smith Possibly adjust diagonal entry on current row to force 23735aab85fSBarry Smith LU matrix to have same row sum as initial matrix. 23835aab85fSBarry Smith */ 239d708749eSBarry Smith if (pv[diag] == 0.0) { 240d708749eSBarry Smith SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot"); 241d708749eSBarry Smith } 24235aab85fSBarry Smith if (preserve_row_sums) { 24335aab85fSBarry Smith pj = b->j + ai[i] + shift; 24435aab85fSBarry Smith sum = rowsums[i]; 24535aab85fSBarry Smith for ( j=0; j<diag; j++ ) { 24635aab85fSBarry Smith u_values = b->a + diag_offset[pj[j]] + shift; 24735aab85fSBarry Smith nz = ai[pj[j]+1] - diag_offset[pj[j]]; 24835aab85fSBarry Smith inner_sum = 0.0; 24935aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 25035aab85fSBarry Smith inner_sum += u_values[k]; 25135aab85fSBarry Smith } 25235aab85fSBarry Smith sum -= pv[j]*inner_sum; 25335aab85fSBarry Smith 25435aab85fSBarry Smith } 25535aab85fSBarry Smith nz = ai[i+1] - diag_offset[i] - 1; 25635aab85fSBarry Smith u_values = b->a + diag_offset[i] + 1 + shift; 25735aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 25835aab85fSBarry Smith sum -= u_values[k]; 25935aab85fSBarry Smith } 26035aab85fSBarry Smith ssum = PetscAbsScalar(sum/pv[diag]); 26135aab85fSBarry Smith if (ssum < 1000. && ssum > .001) pv[diag] = sum; 26235aab85fSBarry Smith } 26335aab85fSBarry Smith /* check pivot entry for current row */ 2648c37ef55SBarry Smith } 2650f11f9aeSSatish Balay 26635aab85fSBarry Smith /* invert diagonal entries for simplier triangular solves */ 26735aab85fSBarry Smith for ( i=0; i<n; i++ ) { 26835aab85fSBarry Smith b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 26935aab85fSBarry Smith } 27035aab85fSBarry Smith 27135aab85fSBarry Smith if (preserve_row_sums) PetscFree(rowsums); 2720452661fSBarry Smith PetscFree(rtmp); 27378b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 27478b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 27578b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 276416022c9SBarry Smith C->factor = FACTOR_LU; 27741c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 278c456f294SBarry Smith C->assembled = PETSC_TRUE; 279416022c9SBarry Smith PLogFlops(b->n); 280*3a40ed3dSBarry Smith PetscFunctionReturn(0); 281289bc588SBarry Smith } 2820a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 2835615d1e5SSatish Balay #undef __FUNC__ 2845615d1e5SSatish Balay #define __FUNC__ "MatLUFactor_SeqAIJ" 285416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 286da3a660dSBarry Smith { 287416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2886abc6512SBarry Smith int ierr; 289416022c9SBarry Smith Mat C; 290416022c9SBarry Smith 291*3a40ed3dSBarry Smith PetscFunctionBegin; 292f2582111SSatish Balay ierr = MatLUFactorSymbolic(A,row,col,f,&C); CHKERRQ(ierr); 293f2582111SSatish Balay ierr = MatLUFactorNumeric(A,&C); CHKERRQ(ierr); 294da3a660dSBarry Smith 295da3a660dSBarry Smith /* free all the data structures from mat */ 2960452661fSBarry Smith PetscFree(mat->a); 2970452661fSBarry Smith if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 2980452661fSBarry Smith if (mat->diag) PetscFree(mat->diag); 2990452661fSBarry Smith if (mat->ilen) PetscFree(mat->ilen); 3000452661fSBarry Smith if (mat->imax) PetscFree(mat->imax); 3010452661fSBarry Smith if (mat->solve_work) PetscFree(mat->solve_work); 302a7a49059SBarry Smith if (mat->inode.size) PetscFree(mat->inode.size); 3030452661fSBarry Smith PetscFree(mat); 304da3a660dSBarry Smith 305f09e8eb9SSatish Balay PetscMemcpy(A,C,sizeof(struct _p_Mat)); 3060452661fSBarry Smith PetscHeaderDestroy(C); 307*3a40ed3dSBarry Smith PetscFunctionReturn(0); 308da3a660dSBarry Smith } 3090a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 3105615d1e5SSatish Balay #undef __FUNC__ 3115615d1e5SSatish Balay #define __FUNC__ "MatSolve_SeqAIJ" 312416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 3138c37ef55SBarry Smith { 314416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 315416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 316416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 3173b2fbd54SBarry Smith int nz,shift = a->indexshift,*rout,*cout; 318416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 3198c37ef55SBarry Smith 320*3a40ed3dSBarry Smith PetscFunctionBegin; 321*3a40ed3dSBarry Smith if (!n) PetscFunctionReturn(0); 32291bf9adeSBarry Smith 32390f02eecSBarry Smith VecGetArray_Fast(bb,b); 32490f02eecSBarry Smith VecGetArray_Fast(xx,x); 325416022c9SBarry Smith tmp = a->solve_work; 3268c37ef55SBarry Smith 3273b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 3283b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 3298c37ef55SBarry Smith 3308c37ef55SBarry Smith /* forward solve the lower triangular */ 3318c37ef55SBarry Smith tmp[0] = b[*r++]; 3320cf60383SBarry Smith tmps = tmp + shift; 3338c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 334dbb450caSBarry Smith v = aa + ai[i] + shift; 335dbb450caSBarry Smith vi = aj + ai[i] + shift; 336416022c9SBarry Smith nz = a->diag[i] - ai[i]; 3378c37ef55SBarry Smith sum = b[*r++]; 3380cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 3398c37ef55SBarry Smith tmp[i] = sum; 3408c37ef55SBarry Smith } 3418c37ef55SBarry Smith 3428c37ef55SBarry Smith /* backward solve the upper triangular */ 3438c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 344416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 345416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 346416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 3478c37ef55SBarry Smith sum = tmp[i]; 3480cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 349416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 3508c37ef55SBarry Smith } 3518c37ef55SBarry Smith 3523b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 3533b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 354416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 355*3a40ed3dSBarry Smith PetscFunctionReturn(0); 3568c37ef55SBarry Smith } 357026e39d0SSatish Balay 358930ae53cSBarry Smith /* ----------------------------------------------------------- */ 359930ae53cSBarry Smith #undef __FUNC__ 360930ae53cSBarry Smith #define __FUNC__ "MatSolve_SeqAIJ_NaturalOrdering" 361930ae53cSBarry Smith int MatSolve_SeqAIJ_NaturalOrdering(Mat A,Vec bb, Vec xx) 362930ae53cSBarry Smith { 363930ae53cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 364930ae53cSBarry Smith int i, n = a->m, *vi, *ai = a->i, *aj = a->j,nz, *adiag = a->diag; 365*3a40ed3dSBarry Smith int ai_i, adiag_i,ierr; 366930ae53cSBarry Smith Scalar *x,*b, *aa = a->a, sum, *v; 367930ae53cSBarry Smith 368*3a40ed3dSBarry Smith PetscFunctionBegin; 369*3a40ed3dSBarry Smith if (!n) PetscFunctionReturn(0); 370930ae53cSBarry Smith if (a->indexshift) { 371*3a40ed3dSBarry Smith ierr = MatSolve_SeqAIJ(A,bb,xx);CHKERRQ(ierr); 372*3a40ed3dSBarry Smith PetscFunctionReturn(0); 373930ae53cSBarry Smith } 374930ae53cSBarry Smith 375930ae53cSBarry Smith VecGetArray_Fast(bb,b); 376930ae53cSBarry Smith VecGetArray_Fast(xx,x); 377930ae53cSBarry Smith 378319c5e4dSSatish Balay #if defined(USE_FORTRAN_KERNELS) 3791c4feecaSSatish Balay fortransolveaij_(&n,x,ai,aj,adiag,aa,b); 3801c4feecaSSatish Balay #else 381930ae53cSBarry Smith /* forward solve the lower triangular */ 382930ae53cSBarry Smith x[0] = b[0]; 383930ae53cSBarry Smith for ( i=1; i<n; i++ ) { 384930ae53cSBarry Smith ai_i = ai[i]; 385930ae53cSBarry Smith v = aa + ai_i; 386930ae53cSBarry Smith vi = aj + ai_i; 387930ae53cSBarry Smith nz = adiag[i] - ai_i; 388930ae53cSBarry Smith sum = b[i]; 389930ae53cSBarry Smith while (nz--) sum -= *v++ * x[*vi++]; 390930ae53cSBarry Smith x[i] = sum; 391930ae53cSBarry Smith } 392930ae53cSBarry Smith 393930ae53cSBarry Smith /* backward solve the upper triangular */ 394930ae53cSBarry Smith for ( i=n-1; i>=0; i-- ){ 395930ae53cSBarry Smith adiag_i = adiag[i]; 396d708749eSBarry Smith v = aa + adiag_i + 1; 397d708749eSBarry Smith vi = aj + adiag_i + 1; 398930ae53cSBarry Smith nz = ai[i+1] - adiag_i - 1; 399930ae53cSBarry Smith sum = x[i]; 400930ae53cSBarry Smith while (nz--) sum -= *v++ * x[*vi++]; 401930ae53cSBarry Smith x[i] = sum*aa[adiag_i]; 402930ae53cSBarry Smith } 4031c4feecaSSatish Balay #endif 404930ae53cSBarry Smith PLogFlops(2*a->nz - a->n); 405*3a40ed3dSBarry Smith PetscFunctionReturn(0); 406930ae53cSBarry Smith } 407930ae53cSBarry Smith 4085615d1e5SSatish Balay #undef __FUNC__ 4095615d1e5SSatish Balay #define __FUNC__ "MatSolveAdd_SeqAIJ" 410416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 411da3a660dSBarry Smith { 412416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 413416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 414416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 4153b2fbd54SBarry Smith int nz, shift = a->indexshift,*rout,*cout; 416416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 417da3a660dSBarry Smith 418*3a40ed3dSBarry Smith PetscFunctionBegin; 41978b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 420da3a660dSBarry Smith 42190f02eecSBarry Smith VecGetArray_Fast(bb,b); 42290f02eecSBarry Smith VecGetArray_Fast(xx,x); 423416022c9SBarry Smith tmp = a->solve_work; 424da3a660dSBarry Smith 4253b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout; 4263b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1); 427da3a660dSBarry Smith 428da3a660dSBarry Smith /* forward solve the lower triangular */ 429da3a660dSBarry Smith tmp[0] = b[*r++]; 430da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 431dbb450caSBarry Smith v = aa + ai[i] + shift; 432dbb450caSBarry Smith vi = aj + ai[i] + shift; 433416022c9SBarry Smith nz = a->diag[i] - ai[i]; 434da3a660dSBarry Smith sum = b[*r++]; 435dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 436da3a660dSBarry Smith tmp[i] = sum; 437da3a660dSBarry Smith } 438da3a660dSBarry Smith 439da3a660dSBarry Smith /* backward solve the upper triangular */ 440da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 441416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 442416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 443416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 444da3a660dSBarry Smith sum = tmp[i]; 445dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 446416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 447da3a660dSBarry Smith x[*c--] += tmp[i]; 448da3a660dSBarry Smith } 449da3a660dSBarry Smith 4503b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 4513b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 452416022c9SBarry Smith PLogFlops(2*a->nz); 453898183ecSLois Curfman McInnes 454*3a40ed3dSBarry Smith PetscFunctionReturn(0); 455da3a660dSBarry Smith } 456da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 4575615d1e5SSatish Balay #undef __FUNC__ 4585615d1e5SSatish Balay #define __FUNC__ "MatSolveTrans_SeqAIJ" 459416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 460da3a660dSBarry Smith { 461416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 462416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 463416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 4643b2fbd54SBarry Smith int nz,shift = a->indexshift,*rout,*cout; 465416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 466da3a660dSBarry Smith 467*3a40ed3dSBarry Smith PetscFunctionBegin; 46890f02eecSBarry Smith VecGetArray_Fast(bb,b); 46990f02eecSBarry Smith VecGetArray_Fast(xx,x); 470416022c9SBarry Smith tmp = a->solve_work; 471da3a660dSBarry Smith 472da3a660dSBarry Smith /* invert the permutations */ 47378b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 47478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 475da3a660dSBarry Smith 4763b2fbd54SBarry Smith ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout; 4773b2fbd54SBarry Smith ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout; 478da3a660dSBarry Smith 479da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 480da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 481da3a660dSBarry Smith 482da3a660dSBarry Smith /* forward solve the U^T */ 483da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 484416022c9SBarry Smith v = aa + a->diag[i] + shift; 485416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 486416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 487da3a660dSBarry Smith tmp[i] *= *v++; 488da3a660dSBarry Smith while (nz--) { 489dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 490da3a660dSBarry Smith } 491da3a660dSBarry Smith } 492da3a660dSBarry Smith 493da3a660dSBarry Smith /* backward solve the L^T */ 494da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 495416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 496416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 497416022c9SBarry Smith nz = a->diag[i] - ai[i]; 498da3a660dSBarry Smith while (nz--) { 499dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 500da3a660dSBarry Smith } 501da3a660dSBarry Smith } 502da3a660dSBarry Smith 503da3a660dSBarry Smith /* copy tmp into x according to permutation */ 504da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 505da3a660dSBarry Smith 5063b2fbd54SBarry Smith ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr); 5073b2fbd54SBarry Smith ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr); 508898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 509898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 510da3a660dSBarry Smith 511416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 512*3a40ed3dSBarry Smith PetscFunctionReturn(0); 513da3a660dSBarry Smith } 514da3a660dSBarry Smith 5155615d1e5SSatish Balay #undef __FUNC__ 5165615d1e5SSatish Balay #define __FUNC__ "MatSolveTransAdd_SeqAIJ" 517416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 518da3a660dSBarry Smith { 519416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 520416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 521416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 5223b2fbd54SBarry Smith int nz,shift = a->indexshift, *rout, *cout; 523416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 5246abc6512SBarry Smith 525*3a40ed3dSBarry Smith PetscFunctionBegin; 5266abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 5276abc6512SBarry Smith 52890f02eecSBarry Smith VecGetArray_Fast(bb,b); 52990f02eecSBarry Smith VecGetArray_Fast(xx,x); 530416022c9SBarry Smith tmp = a->solve_work; 5316abc6512SBarry Smith 5326abc6512SBarry Smith /* invert the permutations */ 53378b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 53478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 5353b2fbd54SBarry Smith ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout; 5363b2fbd54SBarry Smith ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout; 5376abc6512SBarry Smith 5386abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 5396abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 5406abc6512SBarry Smith 5416abc6512SBarry Smith /* forward solve the U^T */ 5426abc6512SBarry Smith for ( i=0; i<n; i++ ) { 543416022c9SBarry Smith v = aa + a->diag[i] + shift; 544416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 545416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 5466abc6512SBarry Smith tmp[i] *= *v++; 5476abc6512SBarry Smith while (nz--) { 548dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 5496abc6512SBarry Smith } 5506abc6512SBarry Smith } 5516abc6512SBarry Smith 5526abc6512SBarry Smith /* backward solve the L^T */ 5536abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 554416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 555416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 556416022c9SBarry Smith nz = a->diag[i] - ai[i]; 5576abc6512SBarry Smith while (nz--) { 558dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 5596abc6512SBarry Smith } 5606abc6512SBarry Smith } 5616abc6512SBarry Smith 5626abc6512SBarry Smith /* copy tmp into x according to permutation */ 5636abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 5646abc6512SBarry Smith 5653b2fbd54SBarry Smith ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr); 5663b2fbd54SBarry Smith ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr); 567898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 568898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 5696abc6512SBarry Smith 570416022c9SBarry Smith PLogFlops(2*a->nz); 571*3a40ed3dSBarry Smith PetscFunctionReturn(0); 572da3a660dSBarry Smith } 5739e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 57408480c60SBarry Smith 5755615d1e5SSatish Balay #undef __FUNC__ 5765615d1e5SSatish Balay #define __FUNC__ "MatILUFactorSymbolic_SeqAIJ" 57708480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 5789e25ed09SBarry Smith { 579416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 5809e25ed09SBarry Smith IS isicol; 581416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 58256beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 58356beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 584416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 58577c4ece6SBarry Smith PetscTruth col_identity, row_identity; 5869e25ed09SBarry Smith 587*3a40ed3dSBarry Smith PetscFunctionBegin; 58808480c60SBarry Smith /* special case that simply copies fill pattern */ 58977c4ece6SBarry Smith ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity); 59077c4ece6SBarry Smith if (levels == 0 && row_identity && col_identity) { 5913d1612f7SBarry Smith ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 59208480c60SBarry Smith (*fact)->factor = FACTOR_LU; 59308480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 59408480c60SBarry Smith if (!b->diag) { 59508480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 59608480c60SBarry Smith } 59708480c60SBarry Smith b->row = isrow; 59808480c60SBarry Smith b->col = iscol; 5990452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 600930ae53cSBarry Smith (*fact)->ops.solve = MatSolve_SeqAIJ_NaturalOrdering; 601*3a40ed3dSBarry Smith PetscFunctionReturn(0); 60208480c60SBarry Smith } 60308480c60SBarry Smith 60478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 605898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 606898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 6079e25ed09SBarry Smith 6089e25ed09SBarry Smith /* get new row pointers */ 6090452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 610dbb450caSBarry Smith ainew[0] = -shift; 6119e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 612dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 6130452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 6149e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 6150452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 6169e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 6170452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 61856beaf04SBarry Smith /* im is level for each filled value */ 6190452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 62056beaf04SBarry Smith /* dloc is location of diagonal in factor */ 6210452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 62256beaf04SBarry Smith dloc[0] = 0; 62356beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 6249e25ed09SBarry Smith /* first copy previous fill into linked list */ 62556beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 626385f7a74SSatish Balay if (!nz) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,1,"Empty row in matrix"); 627dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 6289e25ed09SBarry Smith fill[n] = n; 6299e25ed09SBarry Smith while (nz--) { 6309e25ed09SBarry Smith fm = n; 631dbb450caSBarry Smith idx = ic[*xi++ + shift]; 6329e25ed09SBarry Smith do { 6339e25ed09SBarry Smith m = fm; 6349e25ed09SBarry Smith fm = fill[m]; 6359e25ed09SBarry Smith } while (fm < idx); 6369e25ed09SBarry Smith fill[m] = idx; 6379e25ed09SBarry Smith fill[idx] = fm; 63856beaf04SBarry Smith im[idx] = 0; 6399e25ed09SBarry Smith } 64056beaf04SBarry Smith nzi = 0; 6419e25ed09SBarry Smith row = fill[n]; 64256beaf04SBarry Smith while ( row < prow ) { 64356beaf04SBarry Smith incrlev = im[row] + 1; 64456beaf04SBarry Smith nz = dloc[row]; 645dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 646dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 647416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 648dbb450caSBarry Smith if (*xi++ + shift != row) { 649965fcd8bSBarry Smith SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Zero pivot: try running with -pc_ilu_nonzeros_along_diagonal"); 65056beaf04SBarry Smith } 65156beaf04SBarry Smith fm = row; 65256beaf04SBarry Smith while (nnz-- > 0) { 653dbb450caSBarry Smith idx = *xi++ + shift; 65456beaf04SBarry Smith if (*flev + incrlev > levels) { 65556beaf04SBarry Smith flev++; 65656beaf04SBarry Smith continue; 65756beaf04SBarry Smith } 65856beaf04SBarry Smith do { 6599e25ed09SBarry Smith m = fm; 6609e25ed09SBarry Smith fm = fill[m]; 66156beaf04SBarry Smith } while (fm < idx); 6629e25ed09SBarry Smith if (fm != idx) { 66356beaf04SBarry Smith im[idx] = *flev + incrlev; 6649e25ed09SBarry Smith fill[m] = idx; 6659e25ed09SBarry Smith fill[idx] = fm; 6669e25ed09SBarry Smith fm = idx; 66756beaf04SBarry Smith nzf++; 6689e25ed09SBarry Smith } 66956beaf04SBarry Smith else { 67056beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 6719e25ed09SBarry Smith } 67256beaf04SBarry Smith flev++; 6739e25ed09SBarry Smith } 6749e25ed09SBarry Smith row = fill[row]; 67556beaf04SBarry Smith nzi++; 6769e25ed09SBarry Smith } 6779e25ed09SBarry Smith /* copy new filled row into permanent storage */ 67856beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 679d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 6809e25ed09SBarry Smith /* allocate a longer ajnew */ 681bbb6d6a8SBarry Smith int maxadd; 682dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 68356beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 684bbb6d6a8SBarry Smith jmax += maxadd; 6850452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 686416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 6870452661fSBarry Smith PetscFree(ajnew); 68856beaf04SBarry Smith ajnew = xi; 6899e25ed09SBarry Smith /* allocate a longer ajfill */ 6900452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 691416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 6920452661fSBarry Smith PetscFree(ajfill); 69356beaf04SBarry Smith ajfill = xi; 694bbb6d6a8SBarry Smith realloc++; 6959e25ed09SBarry Smith } 696dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 697dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 69856beaf04SBarry Smith dloc[prow] = nzi; 6999e25ed09SBarry Smith fm = fill[n]; 70056beaf04SBarry Smith while (nzf--) { 701dbb450caSBarry Smith *xi++ = fm - shift; 70256beaf04SBarry Smith *flev++ = im[fm]; 7039e25ed09SBarry Smith fm = fill[fm]; 7049e25ed09SBarry Smith } 7059e25ed09SBarry Smith } 7060452661fSBarry Smith PetscFree(ajfill); 707898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 708898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 709898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 7100452661fSBarry Smith PetscFree(fill); PetscFree(im); 7119e25ed09SBarry Smith 712f64065bbSBarry Smith { 713464e8d44SSatish Balay double af = ((double)ainew[n])/((double)ai[n]); 714f64065bbSBarry Smith PLogInfo(A,"Info:MatILUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 715f64065bbSBarry Smith realloc,f,af); 716f64065bbSBarry Smith PLogInfo(A,"Info:MatILUFactorSymbolic_SeqAIJ:Run with -pc_ilu_fill %g or use \n",af); 717f64065bbSBarry Smith PLogInfo(A,"Info:MatILUFactorSymbolic_SeqAIJ:PCILUSetFill(pc,%g);\n",af); 718f64065bbSBarry Smith PLogInfo(A,"Info:MatILUFactorSymbolic_SeqAIJ:for best performance.\n"); 719f64065bbSBarry Smith } 720bbb6d6a8SBarry Smith 7219e25ed09SBarry Smith /* put together the new matrix */ 722b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr); 723416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 7240452661fSBarry Smith PetscFree(b->imax); 725416022c9SBarry Smith b->singlemalloc = 0; 726dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 7279e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 7280452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 7296b96ef82SSatish Balay b->a = (Scalar *) PetscMalloc( len+1 ); CHKPTRQ(b->a); 730416022c9SBarry Smith b->j = ajnew; 731416022c9SBarry Smith b->i = ainew; 73256beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 733416022c9SBarry Smith b->diag = dloc; 734416022c9SBarry Smith b->ilen = 0; 735416022c9SBarry Smith b->imax = 0; 736416022c9SBarry Smith b->row = isrow; 737416022c9SBarry Smith b->col = iscol; 7380452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 739416022c9SBarry Smith CHKPTRQ(b->solve_work); 740416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 74156beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 742dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 743416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 7449e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 745ae068f58SLois Curfman McInnes 746ae068f58SLois Curfman McInnes (*fact)->info.factor_mallocs = realloc; 747ae068f58SLois Curfman McInnes (*fact)->info.fill_ratio_given = f; 748ae068f58SLois Curfman McInnes (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]); 749ae068f58SLois Curfman McInnes 750*3a40ed3dSBarry Smith PetscFunctionReturn(0); 7519e25ed09SBarry Smith } 752d5d45c9bSBarry Smith 753d5d45c9bSBarry Smith 754d5d45c9bSBarry Smith 755d5d45c9bSBarry Smith 756