1cb512458SBarry Smith #ifndef lint 2*fafbff53SBarry Smith static char vcid[] = "$Id: aijfact.c,v 1.35 1995/09/06 21:06:35 curfman Exp bsmith $"; 3cb512458SBarry Smith #endif 4289bc588SBarry Smith 5289bc588SBarry Smith 6289bc588SBarry Smith #include "aij.h" 78c37ef55SBarry Smith #include "inline/spops.h" 8289bc588SBarry Smith /* 9289bc588SBarry Smith Factorization code for AIJ format. 10289bc588SBarry Smith */ 11289bc588SBarry Smith 1249d8b64dSBarry Smith int MatLUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,double f,Mat *fact) 13289bc588SBarry Smith { 141fb19edaSLois Curfman McInnes Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew; 15289bc588SBarry Smith IS isicol; 166abc6512SBarry Smith int *r,*ic, ierr, i, n = aij->m, *ai = aij->i, *aj = aij->j; 176abc6512SBarry Smith int *ainew,*ajnew, jmax,*fill, *ajtmp, nz; 182fb3ed76SBarry Smith int *idnew, idx, row,m,fm, nnz, nzi,len, realloc = 0,nzbd,*im; 19289bc588SBarry Smith 20d35516d3SLois Curfman McInnes if (n != aij->n) 21bbb6d6a8SBarry Smith SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must be square"); 22d35516d3SLois Curfman McInnes if (!isrow) 23bbb6d6a8SBarry Smith SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must have row permutation"); 24d35516d3SLois Curfman McInnes if (!iscol) 25bbb6d6a8SBarry Smith SETERRQ(1,"MatLUFactorSymbolic_AIJ:Matrix must have column permutation"); 26289bc588SBarry Smith 2778b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 28289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 29289bc588SBarry Smith 30289bc588SBarry Smith /* get new row pointers */ 3178b31e54SBarry Smith ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 32289bc588SBarry Smith ainew[0] = 1; 33289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 34bbb6d6a8SBarry Smith jmax = (int) (f*ai[n]); 3578b31e54SBarry Smith ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 36289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 372fb3ed76SBarry Smith fill = (int *) PETSCMALLOC( (2*n+1)*sizeof(int)); CHKPTRQ(fill); 382fb3ed76SBarry Smith im = fill + n + 1; 39289bc588SBarry Smith /* idnew is location of diagonal in factor */ 4078b31e54SBarry Smith idnew = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(idnew); 41289bc588SBarry Smith idnew[0] = 1; 42289bc588SBarry Smith 43289bc588SBarry Smith for ( i=0; i<n; i++ ) { 44289bc588SBarry Smith /* first copy previous fill into linked list */ 45289bc588SBarry Smith nnz = nz = ai[r[i]+1] - ai[r[i]]; 46289bc588SBarry Smith ajtmp = aj + ai[r[i]] - 1; 47289bc588SBarry Smith fill[n] = n; 48289bc588SBarry Smith while (nz--) { 49289bc588SBarry Smith fm = n; 50289bc588SBarry Smith idx = ic[*ajtmp++ - 1]; 51289bc588SBarry Smith do { 52289bc588SBarry Smith m = fm; 53289bc588SBarry Smith fm = fill[m]; 54289bc588SBarry Smith } while (fm < idx); 55289bc588SBarry Smith fill[m] = idx; 56289bc588SBarry Smith fill[idx] = fm; 57289bc588SBarry Smith } 58289bc588SBarry Smith row = fill[n]; 59289bc588SBarry Smith while ( row < i ) { 602fb3ed76SBarry Smith ajtmp = ajnew + idnew[row]; 612fb3ed76SBarry Smith nzbd = 1 + idnew[row] - ainew[row]; 622fb3ed76SBarry Smith nz = im[row] - nzbd; 63289bc588SBarry Smith fm = row; 642fb3ed76SBarry Smith while (nz-- > 0) { 652fb3ed76SBarry Smith /* fm = n; */ 66289bc588SBarry Smith idx = *ajtmp++ - 1; 672fb3ed76SBarry Smith nzbd++; 682fb3ed76SBarry Smith if (idx == i) im[row] = nzbd; 69289bc588SBarry Smith do { 70289bc588SBarry Smith m = fm; 71289bc588SBarry Smith fm = fill[m]; 72289bc588SBarry Smith } while (fm < idx); 73289bc588SBarry Smith if (fm != idx) { 74289bc588SBarry Smith fill[m] = idx; 75289bc588SBarry Smith fill[idx] = fm; 76289bc588SBarry Smith fm = idx; 77289bc588SBarry Smith nnz++; 78289bc588SBarry Smith } 79d5f53d93SBarry Smith /* printf("i %d row %d nz %d idx %d fm %d\n",i,row,nz,idx,fm); */ 80289bc588SBarry Smith } 81289bc588SBarry Smith row = fill[row]; 82289bc588SBarry Smith } 83289bc588SBarry Smith /* copy new filled row into permanent storage */ 84289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 85289bc588SBarry Smith if (ainew[i+1] > jmax+1) { 86289bc588SBarry Smith /* allocate a longer ajnew */ 872fb3ed76SBarry Smith int maxadd; 88bbb6d6a8SBarry Smith maxadd = (int) ((f*ai[n]*(n-i+5))/n); 89bbb6d6a8SBarry Smith if (maxadd < nnz) maxadd = (n-i)*(nnz+1); 902fb3ed76SBarry Smith jmax += maxadd; 9178b31e54SBarry Smith ajtmp = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(ajtmp); 9278b31e54SBarry Smith PETSCMEMCPY(ajtmp,ajnew,(ainew[i]-1)*sizeof(int)); 9378b31e54SBarry Smith PETSCFREE(ajnew); 94289bc588SBarry Smith ajnew = ajtmp; 952fb3ed76SBarry Smith realloc++; /* count how many times we realloc */ 96289bc588SBarry Smith } 97289bc588SBarry Smith ajtmp = ajnew + ainew[i] - 1; 98289bc588SBarry Smith fm = fill[n]; 99289bc588SBarry Smith nzi = 0; 1002fb3ed76SBarry Smith im[i] = nnz; 101289bc588SBarry Smith while (nnz--) { 102289bc588SBarry Smith if (fm < i) nzi++; 103289bc588SBarry Smith *ajtmp++ = fm + 1; 104289bc588SBarry Smith fm = fill[fm]; 105289bc588SBarry Smith } 106289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 107289bc588SBarry Smith } 108289bc588SBarry Smith 1092fb3ed76SBarry Smith PLogInfo((PetscObject)mat, 110bbb6d6a8SBarry Smith "Info:MatLUFactorSymbolic_AIJ:Reallocs %d Fill ratio:given %g needed %g\n", 111bbb6d6a8SBarry Smith realloc,f,((double)ainew[n])/((double)ai[i])); 1122fb3ed76SBarry Smith 113898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 114898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 115898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 116898183ecSLois Curfman McInnes PETSCFREE(fill); 117289bc588SBarry Smith 118289bc588SBarry Smith /* put together the new matrix */ 119*fafbff53SBarry Smith ierr = MatCreateSeqAIJ(mat->comm,n, n, 0, 0, fact); CHKERRQ(ierr); 1201fb19edaSLois Curfman McInnes aijnew = (Mat_AIJ *) (*fact)->data; 12178b31e54SBarry Smith PETSCFREE(aijnew->imax); 122289bc588SBarry Smith aijnew->singlemalloc = 0; 123f0479e8cSBarry Smith len = (ainew[n] - 1)*sizeof(Scalar); 124e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 12578b31e54SBarry Smith PETSCFREE(aijnew->a); PETSCFREE(aijnew->ilen); 12678b31e54SBarry Smith aijnew->a = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(aijnew->a); 127289bc588SBarry Smith aijnew->j = ajnew; 128289bc588SBarry Smith aijnew->i = ainew; 1298c37ef55SBarry Smith aijnew->diag = idnew; 130e8d4e0b9SBarry Smith aijnew->ilen = 0; 13120563c6bSBarry Smith aijnew->imax = 0; 13286d7a8ceSBarry Smith aijnew->row = isrow; 13386d7a8ceSBarry Smith aijnew->col = iscol; 13478b31e54SBarry Smith aijnew->solve_work = (Scalar *) PETSCMALLOC( n*sizeof(Scalar)); 13578b31e54SBarry Smith CHKPTRQ(aijnew->solve_work); 1367fd98bd6SLois Curfman McInnes /* In aijnew structure: Free imax, ilen, old a, old j. 1377fd98bd6SLois Curfman McInnes Allocate idnew, solve_work, new a, new j */ 138033c1e87SLois Curfman McInnes PLogObjectMemory(*fact,(ainew[n]-1-n)*(sizeof(int)+sizeof(Scalar))); 1397fd98bd6SLois Curfman McInnes aijnew->maxnz = aijnew->nz = ainew[n] - 1; 1407fd98bd6SLois Curfman McInnes 1415e42470aSBarry Smith /* Cannot do this here because child is destroyed before parent created 1425e42470aSBarry Smith PLogObjectParent(*fact,isicol); */ 143289bc588SBarry Smith return 0; 144289bc588SBarry Smith } 1450a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 1461fb19edaSLois Curfman McInnes int MatLUFactorNumeric_AIJ(Mat mat,Mat *infact) 147289bc588SBarry Smith { 14820563c6bSBarry Smith Mat fact = *infact; 1491fb19edaSLois Curfman McInnes Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew = (Mat_AIJ *)fact->data; 15086d7a8ceSBarry Smith IS iscol = aijnew->col, isrow = aijnew->row, isicol; 151289bc588SBarry Smith int *r,*ic, ierr, i, j, n = aij->m, *ai = aijnew->i, *aj = aijnew->j; 1526abc6512SBarry Smith int *ajtmpold, *ajtmp, nz, row,*pj; 1536abc6512SBarry Smith Scalar *rtmp,*v, *pv, *pc, multiplier; 154289bc588SBarry Smith 15578b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 1565e42470aSBarry Smith PLogObjectParent(*infact,isicol); 15778b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 15878b31e54SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 15978b31e54SBarry Smith rtmp = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar) ); CHKPTRQ(rtmp); 160289bc588SBarry Smith 161289bc588SBarry Smith for ( i=0; i<n; i++ ) { 162289bc588SBarry Smith nz = ai[i+1] - ai[i]; 163289bc588SBarry Smith ajtmp = aj + ai[i] - 1; 164289bc588SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ajtmp[j]-1] = 0.0; 165289bc588SBarry Smith 166289bc588SBarry Smith /* load in initial (unfactored row) */ 1678c37ef55SBarry Smith nz = aij->i[r[i]+1] - aij->i[r[i]]; 1688c37ef55SBarry Smith ajtmpold = aij->j + aij->i[r[i]] - 1; 1698c37ef55SBarry Smith v = aij->a + aij->i[r[i]] - 1; 1708c37ef55SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ic[ajtmpold[j]-1]] = v[j]; 171289bc588SBarry Smith 1728c37ef55SBarry Smith row = *ajtmp++ - 1; 173289bc588SBarry Smith while (row < i) { 1748c37ef55SBarry Smith pc = rtmp + row; 1758c37ef55SBarry Smith if (*pc != 0.0) { 1768c37ef55SBarry Smith nz = aijnew->diag[row] - ai[row]; 1778c37ef55SBarry Smith pv = aijnew->a + aijnew->diag[row] - 1; 1788c37ef55SBarry Smith pj = aijnew->j + aijnew->diag[row]; 1798c37ef55SBarry Smith multiplier = *pc * *pv++; 1808c37ef55SBarry Smith *pc = multiplier; 1818c37ef55SBarry Smith nz = ai[row+1] - ai[row] - 1 - nz; 182eef2e97bSBarry Smith PLogFlops(2*nz); 1838c37ef55SBarry Smith while (nz-->0) rtmp[*pj++ - 1] -= multiplier* *pv++; 184289bc588SBarry Smith } 1858c37ef55SBarry Smith row = *ajtmp++ - 1; 186289bc588SBarry Smith } 1878c37ef55SBarry Smith /* finished row so stick it into aijnew->a */ 1888c37ef55SBarry Smith pv = aijnew->a + ai[i] - 1; 1898c37ef55SBarry Smith pj = aijnew->j + ai[i] - 1; 1908c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 191bbb6d6a8SBarry Smith if (rtmp[i] == 0.0) {SETERRQ(1,"MatLUFactorNumeric_AIJ:Zero pivot");} 1928c37ef55SBarry Smith rtmp[i] = 1.0/rtmp[i]; 1938c37ef55SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmp[pj[j]-1];} 1948c37ef55SBarry Smith } 19578b31e54SBarry Smith PETSCFREE(rtmp); 19678b31e54SBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 19778b31e54SBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 19878b31e54SBarry Smith ierr = ISDestroy(isicol); CHKERRQ(ierr); 1998c37ef55SBarry Smith fact->factor = FACTOR_LU; 2009e25ed09SBarry Smith aijnew->assembled = 1; 201eef2e97bSBarry Smith PLogFlops(aijnew->n); 202289bc588SBarry Smith return 0; 203289bc588SBarry Smith } 2040a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 20549d8b64dSBarry Smith int MatLUFactor_AIJ(Mat matin,IS row,IS col,double f) 206da3a660dSBarry Smith { 2071fb19edaSLois Curfman McInnes Mat_AIJ *mat = (Mat_AIJ *) matin->data; 2086abc6512SBarry Smith int ierr; 209da3a660dSBarry Smith Mat fact; 21049d8b64dSBarry Smith ierr = MatLUFactorSymbolic_AIJ(matin,row,col,f,&fact); CHKERRQ(ierr); 21178b31e54SBarry Smith ierr = MatLUFactorNumeric_AIJ(matin,&fact); CHKERRQ(ierr); 212da3a660dSBarry Smith 213da3a660dSBarry Smith /* free all the data structures from mat */ 21478b31e54SBarry Smith PETSCFREE(mat->a); 21578b31e54SBarry Smith if (!mat->singlemalloc) {PETSCFREE(mat->i); PETSCFREE(mat->j);} 21678b31e54SBarry Smith if (mat->diag) PETSCFREE(mat->diag); 21778b31e54SBarry Smith if (mat->ilen) PETSCFREE(mat->ilen); 21878b31e54SBarry Smith if (mat->imax) PETSCFREE(mat->imax); 21994473badSLois Curfman McInnes if (mat->solve_work) PETSCFREE(mat->solve_work); 22078b31e54SBarry Smith PETSCFREE(mat); 221da3a660dSBarry Smith 22278b31e54SBarry Smith PETSCMEMCPY(matin,fact,sizeof(struct _Mat)); 22336d154e2SLois Curfman McInnes PETSCHEADERDESTROY(fact); 224da3a660dSBarry Smith return 0; 225da3a660dSBarry Smith } 2260a6dbcc7SLois Curfman McInnes /* ----------------------------------------------------------- */ 2271fb19edaSLois Curfman McInnes int MatSolve_AIJ(Mat mat,Vec bb, Vec xx) 2288c37ef55SBarry Smith { 2291fb19edaSLois Curfman McInnes Mat_AIJ *aij = (Mat_AIJ *) mat->data; 23086d7a8ceSBarry Smith IS iscol = aij->col, isrow = aij->row; 2316abc6512SBarry Smith int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; 2328c37ef55SBarry Smith int nz; 2338c37ef55SBarry Smith Scalar *x,*b,*tmp, *aa = aij->a, sum, *v; 2348c37ef55SBarry Smith 235d35516d3SLois Curfman McInnes if (mat->factor != FACTOR_LU) 236bbb6d6a8SBarry Smith SETERRQ(1,"MatSolve_AIJ:Cannot solve with unfactored matrix"); 2379e25ed09SBarry Smith 23878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 23978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 240eef2e97bSBarry Smith tmp = aij->solve_work; 2418c37ef55SBarry Smith 24278b31e54SBarry Smith ierr = ISGetIndices(isrow,&r);CHKERRQ(ierr); 24378b31e54SBarry Smith ierr = ISGetIndices(iscol,&c);CHKERRQ(ierr); c = c + (n-1); 2448c37ef55SBarry Smith 2458c37ef55SBarry Smith /* forward solve the lower triangular */ 2468c37ef55SBarry Smith tmp[0] = b[*r++]; 2478c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 2488c37ef55SBarry Smith v = aa + ai[i] - 1; 2498c37ef55SBarry Smith vi = aj + ai[i] - 1; 2508c37ef55SBarry Smith nz = aij->diag[i] - ai[i]; 2518c37ef55SBarry Smith sum = b[*r++]; 2528c37ef55SBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 2538c37ef55SBarry Smith tmp[i] = sum; 2548c37ef55SBarry Smith } 2558c37ef55SBarry Smith 2568c37ef55SBarry Smith /* backward solve the upper triangular */ 2578c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 2588c37ef55SBarry Smith v = aa + aij->diag[i]; 2598c37ef55SBarry Smith vi = aj + aij->diag[i]; 2608c37ef55SBarry Smith nz = ai[i+1] - aij->diag[i] - 1; 2618c37ef55SBarry Smith sum = tmp[i]; 2628c37ef55SBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 2638c37ef55SBarry Smith x[*c--] = tmp[i] = sum*aa[aij->diag[i]-1]; 2648c37ef55SBarry Smith } 2658c37ef55SBarry Smith 266898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 267898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 268eef2e97bSBarry Smith PLogFlops(2*aij->nz - aij->n); 2698c37ef55SBarry Smith return 0; 2708c37ef55SBarry Smith } 2711fb19edaSLois Curfman McInnes int MatSolveAdd_AIJ(Mat mat,Vec bb, Vec yy, Vec xx) 272da3a660dSBarry Smith { 2731fb19edaSLois Curfman McInnes Mat_AIJ *aij = (Mat_AIJ *) mat->data; 27486d7a8ceSBarry Smith IS iscol = aij->col, isrow = aij->row; 2756abc6512SBarry Smith int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; 276da3a660dSBarry Smith int nz; 277da3a660dSBarry Smith Scalar *x,*b,*tmp, *aa = aij->a, sum, *v; 278da3a660dSBarry Smith 279d35516d3SLois Curfman McInnes if (mat->factor != FACTOR_LU) 280bbb6d6a8SBarry Smith SETERRQ(1,"MatSolveAdd_AIJ: Cannot solve with unfactored matrix"); 28178b31e54SBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERRQ(ierr);} 282da3a660dSBarry Smith 28378b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 28478b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 285eef2e97bSBarry Smith tmp = aij->solve_work; 286da3a660dSBarry Smith 28778b31e54SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 28878b31e54SBarry Smith ierr = ISGetIndices(iscol,&c); CHKERRQ(ierr); c = c + (n-1); 289da3a660dSBarry Smith 290da3a660dSBarry Smith /* forward solve the lower triangular */ 291da3a660dSBarry Smith tmp[0] = b[*r++]; 292da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 293da3a660dSBarry Smith v = aa + ai[i] - 1; 294da3a660dSBarry Smith vi = aj + ai[i] - 1; 295da3a660dSBarry Smith nz = aij->diag[i] - ai[i]; 296da3a660dSBarry Smith sum = b[*r++]; 297da3a660dSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 298da3a660dSBarry Smith tmp[i] = sum; 299da3a660dSBarry Smith } 300da3a660dSBarry Smith 301da3a660dSBarry Smith /* backward solve the upper triangular */ 302da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 303da3a660dSBarry Smith v = aa + aij->diag[i]; 304da3a660dSBarry Smith vi = aj + aij->diag[i]; 305da3a660dSBarry Smith nz = ai[i+1] - aij->diag[i] - 1; 306da3a660dSBarry Smith sum = tmp[i]; 307da3a660dSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 308da3a660dSBarry Smith tmp[i] = sum*aa[aij->diag[i]-1]; 309da3a660dSBarry Smith x[*c--] += tmp[i]; 310da3a660dSBarry Smith } 311da3a660dSBarry Smith 312898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 313898183ecSLois Curfman McInnes ierr = ISRestoreIndices(iscol,&c); CHKERRQ(ierr); 314eef2e97bSBarry Smith PLogFlops(2*aij->nz); 315898183ecSLois Curfman McInnes 316da3a660dSBarry Smith return 0; 317da3a660dSBarry Smith } 318da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 3191fb19edaSLois Curfman McInnes int MatSolveTrans_AIJ(Mat mat,Vec bb, Vec xx) 320da3a660dSBarry Smith { 3211fb19edaSLois Curfman McInnes Mat_AIJ *aij = (Mat_AIJ *) mat->data; 32286d7a8ceSBarry Smith IS iscol = aij->col, isrow = aij->row, invisrow,inviscol; 3236abc6512SBarry Smith int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; 324da3a660dSBarry Smith int nz; 3256abc6512SBarry Smith Scalar *x,*b,*tmp, *aa = aij->a, *v; 326da3a660dSBarry Smith 327d35516d3SLois Curfman McInnes if (mat->factor != FACTOR_LU) 328bbb6d6a8SBarry Smith SETERRQ(1,"MatSolveTrans_AIJ:Cannot solve with unfactored matrix"); 32978b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 33078b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 331eef2e97bSBarry Smith tmp = aij->solve_work; 332da3a660dSBarry Smith 333da3a660dSBarry Smith /* invert the permutations */ 33478b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 33578b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 336da3a660dSBarry Smith 33778b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 33878b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 339da3a660dSBarry Smith 340da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 341da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 342da3a660dSBarry Smith 343da3a660dSBarry Smith /* forward solve the U^T */ 344da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 345da3a660dSBarry Smith v = aa + aij->diag[i] - 1; 346da3a660dSBarry Smith vi = aj + aij->diag[i]; 347da3a660dSBarry Smith nz = ai[i+1] - aij->diag[i] - 1; 348da3a660dSBarry Smith tmp[i] *= *v++; 349da3a660dSBarry Smith while (nz--) { 350da3a660dSBarry Smith tmp[*vi++ - 1] -= (*v++)*tmp[i]; 351da3a660dSBarry Smith } 352da3a660dSBarry Smith } 353da3a660dSBarry Smith 354da3a660dSBarry Smith /* backward solve the L^T */ 355da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 356da3a660dSBarry Smith v = aa + aij->diag[i] - 2; 357da3a660dSBarry Smith vi = aj + aij->diag[i] - 2; 358da3a660dSBarry Smith nz = aij->diag[i] - ai[i]; 359da3a660dSBarry Smith while (nz--) { 360da3a660dSBarry Smith tmp[*vi-- - 1] -= (*v--)*tmp[i]; 361da3a660dSBarry Smith } 362da3a660dSBarry Smith } 363da3a660dSBarry Smith 364da3a660dSBarry Smith /* copy tmp into x according to permutation */ 365da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 366da3a660dSBarry Smith 367898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 368898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 369898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 370898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 371da3a660dSBarry Smith 372eef2e97bSBarry Smith PLogFlops(2*aij->nz-aij->n); 373da3a660dSBarry Smith return 0; 374da3a660dSBarry Smith } 375da3a660dSBarry Smith 3761fb19edaSLois Curfman McInnes int MatSolveTransAdd_AIJ(Mat mat,Vec bb, Vec zz,Vec xx) 377da3a660dSBarry Smith { 3781fb19edaSLois Curfman McInnes Mat_AIJ *aij = (Mat_AIJ *) mat->data; 37986d7a8ceSBarry Smith IS iscol = aij->col, isrow = aij->row, invisrow,inviscol; 3806abc6512SBarry Smith int *r,*c, ierr, i, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; 3816abc6512SBarry Smith int nz; 3826abc6512SBarry Smith Scalar *x,*b,*tmp, *aa = aij->a, *v; 3836abc6512SBarry Smith 384d35516d3SLois Curfman McInnes if (mat->factor != FACTOR_LU) 385bbb6d6a8SBarry Smith SETERRQ(1,"MatSolveTransAdd_AIJ:Cannot solve with unfactored matrix"); 3866abc6512SBarry Smith if (zz != xx) VecCopy(zz,xx); 3876abc6512SBarry Smith 38878b31e54SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 38978b31e54SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 390eef2e97bSBarry Smith tmp = aij->solve_work; 3916abc6512SBarry Smith 3926abc6512SBarry Smith /* invert the permutations */ 39378b31e54SBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERRQ(ierr); 39478b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERRQ(ierr); 39578b31e54SBarry Smith ierr = ISGetIndices(invisrow,&r); CHKERRQ(ierr); 39678b31e54SBarry Smith ierr = ISGetIndices(inviscol,&c); CHKERRQ(ierr); 3976abc6512SBarry Smith 3986abc6512SBarry Smith /* copy the b into temp work space according to permutation */ 3996abc6512SBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 4006abc6512SBarry Smith 4016abc6512SBarry Smith /* forward solve the U^T */ 4026abc6512SBarry Smith for ( i=0; i<n; i++ ) { 4036abc6512SBarry Smith v = aa + aij->diag[i] - 1; 4046abc6512SBarry Smith vi = aj + aij->diag[i]; 4056abc6512SBarry Smith nz = ai[i+1] - aij->diag[i] - 1; 4066abc6512SBarry Smith tmp[i] *= *v++; 4076abc6512SBarry Smith while (nz--) { 4086abc6512SBarry Smith tmp[*vi++ - 1] -= (*v++)*tmp[i]; 4096abc6512SBarry Smith } 4106abc6512SBarry Smith } 4116abc6512SBarry Smith 4126abc6512SBarry Smith /* backward solve the L^T */ 4136abc6512SBarry Smith for ( i=n-1; i>=0; i-- ){ 4146abc6512SBarry Smith v = aa + aij->diag[i] - 2; 4156abc6512SBarry Smith vi = aj + aij->diag[i] - 2; 4166abc6512SBarry Smith nz = aij->diag[i] - ai[i]; 4176abc6512SBarry Smith while (nz--) { 4186abc6512SBarry Smith tmp[*vi-- - 1] -= (*v--)*tmp[i]; 4196abc6512SBarry Smith } 4206abc6512SBarry Smith } 4216abc6512SBarry Smith 4226abc6512SBarry Smith /* copy tmp into x according to permutation */ 4236abc6512SBarry Smith for ( i=0; i<n; i++ ) x[r[i]] += tmp[i]; 4246abc6512SBarry Smith 425898183ecSLois Curfman McInnes ierr = ISRestoreIndices(invisrow,&r); CHKERRQ(ierr); 426898183ecSLois Curfman McInnes ierr = ISRestoreIndices(inviscol,&c); CHKERRQ(ierr); 427898183ecSLois Curfman McInnes ierr = ISDestroy(invisrow); CHKERRQ(ierr); 428898183ecSLois Curfman McInnes ierr = ISDestroy(inviscol); CHKERRQ(ierr); 4296abc6512SBarry Smith 430eef2e97bSBarry Smith PLogFlops(2*aij->nz); 4316abc6512SBarry Smith return 0; 432da3a660dSBarry Smith } 4339e25ed09SBarry Smith /* ----------------------------------------------------------------*/ 43449d8b64dSBarry Smith int MatILUFactorSymbolic_AIJ(Mat mat,IS isrow,IS iscol,double f, 43549d8b64dSBarry Smith int levels,Mat *fact) 4369e25ed09SBarry Smith { 4371fb19edaSLois Curfman McInnes Mat_AIJ *aij = (Mat_AIJ *) mat->data, *aijnew; 4389e25ed09SBarry Smith IS isicol; 43956beaf04SBarry Smith int *r,*ic, ierr, prow, n = aij->m, *ai = aij->i, *aj = aij->j; 44056beaf04SBarry Smith int *ainew,*ajnew, jmax,*fill, *xi, nz, *im,*ajfill,*flev; 44156beaf04SBarry Smith int *dloc, idx, row,m,fm, nzf, nzi,len, realloc = 0; 44256beaf04SBarry Smith int incrlev,nnz,i; 4439e25ed09SBarry Smith 444d35516d3SLois Curfman McInnes if (n != aij->n) 445bbb6d6a8SBarry Smith SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must be square"); 446d35516d3SLois Curfman McInnes if (!isrow) 447bbb6d6a8SBarry Smith SETERRQ(1,"MatILUFactorSymbolic_AIJ:Matrix must have row permutation"); 448d35516d3SLois Curfman McInnes if (!iscol) SETERRQ(1, 449bbb6d6a8SBarry Smith "MatILUFactorSymbolic_AIJ:Matrix must have column permutation"); 4509e25ed09SBarry Smith 45178b31e54SBarry Smith ierr = ISInvertPermutation(iscol,&isicol); CHKERRQ(ierr); 452898183ecSLois Curfman McInnes ierr = ISGetIndices(isrow,&r); CHKERRQ(ierr); 453898183ecSLois Curfman McInnes ierr = ISGetIndices(isicol,&ic); CHKERRQ(ierr); 4549e25ed09SBarry Smith 4559e25ed09SBarry Smith /* get new row pointers */ 45678b31e54SBarry Smith ainew = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ainew); 4579e25ed09SBarry Smith ainew[0] = 1; 4589e25ed09SBarry Smith /* don't know how many column pointers are needed so estimate */ 459bbb6d6a8SBarry Smith jmax = (int) (f*ai[n]); 46078b31e54SBarry Smith ajnew = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajnew); 4619e25ed09SBarry Smith /* ajfill is level of fill for each fill entry */ 46278b31e54SBarry Smith ajfill = (int *) PETSCMALLOC( (jmax)*sizeof(int) ); CHKPTRQ(ajfill); 4639e25ed09SBarry Smith /* fill is a linked list of nonzeros in active row */ 46456beaf04SBarry Smith fill = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(fill); 46556beaf04SBarry Smith /* im is level for each filled value */ 46656beaf04SBarry Smith im = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(im); 46756beaf04SBarry Smith /* dloc is location of diagonal in factor */ 46856beaf04SBarry Smith dloc = (int *) PETSCMALLOC( (n+1)*sizeof(int)); CHKPTRQ(dloc); 46956beaf04SBarry Smith dloc[0] = 0; 4709e25ed09SBarry Smith 47156beaf04SBarry Smith for ( prow=0; prow<n; prow++ ) { 4729e25ed09SBarry Smith /* first copy previous fill into linked list */ 47356beaf04SBarry Smith nzf = nz = ai[r[prow]+1] - ai[r[prow]]; 47456beaf04SBarry Smith xi = aj + ai[r[prow]] - 1; 4759e25ed09SBarry Smith fill[n] = n; 4769e25ed09SBarry Smith while (nz--) { 4779e25ed09SBarry Smith fm = n; 47856beaf04SBarry Smith idx = ic[*xi++ - 1]; 4799e25ed09SBarry Smith do { 4809e25ed09SBarry Smith m = fm; 4819e25ed09SBarry Smith fm = fill[m]; 4829e25ed09SBarry Smith } while (fm < idx); 4839e25ed09SBarry Smith fill[m] = idx; 4849e25ed09SBarry Smith fill[idx] = fm; 48556beaf04SBarry Smith im[idx] = 0; 4869e25ed09SBarry Smith } 48756beaf04SBarry Smith nzi = 0; 4889e25ed09SBarry Smith row = fill[n]; 48956beaf04SBarry Smith while ( row < prow ) { 49056beaf04SBarry Smith incrlev = im[row] + 1; 49156beaf04SBarry Smith nz = dloc[row]; 49256beaf04SBarry Smith xi = ajnew + ainew[row] - 1 + nz; 49356beaf04SBarry Smith flev = ajfill + ainew[row] - 1 + nz + 1; 49456beaf04SBarry Smith nnz = ainew[row+1] - ainew[row] - nz - 1; 49556beaf04SBarry Smith if (*xi++ - 1 != row) { 49656beaf04SBarry Smith SETERRQ(1,"MatILUFactorSymbolic_AIJ:zero pivot"); 49756beaf04SBarry Smith } 49856beaf04SBarry Smith fm = row; 49956beaf04SBarry Smith while (nnz-- > 0) { 50056beaf04SBarry Smith idx = *xi++ - 1; 50156beaf04SBarry Smith if (*flev + incrlev > levels) { 50256beaf04SBarry Smith flev++; 50356beaf04SBarry Smith continue; 50456beaf04SBarry Smith } 50556beaf04SBarry Smith do { 5069e25ed09SBarry Smith m = fm; 5079e25ed09SBarry Smith fm = fill[m]; 50856beaf04SBarry Smith } while (fm < idx); 5099e25ed09SBarry Smith if (fm != idx) { 51056beaf04SBarry Smith im[idx] = *flev + incrlev; 5119e25ed09SBarry Smith fill[m] = idx; 5129e25ed09SBarry Smith fill[idx] = fm; 5139e25ed09SBarry Smith fm = idx; 51456beaf04SBarry Smith nzf++; 5159e25ed09SBarry Smith } 51656beaf04SBarry Smith else { 51756beaf04SBarry Smith if (im[idx] > *flev + incrlev) im[idx] = *flev+incrlev; 5189e25ed09SBarry Smith } 51956beaf04SBarry Smith flev++; 5209e25ed09SBarry Smith } 5219e25ed09SBarry Smith row = fill[row]; 52256beaf04SBarry Smith nzi++; 5239e25ed09SBarry Smith } 5249e25ed09SBarry Smith /* copy new filled row into permanent storage */ 52556beaf04SBarry Smith ainew[prow+1] = ainew[prow] + nzf; 52656beaf04SBarry Smith if (ainew[prow+1] > jmax+1) { 5279e25ed09SBarry Smith /* allocate a longer ajnew */ 528bbb6d6a8SBarry Smith int maxadd; 52956beaf04SBarry Smith maxadd = (int) ((f*ai[n]*(n-prow+5))/n); 53056beaf04SBarry Smith if (maxadd < nzf) maxadd = (n-prow)*(nzf+1); 531bbb6d6a8SBarry Smith jmax += maxadd; 53256beaf04SBarry Smith xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi); 53356beaf04SBarry Smith PETSCMEMCPY(xi,ajnew,(ainew[prow]-1)*sizeof(int)); 53478b31e54SBarry Smith PETSCFREE(ajnew); 53556beaf04SBarry Smith ajnew = xi; 5369e25ed09SBarry Smith /* allocate a longer ajfill */ 53756beaf04SBarry Smith xi = (int *) PETSCMALLOC( jmax*sizeof(int) );CHKPTRQ(xi); 53856beaf04SBarry Smith PETSCMEMCPY(xi,ajfill,(ainew[prow]-1)*sizeof(int)); 53978b31e54SBarry Smith PETSCFREE(ajfill); 54056beaf04SBarry Smith ajfill = xi; 541bbb6d6a8SBarry Smith realloc++; 5429e25ed09SBarry Smith } 54356beaf04SBarry Smith xi = ajnew + ainew[prow] - 1; 54456beaf04SBarry Smith flev = ajfill + ainew[prow] - 1; 54556beaf04SBarry Smith dloc[prow] = nzi; 5469e25ed09SBarry Smith fm = fill[n]; 54756beaf04SBarry Smith while (nzf--) { 54856beaf04SBarry Smith *xi++ = fm + 1; 54956beaf04SBarry Smith *flev++ = im[fm]; 5509e25ed09SBarry Smith fm = fill[fm]; 5519e25ed09SBarry Smith } 5529e25ed09SBarry Smith } 55378b31e54SBarry Smith PETSCFREE(ajfill); 554898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isrow,&r); CHKERRQ(ierr); 555898183ecSLois Curfman McInnes ierr = ISRestoreIndices(isicol,&ic); CHKERRQ(ierr); 556898183ecSLois Curfman McInnes ierr = ISDestroy(isicol); CHKERRQ(ierr); 557898183ecSLois Curfman McInnes PETSCFREE(fill); PETSCFREE(im); 5589e25ed09SBarry Smith 559bbb6d6a8SBarry Smith PLogInfo((PetscObject)mat, 560bbb6d6a8SBarry Smith "Info:MatILUFactorSymbolic_AIJ:Realloc %d Fill ratio:given %g needed %g\n", 56156beaf04SBarry Smith realloc,f,((double)ainew[n])/((double)ai[prow])); 562bbb6d6a8SBarry Smith 5639e25ed09SBarry Smith /* put together the new matrix */ 564*fafbff53SBarry Smith ierr = MatCreateSeqAIJ(mat->comm,n, n, 0, 0, fact); CHKERRQ(ierr); 5651fb19edaSLois Curfman McInnes aijnew = (Mat_AIJ *) (*fact)->data; 56678b31e54SBarry Smith PETSCFREE(aijnew->imax); 5679e25ed09SBarry Smith aijnew->singlemalloc = 0; 5689e25ed09SBarry Smith len = (ainew[n] - 1)*sizeof(Scalar); 5699e25ed09SBarry Smith /* the next line frees the default space generated by the Create() */ 57078b31e54SBarry Smith PETSCFREE(aijnew->a); PETSCFREE(aijnew->ilen); 57178b31e54SBarry Smith aijnew->a = (Scalar *) PETSCMALLOC( len ); CHKPTRQ(aijnew->a); 5729e25ed09SBarry Smith aijnew->j = ajnew; 5739e25ed09SBarry Smith aijnew->i = ainew; 57456beaf04SBarry Smith for ( i=0; i<n; i++ ) dloc[i] += ainew[i]; 57556beaf04SBarry Smith aijnew->diag = dloc; 5769e25ed09SBarry Smith aijnew->ilen = 0; 5779e25ed09SBarry Smith aijnew->imax = 0; 57886d7a8ceSBarry Smith aijnew->row = isrow; 57986d7a8ceSBarry Smith aijnew->col = iscol; 5805392566eSBarry Smith aijnew->solve_work = (Scalar *) PETSCMALLOC( (n+1)*sizeof(Scalar)); 58178b31e54SBarry Smith CHKPTRQ(aijnew->solve_work); 5827fd98bd6SLois Curfman McInnes /* In aijnew structure: Free imax, ilen, old a, old j. 58356beaf04SBarry Smith Allocate dloc, solve_work, new a, new j */ 584033c1e87SLois Curfman McInnes PLogObjectMemory(*fact,(ainew[n]-1-n) * (sizeof(int)+sizeof(Scalar))); 5857fd98bd6SLois Curfman McInnes aijnew->maxnz = aijnew->nz = ainew[n] - 1; 5869e25ed09SBarry Smith (*fact)->factor = FACTOR_LU; 5879e25ed09SBarry Smith return 0; 5889e25ed09SBarry Smith } 589