1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*bef8e0ddSBarry Smith static char vcid[] = "$Id: aij.c,v 1.263 1998/05/07 21:09:30 bsmith Exp bsmith $"; 317ab2063SBarry Smith #endif 417ab2063SBarry Smith 5d5d45c9bSBarry Smith /* 63369ce9aSBarry Smith Defines the basic matrix operations for the AIJ (compressed row) 7d5d45c9bSBarry Smith matrix storage format. 8d5d45c9bSBarry Smith */ 93369ce9aSBarry Smith 103369ce9aSBarry Smith #include "pinclude/pviewer.h" 113369ce9aSBarry Smith #include "sys.h" 1270f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h" 13f5eb4b81SSatish Balay #include "src/vec/vecimpl.h" 14f5eb4b81SSatish Balay #include "src/inline/spops.h" 158d195f9aSBarry Smith #include "src/inline/dot.h" 16f5eb4b81SSatish Balay #include "src/inline/bitarray.h" 1717ab2063SBarry Smith 18a2ce50c7SBarry Smith /* 19a2ce50c7SBarry Smith Basic AIJ format ILU based on drop tolerance 20a2ce50c7SBarry Smith */ 215615d1e5SSatish Balay #undef __FUNC__ 225615d1e5SSatish Balay #define __FUNC__ "MatILUDTFactor_SeqAIJ" 23a2ce50c7SBarry Smith int MatILUDTFactor_SeqAIJ(Mat A,double dt,int maxnz,IS row,IS col,Mat *fact) 24a2ce50c7SBarry Smith { 25a2ce50c7SBarry Smith /* Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; */ 26a2ce50c7SBarry Smith int ierr = 1; 27a2ce50c7SBarry Smith 283a40ed3dSBarry Smith PetscFunctionBegin; 29e3372554SBarry Smith SETERRQ(ierr,0,"Not implemented"); 30d64ed03dSBarry Smith #if !defined(USE_PETSC_DEBUG) 31d64ed03dSBarry Smith PetscFunctionReturn(0); 32d64ed03dSBarry Smith #endif 33a2ce50c7SBarry Smith } 34a2ce50c7SBarry Smith 35bcd2baecSBarry Smith extern int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**); 3617ab2063SBarry Smith 375615d1e5SSatish Balay #undef __FUNC__ 385615d1e5SSatish Balay #define __FUNC__ "MatGetRowIJ_SeqAIJ" 398f6be9afSLois Curfman McInnes int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja, 406945ee14SBarry Smith PetscTruth *done) 4117ab2063SBarry Smith { 42416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 436945ee14SBarry Smith int ierr,i,ishift; 4417ab2063SBarry Smith 453a40ed3dSBarry Smith PetscFunctionBegin; 4631625ec5SSatish Balay *m = A->m; 473a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 486945ee14SBarry Smith ishift = a->indexshift; 496945ee14SBarry Smith if (symmetric) { 5031625ec5SSatish Balay ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr); 516945ee14SBarry Smith } else if (oshift == 0 && ishift == -1) { 5231625ec5SSatish Balay int nz = a->i[a->m]; 533b2fbd54SBarry Smith /* malloc space and subtract 1 from i and j indices */ 5431625ec5SSatish Balay *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia); 553b2fbd54SBarry Smith *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja); 563b2fbd54SBarry Smith for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] - 1; 5731625ec5SSatish Balay for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] - 1; 586945ee14SBarry Smith } else if (oshift == 1 && ishift == 0) { 5931625ec5SSatish Balay int nz = a->i[a->m] + 1; 603b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 6131625ec5SSatish Balay *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia); 623b2fbd54SBarry Smith *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja); 633b2fbd54SBarry Smith for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] + 1; 6431625ec5SSatish Balay for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] + 1; 656945ee14SBarry Smith } else { 666945ee14SBarry Smith *ia = a->i; *ja = a->j; 67a2ce50c7SBarry Smith } 68a2ce50c7SBarry Smith 693a40ed3dSBarry Smith PetscFunctionReturn(0); 70a2744918SBarry Smith } 71a2744918SBarry Smith 725615d1e5SSatish Balay #undef __FUNC__ 735615d1e5SSatish Balay #define __FUNC__ "MatRestoreRowIJ_SeqAIJ" 748f6be9afSLois Curfman McInnes int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja, 756945ee14SBarry Smith PetscTruth *done) 766945ee14SBarry Smith { 776945ee14SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 783b2fbd54SBarry Smith int ishift = a->indexshift; 796945ee14SBarry Smith 803a40ed3dSBarry Smith PetscFunctionBegin; 813a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 823b2fbd54SBarry Smith if (symmetric || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) { 836945ee14SBarry Smith PetscFree(*ia); 846945ee14SBarry Smith PetscFree(*ja); 85bcd2baecSBarry Smith } 863a40ed3dSBarry Smith PetscFunctionReturn(0); 8717ab2063SBarry Smith } 8817ab2063SBarry Smith 895615d1e5SSatish Balay #undef __FUNC__ 905615d1e5SSatish Balay #define __FUNC__ "MatGetColumnIJ_SeqAIJ" 9143a90d84SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja, 923b2fbd54SBarry Smith PetscTruth *done) 933b2fbd54SBarry Smith { 943b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 95a93ec695SBarry Smith int ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m; 96a93ec695SBarry Smith int nz = a->i[m]+ishift,row,*jj,mr,col; 973b2fbd54SBarry Smith 983a40ed3dSBarry Smith PetscFunctionBegin; 993b2fbd54SBarry Smith *nn = A->n; 1003a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1013b2fbd54SBarry Smith if (symmetric) { 102179192dfSSatish Balay ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr); 1033b2fbd54SBarry Smith } else { 10461d2ded1SBarry Smith collengths = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(collengths); 1053b2fbd54SBarry Smith PetscMemzero(collengths,n*sizeof(int)); 1063b2fbd54SBarry Smith cia = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(cia); 107a93ec695SBarry Smith cja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(cja); 1083b2fbd54SBarry Smith jj = a->j; 1093b2fbd54SBarry Smith for ( i=0; i<nz; i++ ) { 1103b2fbd54SBarry Smith collengths[jj[i] + ishift]++; 1113b2fbd54SBarry Smith } 1123b2fbd54SBarry Smith cia[0] = oshift; 1133b2fbd54SBarry Smith for ( i=0; i<n; i++) { 1143b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 1153b2fbd54SBarry Smith } 1163b2fbd54SBarry Smith PetscMemzero(collengths,n*sizeof(int)); 1173b2fbd54SBarry Smith jj = a->j; 118a93ec695SBarry Smith for ( row=0; row<m; row++ ) { 119a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 120a93ec695SBarry Smith for ( i=0; i<mr; i++ ) { 1213b2fbd54SBarry Smith col = *jj++ + ishift; 1223b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 1233b2fbd54SBarry Smith } 1243b2fbd54SBarry Smith } 1253b2fbd54SBarry Smith PetscFree(collengths); 1263b2fbd54SBarry Smith *ia = cia; *ja = cja; 1273b2fbd54SBarry Smith } 1283b2fbd54SBarry Smith 1293a40ed3dSBarry Smith PetscFunctionReturn(0); 1303b2fbd54SBarry Smith } 1313b2fbd54SBarry Smith 1325615d1e5SSatish Balay #undef __FUNC__ 1335615d1e5SSatish Balay #define __FUNC__ "MatRestoreColumnIJ_SeqAIJ" 13443a90d84SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia, 1353b2fbd54SBarry Smith int **ja,PetscTruth *done) 1363b2fbd54SBarry Smith { 1373a40ed3dSBarry Smith PetscFunctionBegin; 1383a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1393b2fbd54SBarry Smith 1403b2fbd54SBarry Smith PetscFree(*ia); 1413b2fbd54SBarry Smith PetscFree(*ja); 1423b2fbd54SBarry Smith 1433a40ed3dSBarry Smith PetscFunctionReturn(0); 1443b2fbd54SBarry Smith } 1453b2fbd54SBarry Smith 146227d817aSBarry Smith #define CHUNKSIZE 15 14717ab2063SBarry Smith 1485615d1e5SSatish Balay #undef __FUNC__ 1495615d1e5SSatish Balay #define __FUNC__ "MatSetValues_SeqAIJ" 15061d2ded1SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v,InsertMode is) 15117ab2063SBarry Smith { 152416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 153416022c9SBarry Smith int *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax, N, sorted = a->sorted; 1544b0e389bSBarry Smith int *imax = a->imax, *ai = a->i, *ailen = a->ilen,roworiented = a->roworiented; 155d5d45c9bSBarry Smith int *aj = a->j, nonew = a->nonew,shift = a->indexshift; 156416022c9SBarry Smith Scalar *ap,value, *aa = a->a; 15717ab2063SBarry Smith 1583a40ed3dSBarry Smith PetscFunctionBegin; 15917ab2063SBarry Smith for ( k=0; k<m; k++ ) { /* loop over added rows */ 160416022c9SBarry Smith row = im[k]; 1613a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g) 162a8c6a408SBarry Smith if (row < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row"); 163a8c6a408SBarry Smith if (row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 1643b2fbd54SBarry Smith #endif 16517ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 16617ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 167416022c9SBarry Smith low = 0; 16817ab2063SBarry Smith for ( l=0; l<n; l++ ) { /* loop over added columns */ 1693a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g) 170a8c6a408SBarry Smith if (in[l] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column"); 171a8c6a408SBarry Smith if (in[l] >= a->n) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large"); 1723b2fbd54SBarry Smith #endif 1734b0e389bSBarry Smith col = in[l] - shift; 1744b0e389bSBarry Smith if (roworiented) { 1754b0e389bSBarry Smith value = *v++; 176*bef8e0ddSBarry Smith } else { 1774b0e389bSBarry Smith value = v[k + l*m]; 1784b0e389bSBarry Smith } 179416022c9SBarry Smith if (!sorted) low = 0; high = nrow; 180416022c9SBarry Smith while (high-low > 5) { 181416022c9SBarry Smith t = (low+high)/2; 182416022c9SBarry Smith if (rp[t] > col) high = t; 183416022c9SBarry Smith else low = t; 18417ab2063SBarry Smith } 185416022c9SBarry Smith for ( i=low; i<high; i++ ) { 18617ab2063SBarry Smith if (rp[i] > col) break; 18717ab2063SBarry Smith if (rp[i] == col) { 188416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 18917ab2063SBarry Smith else ap[i] = value; 19017ab2063SBarry Smith goto noinsert; 19117ab2063SBarry Smith } 19217ab2063SBarry Smith } 193c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 194a8c6a408SBarry Smith else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); 19517ab2063SBarry Smith if (nrow >= rmax) { 19617ab2063SBarry Smith /* there is no extra room in row, therefore enlarge */ 197416022c9SBarry Smith int new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; 19817ab2063SBarry Smith Scalar *new_a; 19917ab2063SBarry Smith 200a8c6a408SBarry Smith if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); 20196854ed6SLois Curfman McInnes 20217ab2063SBarry Smith /* malloc new storage space */ 203416022c9SBarry Smith len = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); 2040452661fSBarry Smith new_a = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); 20517ab2063SBarry Smith new_j = (int *) (new_a + new_nz); 20617ab2063SBarry Smith new_i = new_j + new_nz; 20717ab2063SBarry Smith 20817ab2063SBarry Smith /* copy over old data into new slots */ 20917ab2063SBarry Smith for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} 210416022c9SBarry Smith for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} 211416022c9SBarry Smith PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int)); 212416022c9SBarry Smith len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); 213416022c9SBarry Smith PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, 21417ab2063SBarry Smith len*sizeof(int)); 215416022c9SBarry Smith PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar)); 216416022c9SBarry Smith PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, 21717ab2063SBarry Smith len*sizeof(Scalar)); 21817ab2063SBarry Smith /* free up old matrix storage */ 2190452661fSBarry Smith PetscFree(a->a); 2200452661fSBarry Smith if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);} 221416022c9SBarry Smith aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; 222416022c9SBarry Smith a->singlemalloc = 1; 22317ab2063SBarry Smith 22417ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 225416022c9SBarry Smith rmax = imax[row] = imax[row] + CHUNKSIZE; 226416022c9SBarry Smith PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); 227416022c9SBarry Smith a->maxnz += CHUNKSIZE; 228b810aeb4SBarry Smith a->reallocs++; 22917ab2063SBarry Smith } 230416022c9SBarry Smith N = nrow++ - 1; a->nz++; 231416022c9SBarry Smith /* shift up all the later entries in this row */ 232416022c9SBarry Smith for ( ii=N; ii>=i; ii-- ) { 23317ab2063SBarry Smith rp[ii+1] = rp[ii]; 23417ab2063SBarry Smith ap[ii+1] = ap[ii]; 23517ab2063SBarry Smith } 23617ab2063SBarry Smith rp[i] = col; 23717ab2063SBarry Smith ap[i] = value; 23817ab2063SBarry Smith noinsert:; 239416022c9SBarry Smith low = i + 1; 24017ab2063SBarry Smith } 24117ab2063SBarry Smith ailen[row] = nrow; 24217ab2063SBarry Smith } 2433a40ed3dSBarry Smith PetscFunctionReturn(0); 24417ab2063SBarry Smith } 24517ab2063SBarry Smith 2465615d1e5SSatish Balay #undef __FUNC__ 2475615d1e5SSatish Balay #define __FUNC__ "MatGetValues_SeqAIJ" 2488f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v) 2497eb43aa7SLois Curfman McInnes { 2507eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 251b49de8d1SLois Curfman McInnes int *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j; 2527eb43aa7SLois Curfman McInnes int *ai = a->i, *ailen = a->ilen, shift = a->indexshift; 2537eb43aa7SLois Curfman McInnes Scalar *ap, *aa = a->a, zero = 0.0; 2547eb43aa7SLois Curfman McInnes 2553a40ed3dSBarry Smith PetscFunctionBegin; 2567eb43aa7SLois Curfman McInnes for ( k=0; k<m; k++ ) { /* loop over rows */ 2577eb43aa7SLois Curfman McInnes row = im[k]; 258a8c6a408SBarry Smith if (row < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row"); 259a8c6a408SBarry Smith if (row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 2607eb43aa7SLois Curfman McInnes rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 2617eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2627eb43aa7SLois Curfman McInnes for ( l=0; l<n; l++ ) { /* loop over columns */ 263a8c6a408SBarry Smith if (in[l] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column"); 264a8c6a408SBarry Smith if (in[l] >= a->n) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large"); 2657eb43aa7SLois Curfman McInnes col = in[l] - shift; 2667eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2677eb43aa7SLois Curfman McInnes while (high-low > 5) { 2687eb43aa7SLois Curfman McInnes t = (low+high)/2; 2697eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2707eb43aa7SLois Curfman McInnes else low = t; 2717eb43aa7SLois Curfman McInnes } 2727eb43aa7SLois Curfman McInnes for ( i=low; i<high; i++ ) { 2737eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2747eb43aa7SLois Curfman McInnes if (rp[i] == col) { 275b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2767eb43aa7SLois Curfman McInnes goto finished; 2777eb43aa7SLois Curfman McInnes } 2787eb43aa7SLois Curfman McInnes } 279b49de8d1SLois Curfman McInnes *v++ = zero; 2807eb43aa7SLois Curfman McInnes finished:; 2817eb43aa7SLois Curfman McInnes } 2827eb43aa7SLois Curfman McInnes } 2833a40ed3dSBarry Smith PetscFunctionReturn(0); 2847eb43aa7SLois Curfman McInnes } 2857eb43aa7SLois Curfman McInnes 28617ab2063SBarry Smith 2875615d1e5SSatish Balay #undef __FUNC__ 2885615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Binary" 2898f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_Binary(Mat A,Viewer viewer) 29017ab2063SBarry Smith { 291416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 292416022c9SBarry Smith int i, fd, *col_lens, ierr; 29317ab2063SBarry Smith 2943a40ed3dSBarry Smith PetscFunctionBegin; 29590ace30eSBarry Smith ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr); 2960452661fSBarry Smith col_lens = (int *) PetscMalloc( (4+a->m)*sizeof(int) ); CHKPTRQ(col_lens); 297416022c9SBarry Smith col_lens[0] = MAT_COOKIE; 298416022c9SBarry Smith col_lens[1] = a->m; 299416022c9SBarry Smith col_lens[2] = a->n; 300416022c9SBarry Smith col_lens[3] = a->nz; 301416022c9SBarry Smith 302416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 303416022c9SBarry Smith for ( i=0; i<a->m; i++ ) { 304416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 30517ab2063SBarry Smith } 3060752156aSBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+a->m,PETSC_INT,1); CHKERRQ(ierr); 3070452661fSBarry Smith PetscFree(col_lens); 308416022c9SBarry Smith 309416022c9SBarry Smith /* store column indices (zero start index) */ 310416022c9SBarry Smith if (a->indexshift) { 311416022c9SBarry Smith for ( i=0; i<a->nz; i++ ) a->j[i]--; 31217ab2063SBarry Smith } 3130752156aSBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0); CHKERRQ(ierr); 314416022c9SBarry Smith if (a->indexshift) { 315416022c9SBarry Smith for ( i=0; i<a->nz; i++ ) a->j[i]++; 31617ab2063SBarry Smith } 317416022c9SBarry Smith 318416022c9SBarry Smith /* store nonzero values */ 3190752156aSBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0); CHKERRQ(ierr); 3203a40ed3dSBarry Smith PetscFunctionReturn(0); 32117ab2063SBarry Smith } 322416022c9SBarry Smith 3235615d1e5SSatish Balay #undef __FUNC__ 3245615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_ASCII" 3258f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_ASCII(Mat A,Viewer viewer) 326416022c9SBarry Smith { 327416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 328496e697eSBarry Smith int ierr, i,j, m = a->m, shift = a->indexshift, format, flg1,flg2; 32917ab2063SBarry Smith FILE *fd; 33017ab2063SBarry Smith char *outputname; 33117ab2063SBarry Smith 3323a40ed3dSBarry Smith PetscFunctionBegin; 33390ace30eSBarry Smith ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 334416022c9SBarry Smith ierr = ViewerFileGetOutputname_Private(viewer,&outputname); CHKERRQ(ierr); 33590ace30eSBarry Smith ierr = ViewerGetFormat(viewer,&format); 336a93ec695SBarry Smith if (format == VIEWER_FORMAT_ASCII_INFO) { 3373a40ed3dSBarry Smith PetscFunctionReturn(0); 3383a40ed3dSBarry Smith } else if (format == VIEWER_FORMAT_ASCII_INFO_LONG) { 339496e697eSBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg1); CHKERRQ(ierr); 340496e697eSBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_no_unroll",&flg2); CHKERRQ(ierr); 341496e697eSBarry Smith if (flg1 || flg2) fprintf(fd," not using I-node routines\n"); 34295e01e2fSLois Curfman McInnes else fprintf(fd," using I-node routines: found %d nodes, limit used is %d\n", 34395e01e2fSLois Curfman McInnes a->inode.node_count,a->inode.limit); 3443a40ed3dSBarry Smith } else if (format == VIEWER_FORMAT_ASCII_MATLAB) { 345d00d2cf4SBarry Smith int nofinalvalue = 0; 346d00d2cf4SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != a->n-!shift)) { 347d00d2cf4SBarry Smith nofinalvalue = 1; 348d00d2cf4SBarry Smith } 349416022c9SBarry Smith fprintf(fd,"%% Size = %d %d \n",m,a->n); 3504e220ebcSLois Curfman McInnes fprintf(fd,"%% Nonzeros = %d \n",a->nz); 351d00d2cf4SBarry Smith fprintf(fd,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue); 35217ab2063SBarry Smith fprintf(fd,"zzz = [\n"); 35317ab2063SBarry Smith 35417ab2063SBarry Smith for (i=0; i<m; i++) { 355416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 3563a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 357d64ed03dSBarry Smith fprintf(fd,"%d %d %18.16e + %18.16e i \n",i+1,a->j[j]+!shift,real(a->a[j]),imag(a->a[j])); 35817ab2063SBarry Smith #else 3597a743949SBarry Smith fprintf(fd,"%d %d %18.16e\n", i+1, a->j[j]+!shift, a->a[j]); 36017ab2063SBarry Smith #endif 36117ab2063SBarry Smith } 36217ab2063SBarry Smith } 363d00d2cf4SBarry Smith if (nofinalvalue) { 364d00d2cf4SBarry Smith fprintf(fd,"%d %d %18.16e\n", m, a->n, 0.0); 365d00d2cf4SBarry Smith } 36617ab2063SBarry Smith fprintf(fd,"];\n %s = spconvert(zzz);\n",outputname); 3673a40ed3dSBarry Smith } else if (format == VIEWER_FORMAT_ASCII_COMMON) { 36844cd7ae7SLois Curfman McInnes for ( i=0; i<m; i++ ) { 36944cd7ae7SLois Curfman McInnes fprintf(fd,"row %d:",i); 37044cd7ae7SLois Curfman McInnes for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 3713a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 372766eeae4SLois Curfman McInnes if (imag(a->a[j]) > 0.0 && real(a->a[j]) != 0.0) 37344cd7ae7SLois Curfman McInnes fprintf(fd," %d %g + %g i",a->j[j]+shift,real(a->a[j]),imag(a->a[j])); 374766eeae4SLois Curfman McInnes else if (imag(a->a[j]) < 0.0 && real(a->a[j]) != 0.0) 375766eeae4SLois Curfman McInnes fprintf(fd," %d %g - %g i",a->j[j]+shift,real(a->a[j]),-imag(a->a[j])); 37644cd7ae7SLois Curfman McInnes else if (real(a->a[j]) != 0.0) 37744cd7ae7SLois Curfman McInnes fprintf(fd," %d %g ",a->j[j]+shift,real(a->a[j])); 37844cd7ae7SLois Curfman McInnes #else 37944cd7ae7SLois Curfman McInnes if (a->a[j] != 0.0) fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]); 38044cd7ae7SLois Curfman McInnes #endif 38144cd7ae7SLois Curfman McInnes } 38244cd7ae7SLois Curfman McInnes fprintf(fd,"\n"); 38344cd7ae7SLois Curfman McInnes } 38444cd7ae7SLois Curfman McInnes } 385496be53dSLois Curfman McInnes else if (format == VIEWER_FORMAT_ASCII_SYMMODU) { 386496be53dSLois Curfman McInnes int nzd=0, fshift=1, *sptr; 3872e44a96cSLois Curfman McInnes sptr = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(sptr); 388496be53dSLois Curfman McInnes for ( i=0; i<m; i++ ) { 389496be53dSLois Curfman McInnes sptr[i] = nzd+1; 390496be53dSLois Curfman McInnes for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 391496be53dSLois Curfman McInnes if (a->j[j] >= i) { 3923a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 393496be53dSLois Curfman McInnes if (imag(a->a[j]) != 0.0 || real(a->a[j]) != 0.0) nzd++; 394496be53dSLois Curfman McInnes #else 395496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 396496be53dSLois Curfman McInnes #endif 397496be53dSLois Curfman McInnes } 398496be53dSLois Curfman McInnes } 399496be53dSLois Curfman McInnes } 4002e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 401496be53dSLois Curfman McInnes fprintf(fd," %d %d\n\n",m,nzd); 4022e44a96cSLois Curfman McInnes for ( i=0; i<m+1; i+=6 ) { 4032e44a96cSLois Curfman McInnes if (i+4<m) fprintf(fd," %d %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]); 4042e44a96cSLois Curfman McInnes else if (i+3<m) fprintf(fd," %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]); 4052e44a96cSLois Curfman McInnes else if (i+2<m) fprintf(fd," %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]); 4062e44a96cSLois Curfman McInnes else if (i+1<m) fprintf(fd," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]); 4072e44a96cSLois Curfman McInnes else if (i<m) fprintf(fd," %d %d\n",sptr[i],sptr[i+1]); 4087272d637SLois Curfman McInnes else fprintf(fd," %d\n",sptr[i]); 409496be53dSLois Curfman McInnes } 410496be53dSLois Curfman McInnes fprintf(fd,"\n"); 411496be53dSLois Curfman McInnes PetscFree(sptr); 412496be53dSLois Curfman McInnes for ( i=0; i<m; i++ ) { 413496be53dSLois Curfman McInnes for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 414496be53dSLois Curfman McInnes if (a->j[j] >= i) fprintf(fd," %d ",a->j[j]+fshift); 415496be53dSLois Curfman McInnes } 416496be53dSLois Curfman McInnes fprintf(fd,"\n"); 417496be53dSLois Curfman McInnes } 418496be53dSLois Curfman McInnes fprintf(fd,"\n"); 419496be53dSLois Curfman McInnes for ( i=0; i<m; i++ ) { 420496be53dSLois Curfman McInnes for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 421496be53dSLois Curfman McInnes if (a->j[j] >= i) { 4223a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 423496be53dSLois Curfman McInnes if (imag(a->a[j]) != 0.0 || real(a->a[j]) != 0.0) 424496be53dSLois Curfman McInnes fprintf(fd," %18.16e %18.16e ",real(a->a[j]),imag(a->a[j])); 425496be53dSLois Curfman McInnes #else 426496be53dSLois Curfman McInnes if (a->a[j] != 0.0) fprintf(fd," %18.16e ",a->a[j]); 427496be53dSLois Curfman McInnes #endif 428496be53dSLois Curfman McInnes } 429496be53dSLois Curfman McInnes } 430496be53dSLois Curfman McInnes fprintf(fd,"\n"); 431496be53dSLois Curfman McInnes } 4323a40ed3dSBarry Smith } else { 43317ab2063SBarry Smith for ( i=0; i<m; i++ ) { 43417ab2063SBarry Smith fprintf(fd,"row %d:",i); 435416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 4363a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 437766eeae4SLois Curfman McInnes if (imag(a->a[j]) > 0.0) { 438416022c9SBarry Smith fprintf(fd," %d %g + %g i",a->j[j]+shift,real(a->a[j]),imag(a->a[j])); 439766eeae4SLois Curfman McInnes } else if (imag(a->a[j]) < 0.0) { 440766eeae4SLois Curfman McInnes fprintf(fd," %d %g - %g i",a->j[j]+shift,real(a->a[j]),-imag(a->a[j])); 4413a40ed3dSBarry Smith } else { 442416022c9SBarry Smith fprintf(fd," %d %g ",a->j[j]+shift,real(a->a[j])); 44317ab2063SBarry Smith } 44417ab2063SBarry Smith #else 445416022c9SBarry Smith fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]); 44617ab2063SBarry Smith #endif 44717ab2063SBarry Smith } 44817ab2063SBarry Smith fprintf(fd,"\n"); 44917ab2063SBarry Smith } 45017ab2063SBarry Smith } 45117ab2063SBarry Smith fflush(fd); 4523a40ed3dSBarry Smith PetscFunctionReturn(0); 453416022c9SBarry Smith } 454416022c9SBarry Smith 4555615d1e5SSatish Balay #undef __FUNC__ 4565615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Draw" 4578f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_Draw(Mat A,Viewer viewer) 458416022c9SBarry Smith { 459416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 460cddf8d76SBarry Smith int ierr, i,j, m = a->m, shift = a->indexshift,pause,color; 4610513a670SBarry Smith int format; 46294a9d846SBarry Smith double xl,yl,xr,yr,w,h,xc,yc,scale = 1.0,x_l,x_r,y_l,y_r,maxv = 0.0; 463bcd2baecSBarry Smith Draw draw; 464cddf8d76SBarry Smith DrawButton button; 46519bcc07fSBarry Smith PetscTruth isnull; 466cddf8d76SBarry Smith 4673a40ed3dSBarry Smith PetscFunctionBegin; 4680513a670SBarry Smith ierr = ViewerDrawGetDraw(viewer,&draw); CHKERRQ(ierr); 46955843e3eSBarry Smith ierr = DrawSynchronizedClear(draw); CHKERRQ(ierr); 4700513a670SBarry Smith ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr); 4713a40ed3dSBarry Smith ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 47219bcc07fSBarry Smith 473416022c9SBarry Smith xr = a->n; yr = a->m; h = yr/10.0; w = xr/10.0; 474416022c9SBarry Smith xr += w; yr += h; xl = -w; yl = -h; 475416022c9SBarry Smith ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr); 476416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 4770513a670SBarry Smith 4780513a670SBarry Smith if (format != VIEWER_FORMAT_DRAW_CONTOUR) { 4790513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 480cddf8d76SBarry Smith color = DRAW_BLUE; 481416022c9SBarry Smith for ( i=0; i<m; i++ ) { 482cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 483416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 484cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 4853a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 486cddf8d76SBarry Smith if (real(a->a[j]) >= 0.) continue; 487cddf8d76SBarry Smith #else 488cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 489cddf8d76SBarry Smith #endif 490cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 491cddf8d76SBarry Smith } 492cddf8d76SBarry Smith } 493cddf8d76SBarry Smith color = DRAW_CYAN; 494cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 495cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 496cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 497cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 498cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 499cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 500cddf8d76SBarry Smith } 501cddf8d76SBarry Smith } 502cddf8d76SBarry Smith color = DRAW_RED; 503cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 504cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 505cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 506cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 5073a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 508cddf8d76SBarry Smith if (real(a->a[j]) <= 0.) continue; 509cddf8d76SBarry Smith #else 510cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 511cddf8d76SBarry Smith #endif 512cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 513416022c9SBarry Smith } 514416022c9SBarry Smith } 5150513a670SBarry Smith } else { 5160513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 5170513a670SBarry Smith /* first determine max of all nonzero values */ 5180513a670SBarry Smith int nz = a->nz,count; 5190513a670SBarry Smith Draw popup; 5200513a670SBarry Smith 5210513a670SBarry Smith for ( i=0; i<nz; i++ ) { 5220513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 5230513a670SBarry Smith } 524522c5e43SBarry Smith ierr = DrawGetPopup(draw,&popup); CHKERRQ(ierr); 5250513a670SBarry Smith ierr = DrawScalePopup(popup,0.0,maxv); CHKERRQ(ierr); 5260513a670SBarry Smith count = 0; 5270513a670SBarry Smith for ( i=0; i<m; i++ ) { 5280513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 5290513a670SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 5300513a670SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 5310513a670SBarry Smith color = 32 + (int) ((200.0 - 32.0)*PetscAbsScalar(a->a[count])/maxv); 5320513a670SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 5330513a670SBarry Smith count++; 5340513a670SBarry Smith } 5350513a670SBarry Smith } 5360513a670SBarry Smith } 53755843e3eSBarry Smith DrawSynchronizedFlush(draw); 538cddf8d76SBarry Smith DrawGetPause(draw,&pause); 5393a40ed3dSBarry Smith if (pause >= 0) { PetscSleep(pause); PetscFunctionReturn(0);} 540cddf8d76SBarry Smith 541cddf8d76SBarry Smith /* allow the matrix to zoom or shrink */ 5426945ee14SBarry Smith ierr = DrawCheckResizedWindow(draw); 54355843e3eSBarry Smith ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0); 544cddf8d76SBarry Smith while (button != BUTTON_RIGHT) { 54555843e3eSBarry Smith DrawSynchronizedClear(draw); 546cddf8d76SBarry Smith if (button == BUTTON_LEFT) scale = .5; 547cddf8d76SBarry Smith else if (button == BUTTON_CENTER) scale = 2.; 548cddf8d76SBarry Smith xl = scale*(xl + w - xc) + xc - w*scale; 549cddf8d76SBarry Smith xr = scale*(xr - w - xc) + xc + w*scale; 550cddf8d76SBarry Smith yl = scale*(yl + h - yc) + yc - h*scale; 551cddf8d76SBarry Smith yr = scale*(yr - h - yc) + yc + h*scale; 552cddf8d76SBarry Smith w *= scale; h *= scale; 553cddf8d76SBarry Smith ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr); 5540513a670SBarry Smith if (format != VIEWER_FORMAT_DRAW_CONTOUR) { 5550513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 556cddf8d76SBarry Smith color = DRAW_BLUE; 557cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 558cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 559cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 560cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 5613a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 562cddf8d76SBarry Smith if (real(a->a[j]) >= 0.) continue; 563cddf8d76SBarry Smith #else 564cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 565cddf8d76SBarry Smith #endif 566cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 567cddf8d76SBarry Smith } 568cddf8d76SBarry Smith } 569cddf8d76SBarry Smith color = DRAW_CYAN; 570cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 571cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 572cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 573cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 574cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 575cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 576cddf8d76SBarry Smith } 577cddf8d76SBarry Smith } 578cddf8d76SBarry Smith color = DRAW_RED; 579cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 580cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 581cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 582cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 5833a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 584cddf8d76SBarry Smith if (real(a->a[j]) <= 0.) continue; 585cddf8d76SBarry Smith #else 586cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 587cddf8d76SBarry Smith #endif 588cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 589cddf8d76SBarry Smith } 590cddf8d76SBarry Smith } 5910513a670SBarry Smith } else { 5920513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 5930513a670SBarry Smith int count = 0; 5940513a670SBarry Smith for ( i=0; i<m; i++ ) { 5950513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 5960513a670SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 5970513a670SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 5980513a670SBarry Smith color = 32 + (int) ((200.0 - 32.0)*PetscAbsScalar(a->a[count])/maxv); 5993a40ed3dSBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); CHKERRQ(ierr); 6000513a670SBarry Smith count++; 6010513a670SBarry Smith } 6020513a670SBarry Smith } 6030513a670SBarry Smith } 6040513a670SBarry Smith 6053a40ed3dSBarry Smith ierr = DrawCheckResizedWindow(draw); CHKERRQ(ierr); 60655843e3eSBarry Smith ierr = DrawSynchronizedGetMouseButton(draw,&button,&xc,&yc,0,0); CHKERRQ(ierr); 607cddf8d76SBarry Smith } 6083a40ed3dSBarry Smith PetscFunctionReturn(0); 609416022c9SBarry Smith } 610416022c9SBarry Smith 6115615d1e5SSatish Balay #undef __FUNC__ 612d4bb536fSBarry Smith #define __FUNC__ "MatView_SeqAIJ" 613e1311b90SBarry Smith int MatView_SeqAIJ(Mat A,Viewer viewer) 614416022c9SBarry Smith { 615416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 616bcd2baecSBarry Smith ViewerType vtype; 617bcd2baecSBarry Smith int ierr; 618416022c9SBarry Smith 6193a40ed3dSBarry Smith PetscFunctionBegin; 620bcd2baecSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 621bcd2baecSBarry Smith if (vtype == MATLAB_VIEWER) { 6223a40ed3dSBarry Smith ierr = ViewerMatlabPutSparse_Private(viewer,a->m,a->n,a->nz,a->a,a->i,a->j); CHKERRQ(ierr); 6235cd90555SBarry Smith } else if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER){ 6243a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer); CHKERRQ(ierr); 6255cd90555SBarry Smith } else if (vtype == BINARY_FILE_VIEWER) { 6263a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer); CHKERRQ(ierr); 6275cd90555SBarry Smith } else if (vtype == DRAW_VIEWER) { 6283a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer); CHKERRQ(ierr); 6295cd90555SBarry Smith } else { 6305cd90555SBarry Smith SETERRQ(1,1,"Viewer type not supported by PETSc object"); 63117ab2063SBarry Smith } 6323a40ed3dSBarry Smith PetscFunctionReturn(0); 63317ab2063SBarry Smith } 63419bcc07fSBarry Smith 635c456f294SBarry Smith extern int Mat_AIJ_CheckInode(Mat); 6365615d1e5SSatish Balay #undef __FUNC__ 6375615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_SeqAIJ" 6388f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 63917ab2063SBarry Smith { 640416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 64141c01911SSatish Balay int fshift = 0,i,j,*ai = a->i, *aj = a->j, *imax = a->imax,ierr; 64243ee02c3SBarry Smith int m = a->m, *ip, N, *ailen = a->ilen,shift = a->indexshift,rmax = 0; 643416022c9SBarry Smith Scalar *aa = a->a, *ap; 64417ab2063SBarry Smith 6453a40ed3dSBarry Smith PetscFunctionBegin; 6463a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 64717ab2063SBarry Smith 64843ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 64917ab2063SBarry Smith for ( i=1; i<m; i++ ) { 650416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 65117ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 65294a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 65317ab2063SBarry Smith if (fshift) { 654416022c9SBarry Smith ip = aj + ai[i] + shift; ap = aa + ai[i] + shift; 65517ab2063SBarry Smith N = ailen[i]; 65617ab2063SBarry Smith for ( j=0; j<N; j++ ) { 65717ab2063SBarry Smith ip[j-fshift] = ip[j]; 65817ab2063SBarry Smith ap[j-fshift] = ap[j]; 65917ab2063SBarry Smith } 66017ab2063SBarry Smith } 66117ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 66217ab2063SBarry Smith } 66317ab2063SBarry Smith if (m) { 66417ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 66517ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 66617ab2063SBarry Smith } 66717ab2063SBarry Smith /* reset ilen and imax for each row */ 66817ab2063SBarry Smith for ( i=0; i<m; i++ ) { 66917ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 67017ab2063SBarry Smith } 671416022c9SBarry Smith a->nz = ai[m] + shift; 67217ab2063SBarry Smith 67317ab2063SBarry Smith /* diagonals may have moved, so kill the diagonal pointers */ 674416022c9SBarry Smith if (fshift && a->diag) { 6750452661fSBarry Smith PetscFree(a->diag); 676416022c9SBarry Smith PLogObjectMemory(A,-(m+1)*sizeof(int)); 677416022c9SBarry Smith a->diag = 0; 67817ab2063SBarry Smith } 6794e220ebcSLois Curfman McInnes PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded, %d used\n", 6804e220ebcSLois Curfman McInnes m,a->n,fshift,a->nz); 6814e220ebcSLois Curfman McInnes PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues is %d\n", 682b810aeb4SBarry Smith a->reallocs); 68394a9d846SBarry Smith PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax); 684dd5f02e7SSatish Balay a->reallocs = 0; 6854e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 6864e220ebcSLois Curfman McInnes 68776dd722bSSatish Balay /* check out for identical nodes. If found, use inode functions */ 68841c01911SSatish Balay ierr = Mat_AIJ_CheckInode(A); CHKERRQ(ierr); 6893a40ed3dSBarry Smith PetscFunctionReturn(0); 69017ab2063SBarry Smith } 69117ab2063SBarry Smith 6925615d1e5SSatish Balay #undef __FUNC__ 6935615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_SeqAIJ" 6948f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A) 69517ab2063SBarry Smith { 696416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 6973a40ed3dSBarry Smith 6983a40ed3dSBarry Smith PetscFunctionBegin; 699cddf8d76SBarry Smith PetscMemzero(a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar)); 7003a40ed3dSBarry Smith PetscFunctionReturn(0); 70117ab2063SBarry Smith } 702416022c9SBarry Smith 7035615d1e5SSatish Balay #undef __FUNC__ 7045615d1e5SSatish Balay #define __FUNC__ "MatDestroy_SeqAIJ" 705e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A) 70617ab2063SBarry Smith { 707416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 70882bf6240SBarry Smith int ierr; 709d5d45c9bSBarry Smith 7103a40ed3dSBarry Smith PetscFunctionBegin; 7113a40ed3dSBarry Smith #if defined(USE_PETSC_LOG) 712e1311b90SBarry Smith PLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",a->m,a->n,a->nz); 71317ab2063SBarry Smith #endif 7140452661fSBarry Smith PetscFree(a->a); 7150452661fSBarry Smith if (!a->singlemalloc) { PetscFree(a->i); PetscFree(a->j);} 7160452661fSBarry Smith if (a->diag) PetscFree(a->diag); 7170452661fSBarry Smith if (a->ilen) PetscFree(a->ilen); 7180452661fSBarry Smith if (a->imax) PetscFree(a->imax); 7190452661fSBarry Smith if (a->solve_work) PetscFree(a->solve_work); 72076dd722bSSatish Balay if (a->inode.size) PetscFree(a->inode.size); 72182bf6240SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} 7220452661fSBarry Smith PetscFree(a); 723eed86810SBarry Smith 724f2655603SLois Curfman McInnes PLogObjectDestroy(A); 725f2655603SLois Curfman McInnes PetscHeaderDestroy(A); 7263a40ed3dSBarry Smith PetscFunctionReturn(0); 72717ab2063SBarry Smith } 72817ab2063SBarry Smith 7295615d1e5SSatish Balay #undef __FUNC__ 7305615d1e5SSatish Balay #define __FUNC__ "MatCompress_SeqAIJ" 7318f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A) 73217ab2063SBarry Smith { 7333a40ed3dSBarry Smith PetscFunctionBegin; 7343a40ed3dSBarry Smith PetscFunctionReturn(0); 73517ab2063SBarry Smith } 73617ab2063SBarry Smith 7375615d1e5SSatish Balay #undef __FUNC__ 7385615d1e5SSatish Balay #define __FUNC__ "MatSetOption_SeqAIJ" 7398f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op) 74017ab2063SBarry Smith { 741416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 7423a40ed3dSBarry Smith 7433a40ed3dSBarry Smith PetscFunctionBegin; 7446d4a8577SBarry Smith if (op == MAT_ROW_ORIENTED) a->roworiented = 1; 7456d4a8577SBarry Smith else if (op == MAT_COLUMN_ORIENTED) a->roworiented = 0; 7466d4a8577SBarry Smith else if (op == MAT_COLUMNS_SORTED) a->sorted = 1; 747219d9a1aSLois Curfman McInnes else if (op == MAT_COLUMNS_UNSORTED) a->sorted = 0; 7486d4a8577SBarry Smith else if (op == MAT_NO_NEW_NONZERO_LOCATIONS) a->nonew = 1; 749c2653b3dSLois Curfman McInnes else if (op == MAT_NEW_NONZERO_LOCATION_ERROR) a->nonew = -1; 75096854ed6SLois Curfman McInnes else if (op == MAT_NEW_NONZERO_ALLOCATION_ERROR) a->nonew = -2; 7516d4a8577SBarry Smith else if (op == MAT_YES_NEW_NONZERO_LOCATIONS) a->nonew = 0; 7526d4a8577SBarry Smith else if (op == MAT_ROWS_SORTED || 753219d9a1aSLois Curfman McInnes op == MAT_ROWS_UNSORTED || 7546d4a8577SBarry Smith op == MAT_SYMMETRIC || 7556d4a8577SBarry Smith op == MAT_STRUCTURALLY_SYMMETRIC || 75690f02eecSBarry Smith op == MAT_YES_NEW_DIAGONALS || 757b51ba29fSSatish Balay op == MAT_IGNORE_OFF_PROC_ENTRIES|| 758b51ba29fSSatish Balay op == MAT_USE_HASH_TABLE) 759981c4779SBarry Smith PLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n"); 7603a40ed3dSBarry Smith else if (op == MAT_NO_NEW_DIAGONALS) { 7613a40ed3dSBarry Smith SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS"); 7623a40ed3dSBarry Smith } else if (op == MAT_INODE_LIMIT_1) a->inode.limit = 1; 7636d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_2) a->inode.limit = 2; 7646d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_3) a->inode.limit = 3; 7656d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_4) a->inode.limit = 4; 7666d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_5) a->inode.limit = 5; 7673a40ed3dSBarry Smith else SETERRQ(PETSC_ERR_SUP,0,"unknown option"); 7683a40ed3dSBarry Smith PetscFunctionReturn(0); 76917ab2063SBarry Smith } 77017ab2063SBarry Smith 7715615d1e5SSatish Balay #undef __FUNC__ 7725615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_SeqAIJ" 7738f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v) 77417ab2063SBarry Smith { 775416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 7763a40ed3dSBarry Smith int i,j, n,shift = a->indexshift,ierr; 77717ab2063SBarry Smith Scalar *x, zero = 0.0; 77817ab2063SBarry Smith 7793a40ed3dSBarry Smith PetscFunctionBegin; 7803a40ed3dSBarry Smith ierr = VecSet(&zero,v);CHKERRQ(ierr); 781e1311b90SBarry Smith ierr = VecGetArray(v,&x);;CHKERRQ(ierr); 782e1311b90SBarry Smith ierr = VecGetLocalSize(v,&n);;CHKERRQ(ierr); 783a8c6a408SBarry Smith if (n != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Nonconforming matrix and vector"); 784416022c9SBarry Smith for ( i=0; i<a->m; i++ ) { 785416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 786416022c9SBarry Smith if (a->j[j]+shift == i) { 787416022c9SBarry Smith x[i] = a->a[j]; 78817ab2063SBarry Smith break; 78917ab2063SBarry Smith } 79017ab2063SBarry Smith } 79117ab2063SBarry Smith } 792e1311b90SBarry Smith ierr = VecRestoreArray(v,&x);;CHKERRQ(ierr); 7933a40ed3dSBarry Smith PetscFunctionReturn(0); 79417ab2063SBarry Smith } 79517ab2063SBarry Smith 79617ab2063SBarry Smith /* -------------------------------------------------------*/ 79717ab2063SBarry Smith /* Should check that shapes of vectors and matrices match */ 79817ab2063SBarry Smith /* -------------------------------------------------------*/ 7995615d1e5SSatish Balay #undef __FUNC__ 8005615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_SeqAIJ" 80144cd7ae7SLois Curfman McInnes int MatMultTrans_SeqAIJ(Mat A,Vec xx,Vec yy) 80217ab2063SBarry Smith { 803416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 80417ab2063SBarry Smith Scalar *x, *y, *v, alpha; 805e1311b90SBarry Smith int ierr,m = a->m, n, i, *idx, shift = a->indexshift; 80617ab2063SBarry Smith 8073a40ed3dSBarry Smith PetscFunctionBegin; 808e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 809e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 810cddf8d76SBarry Smith PetscMemzero(y,a->n*sizeof(Scalar)); 81117ab2063SBarry Smith y = y + shift; /* shift for Fortran start by 1 indexing */ 81217ab2063SBarry Smith for ( i=0; i<m; i++ ) { 813416022c9SBarry Smith idx = a->j + a->i[i] + shift; 814416022c9SBarry Smith v = a->a + a->i[i] + shift; 815416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 81617ab2063SBarry Smith alpha = x[i]; 81717ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 81817ab2063SBarry Smith } 819416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 820e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 821e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 8223a40ed3dSBarry Smith PetscFunctionReturn(0); 82317ab2063SBarry Smith } 824d5d45c9bSBarry Smith 8255615d1e5SSatish Balay #undef __FUNC__ 8265615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_SeqAIJ" 82744cd7ae7SLois Curfman McInnes int MatMultTransAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 82817ab2063SBarry Smith { 829416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 83017ab2063SBarry Smith Scalar *x, *y, *v, alpha; 831e1311b90SBarry Smith int ierr,m = a->m, n, i, *idx,shift = a->indexshift; 83217ab2063SBarry Smith 8333a40ed3dSBarry Smith PetscFunctionBegin; 834e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 835e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 83617ab2063SBarry Smith if (zz != yy) VecCopy(zz,yy); 83717ab2063SBarry Smith y = y + shift; /* shift for Fortran start by 1 indexing */ 83817ab2063SBarry Smith for ( i=0; i<m; i++ ) { 839416022c9SBarry Smith idx = a->j + a->i[i] + shift; 840416022c9SBarry Smith v = a->a + a->i[i] + shift; 841416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 84217ab2063SBarry Smith alpha = x[i]; 84317ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 84417ab2063SBarry Smith } 84590f02eecSBarry Smith PLogFlops(2*a->nz); 846e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 847e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 8483a40ed3dSBarry Smith PetscFunctionReturn(0); 84917ab2063SBarry Smith } 85017ab2063SBarry Smith 8515615d1e5SSatish Balay #undef __FUNC__ 8525615d1e5SSatish Balay #define __FUNC__ "MatMult_SeqAIJ" 85344cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 85417ab2063SBarry Smith { 855416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 85617ab2063SBarry Smith Scalar *x, *y, *v, sum; 857e1311b90SBarry Smith int ierr,m = a->m, *idx, shift = a->indexshift,*ii; 858e36a17ebSSatish Balay #if !defined(USE_FORTRAN_KERNELS) 859e36a17ebSSatish Balay int n, i, jrow,j; 860e36a17ebSSatish Balay #endif 86117ab2063SBarry Smith 862fee21e36SBarry Smith #if defined(HAVE_PRAGMA_DISJOINT) 863fee21e36SBarry Smith #pragma disjoint(*x,*y,*v) 864fee21e36SBarry Smith #endif 865fee21e36SBarry Smith 8663a40ed3dSBarry Smith PetscFunctionBegin; 867e1311b90SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 868e1311b90SBarry Smith ierr = VecGetArray(yy,&y); CHKERRQ(ierr); 86917ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 870416022c9SBarry Smith idx = a->j; 871d64ed03dSBarry Smith v = a->a; 872416022c9SBarry Smith ii = a->i; 8738d195f9aSBarry Smith #if defined(USE_FORTRAN_KERNELS) 87429b5ca8aSSatish Balay fortranmultaij_(&m,x,ii,idx+shift,v+shift,y); 8758d195f9aSBarry Smith #else 876d64ed03dSBarry Smith v += shift; /* shift for Fortran start by 1 indexing */ 877d64ed03dSBarry Smith idx += shift; 87817ab2063SBarry Smith for ( i=0; i<m; i++ ) { 8799ea0dfa2SSatish Balay jrow = ii[i]; 8809ea0dfa2SSatish Balay n = ii[i+1] - jrow; 88117ab2063SBarry Smith sum = 0.0; 8829ea0dfa2SSatish Balay for ( j=0; j<n; j++) { 8839ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 8849ea0dfa2SSatish Balay } 88517ab2063SBarry Smith y[i] = sum; 88617ab2063SBarry Smith } 8878d195f9aSBarry Smith #endif 888416022c9SBarry Smith PLogFlops(2*a->nz - m); 889e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 890e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y); CHKERRQ(ierr); 8913a40ed3dSBarry Smith PetscFunctionReturn(0); 89217ab2063SBarry Smith } 89317ab2063SBarry Smith 8945615d1e5SSatish Balay #undef __FUNC__ 8955615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_SeqAIJ" 89644cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 89717ab2063SBarry Smith { 898416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 89917ab2063SBarry Smith Scalar *x, *y, *z, *v, sum; 900e1311b90SBarry Smith int ierr,m = a->m, *idx, shift = a->indexshift,*ii; 901e36a17ebSSatish Balay #if !defined(USE_FORTRAN_KERNELS) 902e36a17ebSSatish Balay int n,i,jrow,j; 903e36a17ebSSatish Balay #endif 9049ea0dfa2SSatish Balay 9053a40ed3dSBarry Smith PetscFunctionBegin; 906e1311b90SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 907e1311b90SBarry Smith ierr = VecGetArray(yy,&y); CHKERRQ(ierr); 908e1311b90SBarry Smith ierr = VecGetArray(zz,&z); CHKERRQ(ierr); 90917ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 910cddf8d76SBarry Smith idx = a->j; 911d64ed03dSBarry Smith v = a->a; 912cddf8d76SBarry Smith ii = a->i; 91302ab625aSSatish Balay #if defined(USE_FORTRAN_KERNELS) 91429b5ca8aSSatish Balay fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z); 91502ab625aSSatish Balay #else 916d64ed03dSBarry Smith v += shift; /* shift for Fortran start by 1 indexing */ 917d64ed03dSBarry Smith idx += shift; 91817ab2063SBarry Smith for ( i=0; i<m; i++ ) { 9199ea0dfa2SSatish Balay jrow = ii[i]; 9209ea0dfa2SSatish Balay n = ii[i+1] - jrow; 92117ab2063SBarry Smith sum = y[i]; 9229ea0dfa2SSatish Balay for ( j=0; j<n; j++) { 9239ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 9249ea0dfa2SSatish Balay } 92517ab2063SBarry Smith z[i] = sum; 92617ab2063SBarry Smith } 92702ab625aSSatish Balay #endif 928416022c9SBarry Smith PLogFlops(2*a->nz); 929e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 930e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y); CHKERRQ(ierr); 931e1311b90SBarry Smith ierr = VecRestoreArray(zz,&z); CHKERRQ(ierr); 9323a40ed3dSBarry Smith PetscFunctionReturn(0); 93317ab2063SBarry Smith } 93417ab2063SBarry Smith 93517ab2063SBarry Smith /* 93617ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 93717ab2063SBarry Smith */ 93817ab2063SBarry Smith 9395615d1e5SSatish Balay #undef __FUNC__ 9405615d1e5SSatish Balay #define __FUNC__ "MatMarkDiag_SeqAIJ" 941416022c9SBarry Smith int MatMarkDiag_SeqAIJ(Mat A) 94217ab2063SBarry Smith { 943416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 944416022c9SBarry Smith int i,j, *diag, m = a->m,shift = a->indexshift; 94517ab2063SBarry Smith 9463a40ed3dSBarry Smith PetscFunctionBegin; 9470452661fSBarry Smith diag = (int *) PetscMalloc( (m+1)*sizeof(int)); CHKPTRQ(diag); 948416022c9SBarry Smith PLogObjectMemory(A,(m+1)*sizeof(int)); 949416022c9SBarry Smith for ( i=0; i<a->m; i++ ) { 950416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 951416022c9SBarry Smith if (a->j[j]+shift == i) { 95217ab2063SBarry Smith diag[i] = j - shift; 95317ab2063SBarry Smith break; 95417ab2063SBarry Smith } 95517ab2063SBarry Smith } 95617ab2063SBarry Smith } 957416022c9SBarry Smith a->diag = diag; 9583a40ed3dSBarry Smith PetscFunctionReturn(0); 95917ab2063SBarry Smith } 96017ab2063SBarry Smith 9615615d1e5SSatish Balay #undef __FUNC__ 9625615d1e5SSatish Balay #define __FUNC__ "MatRelax_SeqAIJ" 96344cd7ae7SLois Curfman McInnes int MatRelax_SeqAIJ(Mat A,Vec bb,double omega,MatSORType flag, 96417ab2063SBarry Smith double fshift,int its,Vec xx) 96517ab2063SBarry Smith { 966416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 967416022c9SBarry Smith Scalar *x, *b, *bs, d, *xs, sum, *v = a->a,*t,scale,*ts, *xb; 968d5d45c9bSBarry Smith int ierr, *idx, *diag,n = a->n, m = a->m, i, shift = a->indexshift; 96917ab2063SBarry Smith 9703a40ed3dSBarry Smith PetscFunctionBegin; 971e1311b90SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 972e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 973d00d2cf4SBarry Smith if (!a->diag) {ierr = MatMarkDiag_SeqAIJ(A);CHKERRQ(ierr);} 974416022c9SBarry Smith diag = a->diag; 975416022c9SBarry Smith xs = x + shift; /* shifted by one for index start of a or a->j*/ 97617ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 97717ab2063SBarry Smith /* apply ( U + D/omega) to the vector */ 97817ab2063SBarry Smith bs = b + shift; 97917ab2063SBarry Smith for ( i=0; i<m; i++ ) { 980416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 981416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 982416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 983416022c9SBarry Smith v = a->a + diag[i] + (!shift); 98417ab2063SBarry Smith sum = b[i]*d/omega; 98517ab2063SBarry Smith SPARSEDENSEDOT(sum,bs,v,idx,n); 98617ab2063SBarry Smith x[i] = sum; 98717ab2063SBarry Smith } 9883a40ed3dSBarry Smith PetscFunctionReturn(0); 98917ab2063SBarry Smith } 99017ab2063SBarry Smith if (flag == SOR_APPLY_LOWER) { 991a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"SOR_APPLY_LOWER is not done"); 9923a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 99317ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 99417ab2063SBarry Smith U is upper triangular, E is diagonal; This routine applies 99517ab2063SBarry Smith 99617ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 99717ab2063SBarry Smith 99817ab2063SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 99917ab2063SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 100017ab2063SBarry Smith is the relaxation factor. 100117ab2063SBarry Smith */ 10020452661fSBarry Smith t = (Scalar *) PetscMalloc( m*sizeof(Scalar) ); CHKPTRQ(t); 100317ab2063SBarry Smith scale = (2.0/omega) - 1.0; 100417ab2063SBarry Smith 100517ab2063SBarry Smith /* x = (E + U)^{-1} b */ 100617ab2063SBarry Smith for ( i=m-1; i>=0; i-- ) { 1007416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1008416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1009416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1010416022c9SBarry Smith v = a->a + diag[i] + (!shift); 101117ab2063SBarry Smith sum = b[i]; 101217ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 101317ab2063SBarry Smith x[i] = omega*(sum/d); 101417ab2063SBarry Smith } 101517ab2063SBarry Smith 101617ab2063SBarry Smith /* t = b - (2*E - D)x */ 1017416022c9SBarry Smith v = a->a; 101817ab2063SBarry Smith for ( i=0; i<m; i++ ) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; } 101917ab2063SBarry Smith 102017ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1021416022c9SBarry Smith ts = t + shift; /* shifted by one for index start of a or a->j*/ 1022416022c9SBarry Smith diag = a->diag; 102317ab2063SBarry Smith for ( i=0; i<m; i++ ) { 1024416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1025416022c9SBarry Smith n = diag[i] - a->i[i]; 1026416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1027416022c9SBarry Smith v = a->a + a->i[i] + shift; 102817ab2063SBarry Smith sum = t[i]; 102917ab2063SBarry Smith SPARSEDENSEMDOT(sum,ts,v,idx,n); 103017ab2063SBarry Smith t[i] = omega*(sum/d); 103117ab2063SBarry Smith } 103217ab2063SBarry Smith 103317ab2063SBarry Smith /* x = x + t */ 103417ab2063SBarry Smith for ( i=0; i<m; i++ ) { x[i] += t[i]; } 10350452661fSBarry Smith PetscFree(t); 10363a40ed3dSBarry Smith PetscFunctionReturn(0); 103717ab2063SBarry Smith } 103817ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 103917ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 104017ab2063SBarry Smith for ( i=0; i<m; i++ ) { 1041416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1042416022c9SBarry Smith n = diag[i] - a->i[i]; 1043416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1044416022c9SBarry Smith v = a->a + a->i[i] + shift; 104517ab2063SBarry Smith sum = b[i]; 104617ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 104717ab2063SBarry Smith x[i] = omega*(sum/d); 104817ab2063SBarry Smith } 104917ab2063SBarry Smith xb = x; 10503a40ed3dSBarry Smith } else xb = b; 105117ab2063SBarry Smith if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) && 105217ab2063SBarry Smith (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) { 105317ab2063SBarry Smith for ( i=0; i<m; i++ ) { 1054416022c9SBarry Smith x[i] *= a->a[diag[i]+shift]; 105517ab2063SBarry Smith } 105617ab2063SBarry Smith } 105717ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 105817ab2063SBarry Smith for ( i=m-1; i>=0; i-- ) { 1059416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1060416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1061416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1062416022c9SBarry Smith v = a->a + diag[i] + (!shift); 106317ab2063SBarry Smith sum = xb[i]; 106417ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 106517ab2063SBarry Smith x[i] = omega*(sum/d); 106617ab2063SBarry Smith } 106717ab2063SBarry Smith } 106817ab2063SBarry Smith its--; 106917ab2063SBarry Smith } 107017ab2063SBarry Smith while (its--) { 107117ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 107217ab2063SBarry Smith for ( i=0; i<m; i++ ) { 1073416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1074416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1075416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1076416022c9SBarry Smith v = a->a + a->i[i] + shift; 107717ab2063SBarry Smith sum = b[i]; 107817ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 10797e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 108017ab2063SBarry Smith } 108117ab2063SBarry Smith } 108217ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 108317ab2063SBarry Smith for ( i=m-1; i>=0; i-- ) { 1084416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1085416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1086416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1087416022c9SBarry Smith v = a->a + a->i[i] + shift; 108817ab2063SBarry Smith sum = b[i]; 108917ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 10907e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 109117ab2063SBarry Smith } 109217ab2063SBarry Smith } 109317ab2063SBarry Smith } 10943a40ed3dSBarry Smith PetscFunctionReturn(0); 109517ab2063SBarry Smith } 109617ab2063SBarry Smith 10975615d1e5SSatish Balay #undef __FUNC__ 10985615d1e5SSatish Balay #define __FUNC__ "MatGetInfo_SeqAIJ" 10998f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 110017ab2063SBarry Smith { 1101416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 11024e220ebcSLois Curfman McInnes 11033a40ed3dSBarry Smith PetscFunctionBegin; 11044e220ebcSLois Curfman McInnes info->rows_global = (double)a->m; 11054e220ebcSLois Curfman McInnes info->columns_global = (double)a->n; 11064e220ebcSLois Curfman McInnes info->rows_local = (double)a->m; 11074e220ebcSLois Curfman McInnes info->columns_local = (double)a->n; 11084e220ebcSLois Curfman McInnes info->block_size = 1.0; 11094e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 11104e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 11114e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 11124e220ebcSLois Curfman McInnes /* if (info->nz_unneeded != A->info.nz_unneeded) 11134e220ebcSLois Curfman McInnes printf("space descrepancy: maxnz-nz = %d, nz_unneeded = %d\n",(int)info->nz_unneeded,(int)A->info.nz_unneeded); */ 11144e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 11154e220ebcSLois Curfman McInnes info->mallocs = (double)a->reallocs; 11164e220ebcSLois Curfman McInnes info->memory = A->mem; 11174e220ebcSLois Curfman McInnes if (A->factor) { 11184e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 11194e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 11204e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 11214e220ebcSLois Curfman McInnes } else { 11224e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 11234e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 11244e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 11254e220ebcSLois Curfman McInnes } 11263a40ed3dSBarry Smith PetscFunctionReturn(0); 112717ab2063SBarry Smith } 112817ab2063SBarry Smith 112917ab2063SBarry Smith extern int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,double,Mat*); 113017ab2063SBarry Smith extern int MatLUFactorNumeric_SeqAIJ(Mat,Mat*); 113117ab2063SBarry Smith extern int MatLUFactor_SeqAIJ(Mat,IS,IS,double); 113217ab2063SBarry Smith extern int MatSolve_SeqAIJ(Mat,Vec,Vec); 113317ab2063SBarry Smith extern int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec); 113417ab2063SBarry Smith extern int MatSolveTrans_SeqAIJ(Mat,Vec,Vec); 113517ab2063SBarry Smith extern int MatSolveTransAdd_SeqAIJ(Mat,Vec,Vec,Vec); 113617ab2063SBarry Smith 11375615d1e5SSatish Balay #undef __FUNC__ 11385615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_SeqAIJ" 11398f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag) 114017ab2063SBarry Smith { 1141416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1142416022c9SBarry Smith int i,ierr,N, *rows,m = a->m - 1,shift = a->indexshift; 114317ab2063SBarry Smith 11443a40ed3dSBarry Smith PetscFunctionBegin; 114577c4ece6SBarry Smith ierr = ISGetSize(is,&N); CHKERRQ(ierr); 114617ab2063SBarry Smith ierr = ISGetIndices(is,&rows); CHKERRQ(ierr); 114717ab2063SBarry Smith if (diag) { 114817ab2063SBarry Smith for ( i=0; i<N; i++ ) { 1149a8c6a408SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range"); 1150416022c9SBarry Smith if (a->ilen[rows[i]] > 0) { /* in case row was completely empty */ 1151416022c9SBarry Smith a->ilen[rows[i]] = 1; 1152416022c9SBarry Smith a->a[a->i[rows[i]]+shift] = *diag; 1153416022c9SBarry Smith a->j[a->i[rows[i]]+shift] = rows[i]+shift; 11543a40ed3dSBarry Smith } else { 1155d64ed03dSBarry Smith ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr); 115617ab2063SBarry Smith } 115717ab2063SBarry Smith } 11583a40ed3dSBarry Smith } else { 115917ab2063SBarry Smith for ( i=0; i<N; i++ ) { 1160a8c6a408SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"row out of range"); 1161416022c9SBarry Smith a->ilen[rows[i]] = 0; 116217ab2063SBarry Smith } 116317ab2063SBarry Smith } 116417ab2063SBarry Smith ISRestoreIndices(is,&rows); 116543a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 11663a40ed3dSBarry Smith PetscFunctionReturn(0); 116717ab2063SBarry Smith } 116817ab2063SBarry Smith 11695615d1e5SSatish Balay #undef __FUNC__ 11705615d1e5SSatish Balay #define __FUNC__ "MatGetSize_SeqAIJ" 11718f6be9afSLois Curfman McInnes int MatGetSize_SeqAIJ(Mat A,int *m,int *n) 117217ab2063SBarry Smith { 1173416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 11743a40ed3dSBarry Smith 11753a40ed3dSBarry Smith PetscFunctionBegin; 11760752156aSBarry Smith if (m) *m = a->m; 11770752156aSBarry Smith if (n) *n = a->n; 11783a40ed3dSBarry Smith PetscFunctionReturn(0); 117917ab2063SBarry Smith } 118017ab2063SBarry Smith 11815615d1e5SSatish Balay #undef __FUNC__ 11825615d1e5SSatish Balay #define __FUNC__ "MatGetOwnershipRange_SeqAIJ" 11838f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n) 118417ab2063SBarry Smith { 1185416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 11863a40ed3dSBarry Smith 11873a40ed3dSBarry Smith PetscFunctionBegin; 1188416022c9SBarry Smith *m = 0; *n = a->m; 11893a40ed3dSBarry Smith PetscFunctionReturn(0); 119017ab2063SBarry Smith } 1191026e39d0SSatish Balay 11925615d1e5SSatish Balay #undef __FUNC__ 11935615d1e5SSatish Balay #define __FUNC__ "MatGetRow_SeqAIJ" 11944e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v) 119517ab2063SBarry Smith { 1196416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1197c456f294SBarry Smith int *itmp,i,shift = a->indexshift; 119817ab2063SBarry Smith 11993a40ed3dSBarry Smith PetscFunctionBegin; 1200a8c6a408SBarry Smith if (row < 0 || row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row out of range"); 120117ab2063SBarry Smith 1202416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1203416022c9SBarry Smith if (v) *v = a->a + a->i[row] + shift; 120417ab2063SBarry Smith if (idx) { 1205416022c9SBarry Smith itmp = a->j + a->i[row] + shift; 12064e093b46SBarry Smith if (*nz && shift) { 12070452661fSBarry Smith *idx = (int *) PetscMalloc( (*nz)*sizeof(int) ); CHKPTRQ(*idx); 120817ab2063SBarry Smith for ( i=0; i<(*nz); i++ ) {(*idx)[i] = itmp[i] + shift;} 12094e093b46SBarry Smith } else if (*nz) { 12104e093b46SBarry Smith *idx = itmp; 121117ab2063SBarry Smith } 121217ab2063SBarry Smith else *idx = 0; 121317ab2063SBarry Smith } 12143a40ed3dSBarry Smith PetscFunctionReturn(0); 121517ab2063SBarry Smith } 121617ab2063SBarry Smith 12175615d1e5SSatish Balay #undef __FUNC__ 12185615d1e5SSatish Balay #define __FUNC__ "MatRestoreRow_SeqAIJ" 12194e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v) 122017ab2063SBarry Smith { 12214e093b46SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 12223a40ed3dSBarry Smith 12233a40ed3dSBarry Smith PetscFunctionBegin; 12244e093b46SBarry Smith if (idx) {if (*idx && a->indexshift) PetscFree(*idx);} 12253a40ed3dSBarry Smith PetscFunctionReturn(0); 122617ab2063SBarry Smith } 122717ab2063SBarry Smith 12285615d1e5SSatish Balay #undef __FUNC__ 12295615d1e5SSatish Balay #define __FUNC__ "MatNorm_SeqAIJ" 12308f6be9afSLois Curfman McInnes int MatNorm_SeqAIJ(Mat A,NormType type,double *norm) 123117ab2063SBarry Smith { 1232416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1233416022c9SBarry Smith Scalar *v = a->a; 123417ab2063SBarry Smith double sum = 0.0; 1235416022c9SBarry Smith int i, j,shift = a->indexshift; 123617ab2063SBarry Smith 12373a40ed3dSBarry Smith PetscFunctionBegin; 123817ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1239416022c9SBarry Smith for (i=0; i<a->nz; i++ ) { 12403a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 124117ab2063SBarry Smith sum += real(conj(*v)*(*v)); v++; 124217ab2063SBarry Smith #else 124317ab2063SBarry Smith sum += (*v)*(*v); v++; 124417ab2063SBarry Smith #endif 124517ab2063SBarry Smith } 124617ab2063SBarry Smith *norm = sqrt(sum); 12473a40ed3dSBarry Smith } else if (type == NORM_1) { 124817ab2063SBarry Smith double *tmp; 1249416022c9SBarry Smith int *jj = a->j; 125066963ce1SSatish Balay tmp = (double *) PetscMalloc( (a->n+1)*sizeof(double) ); CHKPTRQ(tmp); 1251cddf8d76SBarry Smith PetscMemzero(tmp,a->n*sizeof(double)); 125217ab2063SBarry Smith *norm = 0.0; 1253416022c9SBarry Smith for ( j=0; j<a->nz; j++ ) { 1254a2744918SBarry Smith tmp[*jj++ + shift] += PetscAbsScalar(*v); v++; 125517ab2063SBarry Smith } 1256416022c9SBarry Smith for ( j=0; j<a->n; j++ ) { 125717ab2063SBarry Smith if (tmp[j] > *norm) *norm = tmp[j]; 125817ab2063SBarry Smith } 12590452661fSBarry Smith PetscFree(tmp); 12603a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 126117ab2063SBarry Smith *norm = 0.0; 1262416022c9SBarry Smith for ( j=0; j<a->m; j++ ) { 1263416022c9SBarry Smith v = a->a + a->i[j] + shift; 126417ab2063SBarry Smith sum = 0.0; 1265416022c9SBarry Smith for ( i=0; i<a->i[j+1]-a->i[j]; i++ ) { 1266cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 126717ab2063SBarry Smith } 126817ab2063SBarry Smith if (sum > *norm) *norm = sum; 126917ab2063SBarry Smith } 12703a40ed3dSBarry Smith } else { 1271a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"No support for two norm"); 127217ab2063SBarry Smith } 12733a40ed3dSBarry Smith PetscFunctionReturn(0); 127417ab2063SBarry Smith } 127517ab2063SBarry Smith 12765615d1e5SSatish Balay #undef __FUNC__ 12775615d1e5SSatish Balay #define __FUNC__ "MatTranspose_SeqAIJ" 12788f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B) 127917ab2063SBarry Smith { 1280416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1281416022c9SBarry Smith Mat C; 1282416022c9SBarry Smith int i, ierr, *aj = a->j, *ai = a->i, m = a->m, len, *col; 1283416022c9SBarry Smith int shift = a->indexshift; 1284d5d45c9bSBarry Smith Scalar *array = a->a; 128517ab2063SBarry Smith 12863a40ed3dSBarry Smith PetscFunctionBegin; 1287a8c6a408SBarry Smith if (B == PETSC_NULL && m != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place"); 12880452661fSBarry Smith col = (int *) PetscMalloc((1+a->n)*sizeof(int)); CHKPTRQ(col); 1289cddf8d76SBarry Smith PetscMemzero(col,(1+a->n)*sizeof(int)); 129017ab2063SBarry Smith if (shift) { 129117ab2063SBarry Smith for ( i=0; i<ai[m]-1; i++ ) aj[i] -= 1; 129217ab2063SBarry Smith } 129317ab2063SBarry Smith for ( i=0; i<ai[m]+shift; i++ ) col[aj[i]] += 1; 1294416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,a->n,m,0,col,&C); CHKERRQ(ierr); 12950452661fSBarry Smith PetscFree(col); 129617ab2063SBarry Smith for ( i=0; i<m; i++ ) { 129717ab2063SBarry Smith len = ai[i+1]-ai[i]; 1298416022c9SBarry Smith ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES); CHKERRQ(ierr); 129917ab2063SBarry Smith array += len; aj += len; 130017ab2063SBarry Smith } 130117ab2063SBarry Smith if (shift) { 130217ab2063SBarry Smith for ( i=0; i<ai[m]-1; i++ ) aj[i] += 1; 130317ab2063SBarry Smith } 130417ab2063SBarry Smith 13056d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 13066d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 130717ab2063SBarry Smith 13083638b69dSLois Curfman McInnes if (B != PETSC_NULL) { 1309416022c9SBarry Smith *B = C; 131017ab2063SBarry Smith } else { 1311f830108cSBarry Smith PetscOps *Abops; 1312f830108cSBarry Smith struct _MatOps *Aops; 1313f830108cSBarry Smith 1314416022c9SBarry Smith /* This isn't really an in-place transpose */ 13150452661fSBarry Smith PetscFree(a->a); 13160452661fSBarry Smith if (!a->singlemalloc) {PetscFree(a->i); PetscFree(a->j);} 13170452661fSBarry Smith if (a->diag) PetscFree(a->diag); 13180452661fSBarry Smith if (a->ilen) PetscFree(a->ilen); 13190452661fSBarry Smith if (a->imax) PetscFree(a->imax); 13200452661fSBarry Smith if (a->solve_work) PetscFree(a->solve_work); 13211073c447SSatish Balay if (a->inode.size) PetscFree(a->inode.size); 13220452661fSBarry Smith PetscFree(a); 1323f830108cSBarry Smith 1324f830108cSBarry Smith /* 1325f830108cSBarry Smith This is horrible, horrible code. We need to keep the 13268f0f457cSSatish Balay the bops and ops Structures, copy everything from C 13278f0f457cSSatish Balay including the function pointers.. 1328f830108cSBarry Smith */ 13298f0f457cSSatish Balay PetscMemcpy(A->ops,C->ops,sizeof(struct _MatOps)); 13308f0f457cSSatish Balay PetscMemcpy(A->bops,C->bops,sizeof(PetscOps)); 1331f830108cSBarry Smith Abops = A->bops; 1332f830108cSBarry Smith Aops = A->ops; 1333f09e8eb9SSatish Balay PetscMemcpy(A,C,sizeof(struct _p_Mat)); 1334f830108cSBarry Smith A->bops = Abops; 1335f830108cSBarry Smith A->ops = Aops; 1336f830108cSBarry Smith 13370452661fSBarry Smith PetscHeaderDestroy(C); 133817ab2063SBarry Smith } 13393a40ed3dSBarry Smith PetscFunctionReturn(0); 134017ab2063SBarry Smith } 134117ab2063SBarry Smith 13425615d1e5SSatish Balay #undef __FUNC__ 13435615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_SeqAIJ" 13448f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 134517ab2063SBarry Smith { 1346416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 134717ab2063SBarry Smith Scalar *l,*r,x,*v; 1348e1311b90SBarry Smith int ierr,i,j,m = a->m, n = a->n, M, nz = a->nz, *jj,shift = a->indexshift; 134917ab2063SBarry Smith 13503a40ed3dSBarry Smith PetscFunctionBegin; 135117ab2063SBarry Smith if (ll) { 13523ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 13533ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1354e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1355a8c6a408SBarry Smith if (m != a->m) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Left scaling vector wrong length"); 1356e1311b90SBarry Smith ierr = VecGetArray(ll,&l); CHKERRQ(ierr); 1357416022c9SBarry Smith v = a->a; 135817ab2063SBarry Smith for ( i=0; i<m; i++ ) { 135917ab2063SBarry Smith x = l[i]; 1360416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 136117ab2063SBarry Smith for ( j=0; j<M; j++ ) { (*v++) *= x;} 136217ab2063SBarry Smith } 1363e1311b90SBarry Smith ierr = VecRestoreArray(ll,&l); CHKERRQ(ierr); 136444cd7ae7SLois Curfman McInnes PLogFlops(nz); 136517ab2063SBarry Smith } 136617ab2063SBarry Smith if (rr) { 1367e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1368a8c6a408SBarry Smith if (n != a->n) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Right scaling vector wrong length"); 1369e1311b90SBarry Smith ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1370416022c9SBarry Smith v = a->a; jj = a->j; 137117ab2063SBarry Smith for ( i=0; i<nz; i++ ) { 137217ab2063SBarry Smith (*v++) *= r[*jj++ + shift]; 137317ab2063SBarry Smith } 1374e1311b90SBarry Smith ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 137544cd7ae7SLois Curfman McInnes PLogFlops(nz); 137617ab2063SBarry Smith } 13773a40ed3dSBarry Smith PetscFunctionReturn(0); 137817ab2063SBarry Smith } 137917ab2063SBarry Smith 13805615d1e5SSatish Balay #undef __FUNC__ 13815615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrix_SeqAIJ" 13826a6a5d1dSBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatGetSubMatrixCall scall,Mat *B) 138317ab2063SBarry Smith { 1384db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data,*c; 1385d5db1b72SBarry Smith int *smap, i, k, kstart, kend, ierr, oldcols = a->n,*lens; 138699141d43SSatish Balay int row,mat_i,*mat_j,tcol,first,step,*mat_ilen; 1387a2744918SBarry Smith register int sum,lensi; 138899141d43SSatish Balay int *irow, *icol, nrows, ncols, shift = a->indexshift,*ssmap; 138999141d43SSatish Balay int *starts,*j_new,*i_new,*aj = a->j, *ai = a->i,ii,*ailen = a->ilen; 139099141d43SSatish Balay Scalar *a_new,*mat_a; 1391416022c9SBarry Smith Mat C; 1392fee21e36SBarry Smith PetscTruth stride; 139317ab2063SBarry Smith 13943a40ed3dSBarry Smith PetscFunctionBegin; 1395d64ed03dSBarry Smith ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr); 1396a8c6a408SBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"ISrow is not sorted"); 1397d64ed03dSBarry Smith ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr); 1398a8c6a408SBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"IScol is not sorted"); 139999141d43SSatish Balay 140017ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow); CHKERRQ(ierr); 140117ab2063SBarry Smith ierr = ISGetSize(isrow,&nrows); CHKERRQ(ierr); 140217ab2063SBarry Smith ierr = ISGetSize(iscol,&ncols); CHKERRQ(ierr); 140317ab2063SBarry Smith 1404fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step); CHKERRQ(ierr); 1405fee21e36SBarry Smith ierr = ISStride(iscol,&stride); CHKERRQ(ierr); 1406fee21e36SBarry Smith if (stride && step == 1) { 140702834360SBarry Smith /* special case of contiguous rows */ 140857faeb66SBarry Smith lens = (int *) PetscMalloc((ncols+nrows+1)*sizeof(int)); CHKPTRQ(lens); 140902834360SBarry Smith starts = lens + ncols; 141002834360SBarry Smith /* loop over new rows determining lens and starting points */ 141102834360SBarry Smith for (i=0; i<nrows; i++) { 1412a2744918SBarry Smith kstart = ai[irow[i]]+shift; 1413a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 141402834360SBarry Smith for ( k=kstart; k<kend; k++ ) { 1415d8ced48eSBarry Smith if (aj[k]+shift >= first) { 141602834360SBarry Smith starts[i] = k; 141702834360SBarry Smith break; 141802834360SBarry Smith } 141902834360SBarry Smith } 1420a2744918SBarry Smith sum = 0; 142102834360SBarry Smith while (k < kend) { 1422d8ced48eSBarry Smith if (aj[k++]+shift >= first+ncols) break; 1423a2744918SBarry Smith sum++; 142402834360SBarry Smith } 1425a2744918SBarry Smith lens[i] = sum; 142602834360SBarry Smith } 142702834360SBarry Smith /* create submatrix */ 1428cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 142908480c60SBarry Smith int n_cols,n_rows; 143008480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols); CHKERRQ(ierr); 1431a8c6a408SBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Reused submatrix wrong size"); 1432d8ced48eSBarry Smith ierr = MatZeroEntries(*B); CHKERRQ(ierr); 143308480c60SBarry Smith C = *B; 14343a40ed3dSBarry Smith } else { 143502834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 143608480c60SBarry Smith } 1437db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*) C->data; 1438db02288aSLois Curfman McInnes 143902834360SBarry Smith /* loop over rows inserting into submatrix */ 1440db02288aSLois Curfman McInnes a_new = c->a; 1441db02288aSLois Curfman McInnes j_new = c->j; 1442db02288aSLois Curfman McInnes i_new = c->i; 1443db02288aSLois Curfman McInnes i_new[0] = -shift; 144402834360SBarry Smith for (i=0; i<nrows; i++) { 1445a2744918SBarry Smith ii = starts[i]; 1446a2744918SBarry Smith lensi = lens[i]; 1447a2744918SBarry Smith for ( k=0; k<lensi; k++ ) { 1448a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 144902834360SBarry Smith } 1450a2744918SBarry Smith PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar)); 1451a2744918SBarry Smith a_new += lensi; 1452a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1453a2744918SBarry Smith c->ilen[i] = lensi; 145402834360SBarry Smith } 14550452661fSBarry Smith PetscFree(lens); 14563a40ed3dSBarry Smith } else { 145702834360SBarry Smith ierr = ISGetIndices(iscol,&icol); CHKERRQ(ierr); 14580452661fSBarry Smith smap = (int *) PetscMalloc((1+oldcols)*sizeof(int)); CHKPTRQ(smap); 145902834360SBarry Smith ssmap = smap + shift; 146099141d43SSatish Balay lens = (int *) PetscMalloc((1+nrows)*sizeof(int)); CHKPTRQ(lens); 1461cddf8d76SBarry Smith PetscMemzero(smap,oldcols*sizeof(int)); 146217ab2063SBarry Smith for ( i=0; i<ncols; i++ ) smap[icol[i]] = i+1; 146302834360SBarry Smith /* determine lens of each row */ 146402834360SBarry Smith for (i=0; i<nrows; i++) { 1465d8ced48eSBarry Smith kstart = ai[irow[i]]+shift; 146602834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 146702834360SBarry Smith lens[i] = 0; 146802834360SBarry Smith for ( k=kstart; k<kend; k++ ) { 1469d8ced48eSBarry Smith if (ssmap[aj[k]]) { 147002834360SBarry Smith lens[i]++; 147102834360SBarry Smith } 147202834360SBarry Smith } 147302834360SBarry Smith } 147417ab2063SBarry Smith /* Create and fill new matrix */ 1475a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 147699141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 1477a8c6a408SBarry Smith if (c->m != nrows || c->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong size"); 147899141d43SSatish Balay if (PetscMemcmp(c->ilen,lens, c->m *sizeof(int))) { 1479a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong no of nonzeros"); 148099141d43SSatish Balay } 148199141d43SSatish Balay PetscMemzero(c->ilen,c->m*sizeof(int)); 148208480c60SBarry Smith C = *B; 14833a40ed3dSBarry Smith } else { 148402834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 148508480c60SBarry Smith } 148699141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 148717ab2063SBarry Smith for (i=0; i<nrows; i++) { 148899141d43SSatish Balay row = irow[i]; 148999141d43SSatish Balay kstart = ai[row]+shift; 149099141d43SSatish Balay kend = kstart + a->ilen[row]; 149199141d43SSatish Balay mat_i = c->i[i]+shift; 149299141d43SSatish Balay mat_j = c->j + mat_i; 149399141d43SSatish Balay mat_a = c->a + mat_i; 149499141d43SSatish Balay mat_ilen = c->ilen + i; 149517ab2063SBarry Smith for ( k=kstart; k<kend; k++ ) { 149699141d43SSatish Balay if ((tcol=ssmap[a->j[k]])) { 149799141d43SSatish Balay *mat_j++ = tcol - (!shift); 149899141d43SSatish Balay *mat_a++ = a->a[k]; 149999141d43SSatish Balay (*mat_ilen)++; 150099141d43SSatish Balay 150117ab2063SBarry Smith } 150217ab2063SBarry Smith } 150317ab2063SBarry Smith } 150402834360SBarry Smith /* Free work space */ 150502834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol); CHKERRQ(ierr); 150699141d43SSatish Balay PetscFree(smap); PetscFree(lens); 150702834360SBarry Smith } 15086d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 15096d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 151017ab2063SBarry Smith 151117ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow); CHKERRQ(ierr); 1512416022c9SBarry Smith *B = C; 15133a40ed3dSBarry Smith PetscFunctionReturn(0); 151417ab2063SBarry Smith } 151517ab2063SBarry Smith 1516a871dcd8SBarry Smith /* 151763b91edcSBarry Smith note: This can only work for identity for row and col. It would 151863b91edcSBarry Smith be good to check this and otherwise generate an error. 1519a871dcd8SBarry Smith */ 15205615d1e5SSatish Balay #undef __FUNC__ 15215615d1e5SSatish Balay #define __FUNC__ "MatILUFactor_SeqAIJ" 15228f6be9afSLois Curfman McInnes int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,double efill,int fill) 1523a871dcd8SBarry Smith { 152463b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data; 152508480c60SBarry Smith int ierr; 152663b91edcSBarry Smith Mat outA; 152763b91edcSBarry Smith 15283a40ed3dSBarry Smith PetscFunctionBegin; 1529a8c6a408SBarry Smith if (fill != 0) SETERRQ(PETSC_ERR_SUP,0,"Only fill=0 supported"); 1530a871dcd8SBarry Smith 153163b91edcSBarry Smith outA = inA; 153263b91edcSBarry Smith inA->factor = FACTOR_LU; 153363b91edcSBarry Smith a->row = row; 153463b91edcSBarry Smith a->col = col; 153563b91edcSBarry Smith 1536f0ec6fceSSatish Balay /* Create the invert permutation so that it can be used in MatLUFactorNumeric() */ 1537f0ec6fceSSatish Balay ierr = ISInvertPermutation(col,&(a->icol)); CHKERRQ(ierr); 15381e4dd532SSatish Balay PLogObjectParent(inA,a->icol); 1539f0ec6fceSSatish Balay 154094a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 15410452661fSBarry Smith a->solve_work = (Scalar *) PetscMalloc( (a->m+1)*sizeof(Scalar));CHKPTRQ(a->solve_work); 154294a9d846SBarry Smith } 154363b91edcSBarry Smith 154408480c60SBarry Smith if (!a->diag) { 154508480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(inA); CHKERRQ(ierr); 154663b91edcSBarry Smith } 154763b91edcSBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA); CHKERRQ(ierr); 15483a40ed3dSBarry Smith PetscFunctionReturn(0); 1549a871dcd8SBarry Smith } 1550a871dcd8SBarry Smith 155174cdf0dfSBarry Smith #include "pinclude/blaslapack.h" 15525615d1e5SSatish Balay #undef __FUNC__ 15535615d1e5SSatish Balay #define __FUNC__ "MatScale_SeqAIJ" 15548f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA) 1555f0b747eeSBarry Smith { 1556f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data; 1557f0b747eeSBarry Smith int one = 1; 15583a40ed3dSBarry Smith 15593a40ed3dSBarry Smith PetscFunctionBegin; 1560f0b747eeSBarry Smith BLscal_( &a->nz, alpha, a->a, &one ); 1561f0b747eeSBarry Smith PLogFlops(a->nz); 15623a40ed3dSBarry Smith PetscFunctionReturn(0); 1563f0b747eeSBarry Smith } 1564f0b747eeSBarry Smith 15655615d1e5SSatish Balay #undef __FUNC__ 15665615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrices_SeqAIJ" 15676a6a5d1dSBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall,Mat **B) 1568cddf8d76SBarry Smith { 1569cddf8d76SBarry Smith int ierr,i; 1570cddf8d76SBarry Smith 15713a40ed3dSBarry Smith PetscFunctionBegin; 1572cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 15730452661fSBarry Smith *B = (Mat *) PetscMalloc( (n+1)*sizeof(Mat) ); CHKPTRQ(*B); 1574cddf8d76SBarry Smith } 1575cddf8d76SBarry Smith 1576cddf8d76SBarry Smith for ( i=0; i<n; i++ ) { 15776a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 1578cddf8d76SBarry Smith } 15793a40ed3dSBarry Smith PetscFunctionReturn(0); 1580cddf8d76SBarry Smith } 1581cddf8d76SBarry Smith 15825615d1e5SSatish Balay #undef __FUNC__ 15835615d1e5SSatish Balay #define __FUNC__ "MatGetBlockSize_SeqAIJ" 15848f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A, int *bs) 15855a838052SSatish Balay { 1586f830108cSBarry Smith PetscFunctionBegin; 15875a838052SSatish Balay *bs = 1; 15883a40ed3dSBarry Smith PetscFunctionReturn(0); 15895a838052SSatish Balay } 15905a838052SSatish Balay 15915615d1e5SSatish Balay #undef __FUNC__ 15925615d1e5SSatish Balay #define __FUNC__ "MatIncreaseOverlap_SeqAIJ" 15938f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A, int is_max, IS *is, int ov) 15944dcbc457SBarry Smith { 1595e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 159606763907SSatish Balay int shift, row, i,j,k,l,m,n, *idx,ierr, *nidx, isz, val; 15978a047759SSatish Balay int start, end, *ai, *aj; 1598bbd702dbSSatish Balay BT table; 1599bbd702dbSSatish Balay 16003a40ed3dSBarry Smith PetscFunctionBegin; 16018a047759SSatish Balay shift = a->indexshift; 1602e4d965acSSatish Balay m = a->m; 1603e4d965acSSatish Balay ai = a->i; 16048a047759SSatish Balay aj = a->j+shift; 16058a047759SSatish Balay 1606a8c6a408SBarry Smith if (ov < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"illegal overlap value used"); 160706763907SSatish Balay 160806763907SSatish Balay nidx = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(nidx); 1609bbd702dbSSatish Balay ierr = BTCreate(m,table); CHKERRQ(ierr); 161006763907SSatish Balay 1611e4d965acSSatish Balay for ( i=0; i<is_max; i++ ) { 1612b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1613e4d965acSSatish Balay isz = 0; 1614bbd702dbSSatish Balay BTMemzero(m,table); 1615e4d965acSSatish Balay 1616e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 16174dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx); CHKERRQ(ierr); 161877c4ece6SBarry Smith ierr = ISGetSize(is[i],&n); CHKERRQ(ierr); 1619e4d965acSSatish Balay 1620dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1621e4d965acSSatish Balay for ( j=0; j<n ; ++j){ 1622bbd702dbSSatish Balay if(!BTLookupSet(table, idx[j])) { nidx[isz++] = idx[j];} 16234dcbc457SBarry Smith } 162406763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx); CHKERRQ(ierr); 162506763907SSatish Balay ierr = ISDestroy(is[i]); CHKERRQ(ierr); 1626e4d965acSSatish Balay 162704a348a9SBarry Smith k = 0; 162804a348a9SBarry Smith for ( j=0; j<ov; j++){ /* for each overlap */ 162904a348a9SBarry Smith n = isz; 163006763907SSatish Balay for ( ; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 1631e4d965acSSatish Balay row = nidx[k]; 1632e4d965acSSatish Balay start = ai[row]; 1633e4d965acSSatish Balay end = ai[row+1]; 163404a348a9SBarry Smith for ( l = start; l<end ; l++){ 16358a047759SSatish Balay val = aj[l] + shift; 16362a8f2162SSatish Balay if (!BTLookupSet(table,val)) {nidx[isz++] = val;} 1637e4d965acSSatish Balay } 1638e4d965acSSatish Balay } 1639e4d965acSSatish Balay } 1640029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, (is+i)); CHKERRQ(ierr); 1641e4d965acSSatish Balay } 1642bbd702dbSSatish Balay BTDestroy(table); 164306763907SSatish Balay PetscFree(nidx); 16443a40ed3dSBarry Smith PetscFunctionReturn(0); 16454dcbc457SBarry Smith } 164617ab2063SBarry Smith 16470513a670SBarry Smith /* -------------------------------------------------------------- */ 16480513a670SBarry Smith #undef __FUNC__ 16490513a670SBarry Smith #define __FUNC__ "MatPermute_SeqAIJ" 16500513a670SBarry Smith int MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B) 16510513a670SBarry Smith { 16520513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 16530513a670SBarry Smith Scalar *vwork; 16540513a670SBarry Smith int i, ierr, nz, m = a->m, n = a->n, *cwork; 16550513a670SBarry Smith int *row,*col,*cnew,j,*lens; 165656cd22aeSBarry Smith IS icolp,irowp; 16570513a670SBarry Smith 16583a40ed3dSBarry Smith PetscFunctionBegin; 165956cd22aeSBarry Smith ierr = ISInvertPermutation(rowp,&irowp); CHKERRQ(ierr); 166056cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row); CHKERRQ(ierr); 166156cd22aeSBarry Smith ierr = ISInvertPermutation(colp,&icolp); CHKERRQ(ierr); 166256cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col); CHKERRQ(ierr); 16630513a670SBarry Smith 16640513a670SBarry Smith /* determine lengths of permuted rows */ 16650513a670SBarry Smith lens = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(lens); 16660513a670SBarry Smith for (i=0; i<m; i++ ) { 16670513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 16680513a670SBarry Smith } 16690513a670SBarry Smith ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr); 16700513a670SBarry Smith PetscFree(lens); 16710513a670SBarry Smith 16720513a670SBarry Smith cnew = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(cnew); 16730513a670SBarry Smith for (i=0; i<m; i++) { 16740513a670SBarry Smith ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 16750513a670SBarry Smith for (j=0; j<nz; j++ ) { cnew[j] = col[cwork[j]];} 16760513a670SBarry Smith ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES); CHKERRQ(ierr); 16770513a670SBarry Smith ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 16780513a670SBarry Smith } 16790513a670SBarry Smith PetscFree(cnew); 16800513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 16810513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 168256cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row); CHKERRQ(ierr); 168356cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col); CHKERRQ(ierr); 168456cd22aeSBarry Smith ierr = ISDestroy(irowp); CHKERRQ(ierr); 168556cd22aeSBarry Smith ierr = ISDestroy(icolp); CHKERRQ(ierr); 16863a40ed3dSBarry Smith PetscFunctionReturn(0); 16870513a670SBarry Smith } 16880513a670SBarry Smith 16895615d1e5SSatish Balay #undef __FUNC__ 16905615d1e5SSatish Balay #define __FUNC__ "MatPrintHelp_SeqAIJ" 1691682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A) 1692682d7d0cSBarry Smith { 1693682d7d0cSBarry Smith static int called = 0; 1694682d7d0cSBarry Smith MPI_Comm comm = A->comm; 1695682d7d0cSBarry Smith 16963a40ed3dSBarry Smith PetscFunctionBegin; 16973a40ed3dSBarry Smith if (called) {PetscFunctionReturn(0);} else called = 1; 169876be9ce4SBarry Smith (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n"); 169976be9ce4SBarry Smith (*PetscHelpPrintf)(comm," -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n"); 170076be9ce4SBarry Smith (*PetscHelpPrintf)(comm," -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n"); 170176be9ce4SBarry Smith (*PetscHelpPrintf)(comm," -mat_aij_no_inode: Do not use inodes\n"); 170276be9ce4SBarry Smith (*PetscHelpPrintf)(comm," -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n"); 1703682d7d0cSBarry Smith #if defined(HAVE_ESSL) 170476be9ce4SBarry Smith (*PetscHelpPrintf)(comm," -mat_aij_essl: Use IBM sparse LU factorization and solve.\n"); 1705682d7d0cSBarry Smith #endif 17063a40ed3dSBarry Smith PetscFunctionReturn(0); 1707682d7d0cSBarry Smith } 17088f6be9afSLois Curfman McInnes extern int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg); 1709a93ec695SBarry Smith extern int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring); 1710a93ec695SBarry Smith extern int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *); 1711a93ec695SBarry Smith 1712682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 171317ab2063SBarry Smith static struct _MatOps MatOps = {MatSetValues_SeqAIJ, 171417ab2063SBarry Smith MatGetRow_SeqAIJ,MatRestoreRow_SeqAIJ, 1715416022c9SBarry Smith MatMult_SeqAIJ,MatMultAdd_SeqAIJ, 1716416022c9SBarry Smith MatMultTrans_SeqAIJ,MatMultTransAdd_SeqAIJ, 171717ab2063SBarry Smith MatSolve_SeqAIJ,MatSolveAdd_SeqAIJ, 171817ab2063SBarry Smith MatSolveTrans_SeqAIJ,MatSolveTransAdd_SeqAIJ, 171917ab2063SBarry Smith MatLUFactor_SeqAIJ,0, 172017ab2063SBarry Smith MatRelax_SeqAIJ, 172117ab2063SBarry Smith MatTranspose_SeqAIJ, 17227264ac53SSatish Balay MatGetInfo_SeqAIJ,MatEqual_SeqAIJ, 1723f0b747eeSBarry Smith MatGetDiagonal_SeqAIJ,MatDiagonalScale_SeqAIJ,MatNorm_SeqAIJ, 172417ab2063SBarry Smith 0,MatAssemblyEnd_SeqAIJ, 172517ab2063SBarry Smith MatCompress_SeqAIJ, 172617ab2063SBarry Smith MatSetOption_SeqAIJ,MatZeroEntries_SeqAIJ,MatZeroRows_SeqAIJ, 172717ab2063SBarry Smith MatLUFactorSymbolic_SeqAIJ,MatLUFactorNumeric_SeqAIJ,0,0, 172817ab2063SBarry Smith MatGetSize_SeqAIJ,MatGetSize_SeqAIJ,MatGetOwnershipRange_SeqAIJ, 172917ab2063SBarry Smith MatILUFactorSymbolic_SeqAIJ,0, 173094a9d846SBarry Smith 0,0, 17313d1612f7SBarry Smith MatConvertSameType_SeqAIJ,0,0, 1732cddf8d76SBarry Smith MatILUFactor_SeqAIJ,0,0, 17337eb43aa7SLois Curfman McInnes MatGetSubMatrices_SeqAIJ,MatIncreaseOverlap_SeqAIJ, 1734682d7d0cSBarry Smith MatGetValues_SeqAIJ,0, 1735f0b747eeSBarry Smith MatPrintHelp_SeqAIJ, 17365a838052SSatish Balay MatScale_SeqAIJ,0,0, 17376945ee14SBarry Smith MatILUDTFactor_SeqAIJ, 17386945ee14SBarry Smith MatGetBlockSize_SeqAIJ, 17393b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 17403b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 17413b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 1742a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 1743a93ec695SBarry Smith MatFDColoringCreate_SeqAIJ, 17440513a670SBarry Smith MatColoringPatch_SeqAIJ, 17450513a670SBarry Smith 0, 17460513a670SBarry Smith MatPermute_SeqAIJ}; 174717ab2063SBarry Smith 174817ab2063SBarry Smith extern int MatUseSuperLU_SeqAIJ(Mat); 174917ab2063SBarry Smith extern int MatUseEssl_SeqAIJ(Mat); 175017ab2063SBarry Smith extern int MatUseDXML_SeqAIJ(Mat); 175117ab2063SBarry Smith 17525615d1e5SSatish Balay #undef __FUNC__ 1753*bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices_SeqAIJ" 1754*bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices) 1755*bef8e0ddSBarry Smith { 1756*bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 1757*bef8e0ddSBarry Smith int i,nz,n; 1758*bef8e0ddSBarry Smith 1759*bef8e0ddSBarry Smith PetscFunctionBegin; 1760*bef8e0ddSBarry Smith if (aij->indexshift) SETERRQ(1,1,"No support with 1 based indexing"); 1761*bef8e0ddSBarry Smith 1762*bef8e0ddSBarry Smith nz = aij->maxnz; 1763*bef8e0ddSBarry Smith n = aij->n; 1764*bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 1765*bef8e0ddSBarry Smith aij->j[i] = indices[i]; 1766*bef8e0ddSBarry Smith } 1767*bef8e0ddSBarry Smith aij->nz = nz; 1768*bef8e0ddSBarry Smith for ( i=0; i<n; i++ ) { 1769*bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 1770*bef8e0ddSBarry Smith } 1771*bef8e0ddSBarry Smith 1772*bef8e0ddSBarry Smith PetscFunctionReturn(0); 1773*bef8e0ddSBarry Smith } 1774*bef8e0ddSBarry Smith 1775*bef8e0ddSBarry Smith #undef __FUNC__ 1776*bef8e0ddSBarry Smith #define __FUNC__ "MatSeqAIJSetColumnIndices" 1777*bef8e0ddSBarry Smith /*@ 1778*bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 1779*bef8e0ddSBarry Smith in the matrix. 1780*bef8e0ddSBarry Smith 1781*bef8e0ddSBarry Smith Input Parameters: 1782*bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 1783*bef8e0ddSBarry Smith - indices - the column indices 1784*bef8e0ddSBarry Smith 1785*bef8e0ddSBarry Smith Notes: 1786*bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 1787*bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 1788*bef8e0ddSBarry Smith of the MatSetValues() operation. 1789*bef8e0ddSBarry Smith 1790*bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 1791*bef8e0ddSBarry Smith MatCreateSeqAIJ(). 1792*bef8e0ddSBarry Smith 1793*bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 1794*bef8e0ddSBarry Smith 1795*bef8e0ddSBarry Smith @*/ 1796*bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices) 1797*bef8e0ddSBarry Smith { 1798*bef8e0ddSBarry Smith int ierr,(*f)(Mat,int *); 1799*bef8e0ddSBarry Smith 1800*bef8e0ddSBarry Smith PetscFunctionBegin; 1801*bef8e0ddSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 1802*bef8e0ddSBarry Smith ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void **)&f); 1803*bef8e0ddSBarry Smith CHKERRQ(ierr); 1804*bef8e0ddSBarry Smith if (f) { 1805*bef8e0ddSBarry Smith ierr = (*f)(mat,indices);CHKERRQ(ierr); 1806*bef8e0ddSBarry Smith } else { 1807*bef8e0ddSBarry Smith SETERRQ(1,1,"Wrong type of matrix to set column indices"); 1808*bef8e0ddSBarry Smith } 1809*bef8e0ddSBarry Smith PetscFunctionReturn(0); 1810*bef8e0ddSBarry Smith } 1811*bef8e0ddSBarry Smith 1812*bef8e0ddSBarry Smith #undef __FUNC__ 18135615d1e5SSatish Balay #define __FUNC__ "MatCreateSeqAIJ" 181417ab2063SBarry Smith /*@C 1815682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 18160d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 18176e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 18182bd5e0b2SLois Curfman McInnes (or the array nzz). By setting these parameters accurately, performance 18192bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 182017ab2063SBarry Smith 1821db81eaa0SLois Curfman McInnes Collective on MPI_Comm 1822db81eaa0SLois Curfman McInnes 182317ab2063SBarry Smith Input Parameters: 1824db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 182517ab2063SBarry Smith . m - number of rows 182617ab2063SBarry Smith . n - number of columns 182717ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 1828db81eaa0SLois Curfman McInnes - nzz - array containing the number of nonzeros in the various rows 18292bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 183017ab2063SBarry Smith 183117ab2063SBarry Smith Output Parameter: 1832416022c9SBarry Smith . A - the matrix 183317ab2063SBarry Smith 1834b259b22eSLois Curfman McInnes Notes: 183517ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 183617ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 18370002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 183844cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 183917ab2063SBarry Smith 184017ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 1841a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 18423d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 18436da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 184417ab2063SBarry Smith 1845682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 18464fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 1847682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 18486c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 18496c7ebb05SLois Curfman McInnes 18506c7ebb05SLois Curfman McInnes Options Database Keys: 1851db81eaa0SLois Curfman McInnes + -mat_aij_no_inode - Do not use inodes 1852db81eaa0SLois Curfman McInnes . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 1853db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 1854db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 1855db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 185617ab2063SBarry Smith 1857*bef8e0ddSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices() 185817ab2063SBarry Smith @*/ 1859416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz, Mat *A) 186017ab2063SBarry Smith { 1861416022c9SBarry Smith Mat B; 1862416022c9SBarry Smith Mat_SeqAIJ *b; 18636945ee14SBarry Smith int i, len, ierr, flg,size; 18646945ee14SBarry Smith 18653a40ed3dSBarry Smith PetscFunctionBegin; 18666945ee14SBarry Smith MPI_Comm_size(comm,&size); 1867a8c6a408SBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Comm must be of size 1"); 1868d5d45c9bSBarry Smith 1869416022c9SBarry Smith *A = 0; 1870f830108cSBarry Smith PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,comm,MatDestroy,MatView); 1871416022c9SBarry Smith PLogObjectCreate(B); 18720452661fSBarry Smith B->data = (void *) (b = PetscNew(Mat_SeqAIJ)); CHKPTRQ(b); 187344cd7ae7SLois Curfman McInnes PetscMemzero(b,sizeof(Mat_SeqAIJ)); 1874f830108cSBarry Smith PetscMemcpy(B->ops,&MatOps,sizeof(struct _MatOps)); 1875e1311b90SBarry Smith B->ops->destroy = MatDestroy_SeqAIJ; 1876e1311b90SBarry Smith B->ops->view = MatView_SeqAIJ; 1877416022c9SBarry Smith B->factor = 0; 1878416022c9SBarry Smith B->lupivotthreshold = 1.0; 187990f02eecSBarry Smith B->mapping = 0; 1880e1311b90SBarry Smith ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,&flg); CHKERRQ(ierr); 18817a743949SBarry Smith b->ilu_preserve_row_sums = PETSC_FALSE; 1882e1311b90SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",(int*) &b->ilu_preserve_row_sums);CHKERRQ(ierr); 1883416022c9SBarry Smith b->row = 0; 1884416022c9SBarry Smith b->col = 0; 188582bf6240SBarry Smith b->icol = 0; 1886416022c9SBarry Smith b->indexshift = 0; 1887b810aeb4SBarry Smith b->reallocs = 0; 188869957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex", &flg); CHKERRQ(ierr); 188969957df2SSatish Balay if (flg) b->indexshift = -1; 189017ab2063SBarry Smith 189144cd7ae7SLois Curfman McInnes b->m = m; B->m = m; B->M = m; 189244cd7ae7SLois Curfman McInnes b->n = n; B->n = n; B->N = n; 18930452661fSBarry Smith b->imax = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(b->imax); 1894b4fd4287SBarry Smith if (nnz == PETSC_NULL) { 18957b8455f0SLois Curfman McInnes if (nz == PETSC_DEFAULT) nz = 10; 18967b8455f0SLois Curfman McInnes else if (nz <= 0) nz = 1; 1897416022c9SBarry Smith for ( i=0; i<m; i++ ) b->imax[i] = nz; 189817ab2063SBarry Smith nz = nz*m; 18993a40ed3dSBarry Smith } else { 190017ab2063SBarry Smith nz = 0; 1901416022c9SBarry Smith for ( i=0; i<m; i++ ) {b->imax[i] = nnz[i]; nz += nnz[i];} 190217ab2063SBarry Smith } 190317ab2063SBarry Smith 190417ab2063SBarry Smith /* allocate the matrix space */ 1905416022c9SBarry Smith len = nz*(sizeof(int) + sizeof(Scalar)) + (b->m+1)*sizeof(int); 19060452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 1907416022c9SBarry Smith b->j = (int *) (b->a + nz); 1908cddf8d76SBarry Smith PetscMemzero(b->j,nz*sizeof(int)); 1909416022c9SBarry Smith b->i = b->j + nz; 1910416022c9SBarry Smith b->singlemalloc = 1; 191117ab2063SBarry Smith 1912416022c9SBarry Smith b->i[0] = -b->indexshift; 191317ab2063SBarry Smith for (i=1; i<m+1; i++) { 1914416022c9SBarry Smith b->i[i] = b->i[i-1] + b->imax[i-1]; 191517ab2063SBarry Smith } 191617ab2063SBarry Smith 1917416022c9SBarry Smith /* b->ilen will count nonzeros in each row so far. */ 19180452661fSBarry Smith b->ilen = (int *) PetscMalloc((m+1)*sizeof(int)); 1919f09e8eb9SSatish Balay PLogObjectMemory(B,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 1920416022c9SBarry Smith for ( i=0; i<b->m; i++ ) { b->ilen[i] = 0;} 192117ab2063SBarry Smith 1922416022c9SBarry Smith b->nz = 0; 1923416022c9SBarry Smith b->maxnz = nz; 1924416022c9SBarry Smith b->sorted = 0; 1925416022c9SBarry Smith b->roworiented = 1; 1926416022c9SBarry Smith b->nonew = 0; 1927416022c9SBarry Smith b->diag = 0; 1928416022c9SBarry Smith b->solve_work = 0; 192971bd300dSLois Curfman McInnes b->spptr = 0; 1930754ec7b1SSatish Balay b->inode.node_count = 0; 1931754ec7b1SSatish Balay b->inode.size = 0; 19326c7ebb05SLois Curfman McInnes b->inode.limit = 5; 19336c7ebb05SLois Curfman McInnes b->inode.max_limit = 5; 19344e220ebcSLois Curfman McInnes B->info.nz_unneeded = (double)b->maxnz; 193517ab2063SBarry Smith 1936416022c9SBarry Smith *A = B; 19374e220ebcSLois Curfman McInnes 19384b14c69eSBarry Smith /* SuperLU is not currently supported through PETSc */ 19394b14c69eSBarry Smith #if defined(HAVE_SUPERLU) 194069957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu", &flg); CHKERRQ(ierr); 194169957df2SSatish Balay if (flg) { ierr = MatUseSuperLU_SeqAIJ(B); CHKERRQ(ierr); } 19424b14c69eSBarry Smith #endif 194369957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl", &flg); CHKERRQ(ierr); 194469957df2SSatish Balay if (flg) { ierr = MatUseEssl_SeqAIJ(B); CHKERRQ(ierr); } 194569957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml", &flg); CHKERRQ(ierr); 194669957df2SSatish Balay if (flg) { 1947a8c6a408SBarry Smith if (!b->indexshift) SETERRQ( PETSC_ERR_LIB,0,"need -mat_aij_oneindex with -mat_aij_dxml"); 1948416022c9SBarry Smith ierr = MatUseDXML_SeqAIJ(B); CHKERRQ(ierr); 194917ab2063SBarry Smith } 195069957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr); 195169957df2SSatish Balay if (flg) {ierr = MatPrintHelp(B); CHKERRQ(ierr); } 1952*bef8e0ddSBarry Smith 1953*bef8e0ddSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C", 1954*bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 1955*bef8e0ddSBarry Smith (void*)MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 19563a40ed3dSBarry Smith PetscFunctionReturn(0); 195717ab2063SBarry Smith } 195817ab2063SBarry Smith 19595615d1e5SSatish Balay #undef __FUNC__ 19605615d1e5SSatish Balay #define __FUNC__ "MatConvertSameType_SeqAIJ" 19613d1612f7SBarry Smith int MatConvertSameType_SeqAIJ(Mat A,Mat *B,int cpvalues) 196217ab2063SBarry Smith { 1963416022c9SBarry Smith Mat C; 1964416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ *) A->data; 1965*bef8e0ddSBarry Smith int i,len, m = a->m,shift = a->indexshift,ierr; 196617ab2063SBarry Smith 19673a40ed3dSBarry Smith PetscFunctionBegin; 19684043dd9cSLois Curfman McInnes *B = 0; 1969f830108cSBarry Smith PetscHeaderCreate(C,_p_Mat,struct _MatOps,MAT_COOKIE,MATSEQAIJ,A->comm,MatDestroy,MatView); 1970416022c9SBarry Smith PLogObjectCreate(C); 19710452661fSBarry Smith C->data = (void *) (c = PetscNew(Mat_SeqAIJ)); CHKPTRQ(c); 1972f830108cSBarry Smith PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps)); 1973e1311b90SBarry Smith C->ops->destroy = MatDestroy_SeqAIJ; 1974e1311b90SBarry Smith C->ops->view = MatView_SeqAIJ; 1975416022c9SBarry Smith C->factor = A->factor; 1976416022c9SBarry Smith c->row = 0; 1977416022c9SBarry Smith c->col = 0; 197882bf6240SBarry Smith c->icol = 0; 1979416022c9SBarry Smith c->indexshift = shift; 1980c456f294SBarry Smith C->assembled = PETSC_TRUE; 198117ab2063SBarry Smith 198244cd7ae7SLois Curfman McInnes c->m = C->m = a->m; 198344cd7ae7SLois Curfman McInnes c->n = C->n = a->n; 198444cd7ae7SLois Curfman McInnes C->M = a->m; 198544cd7ae7SLois Curfman McInnes C->N = a->n; 198617ab2063SBarry Smith 19870452661fSBarry Smith c->imax = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->imax); 19880452661fSBarry Smith c->ilen = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->ilen); 198917ab2063SBarry Smith for ( i=0; i<m; i++ ) { 1990416022c9SBarry Smith c->imax[i] = a->imax[i]; 1991416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 199217ab2063SBarry Smith } 199317ab2063SBarry Smith 199417ab2063SBarry Smith /* allocate the matrix space */ 1995416022c9SBarry Smith c->singlemalloc = 1; 1996416022c9SBarry Smith len = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int)); 19970452661fSBarry Smith c->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(c->a); 1998416022c9SBarry Smith c->j = (int *) (c->a + a->i[m] + shift); 1999416022c9SBarry Smith c->i = c->j + a->i[m] + shift; 2000416022c9SBarry Smith PetscMemcpy(c->i,a->i,(m+1)*sizeof(int)); 200117ab2063SBarry Smith if (m > 0) { 2002416022c9SBarry Smith PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int)); 200308480c60SBarry Smith if (cpvalues == COPY_VALUES) { 2004416022c9SBarry Smith PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar)); 200517ab2063SBarry Smith } 200608480c60SBarry Smith } 200717ab2063SBarry Smith 2008f09e8eb9SSatish Balay PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 2009416022c9SBarry Smith c->sorted = a->sorted; 2010416022c9SBarry Smith c->roworiented = a->roworiented; 2011416022c9SBarry Smith c->nonew = a->nonew; 20127a743949SBarry Smith c->ilu_preserve_row_sums = a->ilu_preserve_row_sums; 201317ab2063SBarry Smith 2014416022c9SBarry Smith if (a->diag) { 20150452661fSBarry Smith c->diag = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->diag); 2016416022c9SBarry Smith PLogObjectMemory(C,(m+1)*sizeof(int)); 201717ab2063SBarry Smith for ( i=0; i<m; i++ ) { 2018416022c9SBarry Smith c->diag[i] = a->diag[i]; 201917ab2063SBarry Smith } 20203a40ed3dSBarry Smith } else c->diag = 0; 20216c7ebb05SLois Curfman McInnes c->inode.limit = a->inode.limit; 20226c7ebb05SLois Curfman McInnes c->inode.max_limit = a->inode.max_limit; 2023754ec7b1SSatish Balay if (a->inode.size){ 2024daed632aSSatish Balay c->inode.size = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->inode.size); 2025754ec7b1SSatish Balay c->inode.node_count = a->inode.node_count; 2026daed632aSSatish Balay PetscMemcpy( c->inode.size, a->inode.size, (m+1)*sizeof(int)); 2027754ec7b1SSatish Balay } else { 2028754ec7b1SSatish Balay c->inode.size = 0; 2029754ec7b1SSatish Balay c->inode.node_count = 0; 2030754ec7b1SSatish Balay } 2031416022c9SBarry Smith c->nz = a->nz; 2032416022c9SBarry Smith c->maxnz = a->maxnz; 2033416022c9SBarry Smith c->solve_work = 0; 203476dd722bSSatish Balay c->spptr = 0; /* Dangerous -I'm throwing away a->spptr */ 2035754ec7b1SSatish Balay 2036416022c9SBarry Smith *B = C; 2037*bef8e0ddSBarry Smith ierr = PetscObjectComposeFunction((PetscObject)C,"MatSeqAIJSetColumnIndices_C", 2038*bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 2039*bef8e0ddSBarry Smith (void*)MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 20403a40ed3dSBarry Smith PetscFunctionReturn(0); 204117ab2063SBarry Smith } 204217ab2063SBarry Smith 20435615d1e5SSatish Balay #undef __FUNC__ 20445615d1e5SSatish Balay #define __FUNC__ "MatLoad_SeqAIJ" 204519bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A) 204617ab2063SBarry Smith { 2047416022c9SBarry Smith Mat_SeqAIJ *a; 2048416022c9SBarry Smith Mat B; 204917699dbbSLois Curfman McInnes int i, nz, ierr, fd, header[4],size,*rowlengths = 0,M,N,shift; 2050bcd2baecSBarry Smith MPI_Comm comm; 205117ab2063SBarry Smith 20523a40ed3dSBarry Smith PetscFunctionBegin; 205319bcc07fSBarry Smith PetscObjectGetComm((PetscObject) viewer,&comm); 205417699dbbSLois Curfman McInnes MPI_Comm_size(comm,&size); 2055a8c6a408SBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,0,"view must have one processor"); 205690ace30eSBarry Smith ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr); 20570752156aSBarry Smith ierr = PetscBinaryRead(fd,header,4,PETSC_INT); CHKERRQ(ierr); 2058a8c6a408SBarry Smith if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object in file"); 205917ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 206017ab2063SBarry Smith 2061d64ed03dSBarry Smith if (nz < 0) { 2062a8c6a408SBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix stored in special format on disk, cannot load as SeqAIJ"); 2063d64ed03dSBarry Smith } 2064d64ed03dSBarry Smith 206517ab2063SBarry Smith /* read in row lengths */ 20660452661fSBarry Smith rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths); 20670752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr); 206817ab2063SBarry Smith 206917ab2063SBarry Smith /* create our matrix */ 2070416022c9SBarry Smith ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A); CHKERRQ(ierr); 2071416022c9SBarry Smith B = *A; 2072416022c9SBarry Smith a = (Mat_SeqAIJ *) B->data; 2073416022c9SBarry Smith shift = a->indexshift; 207417ab2063SBarry Smith 207517ab2063SBarry Smith /* read in column indices and adjust for Fortran indexing*/ 20760752156aSBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT); CHKERRQ(ierr); 207717ab2063SBarry Smith if (shift) { 207817ab2063SBarry Smith for ( i=0; i<nz; i++ ) { 2079416022c9SBarry Smith a->j[i] += 1; 208017ab2063SBarry Smith } 208117ab2063SBarry Smith } 208217ab2063SBarry Smith 208317ab2063SBarry Smith /* read in nonzero values */ 20840752156aSBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR); CHKERRQ(ierr); 208517ab2063SBarry Smith 208617ab2063SBarry Smith /* set matrix "i" values */ 2087416022c9SBarry Smith a->i[0] = -shift; 208817ab2063SBarry Smith for ( i=1; i<= M; i++ ) { 2089416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 2090416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 209117ab2063SBarry Smith } 20920452661fSBarry Smith PetscFree(rowlengths); 209317ab2063SBarry Smith 20946d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 20956d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 20963a40ed3dSBarry Smith PetscFunctionReturn(0); 209717ab2063SBarry Smith } 209817ab2063SBarry Smith 20995615d1e5SSatish Balay #undef __FUNC__ 21005615d1e5SSatish Balay #define __FUNC__ "MatEqual_SeqAIJ" 21018f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg) 21027264ac53SSatish Balay { 21037264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data; 21047264ac53SSatish Balay 21053a40ed3dSBarry Smith PetscFunctionBegin; 2106a8c6a408SBarry Smith if (B->type !=MATSEQAIJ)SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type"); 21077264ac53SSatish Balay 21087264ac53SSatish Balay /* If the matrix dimensions are not equal, or no of nonzeros or shift */ 21097264ac53SSatish Balay if ((a->m != b->m ) || (a->n !=b->n) ||( a->nz != b->nz)|| 2110bcd2baecSBarry Smith (a->indexshift != b->indexshift)) { 21113a40ed3dSBarry Smith *flg = PETSC_FALSE; PetscFunctionReturn(0); 2112bcd2baecSBarry Smith } 21137264ac53SSatish Balay 21147264ac53SSatish Balay /* if the a->i are the same */ 21158108c231SLois Curfman McInnes if (PetscMemcmp(a->i,b->i,(a->m+1)*sizeof(int))) { 21163a40ed3dSBarry Smith *flg = PETSC_FALSE; PetscFunctionReturn(0); 21177264ac53SSatish Balay } 21187264ac53SSatish Balay 21197264ac53SSatish Balay /* if a->j are the same */ 2120bcd2baecSBarry Smith if (PetscMemcmp(a->j, b->j, (a->nz)*sizeof(int))) { 21213a40ed3dSBarry Smith *flg = PETSC_FALSE; PetscFunctionReturn(0); 2122bcd2baecSBarry Smith } 2123bcd2baecSBarry Smith 2124bcd2baecSBarry Smith /* if a->a are the same */ 212519bcc07fSBarry Smith if (PetscMemcmp(a->a, b->a, (a->nz)*sizeof(Scalar))) { 21263a40ed3dSBarry Smith *flg = PETSC_FALSE; PetscFunctionReturn(0); 21277264ac53SSatish Balay } 212877c4ece6SBarry Smith *flg = PETSC_TRUE; 21293a40ed3dSBarry Smith PetscFunctionReturn(0); 21307264ac53SSatish Balay 21317264ac53SSatish Balay } 2132