1cb512458SBarry Smith #ifndef lint 2*77c4ece6SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.58 1996/02/14 14:06:17 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; 78289bc588SBarry Smith if (ainew[i+1] > jmax+1) { 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 102416022c9SBarry Smith PLogInfo((PetscObject)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); 1650cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 166289bc588SBarry Smith 16735aab85fSBarry Smith /* precalcuate row sums */ 16835aab85fSBarry Smith if (preserve_row_sums) { 16935aab85fSBarry Smith rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums); 17035aab85fSBarry Smith for ( i=0; i<n; i++ ) { 17135aab85fSBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 17235aab85fSBarry Smith v = a->a + a->i[r[i]] + shift; 17335aab85fSBarry Smith sum = 0.0; 17435aab85fSBarry Smith for ( j=0; j<nz; j++ ) sum += v[j]; 17535aab85fSBarry Smith rowsums[i] = sum; 17635aab85fSBarry Smith } 17735aab85fSBarry Smith } 17835aab85fSBarry Smith 179289bc588SBarry Smith for ( i=0; i<n; i++ ) { 180289bc588SBarry Smith nz = ai[i+1] - ai[i]; 181dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 18248e94559SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 183289bc588SBarry Smith 184289bc588SBarry Smith /* load in initial (unfactored row) */ 185416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 186416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 187416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1880cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 189289bc588SBarry Smith 190dbb450caSBarry Smith row = *ajtmp++ + shift; 191289bc588SBarry Smith while (row < i) { 1928c37ef55SBarry Smith pc = rtmp + row; 1938c37ef55SBarry Smith if (*pc != 0.0) { 1941987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 1951987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 19635aab85fSBarry Smith multiplier = *pc / *pv++; 1978c37ef55SBarry Smith *pc = multiplier; 1981987afe7SBarry Smith nz = ai[row+1] - diag_offset[row] - 1; 19935aab85fSBarry Smith for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 200eef2e97bSBarry Smith PLogFlops(2*nz); 201289bc588SBarry Smith } 202dbb450caSBarry Smith row = *ajtmp++ + shift; 203289bc588SBarry Smith } 204416022c9SBarry Smith /* finished row so stick it into b->a */ 205416022c9SBarry Smith pv = b->a + ai[i] + shift; 206416022c9SBarry Smith pj = b->j + ai[i] + shift; 2078c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 208416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 20935aab85fSBarry Smith diag = diag_offset[i] - ai[i]; 21035aab85fSBarry Smith /* 21135aab85fSBarry Smith Possibly adjust diagonal entry on current row to force 21235aab85fSBarry Smith LU matrix to have same row sum as initial matrix. 21335aab85fSBarry Smith */ 21435aab85fSBarry Smith if (preserve_row_sums) { 21535aab85fSBarry Smith pj = b->j + ai[i] + shift; 21635aab85fSBarry Smith sum = rowsums[i]; 21735aab85fSBarry Smith for ( j=0; j<diag; j++ ) { 21835aab85fSBarry Smith u_values = b->a + diag_offset[pj[j]] + shift; 21935aab85fSBarry Smith nz = ai[pj[j]+1] - diag_offset[pj[j]]; 22035aab85fSBarry Smith inner_sum = 0.0; 22135aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 22235aab85fSBarry Smith inner_sum += u_values[k]; 22335aab85fSBarry Smith } 22435aab85fSBarry Smith sum -= pv[j]*inner_sum; 22535aab85fSBarry Smith 22635aab85fSBarry Smith } 22735aab85fSBarry Smith nz = ai[i+1] - diag_offset[i] - 1; 22835aab85fSBarry Smith u_values = b->a + diag_offset[i] + 1 + shift; 22935aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 23035aab85fSBarry Smith sum -= u_values[k]; 23135aab85fSBarry Smith } 23235aab85fSBarry Smith ssum = PetscAbsScalar(sum/pv[diag]); 23335aab85fSBarry Smith if (ssum < 1000. && ssum > .001) pv[diag] = sum; 23435aab85fSBarry Smith } 23535aab85fSBarry Smith /* check pivot entry for current row */ 23635aab85fSBarry Smith if (pv[diag] == 0.0) { 23735aab85fSBarry Smith SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot"); 23835aab85fSBarry Smith } 2398c37ef55SBarry Smith } 2400f11f9aeSSatish Balay 24135aab85fSBarry Smith /* invert diagonal entries for simplier triangular solves */ 24235aab85fSBarry Smith for ( i=0; i<n; i++ ) { 24335aab85fSBarry Smith b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 24435aab85fSBarry Smith } 24535aab85fSBarry Smith 24635aab85fSBarry Smith if (preserve_row_sums) PetscFree(rowsums); 2470452661fSBarry Smith PetscFree(rtmp); 24878b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 24978b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 25078b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 251416022c9SBarry Smith C->factor = FACTOR_LU; 25241c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 253c456f294SBarry Smith C->assembled = PETSC_TRUE; 254416022c9SBarry Smith PLogFlops(b->n); 255289bc588SBarry Smith return 0; 256289bc588SBarry Smith } 2570a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 258416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 259da3a660dSBarry Smith { 260416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2616abc6512SBarry Smith int ierr; 262416022c9SBarry Smith Mat C; 263416022c9SBarry Smith 264416022c9SBarry Smith ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 265416022c9SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 266da3a660dSBarry Smith 267da3a660dSBarry Smith /* free all the data structures from mat */ 2680452661fSBarry Smith PetscFree(mat->a); 2690452661fSBarry Smith if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 2700452661fSBarry Smith if (mat->diag) PetscFree(mat->diag); 2710452661fSBarry Smith if (mat->ilen) PetscFree(mat->ilen); 2720452661fSBarry Smith if (mat->imax) PetscFree(mat->imax); 2730452661fSBarry Smith if (mat->solve_work) PetscFree(mat->solve_work); 274a7a49059SBarry Smith if (mat->inode.size) PetscFree(mat->inode.size); 2750452661fSBarry Smith PetscFree(mat); 276da3a660dSBarry Smith 277416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 2780452661fSBarry Smith PetscHeaderDestroy(C); 279da3a660dSBarry Smith return 0; 280da3a660dSBarry Smith } 2810a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 282416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2838c37ef55SBarry Smith { 284416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 285416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 286416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 287416022c9SBarry Smith int nz,shift = a->indexshift; 288416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2898c37ef55SBarry Smith 290416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 2919e25ed09SBarry Smith 29278b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 29378b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 294416022c9SBarry Smith tmp = a->solve_work; 2958c37ef55SBarry Smith 29678b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 29778b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2988c37ef55SBarry Smith 2998c37ef55SBarry Smith /* forward solve the lower triangular */ 3008c37ef55SBarry Smith tmp[0] = b[*r++]; 3010cf60383SBarry Smith tmps = tmp + shift; 3028c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 303dbb450caSBarry Smith v = aa + ai[i] + shift; 304dbb450caSBarry Smith vi = aj + ai[i] + shift; 305416022c9SBarry Smith nz = a->diag[i] - ai[i]; 3068c37ef55SBarry Smith sum = b[*r++]; 3070cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 3088c37ef55SBarry Smith tmp[i] = sum; 3098c37ef55SBarry Smith } 3108c37ef55SBarry Smith 3118c37ef55SBarry Smith /* backward solve the upper triangular */ 3128c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 313416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 314416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 315416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 3168c37ef55SBarry Smith sum = tmp[i]; 3170cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 318416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 3198c37ef55SBarry Smith } 3208c37ef55SBarry Smith 321898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 322898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 323416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 3248c37ef55SBarry Smith return 0; 3258c37ef55SBarry Smith } 326416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 327da3a660dSBarry Smith { 328416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 329416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 330416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 331416022c9SBarry Smith int nz, shift = a->indexshift; 332416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 333da3a660dSBarry Smith 334416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 33578b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 336da3a660dSBarry Smith 33778b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 33878b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 339416022c9SBarry Smith tmp = a->solve_work; 340da3a660dSBarry Smith 34178b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 34278b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 343da3a660dSBarry Smith 344da3a660dSBarry Smith /* forward solve the lower triangular */ 345da3a660dSBarry Smith tmp[0] = b[*r++]; 346da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 347dbb450caSBarry Smith v = aa + ai[i] + shift; 348dbb450caSBarry Smith vi = aj + ai[i] + shift; 349416022c9SBarry Smith nz = a->diag[i] - ai[i]; 350da3a660dSBarry Smith sum = b[*r++]; 351dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 352da3a660dSBarry Smith tmp[i] = sum; 353da3a660dSBarry Smith } 354da3a660dSBarry Smith 355da3a660dSBarry Smith /* backward solve the upper triangular */ 356da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 357416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 358416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 359416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 360da3a660dSBarry Smith sum = tmp[i]; 361dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 362416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 363da3a660dSBarry Smith x[*c--] += tmp[i]; 364da3a660dSBarry Smith } 365da3a660dSBarry Smith 366898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 367898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 368416022c9SBarry Smith PLogFlops(2*a->nz); 369898183ecSLois Curfman McInnes 370da3a660dSBarry Smith return 0; 371da3a660dSBarry Smith } 372da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 373416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 374da3a660dSBarry Smith { 375416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 376416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 377416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 378416022c9SBarry Smith int nz,shift = a->indexshift; 379416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 380da3a660dSBarry Smith 381416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 38278b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 38378b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 384416022c9SBarry Smith tmp = a->solve_work; 385da3a660dSBarry Smith 386da3a660dSBarry Smith /* invert the permutations */ 38778b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 38878b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 389da3a660dSBarry Smith 39078b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 39178b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 392da3a660dSBarry Smith 393da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 394da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 395da3a660dSBarry Smith 396da3a660dSBarry Smith /* forward solve the U^T */ 397da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 398416022c9SBarry Smith v = aa + a->diag[i] + shift; 399416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 400416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 401da3a660dSBarry Smith tmp[i] *= *v++; 402da3a660dSBarry Smith while (nz--) { 403dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 404da3a660dSBarry Smith } 405da3a660dSBarry Smith } 406da3a660dSBarry Smith 407da3a660dSBarry Smith /* backward solve the L^T */ 408da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 409416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 410416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 411416022c9SBarry Smith nz = a->diag[i] - ai[i]; 412da3a660dSBarry Smith while (nz--) { 413dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 414da3a660dSBarry Smith } 415da3a660dSBarry Smith } 416da3a660dSBarry Smith 417da3a660dSBarry Smith /* copy tmp into x according to permutation */ 418da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 419da3a660dSBarry Smith 420898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 421898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 422898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 423898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 424da3a660dSBarry Smith 425416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 426da3a660dSBarry Smith return 0; 427da3a660dSBarry Smith } 428da3a660dSBarry Smith 429416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 430da3a660dSBarry Smith { 431416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 432416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 433416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 434416022c9SBarry Smith int nz,shift = a->indexshift; 435416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 4366abc6512SBarry Smith 437416022c9SBarry Smith if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 4386abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 4396abc6512SBarry Smith 44078b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 44178b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 442416022c9SBarry Smith tmp = a->solve_work; 4436abc6512SBarry Smith 4446abc6512SBarry Smith /* invert the permutations */ 44578b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 44678b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 44778b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 44878b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 4496abc6512SBarry Smith 4506abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 4516abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4526abc6512SBarry Smith 4536abc6512SBarry Smith /* forward solve the U^T */ 4546abc6512SBarry Smith for ( i=0; i<n; i++ ) { 455416022c9SBarry Smith v = aa + a->diag[i] + shift; 456416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 457416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 4586abc6512SBarry Smith tmp[i] *= *v++; 4596abc6512SBarry Smith while (nz--) { 460dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4616abc6512SBarry Smith } 4626abc6512SBarry Smith } 4636abc6512SBarry Smith 4646abc6512SBarry Smith /* backward solve the L^T */ 4656abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 466416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 467416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 468416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4696abc6512SBarry Smith while (nz--) { 470dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4716abc6512SBarry Smith } 4726abc6512SBarry Smith } 4736abc6512SBarry Smith 4746abc6512SBarry Smith /* copy tmp into x according to permutation */ 4756abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4766abc6512SBarry Smith 477898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 478898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 479898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 480898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4816abc6512SBarry Smith 482416022c9SBarry Smith PLogFlops(2*a->nz); 4836abc6512SBarry Smith return 0; 484da3a660dSBarry Smith } 4859e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 48608480c60SBarry Smith 48708480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 4889e25ed09SBarry Smith { 489416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4909e25ed09SBarry Smith IS isicol; 491416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 49256beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 49356beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 494416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 495*77c4ece6SBarry Smith PetscTruth col_identity, row_identity; 4969e25ed09SBarry Smith 497416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square"); 498416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation"); 499416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation"); 5009e25ed09SBarry Smith 50108480c60SBarry Smith /* special case that simply copies fill pattern */ 502*77c4ece6SBarry Smith ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity); 503*77c4ece6SBarry Smith if (levels == 0 && row_identity && col_identity) { 5043d1612f7SBarry Smith ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 50508480c60SBarry Smith (*fact)->factor = FACTOR_LU; 50608480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 50708480c60SBarry Smith if (!b->diag) { 50808480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 50908480c60SBarry Smith } 51008480c60SBarry Smith b->row = isrow; 51108480c60SBarry Smith b->col = iscol; 5120452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 51308480c60SBarry Smith return 0; 51408480c60SBarry Smith } 51508480c60SBarry Smith 51678b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 517898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 518898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 5199e25ed09SBarry Smith 5209e25ed09SBarry Smith /* get new row pointers */ 5210452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 522dbb450caSBarry Smith ainew[0] = -shift; 5239e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 524dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 5250452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 5269e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 5270452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 5289e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 5290452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 53056beaf04SBarry Smith /* im is level for each filled value */ 5310452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 53256beaf04SBarry Smith /* dloc is location of diagonal in factor */ 5330452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 53456beaf04SBarry Smith dloc[0] = 0; 53556beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 5369e25ed09SBarry Smith /* first copy previous fill into linked list */ 53756beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 538dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 5399e25ed09SBarry Smith fill[n] = n; 5409e25ed09SBarry Smith while (nz--) { 5419e25ed09SBarry Smith fm = n; 542dbb450caSBarry Smith idx = ic[*xi++ + shift]; 5439e25ed09SBarry Smith do { 5449e25ed09SBarry Smith m = fm; 5459e25ed09SBarry Smith fm = fill[m]; 5469e25ed09SBarry Smith } while (fm < idx); 5479e25ed09SBarry Smith fill[m] = idx; 5489e25ed09SBarry Smith fill[idx] = fm; 54956beaf04SBarry Smith im[idx] = 0; 5509e25ed09SBarry Smith } 55156beaf04SBarry Smith nzi = 0; 5529e25ed09SBarry Smith row = fill[n]; 55356beaf04SBarry Smith while ( row < prow ) { 55456beaf04SBarry Smith incrlev = im[row] + 1; 55556beaf04SBarry Smith nz = dloc[row]; 556dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 557dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 558416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 559dbb450caSBarry Smith if (*xi++ + shift != row) { 560ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 56156beaf04SBarry Smith } 56256beaf04SBarry Smith fm = row; 56356beaf04SBarry Smith while (nnz-- > 0) { 564dbb450caSBarry Smith idx = *xi++ + shift; 56556beaf04SBarry Smith if (*flev + incrlev > levels) { 56656beaf04SBarry Smith flev++; 56756beaf04SBarry Smith continue; 56856beaf04SBarry Smith } 56956beaf04SBarry Smith do { 5709e25ed09SBarry Smith m = fm; 5719e25ed09SBarry Smith fm = fill[m]; 57256beaf04SBarry Smith } while (fm < idx); 5739e25ed09SBarry Smith if (fm != idx) { 57456beaf04SBarry Smith im[idx] = *flev + incrlev; 5759e25ed09SBarry Smith fill[m] = idx; 5769e25ed09SBarry Smith fill[idx] = fm; 5779e25ed09SBarry Smith fm = idx; 57856beaf04SBarry Smith nzf++; 5799e25ed09SBarry Smith } 58056beaf04SBarry Smith else { 58156beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5829e25ed09SBarry Smith } 58356beaf04SBarry Smith flev++; 5849e25ed09SBarry Smith } 5859e25ed09SBarry Smith row = fill[row]; 58656beaf04SBarry Smith nzi++; 5879e25ed09SBarry Smith } 5889e25ed09SBarry Smith /* copy new filled row into permanent storage */ 58956beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 590d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 5919e25ed09SBarry Smith /* allocate a longer ajnew */ 592bbb6d6a8SBarry Smith int maxadd; 593dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 59456beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 595bbb6d6a8SBarry Smith jmax += maxadd; 5960452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 597416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 5980452661fSBarry Smith PetscFree(ajnew); 59956beaf04SBarry Smith ajnew = xi; 6009e25ed09SBarry Smith /* allocate a longer ajfill */ 6010452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 602416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 6030452661fSBarry Smith PetscFree(ajfill); 60456beaf04SBarry Smith ajfill = xi; 605bbb6d6a8SBarry Smith realloc++; 6069e25ed09SBarry Smith } 607dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 608dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 60956beaf04SBarry Smith dloc[prow] = nzi; 6109e25ed09SBarry Smith fm = fill[n]; 61156beaf04SBarry Smith while (nzf--) { 612dbb450caSBarry Smith *xi++ = fm - shift; 61356beaf04SBarry Smith *flev++ = im[fm]; 6149e25ed09SBarry Smith fm = fill[fm]; 6159e25ed09SBarry Smith } 6169e25ed09SBarry Smith } 6170452661fSBarry Smith PetscFree(ajfill); 618898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 619898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 620898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 6210452661fSBarry Smith PetscFree(fill); PetscFree(im); 6229e25ed09SBarry Smith 623416022c9SBarry Smith PLogInfo((PetscObject)A, 624ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 62556beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 626bbb6d6a8SBarry Smith 6279e25ed09SBarry Smith /* put together the new matrix */ 628b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr); 629416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 6300452661fSBarry Smith PetscFree(b->imax); 631416022c9SBarry Smith b->singlemalloc = 0; 632dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 6339e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 6340452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 6350452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 636416022c9SBarry Smith b->j = ajnew; 637416022c9SBarry Smith b->i = ainew; 63856beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 639416022c9SBarry Smith b->diag = dloc; 640416022c9SBarry Smith b->ilen = 0; 641416022c9SBarry Smith b->imax = 0; 642416022c9SBarry Smith b->row = isrow; 643416022c9SBarry Smith b->col = iscol; 6440452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 645416022c9SBarry Smith CHKPTRQ(b->solve_work); 646416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 64756beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 648dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 649416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 6509e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 6519e25ed09SBarry Smith return 0; 6529e25ed09SBarry Smith } 653d5d45c9bSBarry Smith 654d5d45c9bSBarry Smith 655d5d45c9bSBarry Smith 656d5d45c9bSBarry Smith 657