1cb512458SBarry Smith #ifndef lint 2*13ae1565SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.61 1996/05/03 19:26:39 bsmith Exp bsmith $"; 3cb512458SBarry Smith #endif 4289bc588SBarry Smith 5289bc588SBarry Smith #include "aij.h" 6289bc588SBarry Smith /* 7289bc588SBarry Smith Factorization code for AIJ format. 8289bc588SBarry Smith */ 9289bc588SBarry Smith 10416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 11289bc588SBarry Smith { 12416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 13289bc588SBarry Smith IS isicol; 14416022c9SBarry Smith int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 15416022c9SBarry Smith int *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift; 162fb3ed76SBarry Smith int *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im; 17289bc588SBarry Smith 18416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must be square"); 19416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have row permutation"); 20227d817aSBarry Smith if (!iscol) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have col. permutation"); 21289bc588SBarry Smith 2278b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 23289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 24289bc588SBarry Smith 25289bc588SBarry Smith /* get new row pointers */ 260452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 27dbb450caSBarry Smith ainew[0] = -shift; 28289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 29dbb450caSBarry Smith jmax = (int) (f*ai[n]+(!shift)); 300452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 31289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 320452661fSBarry Smith fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 332fb3ed76SBarry Smith im = fill + n + 1; 34289bc588SBarry Smith /* idnew is location of diagonal in factor */ 350452661fSBarry Smith idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew); 36dbb450caSBarry Smith idnew[0] = -shift; 37289bc588SBarry Smith 38289bc588SBarry Smith for ( i=0; i<n; i++ ) { 39289bc588SBarry Smith /* first copy previous fill into linked list */ 40289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 41dbb450caSBarry Smith ajtmp = aj + ai[r[i]] + shift; 42289bc588SBarry Smith fill[n] = n; 43289bc588SBarry Smith while (nz--) { 44289bc588SBarry Smith fm = n; 45dbb450caSBarry Smith idx = ic[*ajtmp++ + shift]; 46289bc588SBarry Smith do { 47289bc588SBarry Smith m = fm; 48289bc588SBarry Smith fm = fill[m]; 49289bc588SBarry Smith } while (fm < idx); 50289bc588SBarry Smith fill[m] = idx; 51289bc588SBarry Smith fill[idx] = fm; 52289bc588SBarry Smith } 53289bc588SBarry Smith row = fill[n]; 54289bc588SBarry Smith while ( row < i ) { 55dbb450caSBarry Smith ajtmp = ajnew + idnew[row] + (!shift); 562fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 572fb3ed76SBarry Smith nz = im[row] - nzbd; 58289bc588SBarry Smith fm = row; 592fb3ed76SBarry Smith while (nz-- > 0) { 60dbb450caSBarry Smith idx = *ajtmp++ + shift; 612fb3ed76SBarry Smith nzbd++; 622fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 63289bc588SBarry Smith do { 64289bc588SBarry Smith m = fm; 65289bc588SBarry Smith fm = fill[m]; 66289bc588SBarry Smith } while (fm < idx); 67289bc588SBarry Smith if (fm != idx) { 68289bc588SBarry Smith fill[m] = idx; 69289bc588SBarry Smith fill[idx] = fm; 70289bc588SBarry Smith fm = idx; 71289bc588SBarry Smith nnz++; 72289bc588SBarry Smith } 73289bc588SBarry Smith } 74289bc588SBarry Smith row = fill[row]; 75289bc588SBarry Smith } 76289bc588SBarry Smith /* copy new filled row into permanent storage */ 77289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 78496e697eSBarry Smith if (ainew[i+1] > jmax) { 79289bc588SBarry Smith /* allocate a longer ajnew */ 802fb3ed76SBarry Smith int maxadd; 81dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); 82bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 832fb3ed76SBarry Smith jmax += maxadd; 840452661fSBarry Smith ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp); 85416022c9SBarry Smith PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int)); 860452661fSBarry Smith PetscFree(ajnew); 87289bc588SBarry Smith ajnew = ajtmp; 882fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 89289bc588SBarry Smith } 90dbb450caSBarry Smith ajtmp = ajnew + ainew[i] + shift; 91289bc588SBarry Smith fm = fill[n]; 92289bc588SBarry Smith nzi = 0; 932fb3ed76SBarry Smith im[i] = nnz; 94289bc588SBarry Smith while (nnz--) { 95289bc588SBarry Smith if (fm < i) nzi++; 96dbb450caSBarry Smith *ajtmp++ = fm - shift; 97289bc588SBarry Smith fm = fill[fm]; 98289bc588SBarry Smith } 99289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 100289bc588SBarry Smith } 101289bc588SBarry Smith 10294a424c1SBarry Smith PLogInfo(A, 103ec8511deSBarry Smith "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 104bbb6d6a8SBarry Smith realloc,f,((double)ainew[n])/((double)ai[i])); 1052fb3ed76SBarry Smith 106898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 107898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 1081987afe7SBarry Smith 1090452661fSBarry Smith PetscFree(fill); 110289bc588SBarry Smith 111289bc588SBarry Smith /* put together the new matrix */ 112b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr); 1131987afe7SBarry Smith PLogObjectParent(*B,isicol); 1141987afe7SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 115416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 1160452661fSBarry Smith PetscFree(b->imax); 117416022c9SBarry Smith b->singlemalloc = 0; 118dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 119e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 1200452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 1210452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 122416022c9SBarry Smith b->j = ajnew; 123416022c9SBarry Smith b->i = ainew; 124416022c9SBarry Smith b->diag = idnew; 125416022c9SBarry Smith b->ilen = 0; 126416022c9SBarry Smith b->imax = 0; 127416022c9SBarry Smith b->row = isrow; 128416022c9SBarry Smith b->col = iscol; 1290452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar)); 130416022c9SBarry Smith CHKPTRQ(b->solve_work); 131416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 1327fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 133416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 134416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 1357fd98bd6SLois Curfman McInnes 136289bc588SBarry Smith return 0; 137289bc588SBarry Smith } 1380a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 13941c01911SSatish Balay int Mat_AIJ_CheckInode(Mat); 14041c01911SSatish Balay 141416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 142289bc588SBarry Smith { 143416022c9SBarry Smith Mat C = *B; 144416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 145416022c9SBarry Smith IS iscol = b->col, isrow = b->row, isicol; 146416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 1471987afe7SBarry Smith int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 14835aab85fSBarry Smith int *diag_offset = b->diag,diag,k; 14935aab85fSBarry Smith int preserve_row_sums = (int) a->ilu_preserve_row_sums; 1508ecf7165SBarry Smith Scalar *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0; 15135aab85fSBarry Smith double ssum; 1521987afe7SBarry Smith /* These declarations are for optimizations. They reduce the number of 1531987afe7SBarry Smith memory references that are made by locally storing information; the 1541987afe7SBarry Smith word "register" used here with pointers can be viewed as "private" or 1551987afe7SBarry Smith "known only to me" 1561987afe7SBarry Smith */ 15735aab85fSBarry Smith register Scalar *pv, *rtmps,*u_values; 1581987afe7SBarry Smith register int *pj; 159289bc588SBarry Smith 16078b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 161416022c9SBarry Smith PLogObjectParent(*B,isicol); 16278b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 16378b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 1640452661fSBarry Smith rtmp = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 165*13ae1565SBarry Smith PetscMemzero(rtmp,(n+1)*sizeof(Scalar)); 1660cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 167289bc588SBarry Smith 16835aab85fSBarry Smith /* precalcuate row sums */ 16935aab85fSBarry Smith if (preserve_row_sums) { 17035aab85fSBarry Smith rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums); 17135aab85fSBarry Smith for ( i=0; i<n; i++ ) { 17235aab85fSBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 17335aab85fSBarry Smith v = a->a + a->i[r[i]] + shift; 17435aab85fSBarry Smith sum = 0.0; 17535aab85fSBarry Smith for ( j=0; j<nz; j++ ) sum += v[j]; 17635aab85fSBarry Smith rowsums[i] = sum; 17735aab85fSBarry Smith } 17835aab85fSBarry Smith } 17935aab85fSBarry Smith 180289bc588SBarry Smith for ( i=0; i<n; i++ ) { 181289bc588SBarry Smith nz = ai[i+1] - ai[i]; 182dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 18348e94559SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 184289bc588SBarry Smith 185289bc588SBarry Smith /* load in initial (unfactored row) */ 186416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 187416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 188416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1890cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 190289bc588SBarry Smith 191dbb450caSBarry Smith row = *ajtmp++ + shift; 192289bc588SBarry Smith while (row < i) { 1938c37ef55SBarry Smith pc = rtmp + row; 1948c37ef55SBarry Smith if (*pc != 0.0) { 1951987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 1961987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 19735aab85fSBarry Smith multiplier = *pc / *pv++; 1988c37ef55SBarry Smith *pc = multiplier; 1991987afe7SBarry Smith nz = ai[row+1] - diag_offset[row] - 1; 20035aab85fSBarry Smith for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 201eef2e97bSBarry Smith PLogFlops(2*nz); 202289bc588SBarry Smith } 203dbb450caSBarry Smith row = *ajtmp++ + shift; 204289bc588SBarry Smith } 205416022c9SBarry Smith /* finished row so stick it into b->a */ 206416022c9SBarry Smith pv = b->a + ai[i] + shift; 207416022c9SBarry Smith pj = b->j + ai[i] + shift; 2088c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 209416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 21035aab85fSBarry Smith diag = diag_offset[i] - ai[i]; 21135aab85fSBarry Smith /* 21235aab85fSBarry Smith Possibly adjust diagonal entry on current row to force 21335aab85fSBarry Smith LU matrix to have same row sum as initial matrix. 21435aab85fSBarry Smith */ 21535aab85fSBarry Smith if (preserve_row_sums) { 21635aab85fSBarry Smith pj = b->j + ai[i] + shift; 21735aab85fSBarry Smith sum = rowsums[i]; 21835aab85fSBarry Smith for ( j=0; j<diag; j++ ) { 21935aab85fSBarry Smith u_values = b->a + diag_offset[pj[j]] + shift; 22035aab85fSBarry Smith nz = ai[pj[j]+1] - diag_offset[pj[j]]; 22135aab85fSBarry Smith inner_sum = 0.0; 22235aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 22335aab85fSBarry Smith inner_sum += u_values[k]; 22435aab85fSBarry Smith } 22535aab85fSBarry Smith sum -= pv[j]*inner_sum; 22635aab85fSBarry Smith 22735aab85fSBarry Smith } 22835aab85fSBarry Smith nz = ai[i+1] - diag_offset[i] - 1; 22935aab85fSBarry Smith u_values = b->a + diag_offset[i] + 1 + shift; 23035aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 23135aab85fSBarry Smith sum -= u_values[k]; 23235aab85fSBarry Smith } 23335aab85fSBarry Smith ssum = PetscAbsScalar(sum/pv[diag]); 23435aab85fSBarry Smith if (ssum < 1000. && ssum > .001) pv[diag] = sum; 23535aab85fSBarry Smith } 23635aab85fSBarry Smith /* check pivot entry for current row */ 23735aab85fSBarry Smith if (pv[diag] == 0.0) { 23835aab85fSBarry Smith SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot"); 23935aab85fSBarry Smith } 2408c37ef55SBarry Smith } 2410f11f9aeSSatish Balay 24235aab85fSBarry Smith /* invert diagonal entries for simplier triangular solves */ 24335aab85fSBarry Smith for ( i=0; i<n; i++ ) { 24435aab85fSBarry Smith b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 24535aab85fSBarry Smith } 24635aab85fSBarry Smith 24735aab85fSBarry Smith if (preserve_row_sums) PetscFree(rowsums); 2480452661fSBarry Smith PetscFree(rtmp); 24978b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 25078b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 25178b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 252416022c9SBarry Smith C->factor = FACTOR_LU; 25341c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 254c456f294SBarry Smith C->assembled = PETSC_TRUE; 255416022c9SBarry Smith PLogFlops(b->n); 256289bc588SBarry Smith return 0; 257289bc588SBarry Smith } 2580a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 259416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 260da3a660dSBarry Smith { 261416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2626abc6512SBarry Smith int ierr; 263416022c9SBarry Smith Mat C; 264416022c9SBarry Smith 265416022c9SBarry Smith ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 266416022c9SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 267da3a660dSBarry Smith 268da3a660dSBarry Smith /* free all the data structures from mat */ 2690452661fSBarry Smith PetscFree(mat->a); 2700452661fSBarry Smith if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 2710452661fSBarry Smith if (mat->diag) PetscFree(mat->diag); 2720452661fSBarry Smith if (mat->ilen) PetscFree(mat->ilen); 2730452661fSBarry Smith if (mat->imax) PetscFree(mat->imax); 2740452661fSBarry Smith if (mat->solve_work) PetscFree(mat->solve_work); 275a7a49059SBarry Smith if (mat->inode.size) PetscFree(mat->inode.size); 2760452661fSBarry Smith PetscFree(mat); 277da3a660dSBarry Smith 278416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 2790452661fSBarry Smith PetscHeaderDestroy(C); 280da3a660dSBarry Smith return 0; 281da3a660dSBarry Smith } 2820a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 283416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2848c37ef55SBarry Smith { 285416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 286416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 287416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 288416022c9SBarry Smith int nz,shift = a->indexshift; 289416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2908c37ef55SBarry Smith 291416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 2929e25ed09SBarry Smith 29378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 29478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 295416022c9SBarry Smith tmp = a->solve_work; 2968c37ef55SBarry Smith 29778b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 29878b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2998c37ef55SBarry Smith 3008c37ef55SBarry Smith /* forward solve the lower triangular */ 3018c37ef55SBarry Smith tmp[0] = b[*r++]; 3020cf60383SBarry Smith tmps = tmp + shift; 3038c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 304dbb450caSBarry Smith v = aa + ai[i] + shift; 305dbb450caSBarry Smith vi = aj + ai[i] + shift; 306416022c9SBarry Smith nz = a->diag[i] - ai[i]; 3078c37ef55SBarry Smith sum = b[*r++]; 3080cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 3098c37ef55SBarry Smith tmp[i] = sum; 3108c37ef55SBarry Smith } 3118c37ef55SBarry Smith 3128c37ef55SBarry Smith /* backward solve the upper triangular */ 3138c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 314416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 315416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 316416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 3178c37ef55SBarry Smith sum = tmp[i]; 3180cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 319416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 3208c37ef55SBarry Smith } 3218c37ef55SBarry Smith 322898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 323898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 324416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 3258c37ef55SBarry Smith return 0; 3268c37ef55SBarry Smith } 327416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 328da3a660dSBarry Smith { 329416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 330416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 331416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 332416022c9SBarry Smith int nz, shift = a->indexshift; 333416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 334da3a660dSBarry Smith 335416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 33678b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 337da3a660dSBarry Smith 33878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 33978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 340416022c9SBarry Smith tmp = a->solve_work; 341da3a660dSBarry Smith 34278b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 34378b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 344da3a660dSBarry Smith 345da3a660dSBarry Smith /* forward solve the lower triangular */ 346da3a660dSBarry Smith tmp[0] = b[*r++]; 347da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 348dbb450caSBarry Smith v = aa + ai[i] + shift; 349dbb450caSBarry Smith vi = aj + ai[i] + shift; 350416022c9SBarry Smith nz = a->diag[i] - ai[i]; 351da3a660dSBarry Smith sum = b[*r++]; 352dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 353da3a660dSBarry Smith tmp[i] = sum; 354da3a660dSBarry Smith } 355da3a660dSBarry Smith 356da3a660dSBarry Smith /* backward solve the upper triangular */ 357da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 358416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 359416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 360416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 361da3a660dSBarry Smith sum = tmp[i]; 362dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 363416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 364da3a660dSBarry Smith x[*c--] += tmp[i]; 365da3a660dSBarry Smith } 366da3a660dSBarry Smith 367898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 368898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 369416022c9SBarry Smith PLogFlops(2*a->nz); 370898183ecSLois Curfman McInnes 371da3a660dSBarry Smith return 0; 372da3a660dSBarry Smith } 373da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 374416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 375da3a660dSBarry Smith { 376416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 377416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 378416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 379416022c9SBarry Smith int nz,shift = a->indexshift; 380416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 381da3a660dSBarry Smith 382416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 38378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 38478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 385416022c9SBarry Smith tmp = a->solve_work; 386da3a660dSBarry Smith 387da3a660dSBarry Smith /* invert the permutations */ 38878b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 38978b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 390da3a660dSBarry Smith 39178b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 39278b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 393da3a660dSBarry Smith 394da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 395da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 396da3a660dSBarry Smith 397da3a660dSBarry Smith /* forward solve the U^T */ 398da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 399416022c9SBarry Smith v = aa + a->diag[i] + shift; 400416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 401416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 402da3a660dSBarry Smith tmp[i] *= *v++; 403da3a660dSBarry Smith while (nz--) { 404dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 405da3a660dSBarry Smith } 406da3a660dSBarry Smith } 407da3a660dSBarry Smith 408da3a660dSBarry Smith /* backward solve the L^T */ 409da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 410416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 411416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 412416022c9SBarry Smith nz = a->diag[i] - ai[i]; 413da3a660dSBarry Smith while (nz--) { 414dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 415da3a660dSBarry Smith } 416da3a660dSBarry Smith } 417da3a660dSBarry Smith 418da3a660dSBarry Smith /* copy tmp into x according to permutation */ 419da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 420da3a660dSBarry Smith 421898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 422898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 423898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 424898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 425da3a660dSBarry Smith 426416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 427da3a660dSBarry Smith return 0; 428da3a660dSBarry Smith } 429da3a660dSBarry Smith 430416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 431da3a660dSBarry Smith { 432416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 433416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 434416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 435416022c9SBarry Smith int nz,shift = a->indexshift; 436416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 4376abc6512SBarry Smith 438416022c9SBarry Smith if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 4396abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 4406abc6512SBarry Smith 44178b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 44278b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 443416022c9SBarry Smith tmp = a->solve_work; 4446abc6512SBarry Smith 4456abc6512SBarry Smith /* invert the permutations */ 44678b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 44778b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 44878b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 44978b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 4506abc6512SBarry Smith 4516abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 4526abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4536abc6512SBarry Smith 4546abc6512SBarry Smith /* forward solve the U^T */ 4556abc6512SBarry Smith for ( i=0; i<n; i++ ) { 456416022c9SBarry Smith v = aa + a->diag[i] + shift; 457416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 458416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 4596abc6512SBarry Smith tmp[i] *= *v++; 4606abc6512SBarry Smith while (nz--) { 461dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4626abc6512SBarry Smith } 4636abc6512SBarry Smith } 4646abc6512SBarry Smith 4656abc6512SBarry Smith /* backward solve the L^T */ 4666abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 467416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 468416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 469416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4706abc6512SBarry Smith while (nz--) { 471dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4726abc6512SBarry Smith } 4736abc6512SBarry Smith } 4746abc6512SBarry Smith 4756abc6512SBarry Smith /* copy tmp into x according to permutation */ 4766abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4776abc6512SBarry Smith 478898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 479898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 480898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 481898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4826abc6512SBarry Smith 483416022c9SBarry Smith PLogFlops(2*a->nz); 4846abc6512SBarry Smith return 0; 485da3a660dSBarry Smith } 4869e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 48708480c60SBarry Smith 48808480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 4899e25ed09SBarry Smith { 490416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4919e25ed09SBarry Smith IS isicol; 492416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 49356beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 49456beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 495416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 49677c4ece6SBarry Smith PetscTruth col_identity, row_identity; 4979e25ed09SBarry Smith 498416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square"); 499416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation"); 500416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation"); 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; 6529e25ed09SBarry Smith return 0; 6539e25ed09SBarry Smith } 654d5d45c9bSBarry Smith 655d5d45c9bSBarry Smith 656d5d45c9bSBarry Smith 657d5d45c9bSBarry Smith 658