1cb512458SBarry Smith #ifndef lint 2*90f02eecSBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.67 1996/09/14 03:07:52 bsmith Exp bsmith $"; 3cb512458SBarry Smith #endif 4289bc588SBarry Smith 570f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h" 6*90f02eecSBarry Smith #include "src/vec/vecimpl.h" 73b2fbd54SBarry Smith 83b2fbd54SBarry Smith int MatOrder_Flow_SeqAIJ(Mat mat,MatReordering type,IS *irow,IS *icol) 93b2fbd54SBarry Smith { 103b2fbd54SBarry Smith SETERRQ(1,"MatOrder_Flow_SeqAIJ:Code not written"); 113b2fbd54SBarry Smith } 123b2fbd54SBarry Smith 13289bc588SBarry Smith /* 14289bc588SBarry Smith Factorization code for AIJ format. 15289bc588SBarry Smith */ 16416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 17289bc588SBarry Smith { 18416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 19289bc588SBarry Smith IS isicol; 20416022c9SBarry Smith int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 21416022c9SBarry Smith int *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift; 222fb3ed76SBarry Smith int *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im; 23289bc588SBarry Smith 24d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(isrow,IS_COOKIE); 25d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(iscol,IS_COOKIE); 263b2fbd54SBarry Smith 2778b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 28289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 29289bc588SBarry Smith 30289bc588SBarry Smith /* get new row pointers */ 310452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 32dbb450caSBarry Smith ainew[0] = -shift; 33289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 34dbb450caSBarry Smith jmax = (int) (f*ai[n]+(!shift)); 350452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 36289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 370452661fSBarry Smith fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 382fb3ed76SBarry Smith im = fill + n + 1; 39289bc588SBarry Smith /* idnew is location of diagonal in factor */ 400452661fSBarry Smith idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew); 41dbb450caSBarry Smith idnew[0] = -shift; 42289bc588SBarry Smith 43289bc588SBarry Smith for ( i=0; i<n; i++ ) { 44289bc588SBarry Smith /* first copy previous fill into linked list */ 45289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 46dbb450caSBarry Smith ajtmp = aj + ai[r[i]] + shift; 47289bc588SBarry Smith fill[n] = n; 48289bc588SBarry Smith while (nz--) { 49289bc588SBarry Smith fm = n; 50dbb450caSBarry Smith idx = ic[*ajtmp++ + shift]; 51289bc588SBarry Smith do { 52289bc588SBarry Smith m = fm; 53289bc588SBarry Smith fm = fill[m]; 54289bc588SBarry Smith } while (fm < idx); 55289bc588SBarry Smith fill[m] = idx; 56289bc588SBarry Smith fill[idx] = fm; 57289bc588SBarry Smith } 58289bc588SBarry Smith row = fill[n]; 59289bc588SBarry Smith while ( row < i ) { 60dbb450caSBarry Smith ajtmp = ajnew + idnew[row] + (!shift); 612fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 622fb3ed76SBarry Smith nz = im[row] - nzbd; 63289bc588SBarry Smith fm = row; 642fb3ed76SBarry Smith while (nz-- > 0) { 65dbb450caSBarry Smith idx = *ajtmp++ + shift; 662fb3ed76SBarry Smith nzbd++; 672fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 68289bc588SBarry Smith do { 69289bc588SBarry Smith m = fm; 70289bc588SBarry Smith fm = fill[m]; 71289bc588SBarry Smith } while (fm < idx); 72289bc588SBarry Smith if (fm != idx) { 73289bc588SBarry Smith fill[m] = idx; 74289bc588SBarry Smith fill[idx] = fm; 75289bc588SBarry Smith fm = idx; 76289bc588SBarry Smith nnz++; 77289bc588SBarry Smith } 78289bc588SBarry Smith } 79289bc588SBarry Smith row = fill[row]; 80289bc588SBarry Smith } 81289bc588SBarry Smith /* copy new filled row into permanent storage */ 82289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 83496e697eSBarry Smith if (ainew[i+1] > jmax) { 84289bc588SBarry Smith /* allocate a longer ajnew */ 852fb3ed76SBarry Smith int maxadd; 86dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); 87bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 882fb3ed76SBarry Smith jmax += maxadd; 890452661fSBarry Smith ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp); 90416022c9SBarry Smith PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int)); 910452661fSBarry Smith PetscFree(ajnew); 92289bc588SBarry Smith ajnew = ajtmp; 932fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 94289bc588SBarry Smith } 95dbb450caSBarry Smith ajtmp = ajnew + ainew[i] + shift; 96289bc588SBarry Smith fm = fill[n]; 97289bc588SBarry Smith nzi = 0; 982fb3ed76SBarry Smith im[i] = nnz; 99289bc588SBarry Smith while (nnz--) { 100289bc588SBarry Smith if (fm < i) nzi++; 101dbb450caSBarry Smith *ajtmp++ = fm - shift; 102289bc588SBarry Smith fm = fill[fm]; 103289bc588SBarry Smith } 104289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 105289bc588SBarry Smith } 106289bc588SBarry Smith 10794a424c1SBarry Smith PLogInfo(A, 108ec8511deSBarry Smith "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 109bbb6d6a8SBarry Smith realloc,f,((double)ainew[n])/((double)ai[i])); 1102fb3ed76SBarry Smith 111898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 112898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 1131987afe7SBarry Smith 1140452661fSBarry Smith PetscFree(fill); 115289bc588SBarry Smith 116289bc588SBarry Smith /* put together the new matrix */ 117b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr); 1181987afe7SBarry Smith PLogObjectParent(*B,isicol); 1191987afe7SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 120416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 1210452661fSBarry Smith PetscFree(b->imax); 122416022c9SBarry Smith b->singlemalloc = 0; 123dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 124e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 1250452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 1260452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 127416022c9SBarry Smith b->j = ajnew; 128416022c9SBarry Smith b->i = ainew; 129416022c9SBarry Smith b->diag = idnew; 130416022c9SBarry Smith b->ilen = 0; 131416022c9SBarry Smith b->imax = 0; 132416022c9SBarry Smith b->row = isrow; 133416022c9SBarry Smith b->col = iscol; 1340452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar)); 135416022c9SBarry Smith CHKPTRQ(b->solve_work); 136416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 1377fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 138416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 139416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 1407fd98bd6SLois Curfman McInnes 141ae068f58SLois Curfman McInnes (*B)->info.factor_mallocs = realloc; 142ae068f58SLois Curfman McInnes (*B)->info.fill_ratio_given = f; 143ae068f58SLois Curfman McInnes (*B)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[i]); 144ae068f58SLois Curfman McInnes 145289bc588SBarry Smith return 0; 146289bc588SBarry Smith } 1470a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 14841c01911SSatish Balay int Mat_AIJ_CheckInode(Mat); 14941c01911SSatish Balay 150416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 151289bc588SBarry Smith { 152416022c9SBarry Smith Mat C = *B; 153416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 154416022c9SBarry Smith IS iscol = b->col, isrow = b->row, isicol; 155416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 1561987afe7SBarry Smith int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 15735aab85fSBarry Smith int *diag_offset = b->diag,diag,k; 15835aab85fSBarry Smith int preserve_row_sums = (int) a->ilu_preserve_row_sums; 1598ecf7165SBarry Smith Scalar *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0; 16035aab85fSBarry Smith double ssum; 1611987afe7SBarry Smith /* These declarations are for optimizations. They reduce the number of 1621987afe7SBarry Smith memory references that are made by locally storing information; the 1631987afe7SBarry Smith word "register" used here with pointers can be viewed as "private" or 1641987afe7SBarry Smith "known only to me" 1651987afe7SBarry Smith */ 16635aab85fSBarry Smith register Scalar *pv, *rtmps,*u_values; 1671987afe7SBarry Smith register int *pj; 168289bc588SBarry Smith 16978b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 170416022c9SBarry Smith PLogObjectParent(*B,isicol); 17178b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 17278b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 1730452661fSBarry Smith rtmp = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 17413ae1565SBarry Smith PetscMemzero(rtmp,(n+1)*sizeof(Scalar)); 1750cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 176289bc588SBarry Smith 17735aab85fSBarry Smith /* precalcuate row sums */ 17835aab85fSBarry Smith if (preserve_row_sums) { 17935aab85fSBarry Smith rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums); 18035aab85fSBarry Smith for ( i=0; i<n; i++ ) { 18135aab85fSBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 18235aab85fSBarry Smith v = a->a + a->i[r[i]] + shift; 18335aab85fSBarry Smith sum = 0.0; 18435aab85fSBarry Smith for ( j=0; j<nz; j++ ) sum += v[j]; 18535aab85fSBarry Smith rowsums[i] = sum; 18635aab85fSBarry Smith } 18735aab85fSBarry Smith } 18835aab85fSBarry Smith 189289bc588SBarry Smith for ( i=0; i<n; i++ ) { 190289bc588SBarry Smith nz = ai[i+1] - ai[i]; 191dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 19248e94559SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 193289bc588SBarry Smith 194289bc588SBarry Smith /* load in initial (unfactored row) */ 195416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 196416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 197416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1980cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 199289bc588SBarry Smith 200dbb450caSBarry Smith row = *ajtmp++ + shift; 201289bc588SBarry Smith while (row < i) { 2028c37ef55SBarry Smith pc = rtmp + row; 2038c37ef55SBarry Smith if (*pc != 0.0) { 2041987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 2051987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 20635aab85fSBarry Smith multiplier = *pc / *pv++; 2078c37ef55SBarry Smith *pc = multiplier; 2081987afe7SBarry Smith nz = ai[row+1] - diag_offset[row] - 1; 20935aab85fSBarry Smith for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 210eef2e97bSBarry Smith PLogFlops(2*nz); 211289bc588SBarry Smith } 212dbb450caSBarry Smith row = *ajtmp++ + shift; 213289bc588SBarry Smith } 214416022c9SBarry Smith /* finished row so stick it into b->a */ 215416022c9SBarry Smith pv = b->a + ai[i] + shift; 216416022c9SBarry Smith pj = b->j + ai[i] + shift; 2178c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 218416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 21935aab85fSBarry Smith diag = diag_offset[i] - ai[i]; 22035aab85fSBarry Smith /* 22135aab85fSBarry Smith Possibly adjust diagonal entry on current row to force 22235aab85fSBarry Smith LU matrix to have same row sum as initial matrix. 22335aab85fSBarry Smith */ 22435aab85fSBarry Smith if (preserve_row_sums) { 22535aab85fSBarry Smith pj = b->j + ai[i] + shift; 22635aab85fSBarry Smith sum = rowsums[i]; 22735aab85fSBarry Smith for ( j=0; j<diag; j++ ) { 22835aab85fSBarry Smith u_values = b->a + diag_offset[pj[j]] + shift; 22935aab85fSBarry Smith nz = ai[pj[j]+1] - diag_offset[pj[j]]; 23035aab85fSBarry Smith inner_sum = 0.0; 23135aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 23235aab85fSBarry Smith inner_sum += u_values[k]; 23335aab85fSBarry Smith } 23435aab85fSBarry Smith sum -= pv[j]*inner_sum; 23535aab85fSBarry Smith 23635aab85fSBarry Smith } 23735aab85fSBarry Smith nz = ai[i+1] - diag_offset[i] - 1; 23835aab85fSBarry Smith u_values = b->a + diag_offset[i] + 1 + shift; 23935aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 24035aab85fSBarry Smith sum -= u_values[k]; 24135aab85fSBarry Smith } 24235aab85fSBarry Smith ssum = PetscAbsScalar(sum/pv[diag]); 24335aab85fSBarry Smith if (ssum < 1000. && ssum > .001) pv[diag] = sum; 24435aab85fSBarry Smith } 24535aab85fSBarry Smith /* check pivot entry for current row */ 24635aab85fSBarry Smith if (pv[diag] == 0.0) { 24735aab85fSBarry Smith SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot"); 24835aab85fSBarry Smith } 2498c37ef55SBarry Smith } 2500f11f9aeSSatish Balay 25135aab85fSBarry Smith /* invert diagonal entries for simplier triangular solves */ 25235aab85fSBarry Smith for ( i=0; i<n; i++ ) { 25335aab85fSBarry Smith b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 25435aab85fSBarry Smith } 25535aab85fSBarry Smith 25635aab85fSBarry Smith if (preserve_row_sums) PetscFree(rowsums); 2570452661fSBarry Smith PetscFree(rtmp); 25878b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 25978b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 26078b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 261416022c9SBarry Smith C->factor = FACTOR_LU; 26241c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 263c456f294SBarry Smith C->assembled = PETSC_TRUE; 264416022c9SBarry Smith PLogFlops(b->n); 265289bc588SBarry Smith return 0; 266289bc588SBarry Smith } 2670a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 268416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 269da3a660dSBarry Smith { 270416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2716abc6512SBarry Smith int ierr; 272416022c9SBarry Smith Mat C; 273416022c9SBarry Smith 274416022c9SBarry Smith ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 275416022c9SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 276da3a660dSBarry Smith 277da3a660dSBarry Smith /* free all the data structures from mat */ 2780452661fSBarry Smith PetscFree(mat->a); 2790452661fSBarry Smith if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 2800452661fSBarry Smith if (mat->diag) PetscFree(mat->diag); 2810452661fSBarry Smith if (mat->ilen) PetscFree(mat->ilen); 2820452661fSBarry Smith if (mat->imax) PetscFree(mat->imax); 2830452661fSBarry Smith if (mat->solve_work) PetscFree(mat->solve_work); 284a7a49059SBarry Smith if (mat->inode.size) PetscFree(mat->inode.size); 2850452661fSBarry Smith PetscFree(mat); 286da3a660dSBarry Smith 287416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 2880452661fSBarry Smith PetscHeaderDestroy(C); 289da3a660dSBarry Smith return 0; 290da3a660dSBarry Smith } 2910a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 292416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2938c37ef55SBarry Smith { 294416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 295416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 296416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 2973b2fbd54SBarry Smith int nz,shift = a->indexshift,*rout,*cout; 298416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2998c37ef55SBarry Smith 300*90f02eecSBarry Smith VecGetArray_Fast(bb,b); 301*90f02eecSBarry Smith VecGetArray_Fast(xx,x); 302416022c9SBarry Smith tmp = a->solve_work; 3038c37ef55SBarry Smith 3043b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout; 3053b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1); 3068c37ef55SBarry Smith 3078c37ef55SBarry Smith /* forward solve the lower triangular */ 3088c37ef55SBarry Smith tmp[0] = b[*r++]; 3090cf60383SBarry Smith tmps = tmp + shift; 3108c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 311dbb450caSBarry Smith v = aa + ai[i] + shift; 312dbb450caSBarry Smith vi = aj + ai[i] + shift; 313416022c9SBarry Smith nz = a->diag[i] - ai[i]; 3148c37ef55SBarry Smith sum = b[*r++]; 3150cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 3168c37ef55SBarry Smith tmp[i] = sum; 3178c37ef55SBarry Smith } 3188c37ef55SBarry Smith 3198c37ef55SBarry Smith /* backward solve the upper triangular */ 3208c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 321416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 322416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 323416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 3248c37ef55SBarry Smith sum = tmp[i]; 3250cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 326416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 3278c37ef55SBarry Smith } 3288c37ef55SBarry Smith 3293b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 3303b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 331416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 3328c37ef55SBarry Smith return 0; 3338c37ef55SBarry Smith } 334416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 335da3a660dSBarry Smith { 336416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 337416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 338416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 3393b2fbd54SBarry Smith int nz, shift = a->indexshift,*rout,*cout; 340416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 341da3a660dSBarry Smith 34278b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 343da3a660dSBarry Smith 344*90f02eecSBarry Smith VecGetArray_Fast(bb,b); 345*90f02eecSBarry Smith VecGetArray_Fast(xx,x); 346416022c9SBarry Smith tmp = a->solve_work; 347da3a660dSBarry Smith 3483b2fbd54SBarry Smith ierr = ISGetIndices(isrow,&rout); CHKERRQ(ierr); r = rout; 3493b2fbd54SBarry Smith ierr = ISGetIndices(iscol,&cout); CHKERRQ(ierr); c = cout + (n-1); 350da3a660dSBarry Smith 351da3a660dSBarry Smith /* forward solve the lower triangular */ 352da3a660dSBarry Smith tmp[0] = b[*r++]; 353da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 354dbb450caSBarry Smith v = aa + ai[i] + shift; 355dbb450caSBarry Smith vi = aj + ai[i] + shift; 356416022c9SBarry Smith nz = a->diag[i] - ai[i]; 357da3a660dSBarry Smith sum = b[*r++]; 358dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 359da3a660dSBarry Smith tmp[i] = sum; 360da3a660dSBarry Smith } 361da3a660dSBarry Smith 362da3a660dSBarry Smith /* backward solve the upper triangular */ 363da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 364416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 365416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 366416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 367da3a660dSBarry Smith sum = tmp[i]; 368dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 369416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 370da3a660dSBarry Smith x[*c--] += tmp[i]; 371da3a660dSBarry Smith } 372da3a660dSBarry Smith 3733b2fbd54SBarry Smith ierr = ISRestoreIndices(isrow,&rout); CHKERRQ(ierr); 3743b2fbd54SBarry Smith ierr = ISRestoreIndices(iscol,&cout); CHKERRQ(ierr); 375416022c9SBarry Smith PLogFlops(2*a->nz); 376898183ecSLois Curfman McInnes 377da3a660dSBarry Smith return 0; 378da3a660dSBarry Smith } 379da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 380416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 381da3a660dSBarry Smith { 382416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 383416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 384416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 3853b2fbd54SBarry Smith int nz,shift = a->indexshift,*rout,*cout; 386416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 387da3a660dSBarry Smith 388*90f02eecSBarry Smith VecGetArray_Fast(bb,b); 389*90f02eecSBarry Smith VecGetArray_Fast(xx,x); 390416022c9SBarry Smith tmp = a->solve_work; 391da3a660dSBarry Smith 392da3a660dSBarry Smith /* invert the permutations */ 39378b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 39478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 395da3a660dSBarry Smith 3963b2fbd54SBarry Smith ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout; 3973b2fbd54SBarry Smith ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout; 398da3a660dSBarry Smith 399da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 400da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 401da3a660dSBarry Smith 402da3a660dSBarry Smith /* forward solve the U^T */ 403da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 404416022c9SBarry Smith v = aa + a->diag[i] + shift; 405416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 406416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 407da3a660dSBarry Smith tmp[i] *= *v++; 408da3a660dSBarry Smith while (nz--) { 409dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 410da3a660dSBarry Smith } 411da3a660dSBarry Smith } 412da3a660dSBarry Smith 413da3a660dSBarry Smith /* backward solve the L^T */ 414da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 415416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 416416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 417416022c9SBarry Smith nz = a->diag[i] - ai[i]; 418da3a660dSBarry Smith while (nz--) { 419dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 420da3a660dSBarry Smith } 421da3a660dSBarry Smith } 422da3a660dSBarry Smith 423da3a660dSBarry Smith /* copy tmp into x according to permutation */ 424da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 425da3a660dSBarry Smith 4263b2fbd54SBarry Smith ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr); 4273b2fbd54SBarry Smith ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr); 428898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 429898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 430da3a660dSBarry Smith 431416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 432da3a660dSBarry Smith return 0; 433da3a660dSBarry Smith } 434da3a660dSBarry Smith 435416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 436da3a660dSBarry Smith { 437416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 438416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 439416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 4403b2fbd54SBarry Smith int nz,shift = a->indexshift, *rout, *cout; 441416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 4426abc6512SBarry Smith 4436abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 4446abc6512SBarry Smith 445*90f02eecSBarry Smith VecGetArray_Fast(bb,b); 446*90f02eecSBarry Smith VecGetArray_Fast(xx,x); 447416022c9SBarry Smith tmp = a->solve_work; 4486abc6512SBarry Smith 4496abc6512SBarry Smith /* invert the permutations */ 45078b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 45178b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 4523b2fbd54SBarry Smith ierr = ISGetIndices(invisrow,&rout); CHKERRQ(ierr); r = rout; 4533b2fbd54SBarry Smith ierr = ISGetIndices(inviscol,&cout); CHKERRQ(ierr); c = cout; 4546abc6512SBarry Smith 4556abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 4566abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4576abc6512SBarry Smith 4586abc6512SBarry Smith /* forward solve the U^T */ 4596abc6512SBarry Smith for ( i=0; i<n; i++ ) { 460416022c9SBarry Smith v = aa + a->diag[i] + shift; 461416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 462416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 4636abc6512SBarry Smith tmp[i] *= *v++; 4646abc6512SBarry Smith while (nz--) { 465dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4666abc6512SBarry Smith } 4676abc6512SBarry Smith } 4686abc6512SBarry Smith 4696abc6512SBarry Smith /* backward solve the L^T */ 4706abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 471416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 472416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 473416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4746abc6512SBarry Smith while (nz--) { 475dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4766abc6512SBarry Smith } 4776abc6512SBarry Smith } 4786abc6512SBarry Smith 4796abc6512SBarry Smith /* copy tmp into x according to permutation */ 4806abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4816abc6512SBarry Smith 4823b2fbd54SBarry Smith ierr = ISRestoreIndices(invisrow,&rout); CHKERRQ(ierr); 4833b2fbd54SBarry Smith ierr = ISRestoreIndices(inviscol,&cout); CHKERRQ(ierr); 484898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 485898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4866abc6512SBarry Smith 487416022c9SBarry Smith PLogFlops(2*a->nz); 4886abc6512SBarry Smith return 0; 489da3a660dSBarry Smith } 4909e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 49108480c60SBarry Smith 49208480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 4939e25ed09SBarry Smith { 494416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4959e25ed09SBarry Smith IS isicol; 496416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 49756beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 49856beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 499416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 50077c4ece6SBarry Smith PetscTruth col_identity, row_identity; 5019e25ed09SBarry Smith 50208480c60SBarry Smith /* special case that simply copies fill pattern */ 50377c4ece6SBarry Smith ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity); 50477c4ece6SBarry Smith if (levels == 0 && row_identity && col_identity) { 5053d1612f7SBarry Smith ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 50608480c60SBarry Smith (*fact)->factor = FACTOR_LU; 50708480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 50808480c60SBarry Smith if (!b->diag) { 50908480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 51008480c60SBarry Smith } 51108480c60SBarry Smith b->row = isrow; 51208480c60SBarry Smith b->col = iscol; 5130452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 51408480c60SBarry Smith return 0; 51508480c60SBarry Smith } 51608480c60SBarry Smith 51778b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 518898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 519898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 5209e25ed09SBarry Smith 5219e25ed09SBarry Smith /* get new row pointers */ 5220452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 523dbb450caSBarry Smith ainew[0] = -shift; 5249e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 525dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 5260452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 5279e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 5280452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 5299e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 5300452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 53156beaf04SBarry Smith /* im is level for each filled value */ 5320452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 53356beaf04SBarry Smith /* dloc is location of diagonal in factor */ 5340452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 53556beaf04SBarry Smith dloc[0] = 0; 53656beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 5379e25ed09SBarry Smith /* first copy previous fill into linked list */ 53856beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 539dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 5409e25ed09SBarry Smith fill[n] = n; 5419e25ed09SBarry Smith while (nz--) { 5429e25ed09SBarry Smith fm = n; 543dbb450caSBarry Smith idx = ic[*xi++ + shift]; 5449e25ed09SBarry Smith do { 5459e25ed09SBarry Smith m = fm; 5469e25ed09SBarry Smith fm = fill[m]; 5479e25ed09SBarry Smith } while (fm < idx); 5489e25ed09SBarry Smith fill[m] = idx; 5499e25ed09SBarry Smith fill[idx] = fm; 55056beaf04SBarry Smith im[idx] = 0; 5519e25ed09SBarry Smith } 55256beaf04SBarry Smith nzi = 0; 5539e25ed09SBarry Smith row = fill[n]; 55456beaf04SBarry Smith while ( row < prow ) { 55556beaf04SBarry Smith incrlev = im[row] + 1; 55656beaf04SBarry Smith nz = dloc[row]; 557dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 558dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 559416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 560dbb450caSBarry Smith if (*xi++ + shift != row) { 561ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 56256beaf04SBarry Smith } 56356beaf04SBarry Smith fm = row; 56456beaf04SBarry Smith while (nnz-- > 0) { 565dbb450caSBarry Smith idx = *xi++ + shift; 56656beaf04SBarry Smith if (*flev + incrlev > levels) { 56756beaf04SBarry Smith flev++; 56856beaf04SBarry Smith continue; 56956beaf04SBarry Smith } 57056beaf04SBarry Smith do { 5719e25ed09SBarry Smith m = fm; 5729e25ed09SBarry Smith fm = fill[m]; 57356beaf04SBarry Smith } while (fm < idx); 5749e25ed09SBarry Smith if (fm != idx) { 57556beaf04SBarry Smith im[idx] = *flev + incrlev; 5769e25ed09SBarry Smith fill[m] = idx; 5779e25ed09SBarry Smith fill[idx] = fm; 5789e25ed09SBarry Smith fm = idx; 57956beaf04SBarry Smith nzf++; 5809e25ed09SBarry Smith } 58156beaf04SBarry Smith else { 58256beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5839e25ed09SBarry Smith } 58456beaf04SBarry Smith flev++; 5859e25ed09SBarry Smith } 5869e25ed09SBarry Smith row = fill[row]; 58756beaf04SBarry Smith nzi++; 5889e25ed09SBarry Smith } 5899e25ed09SBarry Smith /* copy new filled row into permanent storage */ 59056beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 591d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 5929e25ed09SBarry Smith /* allocate a longer ajnew */ 593bbb6d6a8SBarry Smith int maxadd; 594dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 59556beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 596bbb6d6a8SBarry Smith jmax += maxadd; 5970452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 598416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 5990452661fSBarry Smith PetscFree(ajnew); 60056beaf04SBarry Smith ajnew = xi; 6019e25ed09SBarry Smith /* allocate a longer ajfill */ 6020452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 603416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 6040452661fSBarry Smith PetscFree(ajfill); 60556beaf04SBarry Smith ajfill = xi; 606bbb6d6a8SBarry Smith realloc++; 6079e25ed09SBarry Smith } 608dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 609dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 61056beaf04SBarry Smith dloc[prow] = nzi; 6119e25ed09SBarry Smith fm = fill[n]; 61256beaf04SBarry Smith while (nzf--) { 613dbb450caSBarry Smith *xi++ = fm - shift; 61456beaf04SBarry Smith *flev++ = im[fm]; 6159e25ed09SBarry Smith fm = fill[fm]; 6169e25ed09SBarry Smith } 6179e25ed09SBarry Smith } 6180452661fSBarry Smith PetscFree(ajfill); 619898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 620898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 621898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 6220452661fSBarry Smith PetscFree(fill); PetscFree(im); 6239e25ed09SBarry Smith 62494a424c1SBarry Smith PLogInfo(A, 625ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 62656beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 627bbb6d6a8SBarry Smith 6289e25ed09SBarry Smith /* put together the new matrix */ 629b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr); 630416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 6310452661fSBarry Smith PetscFree(b->imax); 632416022c9SBarry Smith b->singlemalloc = 0; 633dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 6349e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 6350452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 6360452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 637416022c9SBarry Smith b->j = ajnew; 638416022c9SBarry Smith b->i = ainew; 63956beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 640416022c9SBarry Smith b->diag = dloc; 641416022c9SBarry Smith b->ilen = 0; 642416022c9SBarry Smith b->imax = 0; 643416022c9SBarry Smith b->row = isrow; 644416022c9SBarry Smith b->col = iscol; 6450452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 646416022c9SBarry Smith CHKPTRQ(b->solve_work); 647416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 64856beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 649dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 650416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 6519e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 652ae068f58SLois Curfman McInnes 653ae068f58SLois Curfman McInnes (*fact)->info.factor_mallocs = realloc; 654ae068f58SLois Curfman McInnes (*fact)->info.fill_ratio_given = f; 655ae068f58SLois Curfman McInnes (*fact)->info.fill_ratio_needed = ((double)ainew[n])/((double)ai[prow]); 656ae068f58SLois Curfman McInnes 6579e25ed09SBarry Smith return 0; 6589e25ed09SBarry Smith } 659d5d45c9bSBarry Smith 660d5d45c9bSBarry Smith 661d5d45c9bSBarry Smith 662d5d45c9bSBarry Smith 663