1cb512458SBarry Smith #ifndef lint 2*70f55243SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.64 1996/08/06 23:01:07 curfman Exp bsmith $"; 3cb512458SBarry Smith #endif 4289bc588SBarry Smith 5*70f55243SBarry Smith #include "src/mat/impls/aij/seq/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 18d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(isrow,IS_COOKIE); 19d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(iscol,IS_COOKIE); 2078b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 21289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 22289bc588SBarry Smith 23289bc588SBarry Smith /* get new row pointers */ 240452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 25dbb450caSBarry Smith ainew[0] = -shift; 26289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 27dbb450caSBarry Smith jmax = (int) (f*ai[n]+(!shift)); 280452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 29289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 300452661fSBarry Smith fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 312fb3ed76SBarry Smith im = fill + n + 1; 32289bc588SBarry Smith /* idnew is location of diagonal in factor */ 330452661fSBarry Smith idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew); 34dbb450caSBarry Smith idnew[0] = -shift; 35289bc588SBarry Smith 36289bc588SBarry Smith for ( i=0; i<n; i++ ) { 37289bc588SBarry Smith /* first copy previous fill into linked list */ 38289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 39dbb450caSBarry Smith ajtmp = aj + ai[r[i]] + shift; 40289bc588SBarry Smith fill[n] = n; 41289bc588SBarry Smith while (nz--) { 42289bc588SBarry Smith fm = n; 43dbb450caSBarry Smith idx = ic[*ajtmp++ + shift]; 44289bc588SBarry Smith do { 45289bc588SBarry Smith m = fm; 46289bc588SBarry Smith fm = fill[m]; 47289bc588SBarry Smith } while (fm < idx); 48289bc588SBarry Smith fill[m] = idx; 49289bc588SBarry Smith fill[idx] = fm; 50289bc588SBarry Smith } 51289bc588SBarry Smith row = fill[n]; 52289bc588SBarry Smith while ( row < i ) { 53dbb450caSBarry Smith ajtmp = ajnew + idnew[row] + (!shift); 542fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 552fb3ed76SBarry Smith nz = im[row] - nzbd; 56289bc588SBarry Smith fm = row; 572fb3ed76SBarry Smith while (nz-- > 0) { 58dbb450caSBarry Smith idx = *ajtmp++ + shift; 592fb3ed76SBarry Smith nzbd++; 602fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 61289bc588SBarry Smith do { 62289bc588SBarry Smith m = fm; 63289bc588SBarry Smith fm = fill[m]; 64289bc588SBarry Smith } while (fm < idx); 65289bc588SBarry Smith if (fm != idx) { 66289bc588SBarry Smith fill[m] = idx; 67289bc588SBarry Smith fill[idx] = fm; 68289bc588SBarry Smith fm = idx; 69289bc588SBarry Smith nnz++; 70289bc588SBarry Smith } 71289bc588SBarry Smith } 72289bc588SBarry Smith row = fill[row]; 73289bc588SBarry Smith } 74289bc588SBarry Smith /* copy new filled row into permanent storage */ 75289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 76496e697eSBarry Smith if (ainew[i+1] > jmax) { 77289bc588SBarry Smith /* allocate a longer ajnew */ 782fb3ed76SBarry Smith int maxadd; 79dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); 80bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 812fb3ed76SBarry Smith jmax += maxadd; 820452661fSBarry Smith ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp); 83416022c9SBarry Smith PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int)); 840452661fSBarry Smith PetscFree(ajnew); 85289bc588SBarry Smith ajnew = ajtmp; 862fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 87289bc588SBarry Smith } 88dbb450caSBarry Smith ajtmp = ajnew + ainew[i] + shift; 89289bc588SBarry Smith fm = fill[n]; 90289bc588SBarry Smith nzi = 0; 912fb3ed76SBarry Smith im[i] = nnz; 92289bc588SBarry Smith while (nnz--) { 93289bc588SBarry Smith if (fm < i) nzi++; 94dbb450caSBarry Smith *ajtmp++ = fm - shift; 95289bc588SBarry Smith fm = fill[fm]; 96289bc588SBarry Smith } 97289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 98289bc588SBarry Smith } 99289bc588SBarry Smith 10094a424c1SBarry Smith PLogInfo(A, 101ec8511deSBarry Smith "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 102bbb6d6a8SBarry Smith realloc,f,((double)ainew[n])/((double)ai[i])); 1032fb3ed76SBarry Smith 104898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 105898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 1061987afe7SBarry Smith 1070452661fSBarry Smith PetscFree(fill); 108289bc588SBarry Smith 109289bc588SBarry Smith /* put together the new matrix */ 110b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,B); CHKERRQ(ierr); 1111987afe7SBarry Smith PLogObjectParent(*B,isicol); 1121987afe7SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 113416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 1140452661fSBarry Smith PetscFree(b->imax); 115416022c9SBarry Smith b->singlemalloc = 0; 116dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 117e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 1180452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 1190452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 120416022c9SBarry Smith b->j = ajnew; 121416022c9SBarry Smith b->i = ainew; 122416022c9SBarry Smith b->diag = idnew; 123416022c9SBarry Smith b->ilen = 0; 124416022c9SBarry Smith b->imax = 0; 125416022c9SBarry Smith b->row = isrow; 126416022c9SBarry Smith b->col = iscol; 1270452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar)); 128416022c9SBarry Smith CHKPTRQ(b->solve_work); 129416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 1307fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 131416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 132416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 1337fd98bd6SLois Curfman McInnes 134289bc588SBarry Smith return 0; 135289bc588SBarry Smith } 1360a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 13741c01911SSatish Balay int Mat_AIJ_CheckInode(Mat); 13841c01911SSatish Balay 139416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 140289bc588SBarry Smith { 141416022c9SBarry Smith Mat C = *B; 142416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 143416022c9SBarry Smith IS iscol = b->col, isrow = b->row, isicol; 144416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 1451987afe7SBarry Smith int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 14635aab85fSBarry Smith int *diag_offset = b->diag,diag,k; 14735aab85fSBarry Smith int preserve_row_sums = (int) a->ilu_preserve_row_sums; 1488ecf7165SBarry Smith Scalar *rtmp,*v, *pc, multiplier,sum,inner_sum,*rowsums = 0; 14935aab85fSBarry Smith double ssum; 1501987afe7SBarry Smith /* These declarations are for optimizations. They reduce the number of 1511987afe7SBarry Smith memory references that are made by locally storing information; the 1521987afe7SBarry Smith word "register" used here with pointers can be viewed as "private" or 1531987afe7SBarry Smith "known only to me" 1541987afe7SBarry Smith */ 15535aab85fSBarry Smith register Scalar *pv, *rtmps,*u_values; 1561987afe7SBarry Smith register int *pj; 157289bc588SBarry Smith 15878b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 159416022c9SBarry Smith PLogObjectParent(*B,isicol); 16078b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 16178b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 1620452661fSBarry Smith rtmp = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 16313ae1565SBarry Smith PetscMemzero(rtmp,(n+1)*sizeof(Scalar)); 1640cf60383SBarry Smith rtmps = rtmp + shift; ics = ic + shift; 165289bc588SBarry Smith 16635aab85fSBarry Smith /* precalcuate row sums */ 16735aab85fSBarry Smith if (preserve_row_sums) { 16835aab85fSBarry Smith rowsums = (Scalar *) PetscMalloc( n*sizeof(Scalar) ); CHKPTRQ(rowsums); 16935aab85fSBarry Smith for ( i=0; i<n; i++ ) { 17035aab85fSBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 17135aab85fSBarry Smith v = a->a + a->i[r[i]] + shift; 17235aab85fSBarry Smith sum = 0.0; 17335aab85fSBarry Smith for ( j=0; j<nz; j++ ) sum += v[j]; 17435aab85fSBarry Smith rowsums[i] = sum; 17535aab85fSBarry Smith } 17635aab85fSBarry Smith } 17735aab85fSBarry Smith 178289bc588SBarry Smith for ( i=0; i<n; i++ ) { 179289bc588SBarry Smith nz = ai[i+1] - ai[i]; 180dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 18148e94559SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 182289bc588SBarry Smith 183289bc588SBarry Smith /* load in initial (unfactored row) */ 184416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 185416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 186416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1870cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 188289bc588SBarry Smith 189dbb450caSBarry Smith row = *ajtmp++ + shift; 190289bc588SBarry Smith while (row < i) { 1918c37ef55SBarry Smith pc = rtmp + row; 1928c37ef55SBarry Smith if (*pc != 0.0) { 1931987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 1941987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 19535aab85fSBarry Smith multiplier = *pc / *pv++; 1968c37ef55SBarry Smith *pc = multiplier; 1971987afe7SBarry Smith nz = ai[row+1] - diag_offset[row] - 1; 19835aab85fSBarry Smith for (j=0; j<nz; j++) rtmps[pj[j]] -= multiplier * pv[j]; 199eef2e97bSBarry Smith PLogFlops(2*nz); 200289bc588SBarry Smith } 201dbb450caSBarry Smith row = *ajtmp++ + shift; 202289bc588SBarry Smith } 203416022c9SBarry Smith /* finished row so stick it into b->a */ 204416022c9SBarry Smith pv = b->a + ai[i] + shift; 205416022c9SBarry Smith pj = b->j + ai[i] + shift; 2068c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 207416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 20835aab85fSBarry Smith diag = diag_offset[i] - ai[i]; 20935aab85fSBarry Smith /* 21035aab85fSBarry Smith Possibly adjust diagonal entry on current row to force 21135aab85fSBarry Smith LU matrix to have same row sum as initial matrix. 21235aab85fSBarry Smith */ 21335aab85fSBarry Smith if (preserve_row_sums) { 21435aab85fSBarry Smith pj = b->j + ai[i] + shift; 21535aab85fSBarry Smith sum = rowsums[i]; 21635aab85fSBarry Smith for ( j=0; j<diag; j++ ) { 21735aab85fSBarry Smith u_values = b->a + diag_offset[pj[j]] + shift; 21835aab85fSBarry Smith nz = ai[pj[j]+1] - diag_offset[pj[j]]; 21935aab85fSBarry Smith inner_sum = 0.0; 22035aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 22135aab85fSBarry Smith inner_sum += u_values[k]; 22235aab85fSBarry Smith } 22335aab85fSBarry Smith sum -= pv[j]*inner_sum; 22435aab85fSBarry Smith 22535aab85fSBarry Smith } 22635aab85fSBarry Smith nz = ai[i+1] - diag_offset[i] - 1; 22735aab85fSBarry Smith u_values = b->a + diag_offset[i] + 1 + shift; 22835aab85fSBarry Smith for ( k=0; k<nz; k++ ) { 22935aab85fSBarry Smith sum -= u_values[k]; 23035aab85fSBarry Smith } 23135aab85fSBarry Smith ssum = PetscAbsScalar(sum/pv[diag]); 23235aab85fSBarry Smith if (ssum < 1000. && ssum > .001) pv[diag] = sum; 23335aab85fSBarry Smith } 23435aab85fSBarry Smith /* check pivot entry for current row */ 23535aab85fSBarry Smith if (pv[diag] == 0.0) { 23635aab85fSBarry Smith SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot"); 23735aab85fSBarry Smith } 2388c37ef55SBarry Smith } 2390f11f9aeSSatish Balay 24035aab85fSBarry Smith /* invert diagonal entries for simplier triangular solves */ 24135aab85fSBarry Smith for ( i=0; i<n; i++ ) { 24235aab85fSBarry Smith b->a[diag_offset[i]+shift] = 1.0/b->a[diag_offset[i]+shift]; 24335aab85fSBarry Smith } 24435aab85fSBarry Smith 24535aab85fSBarry Smith if (preserve_row_sums) PetscFree(rowsums); 2460452661fSBarry Smith PetscFree(rtmp); 24778b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 24878b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 24978b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 250416022c9SBarry Smith C->factor = FACTOR_LU; 25141c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 252c456f294SBarry Smith C->assembled = PETSC_TRUE; 253416022c9SBarry Smith PLogFlops(b->n); 254289bc588SBarry Smith return 0; 255289bc588SBarry Smith } 2560a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 257416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 258da3a660dSBarry Smith { 259416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2606abc6512SBarry Smith int ierr; 261416022c9SBarry Smith Mat C; 262416022c9SBarry Smith 263d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(row,IS_COOKIE); 264d3cbd50cSLois Curfman McInnes PetscValidHeaderSpecific(col,IS_COOKIE); 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 49808480c60SBarry Smith /* special case that simply copies fill pattern */ 49977c4ece6SBarry Smith ISIdentity(isrow,&row_identity); ISIdentity(iscol,&col_identity); 50077c4ece6SBarry Smith if (levels == 0 && row_identity && col_identity) { 5013d1612f7SBarry Smith ierr = MatConvertSameType_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 50208480c60SBarry Smith (*fact)->factor = FACTOR_LU; 50308480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 50408480c60SBarry Smith if (!b->diag) { 50508480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 50608480c60SBarry Smith } 50708480c60SBarry Smith b->row = isrow; 50808480c60SBarry Smith b->col = iscol; 5090452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 51008480c60SBarry Smith return 0; 51108480c60SBarry Smith } 51208480c60SBarry Smith 51378b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 514898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 515898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 5169e25ed09SBarry Smith 5179e25ed09SBarry Smith /* get new row pointers */ 5180452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 519dbb450caSBarry Smith ainew[0] = -shift; 5209e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 521dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 5220452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 5239e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 5240452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 5259e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 5260452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 52756beaf04SBarry Smith /* im is level for each filled value */ 5280452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 52956beaf04SBarry Smith /* dloc is location of diagonal in factor */ 5300452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 53156beaf04SBarry Smith dloc[0] = 0; 53256beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 5339e25ed09SBarry Smith /* first copy previous fill into linked list */ 53456beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 535dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 5369e25ed09SBarry Smith fill[n] = n; 5379e25ed09SBarry Smith while (nz--) { 5389e25ed09SBarry Smith fm = n; 539dbb450caSBarry Smith idx = ic[*xi++ + shift]; 5409e25ed09SBarry Smith do { 5419e25ed09SBarry Smith m = fm; 5429e25ed09SBarry Smith fm = fill[m]; 5439e25ed09SBarry Smith } while (fm < idx); 5449e25ed09SBarry Smith fill[m] = idx; 5459e25ed09SBarry Smith fill[idx] = fm; 54656beaf04SBarry Smith im[idx] = 0; 5479e25ed09SBarry Smith } 54856beaf04SBarry Smith nzi = 0; 5499e25ed09SBarry Smith row = fill[n]; 55056beaf04SBarry Smith while ( row < prow ) { 55156beaf04SBarry Smith incrlev = im[row] + 1; 55256beaf04SBarry Smith nz = dloc[row]; 553dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 554dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 555416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 556dbb450caSBarry Smith if (*xi++ + shift != row) { 557ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 55856beaf04SBarry Smith } 55956beaf04SBarry Smith fm = row; 56056beaf04SBarry Smith while (nnz-- > 0) { 561dbb450caSBarry Smith idx = *xi++ + shift; 56256beaf04SBarry Smith if (*flev + incrlev > levels) { 56356beaf04SBarry Smith flev++; 56456beaf04SBarry Smith continue; 56556beaf04SBarry Smith } 56656beaf04SBarry Smith do { 5679e25ed09SBarry Smith m = fm; 5689e25ed09SBarry Smith fm = fill[m]; 56956beaf04SBarry Smith } while (fm < idx); 5709e25ed09SBarry Smith if (fm != idx) { 57156beaf04SBarry Smith im[idx] = *flev + incrlev; 5729e25ed09SBarry Smith fill[m] = idx; 5739e25ed09SBarry Smith fill[idx] = fm; 5749e25ed09SBarry Smith fm = idx; 57556beaf04SBarry Smith nzf++; 5769e25ed09SBarry Smith } 57756beaf04SBarry Smith else { 57856beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5799e25ed09SBarry Smith } 58056beaf04SBarry Smith flev++; 5819e25ed09SBarry Smith } 5829e25ed09SBarry Smith row = fill[row]; 58356beaf04SBarry Smith nzi++; 5849e25ed09SBarry Smith } 5859e25ed09SBarry Smith /* copy new filled row into permanent storage */ 58656beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 587d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 5889e25ed09SBarry Smith /* allocate a longer ajnew */ 589bbb6d6a8SBarry Smith int maxadd; 590dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 59156beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 592bbb6d6a8SBarry Smith jmax += maxadd; 5930452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 594416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 5950452661fSBarry Smith PetscFree(ajnew); 59656beaf04SBarry Smith ajnew = xi; 5979e25ed09SBarry Smith /* allocate a longer ajfill */ 5980452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 599416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 6000452661fSBarry Smith PetscFree(ajfill); 60156beaf04SBarry Smith ajfill = xi; 602bbb6d6a8SBarry Smith realloc++; 6039e25ed09SBarry Smith } 604dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 605dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 60656beaf04SBarry Smith dloc[prow] = nzi; 6079e25ed09SBarry Smith fm = fill[n]; 60856beaf04SBarry Smith while (nzf--) { 609dbb450caSBarry Smith *xi++ = fm - shift; 61056beaf04SBarry Smith *flev++ = im[fm]; 6119e25ed09SBarry Smith fm = fill[fm]; 6129e25ed09SBarry Smith } 6139e25ed09SBarry Smith } 6140452661fSBarry Smith PetscFree(ajfill); 615898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 616898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 617898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 6180452661fSBarry Smith PetscFree(fill); PetscFree(im); 6199e25ed09SBarry Smith 62094a424c1SBarry Smith PLogInfo(A, 621ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 62256beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 623bbb6d6a8SBarry Smith 6249e25ed09SBarry Smith /* put together the new matrix */ 625b4fd4287SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n,n,0,PETSC_NULL,fact); CHKERRQ(ierr); 626416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 6270452661fSBarry Smith PetscFree(b->imax); 628416022c9SBarry Smith b->singlemalloc = 0; 629dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 6309e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 6310452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 6320452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 633416022c9SBarry Smith b->j = ajnew; 634416022c9SBarry Smith b->i = ainew; 63556beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 636416022c9SBarry Smith b->diag = dloc; 637416022c9SBarry Smith b->ilen = 0; 638416022c9SBarry Smith b->imax = 0; 639416022c9SBarry Smith b->row = isrow; 640416022c9SBarry Smith b->col = iscol; 6410452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 642416022c9SBarry Smith CHKPTRQ(b->solve_work); 643416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 64456beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 645dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 646416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 6479e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 6489e25ed09SBarry Smith return 0; 6499e25ed09SBarry Smith } 650d5d45c9bSBarry Smith 651d5d45c9bSBarry Smith 652d5d45c9bSBarry Smith 653d5d45c9bSBarry Smith 654