1cb512458SBarry Smith #ifndef lint 2*0f11f9aeSSatish Balay static char vcid[] = "$Id: aijfact.c,v 1.47 1995/11/09 22:28:49 bsmith Exp balay $"; 3cb512458SBarry Smith #endif 4289bc588SBarry Smith 5289bc588SBarry Smith #include "aij.h" 68c37ef55SBarry Smith #include "inline/spops.h" 7289bc588SBarry Smith /* 8289bc588SBarry Smith Factorization code for AIJ format. 9289bc588SBarry Smith */ 10289bc588SBarry Smith 11416022c9SBarry Smith int MatLUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,Mat *B) 12289bc588SBarry Smith { 13416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 14289bc588SBarry Smith IS isicol; 15416022c9SBarry Smith int *r,*ic, ierr, i, n = a->m, *ai = a->i, *aj = a->j; 16416022c9SBarry Smith int *ainew,*ajnew, jmax,*fill, *ajtmp, nz,shift = a->indexshift; 172fb3ed76SBarry Smith int *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im; 18289bc588SBarry Smith 19416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must be square"); 20416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have row permutation"); 21416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatLUFactorSymbolic_SeqAIJ:Must have column permutation"); 22289bc588SBarry Smith 2378b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 24289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 25289bc588SBarry Smith 26289bc588SBarry Smith /* get new row pointers */ 270452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 28dbb450caSBarry Smith ainew[0] = -shift; 29289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 30dbb450caSBarry Smith jmax = (int) (f*ai[n]+(!shift)); 310452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 32289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 330452661fSBarry Smith fill = (int *) PetscMalloc( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 342fb3ed76SBarry Smith im = fill + n + 1; 35289bc588SBarry Smith /* idnew is location of diagonal in factor */ 360452661fSBarry Smith idnew = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(idnew); 37dbb450caSBarry Smith idnew[0] = -shift; 38289bc588SBarry Smith 39289bc588SBarry Smith for ( i=0; i<n; i++ ) { 40289bc588SBarry Smith /* first copy previous fill into linked list */ 41289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 42dbb450caSBarry Smith ajtmp = aj + ai[r[i]] + shift; 43289bc588SBarry Smith fill[n] = n; 44289bc588SBarry Smith while (nz--) { 45289bc588SBarry Smith fm = n; 46dbb450caSBarry Smith idx = ic[*ajtmp++ + shift]; 47289bc588SBarry Smith do { 48289bc588SBarry Smith m = fm; 49289bc588SBarry Smith fm = fill[m]; 50289bc588SBarry Smith } while (fm < idx); 51289bc588SBarry Smith fill[m] = idx; 52289bc588SBarry Smith fill[idx] = fm; 53289bc588SBarry Smith } 54289bc588SBarry Smith row = fill[n]; 55289bc588SBarry Smith while ( row < i ) { 56dbb450caSBarry Smith ajtmp = ajnew + idnew[row] + (!shift); 572fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 582fb3ed76SBarry Smith nz = im[row] - nzbd; 59289bc588SBarry Smith fm = row; 602fb3ed76SBarry Smith while (nz-- > 0) { 61dbb450caSBarry Smith idx = *ajtmp++ + shift; 622fb3ed76SBarry Smith nzbd++; 632fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 64289bc588SBarry Smith do { 65289bc588SBarry Smith m = fm; 66289bc588SBarry Smith fm = fill[m]; 67289bc588SBarry Smith } while (fm < idx); 68289bc588SBarry Smith if (fm != idx) { 69289bc588SBarry Smith fill[m] = idx; 70289bc588SBarry Smith fill[idx] = fm; 71289bc588SBarry Smith fm = idx; 72289bc588SBarry Smith nnz++; 73289bc588SBarry Smith } 74289bc588SBarry Smith } 75289bc588SBarry Smith row = fill[row]; 76289bc588SBarry Smith } 77289bc588SBarry Smith /* copy new filled row into permanent storage */ 78289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 79289bc588SBarry Smith if (ainew[i+1] > jmax+1) { 80289bc588SBarry Smith /* allocate a longer ajnew */ 812fb3ed76SBarry Smith int maxadd; 82dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+(!shift))*(n-i+5))/n); 83bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 842fb3ed76SBarry Smith jmax += maxadd; 850452661fSBarry Smith ajtmp = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(ajtmp); 86416022c9SBarry Smith PetscMemcpy(ajtmp,ajnew,(ainew[i]+shift)*sizeof(int)); 870452661fSBarry Smith PetscFree(ajnew); 88289bc588SBarry Smith ajnew = ajtmp; 892fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 90289bc588SBarry Smith } 91dbb450caSBarry Smith ajtmp = ajnew + ainew[i] + shift; 92289bc588SBarry Smith fm = fill[n]; 93289bc588SBarry Smith nzi = 0; 942fb3ed76SBarry Smith im[i] = nnz; 95289bc588SBarry Smith while (nnz--) { 96289bc588SBarry Smith if (fm < i) nzi++; 97dbb450caSBarry Smith *ajtmp++ = fm - shift; 98289bc588SBarry Smith fm = fill[fm]; 99289bc588SBarry Smith } 100289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 101289bc588SBarry Smith } 102289bc588SBarry Smith 103416022c9SBarry Smith PLogInfo((PetscObject)A, 104ec8511deSBarry Smith "Info:MatLUFactorSymbolic_SeqAIJ:Reallocs %d Fill ratio:given %g needed %g\n", 105bbb6d6a8SBarry Smith realloc,f,((double)ainew[n])/((double)ai[i])); 1062fb3ed76SBarry Smith 107898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 108898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 1091987afe7SBarry Smith 1100452661fSBarry Smith PetscFree(fill); 111289bc588SBarry Smith 112289bc588SBarry Smith /* put together the new matrix */ 113416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, B); CHKERRQ(ierr); 1141987afe7SBarry Smith PLogObjectParent(*B,isicol); 1151987afe7SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 116416022c9SBarry Smith b = (Mat_SeqAIJ *) (*B)->data; 1170452661fSBarry Smith PetscFree(b->imax); 118416022c9SBarry Smith b->singlemalloc = 0; 119dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 120e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 1210452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 1220452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 123416022c9SBarry Smith b->j = ajnew; 124416022c9SBarry Smith b->i = ainew; 125416022c9SBarry Smith b->diag = idnew; 126416022c9SBarry Smith b->ilen = 0; 127416022c9SBarry Smith b->imax = 0; 128416022c9SBarry Smith b->row = isrow; 129416022c9SBarry Smith b->col = iscol; 1300452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( n*sizeof(Scalar)); 131416022c9SBarry Smith CHKPTRQ(b->solve_work); 132416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 1337fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 134416022c9SBarry Smith PLogObjectMemory(*B,(ainew[n]+shift-n)*(sizeof(int)+sizeof(Scalar))); 135416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 1367fd98bd6SLois Curfman McInnes 137289bc588SBarry Smith return 0; 138289bc588SBarry Smith } 1390a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 14041c01911SSatish Balay int Mat_AIJ_CheckInode(Mat); 14141c01911SSatish Balay 142416022c9SBarry Smith int MatLUFactorNumeric_SeqAIJ(Mat A,Mat *B) 143289bc588SBarry Smith { 144416022c9SBarry Smith Mat C = *B; 145416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b = (Mat_SeqAIJ *)C->data; 146416022c9SBarry Smith IS iscol = b->col, isrow = b->row, isicol; 147416022c9SBarry Smith int *r,*ic, ierr, i, j, n = a->m, *ai = b->i, *aj = b->j; 1481987afe7SBarry Smith int *ajtmpold, *ajtmp, nz, row, *ics, shift = a->indexshift; 149d5d45c9bSBarry Smith int *diag_offset = b->diag; 150390f2bbaSBarry Smith Scalar *rtmp,*v, *pc, multiplier; 1511987afe7SBarry Smith /* These declarations are for optimizations. They reduce the number of 1521987afe7SBarry Smith memory references that are made by locally storing information; the 1531987afe7SBarry Smith word "register" used here with pointers can be viewed as "private" or 1541987afe7SBarry Smith "known only to me" 1551987afe7SBarry Smith */ 156390f2bbaSBarry Smith register Scalar *pv, *rtmps; 1571987afe7SBarry Smith register int *pj; 158*0f11f9aeSSatish Balay int fail_count = 0, total_count = 0; 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 167289bc588SBarry Smith for ( i=0; i<n; i++ ) { 168289bc588SBarry Smith nz = ai[i+1] - ai[i]; 169dbb450caSBarry Smith ajtmp = aj + ai[i] + shift; 1700cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmps[ajtmp[j]] = 0.0; 171289bc588SBarry Smith 172289bc588SBarry Smith /* load in initial (unfactored row) */ 173416022c9SBarry Smith nz = a->i[r[i]+1] - a->i[r[i]]; 174416022c9SBarry Smith ajtmpold = a->j + a->i[r[i]] + shift; 175416022c9SBarry Smith v = a->a + a->i[r[i]] + shift; 1760cf60383SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ics[ajtmpold[j]]] = v[j]; 177289bc588SBarry Smith 178dbb450caSBarry Smith row = *ajtmp++ + shift; 179289bc588SBarry Smith while (row < i) { 1808c37ef55SBarry Smith pc = rtmp + row; 1818c37ef55SBarry Smith if (*pc != 0.0) { 1821987afe7SBarry Smith pv = b->a + diag_offset[row] + shift; 1831987afe7SBarry Smith pj = b->j + diag_offset[row] + (!shift); 1848c37ef55SBarry Smith multiplier = *pc * *pv++; 1858c37ef55SBarry Smith *pc = multiplier; 1861987afe7SBarry Smith nz = ai[row+1] - diag_offset[row] - 1; 187eef2e97bSBarry Smith PLogFlops(2*nz); 1881987afe7SBarry Smith /* The for-loop form can aid the compiler in overlapping 1891987afe7SBarry Smith loads and stores */ 1901987afe7SBarry Smith /*while (nz-->0) rtmps[*pj++] -= multiplier* *pv++; */ 1911987afe7SBarry Smith {int __i; 1921987afe7SBarry Smith for (__i=0; __i<nz; __i++) rtmps[pj[__i]] -= multiplier * pv[__i]; 1931987afe7SBarry Smith } 194289bc588SBarry Smith } 195*0f11f9aeSSatish Balay else ++fail_count; 196*0f11f9aeSSatish Balay total_count ++; 197dbb450caSBarry Smith row = *ajtmp++ + shift; 198289bc588SBarry Smith } 199416022c9SBarry Smith /* finished row so stick it into b->a */ 200416022c9SBarry Smith pv = b->a + ai[i] + shift; 201416022c9SBarry Smith pj = b->j + ai[i] + shift; 2028c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 203ec8511deSBarry Smith if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_SeqAIJ:Zero pivot");} 2048c37ef55SBarry Smith rtmp[i] = 1.0/rtmp[i]; 205416022c9SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmps[pj[j]];} 2068c37ef55SBarry Smith } 207*0f11f9aeSSatish Balay 208*0f11f9aeSSatish Balay printf("Fail Count = %d, Total Count = %d \n",fail_count, total_count); 2090452661fSBarry Smith PetscFree(rtmp); 21078b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 21178b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 21278b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 213416022c9SBarry Smith C->factor = FACTOR_LU; 21441c01911SSatish Balay ierr = Mat_AIJ_CheckInode(C); CHKERRQ(ierr); 215416022c9SBarry Smith b->assembled = 1; 216416022c9SBarry Smith PLogFlops(b->n); 217289bc588SBarry Smith return 0; 218289bc588SBarry Smith } 2190a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 220416022c9SBarry Smith int MatLUFactor_SeqAIJ(Mat A,IS row,IS col,double f) 221da3a660dSBarry Smith { 222416022c9SBarry Smith Mat_SeqAIJ *mat = (Mat_SeqAIJ *) A->data; 2236abc6512SBarry Smith int ierr; 224416022c9SBarry Smith Mat C; 225416022c9SBarry Smith 226416022c9SBarry Smith ierr = MatLUFactorSymbolic_SeqAIJ(A,row,col,f,&C); CHKERRQ(ierr); 227416022c9SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(A,&C); CHKERRQ(ierr); 228da3a660dSBarry Smith 229da3a660dSBarry Smith /* free all the data structures from mat */ 2300452661fSBarry Smith PetscFree(mat->a); 2310452661fSBarry Smith if (!mat->singlemalloc) {PetscFree(mat->i); PetscFree(mat->j);} 2320452661fSBarry Smith if (mat->diag) PetscFree(mat->diag); 2330452661fSBarry Smith if (mat->ilen) PetscFree(mat->ilen); 2340452661fSBarry Smith if (mat->imax) PetscFree(mat->imax); 2350452661fSBarry Smith if (mat->solve_work) PetscFree(mat->solve_work); 2360452661fSBarry Smith PetscFree(mat); 237da3a660dSBarry Smith 238416022c9SBarry Smith PetscMemcpy(A,C,sizeof(struct _Mat)); 2390452661fSBarry Smith PetscHeaderDestroy(C); 240da3a660dSBarry Smith return 0; 241da3a660dSBarry Smith } 2420a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 243416022c9SBarry Smith int MatSolve_SeqAIJ(Mat A,Vec bb, Vec xx) 2448c37ef55SBarry Smith { 245416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 246416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 247416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 248416022c9SBarry Smith int nz,shift = a->indexshift; 249416022c9SBarry Smith Scalar *x,*b,*tmp, *tmps, *aa = a->a, sum, *v; 2508c37ef55SBarry Smith 251416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolve_SeqAIJ:Not for unfactored matrix"); 2529e25ed09SBarry Smith 25378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 25478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 255416022c9SBarry Smith tmp = a->solve_work; 2568c37ef55SBarry Smith 25778b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 25878b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2598c37ef55SBarry Smith 2608c37ef55SBarry Smith /* forward solve the lower triangular */ 2618c37ef55SBarry Smith tmp[0] = b[*r++]; 2620cf60383SBarry Smith tmps = tmp + shift; 2638c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 264dbb450caSBarry Smith v = aa + ai[i] + shift; 265dbb450caSBarry Smith vi = aj + ai[i] + shift; 266416022c9SBarry Smith nz = a->diag[i] - ai[i]; 2678c37ef55SBarry Smith sum = b[*r++]; 2680cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 2698c37ef55SBarry Smith tmp[i] = sum; 2708c37ef55SBarry Smith } 2718c37ef55SBarry Smith 2728c37ef55SBarry Smith /* backward solve the upper triangular */ 2738c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 274416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 275416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 276416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 2778c37ef55SBarry Smith sum = tmp[i]; 2780cf60383SBarry Smith while (nz--) sum -= *v++ * tmps[*vi++]; 279416022c9SBarry Smith x[*c--] = tmp[i] = sum*aa[a->diag[i]+shift]; 2808c37ef55SBarry Smith } 2818c37ef55SBarry Smith 282898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 283898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 284416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 2858c37ef55SBarry Smith return 0; 2868c37ef55SBarry Smith } 287416022c9SBarry Smith int MatSolveAdd_SeqAIJ(Mat A,Vec bb, Vec yy, Vec xx) 288da3a660dSBarry Smith { 289416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 290416022c9SBarry Smith IS iscol = a->col, isrow = a->row; 291416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 292416022c9SBarry Smith int nz, shift = a->indexshift; 293416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, sum, *v; 294da3a660dSBarry Smith 295416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveAdd_SeqAIJ:Not for unfactored matrix"); 29678b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 297da3a660dSBarry Smith 29878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 29978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 300416022c9SBarry Smith tmp = a->solve_work; 301da3a660dSBarry Smith 30278b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 30378b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 304da3a660dSBarry Smith 305da3a660dSBarry Smith /* forward solve the lower triangular */ 306da3a660dSBarry Smith tmp[0] = b[*r++]; 307da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 308dbb450caSBarry Smith v = aa + ai[i] + shift; 309dbb450caSBarry Smith vi = aj + ai[i] + shift; 310416022c9SBarry Smith nz = a->diag[i] - ai[i]; 311da3a660dSBarry Smith sum = b[*r++]; 312dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 313da3a660dSBarry Smith tmp[i] = sum; 314da3a660dSBarry Smith } 315da3a660dSBarry Smith 316da3a660dSBarry Smith /* backward solve the upper triangular */ 317da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 318416022c9SBarry Smith v = aa + a->diag[i] + (!shift); 319416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 320416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 321da3a660dSBarry Smith sum = tmp[i]; 322dbb450caSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ + shift]; 323416022c9SBarry Smith tmp[i] = sum*aa[a->diag[i]+shift]; 324da3a660dSBarry Smith x[*c--] += tmp[i]; 325da3a660dSBarry Smith } 326da3a660dSBarry Smith 327898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 328898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 329416022c9SBarry Smith PLogFlops(2*a->nz); 330898183ecSLois Curfman McInnes 331da3a660dSBarry Smith return 0; 332da3a660dSBarry Smith } 333da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 334416022c9SBarry Smith int MatSolveTrans_SeqAIJ(Mat A,Vec bb, Vec xx) 335da3a660dSBarry Smith { 336416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 337416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 338416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 339416022c9SBarry Smith int nz,shift = a->indexshift; 340416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 341da3a660dSBarry Smith 342416022c9SBarry Smith if (A->factor != FACTOR_LU) SETERRQ(1,"MatSolveTrans_SeqAIJ:Not unfactored matrix"); 34378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 34478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 345416022c9SBarry Smith tmp = a->solve_work; 346da3a660dSBarry Smith 347da3a660dSBarry Smith /* invert the permutations */ 34878b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 34978b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 350da3a660dSBarry Smith 35178b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 35278b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 353da3a660dSBarry Smith 354da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 355da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 356da3a660dSBarry Smith 357da3a660dSBarry Smith /* forward solve the U^T */ 358da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 359416022c9SBarry Smith v = aa + a->diag[i] + shift; 360416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 361416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 362da3a660dSBarry Smith tmp[i] *= *v++; 363da3a660dSBarry Smith while (nz--) { 364dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 365da3a660dSBarry Smith } 366da3a660dSBarry Smith } 367da3a660dSBarry Smith 368da3a660dSBarry Smith /* backward solve the L^T */ 369da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 370416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 371416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 372416022c9SBarry Smith nz = a->diag[i] - ai[i]; 373da3a660dSBarry Smith while (nz--) { 374dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 375da3a660dSBarry Smith } 376da3a660dSBarry Smith } 377da3a660dSBarry Smith 378da3a660dSBarry Smith /* copy tmp into x according to permutation */ 379da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 380da3a660dSBarry Smith 381898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 382898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 383898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 384898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 385da3a660dSBarry Smith 386416022c9SBarry Smith PLogFlops(2*a->nz-a->n); 387da3a660dSBarry Smith return 0; 388da3a660dSBarry Smith } 389da3a660dSBarry Smith 390416022c9SBarry Smith int MatSolveTransAdd_SeqAIJ(Mat A,Vec bb, Vec zz,Vec xx) 391da3a660dSBarry Smith { 392416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 393416022c9SBarry Smith IS iscol = a->col, isrow = a->row, invisrow,inviscol; 394416022c9SBarry Smith int *r,*c, ierr, i, n = a->m, *vi, *ai = a->i, *aj = a->j; 395416022c9SBarry Smith int nz,shift = a->indexshift; 396416022c9SBarry Smith Scalar *x,*b,*tmp, *aa = a->a, *v; 3976abc6512SBarry Smith 398416022c9SBarry Smith if (A->factor != FACTOR_LU)SETERRQ(1,"MatSolveTransAdd_SeqAIJ:Not unfactored matrix"); 3996abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 4006abc6512SBarry Smith 40178b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 40278b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 403416022c9SBarry Smith tmp = a->solve_work; 4046abc6512SBarry Smith 4056abc6512SBarry Smith /* invert the permutations */ 40678b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 40778b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 40878b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 40978b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 4106abc6512SBarry Smith 4116abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 4126abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4136abc6512SBarry Smith 4146abc6512SBarry Smith /* forward solve the U^T */ 4156abc6512SBarry Smith for ( i=0; i<n; i++ ) { 416416022c9SBarry Smith v = aa + a->diag[i] + shift; 417416022c9SBarry Smith vi = aj + a->diag[i] + (!shift); 418416022c9SBarry Smith nz = ai[i+1] - a->diag[i] - 1; 4196abc6512SBarry Smith tmp[i] *= *v++; 4206abc6512SBarry Smith while (nz--) { 421dbb450caSBarry Smith tmp[*vi++ + shift] -= (*v++)*tmp[i]; 4226abc6512SBarry Smith } 4236abc6512SBarry Smith } 4246abc6512SBarry Smith 4256abc6512SBarry Smith /* backward solve the L^T */ 4266abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 427416022c9SBarry Smith v = aa + a->diag[i] - 1 + shift; 428416022c9SBarry Smith vi = aj + a->diag[i] - 1 + shift; 429416022c9SBarry Smith nz = a->diag[i] - ai[i]; 4306abc6512SBarry Smith while (nz--) { 431dbb450caSBarry Smith tmp[*vi-- + shift] -= (*v--)*tmp[i]; 4326abc6512SBarry Smith } 4336abc6512SBarry Smith } 4346abc6512SBarry Smith 4356abc6512SBarry Smith /* copy tmp into x according to permutation */ 4366abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4376abc6512SBarry Smith 438898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 439898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 440898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 441898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4426abc6512SBarry Smith 443416022c9SBarry Smith PLogFlops(2*a->nz); 4446abc6512SBarry Smith return 0; 445da3a660dSBarry Smith } 4469e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 44708480c60SBarry Smith 44808480c60SBarry Smith int MatILUFactorSymbolic_SeqAIJ(Mat A,IS isrow,IS iscol,double f,int levels,Mat *fact) 4499e25ed09SBarry Smith { 450416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data, *b; 4519e25ed09SBarry Smith IS isicol; 452416022c9SBarry Smith int *r,*ic, ierr, prow, n = a->m, *ai = a->i, *aj = a->j; 45356beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 45456beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 455416022c9SBarry Smith int incrlev,nnz,i,shift = a->indexshift; 4569e25ed09SBarry Smith 457416022c9SBarry Smith if (n != a->n) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Matrix must be square"); 458416022c9SBarry Smith if (!isrow) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have row permutation"); 459416022c9SBarry Smith if (!iscol) SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:Must have column permutation"); 4609e25ed09SBarry Smith 46108480c60SBarry Smith /* special case that simply copies fill pattern */ 46208480c60SBarry Smith if (levels == 0 && ISIsIdentity(isrow) && ISIsIdentity(iscol)) { 46308480c60SBarry Smith ierr = MatCopyPrivate_SeqAIJ(A,fact,DO_NOT_COPY_VALUES); CHKERRQ(ierr); 46408480c60SBarry Smith (*fact)->factor = FACTOR_LU; 46508480c60SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 46608480c60SBarry Smith if (!b->diag) { 46708480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(*fact); CHKERRQ(ierr); 46808480c60SBarry Smith } 46908480c60SBarry Smith b->row = isrow; 47008480c60SBarry Smith b->col = iscol; 4710452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc((b->m+1)*sizeof(Scalar));CHKPTRQ(b->solve_work); 47208480c60SBarry Smith return 0; 47308480c60SBarry Smith } 47408480c60SBarry Smith 47578b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 476898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 477898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 4789e25ed09SBarry Smith 4799e25ed09SBarry Smith /* get new row pointers */ 4800452661fSBarry Smith ainew = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 481dbb450caSBarry Smith ainew[0] = -shift; 4829e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 483dbb450caSBarry Smith jmax = (int) (f*(ai[n]+!shift)); 4840452661fSBarry Smith ajnew = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 4859e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 4860452661fSBarry Smith ajfill = (int *) PetscMalloc( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 4879e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 4880452661fSBarry Smith fill = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(fill); 48956beaf04SBarry Smith /* im is level for each filled value */ 4900452661fSBarry Smith im = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(im); 49156beaf04SBarry Smith /* dloc is location of diagonal in factor */ 4920452661fSBarry Smith dloc = (int *) PetscMalloc( (n+1)*sizeof(int)); CHKPTRQ(dloc); 49356beaf04SBarry Smith dloc[0] = 0; 49456beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 4959e25ed09SBarry Smith /* first copy previous fill into linked list */ 49656beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 497dbb450caSBarry Smith xi = aj + ai[r[prow]] + shift; 4989e25ed09SBarry Smith fill[n] = n; 4999e25ed09SBarry Smith while (nz--) { 5009e25ed09SBarry Smith fm = n; 501dbb450caSBarry Smith idx = ic[*xi++ + shift]; 5029e25ed09SBarry Smith do { 5039e25ed09SBarry Smith m = fm; 5049e25ed09SBarry Smith fm = fill[m]; 5059e25ed09SBarry Smith } while (fm < idx); 5069e25ed09SBarry Smith fill[m] = idx; 5079e25ed09SBarry Smith fill[idx] = fm; 50856beaf04SBarry Smith im[idx] = 0; 5099e25ed09SBarry Smith } 51056beaf04SBarry Smith nzi = 0; 5119e25ed09SBarry Smith row = fill[n]; 51256beaf04SBarry Smith while ( row < prow ) { 51356beaf04SBarry Smith incrlev = im[row] + 1; 51456beaf04SBarry Smith nz = dloc[row]; 515dbb450caSBarry Smith xi = ajnew + ainew[row] + shift + nz; 516dbb450caSBarry Smith flev = ajfill + ainew[row] + shift + nz + 1; 517416022c9SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 518dbb450caSBarry Smith if (*xi++ + shift != row) { 519ec8511deSBarry Smith SETERRQ(1,"MatILUFactorSymbolic_SeqAIJ:zero pivot"); 52056beaf04SBarry Smith } 52156beaf04SBarry Smith fm = row; 52256beaf04SBarry Smith while (nnz-- > 0) { 523dbb450caSBarry Smith idx = *xi++ + shift; 52456beaf04SBarry Smith if (*flev + incrlev > levels) { 52556beaf04SBarry Smith flev++; 52656beaf04SBarry Smith continue; 52756beaf04SBarry Smith } 52856beaf04SBarry Smith do { 5299e25ed09SBarry Smith m = fm; 5309e25ed09SBarry Smith fm = fill[m]; 53156beaf04SBarry Smith } while (fm < idx); 5329e25ed09SBarry Smith if (fm != idx) { 53356beaf04SBarry Smith im[idx] = *flev + incrlev; 5349e25ed09SBarry Smith fill[m] = idx; 5359e25ed09SBarry Smith fill[idx] = fm; 5369e25ed09SBarry Smith fm = idx; 53756beaf04SBarry Smith nzf++; 5389e25ed09SBarry Smith } 53956beaf04SBarry Smith else { 54056beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5419e25ed09SBarry Smith } 54256beaf04SBarry Smith flev++; 5439e25ed09SBarry Smith } 5449e25ed09SBarry Smith row = fill[row]; 54556beaf04SBarry Smith nzi++; 5469e25ed09SBarry Smith } 5479e25ed09SBarry Smith /* copy new filled row into permanent storage */ 54856beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 549d7e8b826SBarry Smith if (ainew[prow+1] > jmax-shift) { 5509e25ed09SBarry Smith /* allocate a longer ajnew */ 551bbb6d6a8SBarry Smith int maxadd; 552dbb450caSBarry Smith maxadd = (int) ((f*(ai[n]+!shift)*(n-prow+5))/n); 55356beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 554bbb6d6a8SBarry Smith jmax += maxadd; 5550452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 556416022c9SBarry Smith PetscMemcpy(xi,ajnew,(ainew[prow]+shift)*sizeof(int)); 5570452661fSBarry Smith PetscFree(ajnew); 55856beaf04SBarry Smith ajnew = xi; 5599e25ed09SBarry Smith /* allocate a longer ajfill */ 5600452661fSBarry Smith xi = (int *) PetscMalloc( jmax*sizeof(int) );CHKPTRQ(xi); 561416022c9SBarry Smith PetscMemcpy(xi,ajfill,(ainew[prow]+shift)*sizeof(int)); 5620452661fSBarry Smith PetscFree(ajfill); 56356beaf04SBarry Smith ajfill = xi; 564bbb6d6a8SBarry Smith realloc++; 5659e25ed09SBarry Smith } 566dbb450caSBarry Smith xi = ajnew + ainew[prow] + shift; 567dbb450caSBarry Smith flev = ajfill + ainew[prow] + shift; 56856beaf04SBarry Smith dloc[prow] = nzi; 5699e25ed09SBarry Smith fm = fill[n]; 57056beaf04SBarry Smith while (nzf--) { 571dbb450caSBarry Smith *xi++ = fm - shift; 57256beaf04SBarry Smith *flev++ = im[fm]; 5739e25ed09SBarry Smith fm = fill[fm]; 5749e25ed09SBarry Smith } 5759e25ed09SBarry Smith } 5760452661fSBarry Smith PetscFree(ajfill); 577898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 578898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 579898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 5800452661fSBarry Smith PetscFree(fill); PetscFree(im); 5819e25ed09SBarry Smith 582416022c9SBarry Smith PLogInfo((PetscObject)A, 583ec8511deSBarry Smith "Info:MatILUFactorSymbolic_SeqAIJ:Realloc %d Fill ratio:given %g needed %g\n", 58456beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 585bbb6d6a8SBarry Smith 5869e25ed09SBarry Smith /* put together the new matrix */ 587416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,n, n, 0, 0, fact); CHKERRQ(ierr); 588416022c9SBarry Smith b = (Mat_SeqAIJ *) (*fact)->data; 5890452661fSBarry Smith PetscFree(b->imax); 590416022c9SBarry Smith b->singlemalloc = 0; 591dbb450caSBarry Smith len = (ainew[n] + shift)*sizeof(Scalar); 5929e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 5930452661fSBarry Smith PetscFree(b->a); PetscFree(b->ilen); 5940452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 595416022c9SBarry Smith b->j = ajnew; 596416022c9SBarry Smith b->i = ainew; 59756beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 598416022c9SBarry Smith b->diag = dloc; 599416022c9SBarry Smith b->ilen = 0; 600416022c9SBarry Smith b->imax = 0; 601416022c9SBarry Smith b->row = isrow; 602416022c9SBarry Smith b->col = iscol; 6030452661fSBarry Smith b->solve_work = (Scalar *) PetscMalloc( (n+1)*sizeof(Scalar)); 604416022c9SBarry Smith CHKPTRQ(b->solve_work); 605416022c9SBarry Smith /* In b structure: Free imax, ilen, old a, old j. 60656beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 607dbb450caSBarry Smith PLogObjectMemory(*fact,(ainew[n]+shift-n) * (sizeof(int)+sizeof(Scalar))); 608416022c9SBarry Smith b->maxnz = b->nz = ainew[n] + shift; 6099e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 6109e25ed09SBarry Smith return 0; 6119e25ed09SBarry Smith } 612d5d45c9bSBarry Smith 613d5d45c9bSBarry Smith 614d5d45c9bSBarry Smith 615d5d45c9bSBarry Smith 616