173f4d377SMatthew Knepley /*$Id: aij.c,v 1.385 2001/09/07 20:09:22 bsmith Exp $*/ 2d5d45c9bSBarry Smith /* 33369ce9aSBarry Smith Defines the basic matrix operations for the AIJ (compressed row) 4d5d45c9bSBarry Smith matrix storage format. 5d5d45c9bSBarry Smith */ 63369ce9aSBarry Smith 79e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h" /*I "petscmat.h" I*/ 8f5eb4b81SSatish Balay #include "src/vec/vecimpl.h" 9f5eb4b81SSatish Balay #include "src/inline/spops.h" 108d195f9aSBarry Smith #include "src/inline/dot.h" 110a835dfdSSatish Balay #include "petscbt.h" 1217ab2063SBarry Smith 13a2ce50c7SBarry Smith 14ca44d042SBarry Smith EXTERN int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**); 1517ab2063SBarry Smith 164a2ae208SSatish Balay #undef __FUNCT__ 174a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ" 1836db0b34SBarry Smith int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja,PetscTruth *done) 1917ab2063SBarry Smith { 20416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 216945ee14SBarry Smith int ierr,i,ishift; 2217ab2063SBarry Smith 233a40ed3dSBarry Smith PetscFunctionBegin; 2431625ec5SSatish Balay *m = A->m; 253a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 266945ee14SBarry Smith ishift = a->indexshift; 2753e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 28273d9f13SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 296945ee14SBarry Smith } else if (oshift == 0 && ishift == -1) { 30435da068SBarry Smith int nz = a->i[A->m] - 1; 313b2fbd54SBarry Smith /* malloc space and subtract 1 from i and j indices */ 32b0a32e0cSBarry Smith ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr); 33b0a32e0cSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr); 343b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] - 1; 35273d9f13SBarry Smith for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] - 1; 366945ee14SBarry Smith } else if (oshift == 1 && ishift == 0) { 37435da068SBarry Smith int nz = a->i[A->m]; 383b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 391bc30dd5SBarry Smith ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr); 40b0a32e0cSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr); 413b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1; 42273d9f13SBarry Smith for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1; 436945ee14SBarry Smith } else { 446945ee14SBarry Smith *ia = a->i; *ja = a->j; 45a2ce50c7SBarry Smith } 463a40ed3dSBarry Smith PetscFunctionReturn(0); 47a2744918SBarry Smith } 48a2744918SBarry Smith 494a2ae208SSatish Balay #undef __FUNCT__ 504a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ" 5136db0b34SBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done) 526945ee14SBarry Smith { 536945ee14SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 54606d414cSSatish Balay int ishift = a->indexshift,ierr; 556945ee14SBarry Smith 563a40ed3dSBarry Smith PetscFunctionBegin; 573a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 5853e63a63SBarry Smith if ((symmetric && !A->structurally_symmetric) || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) { 59606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 60606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 61bcd2baecSBarry Smith } 623a40ed3dSBarry Smith PetscFunctionReturn(0); 6317ab2063SBarry Smith } 6417ab2063SBarry Smith 654a2ae208SSatish Balay #undef __FUNCT__ 664a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ" 6736db0b34SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,PetscTruth *done) 683b2fbd54SBarry Smith { 693b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 70a93ec695SBarry Smith int ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m; 71a93ec695SBarry Smith int nz = a->i[m]+ishift,row,*jj,mr,col; 723b2fbd54SBarry Smith 733a40ed3dSBarry Smith PetscFunctionBegin; 743b2fbd54SBarry Smith *nn = A->n; 753a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 763b2fbd54SBarry Smith if (symmetric) { 77273d9f13SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 783b2fbd54SBarry Smith } else { 79b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr); 80549d3d68SSatish Balay ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr); 81b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr); 82b0a32e0cSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr); 833b2fbd54SBarry Smith jj = a->j; 843b2fbd54SBarry Smith for (i=0; i<nz; i++) { 853b2fbd54SBarry Smith collengths[jj[i] + ishift]++; 863b2fbd54SBarry Smith } 873b2fbd54SBarry Smith cia[0] = oshift; 883b2fbd54SBarry Smith for (i=0; i<n; i++) { 893b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 903b2fbd54SBarry Smith } 91549d3d68SSatish Balay ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr); 923b2fbd54SBarry Smith jj = a->j; 93a93ec695SBarry Smith for (row=0; row<m; row++) { 94a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 95a93ec695SBarry Smith for (i=0; i<mr; i++) { 963b2fbd54SBarry Smith col = *jj++ + ishift; 973b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 983b2fbd54SBarry Smith } 993b2fbd54SBarry Smith } 100606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 1013b2fbd54SBarry Smith *ia = cia; *ja = cja; 1023b2fbd54SBarry Smith } 1033a40ed3dSBarry Smith PetscFunctionReturn(0); 1043b2fbd54SBarry Smith } 1053b2fbd54SBarry Smith 1064a2ae208SSatish Balay #undef __FUNCT__ 1074a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ" 10836db0b34SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done) 1093b2fbd54SBarry Smith { 110606d414cSSatish Balay int ierr; 111606d414cSSatish Balay 1123a40ed3dSBarry Smith PetscFunctionBegin; 1133a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1143b2fbd54SBarry Smith 115606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 116606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 1173b2fbd54SBarry Smith 1183a40ed3dSBarry Smith PetscFunctionReturn(0); 1193b2fbd54SBarry Smith } 1203b2fbd54SBarry Smith 121227d817aSBarry Smith #define CHUNKSIZE 15 12217ab2063SBarry Smith 1234a2ae208SSatish Balay #undef __FUNCT__ 1244a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ" 12587828ca2SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,PetscScalar *v,InsertMode is) 12617ab2063SBarry Smith { 127416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 128416022c9SBarry Smith int *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted; 129273d9f13SBarry Smith int *imax = a->imax,*ai = a->i,*ailen = a->ilen; 130549d3d68SSatish Balay int *aj = a->j,nonew = a->nonew,shift = a->indexshift,ierr; 13187828ca2SBarry Smith PetscScalar *ap,value,*aa = a->a; 13236db0b34SBarry Smith PetscTruth ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE); 133273d9f13SBarry Smith PetscTruth roworiented = a->roworiented; 13417ab2063SBarry Smith 1353a40ed3dSBarry Smith PetscFunctionBegin; 13617ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 137416022c9SBarry Smith row = im[k]; 1385ef9f2a5SBarry Smith if (row < 0) continue; 139aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 140273d9f13SBarry Smith if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m); 1413b2fbd54SBarry Smith #endif 14217ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 14317ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 144416022c9SBarry Smith low = 0; 14517ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 1465ef9f2a5SBarry Smith if (in[l] < 0) continue; 147aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 148273d9f13SBarry Smith if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n); 1493b2fbd54SBarry Smith #endif 1504b0e389bSBarry Smith col = in[l] - shift; 1514b0e389bSBarry Smith if (roworiented) { 1525ef9f2a5SBarry Smith value = v[l + k*n]; 153bef8e0ddSBarry Smith } else { 1544b0e389bSBarry Smith value = v[k + l*m]; 1554b0e389bSBarry Smith } 15636db0b34SBarry Smith if (value == 0.0 && ignorezeroentries) continue; 15736db0b34SBarry Smith 158416022c9SBarry Smith if (!sorted) low = 0; high = nrow; 159416022c9SBarry Smith while (high-low > 5) { 160416022c9SBarry Smith t = (low+high)/2; 161416022c9SBarry Smith if (rp[t] > col) high = t; 162416022c9SBarry Smith else low = t; 16317ab2063SBarry Smith } 164416022c9SBarry Smith for (i=low; i<high; i++) { 16517ab2063SBarry Smith if (rp[i] > col) break; 16617ab2063SBarry Smith if (rp[i] == col) { 167416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 16817ab2063SBarry Smith else ap[i] = value; 16917ab2063SBarry Smith goto noinsert; 17017ab2063SBarry Smith } 17117ab2063SBarry Smith } 172c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 17329bbc08cSBarry Smith else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col); 17417ab2063SBarry Smith if (nrow >= rmax) { 17517ab2063SBarry Smith /* there is no extra room in row, therefore enlarge */ 176*a7ed9263SMatthew Knepley int new_nz = ai[A->m] + CHUNKSIZE,*new_i,*new_j; 177*a7ed9263SMatthew Knepley size_t len; 17887828ca2SBarry Smith PetscScalar *new_a; 17917ab2063SBarry Smith 18029bbc08cSBarry Smith if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col); 18196854ed6SLois Curfman McInnes 18217ab2063SBarry Smith /* malloc new storage space */ 183*a7ed9263SMatthew Knepley len = ((size_t) new_nz)*(sizeof(int)+sizeof(PetscScalar))+(A->m+1)*sizeof(int); 184b0a32e0cSBarry Smith ierr = PetscMalloc(len,&new_a);CHKERRQ(ierr); 18517ab2063SBarry Smith new_j = (int*)(new_a + new_nz); 18617ab2063SBarry Smith new_i = new_j + new_nz; 18717ab2063SBarry Smith 18817ab2063SBarry Smith /* copy over old data into new slots */ 18917ab2063SBarry Smith for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} 190273d9f13SBarry Smith for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} 191549d3d68SSatish Balay ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); 192*a7ed9263SMatthew Knepley len = (((size_t) new_nz) - CHUNKSIZE - ai[row] - nrow - shift); 193549d3d68SSatish Balay ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,len*sizeof(int));CHKERRQ(ierr); 194*a7ed9263SMatthew Knepley ierr = PetscMemcpy(new_a,aa,(((size_t) ai[row])+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); 19587828ca2SBarry Smith ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,len*sizeof(PetscScalar));CHKERRQ(ierr); 19617ab2063SBarry Smith /* free up old matrix storage */ 197606d414cSSatish Balay ierr = PetscFree(a->a);CHKERRQ(ierr); 198606d414cSSatish Balay if (!a->singlemalloc) { 199606d414cSSatish Balay ierr = PetscFree(a->i);CHKERRQ(ierr); 200606d414cSSatish Balay ierr = PetscFree(a->j);CHKERRQ(ierr); 201606d414cSSatish Balay } 202416022c9SBarry Smith aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; 203f1e2ffcdSBarry Smith a->singlemalloc = PETSC_TRUE; 20417ab2063SBarry Smith 20517ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 206416022c9SBarry Smith rmax = imax[row] = imax[row] + CHUNKSIZE; 20787828ca2SBarry Smith PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); 208416022c9SBarry Smith a->maxnz += CHUNKSIZE; 209b810aeb4SBarry Smith a->reallocs++; 21017ab2063SBarry Smith } 211416022c9SBarry Smith N = nrow++ - 1; a->nz++; 212416022c9SBarry Smith /* shift up all the later entries in this row */ 213416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 21417ab2063SBarry Smith rp[ii+1] = rp[ii]; 21517ab2063SBarry Smith ap[ii+1] = ap[ii]; 21617ab2063SBarry Smith } 21717ab2063SBarry Smith rp[i] = col; 21817ab2063SBarry Smith ap[i] = value; 21917ab2063SBarry Smith noinsert:; 220416022c9SBarry Smith low = i + 1; 22117ab2063SBarry Smith } 22217ab2063SBarry Smith ailen[row] = nrow; 22317ab2063SBarry Smith } 2243a40ed3dSBarry Smith PetscFunctionReturn(0); 22517ab2063SBarry Smith } 22617ab2063SBarry Smith 2274a2ae208SSatish Balay #undef __FUNCT__ 2284a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ" 22987828ca2SBarry Smith int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,PetscScalar *v) 2307eb43aa7SLois Curfman McInnes { 2317eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 232b49de8d1SLois Curfman McInnes int *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 2337eb43aa7SLois Curfman McInnes int *ai = a->i,*ailen = a->ilen,shift = a->indexshift; 23487828ca2SBarry Smith PetscScalar *ap,*aa = a->a,zero = 0.0; 2357eb43aa7SLois Curfman McInnes 2363a40ed3dSBarry Smith PetscFunctionBegin; 2377eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 2387eb43aa7SLois Curfman McInnes row = im[k]; 23929bbc08cSBarry Smith if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",row); 240273d9f13SBarry Smith if (row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: %d",row); 2417eb43aa7SLois Curfman McInnes rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 2427eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2437eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 24429bbc08cSBarry Smith if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",in[l]); 245273d9f13SBarry Smith if (in[l] >= A->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: %d",in[l]); 2467eb43aa7SLois Curfman McInnes col = in[l] - shift; 2477eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2487eb43aa7SLois Curfman McInnes while (high-low > 5) { 2497eb43aa7SLois Curfman McInnes t = (low+high)/2; 2507eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2517eb43aa7SLois Curfman McInnes else low = t; 2527eb43aa7SLois Curfman McInnes } 2537eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 2547eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2557eb43aa7SLois Curfman McInnes if (rp[i] == col) { 256b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2577eb43aa7SLois Curfman McInnes goto finished; 2587eb43aa7SLois Curfman McInnes } 2597eb43aa7SLois Curfman McInnes } 260b49de8d1SLois Curfman McInnes *v++ = zero; 2617eb43aa7SLois Curfman McInnes finished:; 2627eb43aa7SLois Curfman McInnes } 2637eb43aa7SLois Curfman McInnes } 2643a40ed3dSBarry Smith PetscFunctionReturn(0); 2657eb43aa7SLois Curfman McInnes } 2667eb43aa7SLois Curfman McInnes 26717ab2063SBarry Smith 2684a2ae208SSatish Balay #undef __FUNCT__ 2694a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary" 270b0a32e0cSBarry Smith int MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 27117ab2063SBarry Smith { 272416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 273416022c9SBarry Smith int i,fd,*col_lens,ierr; 27417ab2063SBarry Smith 2753a40ed3dSBarry Smith PetscFunctionBegin; 276b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 277b0a32e0cSBarry Smith ierr = PetscMalloc((4+A->m)*sizeof(int),&col_lens);CHKERRQ(ierr); 278552e946dSBarry Smith col_lens[0] = MAT_FILE_COOKIE; 279273d9f13SBarry Smith col_lens[1] = A->m; 280273d9f13SBarry Smith col_lens[2] = A->n; 281416022c9SBarry Smith col_lens[3] = a->nz; 282416022c9SBarry Smith 283416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 284273d9f13SBarry Smith for (i=0; i<A->m; i++) { 285416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 28617ab2063SBarry Smith } 287273d9f13SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,1);CHKERRQ(ierr); 288606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 289416022c9SBarry Smith 290416022c9SBarry Smith /* store column indices (zero start index) */ 291416022c9SBarry Smith if (a->indexshift) { 292416022c9SBarry Smith for (i=0; i<a->nz; i++) a->j[i]--; 29317ab2063SBarry Smith } 2940752156aSBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr); 295416022c9SBarry Smith if (a->indexshift) { 296416022c9SBarry Smith for (i=0; i<a->nz; i++) a->j[i]++; 29717ab2063SBarry Smith } 298416022c9SBarry Smith 299416022c9SBarry Smith /* store nonzero values */ 3000752156aSBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr); 3013a40ed3dSBarry Smith PetscFunctionReturn(0); 30217ab2063SBarry Smith } 303416022c9SBarry Smith 304cd155464SBarry Smith extern int MatMPIAIJFactorInfo_SuperLu(Mat,PetscViewer); 305a072f7f7SHong Zhang extern int MatFactorInfo_Spooles(Mat,PetscViewer); 306cd155464SBarry Smith 3074a2ae208SSatish Balay #undef __FUNCT__ 3084a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII" 309b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 310416022c9SBarry Smith { 311416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 312fb9695e5SSatish Balay int ierr,i,j,m = A->m,shift = a->indexshift; 313fb9695e5SSatish Balay char *name; 314f3ef73ceSBarry Smith PetscViewerFormat format; 31517ab2063SBarry Smith 3163a40ed3dSBarry Smith PetscFunctionBegin; 317435da068SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 318b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 319fb9695e5SSatish Balay if (format == PETSC_VIEWER_ASCII_INFO_LONG || format == PETSC_VIEWER_ASCII_INFO) { 3206831982aSBarry Smith if (a->inode.size) { 321b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"using I-node routines: found %d nodes, limit used is %d\n",a->inode.node_count,a->inode.limit);CHKERRQ(ierr); 3226831982aSBarry Smith } else { 323b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr); 3246831982aSBarry Smith } 325fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_MATLAB) { 326d00d2cf4SBarry Smith int nofinalvalue = 0; 327273d9f13SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) { 328d00d2cf4SBarry Smith nofinalvalue = 1; 329d00d2cf4SBarry Smith } 330b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 331b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr); 332b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr); 333b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 334b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 33517ab2063SBarry Smith 33617ab2063SBarry Smith for (i=0; i<m; i++) { 337416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 338aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 339b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%d %d %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 34017ab2063SBarry Smith #else 341b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%d %d %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr); 34217ab2063SBarry Smith #endif 34317ab2063SBarry Smith } 34417ab2063SBarry Smith } 345d00d2cf4SBarry Smith if (nofinalvalue) { 346b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%d %d %18.16e\n",m,A->n,0.0);CHKERRQ(ierr); 347d00d2cf4SBarry Smith } 348fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 349b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 350cd155464SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 351cd155464SBarry Smith #if defined(PETSC_HAVE_SUPERLUDIST) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX) 352cd155464SBarry Smith ierr = MatMPIAIJFactorInfo_SuperLu(A,viewer);CHKERRQ(ierr); 353cd155464SBarry Smith #endif 354174d842dSHong Zhang #if defined(PETSC_HAVE_SPOOLES) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_COMPLEX) 355a072f7f7SHong Zhang ierr = MatFactorInfo_Spooles(A,viewer);CHKERRQ(ierr); 356174d842dSHong Zhang #endif 357cd155464SBarry Smith PetscFunctionReturn(0); 358fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 359b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 36044cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 361b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr); 36244cd7ae7SLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 363aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 36436db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 36562b941d6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 36636db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 36762b941d6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 36836db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 36962b941d6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 3706831982aSBarry Smith } 37144cd7ae7SLois Curfman McInnes #else 37262b941d6SBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);} 37344cd7ae7SLois Curfman McInnes #endif 37444cd7ae7SLois Curfman McInnes } 375b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 37644cd7ae7SLois Curfman McInnes } 377b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 378fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 379496be53dSLois Curfman McInnes int nzd=0,fshift=1,*sptr; 380b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 381b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr); 382496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 383496be53dSLois Curfman McInnes sptr[i] = nzd+1; 384496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 385496be53dSLois Curfman McInnes if (a->j[j] >= i) { 386aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 38736db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 388496be53dSLois Curfman McInnes #else 389496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 390496be53dSLois Curfman McInnes #endif 391496be53dSLois Curfman McInnes } 392496be53dSLois Curfman McInnes } 393496be53dSLois Curfman McInnes } 3942e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 395b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr); 3962e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 397b0a32e0cSBarry Smith if (i+4<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);} 398b0a32e0cSBarry Smith else if (i+3<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);} 399b0a32e0cSBarry Smith else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);} 400b0a32e0cSBarry Smith else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);} 401b0a32e0cSBarry Smith else if (i<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);} 402b0a32e0cSBarry Smith else {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);} 403496be53dSLois Curfman McInnes } 404b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 405606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 406496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 407496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 408b0a32e0cSBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);} 409496be53dSLois Curfman McInnes } 410b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 411496be53dSLois Curfman McInnes } 412b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 413496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 414496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 415496be53dSLois Curfman McInnes if (a->j[j] >= i) { 416aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 41736db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 418b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4196831982aSBarry Smith } 420496be53dSLois Curfman McInnes #else 421b0a32e0cSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);} 422496be53dSLois Curfman McInnes #endif 423496be53dSLois Curfman McInnes } 424496be53dSLois Curfman McInnes } 425b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 426496be53dSLois Curfman McInnes } 427b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 428fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 42902594712SBarry Smith int cnt = 0,jcnt; 43087828ca2SBarry Smith PetscScalar value; 43102594712SBarry Smith 432b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 43302594712SBarry Smith for (i=0; i<m; i++) { 43402594712SBarry Smith jcnt = 0; 435273d9f13SBarry Smith for (j=0; j<A->n; j++) { 436e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 43702594712SBarry Smith value = a->a[cnt++]; 438e24b481bSBarry Smith jcnt++; 43902594712SBarry Smith } else { 44002594712SBarry Smith value = 0.0; 44102594712SBarry Smith } 442aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 443b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr); 44402594712SBarry Smith #else 445b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr); 44602594712SBarry Smith #endif 44702594712SBarry Smith } 448b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 44902594712SBarry Smith } 450b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 4513a40ed3dSBarry Smith } else { 452b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 45317ab2063SBarry Smith for (i=0; i<m; i++) { 454b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr); 455416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 456aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 45736db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 45862b941d6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%d, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 45936db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 46062b941d6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%d, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4613a40ed3dSBarry Smith } else { 46262b941d6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 46317ab2063SBarry Smith } 46417ab2063SBarry Smith #else 46562b941d6SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%d, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 46617ab2063SBarry Smith #endif 46717ab2063SBarry Smith } 468b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 46917ab2063SBarry Smith } 470b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 47117ab2063SBarry Smith } 472b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 4733a40ed3dSBarry Smith PetscFunctionReturn(0); 474416022c9SBarry Smith } 475416022c9SBarry Smith 4764a2ae208SSatish Balay #undef __FUNCT__ 4774a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom" 478b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 479416022c9SBarry Smith { 480480ef9eaSBarry Smith Mat A = (Mat) Aa; 481416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 482273d9f13SBarry Smith int ierr,i,j,m = A->m,shift = a->indexshift,color; 48336db0b34SBarry Smith PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0; 484b0a32e0cSBarry Smith PetscViewer viewer; 485f3ef73ceSBarry Smith PetscViewerFormat format; 486cddf8d76SBarry Smith 4873a40ed3dSBarry Smith PetscFunctionBegin; 488480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 489b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 49019bcc07fSBarry Smith 491b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 492416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 4930513a670SBarry Smith 494fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 4950513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 496b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 497416022c9SBarry Smith for (i=0; i<m; i++) { 498cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 499416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 500cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 501aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 50236db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 503cddf8d76SBarry Smith #else 504cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 505cddf8d76SBarry Smith #endif 506b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 507cddf8d76SBarry Smith } 508cddf8d76SBarry Smith } 509b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 510cddf8d76SBarry Smith for (i=0; i<m; i++) { 511cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 512cddf8d76SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 513cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 514cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 515b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 516cddf8d76SBarry Smith } 517cddf8d76SBarry Smith } 518b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 519cddf8d76SBarry Smith for (i=0; i<m; i++) { 520cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 521cddf8d76SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 522cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 523aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 52436db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 525cddf8d76SBarry Smith #else 526cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 527cddf8d76SBarry Smith #endif 528b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 529416022c9SBarry Smith } 530416022c9SBarry Smith } 5310513a670SBarry Smith } else { 5320513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 5330513a670SBarry Smith /* first determine max of all nonzero values */ 5340513a670SBarry Smith int nz = a->nz,count; 535b0a32e0cSBarry Smith PetscDraw popup; 53636db0b34SBarry Smith PetscReal scale; 5370513a670SBarry Smith 5380513a670SBarry Smith for (i=0; i<nz; i++) { 5390513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 5400513a670SBarry Smith } 541b0a32e0cSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv; 542b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 543b0a32e0cSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);} 5440513a670SBarry Smith count = 0; 5450513a670SBarry Smith for (i=0; i<m; i++) { 5460513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 5470513a670SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 5480513a670SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 549b0a32e0cSBarry Smith color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count])); 550b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 5510513a670SBarry Smith count++; 5520513a670SBarry Smith } 5530513a670SBarry Smith } 5540513a670SBarry Smith } 555480ef9eaSBarry Smith PetscFunctionReturn(0); 556480ef9eaSBarry Smith } 557cddf8d76SBarry Smith 5584a2ae208SSatish Balay #undef __FUNCT__ 5594a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw" 560b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 561480ef9eaSBarry Smith { 562480ef9eaSBarry Smith int ierr; 563b0a32e0cSBarry Smith PetscDraw draw; 56436db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 565480ef9eaSBarry Smith PetscTruth isnull; 566480ef9eaSBarry Smith 567480ef9eaSBarry Smith PetscFunctionBegin; 568b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 569b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 570480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 571480ef9eaSBarry Smith 572480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 573273d9f13SBarry Smith xr = A->n; yr = A->m; h = yr/10.0; w = xr/10.0; 574480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 575b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 576b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 577480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr); 5783a40ed3dSBarry Smith PetscFunctionReturn(0); 579416022c9SBarry Smith } 580416022c9SBarry Smith 5814a2ae208SSatish Balay #undef __FUNCT__ 5824a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ" 583b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer) 584416022c9SBarry Smith { 585416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 586bcd2baecSBarry Smith int ierr; 5876831982aSBarry Smith PetscTruth issocket,isascii,isbinary,isdraw; 588416022c9SBarry Smith 5893a40ed3dSBarry Smith PetscFunctionBegin; 590b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr); 591b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 592fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr); 593fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr); 5940f5bd95cSBarry Smith if (issocket) { 59501820c62SBarry Smith if (a->indexshift) { 59629bbc08cSBarry Smith SETERRQ(1,"Can only socket send sparse matrix with 0 based indexing"); 59701820c62SBarry Smith } 598b0a32e0cSBarry Smith ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr); 5990f5bd95cSBarry Smith } else if (isascii) { 6003a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 6010f5bd95cSBarry Smith } else if (isbinary) { 6023a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 6030f5bd95cSBarry Smith } else if (isdraw) { 6043a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 6055cd90555SBarry Smith } else { 60629bbc08cSBarry Smith SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name); 60717ab2063SBarry Smith } 6083a40ed3dSBarry Smith PetscFunctionReturn(0); 60917ab2063SBarry Smith } 61019bcc07fSBarry Smith 6113a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth); 6129b9367d5SHong Zhang EXTERN int MatUseSuperLU_DIST_MPIAIJ(Mat); 613cfef3cccSHong Zhang EXTERN int MatUseSpooles_SeqAIJ(Mat); 6144a2ae208SSatish Balay #undef __FUNCT__ 6154a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ" 6168f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 61717ab2063SBarry Smith { 618416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 61941c01911SSatish Balay int fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr; 620273d9f13SBarry Smith int m = A->m,*ip,N,*ailen = a->ilen,shift = a->indexshift,rmax = 0; 62187828ca2SBarry Smith PetscScalar *aa = a->a,*ap; 622cfef3cccSHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) || defined(PETSC_HAVE_SPOOLES) 6239b9367d5SHong Zhang PetscTruth flag; 6249e070d67SMatthew Knepley #endif 62517ab2063SBarry Smith 6263a40ed3dSBarry Smith PetscFunctionBegin; 6273a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 62817ab2063SBarry Smith 62943ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 63017ab2063SBarry Smith for (i=1; i<m; i++) { 631416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 63217ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 63394a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 63417ab2063SBarry Smith if (fshift) { 6350f5bd95cSBarry Smith ip = aj + ai[i] + shift; 6360f5bd95cSBarry Smith ap = aa + ai[i] + shift; 63717ab2063SBarry Smith N = ailen[i]; 63817ab2063SBarry Smith for (j=0; j<N; j++) { 63917ab2063SBarry Smith ip[j-fshift] = ip[j]; 64017ab2063SBarry Smith ap[j-fshift] = ap[j]; 64117ab2063SBarry Smith } 64217ab2063SBarry Smith } 64317ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 64417ab2063SBarry Smith } 64517ab2063SBarry Smith if (m) { 64617ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 64717ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 64817ab2063SBarry Smith } 64917ab2063SBarry Smith /* reset ilen and imax for each row */ 65017ab2063SBarry Smith for (i=0; i<m; i++) { 65117ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 65217ab2063SBarry Smith } 653416022c9SBarry Smith a->nz = ai[m] + shift; 65417ab2063SBarry Smith 65517ab2063SBarry Smith /* diagonals may have moved, so kill the diagonal pointers */ 656416022c9SBarry Smith if (fshift && a->diag) { 657606d414cSSatish Balay ierr = PetscFree(a->diag);CHKERRQ(ierr); 658b0a32e0cSBarry Smith PetscLogObjectMemory(A,-(m+1)*sizeof(int)); 659416022c9SBarry Smith a->diag = 0; 66017ab2063SBarry Smith } 661b0a32e0cSBarry Smith PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz); 662b0a32e0cSBarry Smith PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs); 663b0a32e0cSBarry Smith PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax); 664dd5f02e7SSatish Balay a->reallocs = 0; 6654e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 66636db0b34SBarry Smith a->rmax = rmax; 6674e220ebcSLois Curfman McInnes 66876dd722bSSatish Balay /* check out for identical nodes. If found, use inode functions */ 6693a7fca6bSBarry Smith ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr); 670cfef3cccSHong Zhang 6719b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) 6729b9367d5SHong Zhang ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr); 6739b9367d5SHong Zhang if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(A);CHKERRQ(ierr); } 6749b9367d5SHong Zhang #endif 6759b9367d5SHong Zhang 676cfef3cccSHong Zhang #if defined(PETSC_HAVE_SPOOLES) 677cfef3cccSHong Zhang ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_spooles",&flag);CHKERRQ(ierr); 678cfef3cccSHong Zhang if (flag) { ierr = MatUseSpooles_SeqAIJ(A);CHKERRQ(ierr); } 679cfef3cccSHong Zhang #endif 680cfef3cccSHong Zhang 6813a40ed3dSBarry Smith PetscFunctionReturn(0); 68217ab2063SBarry Smith } 68317ab2063SBarry Smith 6844a2ae208SSatish Balay #undef __FUNCT__ 6854a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ" 6868f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A) 68717ab2063SBarry Smith { 688416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 689549d3d68SSatish Balay int ierr; 6903a40ed3dSBarry Smith 6913a40ed3dSBarry Smith PetscFunctionBegin; 69287828ca2SBarry Smith ierr = PetscMemzero(a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr); 6933a40ed3dSBarry Smith PetscFunctionReturn(0); 69417ab2063SBarry Smith } 695416022c9SBarry Smith 6964a2ae208SSatish Balay #undef __FUNCT__ 6974a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ" 698e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A) 69917ab2063SBarry Smith { 700416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 70182bf6240SBarry Smith int ierr; 702d5d45c9bSBarry Smith 7033a40ed3dSBarry Smith PetscFunctionBegin; 704aa482453SBarry Smith #if defined(PETSC_USE_LOG) 705b0a32e0cSBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz); 70617ab2063SBarry Smith #endif 70736db0b34SBarry Smith if (a->freedata) { 708606d414cSSatish Balay ierr = PetscFree(a->a);CHKERRQ(ierr); 709606d414cSSatish Balay if (!a->singlemalloc) { 710606d414cSSatish Balay ierr = PetscFree(a->i);CHKERRQ(ierr); 711606d414cSSatish Balay ierr = PetscFree(a->j);CHKERRQ(ierr); 712606d414cSSatish Balay } 71336db0b34SBarry Smith } 714c38d4ed2SBarry Smith if (a->row) { 715c38d4ed2SBarry Smith ierr = ISDestroy(a->row);CHKERRQ(ierr); 716c38d4ed2SBarry Smith } 717c38d4ed2SBarry Smith if (a->col) { 718c38d4ed2SBarry Smith ierr = ISDestroy(a->col);CHKERRQ(ierr); 719c38d4ed2SBarry Smith } 720606d414cSSatish Balay if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);} 721606d414cSSatish Balay if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);} 722606d414cSSatish Balay if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);} 723273d9f13SBarry Smith if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);} 724606d414cSSatish Balay if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);} 725606d414cSSatish Balay if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);} 72682bf6240SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} 727606d414cSSatish Balay if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);} 728cc8ba8e1SBarry Smith if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);} 729606d414cSSatish Balay ierr = PetscFree(a);CHKERRQ(ierr); 7303a40ed3dSBarry Smith PetscFunctionReturn(0); 73117ab2063SBarry Smith } 73217ab2063SBarry Smith 7334a2ae208SSatish Balay #undef __FUNCT__ 7344a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ" 7358f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A) 73617ab2063SBarry Smith { 7373a40ed3dSBarry Smith PetscFunctionBegin; 7383a40ed3dSBarry Smith PetscFunctionReturn(0); 73917ab2063SBarry Smith } 74017ab2063SBarry Smith 7414a2ae208SSatish Balay #undef __FUNCT__ 7424a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ" 7438f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op) 74417ab2063SBarry Smith { 745416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 7463a40ed3dSBarry Smith 7473a40ed3dSBarry Smith PetscFunctionBegin; 748a65d3064SKris Buschelman switch (op) { 749a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 750a65d3064SKris Buschelman a->roworiented = PETSC_TRUE; 751a65d3064SKris Buschelman break; 752a65d3064SKris Buschelman case MAT_KEEP_ZEROED_ROWS: 753a65d3064SKris Buschelman a->keepzeroedrows = PETSC_TRUE; 754a65d3064SKris Buschelman break; 755a65d3064SKris Buschelman case MAT_COLUMN_ORIENTED: 756a65d3064SKris Buschelman a->roworiented = PETSC_FALSE; 757a65d3064SKris Buschelman break; 758a65d3064SKris Buschelman case MAT_COLUMNS_SORTED: 759a65d3064SKris Buschelman a->sorted = PETSC_TRUE; 760a65d3064SKris Buschelman break; 761a65d3064SKris Buschelman case MAT_COLUMNS_UNSORTED: 762a65d3064SKris Buschelman a->sorted = PETSC_FALSE; 763a65d3064SKris Buschelman break; 764a65d3064SKris Buschelman case MAT_NO_NEW_NONZERO_LOCATIONS: 765a65d3064SKris Buschelman a->nonew = 1; 766a65d3064SKris Buschelman break; 767a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 768a65d3064SKris Buschelman a->nonew = -1; 769a65d3064SKris Buschelman break; 770a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 771a65d3064SKris Buschelman a->nonew = -2; 772a65d3064SKris Buschelman break; 773a65d3064SKris Buschelman case MAT_YES_NEW_NONZERO_LOCATIONS: 774a65d3064SKris Buschelman a->nonew = 0; 775a65d3064SKris Buschelman break; 776a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 777a65d3064SKris Buschelman a->ignorezeroentries = PETSC_TRUE; 778a65d3064SKris Buschelman break; 779a65d3064SKris Buschelman case MAT_USE_INODES: 780a65d3064SKris Buschelman a->inode.use = PETSC_TRUE; 781a65d3064SKris Buschelman break; 782a65d3064SKris Buschelman case MAT_DO_NOT_USE_INODES: 783a65d3064SKris Buschelman a->inode.use = PETSC_FALSE; 784a65d3064SKris Buschelman break; 785a65d3064SKris Buschelman case MAT_ROWS_SORTED: 786a65d3064SKris Buschelman case MAT_ROWS_UNSORTED: 787a65d3064SKris Buschelman case MAT_YES_NEW_DIAGONALS: 788a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 789a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 790d03495bdSKris Buschelman case MAT_USE_SINGLE_PRECISION_SOLVES: 791b0a32e0cSBarry Smith PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n"); 792a65d3064SKris Buschelman break; 793a65d3064SKris Buschelman case MAT_NO_NEW_DIAGONALS: 79429bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS"); 795a65d3064SKris Buschelman case MAT_INODE_LIMIT_1: 796a65d3064SKris Buschelman a->inode.limit = 1; 797a65d3064SKris Buschelman break; 798a65d3064SKris Buschelman case MAT_INODE_LIMIT_2: 799a65d3064SKris Buschelman a->inode.limit = 2; 800a65d3064SKris Buschelman break; 801a65d3064SKris Buschelman case MAT_INODE_LIMIT_3: 802a65d3064SKris Buschelman a->inode.limit = 3; 803a65d3064SKris Buschelman break; 804a65d3064SKris Buschelman case MAT_INODE_LIMIT_4: 805a65d3064SKris Buschelman a->inode.limit = 4; 806a65d3064SKris Buschelman break; 807a65d3064SKris Buschelman case MAT_INODE_LIMIT_5: 808a65d3064SKris Buschelman a->inode.limit = 5; 809a65d3064SKris Buschelman break; 810a65d3064SKris Buschelman default: 811a65d3064SKris Buschelman SETERRQ(PETSC_ERR_SUP,"unknown option"); 812a65d3064SKris Buschelman } 8133a40ed3dSBarry Smith PetscFunctionReturn(0); 81417ab2063SBarry Smith } 81517ab2063SBarry Smith 8164a2ae208SSatish Balay #undef __FUNCT__ 8174a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ" 8188f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v) 81917ab2063SBarry Smith { 820416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8213a40ed3dSBarry Smith int i,j,n,shift = a->indexshift,ierr; 82287828ca2SBarry Smith PetscScalar *x,zero = 0.0; 82317ab2063SBarry Smith 8243a40ed3dSBarry Smith PetscFunctionBegin; 8253a40ed3dSBarry Smith ierr = VecSet(&zero,v);CHKERRQ(ierr); 82636db0b34SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 82736db0b34SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 828273d9f13SBarry Smith if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 829273d9f13SBarry Smith for (i=0; i<A->m; i++) { 830416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 831416022c9SBarry Smith if (a->j[j]+shift == i) { 832416022c9SBarry Smith x[i] = a->a[j]; 83317ab2063SBarry Smith break; 83417ab2063SBarry Smith } 83517ab2063SBarry Smith } 83617ab2063SBarry Smith } 83736db0b34SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 8383a40ed3dSBarry Smith PetscFunctionReturn(0); 83917ab2063SBarry Smith } 84017ab2063SBarry Smith 841d5d45c9bSBarry Smith 8424a2ae208SSatish Balay #undef __FUNCT__ 8434a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ" 8447c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 84517ab2063SBarry Smith { 846416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8475c897100SBarry Smith PetscScalar *x,*y; 8485c897100SBarry Smith int ierr,m = A->m,shift = a->indexshift; 8495c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 8505c897100SBarry Smith PetscScalar *v,alpha; 8515c897100SBarry Smith int n,i,*idx; 8525c897100SBarry Smith #endif 85317ab2063SBarry Smith 8543a40ed3dSBarry Smith PetscFunctionBegin; 8552e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 856e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 857e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 85817ab2063SBarry Smith y = y + shift; /* shift for Fortran start by 1 indexing */ 8595c897100SBarry Smith 8605c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 8615c897100SBarry Smith fortranmulttransposeaddaij_(&m,x,a->i,a->j+shift,a->a+shift,y); 8625c897100SBarry Smith #else 86317ab2063SBarry Smith for (i=0; i<m; i++) { 864416022c9SBarry Smith idx = a->j + a->i[i] + shift; 865416022c9SBarry Smith v = a->a + a->i[i] + shift; 866416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 86717ab2063SBarry Smith alpha = x[i]; 86817ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 86917ab2063SBarry Smith } 8705c897100SBarry Smith #endif 871b0a32e0cSBarry Smith PetscLogFlops(2*a->nz); 872e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 873e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 8743a40ed3dSBarry Smith PetscFunctionReturn(0); 87517ab2063SBarry Smith } 87617ab2063SBarry Smith 8774a2ae208SSatish Balay #undef __FUNCT__ 8785c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ" 8795c897100SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 8805c897100SBarry Smith { 8818d5b0100SBarry Smith PetscScalar zero = 0.0; 8825c897100SBarry Smith int ierr; 8835c897100SBarry Smith 8845c897100SBarry Smith PetscFunctionBegin; 8855c897100SBarry Smith ierr = VecSet(&zero,yy);CHKERRQ(ierr); 8865c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 8875c897100SBarry Smith PetscFunctionReturn(0); 8885c897100SBarry Smith } 8895c897100SBarry Smith 8905c897100SBarry Smith 8915c897100SBarry Smith #undef __FUNCT__ 8924a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ" 89344cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 89417ab2063SBarry Smith { 895416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 896362ced78SSatish Balay PetscScalar *x,*y,*v; 897273d9f13SBarry Smith int ierr,m = A->m,*idx,shift = a->indexshift,*ii; 898aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 899e36a17ebSSatish Balay int n,i,jrow,j; 900362ced78SSatish Balay PetscScalar sum; 901e36a17ebSSatish Balay #endif 90217ab2063SBarry Smith 903b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 904fee21e36SBarry Smith #pragma disjoint(*x,*y,*v) 905fee21e36SBarry Smith #endif 906fee21e36SBarry Smith 9073a40ed3dSBarry Smith PetscFunctionBegin; 908e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 909e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 91017ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 911416022c9SBarry Smith idx = a->j; 912d64ed03dSBarry Smith v = a->a; 913416022c9SBarry Smith ii = a->i; 914aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 91529b5ca8aSSatish Balay fortranmultaij_(&m,x,ii,idx+shift,v+shift,y); 9168d195f9aSBarry Smith #else 917d64ed03dSBarry Smith v += shift; /* shift for Fortran start by 1 indexing */ 918d64ed03dSBarry Smith idx += shift; 91917ab2063SBarry Smith for (i=0; i<m; i++) { 9209ea0dfa2SSatish Balay jrow = ii[i]; 9219ea0dfa2SSatish Balay n = ii[i+1] - jrow; 92217ab2063SBarry Smith sum = 0.0; 9239ea0dfa2SSatish Balay for (j=0; j<n; j++) { 9249ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 9259ea0dfa2SSatish Balay } 92617ab2063SBarry Smith y[i] = sum; 92717ab2063SBarry Smith } 9288d195f9aSBarry Smith #endif 929b0a32e0cSBarry Smith PetscLogFlops(2*a->nz - m); 930e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 931e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9323a40ed3dSBarry Smith PetscFunctionReturn(0); 93317ab2063SBarry Smith } 93417ab2063SBarry Smith 9354a2ae208SSatish Balay #undef __FUNCT__ 9364a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ" 93744cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 93817ab2063SBarry Smith { 939416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 940362ced78SSatish Balay PetscScalar *x,*y,*z,*v; 941273d9f13SBarry Smith int ierr,m = A->m,*idx,shift = a->indexshift,*ii; 942aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 943e36a17ebSSatish Balay int n,i,jrow,j; 944362ced78SSatish Balay PetscScalar sum; 945e36a17ebSSatish Balay #endif 9469ea0dfa2SSatish Balay 9473a40ed3dSBarry Smith PetscFunctionBegin; 948e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 949e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 9502e8a6d31SBarry Smith if (zz != yy) { 951e1311b90SBarry Smith ierr = VecGetArray(zz,&z);CHKERRQ(ierr); 9522e8a6d31SBarry Smith } else { 9532e8a6d31SBarry Smith z = y; 9542e8a6d31SBarry Smith } 95517ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 956cddf8d76SBarry Smith idx = a->j; 957d64ed03dSBarry Smith v = a->a; 958cddf8d76SBarry Smith ii = a->i; 959aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 96029b5ca8aSSatish Balay fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z); 96102ab625aSSatish Balay #else 962d64ed03dSBarry Smith v += shift; /* shift for Fortran start by 1 indexing */ 963d64ed03dSBarry Smith idx += shift; 96417ab2063SBarry Smith for (i=0; i<m; i++) { 9659ea0dfa2SSatish Balay jrow = ii[i]; 9669ea0dfa2SSatish Balay n = ii[i+1] - jrow; 96717ab2063SBarry Smith sum = y[i]; 9689ea0dfa2SSatish Balay for (j=0; j<n; j++) { 9699ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 9709ea0dfa2SSatish Balay } 97117ab2063SBarry Smith z[i] = sum; 97217ab2063SBarry Smith } 97302ab625aSSatish Balay #endif 974b0a32e0cSBarry Smith PetscLogFlops(2*a->nz); 975e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 976e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9772e8a6d31SBarry Smith if (zz != yy) { 978e1311b90SBarry Smith ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr); 9792e8a6d31SBarry Smith } 9803a40ed3dSBarry Smith PetscFunctionReturn(0); 98117ab2063SBarry Smith } 98217ab2063SBarry Smith 98317ab2063SBarry Smith /* 98417ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 98517ab2063SBarry Smith */ 9864a2ae208SSatish Balay #undef __FUNCT__ 9874a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ" 988f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A) 98917ab2063SBarry Smith { 990416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 991b0a32e0cSBarry Smith int i,j,*diag,m = A->m,shift = a->indexshift,ierr; 99217ab2063SBarry Smith 9933a40ed3dSBarry Smith PetscFunctionBegin; 994f1e2ffcdSBarry Smith if (a->diag) PetscFunctionReturn(0); 995f1e2ffcdSBarry Smith 996b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr); 997b0a32e0cSBarry Smith PetscLogObjectMemory(A,(m+1)*sizeof(int)); 998273d9f13SBarry Smith for (i=0; i<A->m; i++) { 99935b0346bSBarry Smith diag[i] = a->i[i+1]; 1000416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 1001416022c9SBarry Smith if (a->j[j]+shift == i) { 100217ab2063SBarry Smith diag[i] = j - shift; 100317ab2063SBarry Smith break; 100417ab2063SBarry Smith } 100517ab2063SBarry Smith } 100617ab2063SBarry Smith } 1007416022c9SBarry Smith a->diag = diag; 10083a40ed3dSBarry Smith PetscFunctionReturn(0); 100917ab2063SBarry Smith } 101017ab2063SBarry Smith 1011be5855fcSBarry Smith /* 1012be5855fcSBarry Smith Checks for missing diagonals 1013be5855fcSBarry Smith */ 10144a2ae208SSatish Balay #undef __FUNCT__ 10154a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ" 1016f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A) 1017be5855fcSBarry Smith { 1018be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1019f1e2ffcdSBarry Smith int *diag,*jj = a->j,i,shift = a->indexshift,ierr; 1020be5855fcSBarry Smith 1021be5855fcSBarry Smith PetscFunctionBegin; 1022f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1023f1e2ffcdSBarry Smith diag = a->diag; 1024273d9f13SBarry Smith for (i=0; i<A->m; i++) { 1025be5855fcSBarry Smith if (jj[diag[i]+shift] != i-shift) { 102629bbc08cSBarry Smith SETERRQ1(1,"Matrix is missing diagonal number %d",i); 1027be5855fcSBarry Smith } 1028be5855fcSBarry Smith } 1029be5855fcSBarry Smith PetscFunctionReturn(0); 1030be5855fcSBarry Smith } 1031be5855fcSBarry Smith 10324a2ae208SSatish Balay #undef __FUNCT__ 10334a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ" 1034c14dc6b6SHong Zhang int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx) 103517ab2063SBarry Smith { 1036416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 103787828ca2SBarry Smith PetscScalar *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0; 1038273d9f13SBarry Smith int ierr,*idx,*diag,n = A->n,m = A->m,i,shift = a->indexshift; 103917ab2063SBarry Smith 10403a40ed3dSBarry Smith PetscFunctionBegin; 1041b965ef7fSBarry Smith its = its*lits; 104291723122SBarry Smith if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits); 104391723122SBarry Smith 1044e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1045fb2e594dSBarry Smith if (xx != bb) { 1046e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 1047fb2e594dSBarry Smith } else { 1048fb2e594dSBarry Smith b = x; 1049fb2e594dSBarry Smith } 1050fb2e594dSBarry Smith 1051f1e2ffcdSBarry Smith if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);} 1052416022c9SBarry Smith diag = a->diag; 1053416022c9SBarry Smith xs = x + shift; /* shifted by one for index start of a or a->j*/ 105417ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 105517ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 105617ab2063SBarry Smith bs = b + shift; 105717ab2063SBarry Smith for (i=0; i<m; i++) { 1058416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1059416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1060b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1061416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1062416022c9SBarry Smith v = a->a + diag[i] + (!shift); 106317ab2063SBarry Smith sum = b[i]*d/omega; 106417ab2063SBarry Smith SPARSEDENSEDOT(sum,bs,v,idx,n); 106517ab2063SBarry Smith x[i] = sum; 106617ab2063SBarry Smith } 1067cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1068fb2e594dSBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 10693a40ed3dSBarry Smith PetscFunctionReturn(0); 107017ab2063SBarry Smith } 1071c783ea89SBarry Smith 1072c783ea89SBarry Smith /* setup workspace for Eisenstat */ 1073c783ea89SBarry Smith if (flag & SOR_EISENSTAT) { 1074c783ea89SBarry Smith if (!a->idiag) { 107587828ca2SBarry Smith ierr = PetscMalloc(2*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr); 1076c783ea89SBarry Smith a->ssor = a->idiag + m; 1077c783ea89SBarry Smith v = a->a; 1078c783ea89SBarry Smith for (i=0; i<m; i++) { a->idiag[i] = 1.0/v[diag[i]];} 1079c783ea89SBarry Smith } 1080c783ea89SBarry Smith t = a->ssor; 1081c783ea89SBarry Smith idiag = a->idiag; 1082c783ea89SBarry Smith } 1083fc3d8934SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 1084fc3d8934SBarry Smith U is upper triangular, E is diagonal; This routine applies 1085fc3d8934SBarry Smith 1086fc3d8934SBarry Smith (L + E)^{-1} A (U + E)^{-1} 1087fc3d8934SBarry Smith 1088fc3d8934SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 1089fc3d8934SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 109048af12d7SBarry Smith is the relaxation factor. 1091fc3d8934SBarry Smith */ 1092fc3d8934SBarry Smith 109348af12d7SBarry Smith if (flag == SOR_APPLY_LOWER) { 109429bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 109548af12d7SBarry Smith } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) { 109648af12d7SBarry Smith /* special case for omega = 1.0 saves flops and some integer ops */ 109787828ca2SBarry Smith PetscScalar *v2; 109848af12d7SBarry Smith 1099733d66baSBarry Smith v2 = a->a; 1100fc3d8934SBarry Smith /* x = (E + U)^{-1} b */ 1101fc3d8934SBarry Smith for (i=m-1; i>=0; i--) { 1102fc3d8934SBarry Smith n = a->i[i+1] - diag[i] - 1; 1103fc3d8934SBarry Smith idx = a->j + diag[i] + 1; 1104fc3d8934SBarry Smith v = a->a + diag[i] + 1; 1105fc3d8934SBarry Smith sum = b[i]; 1106fc3d8934SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1107b4632166SBarry Smith x[i] = sum*idiag[i]; 1108fc3d8934SBarry Smith 1109fc3d8934SBarry Smith /* t = b - (2*E - D)x */ 1110733d66baSBarry Smith t[i] = b[i] - (v2[diag[i]])*x[i]; 1111733d66baSBarry Smith } 1112fc3d8934SBarry Smith 1113fc3d8934SBarry Smith /* t = (E + L)^{-1}t */ 1114fc3d8934SBarry Smith diag = a->diag; 1115fc3d8934SBarry Smith for (i=0; i<m; i++) { 1116fc3d8934SBarry Smith n = diag[i] - a->i[i]; 1117fc3d8934SBarry Smith idx = a->j + a->i[i]; 1118fc3d8934SBarry Smith v = a->a + a->i[i]; 1119fc3d8934SBarry Smith sum = t[i]; 1120fc3d8934SBarry Smith SPARSEDENSEMDOT(sum,t,v,idx,n); 1121b4632166SBarry Smith t[i] = sum*idiag[i]; 1122fc3d8934SBarry Smith 1123fc3d8934SBarry Smith /* x = x + t */ 1124733d66baSBarry Smith x[i] += t[i]; 1125733d66baSBarry Smith } 1126733d66baSBarry Smith 1127b0a32e0cSBarry Smith PetscLogFlops(3*m-1 + 2*a->nz); 1128fc3d8934SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1129fc3d8934SBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 1130fc3d8934SBarry Smith PetscFunctionReturn(0); 11313a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 113217ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 113317ab2063SBarry Smith U is upper triangular, E is diagonal; This routine applies 113417ab2063SBarry Smith 113517ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 113617ab2063SBarry Smith 113717ab2063SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 113817ab2063SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 113917ab2063SBarry Smith is the relaxation factor. 114017ab2063SBarry Smith */ 114117ab2063SBarry Smith scale = (2.0/omega) - 1.0; 114217ab2063SBarry Smith 114317ab2063SBarry Smith /* x = (E + U)^{-1} b */ 114417ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1145416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1146416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1147416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1148416022c9SBarry Smith v = a->a + diag[i] + (!shift); 114917ab2063SBarry Smith sum = b[i]; 115017ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 115117ab2063SBarry Smith x[i] = omega*(sum/d); 115217ab2063SBarry Smith } 115317ab2063SBarry Smith 115417ab2063SBarry Smith /* t = b - (2*E - D)x */ 1155416022c9SBarry Smith v = a->a; 115617ab2063SBarry Smith for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; } 115717ab2063SBarry Smith 115817ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1159416022c9SBarry Smith ts = t + shift; /* shifted by one for index start of a or a->j*/ 1160416022c9SBarry Smith diag = a->diag; 116117ab2063SBarry Smith for (i=0; i<m; i++) { 1162416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1163416022c9SBarry Smith n = diag[i] - a->i[i]; 1164416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1165416022c9SBarry Smith v = a->a + a->i[i] + shift; 116617ab2063SBarry Smith sum = t[i]; 116717ab2063SBarry Smith SPARSEDENSEMDOT(sum,ts,v,idx,n); 116817ab2063SBarry Smith t[i] = omega*(sum/d); 1169733d66baSBarry Smith /* x = x + t */ 1170733d66baSBarry Smith x[i] += t[i]; 117117ab2063SBarry Smith } 117217ab2063SBarry Smith 1173b0a32e0cSBarry Smith PetscLogFlops(6*m-1 + 2*a->nz); 1174cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1175fb2e594dSBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 11763a40ed3dSBarry Smith PetscFunctionReturn(0); 117717ab2063SBarry Smith } 117817ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 117917ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 118077d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 118177d8c4bbSBarry Smith fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,b); 118277d8c4bbSBarry Smith #else 118317ab2063SBarry Smith for (i=0; i<m; i++) { 1184416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1185416022c9SBarry Smith n = diag[i] - a->i[i]; 1186b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1187416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1188416022c9SBarry Smith v = a->a + a->i[i] + shift; 118917ab2063SBarry Smith sum = b[i]; 119017ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 119117ab2063SBarry Smith x[i] = omega*(sum/d); 119217ab2063SBarry Smith } 119377d8c4bbSBarry Smith #endif 119417ab2063SBarry Smith xb = x; 11953a40ed3dSBarry Smith } else xb = b; 119617ab2063SBarry Smith if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) && 119717ab2063SBarry Smith (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) { 119817ab2063SBarry Smith for (i=0; i<m; i++) { 1199416022c9SBarry Smith x[i] *= a->a[diag[i]+shift]; 120017ab2063SBarry Smith } 1201b0a32e0cSBarry Smith PetscLogFlops(m); 120217ab2063SBarry Smith } 120317ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 120477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 120577d8c4bbSBarry Smith fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,xb); 120677d8c4bbSBarry Smith #else 120717ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1208416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1209416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1210b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1211416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1212416022c9SBarry Smith v = a->a + diag[i] + (!shift); 121317ab2063SBarry Smith sum = xb[i]; 121417ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 121517ab2063SBarry Smith x[i] = omega*(sum/d); 121617ab2063SBarry Smith } 121777d8c4bbSBarry Smith #endif 121817ab2063SBarry Smith } 121917ab2063SBarry Smith its--; 122017ab2063SBarry Smith } 122117ab2063SBarry Smith while (its--) { 122217ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 122377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 122477d8c4bbSBarry Smith fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,diag,a->a,b); 122577d8c4bbSBarry Smith #else 122617ab2063SBarry Smith for (i=0; i<m; i++) { 1227416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1228416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1229b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1230416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1231416022c9SBarry Smith v = a->a + a->i[i] + shift; 123217ab2063SBarry Smith sum = b[i]; 123317ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 12347e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 123517ab2063SBarry Smith } 123677d8c4bbSBarry Smith #endif 123717ab2063SBarry Smith } 123817ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 123977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 124077d8c4bbSBarry Smith fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,diag,a->a,b); 124177d8c4bbSBarry Smith #else 124217ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1243416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1244416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1245b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1246416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1247416022c9SBarry Smith v = a->a + a->i[i] + shift; 124817ab2063SBarry Smith sum = b[i]; 124917ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 12507e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 125117ab2063SBarry Smith } 125277d8c4bbSBarry Smith #endif 125317ab2063SBarry Smith } 125417ab2063SBarry Smith } 1255cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1256fb2e594dSBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 12573a40ed3dSBarry Smith PetscFunctionReturn(0); 125817ab2063SBarry Smith } 125917ab2063SBarry Smith 12604a2ae208SSatish Balay #undef __FUNCT__ 12614a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ" 12628f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 126317ab2063SBarry Smith { 1264416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12654e220ebcSLois Curfman McInnes 12663a40ed3dSBarry Smith PetscFunctionBegin; 1267273d9f13SBarry Smith info->rows_global = (double)A->m; 1268273d9f13SBarry Smith info->columns_global = (double)A->n; 1269273d9f13SBarry Smith info->rows_local = (double)A->m; 1270273d9f13SBarry Smith info->columns_local = (double)A->n; 12714e220ebcSLois Curfman McInnes info->block_size = 1.0; 12724e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 12734e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 12744e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 12754e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 12764e220ebcSLois Curfman McInnes info->mallocs = (double)a->reallocs; 12774e220ebcSLois Curfman McInnes info->memory = A->mem; 12784e220ebcSLois Curfman McInnes if (A->factor) { 12794e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 12804e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 12814e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 12824e220ebcSLois Curfman McInnes } else { 12834e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 12844e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 12854e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 12864e220ebcSLois Curfman McInnes } 12873a40ed3dSBarry Smith PetscFunctionReturn(0); 128817ab2063SBarry Smith } 128917ab2063SBarry Smith 1290b9b97703SBarry Smith EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatLUInfo*,Mat*); 1291ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*); 1292b9b97703SBarry Smith EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatLUInfo*); 1293ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec); 1294ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec); 1295ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec); 1296ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec); 129717ab2063SBarry Smith 12984a2ae208SSatish Balay #undef __FUNCT__ 12994a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ" 130087828ca2SBarry Smith int MatZeroRows_SeqAIJ(Mat A,IS is,PetscScalar *diag) 130117ab2063SBarry Smith { 1302416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1303273d9f13SBarry Smith int i,ierr,N,*rows,m = A->m - 1,shift = a->indexshift; 130417ab2063SBarry Smith 13053a40ed3dSBarry Smith PetscFunctionBegin; 1306b9b97703SBarry Smith ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr); 130717ab2063SBarry Smith ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 1308f1e2ffcdSBarry Smith if (a->keepzeroedrows) { 1309f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 131029bbc08cSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range"); 131187828ca2SBarry Smith ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1312f1e2ffcdSBarry Smith } 1313f1e2ffcdSBarry Smith if (diag) { 1314f1e2ffcdSBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1315f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1316f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1317f1e2ffcdSBarry Smith a->a[a->diag[rows[i]]] = *diag; 1318f1e2ffcdSBarry Smith } 1319f1e2ffcdSBarry Smith } 1320f1e2ffcdSBarry Smith } else { 132117ab2063SBarry Smith if (diag) { 132217ab2063SBarry Smith for (i=0; i<N; i++) { 132329bbc08cSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range"); 13247ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1325416022c9SBarry Smith a->ilen[rows[i]] = 1; 1326416022c9SBarry Smith a->a[a->i[rows[i]]+shift] = *diag; 1327416022c9SBarry Smith a->j[a->i[rows[i]]+shift] = rows[i]+shift; 13287ae801bdSBarry Smith } else { /* in case row was completely empty */ 1329d64ed03dSBarry Smith ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr); 133017ab2063SBarry Smith } 133117ab2063SBarry Smith } 13323a40ed3dSBarry Smith } else { 133317ab2063SBarry Smith for (i=0; i<N; i++) { 133429bbc08cSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range"); 1335416022c9SBarry Smith a->ilen[rows[i]] = 0; 133617ab2063SBarry Smith } 133717ab2063SBarry Smith } 1338f1e2ffcdSBarry Smith } 13397ae801bdSBarry Smith ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 134043a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13413a40ed3dSBarry Smith PetscFunctionReturn(0); 134217ab2063SBarry Smith } 134317ab2063SBarry Smith 13444a2ae208SSatish Balay #undef __FUNCT__ 13454a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ" 134687828ca2SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v) 134717ab2063SBarry Smith { 1348416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1349b0a32e0cSBarry Smith int *itmp,i,shift = a->indexshift,ierr; 135017ab2063SBarry Smith 13513a40ed3dSBarry Smith PetscFunctionBegin; 1352273d9f13SBarry Smith if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row); 135317ab2063SBarry Smith 1354416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1355416022c9SBarry Smith if (v) *v = a->a + a->i[row] + shift; 135617ab2063SBarry Smith if (idx) { 1357416022c9SBarry Smith itmp = a->j + a->i[row] + shift; 13584e093b46SBarry Smith if (*nz && shift) { 1359b0a32e0cSBarry Smith ierr = PetscMalloc((*nz)*sizeof(int),idx);CHKERRQ(ierr); 136017ab2063SBarry Smith for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;} 13614e093b46SBarry Smith } else if (*nz) { 13624e093b46SBarry Smith *idx = itmp; 136317ab2063SBarry Smith } 136417ab2063SBarry Smith else *idx = 0; 136517ab2063SBarry Smith } 13663a40ed3dSBarry Smith PetscFunctionReturn(0); 136717ab2063SBarry Smith } 136817ab2063SBarry Smith 13694a2ae208SSatish Balay #undef __FUNCT__ 13704a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ" 137187828ca2SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v) 137217ab2063SBarry Smith { 13734e093b46SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1374606d414cSSatish Balay int ierr; 13753a40ed3dSBarry Smith 13763a40ed3dSBarry Smith PetscFunctionBegin; 1377606d414cSSatish Balay if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}} 13783a40ed3dSBarry Smith PetscFunctionReturn(0); 137917ab2063SBarry Smith } 138017ab2063SBarry Smith 13814a2ae208SSatish Balay #undef __FUNCT__ 13824a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ" 1383064f8208SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 138417ab2063SBarry Smith { 1385416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 138687828ca2SBarry Smith PetscScalar *v = a->a; 138736db0b34SBarry Smith PetscReal sum = 0.0; 1388549d3d68SSatish Balay int i,j,shift = a->indexshift,ierr; 138917ab2063SBarry Smith 13903a40ed3dSBarry Smith PetscFunctionBegin; 139117ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1392416022c9SBarry Smith for (i=0; i<a->nz; i++) { 1393aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 139436db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 139517ab2063SBarry Smith #else 139617ab2063SBarry Smith sum += (*v)*(*v); v++; 139717ab2063SBarry Smith #endif 139817ab2063SBarry Smith } 1399064f8208SBarry Smith *nrm = sqrt(sum); 14003a40ed3dSBarry Smith } else if (type == NORM_1) { 140136db0b34SBarry Smith PetscReal *tmp; 1402416022c9SBarry Smith int *jj = a->j; 1403b0a32e0cSBarry Smith ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1404273d9f13SBarry Smith ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr); 1405064f8208SBarry Smith *nrm = 0.0; 1406416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1407a2744918SBarry Smith tmp[*jj++ + shift] += PetscAbsScalar(*v); v++; 140817ab2063SBarry Smith } 1409273d9f13SBarry Smith for (j=0; j<A->n; j++) { 1410064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 141117ab2063SBarry Smith } 1412606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 14133a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1414064f8208SBarry Smith *nrm = 0.0; 1415273d9f13SBarry Smith for (j=0; j<A->m; j++) { 1416416022c9SBarry Smith v = a->a + a->i[j] + shift; 141717ab2063SBarry Smith sum = 0.0; 1418416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1419cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 142017ab2063SBarry Smith } 1421064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 142217ab2063SBarry Smith } 14233a40ed3dSBarry Smith } else { 142429bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"No support for two norm"); 142517ab2063SBarry Smith } 14263a40ed3dSBarry Smith PetscFunctionReturn(0); 142717ab2063SBarry Smith } 142817ab2063SBarry Smith 14294a2ae208SSatish Balay #undef __FUNCT__ 14304a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ" 14318f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B) 143217ab2063SBarry Smith { 1433416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1434416022c9SBarry Smith Mat C; 1435273d9f13SBarry Smith int i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col; 1436273d9f13SBarry Smith int shift = a->indexshift; 143787828ca2SBarry Smith PetscScalar *array = a->a; 143817ab2063SBarry Smith 14393a40ed3dSBarry Smith PetscFunctionBegin; 1440273d9f13SBarry Smith if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1441b0a32e0cSBarry Smith ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr); 1442273d9f13SBarry Smith ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr); 144317ab2063SBarry Smith if (shift) { 144417ab2063SBarry Smith for (i=0; i<ai[m]-1; i++) aj[i] -= 1; 144517ab2063SBarry Smith } 144617ab2063SBarry Smith for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1; 1447273d9f13SBarry Smith ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr); 1448606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 144917ab2063SBarry Smith for (i=0; i<m; i++) { 145017ab2063SBarry Smith len = ai[i+1]-ai[i]; 1451416022c9SBarry Smith ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 1452b9b97703SBarry Smith array += len; 1453b9b97703SBarry Smith aj += len; 145417ab2063SBarry Smith } 145517ab2063SBarry Smith if (shift) { 145617ab2063SBarry Smith for (i=0; i<ai[m]-1; i++) aj[i] += 1; 145717ab2063SBarry Smith } 145817ab2063SBarry Smith 14596d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14606d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 146117ab2063SBarry Smith 1462f1e2ffcdSBarry Smith if (B) { 1463416022c9SBarry Smith *B = C; 146417ab2063SBarry Smith } else { 1465273d9f13SBarry Smith ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 146617ab2063SBarry Smith } 14673a40ed3dSBarry Smith PetscFunctionReturn(0); 146817ab2063SBarry Smith } 146917ab2063SBarry Smith 14704a2ae208SSatish Balay #undef __FUNCT__ 14714a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ" 14728f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 147317ab2063SBarry Smith { 1474416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 147587828ca2SBarry Smith PetscScalar *l,*r,x,*v; 1476273d9f13SBarry Smith int ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj,shift = a->indexshift; 147717ab2063SBarry Smith 14783a40ed3dSBarry Smith PetscFunctionBegin; 147917ab2063SBarry Smith if (ll) { 14803ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 14813ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1482e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1483273d9f13SBarry Smith if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 1484e1311b90SBarry Smith ierr = VecGetArray(ll,&l);CHKERRQ(ierr); 1485416022c9SBarry Smith v = a->a; 148617ab2063SBarry Smith for (i=0; i<m; i++) { 148717ab2063SBarry Smith x = l[i]; 1488416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 148917ab2063SBarry Smith for (j=0; j<M; j++) { (*v++) *= x;} 149017ab2063SBarry Smith } 1491e1311b90SBarry Smith ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr); 1492b0a32e0cSBarry Smith PetscLogFlops(nz); 149317ab2063SBarry Smith } 149417ab2063SBarry Smith if (rr) { 1495e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1496273d9f13SBarry Smith if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 1497e1311b90SBarry Smith ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1498416022c9SBarry Smith v = a->a; jj = a->j; 149917ab2063SBarry Smith for (i=0; i<nz; i++) { 150017ab2063SBarry Smith (*v++) *= r[*jj++ + shift]; 150117ab2063SBarry Smith } 1502e1311b90SBarry Smith ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 1503b0a32e0cSBarry Smith PetscLogFlops(nz); 150417ab2063SBarry Smith } 15053a40ed3dSBarry Smith PetscFunctionReturn(0); 150617ab2063SBarry Smith } 150717ab2063SBarry Smith 15084a2ae208SSatish Balay #undef __FUNCT__ 15094a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ" 15107b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B) 151117ab2063SBarry Smith { 1512db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 1513273d9f13SBarry Smith int *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens; 151436db0b34SBarry Smith int row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 151599141d43SSatish Balay int *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap; 151699141d43SSatish Balay int *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 151787828ca2SBarry Smith PetscScalar *a_new,*mat_a; 1518416022c9SBarry Smith Mat C; 1519fee21e36SBarry Smith PetscTruth stride; 152017ab2063SBarry Smith 15213a40ed3dSBarry Smith PetscFunctionBegin; 1522d64ed03dSBarry Smith ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr); 152329bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 1524d64ed03dSBarry Smith ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr); 152529bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); 152699141d43SSatish Balay 152717ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 1528b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 1529b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 153017ab2063SBarry Smith 1531fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 1532fee21e36SBarry Smith ierr = ISStride(iscol,&stride);CHKERRQ(ierr); 1533fee21e36SBarry Smith if (stride && step == 1) { 153402834360SBarry Smith /* special case of contiguous rows */ 153531ebf83bSSatish Balay ierr = PetscMalloc((2*nrows+1)*sizeof(int),&lens);CHKERRQ(ierr); 153631ebf83bSSatish Balay starts = lens + nrows; 153702834360SBarry Smith /* loop over new rows determining lens and starting points */ 153802834360SBarry Smith for (i=0; i<nrows; i++) { 1539a2744918SBarry Smith kstart = ai[irow[i]]+shift; 1540a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 154102834360SBarry Smith for (k=kstart; k<kend; k++) { 1542d8ced48eSBarry Smith if (aj[k]+shift >= first) { 154302834360SBarry Smith starts[i] = k; 154402834360SBarry Smith break; 154502834360SBarry Smith } 154602834360SBarry Smith } 1547a2744918SBarry Smith sum = 0; 154802834360SBarry Smith while (k < kend) { 1549d8ced48eSBarry Smith if (aj[k++]+shift >= first+ncols) break; 1550a2744918SBarry Smith sum++; 155102834360SBarry Smith } 1552a2744918SBarry Smith lens[i] = sum; 155302834360SBarry Smith } 155402834360SBarry Smith /* create submatrix */ 1555cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 155608480c60SBarry Smith int n_cols,n_rows; 155708480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 155829bbc08cSBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 1559d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 156008480c60SBarry Smith C = *B; 15613a40ed3dSBarry Smith } else { 156202834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 156308480c60SBarry Smith } 1564db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 1565db02288aSLois Curfman McInnes 156602834360SBarry Smith /* loop over rows inserting into submatrix */ 1567db02288aSLois Curfman McInnes a_new = c->a; 1568db02288aSLois Curfman McInnes j_new = c->j; 1569db02288aSLois Curfman McInnes i_new = c->i; 1570db02288aSLois Curfman McInnes i_new[0] = -shift; 157102834360SBarry Smith for (i=0; i<nrows; i++) { 1572a2744918SBarry Smith ii = starts[i]; 1573a2744918SBarry Smith lensi = lens[i]; 1574a2744918SBarry Smith for (k=0; k<lensi; k++) { 1575a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 157602834360SBarry Smith } 157787828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 1578a2744918SBarry Smith a_new += lensi; 1579a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1580a2744918SBarry Smith c->ilen[i] = lensi; 158102834360SBarry Smith } 1582606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 15833a40ed3dSBarry Smith } else { 158402834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 1585b0a32e0cSBarry Smith ierr = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr); 158602834360SBarry Smith ssmap = smap + shift; 1587b0a32e0cSBarry Smith ierr = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr); 1588549d3d68SSatish Balay ierr = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr); 158917ab2063SBarry Smith for (i=0; i<ncols; i++) smap[icol[i]] = i+1; 159002834360SBarry Smith /* determine lens of each row */ 159102834360SBarry Smith for (i=0; i<nrows; i++) { 1592d8ced48eSBarry Smith kstart = ai[irow[i]]+shift; 159302834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 159402834360SBarry Smith lens[i] = 0; 159502834360SBarry Smith for (k=kstart; k<kend; k++) { 1596d8ced48eSBarry Smith if (ssmap[aj[k]]) { 159702834360SBarry Smith lens[i]++; 159802834360SBarry Smith } 159902834360SBarry Smith } 160002834360SBarry Smith } 160117ab2063SBarry Smith /* Create and fill new matrix */ 1602a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 16030f5bd95cSBarry Smith PetscTruth equal; 16040f5bd95cSBarry Smith 160599141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 1606273d9f13SBarry Smith if ((*B)->m != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 1607273d9f13SBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr); 16080f5bd95cSBarry Smith if (!equal) { 160929bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 161099141d43SSatish Balay } 1611273d9f13SBarry Smith ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr); 161208480c60SBarry Smith C = *B; 16133a40ed3dSBarry Smith } else { 161402834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 161508480c60SBarry Smith } 161699141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 161717ab2063SBarry Smith for (i=0; i<nrows; i++) { 161899141d43SSatish Balay row = irow[i]; 161999141d43SSatish Balay kstart = ai[row]+shift; 162099141d43SSatish Balay kend = kstart + a->ilen[row]; 162199141d43SSatish Balay mat_i = c->i[i]+shift; 162299141d43SSatish Balay mat_j = c->j + mat_i; 162399141d43SSatish Balay mat_a = c->a + mat_i; 162499141d43SSatish Balay mat_ilen = c->ilen + i; 162517ab2063SBarry Smith for (k=kstart; k<kend; k++) { 162699141d43SSatish Balay if ((tcol=ssmap[a->j[k]])) { 162799141d43SSatish Balay *mat_j++ = tcol - (!shift); 162899141d43SSatish Balay *mat_a++ = a->a[k]; 162999141d43SSatish Balay (*mat_ilen)++; 163099141d43SSatish Balay 163117ab2063SBarry Smith } 163217ab2063SBarry Smith } 163317ab2063SBarry Smith } 163402834360SBarry Smith /* Free work space */ 163502834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 1636606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 1637606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 163802834360SBarry Smith } 16396d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 16406d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 164117ab2063SBarry Smith 164217ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 1643416022c9SBarry Smith *B = C; 16443a40ed3dSBarry Smith PetscFunctionReturn(0); 164517ab2063SBarry Smith } 164617ab2063SBarry Smith 1647a871dcd8SBarry Smith /* 1648a871dcd8SBarry Smith */ 16494a2ae208SSatish Balay #undef __FUNCT__ 16504a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ" 16515ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info) 1652a871dcd8SBarry Smith { 165363b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 165408480c60SBarry Smith int ierr; 165563b91edcSBarry Smith Mat outA; 1656b8a78c4aSBarry Smith PetscTruth row_identity,col_identity; 165763b91edcSBarry Smith 16583a40ed3dSBarry Smith PetscFunctionBegin; 165929bbc08cSBarry Smith if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 1660b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 1661b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 1662b8a78c4aSBarry Smith if (!row_identity || !col_identity) { 166329bbc08cSBarry Smith SETERRQ(1,"Row and column permutations must be identity for in-place ILU"); 1664b8a78c4aSBarry Smith } 1665a871dcd8SBarry Smith 166663b91edcSBarry Smith outA = inA; 166763b91edcSBarry Smith inA->factor = FACTOR_LU; 166863b91edcSBarry Smith a->row = row; 166963b91edcSBarry Smith a->col = col; 1670c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 1671c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 167263b91edcSBarry Smith 167336db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 1674b9b97703SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */ 16754c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 1676b0a32e0cSBarry Smith PetscLogObjectParent(inA,a->icol); 1677f0ec6fceSSatish Balay 167894a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 167987828ca2SBarry Smith ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr); 168094a9d846SBarry Smith } 168163b91edcSBarry Smith 168208480c60SBarry Smith if (!a->diag) { 1683f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 168463b91edcSBarry Smith } 168563b91edcSBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr); 16863a40ed3dSBarry Smith PetscFunctionReturn(0); 1687a871dcd8SBarry Smith } 1688a871dcd8SBarry Smith 1689d9eff348SSatish Balay #include "petscblaslapack.h" 16904a2ae208SSatish Balay #undef __FUNCT__ 16914a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ" 169287828ca2SBarry Smith int MatScale_SeqAIJ(PetscScalar *alpha,Mat inA) 1693f0b747eeSBarry Smith { 1694f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1695f0b747eeSBarry Smith int one = 1; 16963a40ed3dSBarry Smith 16973a40ed3dSBarry Smith PetscFunctionBegin; 1698f0b747eeSBarry Smith BLscal_(&a->nz,alpha,a->a,&one); 1699b0a32e0cSBarry Smith PetscLogFlops(a->nz); 17003a40ed3dSBarry Smith PetscFunctionReturn(0); 1701f0b747eeSBarry Smith } 1702f0b747eeSBarry Smith 17034a2ae208SSatish Balay #undef __FUNCT__ 17044a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ" 17057b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B) 1706cddf8d76SBarry Smith { 1707cddf8d76SBarry Smith int ierr,i; 1708cddf8d76SBarry Smith 17093a40ed3dSBarry Smith PetscFunctionBegin; 1710cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 1711b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr); 1712cddf8d76SBarry Smith } 1713cddf8d76SBarry Smith 1714cddf8d76SBarry Smith for (i=0; i<n; i++) { 17156a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 1716cddf8d76SBarry Smith } 17173a40ed3dSBarry Smith PetscFunctionReturn(0); 1718cddf8d76SBarry Smith } 1719cddf8d76SBarry Smith 17204a2ae208SSatish Balay #undef __FUNCT__ 17214a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ" 17228f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs) 17235a838052SSatish Balay { 1724f830108cSBarry Smith PetscFunctionBegin; 17255a838052SSatish Balay *bs = 1; 17263a40ed3dSBarry Smith PetscFunctionReturn(0); 17275a838052SSatish Balay } 17285a838052SSatish Balay 17294a2ae208SSatish Balay #undef __FUNCT__ 17304a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ" 17318f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov) 17324dcbc457SBarry Smith { 1733e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 173406763907SSatish Balay int shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val; 17358a047759SSatish Balay int start,end,*ai,*aj; 1736f1af5d2fSBarry Smith PetscBT table; 1737bbd702dbSSatish Balay 17383a40ed3dSBarry Smith PetscFunctionBegin; 17398a047759SSatish Balay shift = a->indexshift; 1740273d9f13SBarry Smith m = A->m; 1741e4d965acSSatish Balay ai = a->i; 17428a047759SSatish Balay aj = a->j+shift; 17438a047759SSatish Balay 174429bbc08cSBarry Smith if (ov < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used"); 174506763907SSatish Balay 1746b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr); 17476831982aSBarry Smith ierr = PetscBTCreate(m,table);CHKERRQ(ierr); 174806763907SSatish Balay 1749e4d965acSSatish Balay for (i=0; i<is_max; i++) { 1750b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1751e4d965acSSatish Balay isz = 0; 17526831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 1753e4d965acSSatish Balay 1754e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 17554dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 1756b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 1757e4d965acSSatish Balay 1758dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1759e4d965acSSatish Balay for (j=0; j<n ; ++j){ 1760f1af5d2fSBarry Smith if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];} 17614dcbc457SBarry Smith } 176206763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 176306763907SSatish Balay ierr = ISDestroy(is[i]);CHKERRQ(ierr); 1764e4d965acSSatish Balay 176504a348a9SBarry Smith k = 0; 176604a348a9SBarry Smith for (j=0; j<ov; j++){ /* for each overlap */ 176704a348a9SBarry Smith n = isz; 176806763907SSatish Balay for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 1769e4d965acSSatish Balay row = nidx[k]; 1770e4d965acSSatish Balay start = ai[row]; 1771e4d965acSSatish Balay end = ai[row+1]; 177204a348a9SBarry Smith for (l = start; l<end ; l++){ 17738a047759SSatish Balay val = aj[l] + shift; 1774f1af5d2fSBarry Smith if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;} 1775e4d965acSSatish Balay } 1776e4d965acSSatish Balay } 1777e4d965acSSatish Balay } 1778029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr); 1779e4d965acSSatish Balay } 17806831982aSBarry Smith ierr = PetscBTDestroy(table);CHKERRQ(ierr); 1781606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 17823a40ed3dSBarry Smith PetscFunctionReturn(0); 17834dcbc457SBarry Smith } 178417ab2063SBarry Smith 17850513a670SBarry Smith /* -------------------------------------------------------------- */ 17864a2ae208SSatish Balay #undef __FUNCT__ 17874a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ" 17880513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 17890513a670SBarry Smith { 17900513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 179187828ca2SBarry Smith PetscScalar *vwork; 1792273d9f13SBarry Smith int i,ierr,nz,m = A->m,n = A->n,*cwork; 17930513a670SBarry Smith int *row,*col,*cnew,j,*lens; 179456cd22aeSBarry Smith IS icolp,irowp; 17950513a670SBarry Smith 17963a40ed3dSBarry Smith PetscFunctionBegin; 17974c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 179856cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 17994c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 180056cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 18010513a670SBarry Smith 18020513a670SBarry Smith /* determine lengths of permuted rows */ 1803b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr); 18040513a670SBarry Smith for (i=0; i<m; i++) { 18050513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 18060513a670SBarry Smith } 18070513a670SBarry Smith ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr); 1808606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 18090513a670SBarry Smith 1810b0a32e0cSBarry Smith ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr); 18110513a670SBarry Smith for (i=0; i<m; i++) { 18120513a670SBarry Smith ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 18130513a670SBarry Smith for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];} 18140513a670SBarry Smith ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 18150513a670SBarry Smith ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 18160513a670SBarry Smith } 1817606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 18180513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 18190513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 182056cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 182156cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 182256cd22aeSBarry Smith ierr = ISDestroy(irowp);CHKERRQ(ierr); 182356cd22aeSBarry Smith ierr = ISDestroy(icolp);CHKERRQ(ierr); 18243a40ed3dSBarry Smith PetscFunctionReturn(0); 18250513a670SBarry Smith } 18260513a670SBarry Smith 18274a2ae208SSatish Balay #undef __FUNCT__ 18284a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ" 1829682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A) 1830682d7d0cSBarry Smith { 1831c38d4ed2SBarry Smith static PetscTruth called = PETSC_FALSE; 1832682d7d0cSBarry Smith MPI_Comm comm = A->comm; 1833d132466eSBarry Smith int ierr; 1834682d7d0cSBarry Smith 18353a40ed3dSBarry Smith PetscFunctionBegin; 1836c38d4ed2SBarry Smith if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE; 1837d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr); 1838d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr); 1839d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr); 1840d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr); 1841d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr); 1842aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL) 1843d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr); 1844682d7d0cSBarry Smith #endif 1845cd5578b5SBarry Smith #if defined(PETSC_HAVE_LUSOL) 1846cd5578b5SBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_lusol: Use the Stanford LUSOL sparse factorization and solve.\n");CHKERRQ(ierr); 1847cd5578b5SBarry Smith #endif 184887828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 1849ca44d042SBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr); 1850ca44d042SBarry Smith #endif 18513a40ed3dSBarry Smith PetscFunctionReturn(0); 1852682d7d0cSBarry Smith } 1853ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg); 1854ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring); 1855ca44d042SBarry Smith EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatILUInfo*,IS,IS,Mat*); 18564a2ae208SSatish Balay #undef __FUNCT__ 18574a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ" 1858cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 1859cb5b572fSBarry Smith { 1860be6bf707SBarry Smith int ierr; 1861273d9f13SBarry Smith PetscTruth flg; 1862cb5b572fSBarry Smith 1863cb5b572fSBarry Smith PetscFunctionBegin; 1864273d9f13SBarry Smith ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg);CHKERRQ(ierr); 1865273d9f13SBarry Smith if (str == SAME_NONZERO_PATTERN && flg) { 1866be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1867be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 1868be6bf707SBarry Smith 1869273d9f13SBarry Smith if (a->i[A->m]+a->indexshift != b->i[B->m]+a->indexshift) { 187029bbc08cSBarry Smith SETERRQ(1,"Number of nonzeros in two matrices are different"); 1871cb5b572fSBarry Smith } 187287828ca2SBarry Smith ierr = PetscMemcpy(b->a,a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr); 1873cb5b572fSBarry Smith } else { 1874cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1875cb5b572fSBarry Smith } 1876cb5b572fSBarry Smith PetscFunctionReturn(0); 1877cb5b572fSBarry Smith } 1878cb5b572fSBarry Smith 18794a2ae208SSatish Balay #undef __FUNCT__ 18804a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ" 1881273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A) 1882273d9f13SBarry Smith { 1883273d9f13SBarry Smith int ierr; 1884273d9f13SBarry Smith 1885273d9f13SBarry Smith PetscFunctionBegin; 1886273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 1887273d9f13SBarry Smith PetscFunctionReturn(0); 1888273d9f13SBarry Smith } 1889273d9f13SBarry Smith 18904a2ae208SSatish Balay #undef __FUNCT__ 18914a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ" 189287828ca2SBarry Smith int MatGetArray_SeqAIJ(Mat A,PetscScalar **array) 18936c0721eeSBarry Smith { 18946c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 18956c0721eeSBarry Smith PetscFunctionBegin; 18966c0721eeSBarry Smith *array = a->a; 18976c0721eeSBarry Smith PetscFunctionReturn(0); 18986c0721eeSBarry Smith } 18996c0721eeSBarry Smith 19004a2ae208SSatish Balay #undef __FUNCT__ 19014a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ" 190287828ca2SBarry Smith int MatRestoreArray_SeqAIJ(Mat A,PetscScalar **array) 19036c0721eeSBarry Smith { 19046c0721eeSBarry Smith PetscFunctionBegin; 19056c0721eeSBarry Smith PetscFunctionReturn(0); 19066c0721eeSBarry Smith } 1907273d9f13SBarry Smith 1908ee4f033dSBarry Smith #undef __FUNCT__ 1909ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ" 1910ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 1911ee4f033dSBarry Smith { 1912ee4f033dSBarry Smith int (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f; 1913ee4f033dSBarry Smith int k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2; 191487828ca2SBarry Smith PetscScalar dx,mone = -1.0,*y,*xx,*w3_array; 191587828ca2SBarry Smith PetscScalar *vscale_array; 1916ee4f033dSBarry Smith PetscReal epsilon = coloring->error_rel,umin = coloring->umin; 1917ee4f033dSBarry Smith Vec w1,w2,w3; 1918ee4f033dSBarry Smith void *fctx = coloring->fctx; 1919ee4f033dSBarry Smith PetscTruth flg; 1920ee4f033dSBarry Smith 1921ee4f033dSBarry Smith PetscFunctionBegin; 1922ee4f033dSBarry Smith if (!coloring->w1) { 1923ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr); 1924ee4f033dSBarry Smith PetscLogObjectParent(coloring,coloring->w1); 1925ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr); 1926ee4f033dSBarry Smith PetscLogObjectParent(coloring,coloring->w2); 1927ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 1928ee4f033dSBarry Smith PetscLogObjectParent(coloring,coloring->w3); 1929ee4f033dSBarry Smith } 1930ee4f033dSBarry Smith w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 1931ee4f033dSBarry Smith 1932ee4f033dSBarry Smith ierr = MatSetUnfactored(J);CHKERRQ(ierr); 1933ee4f033dSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr); 1934ee4f033dSBarry Smith if (flg) { 1935ee4f033dSBarry Smith PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"); 1936ee4f033dSBarry Smith } else { 1937ee4f033dSBarry Smith ierr = MatZeroEntries(J);CHKERRQ(ierr); 1938ee4f033dSBarry Smith } 1939ee4f033dSBarry Smith 1940ee4f033dSBarry Smith ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr); 1941ee4f033dSBarry Smith ierr = VecGetSize(x1,&N);CHKERRQ(ierr); 1942ee4f033dSBarry Smith 1943ee4f033dSBarry Smith /* 1944ee4f033dSBarry Smith This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets 1945ee4f033dSBarry Smith coloring->F for the coarser grids from the finest 1946ee4f033dSBarry Smith */ 1947ee4f033dSBarry Smith if (coloring->F) { 1948ee4f033dSBarry Smith ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr); 1949ee4f033dSBarry Smith ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr); 1950ee4f033dSBarry Smith if (m1 != m2) { 1951ee4f033dSBarry Smith coloring->F = 0; 1952ee4f033dSBarry Smith } 1953ee4f033dSBarry Smith } 1954ee4f033dSBarry Smith 1955ee4f033dSBarry Smith if (coloring->F) { 1956ee4f033dSBarry Smith w1 = coloring->F; 1957ee4f033dSBarry Smith coloring->F = 0; 1958ee4f033dSBarry Smith } else { 1959ee4f033dSBarry Smith ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 1960ee4f033dSBarry Smith } 1961ee4f033dSBarry Smith 1962ee4f033dSBarry Smith /* 1963ee4f033dSBarry Smith Compute all the scale factors and share with other processors 1964ee4f033dSBarry Smith */ 1965ee4f033dSBarry Smith ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start; 1966ee4f033dSBarry Smith ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start; 1967ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 1968ee4f033dSBarry Smith /* 1969ee4f033dSBarry Smith Loop over each column associated with color adding the 1970ee4f033dSBarry Smith perturbation to the vector w3. 1971ee4f033dSBarry Smith */ 1972ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 1973ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 1974ee4f033dSBarry Smith dx = xx[col]; 1975ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 1976ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 1977ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 1978ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 1979ee4f033dSBarry Smith #else 1980ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 1981ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 1982ee4f033dSBarry Smith #endif 1983ee4f033dSBarry Smith dx *= epsilon; 1984ee4f033dSBarry Smith vscale_array[col] = 1.0/dx; 1985ee4f033dSBarry Smith } 1986ee4f033dSBarry Smith } 1987ee4f033dSBarry Smith vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 1988ee4f033dSBarry Smith ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1989ee4f033dSBarry Smith ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1990ee4f033dSBarry Smith 1991ee4f033dSBarry Smith /* ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD); 1992ee4f033dSBarry Smith ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/ 1993ee4f033dSBarry Smith 1994ee4f033dSBarry Smith if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow; 1995ee4f033dSBarry Smith else vscaleforrow = coloring->columnsforrow; 1996ee4f033dSBarry Smith 1997ee4f033dSBarry Smith ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 1998ee4f033dSBarry Smith /* 1999ee4f033dSBarry Smith Loop over each color 2000ee4f033dSBarry Smith */ 2001ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 2002ee4f033dSBarry Smith ierr = VecCopy(x1,w3);CHKERRQ(ierr); 2003ee4f033dSBarry Smith ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start; 2004ee4f033dSBarry Smith /* 2005ee4f033dSBarry Smith Loop over each column associated with color adding the 2006ee4f033dSBarry Smith perturbation to the vector w3. 2007ee4f033dSBarry Smith */ 2008ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2009ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2010ee4f033dSBarry Smith dx = xx[col]; 2011ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 2012ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2013ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2014ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2015ee4f033dSBarry Smith #else 2016ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2017ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2018ee4f033dSBarry Smith #endif 2019ee4f033dSBarry Smith dx *= epsilon; 2020ee4f033dSBarry Smith if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter"); 2021ee4f033dSBarry Smith w3_array[col] += dx; 2022ee4f033dSBarry Smith } 2023ee4f033dSBarry Smith w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 2024ee4f033dSBarry Smith 2025ee4f033dSBarry Smith /* 2026ee4f033dSBarry Smith Evaluate function at x1 + dx (here dx is a vector of perturbations) 2027ee4f033dSBarry Smith */ 2028ee4f033dSBarry Smith 2029ee4f033dSBarry Smith ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 2030ee4f033dSBarry Smith ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr); 2031ee4f033dSBarry Smith 2032ee4f033dSBarry Smith /* 2033ee4f033dSBarry Smith Loop over rows of vector, putting results into Jacobian matrix 2034ee4f033dSBarry Smith */ 2035ee4f033dSBarry Smith ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 2036ee4f033dSBarry Smith for (l=0; l<coloring->nrows[k]; l++) { 2037ee4f033dSBarry Smith row = coloring->rows[k][l]; 2038ee4f033dSBarry Smith col = coloring->columnsforrow[k][l]; 2039ee4f033dSBarry Smith y[row] *= vscale_array[vscaleforrow[k][l]]; 2040ee4f033dSBarry Smith srow = row + start; 2041ee4f033dSBarry Smith ierr = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 2042ee4f033dSBarry Smith } 2043ee4f033dSBarry Smith ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 2044ee4f033dSBarry Smith } 2045ee4f033dSBarry Smith ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2046ee4f033dSBarry Smith xx = xx + start; ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 2047ee4f033dSBarry Smith ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2048ee4f033dSBarry Smith ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2049ee4f033dSBarry Smith PetscFunctionReturn(0); 2050ee4f033dSBarry Smith } 2051ee4f033dSBarry Smith 2052ac90fabeSBarry Smith #include "petscblaslapack.h" 2053ac90fabeSBarry Smith 2054ac90fabeSBarry Smith #undef __FUNCT__ 2055ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ" 2056ac90fabeSBarry Smith int MatAXPY_SeqAIJ(PetscScalar *a,Mat X,Mat Y,MatStructure str) 2057ac90fabeSBarry Smith { 2058556bd4faSSatish Balay int ierr,one=1; 2059ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data; 2060ac90fabeSBarry Smith 2061ac90fabeSBarry Smith PetscFunctionBegin; 2062ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2063ac90fabeSBarry Smith BLaxpy_(&x->nz,a,x->a,&one,y->a,&one); 2064ac90fabeSBarry Smith } else { 2065ac90fabeSBarry Smith ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr); 2066ac90fabeSBarry Smith } 2067ac90fabeSBarry Smith PetscFunctionReturn(0); 2068ac90fabeSBarry Smith } 2069ac90fabeSBarry Smith 2070ac90fabeSBarry Smith 2071682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 20720a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ, 2073cb5b572fSBarry Smith MatGetRow_SeqAIJ, 2074cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 2075cb5b572fSBarry Smith MatMult_SeqAIJ, 2076cb5b572fSBarry Smith MatMultAdd_SeqAIJ, 20777c922b88SBarry Smith MatMultTranspose_SeqAIJ, 20787c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 2079cb5b572fSBarry Smith MatSolve_SeqAIJ, 2080cb5b572fSBarry Smith MatSolveAdd_SeqAIJ, 20817c922b88SBarry Smith MatSolveTranspose_SeqAIJ, 20827c922b88SBarry Smith MatSolveTransposeAdd_SeqAIJ, 2083cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 2084cb5b572fSBarry Smith 0, 208517ab2063SBarry Smith MatRelax_SeqAIJ, 208617ab2063SBarry Smith MatTranspose_SeqAIJ, 2087cb5b572fSBarry Smith MatGetInfo_SeqAIJ, 2088cb5b572fSBarry Smith MatEqual_SeqAIJ, 2089cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 2090cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 2091cb5b572fSBarry Smith MatNorm_SeqAIJ, 2092cb5b572fSBarry Smith 0, 2093cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 209417ab2063SBarry Smith MatCompress_SeqAIJ, 2095cb5b572fSBarry Smith MatSetOption_SeqAIJ, 2096cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 2097cb5b572fSBarry Smith MatZeroRows_SeqAIJ, 2098cb5b572fSBarry Smith MatLUFactorSymbolic_SeqAIJ, 2099cb5b572fSBarry Smith MatLUFactorNumeric_SeqAIJ, 2100cb5b572fSBarry Smith 0, 2101cb5b572fSBarry Smith 0, 2102273d9f13SBarry Smith MatSetUpPreallocation_SeqAIJ, 2103cb5b572fSBarry Smith MatILUFactorSymbolic_SeqAIJ, 2104cb5b572fSBarry Smith 0, 21056c0721eeSBarry Smith MatGetArray_SeqAIJ, 21066c0721eeSBarry Smith MatRestoreArray_SeqAIJ, 2107be6bf707SBarry Smith MatDuplicate_SeqAIJ, 2108cb5b572fSBarry Smith 0, 2109cb5b572fSBarry Smith 0, 2110cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 2111cb5b572fSBarry Smith 0, 2112ac90fabeSBarry Smith MatAXPY_SeqAIJ, 2113cb5b572fSBarry Smith MatGetSubMatrices_SeqAIJ, 2114cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 2115cb5b572fSBarry Smith MatGetValues_SeqAIJ, 2116cb5b572fSBarry Smith MatCopy_SeqAIJ, 2117f0b747eeSBarry Smith MatPrintHelp_SeqAIJ, 2118cb5b572fSBarry Smith MatScale_SeqAIJ, 2119cb5b572fSBarry Smith 0, 2120cb5b572fSBarry Smith 0, 21216945ee14SBarry Smith MatILUDTFactor_SeqAIJ, 21226945ee14SBarry Smith MatGetBlockSize_SeqAIJ, 21233b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 21243b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 21253b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 2126a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 2127a93ec695SBarry Smith MatFDColoringCreate_SeqAIJ, 2128b9617806SBarry Smith 0, 21290513a670SBarry Smith 0, 2130cda55fadSBarry Smith MatPermute_SeqAIJ, 2131cda55fadSBarry Smith 0, 2132cda55fadSBarry Smith 0, 2133b9b97703SBarry Smith MatDestroy_SeqAIJ, 2134b9b97703SBarry Smith MatView_SeqAIJ, 21358a124369SBarry Smith MatGetPetscMaps_Petsc, 2136ee4f033dSBarry Smith 0, 2137ee4f033dSBarry Smith 0, 2138ee4f033dSBarry Smith 0, 2139ee4f033dSBarry Smith 0, 2140ee4f033dSBarry Smith 0, 2141ee4f033dSBarry Smith 0, 2142ee4f033dSBarry Smith 0, 2143ee4f033dSBarry Smith 0, 2144ee4f033dSBarry Smith MatSetColoring_SeqAIJ, 2145ee4f033dSBarry Smith MatSetValuesAdic_SeqAIJ, 2146ee4f033dSBarry Smith MatSetValuesAdifor_SeqAIJ, 2147ee4f033dSBarry Smith MatFDColoringApply_SeqAIJ}; 214817ab2063SBarry Smith 2149ca44d042SBarry Smith EXTERN int MatUseSuperLU_SeqAIJ(Mat); 2150ca44d042SBarry Smith EXTERN int MatUseEssl_SeqAIJ(Mat); 2151cd5578b5SBarry Smith EXTERN int MatUseLUSOL_SeqAIJ(Mat); 2152ca44d042SBarry Smith EXTERN int MatUseMatlab_SeqAIJ(Mat); 2153ca44d042SBarry Smith EXTERN int MatUseDXML_SeqAIJ(Mat); 215417ab2063SBarry Smith 2155fb2e594dSBarry Smith EXTERN_C_BEGIN 21564a2ae208SSatish Balay #undef __FUNCT__ 21574a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ" 215815091d37SBarry Smith 2159bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices) 2160bef8e0ddSBarry Smith { 2161bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2162bef8e0ddSBarry Smith int i,nz,n; 2163bef8e0ddSBarry Smith 2164bef8e0ddSBarry Smith PetscFunctionBegin; 2165bef8e0ddSBarry Smith 2166bef8e0ddSBarry Smith nz = aij->maxnz; 2167273d9f13SBarry Smith n = mat->n; 2168bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 2169bef8e0ddSBarry Smith aij->j[i] = indices[i]; 2170bef8e0ddSBarry Smith } 2171bef8e0ddSBarry Smith aij->nz = nz; 2172bef8e0ddSBarry Smith for (i=0; i<n; i++) { 2173bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 2174bef8e0ddSBarry Smith } 2175bef8e0ddSBarry Smith 2176bef8e0ddSBarry Smith PetscFunctionReturn(0); 2177bef8e0ddSBarry Smith } 2178fb2e594dSBarry Smith EXTERN_C_END 2179bef8e0ddSBarry Smith 21804a2ae208SSatish Balay #undef __FUNCT__ 21814a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices" 2182bef8e0ddSBarry Smith /*@ 2183bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 2184bef8e0ddSBarry Smith in the matrix. 2185bef8e0ddSBarry Smith 2186bef8e0ddSBarry Smith Input Parameters: 2187bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 2188bef8e0ddSBarry Smith - indices - the column indices 2189bef8e0ddSBarry Smith 219015091d37SBarry Smith Level: advanced 219115091d37SBarry Smith 2192bef8e0ddSBarry Smith Notes: 2193bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 2194bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 2195bef8e0ddSBarry Smith of the MatSetValues() operation. 2196bef8e0ddSBarry Smith 2197bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 2198bef8e0ddSBarry Smith MatCreateSeqAIJ(). 2199bef8e0ddSBarry Smith 2200bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 2201bef8e0ddSBarry Smith 2202b9617806SBarry Smith The indices should start with zero, not one. 2203b9617806SBarry Smith 2204bef8e0ddSBarry Smith @*/ 2205bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices) 2206bef8e0ddSBarry Smith { 2207bef8e0ddSBarry Smith int ierr,(*f)(Mat,int *); 2208bef8e0ddSBarry Smith 2209bef8e0ddSBarry Smith PetscFunctionBegin; 2210bef8e0ddSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 2211c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr); 2212bef8e0ddSBarry Smith if (f) { 2213bef8e0ddSBarry Smith ierr = (*f)(mat,indices);CHKERRQ(ierr); 2214bef8e0ddSBarry Smith } else { 221529bbc08cSBarry Smith SETERRQ(1,"Wrong type of matrix to set column indices"); 2216bef8e0ddSBarry Smith } 2217bef8e0ddSBarry Smith PetscFunctionReturn(0); 2218bef8e0ddSBarry Smith } 2219bef8e0ddSBarry Smith 2220be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 2221be6bf707SBarry Smith 2222fb2e594dSBarry Smith EXTERN_C_BEGIN 22234a2ae208SSatish Balay #undef __FUNCT__ 22244a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ" 2225be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat) 2226be6bf707SBarry Smith { 2227be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2228*a7ed9263SMatthew Knepley size_t nz = aij->i[mat->m]+aij->indexshift,ierr; 2229be6bf707SBarry Smith 2230be6bf707SBarry Smith PetscFunctionBegin; 2231be6bf707SBarry Smith if (aij->nonew != 1) { 223229bbc08cSBarry Smith SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first"); 2233be6bf707SBarry Smith } 2234be6bf707SBarry Smith 2235be6bf707SBarry Smith /* allocate space for values if not already there */ 2236be6bf707SBarry Smith if (!aij->saved_values) { 223787828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr); 2238be6bf707SBarry Smith } 2239be6bf707SBarry Smith 2240be6bf707SBarry Smith /* copy values over */ 224187828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2242be6bf707SBarry Smith PetscFunctionReturn(0); 2243be6bf707SBarry Smith } 2244fb2e594dSBarry Smith EXTERN_C_END 2245be6bf707SBarry Smith 22464a2ae208SSatish Balay #undef __FUNCT__ 2247b9617806SBarry Smith #define __FUNCT__ "MatStoreValues" 2248be6bf707SBarry Smith /*@ 2249be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 2250be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2251be6bf707SBarry Smith nonlinear portion. 2252be6bf707SBarry Smith 2253be6bf707SBarry Smith Collect on Mat 2254be6bf707SBarry Smith 2255be6bf707SBarry Smith Input Parameters: 2256be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2257be6bf707SBarry Smith 225815091d37SBarry Smith Level: advanced 225915091d37SBarry Smith 2260be6bf707SBarry Smith Common Usage, with SNESSolve(): 2261be6bf707SBarry Smith $ Create Jacobian matrix 2262be6bf707SBarry Smith $ Set linear terms into matrix 2263be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 2264be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 2265be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 2266be6bf707SBarry Smith $ ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); 2267be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2268be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 2269be6bf707SBarry Smith $ In your Jacobian routine 2270be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2271be6bf707SBarry Smith $ Set nonlinear terms in matrix 2272be6bf707SBarry Smith 2273be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 2274be6bf707SBarry Smith $ // build linear portion of Jacobian 2275be6bf707SBarry Smith $ ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); 2276be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2277be6bf707SBarry Smith $ loop over nonlinear iterations 2278be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2279be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 2280be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 2281be6bf707SBarry Smith $ Solve linear system with Jacobian 2282be6bf707SBarry Smith $ endloop 2283be6bf707SBarry Smith 2284be6bf707SBarry Smith Notes: 2285be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 2286be6bf707SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before 2287be6bf707SBarry Smith calling this routine. 2288be6bf707SBarry Smith 2289be6bf707SBarry Smith .seealso: MatRetrieveValues() 2290be6bf707SBarry Smith 2291be6bf707SBarry Smith @*/ 2292be6bf707SBarry Smith int MatStoreValues(Mat mat) 2293be6bf707SBarry Smith { 2294be6bf707SBarry Smith int ierr,(*f)(Mat); 2295be6bf707SBarry Smith 2296be6bf707SBarry Smith PetscFunctionBegin; 2297be6bf707SBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 229829bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 229929bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2300be6bf707SBarry Smith 2301c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2302be6bf707SBarry Smith if (f) { 2303be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2304be6bf707SBarry Smith } else { 230529bbc08cSBarry Smith SETERRQ(1,"Wrong type of matrix to store values"); 2306be6bf707SBarry Smith } 2307be6bf707SBarry Smith PetscFunctionReturn(0); 2308be6bf707SBarry Smith } 2309be6bf707SBarry Smith 2310fb2e594dSBarry Smith EXTERN_C_BEGIN 23114a2ae208SSatish Balay #undef __FUNCT__ 23124a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ" 2313be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat) 2314be6bf707SBarry Smith { 2315be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2316273d9f13SBarry Smith int nz = aij->i[mat->m]+aij->indexshift,ierr; 2317be6bf707SBarry Smith 2318be6bf707SBarry Smith PetscFunctionBegin; 2319be6bf707SBarry Smith if (aij->nonew != 1) { 232029bbc08cSBarry Smith SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first"); 2321be6bf707SBarry Smith } 2322be6bf707SBarry Smith if (!aij->saved_values) { 232329bbc08cSBarry Smith SETERRQ(1,"Must call MatStoreValues(A);first"); 2324be6bf707SBarry Smith } 2325be6bf707SBarry Smith 2326be6bf707SBarry Smith /* copy values over */ 232787828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2328be6bf707SBarry Smith PetscFunctionReturn(0); 2329be6bf707SBarry Smith } 2330fb2e594dSBarry Smith EXTERN_C_END 2331be6bf707SBarry Smith 23324a2ae208SSatish Balay #undef __FUNCT__ 23334a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues" 2334be6bf707SBarry Smith /*@ 2335be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 2336be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2337be6bf707SBarry Smith nonlinear portion. 2338be6bf707SBarry Smith 2339be6bf707SBarry Smith Collect on Mat 2340be6bf707SBarry Smith 2341be6bf707SBarry Smith Input Parameters: 2342be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2343be6bf707SBarry Smith 234415091d37SBarry Smith Level: advanced 234515091d37SBarry Smith 2346be6bf707SBarry Smith .seealso: MatStoreValues() 2347be6bf707SBarry Smith 2348be6bf707SBarry Smith @*/ 2349be6bf707SBarry Smith int MatRetrieveValues(Mat mat) 2350be6bf707SBarry Smith { 2351be6bf707SBarry Smith int ierr,(*f)(Mat); 2352be6bf707SBarry Smith 2353be6bf707SBarry Smith PetscFunctionBegin; 2354be6bf707SBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 235529bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 235629bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2357be6bf707SBarry Smith 2358c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2359be6bf707SBarry Smith if (f) { 2360be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2361be6bf707SBarry Smith } else { 236229bbc08cSBarry Smith SETERRQ(1,"Wrong type of matrix to retrieve values"); 2363be6bf707SBarry Smith } 2364be6bf707SBarry Smith PetscFunctionReturn(0); 2365be6bf707SBarry Smith } 2366be6bf707SBarry Smith 2367f83d6046SBarry Smith /* 2368f83d6046SBarry Smith This allows SeqAIJ matrices to be passed to the matlab engine 2369f83d6046SBarry Smith */ 237087828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 2371f83d6046SBarry Smith #include "engine.h" /* Matlab include file */ 2372f83d6046SBarry Smith #include "mex.h" /* Matlab include file */ 2373f83d6046SBarry Smith EXTERN_C_BEGIN 23744a2ae208SSatish Balay #undef __FUNCT__ 23754a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ" 2376f83d6046SBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *engine) 2377f83d6046SBarry Smith { 237866826eb6SBarry Smith int ierr,i,*ai,*aj; 2379f83d6046SBarry Smith Mat B = (Mat)obj; 238087828ca2SBarry Smith PetscScalar *array; 2381f83d6046SBarry Smith mxArray *mat; 2382d79a51d8SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)B->data; 2383d79a51d8SBarry Smith 2384f83d6046SBarry Smith PetscFunctionBegin; 238501820c62SBarry Smith mat = mxCreateSparse(B->n,B->m,aij->nz,mxREAL); 238687828ca2SBarry Smith ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));CHKERRQ(ierr); 2387d79a51d8SBarry Smith /* Matlab stores by column, not row so we pass in the transpose of the matrix */ 238866826eb6SBarry Smith ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr); 238966826eb6SBarry Smith ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr); 2390f83d6046SBarry Smith 239101820c62SBarry Smith /* Matlab indices start at 0 for sparse (what a surprise) */ 239201820c62SBarry Smith if (aij->indexshift) { 239301820c62SBarry Smith for (i=0; i<B->m+1; i++) { 239401820c62SBarry Smith ai[i]--; 2395d79a51d8SBarry Smith } 2396d79a51d8SBarry Smith for (i=0; i<aij->nz; i++) { 239701820c62SBarry Smith aj[i]--; 2398d79a51d8SBarry Smith } 2399d79a51d8SBarry Smith } 2400ca44d042SBarry Smith ierr = PetscObjectName(obj);CHKERRQ(ierr); 2401f83d6046SBarry Smith mxSetName(mat,obj->name); 2402f83d6046SBarry Smith engPutArray((Engine *)engine,mat); 2403f83d6046SBarry Smith PetscFunctionReturn(0); 2404f83d6046SBarry Smith } 2405f83d6046SBarry Smith EXTERN_C_END 240666826eb6SBarry Smith 240766826eb6SBarry Smith EXTERN_C_BEGIN 24084a2ae208SSatish Balay #undef __FUNCT__ 24094a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ" 241066826eb6SBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *engine) 241166826eb6SBarry Smith { 241266826eb6SBarry Smith int ierr,ii; 241366826eb6SBarry Smith Mat mat = (Mat)obj; 241466826eb6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 241566826eb6SBarry Smith mxArray *mmat; 241666826eb6SBarry Smith 24176c0721eeSBarry Smith PetscFunctionBegin; 241866826eb6SBarry Smith ierr = PetscFree(aij->a);CHKERRQ(ierr); 241966826eb6SBarry Smith aij->indexshift = 0; 242066826eb6SBarry Smith 242166826eb6SBarry Smith mmat = engGetArray((Engine *)engine,obj->name); 242266826eb6SBarry Smith 2423a65776f3SBarry Smith aij->nz = (mxGetJc(mmat))[mat->m]; 2424*a7ed9263SMatthew Knepley ierr = PetscMalloc(((size_t) aij->nz)*(sizeof(int)+sizeof(PetscScalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr); 242566826eb6SBarry Smith aij->j = (int*)(aij->a + aij->nz); 242666826eb6SBarry Smith aij->i = aij->j + aij->nz; 242766826eb6SBarry Smith aij->singlemalloc = PETSC_TRUE; 242866826eb6SBarry Smith aij->freedata = PETSC_TRUE; 242966826eb6SBarry Smith 243087828ca2SBarry Smith ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));CHKERRQ(ierr); 243166826eb6SBarry Smith /* Matlab stores by column, not row so we pass in the transpose of the matrix */ 243266826eb6SBarry Smith ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr); 243366826eb6SBarry Smith ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr); 243466826eb6SBarry Smith 243566826eb6SBarry Smith for (ii=0; ii<mat->m; ii++) { 243666826eb6SBarry Smith aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii]; 243766826eb6SBarry Smith } 243866826eb6SBarry Smith 243966826eb6SBarry Smith ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 244066826eb6SBarry Smith ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 244166826eb6SBarry Smith 244266826eb6SBarry Smith PetscFunctionReturn(0); 244366826eb6SBarry Smith } 244466826eb6SBarry Smith EXTERN_C_END 2445f83d6046SBarry Smith #endif 2446f83d6046SBarry Smith 2447be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 24484a2ae208SSatish Balay #undef __FUNCT__ 24494a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ" 245017ab2063SBarry Smith /*@C 2451682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 24520d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 24536e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 245451c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 24552bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 245617ab2063SBarry Smith 2457db81eaa0SLois Curfman McInnes Collective on MPI_Comm 2458db81eaa0SLois Curfman McInnes 245917ab2063SBarry Smith Input Parameters: 2460db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 246117ab2063SBarry Smith . m - number of rows 246217ab2063SBarry Smith . n - number of columns 246317ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 246451c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 24652bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 246617ab2063SBarry Smith 246717ab2063SBarry Smith Output Parameter: 2468416022c9SBarry Smith . A - the matrix 246917ab2063SBarry Smith 2470b259b22eSLois Curfman McInnes Notes: 247117ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 247217ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 24730002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 247444cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 247517ab2063SBarry Smith 247617ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2477a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 24783d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 24796da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 248017ab2063SBarry Smith 2481682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 24824fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 2483682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 24846c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 24856c7ebb05SLois Curfman McInnes 24866c7ebb05SLois Curfman McInnes Options Database Keys: 2487db81eaa0SLois Curfman McInnes + -mat_aij_no_inode - Do not use inodes 2488db81eaa0SLois Curfman McInnes . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 2489db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 2490db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 2491db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 249217ab2063SBarry Smith 2493027ccd11SLois Curfman McInnes Level: intermediate 2494027ccd11SLois Curfman McInnes 249536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 249636db0b34SBarry Smith 249717ab2063SBarry Smith @*/ 2498416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A) 249917ab2063SBarry Smith { 2500273d9f13SBarry Smith int ierr; 25016945ee14SBarry Smith 25023a40ed3dSBarry Smith PetscFunctionBegin; 2503273d9f13SBarry Smith ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr); 2504273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 2505273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr); 2506273d9f13SBarry Smith PetscFunctionReturn(0); 2507273d9f13SBarry Smith } 2508273d9f13SBarry Smith 25095da197adSKris Buschelman #define SKIP_ALLOCATION -4 25105da197adSKris Buschelman 25114a2ae208SSatish Balay #undef __FUNCT__ 25124a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation" 2513273d9f13SBarry Smith /*@C 2514273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 2515273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 2516273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 2517273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 2518273d9f13SBarry Smith 2519273d9f13SBarry Smith Collective on MPI_Comm 2520273d9f13SBarry Smith 2521273d9f13SBarry Smith Input Parameters: 2522273d9f13SBarry Smith + comm - MPI communicator, set to PETSC_COMM_SELF 2523273d9f13SBarry Smith . m - number of rows 2524273d9f13SBarry Smith . n - number of columns 2525273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 2526273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 2527273d9f13SBarry Smith (possibly different for each row) or PETSC_NULL 2528273d9f13SBarry Smith 2529273d9f13SBarry Smith Output Parameter: 2530273d9f13SBarry Smith . A - the matrix 2531273d9f13SBarry Smith 2532273d9f13SBarry Smith Notes: 2533273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2534273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2535273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2536273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 2537273d9f13SBarry Smith 2538273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2539273d9f13SBarry Smith Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 2540273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 2541273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 2542273d9f13SBarry Smith 2543273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 2544273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 2545273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 2546273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2547273d9f13SBarry Smith 2548273d9f13SBarry Smith Options Database Keys: 2549273d9f13SBarry Smith + -mat_aij_no_inode - Do not use inodes 2550273d9f13SBarry Smith . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 2551273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2552273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2553273d9f13SBarry Smith the user still MUST index entries starting at 0! 2554273d9f13SBarry Smith 2555273d9f13SBarry Smith Level: intermediate 2556273d9f13SBarry Smith 2557273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 2558273d9f13SBarry Smith 2559273d9f13SBarry Smith @*/ 2560273d9f13SBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,int *nnz) 2561273d9f13SBarry Smith { 2562273d9f13SBarry Smith Mat_SeqAIJ *b; 2563*a7ed9263SMatthew Knepley size_t len = 0; 2564c461c341SBarry Smith PetscTruth flg2,skipallocation = PETSC_FALSE; 2565*a7ed9263SMatthew Knepley int i,ierr; 2566273d9f13SBarry Smith 2567273d9f13SBarry Smith PetscFunctionBegin; 2568273d9f13SBarry Smith ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg2);CHKERRQ(ierr); 2569273d9f13SBarry Smith if (!flg2) PetscFunctionReturn(0); 2570d5d45c9bSBarry Smith 2571c461c341SBarry Smith if (nz == SKIP_ALLOCATION) { 2572c461c341SBarry Smith skipallocation = PETSC_TRUE; 2573c461c341SBarry Smith nz = 0; 2574c461c341SBarry Smith } 2575c461c341SBarry Smith 2576435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 2577435da068SBarry Smith if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz); 2578b73539f3SBarry Smith if (nnz) { 2579273d9f13SBarry Smith for (i=0; i<B->m; i++) { 258029bbc08cSBarry Smith if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]); 25813a7fca6bSBarry Smith if (nnz[i] > B->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->n); 2582b73539f3SBarry Smith } 2583b73539f3SBarry Smith } 2584b73539f3SBarry Smith 2585273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 2586273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 2587273d9f13SBarry Smith 2588b0a32e0cSBarry Smith ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr); 2589273d9f13SBarry Smith if (!nnz) { 2590435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 2591273d9f13SBarry Smith else if (nz <= 0) nz = 1; 2592273d9f13SBarry Smith for (i=0; i<B->m; i++) b->imax[i] = nz; 2593273d9f13SBarry Smith nz = nz*B->m; 2594273d9f13SBarry Smith } else { 2595273d9f13SBarry Smith nz = 0; 2596273d9f13SBarry Smith for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 2597273d9f13SBarry Smith } 2598273d9f13SBarry Smith 2599c461c341SBarry Smith if (!skipallocation) { 2600273d9f13SBarry Smith /* allocate the matrix space */ 2601*a7ed9263SMatthew Knepley len = ((size_t) nz)*(sizeof(int) + sizeof(PetscScalar)) + (B->m+1)*sizeof(int); 2602b0a32e0cSBarry Smith ierr = PetscMalloc(len,&b->a);CHKERRQ(ierr); 2603273d9f13SBarry Smith b->j = (int*)(b->a + nz); 2604273d9f13SBarry Smith ierr = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr); 2605273d9f13SBarry Smith b->i = b->j + nz; 26065da197adSKris Buschelman b->i[0] = -b->indexshift; 26075da197adSKris Buschelman for (i=1; i<B->m+1; i++) { 26085da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 26095da197adSKris Buschelman } 2610273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 2611273d9f13SBarry Smith b->freedata = PETSC_TRUE; 2612c461c341SBarry Smith } else { 2613c461c341SBarry Smith b->freedata = PETSC_FALSE; 2614c461c341SBarry Smith } 2615273d9f13SBarry Smith 2616273d9f13SBarry Smith /* b->ilen will count nonzeros in each row so far. */ 2617b0a32e0cSBarry Smith ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr); 2618b0a32e0cSBarry Smith PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 2619273d9f13SBarry Smith for (i=0; i<B->m; i++) { b->ilen[i] = 0;} 2620273d9f13SBarry Smith 2621273d9f13SBarry Smith b->nz = 0; 2622273d9f13SBarry Smith b->maxnz = nz; 2623273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 2624273d9f13SBarry Smith PetscFunctionReturn(0); 2625273d9f13SBarry Smith } 2626273d9f13SBarry Smith 2627eb9c0419SKris Buschelman EXTERN int RegisterApplyPtAPRoutines_Private(Mat); 2628eb9c0419SKris Buschelman 2629273d9f13SBarry Smith EXTERN_C_BEGIN 26304a2ae208SSatish Balay #undef __FUNCT__ 26314a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ" 2632273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B) 2633273d9f13SBarry Smith { 2634273d9f13SBarry Smith Mat_SeqAIJ *b; 2635273d9f13SBarry Smith int ierr,size; 2636273d9f13SBarry Smith PetscTruth flg; 2637273d9f13SBarry Smith 2638273d9f13SBarry Smith PetscFunctionBegin; 2639273d9f13SBarry Smith ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr); 2640273d9f13SBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 2641273d9f13SBarry Smith 2642273d9f13SBarry Smith B->m = B->M = PetscMax(B->m,B->M); 2643273d9f13SBarry Smith B->n = B->N = PetscMax(B->n,B->N); 2644273d9f13SBarry Smith 2645b0a32e0cSBarry Smith ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr); 2646b0a32e0cSBarry Smith B->data = (void*)b; 2647549d3d68SSatish Balay ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr); 2648549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 2649416022c9SBarry Smith B->factor = 0; 2650416022c9SBarry Smith B->lupivotthreshold = 1.0; 265190f02eecSBarry Smith B->mapping = 0; 265287828ca2SBarry Smith ierr = PetscOptionsGetReal(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr); 2653b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr); 2654416022c9SBarry Smith b->row = 0; 2655416022c9SBarry Smith b->col = 0; 265682bf6240SBarry Smith b->icol = 0; 2657416022c9SBarry Smith b->indexshift = 0; 2658b810aeb4SBarry Smith b->reallocs = 0; 2659b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_oneindex",&flg);CHKERRQ(ierr); 266069957df2SSatish Balay if (flg) b->indexshift = -1; 266117ab2063SBarry Smith 26628a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr); 26638a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr); 2664a5ae1ecdSBarry Smith 2665f1e2ffcdSBarry Smith b->sorted = PETSC_FALSE; 266636db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 2667f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 2668416022c9SBarry Smith b->nonew = 0; 2669416022c9SBarry Smith b->diag = 0; 2670416022c9SBarry Smith b->solve_work = 0; 26712a1b7f2aSHong Zhang B->spptr = 0; 2672b65db4caSBarry Smith b->inode.use = PETSC_TRUE; 2673754ec7b1SSatish Balay b->inode.node_count = 0; 2674754ec7b1SSatish Balay b->inode.size = 0; 26756c7ebb05SLois Curfman McInnes b->inode.limit = 5; 26766c7ebb05SLois Curfman McInnes b->inode.max_limit = 5; 2677be6bf707SBarry Smith b->saved_values = 0; 2678d7f994e1SBarry Smith b->idiag = 0; 2679d7f994e1SBarry Smith b->ssor = 0; 2680f1e2ffcdSBarry Smith b->keepzeroedrows = PETSC_FALSE; 268117ab2063SBarry Smith 268235d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 268335d8aa7fSBarry Smith 2684aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU) 2685b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu",&flg);CHKERRQ(ierr); 268669957df2SSatish Balay if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); } 26874b14c69eSBarry Smith #endif 2688b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_essl",&flg);CHKERRQ(ierr); 268969957df2SSatish Balay if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); } 2690b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_lusol",&flg);CHKERRQ(ierr); 2691cd5578b5SBarry Smith if (flg) { ierr = MatUseLUSOL_SeqAIJ(B);CHKERRQ(ierr); } 2692b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_matlab",&flg);CHKERRQ(ierr); 2693ca44d042SBarry Smith if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);} 2694b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_dxml",&flg);CHKERRQ(ierr); 269569957df2SSatish Balay if (flg) { 269629bbc08cSBarry Smith if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,"need -mat_aij_oneindex with -mat_aij_dxml"); 2697416022c9SBarry Smith ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr); 269817ab2063SBarry Smith } 2699f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C", 2700bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 2701bc4b532fSSatish Balay MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 2702f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 2703be6bf707SBarry Smith "MatStoreValues_SeqAIJ", 2704bc4b532fSSatish Balay MatStoreValues_SeqAIJ);CHKERRQ(ierr); 2705f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 2706be6bf707SBarry Smith "MatRetrieveValues_SeqAIJ", 2707bc4b532fSSatish Balay MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 270887828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 2709f83d6046SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr); 271066826eb6SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr); 2711f83d6046SBarry Smith #endif 2712eb9c0419SKris Buschelman ierr = RegisterApplyPtAPRoutines_Private(B);CHKERRQ(ierr); 27133a40ed3dSBarry Smith PetscFunctionReturn(0); 271417ab2063SBarry Smith } 2715273d9f13SBarry Smith EXTERN_C_END 271617ab2063SBarry Smith 27174a2ae208SSatish Balay #undef __FUNCT__ 27184a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ" 2719be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 272017ab2063SBarry Smith { 2721416022c9SBarry Smith Mat C; 2722416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 2723*a7ed9263SMatthew Knepley int i,m = A->m,shift = a->indexshift,ierr; 2724*a7ed9263SMatthew Knepley size_t len; 272517ab2063SBarry Smith 27263a40ed3dSBarry Smith PetscFunctionBegin; 27274043dd9cSLois Curfman McInnes *B = 0; 2728273d9f13SBarry Smith ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr); 2729273d9f13SBarry Smith ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr); 2730273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 2731273d9f13SBarry Smith 2732416022c9SBarry Smith C->factor = A->factor; 2733416022c9SBarry Smith c->row = 0; 2734416022c9SBarry Smith c->col = 0; 273582bf6240SBarry Smith c->icol = 0; 2736416022c9SBarry Smith c->indexshift = shift; 2737f1e2ffcdSBarry Smith c->keepzeroedrows = a->keepzeroedrows; 2738c456f294SBarry Smith C->assembled = PETSC_TRUE; 273917ab2063SBarry Smith 2740273d9f13SBarry Smith C->M = A->m; 2741273d9f13SBarry Smith C->N = A->n; 274217ab2063SBarry Smith 2743b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr); 2744b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr); 274517ab2063SBarry Smith for (i=0; i<m; i++) { 2746416022c9SBarry Smith c->imax[i] = a->imax[i]; 2747416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 274817ab2063SBarry Smith } 274917ab2063SBarry Smith 275017ab2063SBarry Smith /* allocate the matrix space */ 2751f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 2752*a7ed9263SMatthew Knepley len = ((size_t) (m+1))*sizeof(int)+(a->i[m])*(sizeof(PetscScalar)+sizeof(int)); 2753b0a32e0cSBarry Smith ierr = PetscMalloc(len,&c->a);CHKERRQ(ierr); 2754416022c9SBarry Smith c->j = (int*)(c->a + a->i[m] + shift); 2755416022c9SBarry Smith c->i = c->j + a->i[m] + shift; 2756549d3d68SSatish Balay ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr); 275717ab2063SBarry Smith if (m > 0) { 2758549d3d68SSatish Balay ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr); 2759be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 276087828ca2SBarry Smith ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr); 2761be6bf707SBarry Smith } else { 276287828ca2SBarry Smith ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr); 276317ab2063SBarry Smith } 276408480c60SBarry Smith } 276517ab2063SBarry Smith 2766b0a32e0cSBarry Smith PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 2767416022c9SBarry Smith c->sorted = a->sorted; 2768416022c9SBarry Smith c->roworiented = a->roworiented; 2769416022c9SBarry Smith c->nonew = a->nonew; 27707a743949SBarry Smith c->ilu_preserve_row_sums = a->ilu_preserve_row_sums; 2771be6bf707SBarry Smith c->saved_values = 0; 2772d7f994e1SBarry Smith c->idiag = 0; 2773d7f994e1SBarry Smith c->ssor = 0; 277436db0b34SBarry Smith c->ignorezeroentries = a->ignorezeroentries; 277536db0b34SBarry Smith c->freedata = PETSC_TRUE; 277617ab2063SBarry Smith 2777416022c9SBarry Smith if (a->diag) { 2778b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr); 2779b0a32e0cSBarry Smith PetscLogObjectMemory(C,(m+1)*sizeof(int)); 278017ab2063SBarry Smith for (i=0; i<m; i++) { 2781416022c9SBarry Smith c->diag[i] = a->diag[i]; 278217ab2063SBarry Smith } 27833a40ed3dSBarry Smith } else c->diag = 0; 2784b65db4caSBarry Smith c->inode.use = a->inode.use; 27856c7ebb05SLois Curfman McInnes c->inode.limit = a->inode.limit; 27866c7ebb05SLois Curfman McInnes c->inode.max_limit = a->inode.max_limit; 2787754ec7b1SSatish Balay if (a->inode.size){ 2788b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr); 2789754ec7b1SSatish Balay c->inode.node_count = a->inode.node_count; 2790549d3d68SSatish Balay ierr = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr); 2791754ec7b1SSatish Balay } else { 2792754ec7b1SSatish Balay c->inode.size = 0; 2793754ec7b1SSatish Balay c->inode.node_count = 0; 2794754ec7b1SSatish Balay } 2795416022c9SBarry Smith c->nz = a->nz; 2796416022c9SBarry Smith c->maxnz = a->maxnz; 2797416022c9SBarry Smith c->solve_work = 0; 27982a1b7f2aSHong Zhang C->spptr = 0; /* Dangerous -I'm throwing away a->spptr */ 2799273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 2800754ec7b1SSatish Balay 2801416022c9SBarry Smith *B = C; 2802b0a32e0cSBarry Smith ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr); 28033a40ed3dSBarry Smith PetscFunctionReturn(0); 280417ab2063SBarry Smith } 280517ab2063SBarry Smith 2806273d9f13SBarry Smith EXTERN_C_BEGIN 28074a2ae208SSatish Balay #undef __FUNCT__ 28084a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ" 2809b0a32e0cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,MatType type,Mat *A) 281017ab2063SBarry Smith { 2811416022c9SBarry Smith Mat_SeqAIJ *a; 2812416022c9SBarry Smith Mat B; 281317699dbbSLois Curfman McInnes int i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift; 2814bcd2baecSBarry Smith MPI_Comm comm; 281517ab2063SBarry Smith 28163a40ed3dSBarry Smith PetscFunctionBegin; 2817e864ced6SBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 2818d132466eSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 281929bbc08cSBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor"); 2820b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 28210752156aSBarry Smith ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 2822552e946dSBarry Smith if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 282317ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 282417ab2063SBarry Smith 2825d64ed03dSBarry Smith if (nz < 0) { 282629bbc08cSBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 2827d64ed03dSBarry Smith } 2828d64ed03dSBarry Smith 282917ab2063SBarry Smith /* read in row lengths */ 2830b0a32e0cSBarry Smith ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr); 28310752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 283217ab2063SBarry Smith 283317ab2063SBarry Smith /* create our matrix */ 2834416022c9SBarry Smith ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr); 2835416022c9SBarry Smith B = *A; 2836416022c9SBarry Smith a = (Mat_SeqAIJ*)B->data; 2837416022c9SBarry Smith shift = a->indexshift; 283817ab2063SBarry Smith 283917ab2063SBarry Smith /* read in column indices and adjust for Fortran indexing*/ 28400752156aSBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 284117ab2063SBarry Smith if (shift) { 284217ab2063SBarry Smith for (i=0; i<nz; i++) { 2843416022c9SBarry Smith a->j[i] += 1; 284417ab2063SBarry Smith } 284517ab2063SBarry Smith } 284617ab2063SBarry Smith 284717ab2063SBarry Smith /* read in nonzero values */ 28480752156aSBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 284917ab2063SBarry Smith 285017ab2063SBarry Smith /* set matrix "i" values */ 2851416022c9SBarry Smith a->i[0] = -shift; 285217ab2063SBarry Smith for (i=1; i<= M; i++) { 2853416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 2854416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 285517ab2063SBarry Smith } 2856606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 285717ab2063SBarry Smith 28586d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28596d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28603a40ed3dSBarry Smith PetscFunctionReturn(0); 286117ab2063SBarry Smith } 2862273d9f13SBarry Smith EXTERN_C_END 286317ab2063SBarry Smith 28644a2ae208SSatish Balay #undef __FUNCT__ 2865b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ" 28668f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg) 28677264ac53SSatish Balay { 28687264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data; 28690f5bd95cSBarry Smith int ierr; 2870273d9f13SBarry Smith PetscTruth flag; 28717264ac53SSatish Balay 28723a40ed3dSBarry Smith PetscFunctionBegin; 2873273d9f13SBarry Smith ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flag);CHKERRQ(ierr); 2874273d9f13SBarry Smith if (!flag) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type"); 28757264ac53SSatish Balay 28767264ac53SSatish Balay /* If the matrix dimensions are not equal,or no of nonzeros or shift */ 2877273d9f13SBarry Smith if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)|| (a->indexshift != b->indexshift)) { 2878ca44d042SBarry Smith *flg = PETSC_FALSE; 2879ca44d042SBarry Smith PetscFunctionReturn(0); 2880bcd2baecSBarry Smith } 28817264ac53SSatish Balay 28827264ac53SSatish Balay /* if the a->i are the same */ 2883273d9f13SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr); 28840f5bd95cSBarry Smith if (*flg == PETSC_FALSE) PetscFunctionReturn(0); 28857264ac53SSatish Balay 28867264ac53SSatish Balay /* if a->j are the same */ 28870f5bd95cSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr); 28880f5bd95cSBarry Smith if (*flg == PETSC_FALSE) PetscFunctionReturn(0); 2889bcd2baecSBarry Smith 2890bcd2baecSBarry Smith /* if a->a are the same */ 289187828ca2SBarry Smith ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 28920f5bd95cSBarry Smith 28933a40ed3dSBarry Smith PetscFunctionReturn(0); 28947264ac53SSatish Balay 28957264ac53SSatish Balay } 289636db0b34SBarry Smith 28974a2ae208SSatish Balay #undef __FUNCT__ 28984a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays" 289936db0b34SBarry Smith /*@C 290036db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 290136db0b34SBarry Smith provided by the user. 290236db0b34SBarry Smith 290336db0b34SBarry Smith Coolective on MPI_Comm 290436db0b34SBarry Smith 290536db0b34SBarry Smith Input Parameters: 290636db0b34SBarry Smith + comm - must be an MPI communicator of size 1 290736db0b34SBarry Smith . m - number of rows 290836db0b34SBarry Smith . n - number of columns 290936db0b34SBarry Smith . i - row indices 291036db0b34SBarry Smith . j - column indices 291136db0b34SBarry Smith - a - matrix values 291236db0b34SBarry Smith 291336db0b34SBarry Smith Output Parameter: 291436db0b34SBarry Smith . mat - the matrix 291536db0b34SBarry Smith 291636db0b34SBarry Smith Level: intermediate 291736db0b34SBarry Smith 291836db0b34SBarry Smith Notes: 29190551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 292036db0b34SBarry Smith once the matrix is destroyed 292136db0b34SBarry Smith 292236db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 292336db0b34SBarry Smith 292436db0b34SBarry Smith The i and j indices can be either 0- or 1 based 292536db0b34SBarry Smith 292636db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ() 292736db0b34SBarry Smith 292836db0b34SBarry Smith @*/ 292987828ca2SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,PetscScalar *a,Mat *mat) 293036db0b34SBarry Smith { 293136db0b34SBarry Smith int ierr,ii; 293236db0b34SBarry Smith Mat_SeqAIJ *aij; 293336db0b34SBarry Smith 293436db0b34SBarry Smith PetscFunctionBegin; 2935c461c341SBarry Smith ierr = MatCreateSeqAIJ(comm,m,n,SKIP_ALLOCATION,0,mat);CHKERRQ(ierr); 293636db0b34SBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 293736db0b34SBarry Smith 293836db0b34SBarry Smith if (i[0] == 1) { 293936db0b34SBarry Smith aij->indexshift = -1; 294036db0b34SBarry Smith } else if (i[0]) { 294129bbc08cSBarry Smith SETERRQ(1,"i (row indices) do not start with 0 or 1"); 294236db0b34SBarry Smith } 294336db0b34SBarry Smith aij->i = i; 294436db0b34SBarry Smith aij->j = j; 294536db0b34SBarry Smith aij->a = a; 294636db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 294736db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 294836db0b34SBarry Smith aij->freedata = PETSC_FALSE; 294936db0b34SBarry Smith 295036db0b34SBarry Smith for (ii=0; ii<m; ii++) { 295136db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 29529860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g) 295329bbc08cSBarry Smith if (i[ii+1] - i[ii] < 0) SETERRQ2(1,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]); 295436db0b34SBarry Smith #endif 295536db0b34SBarry Smith } 29569860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g) 295736db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 295829bbc08cSBarry Smith if (j[ii] < -aij->indexshift) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]); 295929bbc08cSBarry Smith if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]); 296036db0b34SBarry Smith } 296136db0b34SBarry Smith #endif 296236db0b34SBarry Smith 2963b65db4caSBarry Smith /* changes indices to start at 0 */ 2964b65db4caSBarry Smith if (i[0]) { 2965b65db4caSBarry Smith aij->indexshift = 0; 2966b65db4caSBarry Smith for (ii=0; ii<m; ii++) { 2967b65db4caSBarry Smith i[ii]--; 2968b65db4caSBarry Smith } 2969b65db4caSBarry Smith for (ii=0; ii<i[m]; ii++) { 2970b65db4caSBarry Smith j[ii]--; 2971b65db4caSBarry Smith } 2972b65db4caSBarry Smith } 2973b65db4caSBarry Smith 2974b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2975b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 297636db0b34SBarry Smith PetscFunctionReturn(0); 297736db0b34SBarry Smith } 297836db0b34SBarry Smith 2979cc8ba8e1SBarry Smith #undef __FUNCT__ 2980ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ" 2981ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring) 2982cc8ba8e1SBarry Smith { 2983cc8ba8e1SBarry Smith int ierr; 2984cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 298536db0b34SBarry Smith 2986cc8ba8e1SBarry Smith PetscFunctionBegin; 298712c595b3SBarry Smith if (coloring->ctype == IS_COLORING_LOCAL) { 2988cc8ba8e1SBarry Smith ierr = ISColoringReference(coloring);CHKERRQ(ierr); 2989cc8ba8e1SBarry Smith a->coloring = coloring; 299012c595b3SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 299112c595b3SBarry Smith int *colors,i,*larray; 299212c595b3SBarry Smith ISColoring ocoloring; 299312c595b3SBarry Smith 299412c595b3SBarry Smith /* set coloring for diagonal portion */ 299512c595b3SBarry Smith ierr = PetscMalloc((A->n+1)*sizeof(int),&larray);CHKERRQ(ierr); 299612c595b3SBarry Smith for (i=0; i<A->n; i++) { 299712c595b3SBarry Smith larray[i] = i; 299812c595b3SBarry Smith } 299912c595b3SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 300012c595b3SBarry Smith ierr = PetscMalloc((A->n+1)*sizeof(int),&colors);CHKERRQ(ierr); 300112c595b3SBarry Smith for (i=0; i<A->n; i++) { 300212c595b3SBarry Smith colors[i] = coloring->colors[larray[i]]; 300312c595b3SBarry Smith } 300412c595b3SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 300512c595b3SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr); 300612c595b3SBarry Smith a->coloring = ocoloring; 300712c595b3SBarry Smith } 3008cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3009cc8ba8e1SBarry Smith } 3010cc8ba8e1SBarry Smith 301187828ca2SBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 3012ee4f033dSBarry Smith EXTERN_C_BEGIN 301329c1e371SBarry Smith #include "adic/ad_utils.h" 3014ee4f033dSBarry Smith EXTERN_C_END 3015cc8ba8e1SBarry Smith 3016cc8ba8e1SBarry Smith #undef __FUNCT__ 3017ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 3018ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 3019cc8ba8e1SBarry Smith { 3020cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3021ee4f033dSBarry Smith int m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j,nlen; 302287828ca2SBarry Smith PetscScalar *v = a->a,*values; 3023ee4f033dSBarry Smith char *cadvalues = (char *)advalues; 3024cc8ba8e1SBarry Smith 3025cc8ba8e1SBarry Smith PetscFunctionBegin; 3026cc8ba8e1SBarry Smith if (!a->coloring) SETERRQ(1,"Coloring not set for matrix"); 302729c1e371SBarry Smith nlen = PetscADGetDerivTypeSize(); 3028cc8ba8e1SBarry Smith color = a->coloring->colors; 3029cc8ba8e1SBarry Smith /* loop over rows */ 3030cc8ba8e1SBarry Smith for (i=0; i<m; i++) { 3031cc8ba8e1SBarry Smith nz = ii[i+1] - ii[i]; 3032cc8ba8e1SBarry Smith /* loop over columns putting computed value into matrix */ 303329c1e371SBarry Smith values = PetscADGetGradArray(cadvalues); 3034cc8ba8e1SBarry Smith for (j=0; j<nz; j++) { 3035cc8ba8e1SBarry Smith *v++ = values[color[*jj++]]; 3036cc8ba8e1SBarry Smith } 3037ee4f033dSBarry Smith cadvalues += nlen; /* jump to next row of derivatives */ 3038ee4f033dSBarry Smith } 3039ee4f033dSBarry Smith PetscFunctionReturn(0); 3040ee4f033dSBarry Smith } 3041ee4f033dSBarry Smith 3042ee4f033dSBarry Smith #else 3043ee4f033dSBarry Smith 3044ee4f033dSBarry Smith #undef __FUNCT__ 3045ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 3046ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 3047ee4f033dSBarry Smith { 3048ee4f033dSBarry Smith PetscFunctionBegin; 3049ee4f033dSBarry Smith SETERRQ(1,"PETSc installed without ADIC"); 3050ee4f033dSBarry Smith } 3051ee4f033dSBarry Smith 3052ee4f033dSBarry Smith #endif 3053ee4f033dSBarry Smith 3054ee4f033dSBarry Smith #undef __FUNCT__ 3055ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ" 3056ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues) 3057ee4f033dSBarry Smith { 3058ee4f033dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3059ee4f033dSBarry Smith int m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j; 306087828ca2SBarry Smith PetscScalar *v = a->a,*values = (PetscScalar *)advalues; 3061ee4f033dSBarry Smith 3062ee4f033dSBarry Smith PetscFunctionBegin; 3063ee4f033dSBarry Smith if (!a->coloring) SETERRQ(1,"Coloring not set for matrix"); 3064ee4f033dSBarry Smith color = a->coloring->colors; 3065ee4f033dSBarry Smith /* loop over rows */ 3066ee4f033dSBarry Smith for (i=0; i<m; i++) { 3067ee4f033dSBarry Smith nz = ii[i+1] - ii[i]; 3068ee4f033dSBarry Smith /* loop over columns putting computed value into matrix */ 3069ee4f033dSBarry Smith for (j=0; j<nz; j++) { 3070ee4f033dSBarry Smith *v++ = values[color[*jj++]]; 3071ee4f033dSBarry Smith } 3072ee4f033dSBarry Smith values += nl; /* jump to next row of derivatives */ 3073cc8ba8e1SBarry Smith } 3074cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3075cc8ba8e1SBarry Smith } 307636db0b34SBarry Smith 3077