1289bc588SBarry Smith 2289bc588SBarry Smith 3289bc588SBarry Smith #include "aij.h" 48c37ef55SBarry Smith #include "inline/spops.h" 5289bc588SBarry Smith /* 6289bc588SBarry Smith Factorization code for AIJ format. 7289bc588SBarry Smith */ 8289bc588SBarry Smith 9289bc588SBarry Smith int MatiAIJLUFactorSymbolic(Mat mat,IS isrow,IS iscol,Mat *fact) 10289bc588SBarry Smith { 11289bc588SBarry Smith Matiaij *aij = (Matiaij *) mat->data, *aijnew; 12289bc588SBarry Smith IS isicol; 13289bc588SBarry Smith int *r,*ic, ierr, i, j, n = aij->m, *ai = aij->i, *aj = aij->j; 14289bc588SBarry Smith int prow, *ainew,*ajnew, jmax,*fill, *ajtmp, nz , *ii; 15289bc588SBarry Smith int *idnew, idx, pivot_row,row,m,fm, nnz, nzi,len; 16289bc588SBarry Smith 17289bc588SBarry Smith if (n != aij->n) SETERR(1,"Mat must be square"); 18289bc588SBarry Smith if (!isrow) {SETERR(1,"Must have row permutation");} 19289bc588SBarry Smith if (!iscol) {SETERR(1,"Must have column permutation");} 20289bc588SBarry Smith 21289bc588SBarry Smith if (ierr = ISInvertPermutation(iscol,&isicol)) SETERR(ierr,0); 22289bc588SBarry Smith ISGetIndices(isrow,&r); ISGetIndices(isicol,&ic); 23289bc588SBarry Smith 24289bc588SBarry Smith /* get new row pointers */ 25289bc588SBarry Smith ainew = (int *) MALLOC( (n+1)*sizeof(int) ); CHKPTR(ainew); 26289bc588SBarry Smith ainew[0] = 1; 27289bc588SBarry Smith /* don't know how many column pointers are needed so estimate */ 28289bc588SBarry Smith jmax = 2*ai[n]; 29289bc588SBarry Smith ajnew = (int *) MALLOC( (jmax)*sizeof(int) ); CHKPTR(ajnew); 30289bc588SBarry Smith /* fill is a linked list of nonzeros in active row */ 31289bc588SBarry Smith fill = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(fill); 32289bc588SBarry Smith /* idnew is location of diagonal in factor */ 33289bc588SBarry Smith idnew = (int *) MALLOC( (n+1)*sizeof(int)); CHKPTR(idnew); 34289bc588SBarry Smith idnew[0] = 1; 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]]; 39289bc588SBarry Smith ajtmp = aj + ai[r[i]] - 1; 40289bc588SBarry Smith fill[n] = n; 41289bc588SBarry Smith while (nz--) { 42289bc588SBarry Smith fm = n; 43289bc588SBarry Smith idx = ic[*ajtmp++ - 1]; 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 ) { 53289bc588SBarry Smith ajtmp = ajnew + idnew[row] - 1; 54289bc588SBarry Smith nz = ainew[row+1] - idnew[row]; 55289bc588SBarry Smith fm = row; 56289bc588SBarry Smith while (nz--) { 57289bc588SBarry Smith fm = n; 58289bc588SBarry Smith idx = *ajtmp++ - 1; 59289bc588SBarry Smith do { 60289bc588SBarry Smith m = fm; 61289bc588SBarry Smith fm = fill[m]; 62289bc588SBarry Smith } while (fm < idx); 63289bc588SBarry Smith if (fm != idx) { 64289bc588SBarry Smith fill[m] = idx; 65289bc588SBarry Smith fill[idx] = fm; 66289bc588SBarry Smith fm = idx; 67289bc588SBarry Smith nnz++; 68289bc588SBarry Smith } 69289bc588SBarry Smith } 70289bc588SBarry Smith row = fill[row]; 71289bc588SBarry Smith } 72289bc588SBarry Smith /* copy new filled row into permanent storage */ 73289bc588SBarry Smith ainew[i+1] = ainew[i] + nnz; 74289bc588SBarry Smith if (ainew[i+1] > jmax+1) { 75289bc588SBarry Smith /* allocate a longer ajnew */ 76289bc588SBarry Smith jmax += nnz*(n-i); 77289bc588SBarry Smith ajtmp = (int *) MALLOC( jmax*sizeof(int) );CHKPTR(ajtmp); 78289bc588SBarry Smith MEMCPY(ajtmp,ajnew,(ainew[i]-1)*sizeof(int)); 79289bc588SBarry Smith FREE(ajnew); 80289bc588SBarry Smith ajnew = ajtmp; 81289bc588SBarry Smith } 82289bc588SBarry Smith ajtmp = ajnew + ainew[i] - 1; 83289bc588SBarry Smith fm = fill[n]; 84289bc588SBarry Smith nzi = 0; 85289bc588SBarry Smith while (nnz--) { 86289bc588SBarry Smith if (fm < i) nzi++; 87289bc588SBarry Smith *ajtmp++ = fm + 1; 88289bc588SBarry Smith fm = fill[fm]; 89289bc588SBarry Smith } 90289bc588SBarry Smith idnew[i] = ainew[i] + nzi; 91289bc588SBarry Smith } 92289bc588SBarry Smith 93289bc588SBarry Smith ISDestroy(isicol); FREE(fill); 94289bc588SBarry Smith 95289bc588SBarry Smith /* put together the new matrix */ 9611d228e4SBarry Smith ierr = MatCreateSequentialAIJ(n, n, 0, 0, fact); CHKERR(ierr); 97289bc588SBarry Smith aijnew = (Matiaij *) (*fact)->data; 98289bc588SBarry Smith FREE(aijnew->imax); 99289bc588SBarry Smith aijnew->singlemalloc = 0; 100f0479e8cSBarry Smith len = (ainew[n] - 1)*sizeof(Scalar); 101e8d4e0b9SBarry Smith /* the next line frees the default space generated by the Create() */ 102e8d4e0b9SBarry Smith FREE(aijnew->a); FREE(aijnew->ilen); 103289bc588SBarry Smith aijnew->a = (Scalar *) MALLOC( len ); CHKPTR(aijnew->a); 104289bc588SBarry Smith aijnew->j = ajnew; 105289bc588SBarry Smith aijnew->i = ainew; 1068c37ef55SBarry Smith aijnew->diag = idnew; 107e8d4e0b9SBarry Smith aijnew->ilen = 0; 10820563c6bSBarry Smith aijnew->imax = 0; 109289bc588SBarry Smith (*fact)->row = isrow; 110289bc588SBarry Smith (*fact)->col = iscol; 111289bc588SBarry Smith (*fact)->factor = FACTOR_LU; 112289bc588SBarry Smith return 0; 113289bc588SBarry Smith } 114289bc588SBarry Smith 11520563c6bSBarry Smith int MatiAIJLUFactorNumeric(Mat mat,Mat *infact) 116289bc588SBarry Smith { 11720563c6bSBarry Smith Mat fact = *infact; 118289bc588SBarry Smith Matiaij *aij = (Matiaij *) mat->data, *aijnew = (Matiaij *)fact->data; 119289bc588SBarry Smith IS iscol = fact->col, isrow = fact->row, isicol; 120289bc588SBarry Smith int *r,*ic, ierr, i, j, n = aij->m, *ai = aijnew->i, *aj = aijnew->j; 121289bc588SBarry Smith int prow, *ainew,*ajnew, jmax,*fill, *ajtmpold, *ajtmp, nz , *ii; 1228c37ef55SBarry Smith int *idnew, idx, pivot_row,row,*pj, m,fm, nnz, nzi,len; 1238c37ef55SBarry Smith Scalar *rtmp,*vnew,*v, *pv, *pc, multiplier; 124289bc588SBarry Smith 125289bc588SBarry Smith if (ierr = ISInvertPermutation(iscol,&isicol)) SETERR(ierr,0); 1268c37ef55SBarry Smith ierr = ISGetIndices(isrow,&r); CHKERR(ierr); 1278c37ef55SBarry Smith ierr = ISGetIndices(isicol,&ic); CHKERR(ierr); 128289bc588SBarry Smith rtmp = (Scalar *) MALLOC( (n+1)*sizeof(Scalar) ); CHKPTR(rtmp); 129289bc588SBarry Smith 130289bc588SBarry Smith for ( i=0; i<n; i++ ) { 131289bc588SBarry Smith nz = ai[i+1] - ai[i]; 132289bc588SBarry Smith ajtmp = aj + ai[i] - 1; 133289bc588SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ajtmp[j]-1] = 0.0; 134289bc588SBarry Smith 135289bc588SBarry Smith /* load in initial (unfactored row) */ 1368c37ef55SBarry Smith nz = aij->i[r[i]+1] - aij->i[r[i]]; 1378c37ef55SBarry Smith ajtmpold = aij->j + aij->i[r[i]] - 1; 1388c37ef55SBarry Smith v = aij->a + aij->i[r[i]] - 1; 1398c37ef55SBarry Smith for ( j=0; j<nz; j++ ) rtmp[ic[ajtmpold[j]-1]] = v[j]; 140289bc588SBarry Smith 1418c37ef55SBarry Smith row = *ajtmp++ - 1; 142289bc588SBarry Smith while (row < i) { 1438c37ef55SBarry Smith pc = rtmp + row; 1448c37ef55SBarry Smith if (*pc != 0.0) { 1458c37ef55SBarry Smith nz = aijnew->diag[row] - ai[row]; 1468c37ef55SBarry Smith pv = aijnew->a + aijnew->diag[row] - 1; 1478c37ef55SBarry Smith pj = aijnew->j + aijnew->diag[row]; 1488c37ef55SBarry Smith multiplier = *pc * *pv++; 1498c37ef55SBarry Smith *pc = multiplier; 1508c37ef55SBarry Smith nz = ai[row+1] - ai[row] - 1 - nz; 1518c37ef55SBarry Smith while (nz-->0) rtmp[*pj++ - 1] -= multiplier* *pv++; 152289bc588SBarry Smith } 1538c37ef55SBarry Smith row = *ajtmp++ - 1; 154289bc588SBarry Smith } 1558c37ef55SBarry Smith /* finished row so stick it into aijnew->a */ 1568c37ef55SBarry Smith pv = aijnew->a + ai[i] - 1; 1578c37ef55SBarry Smith pj = aijnew->j + ai[i] - 1; 1588c37ef55SBarry Smith nz = ai[i+1] - ai[i]; 1598c37ef55SBarry Smith rtmp[i] = 1.0/rtmp[i]; 1608c37ef55SBarry Smith for ( j=0; j<nz; j++ ) {pv[j] = rtmp[pj[j]-1];} 1618c37ef55SBarry Smith } 1628c37ef55SBarry Smith FREE(rtmp); 163f0479e8cSBarry Smith ierr = ISRestoreIndices(isicol,&ic); CHKERR(ierr); 164f0479e8cSBarry Smith ierr = ISRestoreIndices(isrow,&r); CHKERR(ierr); 1658c37ef55SBarry Smith ierr = ISDestroy(isicol); CHKERR(ierr); 1668c37ef55SBarry Smith fact->factor = FACTOR_LU; 167289bc588SBarry Smith 168289bc588SBarry Smith return 0; 169289bc588SBarry Smith } 170*da3a660dSBarry Smith int MatiAIJLUFactor(Mat matin,IS row,IS col) 171*da3a660dSBarry Smith { 172*da3a660dSBarry Smith Matiaij *mat = (Matiaij *) matin->data; 173*da3a660dSBarry Smith int ierr, info; 174*da3a660dSBarry Smith Mat fact; 175*da3a660dSBarry Smith ierr = MatiAIJLUFactorSymbolic(matin,row,col,&fact); CHKERR(ierr); 176*da3a660dSBarry Smith ierr = MatiAIJLUFactorNumeric(matin,&fact); CHKERR(ierr); 177*da3a660dSBarry Smith 178*da3a660dSBarry Smith /* free all the data structures from mat */ 179*da3a660dSBarry Smith FREE(mat->a); 180*da3a660dSBarry Smith if (!mat->singlemalloc) {FREE(mat->i); FREE(mat->j);} 181*da3a660dSBarry Smith if (mat->diag) FREE(mat->diag); 182*da3a660dSBarry Smith if (mat->ilen) FREE(mat->ilen); 183*da3a660dSBarry Smith if (mat->imax) FREE(mat->imax); 184*da3a660dSBarry Smith if (matin->row && matin->col && matin->row != matin->col) { 185*da3a660dSBarry Smith ISDestroy(matin->row); 186*da3a660dSBarry Smith } 187*da3a660dSBarry Smith if (matin->col) ISDestroy(matin->col); 188*da3a660dSBarry Smith FREE(mat); 189*da3a660dSBarry Smith 190*da3a660dSBarry Smith MEMCPY(matin,fact,sizeof(struct _Mat)); 191*da3a660dSBarry Smith FREE(fact); 192*da3a660dSBarry Smith return 0; 193*da3a660dSBarry Smith } 194*da3a660dSBarry Smith 1958c37ef55SBarry Smith int MatiAIJSolve(Mat mat,Vec bb, Vec xx) 1968c37ef55SBarry Smith { 1978c37ef55SBarry Smith Matiaij *aij = (Matiaij *) mat->data; 1988c37ef55SBarry Smith IS iscol = mat->col, isrow = mat->row; 1998c37ef55SBarry Smith int *r,*c, ierr, i, j, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; 2008c37ef55SBarry Smith int nz; 2018c37ef55SBarry Smith Scalar *x,*b,*tmp, *aa = aij->a, sum, *v; 2028c37ef55SBarry Smith 2038c37ef55SBarry Smith if (ierr = VecGetArray(bb,&b)) SETERR(ierr,0); 2048c37ef55SBarry Smith if (ierr = VecGetArray(xx,&x)) SETERR(ierr,0); 2058c37ef55SBarry Smith tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp); 2068c37ef55SBarry Smith 2078c37ef55SBarry Smith if (ierr = ISGetIndices(isrow,&r)) SETERR(ierr,0); 2088c37ef55SBarry Smith if (ierr = ISGetIndices(iscol,&c)) SETERR(ierr,0); c = c + (n-1); 2098c37ef55SBarry Smith 2108c37ef55SBarry Smith /* forward solve the lower triangular */ 2118c37ef55SBarry Smith tmp[0] = b[*r++]; 2128c37ef55SBarry Smith for ( i=1; i<n; i++ ) { 2138c37ef55SBarry Smith v = aa + ai[i] - 1; 2148c37ef55SBarry Smith vi = aj + ai[i] - 1; 2158c37ef55SBarry Smith nz = aij->diag[i] - ai[i]; 2168c37ef55SBarry Smith sum = b[*r++]; 2178c37ef55SBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 2188c37ef55SBarry Smith tmp[i] = sum; 2198c37ef55SBarry Smith } 2208c37ef55SBarry Smith 2218c37ef55SBarry Smith /* backward solve the upper triangular */ 2228c37ef55SBarry Smith for ( i=n-1; i>=0; i-- ){ 2238c37ef55SBarry Smith v = aa + aij->diag[i]; 2248c37ef55SBarry Smith vi = aj + aij->diag[i]; 2258c37ef55SBarry Smith nz = ai[i+1] - aij->diag[i] - 1; 2268c37ef55SBarry Smith sum = tmp[i]; 2278c37ef55SBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 2288c37ef55SBarry Smith x[*c--] = tmp[i] = sum*aa[aij->diag[i]-1]; 2298c37ef55SBarry Smith } 2308c37ef55SBarry Smith 2318c37ef55SBarry Smith FREE(tmp); 2328c37ef55SBarry Smith return 0; 2338c37ef55SBarry Smith } 234*da3a660dSBarry Smith int MatiAIJSolveAdd(Mat mat,Vec bb, Vec yy, Vec xx) 235*da3a660dSBarry Smith { 236*da3a660dSBarry Smith Matiaij *aij = (Matiaij *) mat->data; 237*da3a660dSBarry Smith IS iscol = mat->col, isrow = mat->row; 238*da3a660dSBarry Smith int *r,*c, ierr, i, j, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; 239*da3a660dSBarry Smith int nz; 240*da3a660dSBarry Smith Scalar *x,*b,*tmp, *aa = aij->a, sum, *v; 241*da3a660dSBarry Smith 242*da3a660dSBarry Smith if (yy != xx) {ierr = VecCopy(yy,xx); CHKERR(ierr);} 243*da3a660dSBarry Smith 244*da3a660dSBarry Smith if (ierr = VecGetArray(bb,&b)) SETERR(ierr,0); 245*da3a660dSBarry Smith if (ierr = VecGetArray(xx,&x)) SETERR(ierr,0); 246*da3a660dSBarry Smith tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp); 247*da3a660dSBarry Smith 248*da3a660dSBarry Smith if (ierr = ISGetIndices(isrow,&r)) SETERR(ierr,0); 249*da3a660dSBarry Smith if (ierr = ISGetIndices(iscol,&c)) SETERR(ierr,0); c = c + (n-1); 250*da3a660dSBarry Smith 251*da3a660dSBarry Smith /* forward solve the lower triangular */ 252*da3a660dSBarry Smith tmp[0] = b[*r++]; 253*da3a660dSBarry Smith for ( i=1; i<n; i++ ) { 254*da3a660dSBarry Smith v = aa + ai[i] - 1; 255*da3a660dSBarry Smith vi = aj + ai[i] - 1; 256*da3a660dSBarry Smith nz = aij->diag[i] - ai[i]; 257*da3a660dSBarry Smith sum = b[*r++]; 258*da3a660dSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 259*da3a660dSBarry Smith tmp[i] = sum; 260*da3a660dSBarry Smith } 261*da3a660dSBarry Smith 262*da3a660dSBarry Smith /* backward solve the upper triangular */ 263*da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 264*da3a660dSBarry Smith v = aa + aij->diag[i]; 265*da3a660dSBarry Smith vi = aj + aij->diag[i]; 266*da3a660dSBarry Smith nz = ai[i+1] - aij->diag[i] - 1; 267*da3a660dSBarry Smith sum = tmp[i]; 268*da3a660dSBarry Smith while (nz--) sum -= *v++ * tmp[*vi++ - 1]; 269*da3a660dSBarry Smith tmp[i] = sum*aa[aij->diag[i]-1]; 270*da3a660dSBarry Smith x[*c--] += tmp[i]; 271*da3a660dSBarry Smith } 272*da3a660dSBarry Smith 273*da3a660dSBarry Smith FREE(tmp); 274*da3a660dSBarry Smith return 0; 275*da3a660dSBarry Smith } 276*da3a660dSBarry Smith /* -------------------------------------------------------------------*/ 277*da3a660dSBarry Smith int MatiAIJSolveTrans(Mat mat,Vec bb, Vec xx) 278*da3a660dSBarry Smith { 279*da3a660dSBarry Smith Matiaij *aij = (Matiaij *) mat->data; 280*da3a660dSBarry Smith IS iscol = mat->col, isrow = mat->row, invisrow,inviscol; 281*da3a660dSBarry Smith int *r,*c, ierr, i, j, n = aij->m, *vi, *ai = aij->i, *aj = aij->j; 282*da3a660dSBarry Smith int nz; 283*da3a660dSBarry Smith Scalar *x,*b,*tmp, *aa = aij->a, sum, *v; 284*da3a660dSBarry Smith 285*da3a660dSBarry Smith if (ierr = VecGetArray(bb,&b)) SETERR(ierr,0); 286*da3a660dSBarry Smith if (ierr = VecGetArray(xx,&x)) SETERR(ierr,0); 287*da3a660dSBarry Smith tmp = (Scalar *) MALLOC(n*sizeof(Scalar)); CHKPTR(tmp); 288*da3a660dSBarry Smith 289*da3a660dSBarry Smith /* invert the permutations */ 290*da3a660dSBarry Smith ierr = ISInvertPermutation(isrow,&invisrow); CHKERR(ierr); 291*da3a660dSBarry Smith ierr = ISInvertPermutation(iscol,&inviscol); CHKERR(ierr); 292*da3a660dSBarry Smith 293*da3a660dSBarry Smith 294*da3a660dSBarry Smith if (ierr = ISGetIndices(invisrow,&r)) SETERR(ierr,0); 295*da3a660dSBarry Smith if (ierr = ISGetIndices(inviscol,&c)) SETERR(ierr,0); 296*da3a660dSBarry Smith 297*da3a660dSBarry Smith /* copy the b into temp work space according to permutation */ 298*da3a660dSBarry Smith for ( i=0; i<n; i++ ) tmp[c[i]] = b[i]; 299*da3a660dSBarry Smith 300*da3a660dSBarry Smith /* forward solve the U^T */ 301*da3a660dSBarry Smith for ( i=0; i<n; i++ ) { 302*da3a660dSBarry Smith v = aa + aij->diag[i] - 1; 303*da3a660dSBarry Smith vi = aj + aij->diag[i]; 304*da3a660dSBarry Smith nz = ai[i+1] - aij->diag[i] - 1; 305*da3a660dSBarry Smith tmp[i] *= *v++; 306*da3a660dSBarry Smith while (nz--) { 307*da3a660dSBarry Smith tmp[*vi++ - 1] -= (*v++)*tmp[i]; 308*da3a660dSBarry Smith } 309*da3a660dSBarry Smith } 310*da3a660dSBarry Smith 311*da3a660dSBarry Smith /* backward solve the L^T */ 312*da3a660dSBarry Smith for ( i=n-1; i>=0; i-- ){ 313*da3a660dSBarry Smith v = aa + aij->diag[i] - 2; 314*da3a660dSBarry Smith vi = aj + aij->diag[i] - 2; 315*da3a660dSBarry Smith nz = aij->diag[i] - ai[i]; 316*da3a660dSBarry Smith while (nz--) { 317*da3a660dSBarry Smith tmp[*vi-- - 1] -= (*v--)*tmp[i]; 318*da3a660dSBarry Smith } 319*da3a660dSBarry Smith } 320*da3a660dSBarry Smith 321*da3a660dSBarry Smith /* copy tmp into x according to permutation */ 322*da3a660dSBarry Smith for ( i=0; i<n; i++ ) x[r[i]] = tmp[i]; 323*da3a660dSBarry Smith 324*da3a660dSBarry Smith ISDestroy(invisrow); ISDestroy(inviscol); 325*da3a660dSBarry Smith 326*da3a660dSBarry Smith FREE(tmp); 327*da3a660dSBarry Smith return 0; 328*da3a660dSBarry Smith } 329*da3a660dSBarry Smith 330*da3a660dSBarry Smith int MatiAIJSolveTransAdd(Mat mat,Vec bb, Vec xx,Vec zz) 331*da3a660dSBarry Smith { 332*da3a660dSBarry Smith Matiaij *aij = (Matiaij *) mat->data; 333*da3a660dSBarry Smith } 334