117ab2063SBarry Smith #ifndef lint 2*766eeae4SLois Curfman McInnes static char vcid[] = "$Id: aij.c,v 1.221 1997/06/08 02:35:43 curfman Exp curfman $"; 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" 15e4d965acSSatish Balay #include "petsc.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 28e3372554SBarry Smith SETERRQ(ierr,0,"Not implemented"); 29a2ce50c7SBarry Smith } 30a2ce50c7SBarry Smith 31bcd2baecSBarry Smith extern int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**); 3217ab2063SBarry Smith 335615d1e5SSatish Balay #undef __FUNC__ 345615d1e5SSatish Balay #define __FUNC__ "MatGetRowIJ_SeqAIJ" 358f6be9afSLois Curfman McInnes int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja, 366945ee14SBarry Smith PetscTruth *done) 3717ab2063SBarry Smith { 38416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 396945ee14SBarry Smith int ierr,i,ishift; 4017ab2063SBarry Smith 4131625ec5SSatish Balay *m = A->m; 426945ee14SBarry Smith if (!ia) return 0; 436945ee14SBarry Smith ishift = a->indexshift; 446945ee14SBarry Smith if (symmetric) { 4531625ec5SSatish Balay ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr); 466945ee14SBarry Smith } else if (oshift == 0 && ishift == -1) { 4731625ec5SSatish Balay int nz = a->i[a->m]; 483b2fbd54SBarry Smith /* malloc space and subtract 1 from i and j indices */ 4931625ec5SSatish Balay *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia); 503b2fbd54SBarry Smith *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja); 513b2fbd54SBarry Smith for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] - 1; 5231625ec5SSatish Balay for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] - 1; 536945ee14SBarry Smith } else if (oshift == 1 && ishift == 0) { 5431625ec5SSatish Balay int nz = a->i[a->m] + 1; 553b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 5631625ec5SSatish Balay *ia = (int *) PetscMalloc( (a->m+1)*sizeof(int) ); CHKPTRQ(*ia); 573b2fbd54SBarry Smith *ja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(*ja); 583b2fbd54SBarry Smith for ( i=0; i<nz; i++ ) (*ja)[i] = a->j[i] + 1; 5931625ec5SSatish Balay for ( i=0; i<a->m+1; i++ ) (*ia)[i] = a->i[i] + 1; 606945ee14SBarry Smith } else { 616945ee14SBarry Smith *ia = a->i; *ja = a->j; 62a2ce50c7SBarry Smith } 63a2ce50c7SBarry Smith 64a2744918SBarry Smith return 0; 65a2744918SBarry Smith } 66a2744918SBarry Smith 675615d1e5SSatish Balay #undef __FUNC__ 685615d1e5SSatish Balay #define __FUNC__ "MatRestoreRowIJ_SeqAIJ" 698f6be9afSLois Curfman McInnes int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja, 706945ee14SBarry Smith PetscTruth *done) 716945ee14SBarry Smith { 726945ee14SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 733b2fbd54SBarry Smith int ishift = a->indexshift; 746945ee14SBarry Smith 756945ee14SBarry Smith if (!ia) return 0; 763b2fbd54SBarry Smith if (symmetric || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) { 776945ee14SBarry Smith PetscFree(*ia); 786945ee14SBarry Smith PetscFree(*ja); 79bcd2baecSBarry Smith } 8017ab2063SBarry Smith return 0; 8117ab2063SBarry Smith } 8217ab2063SBarry Smith 835615d1e5SSatish Balay #undef __FUNC__ 845615d1e5SSatish Balay #define __FUNC__ "MatGetColumnIJ_SeqAIJ" 8543a90d84SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja, 863b2fbd54SBarry Smith PetscTruth *done) 873b2fbd54SBarry Smith { 883b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 89a93ec695SBarry Smith int ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m; 90a93ec695SBarry Smith int nz = a->i[m]+ishift,row,*jj,mr,col; 913b2fbd54SBarry Smith 923b2fbd54SBarry Smith *nn = A->n; 933b2fbd54SBarry Smith if (!ia) return 0; 943b2fbd54SBarry Smith if (symmetric) { 95179192dfSSatish Balay ierr = MatToSymmetricIJ_SeqAIJ(a->m,a->i,a->j,ishift,oshift,ia,ja); CHKERRQ(ierr); 963b2fbd54SBarry Smith } else { 9761d2ded1SBarry Smith collengths = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(collengths); 983b2fbd54SBarry Smith PetscMemzero(collengths,n*sizeof(int)); 993b2fbd54SBarry Smith cia = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(cia); 100a93ec695SBarry Smith cja = (int *) PetscMalloc( (nz+1)*sizeof(int) ); CHKPTRQ(cja); 1013b2fbd54SBarry Smith jj = a->j; 1023b2fbd54SBarry Smith for ( i=0; i<nz; i++ ) { 1033b2fbd54SBarry Smith collengths[jj[i] + ishift]++; 1043b2fbd54SBarry Smith } 1053b2fbd54SBarry Smith cia[0] = oshift; 1063b2fbd54SBarry Smith for ( i=0; i<n; i++) { 1073b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 1083b2fbd54SBarry Smith } 1093b2fbd54SBarry Smith PetscMemzero(collengths,n*sizeof(int)); 1103b2fbd54SBarry Smith jj = a->j; 111a93ec695SBarry Smith for ( row=0; row<m; row++ ) { 112a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 113a93ec695SBarry Smith for ( i=0; i<mr; i++ ) { 1143b2fbd54SBarry Smith col = *jj++ + ishift; 1153b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 1163b2fbd54SBarry Smith } 1173b2fbd54SBarry Smith } 1183b2fbd54SBarry Smith PetscFree(collengths); 1193b2fbd54SBarry Smith *ia = cia; *ja = cja; 1203b2fbd54SBarry Smith } 1213b2fbd54SBarry Smith 1223b2fbd54SBarry Smith return 0; 1233b2fbd54SBarry Smith } 1243b2fbd54SBarry Smith 1255615d1e5SSatish Balay #undef __FUNC__ 1265615d1e5SSatish Balay #define __FUNC__ "MatRestoreColumnIJ_SeqAIJ" 12743a90d84SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia, 1283b2fbd54SBarry Smith int **ja,PetscTruth *done) 1293b2fbd54SBarry Smith { 1303b2fbd54SBarry Smith if (!ia) return 0; 1313b2fbd54SBarry Smith 1323b2fbd54SBarry Smith PetscFree(*ia); 1333b2fbd54SBarry Smith PetscFree(*ja); 1343b2fbd54SBarry Smith 1353b2fbd54SBarry Smith return 0; 1363b2fbd54SBarry Smith } 1373b2fbd54SBarry Smith 138227d817aSBarry Smith #define CHUNKSIZE 15 13917ab2063SBarry Smith 1405615d1e5SSatish Balay #undef __FUNC__ 1415615d1e5SSatish Balay #define __FUNC__ "MatSetValues_SeqAIJ" 14261d2ded1SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v,InsertMode is) 14317ab2063SBarry Smith { 144416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 145416022c9SBarry Smith int *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax, N, sorted = a->sorted; 1464b0e389bSBarry Smith int *imax = a->imax, *ai = a->i, *ailen = a->ilen,roworiented = a->roworiented; 147d5d45c9bSBarry Smith int *aj = a->j, nonew = a->nonew,shift = a->indexshift; 148416022c9SBarry Smith Scalar *ap,value, *aa = a->a; 14917ab2063SBarry Smith 15017ab2063SBarry Smith for ( k=0; k<m; k++ ) { /* loop over added rows */ 151416022c9SBarry Smith row = im[k]; 1523b2fbd54SBarry Smith #if defined(PETSC_BOPT_g) 153e3372554SBarry Smith if (row < 0) SETERRQ(1,0,"Negative row"); 154e3372554SBarry Smith if (row >= a->m) SETERRQ(1,0,"Row too large"); 1553b2fbd54SBarry Smith #endif 15617ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 15717ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 158416022c9SBarry Smith low = 0; 15917ab2063SBarry Smith for ( l=0; l<n; l++ ) { /* loop over added columns */ 1603b2fbd54SBarry Smith #if defined(PETSC_BOPT_g) 161e3372554SBarry Smith if (in[l] < 0) SETERRQ(1,0,"Negative column"); 162e3372554SBarry Smith if (in[l] >= a->n) SETERRQ(1,0,"Column too large"); 1633b2fbd54SBarry Smith #endif 1644b0e389bSBarry Smith col = in[l] - shift; 1654b0e389bSBarry Smith if (roworiented) { 1664b0e389bSBarry Smith value = *v++; 1674b0e389bSBarry Smith } 1684b0e389bSBarry Smith else { 1694b0e389bSBarry Smith value = v[k + l*m]; 1704b0e389bSBarry Smith } 171416022c9SBarry Smith if (!sorted) low = 0; high = nrow; 172416022c9SBarry Smith while (high-low > 5) { 173416022c9SBarry Smith t = (low+high)/2; 174416022c9SBarry Smith if (rp[t] > col) high = t; 175416022c9SBarry Smith else low = t; 17617ab2063SBarry Smith } 177416022c9SBarry Smith for ( i=low; i<high; i++ ) { 17817ab2063SBarry Smith if (rp[i] > col) break; 17917ab2063SBarry Smith if (rp[i] == col) { 180416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 18117ab2063SBarry Smith else ap[i] = value; 18217ab2063SBarry Smith goto noinsert; 18317ab2063SBarry Smith } 18417ab2063SBarry Smith } 185c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 18611ebbc71SLois Curfman McInnes else if (nonew == -1) SETERRQ(1,0,"Inserting a new nonzero in the matrix"); 18717ab2063SBarry Smith if (nrow >= rmax) { 18817ab2063SBarry Smith /* there is no extra room in row, therefore enlarge */ 189416022c9SBarry Smith int new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; 19017ab2063SBarry Smith Scalar *new_a; 19117ab2063SBarry Smith 19211ebbc71SLois Curfman McInnes if (nonew == -2) SETERRQ(1,0,"Inserting a new nonzero in the matrix"); 19396854ed6SLois Curfman McInnes 19417ab2063SBarry Smith /* malloc new storage space */ 195416022c9SBarry Smith len = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); 1960452661fSBarry Smith new_a = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); 19717ab2063SBarry Smith new_j = (int *) (new_a + new_nz); 19817ab2063SBarry Smith new_i = new_j + new_nz; 19917ab2063SBarry Smith 20017ab2063SBarry Smith /* copy over old data into new slots */ 20117ab2063SBarry Smith for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} 202416022c9SBarry Smith for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} 203416022c9SBarry Smith PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int)); 204416022c9SBarry Smith len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); 205416022c9SBarry Smith PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, 20617ab2063SBarry Smith len*sizeof(int)); 207416022c9SBarry Smith PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar)); 208416022c9SBarry Smith PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, 20917ab2063SBarry Smith len*sizeof(Scalar)); 21017ab2063SBarry Smith /* free up old matrix storage */ 2110452661fSBarry Smith PetscFree(a->a); 2120452661fSBarry Smith if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);} 213416022c9SBarry Smith aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; 214416022c9SBarry Smith a->singlemalloc = 1; 21517ab2063SBarry Smith 21617ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 217416022c9SBarry Smith rmax = imax[row] = imax[row] + CHUNKSIZE; 218416022c9SBarry Smith PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); 219416022c9SBarry Smith a->maxnz += CHUNKSIZE; 220b810aeb4SBarry Smith a->reallocs++; 22117ab2063SBarry Smith } 222416022c9SBarry Smith N = nrow++ - 1; a->nz++; 223416022c9SBarry Smith /* shift up all the later entries in this row */ 224416022c9SBarry Smith for ( ii=N; ii>=i; ii-- ) { 22517ab2063SBarry Smith rp[ii+1] = rp[ii]; 22617ab2063SBarry Smith ap[ii+1] = ap[ii]; 22717ab2063SBarry Smith } 22817ab2063SBarry Smith rp[i] = col; 22917ab2063SBarry Smith ap[i] = value; 23017ab2063SBarry Smith noinsert:; 231416022c9SBarry Smith low = i + 1; 23217ab2063SBarry Smith } 23317ab2063SBarry Smith ailen[row] = nrow; 23417ab2063SBarry Smith } 23517ab2063SBarry Smith return 0; 23617ab2063SBarry Smith } 23717ab2063SBarry Smith 2385615d1e5SSatish Balay #undef __FUNC__ 2395615d1e5SSatish Balay #define __FUNC__ "MatGetValues_SeqAIJ" 2408f6be9afSLois Curfman McInnes int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,Scalar *v) 2417eb43aa7SLois Curfman McInnes { 2427eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 243b49de8d1SLois Curfman McInnes int *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j; 2447eb43aa7SLois Curfman McInnes int *ai = a->i, *ailen = a->ilen, shift = a->indexshift; 2457eb43aa7SLois Curfman McInnes Scalar *ap, *aa = a->a, zero = 0.0; 2467eb43aa7SLois Curfman McInnes 2477eb43aa7SLois Curfman McInnes for ( k=0; k<m; k++ ) { /* loop over rows */ 2487eb43aa7SLois Curfman McInnes row = im[k]; 249e3372554SBarry Smith if (row < 0) SETERRQ(1,0,"Negative row"); 250e3372554SBarry Smith if (row >= a->m) SETERRQ(1,0,"Row too large"); 2517eb43aa7SLois Curfman McInnes rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 2527eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2537eb43aa7SLois Curfman McInnes for ( l=0; l<n; l++ ) { /* loop over columns */ 254e3372554SBarry Smith if (in[l] < 0) SETERRQ(1,0,"Negative column"); 255e3372554SBarry Smith if (in[l] >= a->n) SETERRQ(1,0,"Column too large"); 2567eb43aa7SLois Curfman McInnes col = in[l] - shift; 2577eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2587eb43aa7SLois Curfman McInnes while (high-low > 5) { 2597eb43aa7SLois Curfman McInnes t = (low+high)/2; 2607eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2617eb43aa7SLois Curfman McInnes else low = t; 2627eb43aa7SLois Curfman McInnes } 2637eb43aa7SLois Curfman McInnes for ( i=low; i<high; i++ ) { 2647eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2657eb43aa7SLois Curfman McInnes if (rp[i] == col) { 266b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2677eb43aa7SLois Curfman McInnes goto finished; 2687eb43aa7SLois Curfman McInnes } 2697eb43aa7SLois Curfman McInnes } 270b49de8d1SLois Curfman McInnes *v++ = zero; 2717eb43aa7SLois Curfman McInnes finished:; 2727eb43aa7SLois Curfman McInnes } 2737eb43aa7SLois Curfman McInnes } 2747eb43aa7SLois Curfman McInnes return 0; 2757eb43aa7SLois Curfman McInnes } 2767eb43aa7SLois Curfman McInnes 27717ab2063SBarry Smith 2785615d1e5SSatish Balay #undef __FUNC__ 2795615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Binary" 2808f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_Binary(Mat A,Viewer viewer) 28117ab2063SBarry Smith { 282416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 283416022c9SBarry Smith int i, fd, *col_lens, ierr; 28417ab2063SBarry Smith 28590ace30eSBarry Smith ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr); 2860452661fSBarry Smith col_lens = (int *) PetscMalloc( (4+a->m)*sizeof(int) ); CHKPTRQ(col_lens); 287416022c9SBarry Smith col_lens[0] = MAT_COOKIE; 288416022c9SBarry Smith col_lens[1] = a->m; 289416022c9SBarry Smith col_lens[2] = a->n; 290416022c9SBarry Smith col_lens[3] = a->nz; 291416022c9SBarry Smith 292416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 293416022c9SBarry Smith for ( i=0; i<a->m; i++ ) { 294416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 29517ab2063SBarry Smith } 29677c4ece6SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+a->m,BINARY_INT,1); CHKERRQ(ierr); 2970452661fSBarry Smith PetscFree(col_lens); 298416022c9SBarry Smith 299416022c9SBarry Smith /* store column indices (zero start index) */ 300416022c9SBarry Smith if (a->indexshift) { 301416022c9SBarry Smith for ( i=0; i<a->nz; i++ ) a->j[i]--; 30217ab2063SBarry Smith } 30377c4ece6SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,BINARY_INT,0); CHKERRQ(ierr); 304416022c9SBarry Smith if (a->indexshift) { 305416022c9SBarry Smith for ( i=0; i<a->nz; i++ ) a->j[i]++; 30617ab2063SBarry Smith } 307416022c9SBarry Smith 308416022c9SBarry Smith /* store nonzero values */ 30977c4ece6SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,BINARY_SCALAR,0); CHKERRQ(ierr); 31017ab2063SBarry Smith return 0; 31117ab2063SBarry Smith } 312416022c9SBarry Smith 3135615d1e5SSatish Balay #undef __FUNC__ 3145615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_ASCII" 3158f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_ASCII(Mat A,Viewer viewer) 316416022c9SBarry Smith { 317416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 318496e697eSBarry Smith int ierr, i,j, m = a->m, shift = a->indexshift, format, flg1,flg2; 31917ab2063SBarry Smith FILE *fd; 32017ab2063SBarry Smith char *outputname; 32117ab2063SBarry Smith 32290ace30eSBarry Smith ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 323416022c9SBarry Smith ierr = ViewerFileGetOutputname_Private(viewer,&outputname); CHKERRQ(ierr); 32490ace30eSBarry Smith ierr = ViewerGetFormat(viewer,&format); 325a93ec695SBarry Smith if (format == VIEWER_FORMAT_ASCII_INFO) { 32695e01e2fSLois Curfman McInnes return 0; 32795e01e2fSLois Curfman McInnes } 328a93ec695SBarry Smith else if (format == VIEWER_FORMAT_ASCII_INFO_LONG) { 329496e697eSBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg1); CHKERRQ(ierr); 330496e697eSBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_no_unroll",&flg2); CHKERRQ(ierr); 331496e697eSBarry Smith if (flg1 || flg2) fprintf(fd," not using I-node routines\n"); 33295e01e2fSLois Curfman McInnes else fprintf(fd," using I-node routines: found %d nodes, limit used is %d\n", 33395e01e2fSLois Curfman McInnes a->inode.node_count,a->inode.limit); 33417ab2063SBarry Smith } 335a93ec695SBarry Smith else if (format == VIEWER_FORMAT_ASCII_MATLAB) { 336416022c9SBarry Smith fprintf(fd,"%% Size = %d %d \n",m,a->n); 3374e220ebcSLois Curfman McInnes fprintf(fd,"%% Nonzeros = %d \n",a->nz); 3384e220ebcSLois Curfman McInnes fprintf(fd,"zzz = zeros(%d,3);\n",a->nz); 33917ab2063SBarry Smith fprintf(fd,"zzz = [\n"); 34017ab2063SBarry Smith 34117ab2063SBarry Smith for (i=0; i<m; i++) { 342416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 34317ab2063SBarry Smith #if defined(PETSC_COMPLEX) 3446945ee14SBarry Smith fprintf(fd,"%d %d %18.16e + %18.16e i \n",i+1,a->j[j]+!shift,real(a->a[j]), 345416022c9SBarry Smith imag(a->a[j])); 34617ab2063SBarry Smith #else 3477a743949SBarry Smith fprintf(fd,"%d %d %18.16e\n", i+1, a->j[j]+!shift, a->a[j]); 34817ab2063SBarry Smith #endif 34917ab2063SBarry Smith } 35017ab2063SBarry Smith } 35117ab2063SBarry Smith fprintf(fd,"];\n %s = spconvert(zzz);\n",outputname); 35217ab2063SBarry Smith } 353a93ec695SBarry Smith else if (format == VIEWER_FORMAT_ASCII_COMMON) { 35444cd7ae7SLois Curfman McInnes for ( i=0; i<m; i++ ) { 35544cd7ae7SLois Curfman McInnes fprintf(fd,"row %d:",i); 35644cd7ae7SLois Curfman McInnes for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 35744cd7ae7SLois Curfman McInnes #if defined(PETSC_COMPLEX) 358*766eeae4SLois Curfman McInnes if (imag(a->a[j]) > 0.0 && real(a->a[j]) != 0.0) 35944cd7ae7SLois Curfman McInnes fprintf(fd," %d %g + %g i",a->j[j]+shift,real(a->a[j]),imag(a->a[j])); 360*766eeae4SLois Curfman McInnes else if (imag(a->a[j]) < 0.0 && real(a->a[j]) != 0.0) 361*766eeae4SLois Curfman McInnes fprintf(fd," %d %g - %g i",a->j[j]+shift,real(a->a[j]),-imag(a->a[j])); 36244cd7ae7SLois Curfman McInnes else if (real(a->a[j]) != 0.0) 36344cd7ae7SLois Curfman McInnes fprintf(fd," %d %g ",a->j[j]+shift,real(a->a[j])); 36444cd7ae7SLois Curfman McInnes #else 36544cd7ae7SLois Curfman McInnes if (a->a[j] != 0.0) fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]); 36644cd7ae7SLois Curfman McInnes #endif 36744cd7ae7SLois Curfman McInnes } 36844cd7ae7SLois Curfman McInnes fprintf(fd,"\n"); 36944cd7ae7SLois Curfman McInnes } 37044cd7ae7SLois Curfman McInnes } 37117ab2063SBarry Smith else { 37217ab2063SBarry Smith for ( i=0; i<m; i++ ) { 37317ab2063SBarry Smith fprintf(fd,"row %d:",i); 374416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 37517ab2063SBarry Smith #if defined(PETSC_COMPLEX) 376*766eeae4SLois Curfman McInnes if (imag(a->a[j]) > 0.0) { 377416022c9SBarry Smith fprintf(fd," %d %g + %g i",a->j[j]+shift,real(a->a[j]),imag(a->a[j])); 378*766eeae4SLois Curfman McInnes } else if (imag(a->a[j]) < 0.0) { 379*766eeae4SLois Curfman McInnes fprintf(fd," %d %g - %g i",a->j[j]+shift,real(a->a[j]),-imag(a->a[j])); 38017ab2063SBarry Smith } 38117ab2063SBarry Smith else { 382416022c9SBarry Smith fprintf(fd," %d %g ",a->j[j]+shift,real(a->a[j])); 38317ab2063SBarry Smith } 38417ab2063SBarry Smith #else 385416022c9SBarry Smith fprintf(fd," %d %g ",a->j[j]+shift,a->a[j]); 38617ab2063SBarry Smith #endif 38717ab2063SBarry Smith } 38817ab2063SBarry Smith fprintf(fd,"\n"); 38917ab2063SBarry Smith } 39017ab2063SBarry Smith } 39117ab2063SBarry Smith fflush(fd); 392416022c9SBarry Smith return 0; 393416022c9SBarry Smith } 394416022c9SBarry Smith 3955615d1e5SSatish Balay #undef __FUNC__ 3965615d1e5SSatish Balay #define __FUNC__ "MatView_SeqAIJ_Draw" 3978f6be9afSLois Curfman McInnes extern int MatView_SeqAIJ_Draw(Mat A,Viewer viewer) 398416022c9SBarry Smith { 399416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 400cddf8d76SBarry Smith int ierr, i,j, m = a->m, shift = a->indexshift,pause,color; 4010513a670SBarry Smith int format; 40294a9d846SBarry Smith double xl,yl,xr,yr,w,h,xc,yc,scale = 1.0,x_l,x_r,y_l,y_r,maxv = 0.0; 403bcd2baecSBarry Smith Draw draw; 404cddf8d76SBarry Smith DrawButton button; 40519bcc07fSBarry Smith PetscTruth isnull; 406cddf8d76SBarry Smith 4070513a670SBarry Smith ierr = ViewerDrawGetDraw(viewer,&draw); CHKERRQ(ierr); 4080513a670SBarry Smith ierr = DrawClear(draw); CHKERRQ(ierr); 4090513a670SBarry Smith ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr); 41019bcc07fSBarry Smith ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) return 0; 41119bcc07fSBarry Smith 412416022c9SBarry Smith xr = a->n; yr = a->m; h = yr/10.0; w = xr/10.0; 413416022c9SBarry Smith xr += w; yr += h; xl = -w; yl = -h; 414416022c9SBarry Smith ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr); 415416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 4160513a670SBarry Smith 4170513a670SBarry Smith if (format != VIEWER_FORMAT_DRAW_CONTOUR) { 4180513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 419cddf8d76SBarry Smith color = DRAW_BLUE; 420416022c9SBarry Smith for ( i=0; i<m; i++ ) { 421cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 422416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 423cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 424cddf8d76SBarry Smith #if defined(PETSC_COMPLEX) 425cddf8d76SBarry Smith if (real(a->a[j]) >= 0.) continue; 426cddf8d76SBarry Smith #else 427cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 428cddf8d76SBarry Smith #endif 429cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 430cddf8d76SBarry Smith } 431cddf8d76SBarry Smith } 432cddf8d76SBarry Smith color = DRAW_CYAN; 433cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 434cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 435cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 436cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 437cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 438cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 439cddf8d76SBarry Smith } 440cddf8d76SBarry Smith } 441cddf8d76SBarry Smith color = DRAW_RED; 442cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 443cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 444cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 445cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 446cddf8d76SBarry Smith #if defined(PETSC_COMPLEX) 447cddf8d76SBarry Smith if (real(a->a[j]) <= 0.) continue; 448cddf8d76SBarry Smith #else 449cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 450cddf8d76SBarry Smith #endif 451cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 452416022c9SBarry Smith } 453416022c9SBarry Smith } 4540513a670SBarry Smith } else { 4550513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 4560513a670SBarry Smith /* first determine max of all nonzero values */ 4570513a670SBarry Smith int nz = a->nz,count; 4580513a670SBarry Smith Draw popup; 4590513a670SBarry Smith 4600513a670SBarry Smith for ( i=0; i<nz; i++ ) { 4610513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 4620513a670SBarry Smith } 4630513a670SBarry Smith ierr = DrawCreatePopUp(draw,&popup); CHKERRQ(ierr); 4640513a670SBarry Smith ierr = DrawScalePopup(popup,0.0,maxv); CHKERRQ(ierr); 4650513a670SBarry Smith count = 0; 4660513a670SBarry Smith for ( i=0; i<m; i++ ) { 4670513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 4680513a670SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 4690513a670SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 4700513a670SBarry Smith color = 32 + (int) ((200.0 - 32.0)*PetscAbsScalar(a->a[count])/maxv); 4710513a670SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 4720513a670SBarry Smith count++; 4730513a670SBarry Smith } 4740513a670SBarry Smith } 4750513a670SBarry Smith } 476416022c9SBarry Smith DrawFlush(draw); 477cddf8d76SBarry Smith DrawGetPause(draw,&pause); 478cddf8d76SBarry Smith if (pause >= 0) { PetscSleep(pause); return 0;} 479cddf8d76SBarry Smith 480cddf8d76SBarry Smith /* allow the matrix to zoom or shrink */ 4816945ee14SBarry Smith ierr = DrawCheckResizedWindow(draw); 482cddf8d76SBarry Smith ierr = DrawGetMouseButton(draw,&button,&xc,&yc,0,0); 483cddf8d76SBarry Smith while (button != BUTTON_RIGHT) { 484cddf8d76SBarry Smith DrawClear(draw); 485cddf8d76SBarry Smith if (button == BUTTON_LEFT) scale = .5; 486cddf8d76SBarry Smith else if (button == BUTTON_CENTER) scale = 2.; 487cddf8d76SBarry Smith xl = scale*(xl + w - xc) + xc - w*scale; 488cddf8d76SBarry Smith xr = scale*(xr - w - xc) + xc + w*scale; 489cddf8d76SBarry Smith yl = scale*(yl + h - yc) + yc - h*scale; 490cddf8d76SBarry Smith yr = scale*(yr - h - yc) + yc + h*scale; 491cddf8d76SBarry Smith w *= scale; h *= scale; 492cddf8d76SBarry Smith ierr = DrawSetCoordinates(draw,xl,yl,xr,yr); CHKERRQ(ierr); 4930513a670SBarry Smith if (format != VIEWER_FORMAT_DRAW_CONTOUR) { 4940513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 495cddf8d76SBarry Smith color = DRAW_BLUE; 496cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 497cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 498cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 499cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 500cddf8d76SBarry Smith #if defined(PETSC_COMPLEX) 501cddf8d76SBarry Smith if (real(a->a[j]) >= 0.) continue; 502cddf8d76SBarry Smith #else 503cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 504cddf8d76SBarry Smith #endif 505cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 506cddf8d76SBarry Smith } 507cddf8d76SBarry Smith } 508cddf8d76SBarry Smith color = DRAW_CYAN; 509cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 510cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 511cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 512cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 513cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 514cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 515cddf8d76SBarry Smith } 516cddf8d76SBarry Smith } 517cddf8d76SBarry Smith color = DRAW_RED; 518cddf8d76SBarry Smith for ( i=0; i<m; i++ ) { 519cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 520cddf8d76SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 521cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 522cddf8d76SBarry Smith #if defined(PETSC_COMPLEX) 523cddf8d76SBarry Smith if (real(a->a[j]) <= 0.) continue; 524cddf8d76SBarry Smith #else 525cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 526cddf8d76SBarry Smith #endif 527cddf8d76SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 528cddf8d76SBarry Smith } 529cddf8d76SBarry Smith } 5300513a670SBarry Smith } else { 5310513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 5320513a670SBarry Smith int count = 0; 5330513a670SBarry Smith for ( i=0; i<m; i++ ) { 5340513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 5350513a670SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 5360513a670SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 5370513a670SBarry Smith color = 32 + (int) ((200.0 - 32.0)*PetscAbsScalar(a->a[count])/maxv); 5380513a670SBarry Smith DrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color); 5390513a670SBarry Smith count++; 5400513a670SBarry Smith } 5410513a670SBarry Smith } 5420513a670SBarry Smith } 5430513a670SBarry Smith 5446945ee14SBarry Smith ierr = DrawCheckResizedWindow(draw); 545cddf8d76SBarry Smith ierr = DrawGetMouseButton(draw,&button,&xc,&yc,0,0); 546cddf8d76SBarry Smith } 547416022c9SBarry Smith return 0; 548416022c9SBarry Smith } 549416022c9SBarry Smith 5505615d1e5SSatish Balay #undef __FUNC__ 551c22c1629SBarry Smith #define __FUNC__ "MatView_SeqAIJ" /* ADIC Ignore */ 5528f6be9afSLois Curfman McInnes int MatView_SeqAIJ(PetscObject obj,Viewer viewer) 553416022c9SBarry Smith { 554416022c9SBarry Smith Mat A = (Mat) obj; 555416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 556bcd2baecSBarry Smith ViewerType vtype; 557bcd2baecSBarry Smith int ierr; 558416022c9SBarry Smith 559bcd2baecSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 560bcd2baecSBarry Smith if (vtype == MATLAB_VIEWER) { 561416022c9SBarry Smith return ViewerMatlabPutSparse_Private(viewer,a->m,a->n,a->nz,a->a,a->i,a->j); 562416022c9SBarry Smith } 563bcd2baecSBarry Smith else if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER){ 564416022c9SBarry Smith return MatView_SeqAIJ_ASCII(A,viewer); 565416022c9SBarry Smith } 566bcd2baecSBarry Smith else if (vtype == BINARY_FILE_VIEWER) { 567416022c9SBarry Smith return MatView_SeqAIJ_Binary(A,viewer); 568416022c9SBarry Smith } 569bcd2baecSBarry Smith else if (vtype == DRAW_VIEWER) { 570bcd2baecSBarry Smith return MatView_SeqAIJ_Draw(A,viewer); 57117ab2063SBarry Smith } 57217ab2063SBarry Smith return 0; 57317ab2063SBarry Smith } 57419bcc07fSBarry Smith 575c456f294SBarry Smith extern int Mat_AIJ_CheckInode(Mat); 5765615d1e5SSatish Balay #undef __FUNC__ 5775615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_SeqAIJ" 5788f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 57917ab2063SBarry Smith { 580416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 58141c01911SSatish Balay int fshift = 0,i,j,*ai = a->i, *aj = a->j, *imax = a->imax,ierr; 58294a9d846SBarry Smith int m = a->m, *ip, N, *ailen = a->ilen,shift = a->indexshift,rmax; 583416022c9SBarry Smith Scalar *aa = a->a, *ap; 58417ab2063SBarry Smith 5856d4a8577SBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) return 0; 58617ab2063SBarry Smith 58794a9d846SBarry Smith rmax = ailen[0]; /* determine row with most nonzeros */ 58817ab2063SBarry Smith for ( i=1; i<m; i++ ) { 589416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 59017ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 59194a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 59217ab2063SBarry Smith if (fshift) { 593416022c9SBarry Smith ip = aj + ai[i] + shift; ap = aa + ai[i] + shift; 59417ab2063SBarry Smith N = ailen[i]; 59517ab2063SBarry Smith for ( j=0; j<N; j++ ) { 59617ab2063SBarry Smith ip[j-fshift] = ip[j]; 59717ab2063SBarry Smith ap[j-fshift] = ap[j]; 59817ab2063SBarry Smith } 59917ab2063SBarry Smith } 60017ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 60117ab2063SBarry Smith } 60217ab2063SBarry Smith if (m) { 60317ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 60417ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 60517ab2063SBarry Smith } 60617ab2063SBarry Smith /* reset ilen and imax for each row */ 60717ab2063SBarry Smith for ( i=0; i<m; i++ ) { 60817ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 60917ab2063SBarry Smith } 610416022c9SBarry Smith a->nz = ai[m] + shift; 61117ab2063SBarry Smith 61217ab2063SBarry Smith /* diagonals may have moved, so kill the diagonal pointers */ 613416022c9SBarry Smith if (fshift && a->diag) { 6140452661fSBarry Smith PetscFree(a->diag); 615416022c9SBarry Smith PLogObjectMemory(A,-(m+1)*sizeof(int)); 616416022c9SBarry Smith a->diag = 0; 61717ab2063SBarry Smith } 6184e220ebcSLois Curfman McInnes PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded, %d used\n", 6194e220ebcSLois Curfman McInnes m,a->n,fshift,a->nz); 6204e220ebcSLois Curfman McInnes PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues is %d\n", 621b810aeb4SBarry Smith a->reallocs); 62294a9d846SBarry Smith PLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax); 623dd5f02e7SSatish Balay a->reallocs = 0; 6244e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 6254e220ebcSLois Curfman McInnes 62676dd722bSSatish Balay /* check out for identical nodes. If found, use inode functions */ 62741c01911SSatish Balay ierr = Mat_AIJ_CheckInode(A); CHKERRQ(ierr); 62817ab2063SBarry Smith return 0; 62917ab2063SBarry Smith } 63017ab2063SBarry Smith 6315615d1e5SSatish Balay #undef __FUNC__ 6325615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_SeqAIJ" 6338f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A) 63417ab2063SBarry Smith { 635416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 636cddf8d76SBarry Smith PetscMemzero(a->a,(a->i[a->m]+a->indexshift)*sizeof(Scalar)); 63717ab2063SBarry Smith return 0; 63817ab2063SBarry Smith } 639416022c9SBarry Smith 6405615d1e5SSatish Balay #undef __FUNC__ 6415615d1e5SSatish Balay #define __FUNC__ "MatDestroy_SeqAIJ" 64217ab2063SBarry Smith int MatDestroy_SeqAIJ(PetscObject obj) 64317ab2063SBarry Smith { 644416022c9SBarry Smith Mat A = (Mat) obj; 645416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 646d5d45c9bSBarry Smith 64717ab2063SBarry Smith #if defined(PETSC_LOG) 648416022c9SBarry Smith PLogObjectState(obj,"Rows=%d, Cols=%d, NZ=%d",a->m,a->n,a->nz); 64917ab2063SBarry Smith #endif 6500452661fSBarry Smith PetscFree(a->a); 6510452661fSBarry Smith if (!a->singlemalloc) { PetscFree(a->i); PetscFree(a->j);} 6520452661fSBarry Smith if (a->diag) PetscFree(a->diag); 6530452661fSBarry Smith if (a->ilen) PetscFree(a->ilen); 6540452661fSBarry Smith if (a->imax) PetscFree(a->imax); 6550452661fSBarry Smith if (a->solve_work) PetscFree(a->solve_work); 65676dd722bSSatish Balay if (a->inode.size) PetscFree(a->inode.size); 6570452661fSBarry Smith PetscFree(a); 658eed86810SBarry Smith 659f2655603SLois Curfman McInnes PLogObjectDestroy(A); 660f2655603SLois Curfman McInnes PetscHeaderDestroy(A); 66117ab2063SBarry Smith return 0; 66217ab2063SBarry Smith } 66317ab2063SBarry Smith 6645615d1e5SSatish Balay #undef __FUNC__ 6655615d1e5SSatish Balay #define __FUNC__ "MatCompress_SeqAIJ" 6668f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A) 66717ab2063SBarry Smith { 66817ab2063SBarry Smith return 0; 66917ab2063SBarry Smith } 67017ab2063SBarry Smith 6715615d1e5SSatish Balay #undef __FUNC__ 6725615d1e5SSatish Balay #define __FUNC__ "MatSetOption_SeqAIJ" 6738f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op) 67417ab2063SBarry Smith { 675416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 6766d4a8577SBarry Smith if (op == MAT_ROW_ORIENTED) a->roworiented = 1; 6776d4a8577SBarry Smith else if (op == MAT_COLUMN_ORIENTED) a->roworiented = 0; 6786d4a8577SBarry Smith else if (op == MAT_COLUMNS_SORTED) a->sorted = 1; 679219d9a1aSLois Curfman McInnes else if (op == MAT_COLUMNS_UNSORTED) a->sorted = 0; 6806d4a8577SBarry Smith else if (op == MAT_NO_NEW_NONZERO_LOCATIONS) a->nonew = 1; 681c2653b3dSLois Curfman McInnes else if (op == MAT_NEW_NONZERO_LOCATION_ERROR) a->nonew = -1; 68296854ed6SLois Curfman McInnes else if (op == MAT_NEW_NONZERO_ALLOCATION_ERROR) a->nonew = -2; 6836d4a8577SBarry Smith else if (op == MAT_YES_NEW_NONZERO_LOCATIONS) a->nonew = 0; 6846d4a8577SBarry Smith else if (op == MAT_ROWS_SORTED || 685219d9a1aSLois Curfman McInnes op == MAT_ROWS_UNSORTED || 6866d4a8577SBarry Smith op == MAT_SYMMETRIC || 6876d4a8577SBarry Smith op == MAT_STRUCTURALLY_SYMMETRIC || 68890f02eecSBarry Smith op == MAT_YES_NEW_DIAGONALS || 6892b362799SSatish Balay op == MAT_IGNORE_OFF_PROC_ENTRIES) 69094a424c1SBarry Smith PLogInfo(A,"Info:MatSetOption_SeqAIJ:Option ignored\n"); 6916d4a8577SBarry Smith else if (op == MAT_NO_NEW_DIAGONALS) 692e3372554SBarry Smith {SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS");} 6936d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_1) a->inode.limit = 1; 6946d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_2) a->inode.limit = 2; 6956d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_3) a->inode.limit = 3; 6966d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_4) a->inode.limit = 4; 6976d4a8577SBarry Smith else if (op == MAT_INODE_LIMIT_5) a->inode.limit = 5; 698e2f28af5SBarry Smith else 699e3372554SBarry Smith {SETERRQ(PETSC_ERR_SUP,0,"unknown option");} 70017ab2063SBarry Smith return 0; 70117ab2063SBarry Smith } 70217ab2063SBarry Smith 7035615d1e5SSatish Balay #undef __FUNC__ 7045615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_SeqAIJ" 7058f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v) 70617ab2063SBarry Smith { 707416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 708416022c9SBarry Smith int i,j, n,shift = a->indexshift; 70917ab2063SBarry Smith Scalar *x, zero = 0.0; 71017ab2063SBarry Smith 71117ab2063SBarry Smith VecSet(&zero,v); 71290f02eecSBarry Smith VecGetArray_Fast(v,x); VecGetLocalSize(v,&n); 713e3372554SBarry Smith if (n != a->m) SETERRQ(1,0,"Nonconforming matrix and vector"); 714416022c9SBarry Smith for ( i=0; i<a->m; i++ ) { 715416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 716416022c9SBarry Smith if (a->j[j]+shift == i) { 717416022c9SBarry Smith x[i] = a->a[j]; 71817ab2063SBarry Smith break; 71917ab2063SBarry Smith } 72017ab2063SBarry Smith } 72117ab2063SBarry Smith } 72217ab2063SBarry Smith return 0; 72317ab2063SBarry Smith } 72417ab2063SBarry Smith 72517ab2063SBarry Smith /* -------------------------------------------------------*/ 72617ab2063SBarry Smith /* Should check that shapes of vectors and matrices match */ 72717ab2063SBarry Smith /* -------------------------------------------------------*/ 7285615d1e5SSatish Balay #undef __FUNC__ 7295615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_SeqAIJ" 73044cd7ae7SLois Curfman McInnes int MatMultTrans_SeqAIJ(Mat A,Vec xx,Vec yy) 73117ab2063SBarry Smith { 732416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 73317ab2063SBarry Smith Scalar *x, *y, *v, alpha; 734416022c9SBarry Smith int m = a->m, n, i, *idx, shift = a->indexshift; 73517ab2063SBarry Smith 73690f02eecSBarry Smith VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y); 737cddf8d76SBarry Smith PetscMemzero(y,a->n*sizeof(Scalar)); 73817ab2063SBarry Smith y = y + shift; /* shift for Fortran start by 1 indexing */ 73917ab2063SBarry Smith for ( i=0; i<m; i++ ) { 740416022c9SBarry Smith idx = a->j + a->i[i] + shift; 741416022c9SBarry Smith v = a->a + a->i[i] + shift; 742416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 74317ab2063SBarry Smith alpha = x[i]; 74417ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 74517ab2063SBarry Smith } 746416022c9SBarry Smith PLogFlops(2*a->nz - a->n); 74717ab2063SBarry Smith return 0; 74817ab2063SBarry Smith } 749d5d45c9bSBarry Smith 7505615d1e5SSatish Balay #undef __FUNC__ 7515615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_SeqAIJ" 75244cd7ae7SLois Curfman McInnes int MatMultTransAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 75317ab2063SBarry Smith { 754416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 75517ab2063SBarry Smith Scalar *x, *y, *v, alpha; 756416022c9SBarry Smith int m = a->m, n, i, *idx,shift = a->indexshift; 75717ab2063SBarry Smith 75890f02eecSBarry Smith VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y); 75917ab2063SBarry Smith if (zz != yy) VecCopy(zz,yy); 76017ab2063SBarry Smith y = y + shift; /* shift for Fortran start by 1 indexing */ 76117ab2063SBarry Smith for ( i=0; i<m; i++ ) { 762416022c9SBarry Smith idx = a->j + a->i[i] + shift; 763416022c9SBarry Smith v = a->a + a->i[i] + shift; 764416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 76517ab2063SBarry Smith alpha = x[i]; 76617ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 76717ab2063SBarry Smith } 76890f02eecSBarry Smith PLogFlops(2*a->nz); 76917ab2063SBarry Smith return 0; 77017ab2063SBarry Smith } 77117ab2063SBarry Smith 7725615d1e5SSatish Balay #undef __FUNC__ 7735615d1e5SSatish Balay #define __FUNC__ "MatMult_SeqAIJ" 77444cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 77517ab2063SBarry Smith { 776416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 77717ab2063SBarry Smith Scalar *x, *y, *v, sum; 7789ea0dfa2SSatish Balay int m = a->m, n, i, *idx, shift = a->indexshift,*ii,jrow,j; 77917ab2063SBarry Smith 78090f02eecSBarry Smith VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y); 78117ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 782416022c9SBarry Smith idx = a->j; 783416022c9SBarry Smith v = a->a; 784416022c9SBarry Smith ii = a->i; 78517ab2063SBarry Smith for ( i=0; i<m; i++ ) { 7869ea0dfa2SSatish Balay jrow = ii[i]; 7879ea0dfa2SSatish Balay n = ii[i+1] - jrow; 78817ab2063SBarry Smith sum = 0.0; 7899ea0dfa2SSatish Balay /* while (n--) sum += *v++ * x[*idx++]; */ 7909ea0dfa2SSatish Balay for ( j=0; j<n; j++) { 7919ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 7929ea0dfa2SSatish Balay } 79317ab2063SBarry Smith y[i] = sum; 79417ab2063SBarry Smith } 795416022c9SBarry Smith PLogFlops(2*a->nz - m); 79617ab2063SBarry Smith return 0; 79717ab2063SBarry Smith } 79817ab2063SBarry Smith 7995615d1e5SSatish Balay #undef __FUNC__ 8005615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_SeqAIJ" 80144cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 80217ab2063SBarry Smith { 803416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 80417ab2063SBarry Smith Scalar *x, *y, *z, *v, sum; 8059ea0dfa2SSatish Balay int m = a->m, n, i, *idx, shift = a->indexshift,*ii,jrow,j; 8069ea0dfa2SSatish Balay 80717ab2063SBarry Smith 80890f02eecSBarry Smith VecGetArray_Fast(xx,x); VecGetArray_Fast(yy,y); VecGetArray_Fast(zz,z); 80917ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 810cddf8d76SBarry Smith idx = a->j; 811cddf8d76SBarry Smith v = a->a; 812cddf8d76SBarry Smith ii = a->i; 81317ab2063SBarry Smith for ( i=0; i<m; i++ ) { 8149ea0dfa2SSatish Balay jrow = ii[i]; 8159ea0dfa2SSatish Balay n = ii[i+1] - jrow; 81617ab2063SBarry Smith sum = y[i]; 8179ea0dfa2SSatish Balay /* while (n--) sum += *v++ * x[*idx++]; */ 8189ea0dfa2SSatish Balay for ( j=0; j<n; j++) { 8199ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 8209ea0dfa2SSatish Balay } 82117ab2063SBarry Smith z[i] = sum; 82217ab2063SBarry Smith } 823416022c9SBarry Smith PLogFlops(2*a->nz); 82417ab2063SBarry Smith return 0; 82517ab2063SBarry Smith } 82617ab2063SBarry Smith 82717ab2063SBarry Smith /* 82817ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 82917ab2063SBarry Smith */ 83017ab2063SBarry Smith 8315615d1e5SSatish Balay #undef __FUNC__ 8325615d1e5SSatish Balay #define __FUNC__ "MatMarkDiag_SeqAIJ" 833416022c9SBarry Smith int MatMarkDiag_SeqAIJ(Mat A) 83417ab2063SBarry Smith { 835416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 836416022c9SBarry Smith int i,j, *diag, m = a->m,shift = a->indexshift; 83717ab2063SBarry Smith 8380452661fSBarry Smith diag = (int *) PetscMalloc( (m+1)*sizeof(int)); CHKPTRQ(diag); 839416022c9SBarry Smith PLogObjectMemory(A,(m+1)*sizeof(int)); 840416022c9SBarry Smith for ( i=0; i<a->m; i++ ) { 841416022c9SBarry Smith for ( j=a->i[i]+shift; j<a->i[i+1]+shift; j++ ) { 842416022c9SBarry Smith if (a->j[j]+shift == i) { 84317ab2063SBarry Smith diag[i] = j - shift; 84417ab2063SBarry Smith break; 84517ab2063SBarry Smith } 84617ab2063SBarry Smith } 84717ab2063SBarry Smith } 848416022c9SBarry Smith a->diag = diag; 84917ab2063SBarry Smith return 0; 85017ab2063SBarry Smith } 85117ab2063SBarry Smith 8525615d1e5SSatish Balay #undef __FUNC__ 8535615d1e5SSatish Balay #define __FUNC__ "MatRelax_SeqAIJ" 85444cd7ae7SLois Curfman McInnes int MatRelax_SeqAIJ(Mat A,Vec bb,double omega,MatSORType flag, 85517ab2063SBarry Smith double fshift,int its,Vec xx) 85617ab2063SBarry Smith { 857416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 858416022c9SBarry Smith Scalar *x, *b, *bs, d, *xs, sum, *v = a->a,*t,scale,*ts, *xb; 859d5d45c9bSBarry Smith int ierr, *idx, *diag,n = a->n, m = a->m, i, shift = a->indexshift; 86017ab2063SBarry Smith 86190f02eecSBarry Smith VecGetArray_Fast(xx,x); VecGetArray_Fast(bb,b); 862416022c9SBarry Smith if (!a->diag) {if ((ierr = MatMarkDiag_SeqAIJ(A))) return ierr;} 863416022c9SBarry Smith diag = a->diag; 864416022c9SBarry Smith xs = x + shift; /* shifted by one for index start of a or a->j*/ 86517ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 86617ab2063SBarry Smith /* apply ( U + D/omega) to the vector */ 86717ab2063SBarry Smith bs = b + shift; 86817ab2063SBarry Smith for ( i=0; i<m; i++ ) { 869416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 870416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 871416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 872416022c9SBarry Smith v = a->a + diag[i] + (!shift); 87317ab2063SBarry Smith sum = b[i]*d/omega; 87417ab2063SBarry Smith SPARSEDENSEDOT(sum,bs,v,idx,n); 87517ab2063SBarry Smith x[i] = sum; 87617ab2063SBarry Smith } 87717ab2063SBarry Smith return 0; 87817ab2063SBarry Smith } 87917ab2063SBarry Smith if (flag == SOR_APPLY_LOWER) { 880e3372554SBarry Smith SETERRQ(1,0,"SOR_APPLY_LOWER is not done"); 88117ab2063SBarry Smith } 882416022c9SBarry Smith else if (flag & SOR_EISENSTAT) { 88317ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 88417ab2063SBarry Smith U is upper triangular, E is diagonal; This routine applies 88517ab2063SBarry Smith 88617ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 88717ab2063SBarry Smith 88817ab2063SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 88917ab2063SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 89017ab2063SBarry Smith is the relaxation factor. 89117ab2063SBarry Smith */ 8920452661fSBarry Smith t = (Scalar *) PetscMalloc( m*sizeof(Scalar) ); CHKPTRQ(t); 89317ab2063SBarry Smith scale = (2.0/omega) - 1.0; 89417ab2063SBarry Smith 89517ab2063SBarry Smith /* x = (E + U)^{-1} b */ 89617ab2063SBarry Smith for ( i=m-1; i>=0; i-- ) { 897416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 898416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 899416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 900416022c9SBarry Smith v = a->a + diag[i] + (!shift); 90117ab2063SBarry Smith sum = b[i]; 90217ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 90317ab2063SBarry Smith x[i] = omega*(sum/d); 90417ab2063SBarry Smith } 90517ab2063SBarry Smith 90617ab2063SBarry Smith /* t = b - (2*E - D)x */ 907416022c9SBarry Smith v = a->a; 90817ab2063SBarry Smith for ( i=0; i<m; i++ ) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; } 90917ab2063SBarry Smith 91017ab2063SBarry Smith /* t = (E + L)^{-1}t */ 911416022c9SBarry Smith ts = t + shift; /* shifted by one for index start of a or a->j*/ 912416022c9SBarry Smith diag = a->diag; 91317ab2063SBarry Smith for ( i=0; i<m; i++ ) { 914416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 915416022c9SBarry Smith n = diag[i] - a->i[i]; 916416022c9SBarry Smith idx = a->j + a->i[i] + shift; 917416022c9SBarry Smith v = a->a + a->i[i] + shift; 91817ab2063SBarry Smith sum = t[i]; 91917ab2063SBarry Smith SPARSEDENSEMDOT(sum,ts,v,idx,n); 92017ab2063SBarry Smith t[i] = omega*(sum/d); 92117ab2063SBarry Smith } 92217ab2063SBarry Smith 92317ab2063SBarry Smith /* x = x + t */ 92417ab2063SBarry Smith for ( i=0; i<m; i++ ) { x[i] += t[i]; } 9250452661fSBarry Smith PetscFree(t); 92617ab2063SBarry Smith return 0; 92717ab2063SBarry Smith } 92817ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 92917ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 93017ab2063SBarry Smith for ( i=0; i<m; i++ ) { 931416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 932416022c9SBarry Smith n = diag[i] - a->i[i]; 933416022c9SBarry Smith idx = a->j + a->i[i] + shift; 934416022c9SBarry Smith v = a->a + a->i[i] + shift; 93517ab2063SBarry Smith sum = b[i]; 93617ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 93717ab2063SBarry Smith x[i] = omega*(sum/d); 93817ab2063SBarry Smith } 93917ab2063SBarry Smith xb = x; 94017ab2063SBarry Smith } 94117ab2063SBarry Smith else xb = b; 94217ab2063SBarry Smith if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) && 94317ab2063SBarry Smith (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) { 94417ab2063SBarry Smith for ( i=0; i<m; i++ ) { 945416022c9SBarry Smith x[i] *= a->a[diag[i]+shift]; 94617ab2063SBarry Smith } 94717ab2063SBarry Smith } 94817ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 94917ab2063SBarry Smith for ( i=m-1; i>=0; i-- ) { 950416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 951416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 952416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 953416022c9SBarry Smith v = a->a + diag[i] + (!shift); 95417ab2063SBarry Smith sum = xb[i]; 95517ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 95617ab2063SBarry Smith x[i] = omega*(sum/d); 95717ab2063SBarry Smith } 95817ab2063SBarry Smith } 95917ab2063SBarry Smith its--; 96017ab2063SBarry Smith } 96117ab2063SBarry Smith while (its--) { 96217ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 96317ab2063SBarry Smith for ( i=0; i<m; i++ ) { 964416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 965416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 966416022c9SBarry Smith idx = a->j + a->i[i] + shift; 967416022c9SBarry Smith v = a->a + a->i[i] + shift; 96817ab2063SBarry Smith sum = b[i]; 96917ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 9707e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 97117ab2063SBarry Smith } 97217ab2063SBarry Smith } 97317ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 97417ab2063SBarry Smith for ( i=m-1; i>=0; i-- ) { 975416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 976416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 977416022c9SBarry Smith idx = a->j + a->i[i] + shift; 978416022c9SBarry Smith v = a->a + a->i[i] + shift; 97917ab2063SBarry Smith sum = b[i]; 98017ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 9817e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 98217ab2063SBarry Smith } 98317ab2063SBarry Smith } 98417ab2063SBarry Smith } 98517ab2063SBarry Smith return 0; 98617ab2063SBarry Smith } 98717ab2063SBarry Smith 9885615d1e5SSatish Balay #undef __FUNC__ 9895615d1e5SSatish Balay #define __FUNC__ "MatGetInfo_SeqAIJ" 9908f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 99117ab2063SBarry Smith { 992416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 9934e220ebcSLois Curfman McInnes 9944e220ebcSLois Curfman McInnes info->rows_global = (double)a->m; 9954e220ebcSLois Curfman McInnes info->columns_global = (double)a->n; 9964e220ebcSLois Curfman McInnes info->rows_local = (double)a->m; 9974e220ebcSLois Curfman McInnes info->columns_local = (double)a->n; 9984e220ebcSLois Curfman McInnes info->block_size = 1.0; 9994e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 10004e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 10014e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 10024e220ebcSLois Curfman McInnes /* if (info->nz_unneeded != A->info.nz_unneeded) 10034e220ebcSLois Curfman McInnes printf("space descrepancy: maxnz-nz = %d, nz_unneeded = %d\n",(int)info->nz_unneeded,(int)A->info.nz_unneeded); */ 10044e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 10054e220ebcSLois Curfman McInnes info->mallocs = (double)a->reallocs; 10064e220ebcSLois Curfman McInnes info->memory = A->mem; 10074e220ebcSLois Curfman McInnes if (A->factor) { 10084e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 10094e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 10104e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 10114e220ebcSLois Curfman McInnes } else { 10124e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 10134e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 10144e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 10154e220ebcSLois Curfman McInnes } 101617ab2063SBarry Smith return 0; 101717ab2063SBarry Smith } 101817ab2063SBarry Smith 101917ab2063SBarry Smith extern int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,double,Mat*); 102017ab2063SBarry Smith extern int MatLUFactorNumeric_SeqAIJ(Mat,Mat*); 102117ab2063SBarry Smith extern int MatLUFactor_SeqAIJ(Mat,IS,IS,double); 102217ab2063SBarry Smith extern int MatSolve_SeqAIJ(Mat,Vec,Vec); 102317ab2063SBarry Smith extern int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec); 102417ab2063SBarry Smith extern int MatSolveTrans_SeqAIJ(Mat,Vec,Vec); 102517ab2063SBarry Smith extern int MatSolveTransAdd_SeqAIJ(Mat,Vec,Vec,Vec); 102617ab2063SBarry Smith 10275615d1e5SSatish Balay #undef __FUNC__ 10285615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_SeqAIJ" 10298f6be9afSLois Curfman McInnes int MatZeroRows_SeqAIJ(Mat A,IS is,Scalar *diag) 103017ab2063SBarry Smith { 1031416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1032416022c9SBarry Smith int i,ierr,N, *rows,m = a->m - 1,shift = a->indexshift; 103317ab2063SBarry Smith 103477c4ece6SBarry Smith ierr = ISGetSize(is,&N); CHKERRQ(ierr); 103517ab2063SBarry Smith ierr = ISGetIndices(is,&rows); CHKERRQ(ierr); 103617ab2063SBarry Smith if (diag) { 103717ab2063SBarry Smith for ( i=0; i<N; i++ ) { 1038e3372554SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(1,0,"row out of range"); 1039416022c9SBarry Smith if (a->ilen[rows[i]] > 0) { /* in case row was completely empty */ 1040416022c9SBarry Smith a->ilen[rows[i]] = 1; 1041416022c9SBarry Smith a->a[a->i[rows[i]]+shift] = *diag; 1042416022c9SBarry Smith a->j[a->i[rows[i]]+shift] = rows[i]+shift; 104317ab2063SBarry Smith } 104417ab2063SBarry Smith else { 104517ab2063SBarry Smith ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES); 104617ab2063SBarry Smith CHKERRQ(ierr); 104717ab2063SBarry Smith } 104817ab2063SBarry Smith } 104917ab2063SBarry Smith } 105017ab2063SBarry Smith else { 105117ab2063SBarry Smith for ( i=0; i<N; i++ ) { 1052e3372554SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(1,0,"row out of range"); 1053416022c9SBarry Smith a->ilen[rows[i]] = 0; 105417ab2063SBarry Smith } 105517ab2063SBarry Smith } 105617ab2063SBarry Smith ISRestoreIndices(is,&rows); 105743a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 105817ab2063SBarry Smith return 0; 105917ab2063SBarry Smith } 106017ab2063SBarry Smith 10615615d1e5SSatish Balay #undef __FUNC__ 10625615d1e5SSatish Balay #define __FUNC__ "MatGetSize_SeqAIJ" 10638f6be9afSLois Curfman McInnes int MatGetSize_SeqAIJ(Mat A,int *m,int *n) 106417ab2063SBarry Smith { 1065416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1066416022c9SBarry Smith *m = a->m; *n = a->n; 106717ab2063SBarry Smith return 0; 106817ab2063SBarry Smith } 106917ab2063SBarry Smith 10705615d1e5SSatish Balay #undef __FUNC__ 10715615d1e5SSatish Balay #define __FUNC__ "MatGetOwnershipRange_SeqAIJ" 10728f6be9afSLois Curfman McInnes int MatGetOwnershipRange_SeqAIJ(Mat A,int *m,int *n) 107317ab2063SBarry Smith { 1074416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1075416022c9SBarry Smith *m = 0; *n = a->m; 107617ab2063SBarry Smith return 0; 107717ab2063SBarry Smith } 1078026e39d0SSatish Balay 10795615d1e5SSatish Balay #undef __FUNC__ 10805615d1e5SSatish Balay #define __FUNC__ "MatGetRow_SeqAIJ" 10814e093b46SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v) 108217ab2063SBarry Smith { 1083416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1084c456f294SBarry Smith int *itmp,i,shift = a->indexshift; 108517ab2063SBarry Smith 1086e3372554SBarry Smith if (row < 0 || row >= a->m) SETERRQ(1,0,"Row out of range"); 108717ab2063SBarry Smith 1088416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1089416022c9SBarry Smith if (v) *v = a->a + a->i[row] + shift; 109017ab2063SBarry Smith if (idx) { 1091416022c9SBarry Smith itmp = a->j + a->i[row] + shift; 10924e093b46SBarry Smith if (*nz && shift) { 10930452661fSBarry Smith *idx = (int *) PetscMalloc( (*nz)*sizeof(int) ); CHKPTRQ(*idx); 109417ab2063SBarry Smith for ( i=0; i<(*nz); i++ ) {(*idx)[i] = itmp[i] + shift;} 10954e093b46SBarry Smith } else if (*nz) { 10964e093b46SBarry Smith *idx = itmp; 109717ab2063SBarry Smith } 109817ab2063SBarry Smith else *idx = 0; 109917ab2063SBarry Smith } 110017ab2063SBarry Smith return 0; 110117ab2063SBarry Smith } 110217ab2063SBarry Smith 11035615d1e5SSatish Balay #undef __FUNC__ 11045615d1e5SSatish Balay #define __FUNC__ "MatRestoreRow_SeqAIJ" 11054e093b46SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,Scalar **v) 110617ab2063SBarry Smith { 11074e093b46SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 11084e093b46SBarry Smith if (idx) {if (*idx && a->indexshift) PetscFree(*idx);} 110917ab2063SBarry Smith return 0; 111017ab2063SBarry Smith } 111117ab2063SBarry Smith 11125615d1e5SSatish Balay #undef __FUNC__ 11135615d1e5SSatish Balay #define __FUNC__ "MatNorm_SeqAIJ" 11148f6be9afSLois Curfman McInnes int MatNorm_SeqAIJ(Mat A,NormType type,double *norm) 111517ab2063SBarry Smith { 1116416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1117416022c9SBarry Smith Scalar *v = a->a; 111817ab2063SBarry Smith double sum = 0.0; 1119416022c9SBarry Smith int i, j,shift = a->indexshift; 112017ab2063SBarry Smith 112117ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1122416022c9SBarry Smith for (i=0; i<a->nz; i++ ) { 112317ab2063SBarry Smith #if defined(PETSC_COMPLEX) 112417ab2063SBarry Smith sum += real(conj(*v)*(*v)); v++; 112517ab2063SBarry Smith #else 112617ab2063SBarry Smith sum += (*v)*(*v); v++; 112717ab2063SBarry Smith #endif 112817ab2063SBarry Smith } 112917ab2063SBarry Smith *norm = sqrt(sum); 113017ab2063SBarry Smith } 113117ab2063SBarry Smith else if (type == NORM_1) { 113217ab2063SBarry Smith double *tmp; 1133416022c9SBarry Smith int *jj = a->j; 113466963ce1SSatish Balay tmp = (double *) PetscMalloc( (a->n+1)*sizeof(double) ); CHKPTRQ(tmp); 1135cddf8d76SBarry Smith PetscMemzero(tmp,a->n*sizeof(double)); 113617ab2063SBarry Smith *norm = 0.0; 1137416022c9SBarry Smith for ( j=0; j<a->nz; j++ ) { 1138a2744918SBarry Smith tmp[*jj++ + shift] += PetscAbsScalar(*v); v++; 113917ab2063SBarry Smith } 1140416022c9SBarry Smith for ( j=0; j<a->n; j++ ) { 114117ab2063SBarry Smith if (tmp[j] > *norm) *norm = tmp[j]; 114217ab2063SBarry Smith } 11430452661fSBarry Smith PetscFree(tmp); 114417ab2063SBarry Smith } 114517ab2063SBarry Smith else if (type == NORM_INFINITY) { 114617ab2063SBarry Smith *norm = 0.0; 1147416022c9SBarry Smith for ( j=0; j<a->m; j++ ) { 1148416022c9SBarry Smith v = a->a + a->i[j] + shift; 114917ab2063SBarry Smith sum = 0.0; 1150416022c9SBarry Smith for ( i=0; i<a->i[j+1]-a->i[j]; i++ ) { 1151cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 115217ab2063SBarry Smith } 115317ab2063SBarry Smith if (sum > *norm) *norm = sum; 115417ab2063SBarry Smith } 115517ab2063SBarry Smith } 115617ab2063SBarry Smith else { 1157e3372554SBarry Smith SETERRQ(1,0,"No support for two norm yet"); 115817ab2063SBarry Smith } 115917ab2063SBarry Smith return 0; 116017ab2063SBarry Smith } 116117ab2063SBarry Smith 11625615d1e5SSatish Balay #undef __FUNC__ 11635615d1e5SSatish Balay #define __FUNC__ "MatTranspose_SeqAIJ" 11648f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B) 116517ab2063SBarry Smith { 1166416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 1167416022c9SBarry Smith Mat C; 1168416022c9SBarry Smith int i, ierr, *aj = a->j, *ai = a->i, m = a->m, len, *col; 1169416022c9SBarry Smith int shift = a->indexshift; 1170d5d45c9bSBarry Smith Scalar *array = a->a; 117117ab2063SBarry Smith 11723638b69dSLois Curfman McInnes if (B == PETSC_NULL && m != a->n) 1173e3372554SBarry Smith SETERRQ(1,0,"Square matrix only for in-place"); 11740452661fSBarry Smith col = (int *) PetscMalloc((1+a->n)*sizeof(int)); CHKPTRQ(col); 1175cddf8d76SBarry Smith PetscMemzero(col,(1+a->n)*sizeof(int)); 117617ab2063SBarry Smith if (shift) { 117717ab2063SBarry Smith for ( i=0; i<ai[m]-1; i++ ) aj[i] -= 1; 117817ab2063SBarry Smith } 117917ab2063SBarry Smith for ( i=0; i<ai[m]+shift; i++ ) col[aj[i]] += 1; 1180416022c9SBarry Smith ierr = MatCreateSeqAIJ(A->comm,a->n,m,0,col,&C); CHKERRQ(ierr); 11810452661fSBarry Smith PetscFree(col); 118217ab2063SBarry Smith for ( i=0; i<m; i++ ) { 118317ab2063SBarry Smith len = ai[i+1]-ai[i]; 1184416022c9SBarry Smith ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES); CHKERRQ(ierr); 118517ab2063SBarry Smith array += len; aj += len; 118617ab2063SBarry Smith } 118717ab2063SBarry Smith if (shift) { 118817ab2063SBarry Smith for ( i=0; i<ai[m]-1; i++ ) aj[i] += 1; 118917ab2063SBarry Smith } 119017ab2063SBarry Smith 11916d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 11926d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 119317ab2063SBarry Smith 11943638b69dSLois Curfman McInnes if (B != PETSC_NULL) { 1195416022c9SBarry Smith *B = C; 119617ab2063SBarry Smith } else { 1197416022c9SBarry Smith /* This isn't really an in-place transpose */ 11980452661fSBarry Smith PetscFree(a->a); 11990452661fSBarry Smith if (!a->singlemalloc) {PetscFree(a->i); PetscFree(a->j);} 12000452661fSBarry Smith if (a->diag) PetscFree(a->diag); 12010452661fSBarry Smith if (a->ilen) PetscFree(a->ilen); 12020452661fSBarry Smith if (a->imax) PetscFree(a->imax); 12030452661fSBarry Smith if (a->solve_work) PetscFree(a->solve_work); 12041073c447SSatish Balay if (a->inode.size) PetscFree(a->inode.size); 12050452661fSBarry Smith PetscFree(a); 1206f09e8eb9SSatish Balay PetscMemcpy(A,C,sizeof(struct _p_Mat)); 12070452661fSBarry Smith PetscHeaderDestroy(C); 120817ab2063SBarry Smith } 120917ab2063SBarry Smith return 0; 121017ab2063SBarry Smith } 121117ab2063SBarry Smith 12125615d1e5SSatish Balay #undef __FUNC__ 12135615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_SeqAIJ" 12148f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 121517ab2063SBarry Smith { 1216416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 121717ab2063SBarry Smith Scalar *l,*r,x,*v; 1218d5d45c9bSBarry Smith int i,j,m = a->m, n = a->n, M, nz = a->nz, *jj,shift = a->indexshift; 121917ab2063SBarry Smith 122017ab2063SBarry Smith if (ll) { 12213ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 12223ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 12239b1297e1SSatish Balay VecGetLocalSize_Fast(ll,m); 1224e3372554SBarry Smith if (m != a->m) SETERRQ(1,0,"Left scaling vector wrong length"); 122590f02eecSBarry Smith VecGetArray_Fast(ll,l); 1226416022c9SBarry Smith v = a->a; 122717ab2063SBarry Smith for ( i=0; i<m; i++ ) { 122817ab2063SBarry Smith x = l[i]; 1229416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 123017ab2063SBarry Smith for ( j=0; j<M; j++ ) { (*v++) *= x;} 123117ab2063SBarry Smith } 123244cd7ae7SLois Curfman McInnes PLogFlops(nz); 123317ab2063SBarry Smith } 123417ab2063SBarry Smith if (rr) { 12359b1297e1SSatish Balay VecGetLocalSize_Fast(rr,n); 1236e3372554SBarry Smith if (n != a->n) SETERRQ(1,0,"Right scaling vector wrong length"); 123790f02eecSBarry Smith VecGetArray_Fast(rr,r); 1238416022c9SBarry Smith v = a->a; jj = a->j; 123917ab2063SBarry Smith for ( i=0; i<nz; i++ ) { 124017ab2063SBarry Smith (*v++) *= r[*jj++ + shift]; 124117ab2063SBarry Smith } 124244cd7ae7SLois Curfman McInnes PLogFlops(nz); 124317ab2063SBarry Smith } 124417ab2063SBarry Smith return 0; 124517ab2063SBarry Smith } 124617ab2063SBarry Smith 12475615d1e5SSatish Balay #undef __FUNC__ 12485615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrix_SeqAIJ" 12498f6be9afSLois Curfman McInnes int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,MatGetSubMatrixCall scall,Mat *B) 125017ab2063SBarry Smith { 1251db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data,*c; 125202834360SBarry Smith int nznew, *smap, i, k, kstart, kend, ierr, oldcols = a->n,*lens; 125399141d43SSatish Balay int row,mat_i,*mat_j,tcol,first,step,*mat_ilen; 1254a2744918SBarry Smith register int sum,lensi; 125599141d43SSatish Balay int *irow, *icol, nrows, ncols, shift = a->indexshift,*ssmap; 125699141d43SSatish Balay int *starts,*j_new,*i_new,*aj = a->j, *ai = a->i,ii,*ailen = a->ilen; 125799141d43SSatish Balay Scalar *a_new,*mat_a; 1258416022c9SBarry Smith Mat C; 125917ab2063SBarry Smith 1260b48a1e75SSatish Balay ierr = ISSorted(isrow,(PetscTruth*)&i); 1261e3372554SBarry Smith if (!i) SETERRQ(1,0,"ISrow is not sorted"); 126299141d43SSatish Balay ierr = ISSorted(iscol,(PetscTruth*)&i); 1263e3372554SBarry Smith if (!i) SETERRQ(1,0,"IScol is not sorted"); 126499141d43SSatish Balay 126517ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow); CHKERRQ(ierr); 126617ab2063SBarry Smith ierr = ISGetSize(isrow,&nrows); CHKERRQ(ierr); 126717ab2063SBarry Smith ierr = ISGetSize(iscol,&ncols); CHKERRQ(ierr); 126817ab2063SBarry Smith 12697264ac53SSatish Balay if (ISStrideGetInfo(iscol,&first,&step) && step == 1) { /* no need to sort */ 127002834360SBarry Smith /* special case of contiguous rows */ 127157faeb66SBarry Smith lens = (int *) PetscMalloc((ncols+nrows+1)*sizeof(int)); CHKPTRQ(lens); 127202834360SBarry Smith starts = lens + ncols; 127302834360SBarry Smith /* loop over new rows determining lens and starting points */ 127402834360SBarry Smith for (i=0; i<nrows; i++) { 1275a2744918SBarry Smith kstart = ai[irow[i]]+shift; 1276a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 127702834360SBarry Smith for ( k=kstart; k<kend; k++ ) { 1278d8ced48eSBarry Smith if (aj[k]+shift >= first) { 127902834360SBarry Smith starts[i] = k; 128002834360SBarry Smith break; 128102834360SBarry Smith } 128202834360SBarry Smith } 1283a2744918SBarry Smith sum = 0; 128402834360SBarry Smith while (k < kend) { 1285d8ced48eSBarry Smith if (aj[k++]+shift >= first+ncols) break; 1286a2744918SBarry Smith sum++; 128702834360SBarry Smith } 1288a2744918SBarry Smith lens[i] = sum; 128902834360SBarry Smith } 129002834360SBarry Smith /* create submatrix */ 1291cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 129208480c60SBarry Smith int n_cols,n_rows; 129308480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols); CHKERRQ(ierr); 1294e3372554SBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(1,0,""); 1295d8ced48eSBarry Smith ierr = MatZeroEntries(*B); CHKERRQ(ierr); 129608480c60SBarry Smith C = *B; 129708480c60SBarry Smith } 129808480c60SBarry Smith else { 129902834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 130008480c60SBarry Smith } 1301db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*) C->data; 1302db02288aSLois Curfman McInnes 130302834360SBarry Smith /* loop over rows inserting into submatrix */ 1304db02288aSLois Curfman McInnes a_new = c->a; 1305db02288aSLois Curfman McInnes j_new = c->j; 1306db02288aSLois Curfman McInnes i_new = c->i; 1307db02288aSLois Curfman McInnes i_new[0] = -shift; 130802834360SBarry Smith for (i=0; i<nrows; i++) { 1309a2744918SBarry Smith ii = starts[i]; 1310a2744918SBarry Smith lensi = lens[i]; 1311a2744918SBarry Smith for ( k=0; k<lensi; k++ ) { 1312a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 131302834360SBarry Smith } 1314a2744918SBarry Smith PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(Scalar)); 1315a2744918SBarry Smith a_new += lensi; 1316a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1317a2744918SBarry Smith c->ilen[i] = lensi; 131802834360SBarry Smith } 13190452661fSBarry Smith PetscFree(lens); 132002834360SBarry Smith } 132102834360SBarry Smith else { 132202834360SBarry Smith ierr = ISGetIndices(iscol,&icol); CHKERRQ(ierr); 13230452661fSBarry Smith smap = (int *) PetscMalloc((1+oldcols)*sizeof(int)); CHKPTRQ(smap); 132402834360SBarry Smith ssmap = smap + shift; 132599141d43SSatish Balay lens = (int *) PetscMalloc((1+nrows)*sizeof(int)); CHKPTRQ(lens); 1326cddf8d76SBarry Smith PetscMemzero(smap,oldcols*sizeof(int)); 132717ab2063SBarry Smith for ( i=0; i<ncols; i++ ) smap[icol[i]] = i+1; 132802834360SBarry Smith /* determine lens of each row */ 132902834360SBarry Smith for (i=0; i<nrows; i++) { 1330d8ced48eSBarry Smith kstart = ai[irow[i]]+shift; 133102834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 133202834360SBarry Smith lens[i] = 0; 133302834360SBarry Smith for ( k=kstart; k<kend; k++ ) { 1334d8ced48eSBarry Smith if (ssmap[aj[k]]) { 133502834360SBarry Smith lens[i]++; 133602834360SBarry Smith } 133702834360SBarry Smith } 133802834360SBarry Smith } 133917ab2063SBarry Smith /* Create and fill new matrix */ 1340a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 134199141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 134299141d43SSatish Balay 1343e3372554SBarry Smith if (c->m != nrows || c->n != ncols) SETERRQ(1,0,""); 134499141d43SSatish Balay if (PetscMemcmp(c->ilen,lens, c->m *sizeof(int))) { 1345e3372554SBarry Smith SETERRQ(1,0,"Cannot reuse matrix. wrong no of nonzeros"); 134699141d43SSatish Balay } 134799141d43SSatish Balay PetscMemzero(c->ilen,c->m*sizeof(int)); 134808480c60SBarry Smith C = *B; 134908480c60SBarry Smith } 135008480c60SBarry Smith else { 135102834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 135208480c60SBarry Smith } 135399141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 135417ab2063SBarry Smith for (i=0; i<nrows; i++) { 135599141d43SSatish Balay row = irow[i]; 135617ab2063SBarry Smith nznew = 0; 135799141d43SSatish Balay kstart = ai[row]+shift; 135899141d43SSatish Balay kend = kstart + a->ilen[row]; 135999141d43SSatish Balay mat_i = c->i[i]+shift; 136099141d43SSatish Balay mat_j = c->j + mat_i; 136199141d43SSatish Balay mat_a = c->a + mat_i; 136299141d43SSatish Balay mat_ilen = c->ilen + i; 136317ab2063SBarry Smith for ( k=kstart; k<kend; k++ ) { 136499141d43SSatish Balay if ((tcol=ssmap[a->j[k]])) { 136599141d43SSatish Balay *mat_j++ = tcol - (!shift); 136699141d43SSatish Balay *mat_a++ = a->a[k]; 136799141d43SSatish Balay (*mat_ilen)++; 136899141d43SSatish Balay 136917ab2063SBarry Smith } 137017ab2063SBarry Smith } 137117ab2063SBarry Smith } 137202834360SBarry Smith /* Free work space */ 137302834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol); CHKERRQ(ierr); 137499141d43SSatish Balay PetscFree(smap); PetscFree(lens); 137502834360SBarry Smith } 13766d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 13776d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 137817ab2063SBarry Smith 137917ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow); CHKERRQ(ierr); 1380416022c9SBarry Smith *B = C; 138117ab2063SBarry Smith return 0; 138217ab2063SBarry Smith } 138317ab2063SBarry Smith 1384a871dcd8SBarry Smith /* 138563b91edcSBarry Smith note: This can only work for identity for row and col. It would 138663b91edcSBarry Smith be good to check this and otherwise generate an error. 1387a871dcd8SBarry Smith */ 13885615d1e5SSatish Balay #undef __FUNC__ 13895615d1e5SSatish Balay #define __FUNC__ "MatILUFactor_SeqAIJ" 13908f6be9afSLois Curfman McInnes int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,double efill,int fill) 1391a871dcd8SBarry Smith { 139263b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data; 139308480c60SBarry Smith int ierr; 139463b91edcSBarry Smith Mat outA; 139563b91edcSBarry Smith 1396e3372554SBarry Smith if (fill != 0) SETERRQ(1,0,"Only fill=0 supported"); 1397a871dcd8SBarry Smith 139863b91edcSBarry Smith outA = inA; 139963b91edcSBarry Smith inA->factor = FACTOR_LU; 140063b91edcSBarry Smith a->row = row; 140163b91edcSBarry Smith a->col = col; 140263b91edcSBarry Smith 140394a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 14040452661fSBarry Smith a->solve_work = (Scalar *) PetscMalloc( (a->m+1)*sizeof(Scalar)); CHKPTRQ(a->solve_work); 140594a9d846SBarry Smith } 140663b91edcSBarry Smith 140708480c60SBarry Smith if (!a->diag) { 140808480c60SBarry Smith ierr = MatMarkDiag_SeqAIJ(inA); CHKERRQ(ierr); 140963b91edcSBarry Smith } 141063b91edcSBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA); CHKERRQ(ierr); 1411a871dcd8SBarry Smith return 0; 1412a871dcd8SBarry Smith } 1413a871dcd8SBarry Smith 1414f0b747eeSBarry Smith #include "pinclude/plapack.h" 14155615d1e5SSatish Balay #undef __FUNC__ 14165615d1e5SSatish Balay #define __FUNC__ "MatScale_SeqAIJ" 14178f6be9afSLois Curfman McInnes int MatScale_SeqAIJ(Scalar *alpha,Mat inA) 1418f0b747eeSBarry Smith { 1419f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) inA->data; 1420f0b747eeSBarry Smith int one = 1; 1421f0b747eeSBarry Smith BLscal_( &a->nz, alpha, a->a, &one ); 1422f0b747eeSBarry Smith PLogFlops(a->nz); 1423f0b747eeSBarry Smith return 0; 1424f0b747eeSBarry Smith } 1425f0b747eeSBarry Smith 14265615d1e5SSatish Balay #undef __FUNC__ 14275615d1e5SSatish Balay #define __FUNC__ "MatGetSubMatrices_SeqAIJ" 14288f6be9afSLois Curfman McInnes int MatGetSubMatrices_SeqAIJ(Mat A,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall, 1429cddf8d76SBarry Smith Mat **B) 1430cddf8d76SBarry Smith { 1431cddf8d76SBarry Smith int ierr,i; 1432cddf8d76SBarry Smith 1433cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 14340452661fSBarry Smith *B = (Mat *) PetscMalloc( (n+1)*sizeof(Mat) ); CHKPTRQ(*B); 1435cddf8d76SBarry Smith } 1436cddf8d76SBarry Smith 1437cddf8d76SBarry Smith for ( i=0; i<n; i++ ) { 1438905e6a2fSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],scall,&(*B)[i]);CHKERRQ(ierr); 1439cddf8d76SBarry Smith } 1440cddf8d76SBarry Smith return 0; 1441cddf8d76SBarry Smith } 1442cddf8d76SBarry Smith 14435615d1e5SSatish Balay #undef __FUNC__ 14445615d1e5SSatish Balay #define __FUNC__ "MatGetBlockSize_SeqAIJ" 14458f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A, int *bs) 14465a838052SSatish Balay { 14475a838052SSatish Balay *bs = 1; 14485a838052SSatish Balay return 0; 14495a838052SSatish Balay } 14505a838052SSatish Balay 14515615d1e5SSatish Balay #undef __FUNC__ 14525615d1e5SSatish Balay #define __FUNC__ "MatIncreaseOverlap_SeqAIJ" 14538f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A, int is_max, IS *is, int ov) 14544dcbc457SBarry Smith { 1455e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 145606763907SSatish Balay int shift, row, i,j,k,l,m,n, *idx,ierr, *nidx, isz, val; 14578a047759SSatish Balay int start, end, *ai, *aj; 145806763907SSatish Balay char *table; 14598a047759SSatish Balay shift = a->indexshift; 1460e4d965acSSatish Balay m = a->m; 1461e4d965acSSatish Balay ai = a->i; 14628a047759SSatish Balay aj = a->j+shift; 14638a047759SSatish Balay 1464e3372554SBarry Smith if (ov < 0) SETERRQ(1,0,"illegal overlap value used"); 146506763907SSatish Balay 146606763907SSatish Balay table = (char *) PetscMalloc((m/BITSPERBYTE +1)*sizeof(char)); CHKPTRQ(table); 146706763907SSatish Balay nidx = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(nidx); 146806763907SSatish Balay 1469e4d965acSSatish Balay for ( i=0; i<is_max; i++ ) { 1470b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1471e4d965acSSatish Balay isz = 0; 147206763907SSatish Balay PetscMemzero(table,(m/BITSPERBYTE +1)*sizeof(char)); 1473e4d965acSSatish Balay 1474e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 14754dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx); CHKERRQ(ierr); 147677c4ece6SBarry Smith ierr = ISGetSize(is[i],&n); CHKERRQ(ierr); 1477e4d965acSSatish Balay 1478dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1479e4d965acSSatish Balay for ( j=0; j<n ; ++j){ 148006763907SSatish Balay if(!BT_LOOKUP(table, idx[j])) { nidx[isz++] = idx[j];} 14814dcbc457SBarry Smith } 148206763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx); CHKERRQ(ierr); 148306763907SSatish Balay ierr = ISDestroy(is[i]); CHKERRQ(ierr); 1484e4d965acSSatish Balay 148504a348a9SBarry Smith k = 0; 148604a348a9SBarry Smith for ( j=0; j<ov; j++){ /* for each overlap */ 148704a348a9SBarry Smith n = isz; 148806763907SSatish Balay for ( ; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 1489e4d965acSSatish Balay row = nidx[k]; 1490e4d965acSSatish Balay start = ai[row]; 1491e4d965acSSatish Balay end = ai[row+1]; 149204a348a9SBarry Smith for ( l = start; l<end ; l++){ 14938a047759SSatish Balay val = aj[l] + shift; 149406763907SSatish Balay if (!BT_LOOKUP(table,val)) {nidx[isz++] = val;} 1495e4d965acSSatish Balay } 1496e4d965acSSatish Balay } 1497e4d965acSSatish Balay } 1498029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, (is+i)); CHKERRQ(ierr); 1499e4d965acSSatish Balay } 150004a348a9SBarry Smith PetscFree(table); 150106763907SSatish Balay PetscFree(nidx); 1502e4d965acSSatish Balay return 0; 15034dcbc457SBarry Smith } 150417ab2063SBarry Smith 15050513a670SBarry Smith /* -------------------------------------------------------------- */ 15060513a670SBarry Smith #undef __FUNC__ 15070513a670SBarry Smith #define __FUNC__ "MatPermute_SeqAIJ" 15080513a670SBarry Smith int MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B) 15090513a670SBarry Smith { 15100513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 15110513a670SBarry Smith Scalar *vwork; 15120513a670SBarry Smith int i, ierr, nz, m = a->m, n = a->n, *cwork; 15130513a670SBarry Smith int *row,*col,*cnew,j,*lens; 15140513a670SBarry Smith 15150513a670SBarry Smith ierr = ISGetIndices(rowp,&row); CHKERRQ(ierr); 15160513a670SBarry Smith ierr = ISGetIndices(colp,&col); CHKERRQ(ierr); 15170513a670SBarry Smith 15180513a670SBarry Smith /* determine lengths of permuted rows */ 15190513a670SBarry Smith lens = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(lens); 15200513a670SBarry Smith for (i=0; i<m; i++ ) { 15210513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 15220513a670SBarry Smith } 15230513a670SBarry Smith ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr); 15240513a670SBarry Smith PetscFree(lens); 15250513a670SBarry Smith 15260513a670SBarry Smith cnew = (int *) PetscMalloc( n*sizeof(int) ); CHKPTRQ(cnew); 15270513a670SBarry Smith for (i=0; i<m; i++) { 15280513a670SBarry Smith ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 15290513a670SBarry Smith for (j=0; j<nz; j++ ) { cnew[j] = col[cwork[j]];} 15300513a670SBarry Smith ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES); CHKERRQ(ierr); 15310513a670SBarry Smith ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 15320513a670SBarry Smith } 15330513a670SBarry Smith PetscFree(cnew); 15340513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 15350513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 15360513a670SBarry Smith ierr = ISRestoreIndices(rowp,&row); CHKERRQ(ierr); 15370513a670SBarry Smith ierr = ISRestoreIndices(colp,&col); CHKERRQ(ierr); 15380513a670SBarry Smith return 0; 15390513a670SBarry Smith } 15400513a670SBarry Smith 15415615d1e5SSatish Balay #undef __FUNC__ 15425615d1e5SSatish Balay #define __FUNC__ "MatPrintHelp_SeqAIJ" 1543682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A) 1544682d7d0cSBarry Smith { 1545682d7d0cSBarry Smith static int called = 0; 1546682d7d0cSBarry Smith MPI_Comm comm = A->comm; 1547682d7d0cSBarry Smith 1548682d7d0cSBarry Smith if (called) return 0; else called = 1; 154977c4ece6SBarry Smith PetscPrintf(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n"); 15500f665d81SLois Curfman McInnes PetscPrintf(comm," -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n"); 15510f665d81SLois Curfman McInnes PetscPrintf(comm," -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n"); 15520f665d81SLois Curfman McInnes PetscPrintf(comm," -mat_aij_no_inode: Do not use inodes\n"); 15530f665d81SLois Curfman McInnes PetscPrintf(comm," -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n"); 1554682d7d0cSBarry Smith #if defined(HAVE_ESSL) 15550f665d81SLois Curfman McInnes PetscPrintf(comm," -mat_aij_essl: Use IBM sparse LU factorization and solve.\n"); 1556682d7d0cSBarry Smith #endif 1557682d7d0cSBarry Smith return 0; 1558682d7d0cSBarry Smith } 15598f6be9afSLois Curfman McInnes extern int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg); 1560a93ec695SBarry Smith extern int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring); 1561a93ec695SBarry Smith extern int MatColoringPatch_SeqAIJ(Mat,int,int *,ISColoring *); 1562a93ec695SBarry Smith 1563682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 156417ab2063SBarry Smith static struct _MatOps MatOps = {MatSetValues_SeqAIJ, 156517ab2063SBarry Smith MatGetRow_SeqAIJ,MatRestoreRow_SeqAIJ, 1566416022c9SBarry Smith MatMult_SeqAIJ,MatMultAdd_SeqAIJ, 1567416022c9SBarry Smith MatMultTrans_SeqAIJ,MatMultTransAdd_SeqAIJ, 156817ab2063SBarry Smith MatSolve_SeqAIJ,MatSolveAdd_SeqAIJ, 156917ab2063SBarry Smith MatSolveTrans_SeqAIJ,MatSolveTransAdd_SeqAIJ, 157017ab2063SBarry Smith MatLUFactor_SeqAIJ,0, 157117ab2063SBarry Smith MatRelax_SeqAIJ, 157217ab2063SBarry Smith MatTranspose_SeqAIJ, 15737264ac53SSatish Balay MatGetInfo_SeqAIJ,MatEqual_SeqAIJ, 1574f0b747eeSBarry Smith MatGetDiagonal_SeqAIJ,MatDiagonalScale_SeqAIJ,MatNorm_SeqAIJ, 157517ab2063SBarry Smith 0,MatAssemblyEnd_SeqAIJ, 157617ab2063SBarry Smith MatCompress_SeqAIJ, 157717ab2063SBarry Smith MatSetOption_SeqAIJ,MatZeroEntries_SeqAIJ,MatZeroRows_SeqAIJ, 157817ab2063SBarry Smith MatLUFactorSymbolic_SeqAIJ,MatLUFactorNumeric_SeqAIJ,0,0, 157917ab2063SBarry Smith MatGetSize_SeqAIJ,MatGetSize_SeqAIJ,MatGetOwnershipRange_SeqAIJ, 158017ab2063SBarry Smith MatILUFactorSymbolic_SeqAIJ,0, 158194a9d846SBarry Smith 0,0, 15823d1612f7SBarry Smith MatConvertSameType_SeqAIJ,0,0, 1583cddf8d76SBarry Smith MatILUFactor_SeqAIJ,0,0, 15847eb43aa7SLois Curfman McInnes MatGetSubMatrices_SeqAIJ,MatIncreaseOverlap_SeqAIJ, 1585682d7d0cSBarry Smith MatGetValues_SeqAIJ,0, 1586f0b747eeSBarry Smith MatPrintHelp_SeqAIJ, 15875a838052SSatish Balay MatScale_SeqAIJ,0,0, 15886945ee14SBarry Smith MatILUDTFactor_SeqAIJ, 15896945ee14SBarry Smith MatGetBlockSize_SeqAIJ, 15903b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 15913b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 15923b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 1593a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 1594a93ec695SBarry Smith MatFDColoringCreate_SeqAIJ, 15950513a670SBarry Smith MatColoringPatch_SeqAIJ, 15960513a670SBarry Smith 0, 15970513a670SBarry Smith MatPermute_SeqAIJ}; 159817ab2063SBarry Smith 159917ab2063SBarry Smith extern int MatUseSuperLU_SeqAIJ(Mat); 160017ab2063SBarry Smith extern int MatUseEssl_SeqAIJ(Mat); 160117ab2063SBarry Smith extern int MatUseDXML_SeqAIJ(Mat); 160217ab2063SBarry Smith 16035615d1e5SSatish Balay #undef __FUNC__ 16045615d1e5SSatish Balay #define __FUNC__ "MatCreateSeqAIJ" 160517ab2063SBarry Smith /*@C 1606682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 16070d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 16086e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 16092bd5e0b2SLois Curfman McInnes (or the array nzz). By setting these parameters accurately, performance 16102bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 161117ab2063SBarry Smith 161217ab2063SBarry Smith Input Parameters: 1613029af93fSBarry Smith . comm - MPI communicator, set to PETSC_COMM_SELF 161417ab2063SBarry Smith . m - number of rows 161517ab2063SBarry Smith . n - number of columns 161617ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 16172bd5e0b2SLois Curfman McInnes . nzz - array containing the number of nonzeros in the various rows 16182bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 161917ab2063SBarry Smith 162017ab2063SBarry Smith Output Parameter: 1621416022c9SBarry Smith . A - the matrix 162217ab2063SBarry Smith 162317ab2063SBarry Smith Notes: 162417ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 162517ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 16260002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 162744cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 162817ab2063SBarry Smith 162917ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 1630a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 16313d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 16326da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 163317ab2063SBarry Smith 1634682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 1635682d7d0cSBarry Smith improve numerical efficiency of Matrix vector products and solves. We 1636682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 16376c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 16386c7ebb05SLois Curfman McInnes 16396c7ebb05SLois Curfman McInnes Options Database Keys: 16406c7ebb05SLois Curfman McInnes $ -mat_aij_no_inode - Do not use inodes 16416e62573dSLois Curfman McInnes $ -mat_aij_inode_limit <limit> - Set inode limit. 16426e62573dSLois Curfman McInnes $ (max limit=5) 16436e62573dSLois Curfman McInnes $ -mat_aij_oneindex - Internally use indexing starting at 1 16446e62573dSLois Curfman McInnes $ rather than 0. Note: When calling MatSetValues(), 16456e62573dSLois Curfman McInnes $ the user still MUST index entries starting at 0! 164617ab2063SBarry Smith 164717ab2063SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues() 164817ab2063SBarry Smith @*/ 1649416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz, Mat *A) 165017ab2063SBarry Smith { 1651416022c9SBarry Smith Mat B; 1652416022c9SBarry Smith Mat_SeqAIJ *b; 16536945ee14SBarry Smith int i, len, ierr, flg,size; 16546945ee14SBarry Smith 16556945ee14SBarry Smith MPI_Comm_size(comm,&size); 1656e3372554SBarry Smith if (size > 1) SETERRQ(1,0,"Comm must be of size 1"); 1657d5d45c9bSBarry Smith 1658416022c9SBarry Smith *A = 0; 1659f09e8eb9SSatish Balay PetscHeaderCreate(B,_p_Mat,MAT_COOKIE,MATSEQAIJ,comm); 1660416022c9SBarry Smith PLogObjectCreate(B); 16610452661fSBarry Smith B->data = (void *) (b = PetscNew(Mat_SeqAIJ)); CHKPTRQ(b); 166244cd7ae7SLois Curfman McInnes PetscMemzero(b,sizeof(Mat_SeqAIJ)); 1663416022c9SBarry Smith PetscMemcpy(&B->ops,&MatOps,sizeof(struct _MatOps)); 1664416022c9SBarry Smith B->destroy = MatDestroy_SeqAIJ; 1665416022c9SBarry Smith B->view = MatView_SeqAIJ; 1666416022c9SBarry Smith B->factor = 0; 1667416022c9SBarry Smith B->lupivotthreshold = 1.0; 166890f02eecSBarry Smith B->mapping = 0; 16697a743949SBarry Smith ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold, 167069957df2SSatish Balay &flg); CHKERRQ(ierr); 16717a743949SBarry Smith b->ilu_preserve_row_sums = PETSC_FALSE; 16727a743949SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums", 16737a743949SBarry Smith (int*) &b->ilu_preserve_row_sums); CHKERRQ(ierr); 1674416022c9SBarry Smith b->row = 0; 1675416022c9SBarry Smith b->col = 0; 1676416022c9SBarry Smith b->indexshift = 0; 1677b810aeb4SBarry Smith b->reallocs = 0; 167869957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_oneindex", &flg); CHKERRQ(ierr); 167969957df2SSatish Balay if (flg) b->indexshift = -1; 168017ab2063SBarry Smith 168144cd7ae7SLois Curfman McInnes b->m = m; B->m = m; B->M = m; 168244cd7ae7SLois Curfman McInnes b->n = n; B->n = n; B->N = n; 16830452661fSBarry Smith b->imax = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(b->imax); 1684b4fd4287SBarry Smith if (nnz == PETSC_NULL) { 16857b8455f0SLois Curfman McInnes if (nz == PETSC_DEFAULT) nz = 10; 16867b8455f0SLois Curfman McInnes else if (nz <= 0) nz = 1; 1687416022c9SBarry Smith for ( i=0; i<m; i++ ) b->imax[i] = nz; 168817ab2063SBarry Smith nz = nz*m; 168917ab2063SBarry Smith } 169017ab2063SBarry Smith else { 169117ab2063SBarry Smith nz = 0; 1692416022c9SBarry Smith for ( i=0; i<m; i++ ) {b->imax[i] = nnz[i]; nz += nnz[i];} 169317ab2063SBarry Smith } 169417ab2063SBarry Smith 169517ab2063SBarry Smith /* allocate the matrix space */ 1696416022c9SBarry Smith len = nz*(sizeof(int) + sizeof(Scalar)) + (b->m+1)*sizeof(int); 16970452661fSBarry Smith b->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(b->a); 1698416022c9SBarry Smith b->j = (int *) (b->a + nz); 1699cddf8d76SBarry Smith PetscMemzero(b->j,nz*sizeof(int)); 1700416022c9SBarry Smith b->i = b->j + nz; 1701416022c9SBarry Smith b->singlemalloc = 1; 170217ab2063SBarry Smith 1703416022c9SBarry Smith b->i[0] = -b->indexshift; 170417ab2063SBarry Smith for (i=1; i<m+1; i++) { 1705416022c9SBarry Smith b->i[i] = b->i[i-1] + b->imax[i-1]; 170617ab2063SBarry Smith } 170717ab2063SBarry Smith 1708416022c9SBarry Smith /* b->ilen will count nonzeros in each row so far. */ 17090452661fSBarry Smith b->ilen = (int *) PetscMalloc((m+1)*sizeof(int)); 1710f09e8eb9SSatish Balay PLogObjectMemory(B,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 1711416022c9SBarry Smith for ( i=0; i<b->m; i++ ) { b->ilen[i] = 0;} 171217ab2063SBarry Smith 1713416022c9SBarry Smith b->nz = 0; 1714416022c9SBarry Smith b->maxnz = nz; 1715416022c9SBarry Smith b->sorted = 0; 1716416022c9SBarry Smith b->roworiented = 1; 1717416022c9SBarry Smith b->nonew = 0; 1718416022c9SBarry Smith b->diag = 0; 1719416022c9SBarry Smith b->solve_work = 0; 172071bd300dSLois Curfman McInnes b->spptr = 0; 1721754ec7b1SSatish Balay b->inode.node_count = 0; 1722754ec7b1SSatish Balay b->inode.size = 0; 17236c7ebb05SLois Curfman McInnes b->inode.limit = 5; 17246c7ebb05SLois Curfman McInnes b->inode.max_limit = 5; 17254e220ebcSLois Curfman McInnes B->info.nz_unneeded = (double)b->maxnz; 172617ab2063SBarry Smith 1727416022c9SBarry Smith *A = B; 17284e220ebcSLois Curfman McInnes 17294b14c69eSBarry Smith /* SuperLU is not currently supported through PETSc */ 17304b14c69eSBarry Smith #if defined(HAVE_SUPERLU) 173169957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_superlu", &flg); CHKERRQ(ierr); 173269957df2SSatish Balay if (flg) { ierr = MatUseSuperLU_SeqAIJ(B); CHKERRQ(ierr); } 17334b14c69eSBarry Smith #endif 173469957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_essl", &flg); CHKERRQ(ierr); 173569957df2SSatish Balay if (flg) { ierr = MatUseEssl_SeqAIJ(B); CHKERRQ(ierr); } 173669957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-mat_aij_dxml", &flg); CHKERRQ(ierr); 173769957df2SSatish Balay if (flg) { 1738e3372554SBarry Smith if (!b->indexshift) SETERRQ(1,0,"need -mat_aij_oneindex with -mat_aij_dxml"); 1739416022c9SBarry Smith ierr = MatUseDXML_SeqAIJ(B); CHKERRQ(ierr); 174017ab2063SBarry Smith } 174169957df2SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr); 174269957df2SSatish Balay if (flg) {ierr = MatPrintHelp(B); CHKERRQ(ierr); } 174317ab2063SBarry Smith return 0; 174417ab2063SBarry Smith } 174517ab2063SBarry Smith 17465615d1e5SSatish Balay #undef __FUNC__ 17475615d1e5SSatish Balay #define __FUNC__ "MatConvertSameType_SeqAIJ" 17483d1612f7SBarry Smith int MatConvertSameType_SeqAIJ(Mat A,Mat *B,int cpvalues) 174917ab2063SBarry Smith { 1750416022c9SBarry Smith Mat C; 1751416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ *) A->data; 175208480c60SBarry Smith int i,len, m = a->m,shift = a->indexshift; 175317ab2063SBarry Smith 17544043dd9cSLois Curfman McInnes *B = 0; 1755f09e8eb9SSatish Balay PetscHeaderCreate(C,_p_Mat,MAT_COOKIE,MATSEQAIJ,A->comm); 1756416022c9SBarry Smith PLogObjectCreate(C); 17570452661fSBarry Smith C->data = (void *) (c = PetscNew(Mat_SeqAIJ)); CHKPTRQ(c); 175841c01911SSatish Balay PetscMemcpy(&C->ops,&A->ops,sizeof(struct _MatOps)); 1759416022c9SBarry Smith C->destroy = MatDestroy_SeqAIJ; 1760416022c9SBarry Smith C->view = MatView_SeqAIJ; 1761416022c9SBarry Smith C->factor = A->factor; 1762416022c9SBarry Smith c->row = 0; 1763416022c9SBarry Smith c->col = 0; 1764416022c9SBarry Smith c->indexshift = shift; 1765c456f294SBarry Smith C->assembled = PETSC_TRUE; 176617ab2063SBarry Smith 176744cd7ae7SLois Curfman McInnes c->m = C->m = a->m; 176844cd7ae7SLois Curfman McInnes c->n = C->n = a->n; 176944cd7ae7SLois Curfman McInnes C->M = a->m; 177044cd7ae7SLois Curfman McInnes C->N = a->n; 177117ab2063SBarry Smith 17720452661fSBarry Smith c->imax = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->imax); 17730452661fSBarry Smith c->ilen = (int *) PetscMalloc((m+1)*sizeof(int)); CHKPTRQ(c->ilen); 177417ab2063SBarry Smith for ( i=0; i<m; i++ ) { 1775416022c9SBarry Smith c->imax[i] = a->imax[i]; 1776416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 177717ab2063SBarry Smith } 177817ab2063SBarry Smith 177917ab2063SBarry Smith /* allocate the matrix space */ 1780416022c9SBarry Smith c->singlemalloc = 1; 1781416022c9SBarry Smith len = (m+1)*sizeof(int)+(a->i[m])*(sizeof(Scalar)+sizeof(int)); 17820452661fSBarry Smith c->a = (Scalar *) PetscMalloc( len ); CHKPTRQ(c->a); 1783416022c9SBarry Smith c->j = (int *) (c->a + a->i[m] + shift); 1784416022c9SBarry Smith c->i = c->j + a->i[m] + shift; 1785416022c9SBarry Smith PetscMemcpy(c->i,a->i,(m+1)*sizeof(int)); 178617ab2063SBarry Smith if (m > 0) { 1787416022c9SBarry Smith PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int)); 178808480c60SBarry Smith if (cpvalues == COPY_VALUES) { 1789416022c9SBarry Smith PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(Scalar)); 179017ab2063SBarry Smith } 179108480c60SBarry Smith } 179217ab2063SBarry Smith 1793f09e8eb9SSatish Balay PLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 1794416022c9SBarry Smith c->sorted = a->sorted; 1795416022c9SBarry Smith c->roworiented = a->roworiented; 1796416022c9SBarry Smith c->nonew = a->nonew; 17977a743949SBarry Smith c->ilu_preserve_row_sums = a->ilu_preserve_row_sums; 179817ab2063SBarry Smith 1799416022c9SBarry Smith if (a->diag) { 18000452661fSBarry Smith c->diag = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->diag); 1801416022c9SBarry Smith PLogObjectMemory(C,(m+1)*sizeof(int)); 180217ab2063SBarry Smith for ( i=0; i<m; i++ ) { 1803416022c9SBarry Smith c->diag[i] = a->diag[i]; 180417ab2063SBarry Smith } 180517ab2063SBarry Smith } 1806416022c9SBarry Smith else c->diag = 0; 18076c7ebb05SLois Curfman McInnes c->inode.limit = a->inode.limit; 18086c7ebb05SLois Curfman McInnes c->inode.max_limit = a->inode.max_limit; 1809754ec7b1SSatish Balay if (a->inode.size){ 1810daed632aSSatish Balay c->inode.size = (int *) PetscMalloc( (m+1)*sizeof(int) ); CHKPTRQ(c->inode.size); 1811754ec7b1SSatish Balay c->inode.node_count = a->inode.node_count; 1812daed632aSSatish Balay PetscMemcpy( c->inode.size, a->inode.size, (m+1)*sizeof(int)); 1813754ec7b1SSatish Balay } else { 1814754ec7b1SSatish Balay c->inode.size = 0; 1815754ec7b1SSatish Balay c->inode.node_count = 0; 1816754ec7b1SSatish Balay } 1817416022c9SBarry Smith c->nz = a->nz; 1818416022c9SBarry Smith c->maxnz = a->maxnz; 1819416022c9SBarry Smith c->solve_work = 0; 182076dd722bSSatish Balay c->spptr = 0; /* Dangerous -I'm throwing away a->spptr */ 1821754ec7b1SSatish Balay 1822416022c9SBarry Smith *B = C; 182317ab2063SBarry Smith return 0; 182417ab2063SBarry Smith } 182517ab2063SBarry Smith 18265615d1e5SSatish Balay #undef __FUNC__ 18275615d1e5SSatish Balay #define __FUNC__ "MatLoad_SeqAIJ" 182819bcc07fSBarry Smith int MatLoad_SeqAIJ(Viewer viewer,MatType type,Mat *A) 182917ab2063SBarry Smith { 1830416022c9SBarry Smith Mat_SeqAIJ *a; 1831416022c9SBarry Smith Mat B; 183217699dbbSLois Curfman McInnes int i, nz, ierr, fd, header[4],size,*rowlengths = 0,M,N,shift; 1833bcd2baecSBarry Smith MPI_Comm comm; 183417ab2063SBarry Smith 183519bcc07fSBarry Smith PetscObjectGetComm((PetscObject) viewer,&comm); 183617699dbbSLois Curfman McInnes MPI_Comm_size(comm,&size); 1837e3372554SBarry Smith if (size > 1) SETERRQ(1,0,"view must have one processor"); 183890ace30eSBarry Smith ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr); 183977c4ece6SBarry Smith ierr = PetscBinaryRead(fd,header,4,BINARY_INT); CHKERRQ(ierr); 1840e3372554SBarry Smith if (header[0] != MAT_COOKIE) SETERRQ(1,0,"not matrix object in file"); 184117ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 184217ab2063SBarry Smith 184317ab2063SBarry Smith /* read in row lengths */ 18440452661fSBarry Smith rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths); 184577c4ece6SBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,BINARY_INT); CHKERRQ(ierr); 184617ab2063SBarry Smith 184717ab2063SBarry Smith /* create our matrix */ 1848416022c9SBarry Smith ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A); CHKERRQ(ierr); 1849416022c9SBarry Smith B = *A; 1850416022c9SBarry Smith a = (Mat_SeqAIJ *) B->data; 1851416022c9SBarry Smith shift = a->indexshift; 185217ab2063SBarry Smith 185317ab2063SBarry Smith /* read in column indices and adjust for Fortran indexing*/ 185477c4ece6SBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,BINARY_INT); CHKERRQ(ierr); 185517ab2063SBarry Smith if (shift) { 185617ab2063SBarry Smith for ( i=0; i<nz; i++ ) { 1857416022c9SBarry Smith a->j[i] += 1; 185817ab2063SBarry Smith } 185917ab2063SBarry Smith } 186017ab2063SBarry Smith 186117ab2063SBarry Smith /* read in nonzero values */ 186277c4ece6SBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,BINARY_SCALAR); CHKERRQ(ierr); 186317ab2063SBarry Smith 186417ab2063SBarry Smith /* set matrix "i" values */ 1865416022c9SBarry Smith a->i[0] = -shift; 186617ab2063SBarry Smith for ( i=1; i<= M; i++ ) { 1867416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 1868416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 186917ab2063SBarry Smith } 18700452661fSBarry Smith PetscFree(rowlengths); 187117ab2063SBarry Smith 18726d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 18736d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 187417ab2063SBarry Smith return 0; 187517ab2063SBarry Smith } 187617ab2063SBarry Smith 18775615d1e5SSatish Balay #undef __FUNC__ 18785615d1e5SSatish Balay #define __FUNC__ "MatEqual_SeqAIJ" 18798f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B, PetscTruth* flg) 18807264ac53SSatish Balay { 18817264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data; 18827264ac53SSatish Balay 1883e3372554SBarry Smith if (B->type !=MATSEQAIJ)SETERRQ(1,0,"Matrices must be same type"); 18847264ac53SSatish Balay 18857264ac53SSatish Balay /* If the matrix dimensions are not equal, or no of nonzeros or shift */ 18867264ac53SSatish Balay if ((a->m != b->m ) || (a->n !=b->n) ||( a->nz != b->nz)|| 1887bcd2baecSBarry Smith (a->indexshift != b->indexshift)) { 188877c4ece6SBarry Smith *flg = PETSC_FALSE; return 0; 1889bcd2baecSBarry Smith } 18907264ac53SSatish Balay 18917264ac53SSatish Balay /* if the a->i are the same */ 1892bcd2baecSBarry Smith if (PetscMemcmp(a->i,b->i, (a->n+1)*sizeof(int))) { 189377c4ece6SBarry Smith *flg = PETSC_FALSE; return 0; 18947264ac53SSatish Balay } 18957264ac53SSatish Balay 18967264ac53SSatish Balay /* if a->j are the same */ 1897bcd2baecSBarry Smith if (PetscMemcmp(a->j, b->j, (a->nz)*sizeof(int))) { 189877c4ece6SBarry Smith *flg = PETSC_FALSE; return 0; 1899bcd2baecSBarry Smith } 1900bcd2baecSBarry Smith 1901bcd2baecSBarry Smith /* if a->a are the same */ 190219bcc07fSBarry Smith if (PetscMemcmp(a->a, b->a, (a->nz)*sizeof(Scalar))) { 190377c4ece6SBarry Smith *flg = PETSC_FALSE; return 0; 19047264ac53SSatish Balay } 190577c4ece6SBarry Smith *flg = PETSC_TRUE; 19067264ac53SSatish Balay return 0; 19077264ac53SSatish Balay 19087264ac53SSatish Balay } 1909