1992c00aaSSatish Balay #define PETSCMAT_DLL 2b3cc6726SBarry Smith 3b377110cSBarry Smith 4d5d45c9bSBarry Smith /* 53369ce9aSBarry Smith Defines the basic matrix operations for the AIJ (compressed row) 6d5d45c9bSBarry Smith matrix storage format. 7d5d45c9bSBarry Smith */ 83369ce9aSBarry Smith 97c4f633dSBarry Smith 107c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h" /*I "petscmat.h" I*/ 11f3da1532SBarry Smith #include "petscblaslapack.h" 120a835dfdSSatish Balay #include "petscbt.h" 1317ab2063SBarry Smith 144a2ae208SSatish Balay #undef __FUNCT__ 1579299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ" 1679299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is) 1779299369SBarry Smith { 1879299369SBarry Smith PetscErrorCode ierr; 1979299369SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) Y->data; 20d0f46423SBarry Smith PetscInt i,*diag, m = Y->rmap->n; 2154f21887SBarry Smith MatScalar *aa = aij->a; 2254f21887SBarry Smith PetscScalar *v; 2309f38230SBarry Smith PetscTruth missing; 2479299369SBarry Smith 2579299369SBarry Smith PetscFunctionBegin; 2609f38230SBarry Smith if (Y->assembled) { 2709f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr); 2809f38230SBarry Smith if (!missing) { 2979299369SBarry Smith diag = aij->diag; 3079299369SBarry Smith ierr = VecGetArray(D,&v);CHKERRQ(ierr); 3179299369SBarry Smith if (is == INSERT_VALUES) { 3279299369SBarry Smith for (i=0; i<m; i++) { 3379299369SBarry Smith aa[diag[i]] = v[i]; 3479299369SBarry Smith } 3579299369SBarry Smith } else { 3679299369SBarry Smith for (i=0; i<m; i++) { 3779299369SBarry Smith aa[diag[i]] += v[i]; 3879299369SBarry Smith } 3979299369SBarry Smith } 4079299369SBarry Smith ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 4179299369SBarry Smith PetscFunctionReturn(0); 4279299369SBarry Smith } 4309f38230SBarry Smith } 4409f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 4509f38230SBarry Smith PetscFunctionReturn(0); 4609f38230SBarry Smith } 4779299369SBarry Smith 4879299369SBarry Smith #undef __FUNCT__ 494a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ" 503acb8795SBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 5117ab2063SBarry Smith { 52416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 53dfbe8321SBarry Smith PetscErrorCode ierr; 5497f1f81fSBarry Smith PetscInt i,ishift; 5517ab2063SBarry Smith 563a40ed3dSBarry Smith PetscFunctionBegin; 57d0f46423SBarry Smith *m = A->rmap->n; 583a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 59bfeeae90SHong Zhang ishift = 0; 6053e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 61d0f46423SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 62bfeeae90SHong Zhang } else if (oshift == 1) { 63d0f46423SBarry Smith PetscInt nz = a->i[A->rmap->n]; 643b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 65d0f46423SBarry Smith ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr); 66d0f46423SBarry Smith for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1; 67ecc77c7aSBarry Smith if (ja) { 6897f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr); 693b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1; 70ecc77c7aSBarry Smith } 716945ee14SBarry Smith } else { 72ecc77c7aSBarry Smith *ia = a->i; 73ecc77c7aSBarry Smith if (ja) *ja = a->j; 74a2ce50c7SBarry Smith } 753a40ed3dSBarry Smith PetscFunctionReturn(0); 76a2744918SBarry Smith } 77a2744918SBarry Smith 784a2ae208SSatish Balay #undef __FUNCT__ 794a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ" 803acb8795SBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 816945ee14SBarry Smith { 82dfbe8321SBarry Smith PetscErrorCode ierr; 836945ee14SBarry Smith 843a40ed3dSBarry Smith PetscFunctionBegin; 853a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 86bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 87606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 88ecc77c7aSBarry Smith if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);} 89bcd2baecSBarry Smith } 903a40ed3dSBarry Smith PetscFunctionReturn(0); 9117ab2063SBarry Smith } 9217ab2063SBarry Smith 934a2ae208SSatish Balay #undef __FUNCT__ 944a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ" 953acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 963b2fbd54SBarry Smith { 973b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 98dfbe8321SBarry Smith PetscErrorCode ierr; 99d0f46423SBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 10097f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 1013b2fbd54SBarry Smith 1023a40ed3dSBarry Smith PetscFunctionBegin; 103899cda47SBarry Smith *nn = n; 1043a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1053b2fbd54SBarry Smith if (symmetric) { 106d0f46423SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr); 1073b2fbd54SBarry Smith } else { 10897f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr); 10997f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 11097f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr); 11197f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr); 1123b2fbd54SBarry Smith jj = a->j; 1133b2fbd54SBarry Smith for (i=0; i<nz; i++) { 114bfeeae90SHong Zhang collengths[jj[i]]++; 1153b2fbd54SBarry Smith } 1163b2fbd54SBarry Smith cia[0] = oshift; 1173b2fbd54SBarry Smith for (i=0; i<n; i++) { 1183b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 1193b2fbd54SBarry Smith } 12097f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 1213b2fbd54SBarry Smith jj = a->j; 122a93ec695SBarry Smith for (row=0; row<m; row++) { 123a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 124a93ec695SBarry Smith for (i=0; i<mr; i++) { 125bfeeae90SHong Zhang col = *jj++; 1263b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 1273b2fbd54SBarry Smith } 1283b2fbd54SBarry Smith } 129606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 1303b2fbd54SBarry Smith *ia = cia; *ja = cja; 1313b2fbd54SBarry Smith } 1323a40ed3dSBarry Smith PetscFunctionReturn(0); 1333b2fbd54SBarry Smith } 1343b2fbd54SBarry Smith 1354a2ae208SSatish Balay #undef __FUNCT__ 1364a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ" 1373acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 1383b2fbd54SBarry Smith { 139dfbe8321SBarry Smith PetscErrorCode ierr; 140606d414cSSatish Balay 1413a40ed3dSBarry Smith PetscFunctionBegin; 1423a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1433b2fbd54SBarry Smith 144606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 145606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 1463b2fbd54SBarry Smith 1473a40ed3dSBarry Smith PetscFunctionReturn(0); 1483b2fbd54SBarry Smith } 1493b2fbd54SBarry Smith 15087d4246cSBarry Smith #undef __FUNCT__ 15187d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ" 15287d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 15387d4246cSBarry Smith { 15487d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15587d4246cSBarry Smith PetscInt *ai = a->i; 15687d4246cSBarry Smith PetscErrorCode ierr; 15787d4246cSBarry Smith 15887d4246cSBarry Smith PetscFunctionBegin; 15987d4246cSBarry Smith ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr); 16087d4246cSBarry Smith PetscFunctionReturn(0); 16187d4246cSBarry Smith } 16287d4246cSBarry Smith 1634a2ae208SSatish Balay #undef __FUNCT__ 1644a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ" 16597f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 16617ab2063SBarry Smith { 167416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 168e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 16997f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 1706849ba73SBarry Smith PetscErrorCode ierr; 171e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 17254f21887SBarry Smith MatScalar *ap,value,*aa = a->a; 173edb03aefSBarry Smith PetscTruth ignorezeroentries = a->ignorezeroentries; 174273d9f13SBarry Smith PetscTruth roworiented = a->roworiented; 17517ab2063SBarry Smith 1763a40ed3dSBarry Smith PetscFunctionBegin; 17771fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 17817ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 179416022c9SBarry Smith row = im[k]; 1805ef9f2a5SBarry Smith if (row < 0) continue; 1812515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 182e32f2f54SBarry Smith if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1); 1833b2fbd54SBarry Smith #endif 184bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 18517ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 186416022c9SBarry Smith low = 0; 187c71e6ed7SBarry Smith high = nrow; 18817ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 1895ef9f2a5SBarry Smith if (in[l] < 0) continue; 1902515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 191e32f2f54SBarry Smith if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1); 1923b2fbd54SBarry Smith #endif 193bfeeae90SHong Zhang col = in[l]; 19416371a99SBarry Smith if (v) { 1954b0e389bSBarry Smith if (roworiented) { 1965ef9f2a5SBarry Smith value = v[l + k*n]; 197bef8e0ddSBarry Smith } else { 1984b0e389bSBarry Smith value = v[k + l*m]; 1994b0e389bSBarry Smith } 20016371a99SBarry Smith } else { 20175567043SBarry Smith value = 0.; 20216371a99SBarry Smith } 203abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 20436db0b34SBarry Smith 2057cd84e04SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 206e2ee6c50SBarry Smith lastcol = col; 207416022c9SBarry Smith while (high-low > 5) { 208416022c9SBarry Smith t = (low+high)/2; 209416022c9SBarry Smith if (rp[t] > col) high = t; 210416022c9SBarry Smith else low = t; 21117ab2063SBarry Smith } 212416022c9SBarry Smith for (i=low; i<high; i++) { 21317ab2063SBarry Smith if (rp[i] > col) break; 21417ab2063SBarry Smith if (rp[i] == col) { 215416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 21617ab2063SBarry Smith else ap[i] = value; 217e44c0bd4SBarry Smith low = i + 1; 21817ab2063SBarry Smith goto noinsert; 21917ab2063SBarry Smith } 22017ab2063SBarry Smith } 221abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 222c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 223e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 224fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 225c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 226416022c9SBarry Smith /* shift up all the later entries in this row */ 227416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 22817ab2063SBarry Smith rp[ii+1] = rp[ii]; 22917ab2063SBarry Smith ap[ii+1] = ap[ii]; 23017ab2063SBarry Smith } 23117ab2063SBarry Smith rp[i] = col; 23217ab2063SBarry Smith ap[i] = value; 233416022c9SBarry Smith low = i + 1; 234e44c0bd4SBarry Smith noinsert:; 23517ab2063SBarry Smith } 23617ab2063SBarry Smith ailen[row] = nrow; 23717ab2063SBarry Smith } 23888e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 2393a40ed3dSBarry Smith PetscFunctionReturn(0); 24017ab2063SBarry Smith } 24117ab2063SBarry Smith 24281824310SBarry Smith 2434a2ae208SSatish Balay #undef __FUNCT__ 2444a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ" 245a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 2467eb43aa7SLois Curfman McInnes { 2477eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 24897f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 24997f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 25054f21887SBarry Smith MatScalar *ap,*aa = a->a; 2517eb43aa7SLois Curfman McInnes 2523a40ed3dSBarry Smith PetscFunctionBegin; 2537eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 2547eb43aa7SLois Curfman McInnes row = im[k]; 255e32f2f54SBarry Smith if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */ 256e32f2f54SBarry Smith if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1); 257bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 2587eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2597eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 260e32f2f54SBarry Smith if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */ 261e32f2f54SBarry Smith if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1); 262bfeeae90SHong Zhang col = in[l] ; 2637eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2647eb43aa7SLois Curfman McInnes while (high-low > 5) { 2657eb43aa7SLois Curfman McInnes t = (low+high)/2; 2667eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2677eb43aa7SLois Curfman McInnes else low = t; 2687eb43aa7SLois Curfman McInnes } 2697eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 2707eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2717eb43aa7SLois Curfman McInnes if (rp[i] == col) { 272b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2737eb43aa7SLois Curfman McInnes goto finished; 2747eb43aa7SLois Curfman McInnes } 2757eb43aa7SLois Curfman McInnes } 27697e567efSBarry Smith *v++ = 0.0; 2777eb43aa7SLois Curfman McInnes finished:; 2787eb43aa7SLois Curfman McInnes } 2797eb43aa7SLois Curfman McInnes } 2803a40ed3dSBarry Smith PetscFunctionReturn(0); 2817eb43aa7SLois Curfman McInnes } 2827eb43aa7SLois Curfman McInnes 28317ab2063SBarry Smith 2844a2ae208SSatish Balay #undef __FUNCT__ 2854a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary" 286dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 28717ab2063SBarry Smith { 288416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2896849ba73SBarry Smith PetscErrorCode ierr; 2906f69ff64SBarry Smith PetscInt i,*col_lens; 2916f69ff64SBarry Smith int fd; 29217ab2063SBarry Smith 2933a40ed3dSBarry Smith PetscFunctionBegin; 294b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 295d0f46423SBarry Smith ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr); 2960700a824SBarry Smith col_lens[0] = MAT_FILE_CLASSID; 297d0f46423SBarry Smith col_lens[1] = A->rmap->n; 298d0f46423SBarry Smith col_lens[2] = A->cmap->n; 299416022c9SBarry Smith col_lens[3] = a->nz; 300416022c9SBarry Smith 301416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 302d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 303416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 30417ab2063SBarry Smith } 305d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 306606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 307416022c9SBarry Smith 308416022c9SBarry Smith /* store column indices (zero start index) */ 3096f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 310416022c9SBarry Smith 311416022c9SBarry Smith /* store nonzero values */ 3126f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 3133a40ed3dSBarry Smith PetscFunctionReturn(0); 31417ab2063SBarry Smith } 315416022c9SBarry Smith 316dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 317cd155464SBarry Smith 3184a2ae208SSatish Balay #undef __FUNCT__ 3194a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII" 320dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 321416022c9SBarry Smith { 322416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 323dfbe8321SBarry Smith PetscErrorCode ierr; 324d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,shift=0; 325e060cb09SBarry Smith const char *name; 326f3ef73ceSBarry Smith PetscViewerFormat format; 32717ab2063SBarry Smith 3283a40ed3dSBarry Smith PetscFunctionBegin; 329435da068SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 330b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 33171c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 33297f1f81fSBarry Smith PetscInt nofinalvalue = 0; 333d0f46423SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) { 334d00d2cf4SBarry Smith nofinalvalue = 1; 335d00d2cf4SBarry Smith } 336b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 337d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr); 33877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 33977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 340b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 34117ab2063SBarry Smith 34217ab2063SBarry Smith for (i=0; i<m; i++) { 343416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 344aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 34577431f27SBarry 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); 34617ab2063SBarry Smith #else 34777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr); 34817ab2063SBarry Smith #endif 34917ab2063SBarry Smith } 35017ab2063SBarry Smith } 351d00d2cf4SBarry Smith if (nofinalvalue) { 352d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr); 353d00d2cf4SBarry Smith } 354fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 355b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 35668369a75SKris Buschelman } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) { 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++) { 36177431f27SBarry 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) { 365a83599f4SBarry 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) { 367a83599f4SBarry 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) { 369a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 3706831982aSBarry Smith } 37144cd7ae7SLois Curfman McInnes #else 372a83599f4SBarry 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) { 37997f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 380b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 38197f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&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; 39577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 3962e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 39777431f27SBarry 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);} 39877431f27SBarry 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);} 39977431f27SBarry 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);} 40077431f27SBarry Smith else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);} 40177431f27SBarry Smith else if (i<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);} 40277431f27SBarry 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++) { 40877431f27SBarry 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) { 42997f1f81fSBarry Smith PetscInt 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; 435d0f46423SBarry Smith for (j=0; j<A->cmap->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); 4513c215bfdSMatthew Knepley } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) { 4523c215bfdSMatthew Knepley ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 4533c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 4543c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr); 4553c215bfdSMatthew Knepley #else 4563c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr); 4573c215bfdSMatthew Knepley #endif 458d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr); 4593c215bfdSMatthew Knepley for (i=0; i<m; i++) { 4603c215bfdSMatthew Knepley for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 4613c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 4623c215bfdSMatthew Knepley if (PetscImaginaryPart(a->a[j]) > 0.0) { 4633c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4643c215bfdSMatthew Knepley } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 4653c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G -%G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4663c215bfdSMatthew Knepley } else { 4673c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 4683c215bfdSMatthew Knepley } 4693c215bfdSMatthew Knepley #else 4703c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr); 4713c215bfdSMatthew Knepley #endif 4723c215bfdSMatthew Knepley } 4733c215bfdSMatthew Knepley } 4743c215bfdSMatthew Knepley ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 4753a40ed3dSBarry Smith } else { 476d6e78c31SSatish Balay ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 477d5f3da31SBarry Smith if (A->factortype){ 47816cd7e1dSShri Abhyankar for (i=0; i<m; i++) { 47916cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 48016cd7e1dSShri Abhyankar /* L part */ 48116cd7e1dSShri Abhyankar for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 48216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 48316cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 48416cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 48516cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 48616cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 48716cd7e1dSShri Abhyankar } else { 48816cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 48916cd7e1dSShri Abhyankar } 49016cd7e1dSShri Abhyankar #else 49116cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 49216cd7e1dSShri Abhyankar #endif 49316cd7e1dSShri Abhyankar } 49416cd7e1dSShri Abhyankar /* diagonal */ 49516cd7e1dSShri Abhyankar j = a->diag[i]; 49616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 49716cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 49816cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 49916cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 50016cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 50116cd7e1dSShri Abhyankar } else { 50216cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 50316cd7e1dSShri Abhyankar } 50416cd7e1dSShri Abhyankar #else 50516cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 50616cd7e1dSShri Abhyankar #endif 50716cd7e1dSShri Abhyankar 50816cd7e1dSShri Abhyankar /* U part */ 50916cd7e1dSShri Abhyankar for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) { 51016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 51116cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 51216cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 51316cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 51416cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 51516cd7e1dSShri Abhyankar } else { 51616cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 51716cd7e1dSShri Abhyankar } 51816cd7e1dSShri Abhyankar #else 51916cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 52016cd7e1dSShri Abhyankar #endif 52116cd7e1dSShri Abhyankar } 52216cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 52316cd7e1dSShri Abhyankar } 52416cd7e1dSShri Abhyankar } else { 52517ab2063SBarry Smith for (i=0; i<m; i++) { 52677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 527416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 528aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 52936db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 530a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 53136db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 532a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 5333a40ed3dSBarry Smith } else { 534a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 53517ab2063SBarry Smith } 53617ab2063SBarry Smith #else 537a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 53817ab2063SBarry Smith #endif 53917ab2063SBarry Smith } 540b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 54117ab2063SBarry Smith } 54216cd7e1dSShri Abhyankar } 543b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 54417ab2063SBarry Smith } 545b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 5463a40ed3dSBarry Smith PetscFunctionReturn(0); 547416022c9SBarry Smith } 548416022c9SBarry Smith 5494a2ae208SSatish Balay #undef __FUNCT__ 5504a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom" 551dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 552416022c9SBarry Smith { 553480ef9eaSBarry Smith Mat A = (Mat) Aa; 554416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 555dfbe8321SBarry Smith PetscErrorCode ierr; 556d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,color; 55736db0b34SBarry Smith PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0; 558b0a32e0cSBarry Smith PetscViewer viewer; 559f3ef73ceSBarry Smith PetscViewerFormat format; 560cddf8d76SBarry Smith 5613a40ed3dSBarry Smith PetscFunctionBegin; 562480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 563b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 56419bcc07fSBarry Smith 565b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 566416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 5670513a670SBarry Smith 568fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 5690513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 570b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 571416022c9SBarry Smith for (i=0; i<m; i++) { 572cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 573bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 574bfeeae90SHong Zhang x_l = a->j[j] ; x_r = x_l + 1.0; 575aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 57636db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 577cddf8d76SBarry Smith #else 578cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 579cddf8d76SBarry Smith #endif 580b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 581cddf8d76SBarry Smith } 582cddf8d76SBarry Smith } 583b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 584cddf8d76SBarry Smith for (i=0; i<m; i++) { 585cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 586bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 587bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 588cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 589b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 590cddf8d76SBarry Smith } 591cddf8d76SBarry Smith } 592b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 593cddf8d76SBarry Smith for (i=0; i<m; i++) { 594cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 595bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 596bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 597aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 59836db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 599cddf8d76SBarry Smith #else 600cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 601cddf8d76SBarry Smith #endif 602b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 603416022c9SBarry Smith } 604416022c9SBarry Smith } 6050513a670SBarry Smith } else { 6060513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 6070513a670SBarry Smith /* first determine max of all nonzero values */ 60897f1f81fSBarry Smith PetscInt nz = a->nz,count; 609b0a32e0cSBarry Smith PetscDraw popup; 61036db0b34SBarry Smith PetscReal scale; 6110513a670SBarry Smith 6120513a670SBarry Smith for (i=0; i<nz; i++) { 6130513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 6140513a670SBarry Smith } 615b0a32e0cSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv; 616b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 617b0a32e0cSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);} 6180513a670SBarry Smith count = 0; 6190513a670SBarry Smith for (i=0; i<m; i++) { 6200513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 621bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 622bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 62397f1f81fSBarry Smith color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count])); 624b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 6250513a670SBarry Smith count++; 6260513a670SBarry Smith } 6270513a670SBarry Smith } 6280513a670SBarry Smith } 629480ef9eaSBarry Smith PetscFunctionReturn(0); 630480ef9eaSBarry Smith } 631cddf8d76SBarry Smith 6324a2ae208SSatish Balay #undef __FUNCT__ 6334a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw" 634dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 635480ef9eaSBarry Smith { 636dfbe8321SBarry Smith PetscErrorCode ierr; 637b0a32e0cSBarry Smith PetscDraw draw; 63836db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 639480ef9eaSBarry Smith PetscTruth isnull; 640480ef9eaSBarry Smith 641480ef9eaSBarry Smith PetscFunctionBegin; 642b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 643b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 644480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 645480ef9eaSBarry Smith 646480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 647d0f46423SBarry Smith xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0; 648480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 649b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 650b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 651480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr); 6523a40ed3dSBarry Smith PetscFunctionReturn(0); 653416022c9SBarry Smith } 654416022c9SBarry Smith 6554a2ae208SSatish Balay #undef __FUNCT__ 6564a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ" 657dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 658416022c9SBarry Smith { 659dfbe8321SBarry Smith PetscErrorCode ierr; 6606805f65bSBarry Smith PetscTruth iascii,isbinary,isdraw; 661416022c9SBarry Smith 6623a40ed3dSBarry Smith PetscFunctionBegin; 6632692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 6642692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 6652692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 666c45a1595SBarry Smith if (iascii) { 6673a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 6680f5bd95cSBarry Smith } else if (isbinary) { 6693a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 6700f5bd95cSBarry Smith } else if (isdraw) { 6713a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 6725cd90555SBarry Smith } else { 673e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name); 67417ab2063SBarry Smith } 6754108e4d5SBarry Smith ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr); 6763a40ed3dSBarry Smith PetscFunctionReturn(0); 67717ab2063SBarry Smith } 67819bcc07fSBarry Smith 6794a2ae208SSatish Balay #undef __FUNCT__ 6804a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ" 681dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 68217ab2063SBarry Smith { 683416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 6846849ba73SBarry Smith PetscErrorCode ierr; 68597f1f81fSBarry Smith PetscInt fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax; 686d0f46423SBarry Smith PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0; 68754f21887SBarry Smith MatScalar *aa = a->a,*ap; 6883447b6efSHong Zhang PetscReal ratio=0.6; 68917ab2063SBarry Smith 6903a40ed3dSBarry Smith PetscFunctionBegin; 6913a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 69217ab2063SBarry Smith 69343ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 69417ab2063SBarry Smith for (i=1; i<m; i++) { 695416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 69617ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 69794a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 69817ab2063SBarry Smith if (fshift) { 699bfeeae90SHong Zhang ip = aj + ai[i] ; 700bfeeae90SHong Zhang ap = aa + ai[i] ; 70117ab2063SBarry Smith N = ailen[i]; 70217ab2063SBarry Smith for (j=0; j<N; j++) { 70317ab2063SBarry Smith ip[j-fshift] = ip[j]; 70417ab2063SBarry Smith ap[j-fshift] = ap[j]; 70517ab2063SBarry Smith } 70617ab2063SBarry Smith } 70717ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 70817ab2063SBarry Smith } 70917ab2063SBarry Smith if (m) { 71017ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 71117ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 71217ab2063SBarry Smith } 71317ab2063SBarry Smith /* reset ilen and imax for each row */ 71417ab2063SBarry Smith for (i=0; i<m; i++) { 71517ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 71617ab2063SBarry Smith } 717bfeeae90SHong Zhang a->nz = ai[m]; 71865e19b50SBarry Smith if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift); 71917ab2063SBarry Smith 72009f38230SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 721d0f46423SBarry Smith ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);CHKERRQ(ierr); 722ae15b995SBarry Smith ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr); 723ae15b995SBarry Smith ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr); 7248e58a170SBarry Smith A->info.mallocs += a->reallocs; 725dd5f02e7SSatish Balay a->reallocs = 0; 7264e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 72736db0b34SBarry Smith a->rmax = rmax; 7284e220ebcSLois Curfman McInnes 729cb5d8e9eSHong Zhang /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */ 730317fbc4cSHong Zhang ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 73188e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 73271c2f376SKris Buschelman 7334108e4d5SBarry Smith ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr); 73471f1c65dSBarry Smith 73571f1c65dSBarry Smith a->idiagvalid = PETSC_FALSE; 73616545cd9SVictor Minden 73716545cd9SVictor Minden #if defined(PETSC_HAVE_CUDA) 738*8f9db167SVictor Minden a->GPUmatrix.resize(m+1,A->cmap->n,a->nz); 739*8f9db167SVictor Minden a->GPUmatrix.row_offsets.assign(a->i,a->i+m+1); 74016545cd9SVictor Minden a->GPUmatrix.column_indices.assign(a->j,a->j+A->cmap->n); 74116545cd9SVictor Minden a->GPUmatrix.values.assign(a->a,a->a+a->nz); 74216545cd9SVictor Minden #endif 7433a40ed3dSBarry Smith PetscFunctionReturn(0); 74417ab2063SBarry Smith } 74517ab2063SBarry Smith 7464a2ae208SSatish Balay #undef __FUNCT__ 74799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ" 74899cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 74999cafbc1SBarry Smith { 75099cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 75199cafbc1SBarry Smith PetscInt i,nz = a->nz; 75254f21887SBarry Smith MatScalar *aa = a->a; 75399cafbc1SBarry Smith 75499cafbc1SBarry Smith PetscFunctionBegin; 75599cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 75699cafbc1SBarry Smith PetscFunctionReturn(0); 75799cafbc1SBarry Smith } 75899cafbc1SBarry Smith 75999cafbc1SBarry Smith #undef __FUNCT__ 76099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ" 76199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 76299cafbc1SBarry Smith { 76399cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 76499cafbc1SBarry Smith PetscInt i,nz = a->nz; 76554f21887SBarry Smith MatScalar *aa = a->a; 76699cafbc1SBarry Smith 76799cafbc1SBarry Smith PetscFunctionBegin; 76899cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 76999cafbc1SBarry Smith PetscFunctionReturn(0); 77099cafbc1SBarry Smith } 77199cafbc1SBarry Smith 77299cafbc1SBarry Smith #undef __FUNCT__ 7734a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ" 774dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 77517ab2063SBarry Smith { 776416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 777dfbe8321SBarry Smith PetscErrorCode ierr; 7783a40ed3dSBarry Smith 7793a40ed3dSBarry Smith PetscFunctionBegin; 780d0f46423SBarry Smith ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 7813a40ed3dSBarry Smith PetscFunctionReturn(0); 78217ab2063SBarry Smith } 783416022c9SBarry Smith 7844a2ae208SSatish Balay #undef __FUNCT__ 7854a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ" 786dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 78717ab2063SBarry Smith { 788416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 789dfbe8321SBarry Smith PetscErrorCode ierr; 790d5d45c9bSBarry Smith 7913a40ed3dSBarry Smith PetscFunctionBegin; 792aa482453SBarry Smith #if defined(PETSC_USE_LOG) 793d0f46423SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz); 79417ab2063SBarry Smith #endif 795e6b907acSBarry Smith ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr); 796c38d4ed2SBarry Smith if (a->row) { 797c38d4ed2SBarry Smith ierr = ISDestroy(a->row);CHKERRQ(ierr); 798c38d4ed2SBarry Smith } 799c38d4ed2SBarry Smith if (a->col) { 800c38d4ed2SBarry Smith ierr = ISDestroy(a->col);CHKERRQ(ierr); 801c38d4ed2SBarry Smith } 80205b42c5fSBarry Smith ierr = PetscFree(a->diag);CHKERRQ(ierr); 80305b42c5fSBarry Smith ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr); 80471f1c65dSBarry Smith ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr); 80505b42c5fSBarry Smith ierr = PetscFree(a->solve_work);CHKERRQ(ierr); 80682bf6240SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} 80705b42c5fSBarry Smith ierr = PetscFree(a->saved_values);CHKERRQ(ierr); 808cc8ba8e1SBarry Smith if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);} 80905b42c5fSBarry Smith ierr = PetscFree(a->xtoy);CHKERRQ(ierr); 810407f6b05SHong Zhang if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);} 8110e83c824SBarry Smith if (a->compressedrow.checked && a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);} 812a30b2313SHong Zhang 8134108e4d5SBarry Smith ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr); 8144846f1f5SKris Buschelman 815606d414cSSatish Balay ierr = PetscFree(a);CHKERRQ(ierr); 816901853e0SKris Buschelman 817dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr); 818901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr); 819901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 820901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 821901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr); 822901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr); 823ba03775dSHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr); 824901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 825901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 826a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 827901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr); 8283a40ed3dSBarry Smith PetscFunctionReturn(0); 82917ab2063SBarry Smith } 83017ab2063SBarry Smith 8314a2ae208SSatish Balay #undef __FUNCT__ 8324a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ" 8334e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg) 83417ab2063SBarry Smith { 835416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8364846f1f5SKris Buschelman PetscErrorCode ierr; 8373a40ed3dSBarry Smith 8383a40ed3dSBarry Smith PetscFunctionBegin; 839a65d3064SKris Buschelman switch (op) { 840a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 8414e0d8c25SBarry Smith a->roworiented = flg; 842a65d3064SKris Buschelman break; 843a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 844a9817697SBarry Smith a->keepnonzeropattern = flg; 845a65d3064SKris Buschelman break; 846512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 847512a5fc5SBarry Smith a->nonew = (flg ? 0 : 1); 848a65d3064SKris Buschelman break; 849a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 8504e0d8c25SBarry Smith a->nonew = (flg ? -1 : 0); 851a65d3064SKris Buschelman break; 852a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 8534e0d8c25SBarry Smith a->nonew = (flg ? -2 : 0); 854a65d3064SKris Buschelman break; 85528b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 85628b2fa4aSMatthew Knepley a->nounused = (flg ? -1 : 0); 85728b2fa4aSMatthew Knepley break; 858a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 8594e0d8c25SBarry Smith a->ignorezeroentries = flg; 8600df259c2SBarry Smith break; 861d487561eSHong Zhang case MAT_USE_COMPRESSEDROW: 8624e0d8c25SBarry Smith a->compressedrow.use = flg; 863d487561eSHong Zhang break; 864b1646e73SJed Brown case MAT_SYMMETRIC: 865b1646e73SJed Brown case MAT_STRUCTURALLY_SYMMETRIC: 866b1646e73SJed Brown case MAT_HERMITIAN: 867b1646e73SJed Brown case MAT_SYMMETRY_ETERNAL: 8684e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 869a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 870a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 871290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 872a65d3064SKris Buschelman break; 873b87ac2d8SJed Brown case MAT_USE_INODES: 874b87ac2d8SJed Brown /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */ 875b87ac2d8SJed Brown break; 876a65d3064SKris Buschelman default: 877e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 878a65d3064SKris Buschelman } 8794108e4d5SBarry Smith ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr); 8803a40ed3dSBarry Smith PetscFunctionReturn(0); 88117ab2063SBarry Smith } 88217ab2063SBarry Smith 8834a2ae208SSatish Balay #undef __FUNCT__ 8844a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ" 885dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 88617ab2063SBarry Smith { 887416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8886849ba73SBarry Smith PetscErrorCode ierr; 889d3e70bfaSHong Zhang PetscInt i,j,n,*ai=a->i,*aj=a->j,nz; 89035e7444dSHong Zhang PetscScalar *aa=a->a,*x,zero=0.0; 89117ab2063SBarry Smith 8923a40ed3dSBarry Smith PetscFunctionBegin; 893d3e70bfaSHong Zhang ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 894e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 89535e7444dSHong Zhang 896d5f3da31SBarry Smith if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){ 897d3e70bfaSHong Zhang PetscInt *diag=a->diag; 89835e7444dSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 89935e7444dSHong Zhang for (i=0; i<n; i++) x[i] = aa[diag[i]]; 90035e7444dSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 90135e7444dSHong Zhang PetscFunctionReturn(0); 90235e7444dSHong Zhang } 90335e7444dSHong Zhang 9042dcb1b2aSMatthew Knepley ierr = VecSet(v,zero);CHKERRQ(ierr); 9051ebc52fbSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 90635e7444dSHong Zhang for (i=0; i<n; i++) { 90735e7444dSHong Zhang nz = ai[i+1] - ai[i]; 9082f5a7c2eSBarry Smith if (!nz) x[i] = 0.0; 90935e7444dSHong Zhang for (j=ai[i]; j<ai[i+1]; j++){ 91035e7444dSHong Zhang if (aj[j] == i) { 91135e7444dSHong Zhang x[i] = aa[j]; 91217ab2063SBarry Smith break; 91317ab2063SBarry Smith } 91417ab2063SBarry Smith } 91517ab2063SBarry Smith } 9161ebc52fbSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 9173a40ed3dSBarry Smith PetscFunctionReturn(0); 91817ab2063SBarry Smith } 91917ab2063SBarry Smith 920b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h" 9214a2ae208SSatish Balay #undef __FUNCT__ 9224a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ" 923dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 92417ab2063SBarry Smith { 925416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 9265c897100SBarry Smith PetscScalar *x,*y; 927dfbe8321SBarry Smith PetscErrorCode ierr; 928d0f46423SBarry Smith PetscInt m = A->rmap->n; 9295c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 930a77337e4SBarry Smith MatScalar *v; 931a77337e4SBarry Smith PetscScalar alpha; 93204fbf559SBarry Smith PetscInt n,i,j,*idx,*ii,*ridx=PETSC_NULL; 9333447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 9344eb6d288SHong Zhang PetscTruth usecprow = cprow.use; 9355c897100SBarry Smith #endif 93617ab2063SBarry Smith 9373a40ed3dSBarry Smith PetscFunctionBegin; 9382e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 9391ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 9401ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 9415c897100SBarry Smith 9425c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 943bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 9445c897100SBarry Smith #else 9453447b6efSHong Zhang if (usecprow){ 9463447b6efSHong Zhang m = cprow.nrows; 9473447b6efSHong Zhang ii = cprow.i; 9487b2bb3b9SHong Zhang ridx = cprow.rindex; 9493447b6efSHong Zhang } else { 9503447b6efSHong Zhang ii = a->i; 9513447b6efSHong Zhang } 95217ab2063SBarry Smith for (i=0; i<m; i++) { 9533447b6efSHong Zhang idx = a->j + ii[i] ; 9543447b6efSHong Zhang v = a->a + ii[i] ; 9553447b6efSHong Zhang n = ii[i+1] - ii[i]; 9563447b6efSHong Zhang if (usecprow){ 9577b2bb3b9SHong Zhang alpha = x[ridx[i]]; 9583447b6efSHong Zhang } else { 95917ab2063SBarry Smith alpha = x[i]; 9603447b6efSHong Zhang } 96104fbf559SBarry Smith for (j=0; j<n; j++) y[idx[j]] += alpha*v[j]; 96217ab2063SBarry Smith } 9635c897100SBarry Smith #endif 964dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 9651ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 9661ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9673a40ed3dSBarry Smith PetscFunctionReturn(0); 96817ab2063SBarry Smith } 96917ab2063SBarry Smith 9704a2ae208SSatish Balay #undef __FUNCT__ 9715c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ" 972dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 9735c897100SBarry Smith { 974dfbe8321SBarry Smith PetscErrorCode ierr; 9755c897100SBarry Smith 9765c897100SBarry Smith PetscFunctionBegin; 977170fe5c8SBarry Smith ierr = VecSet(yy,0.0);CHKERRQ(ierr); 9785c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 9795c897100SBarry Smith PetscFunctionReturn(0); 9805c897100SBarry Smith } 9815c897100SBarry Smith 982a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h" 9835c897100SBarry Smith #undef __FUNCT__ 9844a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ" 985dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 98617ab2063SBarry Smith { 987416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 988d9fead3dSBarry Smith PetscScalar *y; 98954f21887SBarry Smith const PetscScalar *x; 99054f21887SBarry Smith const MatScalar *aa; 991dfbe8321SBarry Smith PetscErrorCode ierr; 992003131ecSBarry Smith PetscInt m=A->rmap->n; 993003131ecSBarry Smith const PetscInt *aj,*ii,*ridx=PETSC_NULL; 9948aee2decSHong Zhang PetscInt n,i,nonzerorow=0; 995362ced78SSatish Balay PetscScalar sum; 99697952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 99717ab2063SBarry Smith 998b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 99997952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 1000fee21e36SBarry Smith #endif 1001fee21e36SBarry Smith 10023a40ed3dSBarry Smith PetscFunctionBegin; 1003*8f9db167SVictor Minden #if !defined(PETSC_HAVE_CUDA) 10043649974fSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 10051ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 1006*8f9db167SVictor Minden #endif 100797952fefSHong Zhang aj = a->j; 100897952fefSHong Zhang aa = a->a; 1009416022c9SBarry Smith ii = a->i; 10104eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 101197952fefSHong Zhang m = a->compressedrow.nrows; 101297952fefSHong Zhang ii = a->compressedrow.i; 101397952fefSHong Zhang ridx = a->compressedrow.rindex; 101497952fefSHong Zhang for (i=0; i<m; i++){ 101597952fefSHong Zhang n = ii[i+1] - ii[i]; 101697952fefSHong Zhang aj = a->j + ii[i]; 101797952fefSHong Zhang aa = a->a + ii[i]; 101897952fefSHong Zhang sum = 0.0; 1019a46b3154SVictor Eijkhout nonzerorow += (n>0); 1020003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 1021003131ecSBarry Smith /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 102297952fefSHong Zhang y[*ridx++] = sum; 102397952fefSHong Zhang } 102497952fefSHong Zhang } else { /* do not use compressed row format */ 1025b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 1026b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 1027b05257ddSBarry Smith #else 10281ae0e694SVictor Minden #if defined(PETSC_HAVE_CUDA) 10291ae0e694SVictor Minden ierr = VecCUDACopyToGPU(xx);CHKERRQ(ierr); 1030*8f9db167SVictor Minden ierr = VecCUDACopyToGPU(yy);CHKERRQ(ierr); 10311ae0e694SVictor Minden cusp::multiply(a->GPUmatrix,xx->GPUarray,yy->GPUarray); 10321ae0e694SVictor Minden yy->valid_GPU_array = PETSC_CUDA_GPU; 1033*8f9db167SVictor Minden /* ierr = VecCUDACopyFromGPU(yy);CHKERRQ(ierr); 1034*8f9db167SVictor Minden ierr = PetscPrintf(PETSC_COMM_WORLD, "b's flag is %i\n\n",yy->valid_GPU_array);CHKERRQ(ierr); */ 10351ae0e694SVictor Minden #else 103617ab2063SBarry Smith for (i=0; i<m; i++) { 1037003131ecSBarry Smith n = ii[i+1] - ii[i]; 1038003131ecSBarry Smith aj = a->j + ii[i]; 1039003131ecSBarry Smith aa = a->a + ii[i]; 104017ab2063SBarry Smith sum = 0.0; 1041a46b3154SVictor Eijkhout nonzerorow += (n>0); 1042003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 104317ab2063SBarry Smith y[i] = sum; 104417ab2063SBarry Smith } 10458d195f9aSBarry Smith #endif 10461ae0e694SVictor Minden #endif 1047b05257ddSBarry Smith } 1048dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr); 1049*8f9db167SVictor Minden #if !defined(PETSC_HAVE_CUDA) 10503649974fSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 10511ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 1052*8f9db167SVictor Minden #endif 10533a40ed3dSBarry Smith PetscFunctionReturn(0); 105417ab2063SBarry Smith } 105517ab2063SBarry Smith 1056a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h" 10574a2ae208SSatish Balay #undef __FUNCT__ 10584a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ" 1059dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 106017ab2063SBarry Smith { 1061416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 106254f21887SBarry Smith PetscScalar *x,*y,*z; 106354f21887SBarry Smith const MatScalar *aa; 1064dfbe8321SBarry Smith PetscErrorCode ierr; 1065d0f46423SBarry Smith PetscInt m = A->rmap->n,*aj,*ii; 1066aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 106797952fefSHong Zhang PetscInt n,i,jrow,j,*ridx=PETSC_NULL; 1068362ced78SSatish Balay PetscScalar sum; 106997952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 1070e36a17ebSSatish Balay #endif 10719ea0dfa2SSatish Balay 10723a40ed3dSBarry Smith PetscFunctionBegin; 10731ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 10741ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 10752e8a6d31SBarry Smith if (zz != yy) { 10761ebc52fbSHong Zhang ierr = VecGetArray(zz,&z);CHKERRQ(ierr); 10772e8a6d31SBarry Smith } else { 10782e8a6d31SBarry Smith z = y; 10792e8a6d31SBarry Smith } 1080bfeeae90SHong Zhang 108197952fefSHong Zhang aj = a->j; 108297952fefSHong Zhang aa = a->a; 1083cddf8d76SBarry Smith ii = a->i; 1084aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 108597952fefSHong Zhang fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 108602ab625aSSatish Balay #else 10874eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 10884eb6d288SHong Zhang if (zz != yy){ 10894eb6d288SHong Zhang ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr); 10904eb6d288SHong Zhang } 109197952fefSHong Zhang m = a->compressedrow.nrows; 109297952fefSHong Zhang ii = a->compressedrow.i; 109397952fefSHong Zhang ridx = a->compressedrow.rindex; 109497952fefSHong Zhang for (i=0; i<m; i++){ 109597952fefSHong Zhang n = ii[i+1] - ii[i]; 109697952fefSHong Zhang aj = a->j + ii[i]; 109797952fefSHong Zhang aa = a->a + ii[i]; 109897952fefSHong Zhang sum = y[*ridx]; 109997952fefSHong Zhang for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; 110097952fefSHong Zhang z[*ridx++] = sum; 110197952fefSHong Zhang } 110297952fefSHong Zhang } else { /* do not use compressed row format */ 110317ab2063SBarry Smith for (i=0; i<m; i++) { 11049ea0dfa2SSatish Balay jrow = ii[i]; 11059ea0dfa2SSatish Balay n = ii[i+1] - jrow; 110617ab2063SBarry Smith sum = y[i]; 11079ea0dfa2SSatish Balay for (j=0; j<n; j++) { 110897952fefSHong Zhang sum += aa[jrow]*x[aj[jrow]]; jrow++; 11099ea0dfa2SSatish Balay } 111017ab2063SBarry Smith z[i] = sum; 111117ab2063SBarry Smith } 111297952fefSHong Zhang } 111302ab625aSSatish Balay #endif 1114dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 11151ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 11161ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 11172e8a6d31SBarry Smith if (zz != yy) { 11181ebc52fbSHong Zhang ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr); 11192e8a6d31SBarry Smith } 11203a40ed3dSBarry Smith PetscFunctionReturn(0); 112117ab2063SBarry Smith } 112217ab2063SBarry Smith 112317ab2063SBarry Smith /* 112417ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 112517ab2063SBarry Smith */ 11264a2ae208SSatish Balay #undef __FUNCT__ 11274a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ" 1128dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 112917ab2063SBarry Smith { 1130416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 11316849ba73SBarry Smith PetscErrorCode ierr; 1132d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n; 113317ab2063SBarry Smith 11343a40ed3dSBarry Smith PetscFunctionBegin; 113509f38230SBarry Smith if (!a->diag) { 113609f38230SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr); 11379518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr); 113809f38230SBarry Smith } 1139d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 114009f38230SBarry Smith a->diag[i] = a->i[i+1]; 1141bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1142bfeeae90SHong Zhang if (a->j[j] == i) { 114309f38230SBarry Smith a->diag[i] = j; 114417ab2063SBarry Smith break; 114517ab2063SBarry Smith } 114617ab2063SBarry Smith } 114717ab2063SBarry Smith } 11483a40ed3dSBarry Smith PetscFunctionReturn(0); 114917ab2063SBarry Smith } 115017ab2063SBarry Smith 1151be5855fcSBarry Smith /* 1152be5855fcSBarry Smith Checks for missing diagonals 1153be5855fcSBarry Smith */ 11544a2ae208SSatish Balay #undef __FUNCT__ 11554a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ" 115609f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d) 1157be5855fcSBarry Smith { 1158be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 115997f1f81fSBarry Smith PetscInt *diag,*jj = a->j,i; 1160be5855fcSBarry Smith 1161be5855fcSBarry Smith PetscFunctionBegin; 116209f38230SBarry Smith *missing = PETSC_FALSE; 1163d0f46423SBarry Smith if (A->rmap->n > 0 && !jj) { 116409f38230SBarry Smith *missing = PETSC_TRUE; 116509f38230SBarry Smith if (d) *d = 0; 116609f38230SBarry Smith PetscInfo(A,"Matrix has no entries therefor is missing diagonal"); 116709f38230SBarry Smith } else { 1168f1e2ffcdSBarry Smith diag = a->diag; 1169d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 1170bfeeae90SHong Zhang if (jj[diag[i]] != i) { 117109f38230SBarry Smith *missing = PETSC_TRUE; 117209f38230SBarry Smith if (d) *d = i; 117309f38230SBarry Smith PetscInfo1(A,"Matrix is missing diagonal number %D",i); 117409f38230SBarry Smith } 1175be5855fcSBarry Smith } 1176be5855fcSBarry Smith } 1177be5855fcSBarry Smith PetscFunctionReturn(0); 1178be5855fcSBarry Smith } 1179be5855fcSBarry Smith 118071f1c65dSBarry Smith EXTERN_C_BEGIN 118171f1c65dSBarry Smith #undef __FUNCT__ 118271f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ" 118371f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift) 118471f1c65dSBarry Smith { 118571f1c65dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 118671f1c65dSBarry Smith PetscErrorCode ierr; 1187d0f46423SBarry Smith PetscInt i,*diag,m = A->rmap->n; 118854f21887SBarry Smith MatScalar *v = a->a; 118954f21887SBarry Smith PetscScalar *idiag,*mdiag; 119071f1c65dSBarry Smith 119171f1c65dSBarry Smith PetscFunctionBegin; 119271f1c65dSBarry Smith if (a->idiagvalid) PetscFunctionReturn(0); 119371f1c65dSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 119471f1c65dSBarry Smith diag = a->diag; 119571f1c65dSBarry Smith if (!a->idiag) { 119671f1c65dSBarry Smith ierr = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr); 119771f1c65dSBarry Smith ierr = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr); 119871f1c65dSBarry Smith v = a->a; 119971f1c65dSBarry Smith } 120071f1c65dSBarry Smith mdiag = a->mdiag; 120171f1c65dSBarry Smith idiag = a->idiag; 120271f1c65dSBarry Smith 1203028cd4eaSSatish Balay if (omega == 1.0 && !PetscAbsScalar(fshift)) { 120471f1c65dSBarry Smith for (i=0; i<m; i++) { 120571f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 1206e32f2f54SBarry Smith if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i); 120771f1c65dSBarry Smith idiag[i] = 1.0/v[diag[i]]; 120871f1c65dSBarry Smith } 120971f1c65dSBarry Smith ierr = PetscLogFlops(m);CHKERRQ(ierr); 121071f1c65dSBarry Smith } else { 121171f1c65dSBarry Smith for (i=0; i<m; i++) { 121271f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 121371f1c65dSBarry Smith idiag[i] = omega/(fshift + v[diag[i]]); 121471f1c65dSBarry Smith } 1215dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr); 121671f1c65dSBarry Smith } 121771f1c65dSBarry Smith a->idiagvalid = PETSC_TRUE; 121871f1c65dSBarry Smith PetscFunctionReturn(0); 121971f1c65dSBarry Smith } 12205a9745a3SMatthew Knepley EXTERN_C_END 122171f1c65dSBarry Smith 1222a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h" 12234a2ae208SSatish Balay #undef __FUNCT__ 122441f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ" 122541f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 122617ab2063SBarry Smith { 1227416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1228e6d1f457SBarry Smith PetscScalar *x,d,sum,*t,scale; 1229e6d1f457SBarry Smith const MatScalar *v = a->a,*idiag=0,*mdiag; 123054f21887SBarry Smith const PetscScalar *b, *bs,*xb, *ts; 1231dfbe8321SBarry Smith PetscErrorCode ierr; 1232d0f46423SBarry Smith PetscInt n = A->cmap->n,m = A->rmap->n,i; 123397f1f81fSBarry Smith const PetscInt *idx,*diag; 123417ab2063SBarry Smith 12353a40ed3dSBarry Smith PetscFunctionBegin; 1236b965ef7fSBarry Smith its = its*lits; 123791723122SBarry Smith 123871f1c65dSBarry Smith if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */ 123971f1c65dSBarry Smith if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);} 124071f1c65dSBarry Smith a->fshift = fshift; 124171f1c65dSBarry Smith a->omega = omega; 1242ed480e8bSBarry Smith 124371f1c65dSBarry Smith diag = a->diag; 124471f1c65dSBarry Smith t = a->ssor_work; 1245ed480e8bSBarry Smith idiag = a->idiag; 124671f1c65dSBarry Smith mdiag = a->mdiag; 1247ed480e8bSBarry Smith 12481ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 12493649974fSBarry Smith ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr); 125071f1c65dSBarry Smith CHKMEMQ; 1251ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 125217ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 125317ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1254ed480e8bSBarry Smith bs = b; 125517ab2063SBarry Smith for (i=0; i<m; i++) { 125671f1c65dSBarry Smith d = fshift + mdiag[i]; 1257416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1258ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1259ed480e8bSBarry Smith v = a->a + diag[i] + 1; 126017ab2063SBarry Smith sum = b[i]*d/omega; 1261003131ecSBarry Smith PetscSparseDensePlusDot(sum,bs,v,idx,n); 126217ab2063SBarry Smith x[i] = sum; 126317ab2063SBarry Smith } 12641ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 12653649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1266efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 12673a40ed3dSBarry Smith PetscFunctionReturn(0); 126817ab2063SBarry Smith } 1269c783ea89SBarry Smith 127048af12d7SBarry Smith if (flag == SOR_APPLY_LOWER) { 1271e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 12723a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 127317ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 1274887ee2caSBarry Smith U is upper triangular, E = D/omega; This routine applies 127517ab2063SBarry Smith 127617ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 127717ab2063SBarry Smith 1278887ee2caSBarry Smith to a vector efficiently using Eisenstat's trick. 127917ab2063SBarry Smith */ 128017ab2063SBarry Smith scale = (2.0/omega) - 1.0; 128117ab2063SBarry Smith 128217ab2063SBarry Smith /* x = (E + U)^{-1} b */ 128317ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1284416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1285ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1286ed480e8bSBarry Smith v = a->a + diag[i] + 1; 128717ab2063SBarry Smith sum = b[i]; 1288e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1289ed480e8bSBarry Smith x[i] = sum*idiag[i]; 129017ab2063SBarry Smith } 129117ab2063SBarry Smith 129217ab2063SBarry Smith /* t = b - (2*E - D)x */ 1293416022c9SBarry Smith v = a->a; 1294ed480e8bSBarry Smith for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; } 129517ab2063SBarry Smith 129617ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1297ed480e8bSBarry Smith ts = t; 1298416022c9SBarry Smith diag = a->diag; 129917ab2063SBarry Smith for (i=0; i<m; i++) { 1300416022c9SBarry Smith n = diag[i] - a->i[i]; 1301ed480e8bSBarry Smith idx = a->j + a->i[i]; 1302ed480e8bSBarry Smith v = a->a + a->i[i]; 130317ab2063SBarry Smith sum = t[i]; 1304003131ecSBarry Smith PetscSparseDenseMinusDot(sum,ts,v,idx,n); 1305ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1306733d66baSBarry Smith /* x = x + t */ 1307733d66baSBarry Smith x[i] += t[i]; 130817ab2063SBarry Smith } 130917ab2063SBarry Smith 1310dc0b31edSSatish Balay ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr); 13111ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 13123649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 13133a40ed3dSBarry Smith PetscFunctionReturn(0); 131417ab2063SBarry Smith } 131517ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 131617ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 131717ab2063SBarry Smith for (i=0; i<m; i++) { 1318416022c9SBarry Smith n = diag[i] - a->i[i]; 1319ed480e8bSBarry Smith idx = a->j + a->i[i]; 1320ed480e8bSBarry Smith v = a->a + a->i[i]; 132117ab2063SBarry Smith sum = b[i]; 1322e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 13235c99c7daSBarry Smith t[i] = sum; 1324ed480e8bSBarry Smith x[i] = sum*idiag[i]; 132517ab2063SBarry Smith } 13265c99c7daSBarry Smith xb = t; 1327efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 13283a40ed3dSBarry Smith } else xb = b; 132917ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 133017ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1331416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1332ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1333ed480e8bSBarry Smith v = a->a + diag[i] + 1; 133417ab2063SBarry Smith sum = xb[i]; 1335e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 13365c99c7daSBarry Smith if (xb == b) { 1337ed480e8bSBarry Smith x[i] = sum*idiag[i]; 13385c99c7daSBarry Smith } else { 13395c99c7daSBarry Smith x[i] = (1-omega)*x[i] + sum*idiag[i]; 134017ab2063SBarry Smith } 13415c99c7daSBarry Smith } 1342efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 134317ab2063SBarry Smith } 134417ab2063SBarry Smith its--; 134517ab2063SBarry Smith } 134617ab2063SBarry Smith while (its--) { 134717ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 134817ab2063SBarry Smith for (i=0; i<m; i++) { 1349416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1350ed480e8bSBarry Smith idx = a->j + a->i[i]; 1351ed480e8bSBarry Smith v = a->a + a->i[i]; 135217ab2063SBarry Smith sum = b[i]; 1353e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1354ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 135517ab2063SBarry Smith } 13569f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 135717ab2063SBarry Smith } 135817ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 135917ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1360416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1361ed480e8bSBarry Smith idx = a->j + a->i[i]; 1362ed480e8bSBarry Smith v = a->a + a->i[i]; 136317ab2063SBarry Smith sum = b[i]; 1364e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1365ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 136617ab2063SBarry Smith } 13679f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 136817ab2063SBarry Smith } 136917ab2063SBarry Smith } 13701ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 13713649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 137271f1c65dSBarry Smith CHKMEMQ; PetscFunctionReturn(0); 137317ab2063SBarry Smith } 137417ab2063SBarry Smith 13752af78befSBarry Smith 13764a2ae208SSatish Balay #undef __FUNCT__ 13774a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ" 1378dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 137917ab2063SBarry Smith { 1380416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 13814e220ebcSLois Curfman McInnes 13823a40ed3dSBarry Smith PetscFunctionBegin; 13834e220ebcSLois Curfman McInnes info->block_size = 1.0; 13844e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 13854e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 13864e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 13874e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 13888e58a170SBarry Smith info->mallocs = (double)A->info.mallocs; 13897adad957SLisandro Dalcin info->memory = ((PetscObject)A)->mem; 1390d5f3da31SBarry Smith if (A->factortype) { 13914e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 13924e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 13934e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 13944e220ebcSLois Curfman McInnes } else { 13954e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 13964e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 13974e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 13984e220ebcSLois Curfman McInnes } 13993a40ed3dSBarry Smith PetscFunctionReturn(0); 140017ab2063SBarry Smith } 140117ab2063SBarry Smith 14024a2ae208SSatish Balay #undef __FUNCT__ 14034a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ" 1404f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag) 140517ab2063SBarry Smith { 1406416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 14073b98c0a2SBarry Smith PetscInt i,m = A->rmap->n - 1,d = 0; 14086849ba73SBarry Smith PetscErrorCode ierr; 140909f38230SBarry Smith PetscTruth missing; 141017ab2063SBarry Smith 14113a40ed3dSBarry Smith PetscFunctionBegin; 1412a9817697SBarry Smith if (a->keepnonzeropattern) { 1413f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1414e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1415bfeeae90SHong Zhang ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1416f1e2ffcdSBarry Smith } 1417f4df32b1SMatthew Knepley if (diag != 0.0) { 141809f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 1419e32f2f54SBarry Smith if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d); 1420f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1421f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 1422f1e2ffcdSBarry Smith } 1423f1e2ffcdSBarry Smith } 142488e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 1425f1e2ffcdSBarry Smith } else { 1426f4df32b1SMatthew Knepley if (diag != 0.0) { 142717ab2063SBarry Smith for (i=0; i<N; i++) { 1428e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 14297ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1430416022c9SBarry Smith a->ilen[rows[i]] = 1; 1431f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 1432bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 14337ae801bdSBarry Smith } else { /* in case row was completely empty */ 1434f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 143517ab2063SBarry Smith } 143617ab2063SBarry Smith } 14373a40ed3dSBarry Smith } else { 143817ab2063SBarry Smith for (i=0; i<N; i++) { 1439e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1440416022c9SBarry Smith a->ilen[rows[i]] = 0; 144117ab2063SBarry Smith } 144217ab2063SBarry Smith } 144388e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 1444f1e2ffcdSBarry Smith } 144543a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14463a40ed3dSBarry Smith PetscFunctionReturn(0); 144717ab2063SBarry Smith } 144817ab2063SBarry Smith 14494a2ae208SSatish Balay #undef __FUNCT__ 14504a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ" 1451a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 145217ab2063SBarry Smith { 1453416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 145497f1f81fSBarry Smith PetscInt *itmp; 145517ab2063SBarry Smith 14563a40ed3dSBarry Smith PetscFunctionBegin; 1457e32f2f54SBarry Smith if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 145817ab2063SBarry Smith 1459416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1460bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 146117ab2063SBarry Smith if (idx) { 1462bfeeae90SHong Zhang itmp = a->j + a->i[row]; 1463bfeeae90SHong Zhang if (*nz) { 14644e093b46SBarry Smith *idx = itmp; 146517ab2063SBarry Smith } 146617ab2063SBarry Smith else *idx = 0; 146717ab2063SBarry Smith } 14683a40ed3dSBarry Smith PetscFunctionReturn(0); 146917ab2063SBarry Smith } 147017ab2063SBarry Smith 1471bfeeae90SHong Zhang /* remove this function? */ 14724a2ae208SSatish Balay #undef __FUNCT__ 14734a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ" 1474a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 147517ab2063SBarry Smith { 14763a40ed3dSBarry Smith PetscFunctionBegin; 14773a40ed3dSBarry Smith PetscFunctionReturn(0); 147817ab2063SBarry Smith } 147917ab2063SBarry Smith 14804a2ae208SSatish Balay #undef __FUNCT__ 14814a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ" 1482dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 148317ab2063SBarry Smith { 1484416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 148554f21887SBarry Smith MatScalar *v = a->a; 148636db0b34SBarry Smith PetscReal sum = 0.0; 14876849ba73SBarry Smith PetscErrorCode ierr; 148897f1f81fSBarry Smith PetscInt i,j; 148917ab2063SBarry Smith 14903a40ed3dSBarry Smith PetscFunctionBegin; 149117ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1492416022c9SBarry Smith for (i=0; i<a->nz; i++) { 1493aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 149436db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 149517ab2063SBarry Smith #else 149617ab2063SBarry Smith sum += (*v)*(*v); v++; 149717ab2063SBarry Smith #endif 149817ab2063SBarry Smith } 1499064f8208SBarry Smith *nrm = sqrt(sum); 15003a40ed3dSBarry Smith } else if (type == NORM_1) { 150136db0b34SBarry Smith PetscReal *tmp; 150297f1f81fSBarry Smith PetscInt *jj = a->j; 1503d0f46423SBarry Smith ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1504d0f46423SBarry Smith ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr); 1505064f8208SBarry Smith *nrm = 0.0; 1506416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1507bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 150817ab2063SBarry Smith } 1509d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 1510064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 151117ab2063SBarry Smith } 1512606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 15133a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1514064f8208SBarry Smith *nrm = 0.0; 1515d0f46423SBarry Smith for (j=0; j<A->rmap->n; j++) { 1516bfeeae90SHong Zhang v = a->a + a->i[j]; 151717ab2063SBarry Smith sum = 0.0; 1518416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1519cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 152017ab2063SBarry Smith } 1521064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 152217ab2063SBarry Smith } 15233a40ed3dSBarry Smith } else { 1524e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm"); 152517ab2063SBarry Smith } 15263a40ed3dSBarry Smith PetscFunctionReturn(0); 152717ab2063SBarry Smith } 152817ab2063SBarry Smith 15294a2ae208SSatish Balay #undef __FUNCT__ 15304a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ" 1531fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B) 153217ab2063SBarry Smith { 1533416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1534416022c9SBarry Smith Mat C; 15356849ba73SBarry Smith PetscErrorCode ierr; 1536d0f46423SBarry Smith PetscInt i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col; 153754f21887SBarry Smith MatScalar *array = a->a; 153817ab2063SBarry Smith 15393a40ed3dSBarry Smith PetscFunctionBegin; 1540e32f2f54SBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1541fc4dec0aSBarry Smith 1542fc4dec0aSBarry Smith if (reuse == MAT_INITIAL_MATRIX || *B == A) { 1543d0f46423SBarry Smith ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr); 1544d0f46423SBarry Smith ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr); 1545bfeeae90SHong Zhang 1546bfeeae90SHong Zhang for (i=0; i<ai[m]; i++) col[aj[i]] += 1; 15477adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1548d0f46423SBarry Smith ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr); 15497adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1550ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr); 1551606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 1552a541d17aSBarry Smith } else { 1553a541d17aSBarry Smith C = *B; 1554a541d17aSBarry Smith } 1555a541d17aSBarry Smith 155617ab2063SBarry Smith for (i=0; i<m; i++) { 155717ab2063SBarry Smith len = ai[i+1]-ai[i]; 155887d4246cSBarry Smith ierr = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 1559b9b97703SBarry Smith array += len; 1560b9b97703SBarry Smith aj += len; 156117ab2063SBarry Smith } 15626d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 15636d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 156417ab2063SBarry Smith 1565815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *B != A) { 1566416022c9SBarry Smith *B = C; 156717ab2063SBarry Smith } else { 1568273d9f13SBarry Smith ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 156917ab2063SBarry Smith } 15703a40ed3dSBarry Smith PetscFunctionReturn(0); 157117ab2063SBarry Smith } 157217ab2063SBarry Smith 1573cd0d46ebSvictorle EXTERN_C_BEGIN 1574cd0d46ebSvictorle #undef __FUNCT__ 15755fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ" 1576be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f) 1577cd0d46ebSvictorle { 1578cd0d46ebSvictorle Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 157954f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 158054f21887SBarry Smith MatScalar *va,*vb; 15816849ba73SBarry Smith PetscErrorCode ierr; 158297f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 1583cd0d46ebSvictorle 1584cd0d46ebSvictorle PetscFunctionBegin; 1585cd0d46ebSvictorle bij = (Mat_SeqAIJ *) B->data; 1586cd0d46ebSvictorle 1587cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 1588cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 15895485867bSBarry Smith if (ma!=nb || na!=mb){ 15905485867bSBarry Smith *f = PETSC_FALSE; 15915485867bSBarry Smith PetscFunctionReturn(0); 15925485867bSBarry Smith } 1593cd0d46ebSvictorle aii = aij->i; bii = bij->i; 1594cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 1595cd0d46ebSvictorle va = aij->a; vb = bij->a; 159697f1f81fSBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 159797f1f81fSBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 1598cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 1599cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 1600cd0d46ebSvictorle 1601cd0d46ebSvictorle *f = PETSC_TRUE; 1602cd0d46ebSvictorle for (i=0; i<ma; i++) { 1603cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 160497f1f81fSBarry Smith PetscInt idc,idr; 16055485867bSBarry Smith PetscScalar vc,vr; 1606cd0d46ebSvictorle /* column/row index/value */ 16075485867bSBarry Smith idc = adx[aptr[i]]; 16085485867bSBarry Smith idr = bdx[bptr[idc]]; 16095485867bSBarry Smith vc = va[aptr[i]]; 16105485867bSBarry Smith vr = vb[bptr[idc]]; 16115485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 16125485867bSBarry Smith *f = PETSC_FALSE; 16135485867bSBarry Smith goto done; 1614cd0d46ebSvictorle } else { 16155485867bSBarry Smith aptr[i]++; 16165485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 1617cd0d46ebSvictorle } 1618cd0d46ebSvictorle } 1619cd0d46ebSvictorle } 1620cd0d46ebSvictorle done: 1621cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 16223aeef889SHong Zhang if (B) { 16233aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 16243aeef889SHong Zhang } 1625cd0d46ebSvictorle PetscFunctionReturn(0); 1626cd0d46ebSvictorle } 1627cd0d46ebSvictorle EXTERN_C_END 1628cd0d46ebSvictorle 16291cbb95d3SBarry Smith EXTERN_C_BEGIN 16301cbb95d3SBarry Smith #undef __FUNCT__ 16311cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ" 16321cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f) 16331cbb95d3SBarry Smith { 16341cbb95d3SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 163554f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 163654f21887SBarry Smith MatScalar *va,*vb; 16371cbb95d3SBarry Smith PetscErrorCode ierr; 16381cbb95d3SBarry Smith PetscInt ma,na,mb,nb, i; 16391cbb95d3SBarry Smith 16401cbb95d3SBarry Smith PetscFunctionBegin; 16411cbb95d3SBarry Smith bij = (Mat_SeqAIJ *) B->data; 16421cbb95d3SBarry Smith 16431cbb95d3SBarry Smith ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 16441cbb95d3SBarry Smith ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 16451cbb95d3SBarry Smith if (ma!=nb || na!=mb){ 16461cbb95d3SBarry Smith *f = PETSC_FALSE; 16471cbb95d3SBarry Smith PetscFunctionReturn(0); 16481cbb95d3SBarry Smith } 16491cbb95d3SBarry Smith aii = aij->i; bii = bij->i; 16501cbb95d3SBarry Smith adx = aij->j; bdx = bij->j; 16511cbb95d3SBarry Smith va = aij->a; vb = bij->a; 16521cbb95d3SBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 16531cbb95d3SBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 16541cbb95d3SBarry Smith for (i=0; i<ma; i++) aptr[i] = aii[i]; 16551cbb95d3SBarry Smith for (i=0; i<mb; i++) bptr[i] = bii[i]; 16561cbb95d3SBarry Smith 16571cbb95d3SBarry Smith *f = PETSC_TRUE; 16581cbb95d3SBarry Smith for (i=0; i<ma; i++) { 16591cbb95d3SBarry Smith while (aptr[i]<aii[i+1]) { 16601cbb95d3SBarry Smith PetscInt idc,idr; 16611cbb95d3SBarry Smith PetscScalar vc,vr; 16621cbb95d3SBarry Smith /* column/row index/value */ 16631cbb95d3SBarry Smith idc = adx[aptr[i]]; 16641cbb95d3SBarry Smith idr = bdx[bptr[idc]]; 16651cbb95d3SBarry Smith vc = va[aptr[i]]; 16661cbb95d3SBarry Smith vr = vb[bptr[idc]]; 16671cbb95d3SBarry Smith if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) { 16681cbb95d3SBarry Smith *f = PETSC_FALSE; 16691cbb95d3SBarry Smith goto done; 16701cbb95d3SBarry Smith } else { 16711cbb95d3SBarry Smith aptr[i]++; 16721cbb95d3SBarry Smith if (B || i!=idc) bptr[idc]++; 16731cbb95d3SBarry Smith } 16741cbb95d3SBarry Smith } 16751cbb95d3SBarry Smith } 16761cbb95d3SBarry Smith done: 16771cbb95d3SBarry Smith ierr = PetscFree(aptr);CHKERRQ(ierr); 16781cbb95d3SBarry Smith if (B) { 16791cbb95d3SBarry Smith ierr = PetscFree(bptr);CHKERRQ(ierr); 16801cbb95d3SBarry Smith } 16811cbb95d3SBarry Smith PetscFunctionReturn(0); 16821cbb95d3SBarry Smith } 16831cbb95d3SBarry Smith EXTERN_C_END 16841cbb95d3SBarry Smith 16859e29f15eSvictorle #undef __FUNCT__ 16869e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ" 1687dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f) 16889e29f15eSvictorle { 1689dfbe8321SBarry Smith PetscErrorCode ierr; 16909e29f15eSvictorle PetscFunctionBegin; 16915485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 16929e29f15eSvictorle PetscFunctionReturn(0); 16939e29f15eSvictorle } 16949e29f15eSvictorle 16954a2ae208SSatish Balay #undef __FUNCT__ 16961cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ" 16971cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f) 16981cbb95d3SBarry Smith { 16991cbb95d3SBarry Smith PetscErrorCode ierr; 17001cbb95d3SBarry Smith PetscFunctionBegin; 17011cbb95d3SBarry Smith ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 17021cbb95d3SBarry Smith PetscFunctionReturn(0); 17031cbb95d3SBarry Smith } 17041cbb95d3SBarry Smith 17051cbb95d3SBarry Smith #undef __FUNCT__ 17064a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ" 1707dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 170817ab2063SBarry Smith { 1709416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 171054f21887SBarry Smith PetscScalar *l,*r,x; 171154f21887SBarry Smith MatScalar *v; 1712dfbe8321SBarry Smith PetscErrorCode ierr; 1713d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj; 171417ab2063SBarry Smith 17153a40ed3dSBarry Smith PetscFunctionBegin; 171617ab2063SBarry Smith if (ll) { 17173ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 17183ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1719e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1720e32f2f54SBarry Smith if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 17211ebc52fbSHong Zhang ierr = VecGetArray(ll,&l);CHKERRQ(ierr); 1722416022c9SBarry Smith v = a->a; 172317ab2063SBarry Smith for (i=0; i<m; i++) { 172417ab2063SBarry Smith x = l[i]; 1725416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 172617ab2063SBarry Smith for (j=0; j<M; j++) { (*v++) *= x;} 172717ab2063SBarry Smith } 17281ebc52fbSHong Zhang ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr); 1729efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 173017ab2063SBarry Smith } 173117ab2063SBarry Smith if (rr) { 1732e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1733e32f2f54SBarry Smith if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 17341ebc52fbSHong Zhang ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1735416022c9SBarry Smith v = a->a; jj = a->j; 173617ab2063SBarry Smith for (i=0; i<nz; i++) { 1737bfeeae90SHong Zhang (*v++) *= r[*jj++]; 173817ab2063SBarry Smith } 17391ebc52fbSHong Zhang ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 1740efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 174117ab2063SBarry Smith } 17423a40ed3dSBarry Smith PetscFunctionReturn(0); 174317ab2063SBarry Smith } 174417ab2063SBarry Smith 17454a2ae208SSatish Balay #undef __FUNCT__ 17464a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ" 174797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 174817ab2063SBarry Smith { 1749db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 17506849ba73SBarry Smith PetscErrorCode ierr; 1751d0f46423SBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens; 175297f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 17535d0c19d7SBarry Smith const PetscInt *irow,*icol; 17545d0c19d7SBarry Smith PetscInt nrows,ncols; 175597f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 175654f21887SBarry Smith MatScalar *a_new,*mat_a; 1757416022c9SBarry Smith Mat C; 175814ca34e6SBarry Smith PetscTruth stride,sorted; 175917ab2063SBarry Smith 17603a40ed3dSBarry Smith PetscFunctionBegin; 176114ca34e6SBarry Smith ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr); 1762e32f2f54SBarry Smith if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 176314ca34e6SBarry Smith ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr); 1764e32f2f54SBarry Smith if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); 176599141d43SSatish Balay 176617ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 1767b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 1768b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 176917ab2063SBarry Smith 1770fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 1771fee21e36SBarry Smith ierr = ISStride(iscol,&stride);CHKERRQ(ierr); 1772fee21e36SBarry Smith if (stride && step == 1) { 177302834360SBarry Smith /* special case of contiguous rows */ 17740e83c824SBarry Smith ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr); 177502834360SBarry Smith /* loop over new rows determining lens and starting points */ 177602834360SBarry Smith for (i=0; i<nrows; i++) { 1777bfeeae90SHong Zhang kstart = ai[irow[i]]; 1778a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 177902834360SBarry Smith for (k=kstart; k<kend; k++) { 1780bfeeae90SHong Zhang if (aj[k] >= first) { 178102834360SBarry Smith starts[i] = k; 178202834360SBarry Smith break; 178302834360SBarry Smith } 178402834360SBarry Smith } 1785a2744918SBarry Smith sum = 0; 178602834360SBarry Smith while (k < kend) { 1787bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 1788a2744918SBarry Smith sum++; 178902834360SBarry Smith } 1790a2744918SBarry Smith lens[i] = sum; 179102834360SBarry Smith } 179202834360SBarry Smith /* create submatrix */ 1793cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 179497f1f81fSBarry Smith PetscInt n_cols,n_rows; 179508480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 1796e32f2f54SBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 1797d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 179808480c60SBarry Smith C = *B; 17993a40ed3dSBarry Smith } else { 18007adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1801f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 18027adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1803ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 180408480c60SBarry Smith } 1805db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 1806db02288aSLois Curfman McInnes 180702834360SBarry Smith /* loop over rows inserting into submatrix */ 1808db02288aSLois Curfman McInnes a_new = c->a; 1809db02288aSLois Curfman McInnes j_new = c->j; 1810db02288aSLois Curfman McInnes i_new = c->i; 1811bfeeae90SHong Zhang 181202834360SBarry Smith for (i=0; i<nrows; i++) { 1813a2744918SBarry Smith ii = starts[i]; 1814a2744918SBarry Smith lensi = lens[i]; 1815a2744918SBarry Smith for (k=0; k<lensi; k++) { 1816a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 181702834360SBarry Smith } 181887828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 1819a2744918SBarry Smith a_new += lensi; 1820a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1821a2744918SBarry Smith c->ilen[i] = lensi; 182202834360SBarry Smith } 18230e83c824SBarry Smith ierr = PetscFree2(lens,starts);CHKERRQ(ierr); 18243a40ed3dSBarry Smith } else { 182502834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 18260e83c824SBarry Smith ierr = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr); 182797f1f81fSBarry Smith ierr = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr); 18280e83c824SBarry Smith ierr = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 182917ab2063SBarry Smith for (i=0; i<ncols; i++) smap[icol[i]] = i+1; 183002834360SBarry Smith /* determine lens of each row */ 183102834360SBarry Smith for (i=0; i<nrows; i++) { 1832bfeeae90SHong Zhang kstart = ai[irow[i]]; 183302834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 183402834360SBarry Smith lens[i] = 0; 183502834360SBarry Smith for (k=kstart; k<kend; k++) { 1836bfeeae90SHong Zhang if (smap[aj[k]]) { 183702834360SBarry Smith lens[i]++; 183802834360SBarry Smith } 183902834360SBarry Smith } 184002834360SBarry Smith } 184117ab2063SBarry Smith /* Create and fill new matrix */ 1842a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 18430f5bd95cSBarry Smith PetscTruth equal; 18440f5bd95cSBarry Smith 184599141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 1846e32f2f54SBarry Smith if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 1847d0f46423SBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr); 18480f5bd95cSBarry Smith if (!equal) { 1849e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 185099141d43SSatish Balay } 1851d0f46423SBarry Smith ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 185208480c60SBarry Smith C = *B; 18533a40ed3dSBarry Smith } else { 18547adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1855f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 18567adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1857ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 185808480c60SBarry Smith } 185999141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 186017ab2063SBarry Smith for (i=0; i<nrows; i++) { 186199141d43SSatish Balay row = irow[i]; 1862bfeeae90SHong Zhang kstart = ai[row]; 186399141d43SSatish Balay kend = kstart + a->ilen[row]; 1864bfeeae90SHong Zhang mat_i = c->i[i]; 186599141d43SSatish Balay mat_j = c->j + mat_i; 186699141d43SSatish Balay mat_a = c->a + mat_i; 186799141d43SSatish Balay mat_ilen = c->ilen + i; 186817ab2063SBarry Smith for (k=kstart; k<kend; k++) { 1869bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 1870ed480e8bSBarry Smith *mat_j++ = tcol - 1; 187199141d43SSatish Balay *mat_a++ = a->a[k]; 187299141d43SSatish Balay (*mat_ilen)++; 187399141d43SSatish Balay 187417ab2063SBarry Smith } 187517ab2063SBarry Smith } 187617ab2063SBarry Smith } 187702834360SBarry Smith /* Free work space */ 187802834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 1879606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 1880606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 188102834360SBarry Smith } 18826d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 18836d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 188417ab2063SBarry Smith 188517ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 1886416022c9SBarry Smith *B = C; 18873a40ed3dSBarry Smith PetscFunctionReturn(0); 188817ab2063SBarry Smith } 188917ab2063SBarry Smith 18901df811f5SHong Zhang #undef __FUNCT__ 18914a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ" 18920481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info) 1893a871dcd8SBarry Smith { 189463b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1895dfbe8321SBarry Smith PetscErrorCode ierr; 189663b91edcSBarry Smith Mat outA; 1897b8a78c4aSBarry Smith PetscTruth row_identity,col_identity; 189863b91edcSBarry Smith 18993a40ed3dSBarry Smith PetscFunctionBegin; 1900e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 19011df811f5SHong Zhang 1902b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 1903b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 1904a871dcd8SBarry Smith 190563b91edcSBarry Smith outA = inA; 1906d5f3da31SBarry Smith outA->factortype = MAT_FACTOR_LU; 1907c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 1908c3122656SLisandro Dalcin if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);} 1909c3122656SLisandro Dalcin a->row = row; 1910c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 1911c3122656SLisandro Dalcin if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);} 1912c3122656SLisandro Dalcin a->col = col; 191363b91edcSBarry Smith 191436db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 1915b9b97703SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */ 19164c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 191752e6d16bSBarry Smith ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr); 1918f0ec6fceSSatish Balay 191994a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 1920d0f46423SBarry Smith ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr); 1921d0f46423SBarry Smith ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 192294a9d846SBarry Smith } 192363b91edcSBarry Smith 1924f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 1925137fb511SHong Zhang if (row_identity && col_identity) { 1926ad04f41aSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr); 1927137fb511SHong Zhang } else { 1928719d5645SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr); 1929137fb511SHong Zhang } 19303a40ed3dSBarry Smith PetscFunctionReturn(0); 1931a871dcd8SBarry Smith } 1932a871dcd8SBarry Smith 19334a2ae208SSatish Balay #undef __FUNCT__ 19344a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ" 1935f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 1936f0b747eeSBarry Smith { 1937f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1938f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 1939efee365bSSatish Balay PetscErrorCode ierr; 19400805154bSBarry Smith PetscBLASInt one = 1,bnz = PetscBLASIntCast(a->nz); 19413a40ed3dSBarry Smith 19423a40ed3dSBarry Smith PetscFunctionBegin; 1943f4df32b1SMatthew Knepley BLASscal_(&bnz,&oalpha,a->a,&one); 1944efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 19453a40ed3dSBarry Smith PetscFunctionReturn(0); 1946f0b747eeSBarry Smith } 1947f0b747eeSBarry Smith 19484a2ae208SSatish Balay #undef __FUNCT__ 19494a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ" 195097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 1951cddf8d76SBarry Smith { 1952dfbe8321SBarry Smith PetscErrorCode ierr; 195397f1f81fSBarry Smith PetscInt i; 1954cddf8d76SBarry Smith 19553a40ed3dSBarry Smith PetscFunctionBegin; 1956cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 1957b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr); 1958cddf8d76SBarry Smith } 1959cddf8d76SBarry Smith 1960cddf8d76SBarry Smith for (i=0; i<n; i++) { 19616a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 1962cddf8d76SBarry Smith } 19633a40ed3dSBarry Smith PetscFunctionReturn(0); 1964cddf8d76SBarry Smith } 1965cddf8d76SBarry Smith 19664a2ae208SSatish Balay #undef __FUNCT__ 19674a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ" 196897f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 19694dcbc457SBarry Smith { 1970e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 19716849ba73SBarry Smith PetscErrorCode ierr; 19725d0c19d7SBarry Smith PetscInt row,i,j,k,l,m,n,*nidx,isz,val; 19735d0c19d7SBarry Smith const PetscInt *idx; 197497f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 1975f1af5d2fSBarry Smith PetscBT table; 1976bbd702dbSSatish Balay 19773a40ed3dSBarry Smith PetscFunctionBegin; 1978d0f46423SBarry Smith m = A->rmap->n; 1979e4d965acSSatish Balay ai = a->i; 1980bfeeae90SHong Zhang aj = a->j; 19818a047759SSatish Balay 1982e32f2f54SBarry Smith if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 198306763907SSatish Balay 198497f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr); 19856831982aSBarry Smith ierr = PetscBTCreate(m,table);CHKERRQ(ierr); 198606763907SSatish Balay 1987e4d965acSSatish Balay for (i=0; i<is_max; i++) { 1988b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1989e4d965acSSatish Balay isz = 0; 19906831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 1991e4d965acSSatish Balay 1992e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 19934dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 1994b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 1995e4d965acSSatish Balay 1996dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1997e4d965acSSatish Balay for (j=0; j<n ; ++j){ 1998f1af5d2fSBarry Smith if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];} 19994dcbc457SBarry Smith } 200006763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 200106763907SSatish Balay ierr = ISDestroy(is[i]);CHKERRQ(ierr); 2002e4d965acSSatish Balay 200304a348a9SBarry Smith k = 0; 200404a348a9SBarry Smith for (j=0; j<ov; j++){ /* for each overlap */ 200504a348a9SBarry Smith n = isz; 200606763907SSatish Balay for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 2007e4d965acSSatish Balay row = nidx[k]; 2008e4d965acSSatish Balay start = ai[row]; 2009e4d965acSSatish Balay end = ai[row+1]; 201004a348a9SBarry Smith for (l = start; l<end ; l++){ 2011efb16452SHong Zhang val = aj[l] ; 2012f1af5d2fSBarry Smith if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;} 2013e4d965acSSatish Balay } 2014e4d965acSSatish Balay } 2015e4d965acSSatish Balay } 2016029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr); 2017e4d965acSSatish Balay } 20186831982aSBarry Smith ierr = PetscBTDestroy(table);CHKERRQ(ierr); 2019606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 20203a40ed3dSBarry Smith PetscFunctionReturn(0); 20214dcbc457SBarry Smith } 202217ab2063SBarry Smith 20230513a670SBarry Smith /* -------------------------------------------------------------- */ 20244a2ae208SSatish Balay #undef __FUNCT__ 20254a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ" 2026dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 20270513a670SBarry Smith { 20280513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 20296849ba73SBarry Smith PetscErrorCode ierr; 20303b98c0a2SBarry Smith PetscInt i,nz = 0,m = A->rmap->n,n = A->cmap->n; 20315d0c19d7SBarry Smith const PetscInt *row,*col; 20325d0c19d7SBarry Smith PetscInt *cnew,j,*lens; 203356cd22aeSBarry Smith IS icolp,irowp; 20343b98c0a2SBarry Smith PetscInt *cwork = PETSC_NULL; 20353b98c0a2SBarry Smith PetscScalar *vwork = PETSC_NULL; 20360513a670SBarry Smith 20373a40ed3dSBarry Smith PetscFunctionBegin; 20384c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 203956cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 20404c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 204156cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 20420513a670SBarry Smith 20430513a670SBarry Smith /* determine lengths of permuted rows */ 204497f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 20450513a670SBarry Smith for (i=0; i<m; i++) { 20460513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 20470513a670SBarry Smith } 20487adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 2049f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 20507adad957SLisandro Dalcin ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2051ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 2052606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 20530513a670SBarry Smith 205497f1f81fSBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr); 20550513a670SBarry Smith for (i=0; i<m; i++) { 205632ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 20570513a670SBarry Smith for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];} 2058cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 205932ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 20600513a670SBarry Smith } 2061606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 20623c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 20630513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20640513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 206556cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 206656cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 206756cd22aeSBarry Smith ierr = ISDestroy(irowp);CHKERRQ(ierr); 206856cd22aeSBarry Smith ierr = ISDestroy(icolp);CHKERRQ(ierr); 20693a40ed3dSBarry Smith PetscFunctionReturn(0); 20700513a670SBarry Smith } 20710513a670SBarry Smith 20724a2ae208SSatish Balay #undef __FUNCT__ 20734a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ" 2074dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 2075cb5b572fSBarry Smith { 2076dfbe8321SBarry Smith PetscErrorCode ierr; 2077cb5b572fSBarry Smith 2078cb5b572fSBarry Smith PetscFunctionBegin; 207933f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 208033f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 2081be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2082be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 2083be6bf707SBarry Smith 2084d0f46423SBarry Smith if (a->i[A->rmap->n] != b->i[B->rmap->n]) { 2085e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different"); 2086cb5b572fSBarry Smith } 2087d0f46423SBarry Smith ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 2088cb5b572fSBarry Smith } else { 2089cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2090cb5b572fSBarry Smith } 2091cb5b572fSBarry Smith PetscFunctionReturn(0); 2092cb5b572fSBarry Smith } 2093cb5b572fSBarry Smith 20944a2ae208SSatish Balay #undef __FUNCT__ 20954a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ" 2096dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A) 2097273d9f13SBarry Smith { 2098dfbe8321SBarry Smith PetscErrorCode ierr; 2099273d9f13SBarry Smith 2100273d9f13SBarry Smith PetscFunctionBegin; 2101ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 2102273d9f13SBarry Smith PetscFunctionReturn(0); 2103273d9f13SBarry Smith } 2104273d9f13SBarry Smith 21054a2ae208SSatish Balay #undef __FUNCT__ 21064a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ" 2107a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 21086c0721eeSBarry Smith { 21096c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 21106c0721eeSBarry Smith PetscFunctionBegin; 21116c0721eeSBarry Smith *array = a->a; 21126c0721eeSBarry Smith PetscFunctionReturn(0); 21136c0721eeSBarry Smith } 21146c0721eeSBarry Smith 21154a2ae208SSatish Balay #undef __FUNCT__ 21164a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ" 2117dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 21186c0721eeSBarry Smith { 21196c0721eeSBarry Smith PetscFunctionBegin; 21206c0721eeSBarry Smith PetscFunctionReturn(0); 21216c0721eeSBarry Smith } 2122273d9f13SBarry Smith 2123ee4f033dSBarry Smith #undef __FUNCT__ 2124ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ" 2125dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 2126ee4f033dSBarry Smith { 21276849ba73SBarry Smith PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f; 21286849ba73SBarry Smith PetscErrorCode ierr; 212997f1f81fSBarry Smith PetscInt k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2; 2130efb30889SBarry Smith PetscScalar dx,*y,*xx,*w3_array; 213187828ca2SBarry Smith PetscScalar *vscale_array; 2132ee4f033dSBarry Smith PetscReal epsilon = coloring->error_rel,umin = coloring->umin; 2133ee4f033dSBarry Smith Vec w1,w2,w3; 2134ee4f033dSBarry Smith void *fctx = coloring->fctx; 213590d69ab7SBarry Smith PetscTruth flg = PETSC_FALSE; 2136ee4f033dSBarry Smith 2137ee4f033dSBarry Smith PetscFunctionBegin; 2138ee4f033dSBarry Smith if (!coloring->w1) { 2139ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr); 214052e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr); 2141ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr); 214252e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr); 2143ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 214452e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr); 2145ee4f033dSBarry Smith } 2146ee4f033dSBarry Smith w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 2147ee4f033dSBarry Smith 2148ee4f033dSBarry Smith ierr = MatSetUnfactored(J);CHKERRQ(ierr); 214990d69ab7SBarry Smith ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr); 2150ee4f033dSBarry Smith if (flg) { 2151ae15b995SBarry Smith ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr); 2152ee4f033dSBarry Smith } else { 21530b9b6f31SBarry Smith PetscTruth assembled; 21540b9b6f31SBarry Smith ierr = MatAssembled(J,&assembled);CHKERRQ(ierr); 21550b9b6f31SBarry Smith if (assembled) { 2156ee4f033dSBarry Smith ierr = MatZeroEntries(J);CHKERRQ(ierr); 2157ee4f033dSBarry Smith } 21580b9b6f31SBarry Smith } 2159ee4f033dSBarry Smith 2160ee4f033dSBarry Smith ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr); 2161ee4f033dSBarry Smith ierr = VecGetSize(x1,&N);CHKERRQ(ierr); 2162ee4f033dSBarry Smith 2163ee4f033dSBarry Smith /* 2164ee4f033dSBarry Smith This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets 2165ee4f033dSBarry Smith coloring->F for the coarser grids from the finest 2166ee4f033dSBarry Smith */ 2167ee4f033dSBarry Smith if (coloring->F) { 2168ee4f033dSBarry Smith ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr); 2169ee4f033dSBarry Smith ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr); 2170ee4f033dSBarry Smith if (m1 != m2) { 2171ee4f033dSBarry Smith coloring->F = 0; 2172ee4f033dSBarry Smith } 2173ee4f033dSBarry Smith } 2174ee4f033dSBarry Smith 2175ee4f033dSBarry Smith if (coloring->F) { 2176ee4f033dSBarry Smith w1 = coloring->F; 2177ee4f033dSBarry Smith coloring->F = 0; 2178ee4f033dSBarry Smith } else { 217966f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2180ee4f033dSBarry Smith ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 218166f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2182ee4f033dSBarry Smith } 2183ee4f033dSBarry Smith 2184ee4f033dSBarry Smith /* 2185ee4f033dSBarry Smith Compute all the scale factors and share with other processors 2186ee4f033dSBarry Smith */ 21871ebc52fbSHong Zhang ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start; 21881ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start; 2189ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 2190ee4f033dSBarry Smith /* 2191ee4f033dSBarry Smith Loop over each column associated with color adding the 2192ee4f033dSBarry Smith perturbation to the vector w3. 2193ee4f033dSBarry Smith */ 2194ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2195ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2196ee4f033dSBarry Smith dx = xx[col]; 2197ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 2198ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2199ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2200ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2201ee4f033dSBarry Smith #else 2202ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2203ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2204ee4f033dSBarry Smith #endif 2205ee4f033dSBarry Smith dx *= epsilon; 2206ee4f033dSBarry Smith vscale_array[col] = 1.0/dx; 2207ee4f033dSBarry Smith } 2208ee4f033dSBarry Smith } 22091ebc52fbSHong Zhang vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2210ee4f033dSBarry Smith ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2211ee4f033dSBarry Smith ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2212ee4f033dSBarry Smith 2213ee4f033dSBarry Smith /* ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD); 2214ee4f033dSBarry Smith ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/ 2215ee4f033dSBarry Smith 2216ee4f033dSBarry Smith if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow; 2217ee4f033dSBarry Smith else vscaleforrow = coloring->columnsforrow; 2218ee4f033dSBarry Smith 22191ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2220ee4f033dSBarry Smith /* 2221ee4f033dSBarry Smith Loop over each color 2222ee4f033dSBarry Smith */ 2223ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 222449b058dcSBarry Smith coloring->currentcolor = k; 2225ee4f033dSBarry Smith ierr = VecCopy(x1,w3);CHKERRQ(ierr); 22261ebc52fbSHong Zhang ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start; 2227ee4f033dSBarry Smith /* 2228ee4f033dSBarry Smith Loop over each column associated with color adding the 2229ee4f033dSBarry Smith perturbation to the vector w3. 2230ee4f033dSBarry Smith */ 2231ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2232ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2233ee4f033dSBarry Smith dx = xx[col]; 22345b8514ebSBarry Smith if (dx == 0.0) dx = 1.0; 2235ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2236ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2237ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2238ee4f033dSBarry Smith #else 2239ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2240ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2241ee4f033dSBarry Smith #endif 2242ee4f033dSBarry Smith dx *= epsilon; 2243e32f2f54SBarry Smith if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter"); 2244ee4f033dSBarry Smith w3_array[col] += dx; 2245ee4f033dSBarry Smith } 22461ebc52fbSHong Zhang w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 2247ee4f033dSBarry Smith 2248ee4f033dSBarry Smith /* 2249ee4f033dSBarry Smith Evaluate function at x1 + dx (here dx is a vector of perturbations) 2250ee4f033dSBarry Smith */ 2251ee4f033dSBarry Smith 225266f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2253ee4f033dSBarry Smith ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 225466f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2255efb30889SBarry Smith ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 2256ee4f033dSBarry Smith 2257ee4f033dSBarry Smith /* 2258ee4f033dSBarry Smith Loop over rows of vector, putting results into Jacobian matrix 2259ee4f033dSBarry Smith */ 22601ebc52fbSHong Zhang ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 2261ee4f033dSBarry Smith for (l=0; l<coloring->nrows[k]; l++) { 2262ee4f033dSBarry Smith row = coloring->rows[k][l]; 2263ee4f033dSBarry Smith col = coloring->columnsforrow[k][l]; 2264ee4f033dSBarry Smith y[row] *= vscale_array[vscaleforrow[k][l]]; 2265ee4f033dSBarry Smith srow = row + start; 2266ee4f033dSBarry Smith ierr = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 2267ee4f033dSBarry Smith } 22681ebc52fbSHong Zhang ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 2269ee4f033dSBarry Smith } 227049b058dcSBarry Smith coloring->currentcolor = k; 22711ebc52fbSHong Zhang ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 22721ebc52fbSHong Zhang xx = xx + start; ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 2273ee4f033dSBarry Smith ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2274ee4f033dSBarry Smith ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2275ee4f033dSBarry Smith PetscFunctionReturn(0); 2276ee4f033dSBarry Smith } 2277ee4f033dSBarry Smith 2278ac90fabeSBarry Smith #undef __FUNCT__ 2279ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ" 2280f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2281ac90fabeSBarry Smith { 2282dfbe8321SBarry Smith PetscErrorCode ierr; 228397f1f81fSBarry Smith PetscInt i; 2284ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data; 22850805154bSBarry Smith PetscBLASInt one=1,bnz = PetscBLASIntCast(x->nz); 2286ac90fabeSBarry Smith 2287ac90fabeSBarry Smith PetscFunctionBegin; 2288ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2289f4df32b1SMatthew Knepley PetscScalar alpha = a; 2290f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2291c537a176SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 2292a30b2313SHong Zhang if (y->xtoy && y->XtoY != X) { 2293a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 2294a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 2295a30b2313SHong Zhang } 2296a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2297d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr); 2298a30b2313SHong Zhang y->XtoY = X; 2299407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr); 2300c537a176SHong Zhang } 2301f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 23021e2582c4SBarry Smith ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);CHKERRQ(ierr); 2303ac90fabeSBarry Smith } else { 2304f4df32b1SMatthew Knepley ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 2305ac90fabeSBarry Smith } 2306ac90fabeSBarry Smith PetscFunctionReturn(0); 2307ac90fabeSBarry Smith } 2308ac90fabeSBarry Smith 2309521d7252SBarry Smith #undef __FUNCT__ 2310521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ" 2311521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs) 2312521d7252SBarry Smith { 231341c166b1SJed Brown PetscErrorCode ierr; 231441c166b1SJed Brown 2315521d7252SBarry Smith PetscFunctionBegin; 231641c166b1SJed Brown ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr); 231741c166b1SJed Brown ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr); 2318521d7252SBarry Smith PetscFunctionReturn(0); 2319521d7252SBarry Smith } 2320521d7252SBarry Smith 2321354c94deSBarry Smith #undef __FUNCT__ 2322354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ" 2323354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat) 2324354c94deSBarry Smith { 2325354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2326354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2327354c94deSBarry Smith PetscInt i,nz; 2328354c94deSBarry Smith PetscScalar *a; 2329354c94deSBarry Smith 2330354c94deSBarry Smith PetscFunctionBegin; 2331354c94deSBarry Smith nz = aij->nz; 2332354c94deSBarry Smith a = aij->a; 2333354c94deSBarry Smith for (i=0; i<nz; i++) { 2334354c94deSBarry Smith a[i] = PetscConj(a[i]); 2335354c94deSBarry Smith } 2336354c94deSBarry Smith #else 2337354c94deSBarry Smith PetscFunctionBegin; 2338354c94deSBarry Smith #endif 2339354c94deSBarry Smith PetscFunctionReturn(0); 2340354c94deSBarry Smith } 2341354c94deSBarry Smith 2342e34fafa9SBarry Smith #undef __FUNCT__ 2343985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ" 2344985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2345e34fafa9SBarry Smith { 2346e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2347e34fafa9SBarry Smith PetscErrorCode ierr; 2348d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2349e34fafa9SBarry Smith PetscReal atmp; 2350985db425SBarry Smith PetscScalar *x; 2351e34fafa9SBarry Smith MatScalar *aa; 2352e34fafa9SBarry Smith 2353e34fafa9SBarry Smith PetscFunctionBegin; 2354e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2355e34fafa9SBarry Smith aa = a->a; 2356e34fafa9SBarry Smith ai = a->i; 2357e34fafa9SBarry Smith aj = a->j; 2358e34fafa9SBarry Smith 2359985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2360e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2361e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2362e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2363e34fafa9SBarry Smith for (i=0; i<m; i++) { 2364e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 23659189402eSHong Zhang x[i] = 0.0; 2366e34fafa9SBarry Smith for (j=0; j<ncols; j++){ 2367985db425SBarry Smith atmp = PetscAbsScalar(*aa); 2368985db425SBarry Smith if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2369985db425SBarry Smith aa++; aj++; 2370985db425SBarry Smith } 2371985db425SBarry Smith } 2372985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2373985db425SBarry Smith PetscFunctionReturn(0); 2374985db425SBarry Smith } 2375985db425SBarry Smith 2376985db425SBarry Smith #undef __FUNCT__ 2377985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ" 2378985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2379985db425SBarry Smith { 2380985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2381985db425SBarry Smith PetscErrorCode ierr; 2382d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2383985db425SBarry Smith PetscScalar *x; 2384985db425SBarry Smith MatScalar *aa; 2385985db425SBarry Smith 2386985db425SBarry Smith PetscFunctionBegin; 2387e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2388985db425SBarry Smith aa = a->a; 2389985db425SBarry Smith ai = a->i; 2390985db425SBarry Smith aj = a->j; 2391985db425SBarry Smith 2392985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2393985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2394985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2395e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2396985db425SBarry Smith for (i=0; i<m; i++) { 2397985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2398d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2399985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2400985db425SBarry Smith } else { /* row is sparse so already KNOW maximum is 0.0 or higher */ 2401985db425SBarry Smith x[i] = 0.0; 2402985db425SBarry Smith if (idx) { 2403985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2404985db425SBarry Smith for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */ 2405985db425SBarry Smith if (aj[j] > j) { 2406985db425SBarry Smith idx[i] = j; 2407985db425SBarry Smith break; 2408985db425SBarry Smith } 2409985db425SBarry Smith } 2410985db425SBarry Smith } 2411985db425SBarry Smith } 2412985db425SBarry Smith for (j=0; j<ncols; j++){ 2413985db425SBarry Smith if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2414985db425SBarry Smith aa++; aj++; 2415985db425SBarry Smith } 2416985db425SBarry Smith } 2417985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2418985db425SBarry Smith PetscFunctionReturn(0); 2419985db425SBarry Smith } 2420985db425SBarry Smith 2421985db425SBarry Smith #undef __FUNCT__ 2422c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ" 2423c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2424c87e5d42SMatthew Knepley { 2425c87e5d42SMatthew Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2426c87e5d42SMatthew Knepley PetscErrorCode ierr; 2427c87e5d42SMatthew Knepley PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2428c87e5d42SMatthew Knepley PetscReal atmp; 2429c87e5d42SMatthew Knepley PetscScalar *x; 2430c87e5d42SMatthew Knepley MatScalar *aa; 2431c87e5d42SMatthew Knepley 2432c87e5d42SMatthew Knepley PetscFunctionBegin; 2433e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2434c87e5d42SMatthew Knepley aa = a->a; 2435c87e5d42SMatthew Knepley ai = a->i; 2436c87e5d42SMatthew Knepley aj = a->j; 2437c87e5d42SMatthew Knepley 2438c87e5d42SMatthew Knepley ierr = VecSet(v,0.0);CHKERRQ(ierr); 2439c87e5d42SMatthew Knepley ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2440c87e5d42SMatthew Knepley ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2441e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2442c87e5d42SMatthew Knepley for (i=0; i<m; i++) { 2443c87e5d42SMatthew Knepley ncols = ai[1] - ai[0]; ai++; 2444289a08f5SMatthew Knepley if (ncols) { 2445289a08f5SMatthew Knepley /* Get first nonzero */ 2446289a08f5SMatthew Knepley for(j = 0; j < ncols; j++) { 2447289a08f5SMatthew Knepley atmp = PetscAbsScalar(aa[j]); 2448289a08f5SMatthew Knepley if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;} 2449289a08f5SMatthew Knepley } 2450289a08f5SMatthew Knepley if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;} 2451289a08f5SMatthew Knepley } else { 2452289a08f5SMatthew Knepley x[i] = 0.0; if (idx) idx[i] = 0; 2453289a08f5SMatthew Knepley } 2454c87e5d42SMatthew Knepley for(j = 0; j < ncols; j++) { 2455c87e5d42SMatthew Knepley atmp = PetscAbsScalar(*aa); 2456289a08f5SMatthew Knepley if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2457c87e5d42SMatthew Knepley aa++; aj++; 2458c87e5d42SMatthew Knepley } 2459c87e5d42SMatthew Knepley } 2460c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2461c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2462c87e5d42SMatthew Knepley } 2463c87e5d42SMatthew Knepley 2464c87e5d42SMatthew Knepley #undef __FUNCT__ 2465985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ" 2466985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2467985db425SBarry Smith { 2468985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2469985db425SBarry Smith PetscErrorCode ierr; 2470d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2471985db425SBarry Smith PetscScalar *x; 2472985db425SBarry Smith MatScalar *aa; 2473985db425SBarry Smith 2474985db425SBarry Smith PetscFunctionBegin; 2475e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2476985db425SBarry Smith aa = a->a; 2477985db425SBarry Smith ai = a->i; 2478985db425SBarry Smith aj = a->j; 2479985db425SBarry Smith 2480985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2481985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2482985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2483e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2484985db425SBarry Smith for (i=0; i<m; i++) { 2485985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2486d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2487985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2488985db425SBarry Smith } else { /* row is sparse so already KNOW minimum is 0.0 or lower */ 2489985db425SBarry Smith x[i] = 0.0; 2490985db425SBarry Smith if (idx) { /* find first implicit 0.0 in the row */ 2491985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2492985db425SBarry Smith for (j=0;j<ncols;j++) { 2493985db425SBarry Smith if (aj[j] > j) { 2494985db425SBarry Smith idx[i] = j; 2495985db425SBarry Smith break; 2496985db425SBarry Smith } 2497985db425SBarry Smith } 2498985db425SBarry Smith } 2499985db425SBarry Smith } 2500985db425SBarry Smith for (j=0; j<ncols; j++){ 2501985db425SBarry Smith if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2502985db425SBarry Smith aa++; aj++; 2503e34fafa9SBarry Smith } 2504e34fafa9SBarry Smith } 2505e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2506e34fafa9SBarry Smith PetscFunctionReturn(0); 2507e34fafa9SBarry Smith } 25083acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 2509682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 25100a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ, 2511cb5b572fSBarry Smith MatGetRow_SeqAIJ, 2512cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 2513cb5b572fSBarry Smith MatMult_SeqAIJ, 251497304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 25157c922b88SBarry Smith MatMultTranspose_SeqAIJ, 25167c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 2517db4efbfdSBarry Smith 0, 2518db4efbfdSBarry Smith 0, 2519db4efbfdSBarry Smith 0, 2520db4efbfdSBarry Smith /*10*/ 0, 2521cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 2522cb5b572fSBarry Smith 0, 252341f059aeSBarry Smith MatSOR_SeqAIJ, 252417ab2063SBarry Smith MatTranspose_SeqAIJ, 252597304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ, 2526cb5b572fSBarry Smith MatEqual_SeqAIJ, 2527cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 2528cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 2529cb5b572fSBarry Smith MatNorm_SeqAIJ, 253097304618SKris Buschelman /*20*/ 0, 2531cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 2532cb5b572fSBarry Smith MatSetOption_SeqAIJ, 2533cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 2534d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ, 2535db4efbfdSBarry Smith 0, 2536db4efbfdSBarry Smith 0, 2537db4efbfdSBarry Smith 0, 2538db4efbfdSBarry Smith 0, 2539d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ, 2540db4efbfdSBarry Smith 0, 2541db4efbfdSBarry Smith 0, 25426c0721eeSBarry Smith MatGetArray_SeqAIJ, 25436c0721eeSBarry Smith MatRestoreArray_SeqAIJ, 2544d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ, 2545cb5b572fSBarry Smith 0, 2546cb5b572fSBarry Smith 0, 2547cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 2548cb5b572fSBarry Smith 0, 2549d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ, 2550cb5b572fSBarry Smith MatGetSubMatrices_SeqAIJ, 2551cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 2552cb5b572fSBarry Smith MatGetValues_SeqAIJ, 2553cb5b572fSBarry Smith MatCopy_SeqAIJ, 2554d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ, 2555cb5b572fSBarry Smith MatScale_SeqAIJ, 2556cb5b572fSBarry Smith 0, 255779299369SBarry Smith MatDiagonalSet_SeqAIJ, 2558fe97e370SBarry Smith 0, 2559d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ, 25603b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 25613b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 25623b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 2563a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 2564d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ, 2565b9617806SBarry Smith 0, 25660513a670SBarry Smith 0, 2567cda55fadSBarry Smith MatPermute_SeqAIJ, 2568cda55fadSBarry Smith 0, 2569d519adbfSMatthew Knepley /*59*/ 0, 2570b9b97703SBarry Smith MatDestroy_SeqAIJ, 2571b9b97703SBarry Smith MatView_SeqAIJ, 2572357abbc8SBarry Smith 0, 2573ee4f033dSBarry Smith 0, 2574d519adbfSMatthew Knepley /*64*/ 0, 2575ee4f033dSBarry Smith 0, 2576ee4f033dSBarry Smith 0, 2577ee4f033dSBarry Smith 0, 2578ee4f033dSBarry Smith 0, 2579d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ, 2580c87e5d42SMatthew Knepley MatGetRowMinAbs_SeqAIJ, 2581ee4f033dSBarry Smith 0, 2582ee4f033dSBarry Smith MatSetColoring_SeqAIJ, 2583dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2584ee4f033dSBarry Smith MatSetValuesAdic_SeqAIJ, 2585dcf5cc72SBarry Smith #else 2586dcf5cc72SBarry Smith 0, 2587dcf5cc72SBarry Smith #endif 2588d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ, 25893acb8795SBarry Smith MatFDColoringApply_AIJ, 259097304618SKris Buschelman 0, 259197304618SKris Buschelman 0, 259297304618SKris Buschelman 0, 2593d519adbfSMatthew Knepley /*79*/ 0, 259497304618SKris Buschelman 0, 259597304618SKris Buschelman 0, 259697304618SKris Buschelman 0, 2597bc011b1eSHong Zhang MatLoad_SeqAIJ, 2598d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ, 25991cbb95d3SBarry Smith MatIsHermitian_SeqAIJ, 26006284ec50SHong Zhang 0, 26016284ec50SHong Zhang 0, 2602bc011b1eSHong Zhang 0, 2603d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ, 260426be0446SHong Zhang MatMatMultSymbolic_SeqAIJ_SeqAIJ, 260526be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 2606d439da42SKris Buschelman MatPtAP_Basic, 26077ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ, 2608d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ, 2609bc011b1eSHong Zhang MatMatMultTranspose_SeqAIJ_SeqAIJ, 2610bc011b1eSHong Zhang MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ, 2611bc011b1eSHong Zhang MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ, 26127ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ_SeqAIJ, 2613d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ, 2614609c6c4dSKris Buschelman 0, 2615609c6c4dSKris Buschelman 0, 261687d4246cSBarry Smith MatConjugate_SeqAIJ, 261787d4246cSBarry Smith 0, 2618d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ, 261999cafbc1SBarry Smith MatRealPart_SeqAIJ, 2620f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 2621f5edf698SHong Zhang 0, 26222bebee5dSHong Zhang 0, 2623d519adbfSMatthew Knepley /*109*/0, 2624985db425SBarry Smith 0, 26252af78befSBarry Smith MatGetRowMin_SeqAIJ, 26262af78befSBarry Smith 0, 2627599ef60dSHong Zhang MatMissingDiagonal_SeqAIJ, 2628d519adbfSMatthew Knepley /*114*/0, 2629599ef60dSHong Zhang 0, 26303c2a7987SHong Zhang 0, 2631fe97e370SBarry Smith 0, 2632fbdbba38SShri Abhyankar 0, 2633fbdbba38SShri Abhyankar /*119*/0, 2634fbdbba38SShri Abhyankar 0, 2635fbdbba38SShri Abhyankar 0, 2636fbdbba38SShri Abhyankar 0, 2637fbdbba38SShri Abhyankar MatLoadnew_SeqAIJ 26389e29f15eSvictorle }; 263917ab2063SBarry Smith 2640fb2e594dSBarry Smith EXTERN_C_BEGIN 26414a2ae208SSatish Balay #undef __FUNCT__ 26424a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ" 2643be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 2644bef8e0ddSBarry Smith { 2645bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 264697f1f81fSBarry Smith PetscInt i,nz,n; 2647bef8e0ddSBarry Smith 2648bef8e0ddSBarry Smith PetscFunctionBegin; 2649bef8e0ddSBarry Smith 2650bef8e0ddSBarry Smith nz = aij->maxnz; 2651d0f46423SBarry Smith n = mat->rmap->n; 2652bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 2653bef8e0ddSBarry Smith aij->j[i] = indices[i]; 2654bef8e0ddSBarry Smith } 2655bef8e0ddSBarry Smith aij->nz = nz; 2656bef8e0ddSBarry Smith for (i=0; i<n; i++) { 2657bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 2658bef8e0ddSBarry Smith } 2659bef8e0ddSBarry Smith 2660bef8e0ddSBarry Smith PetscFunctionReturn(0); 2661bef8e0ddSBarry Smith } 2662fb2e594dSBarry Smith EXTERN_C_END 2663bef8e0ddSBarry Smith 26644a2ae208SSatish Balay #undef __FUNCT__ 26654a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices" 2666bef8e0ddSBarry Smith /*@ 2667bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 2668bef8e0ddSBarry Smith in the matrix. 2669bef8e0ddSBarry Smith 2670bef8e0ddSBarry Smith Input Parameters: 2671bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 2672bef8e0ddSBarry Smith - indices - the column indices 2673bef8e0ddSBarry Smith 267415091d37SBarry Smith Level: advanced 267515091d37SBarry Smith 2676bef8e0ddSBarry Smith Notes: 2677bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 2678bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 2679bef8e0ddSBarry Smith of the MatSetValues() operation. 2680bef8e0ddSBarry Smith 2681bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 2682d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 2683bef8e0ddSBarry Smith 2684bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 2685bef8e0ddSBarry Smith 2686b9617806SBarry Smith The indices should start with zero, not one. 2687b9617806SBarry Smith 2688bef8e0ddSBarry Smith @*/ 2689be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 2690bef8e0ddSBarry Smith { 269197f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt *); 2692bef8e0ddSBarry Smith 2693bef8e0ddSBarry Smith PetscFunctionBegin; 26940700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 26954482741eSBarry Smith PetscValidPointer(indices,2); 2696c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr); 2697bef8e0ddSBarry Smith if (f) { 2698bef8e0ddSBarry Smith ierr = (*f)(mat,indices);CHKERRQ(ierr); 2699bef8e0ddSBarry Smith } else { 2700e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to set column indices"); 2701bef8e0ddSBarry Smith } 2702bef8e0ddSBarry Smith PetscFunctionReturn(0); 2703bef8e0ddSBarry Smith } 2704bef8e0ddSBarry Smith 2705be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 2706be6bf707SBarry Smith 2707fb2e594dSBarry Smith EXTERN_C_BEGIN 27084a2ae208SSatish Balay #undef __FUNCT__ 27094a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ" 2710be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat) 2711be6bf707SBarry Smith { 2712be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 27136849ba73SBarry Smith PetscErrorCode ierr; 2714d0f46423SBarry Smith size_t nz = aij->i[mat->rmap->n]; 2715be6bf707SBarry Smith 2716be6bf707SBarry Smith PetscFunctionBegin; 2717be6bf707SBarry Smith if (aij->nonew != 1) { 2718e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 2719be6bf707SBarry Smith } 2720be6bf707SBarry Smith 2721be6bf707SBarry Smith /* allocate space for values if not already there */ 2722be6bf707SBarry Smith if (!aij->saved_values) { 272387828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr); 27249518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr); 2725be6bf707SBarry Smith } 2726be6bf707SBarry Smith 2727be6bf707SBarry Smith /* copy values over */ 272887828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2729be6bf707SBarry Smith PetscFunctionReturn(0); 2730be6bf707SBarry Smith } 2731fb2e594dSBarry Smith EXTERN_C_END 2732be6bf707SBarry Smith 27334a2ae208SSatish Balay #undef __FUNCT__ 2734b9617806SBarry Smith #define __FUNCT__ "MatStoreValues" 2735be6bf707SBarry Smith /*@ 2736be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 2737be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2738be6bf707SBarry Smith nonlinear portion. 2739be6bf707SBarry Smith 2740be6bf707SBarry Smith Collect on Mat 2741be6bf707SBarry Smith 2742be6bf707SBarry Smith Input Parameters: 27430e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 2744be6bf707SBarry Smith 274515091d37SBarry Smith Level: advanced 274615091d37SBarry Smith 2747be6bf707SBarry Smith Common Usage, with SNESSolve(): 2748be6bf707SBarry Smith $ Create Jacobian matrix 2749be6bf707SBarry Smith $ Set linear terms into matrix 2750be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 2751be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 2752be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 2753512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 2754be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2755be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 2756be6bf707SBarry Smith $ In your Jacobian routine 2757be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2758be6bf707SBarry Smith $ Set nonlinear terms in matrix 2759be6bf707SBarry Smith 2760be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 2761be6bf707SBarry Smith $ // build linear portion of Jacobian 2762512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 2763be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2764be6bf707SBarry Smith $ loop over nonlinear iterations 2765be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2766be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 2767be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 2768be6bf707SBarry Smith $ Solve linear system with Jacobian 2769be6bf707SBarry Smith $ endloop 2770be6bf707SBarry Smith 2771be6bf707SBarry Smith Notes: 2772be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 2773512a5fc5SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before 2774be6bf707SBarry Smith calling this routine. 2775be6bf707SBarry Smith 27760c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 27770c468ba9SBarry Smith and does not allocated additional space. 27780c468ba9SBarry Smith 2779be6bf707SBarry Smith .seealso: MatRetrieveValues() 2780be6bf707SBarry Smith 2781be6bf707SBarry Smith @*/ 2782be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat) 2783be6bf707SBarry Smith { 2784dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2785be6bf707SBarry Smith 2786be6bf707SBarry Smith PetscFunctionBegin; 27870700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2788e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2789e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2790be6bf707SBarry Smith 2791c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2792be6bf707SBarry Smith if (f) { 2793be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2794be6bf707SBarry Smith } else { 2795e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to store values"); 2796be6bf707SBarry Smith } 2797be6bf707SBarry Smith PetscFunctionReturn(0); 2798be6bf707SBarry Smith } 2799be6bf707SBarry Smith 2800fb2e594dSBarry Smith EXTERN_C_BEGIN 28014a2ae208SSatish Balay #undef __FUNCT__ 28024a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ" 2803be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat) 2804be6bf707SBarry Smith { 2805be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 28066849ba73SBarry Smith PetscErrorCode ierr; 2807d0f46423SBarry Smith PetscInt nz = aij->i[mat->rmap->n]; 2808be6bf707SBarry Smith 2809be6bf707SBarry Smith PetscFunctionBegin; 2810be6bf707SBarry Smith if (aij->nonew != 1) { 2811e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 2812be6bf707SBarry Smith } 2813be6bf707SBarry Smith if (!aij->saved_values) { 2814e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 2815be6bf707SBarry Smith } 2816be6bf707SBarry Smith /* copy values over */ 281787828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2818be6bf707SBarry Smith PetscFunctionReturn(0); 2819be6bf707SBarry Smith } 2820fb2e594dSBarry Smith EXTERN_C_END 2821be6bf707SBarry Smith 28224a2ae208SSatish Balay #undef __FUNCT__ 28234a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues" 2824be6bf707SBarry Smith /*@ 2825be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 2826be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2827be6bf707SBarry Smith nonlinear portion. 2828be6bf707SBarry Smith 2829be6bf707SBarry Smith Collect on Mat 2830be6bf707SBarry Smith 2831be6bf707SBarry Smith Input Parameters: 2832be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2833be6bf707SBarry Smith 283415091d37SBarry Smith Level: advanced 283515091d37SBarry Smith 2836be6bf707SBarry Smith .seealso: MatStoreValues() 2837be6bf707SBarry Smith 2838be6bf707SBarry Smith @*/ 2839be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat) 2840be6bf707SBarry Smith { 2841dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2842be6bf707SBarry Smith 2843be6bf707SBarry Smith PetscFunctionBegin; 28440700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 2845e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2846e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2847be6bf707SBarry Smith 2848c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2849be6bf707SBarry Smith if (f) { 2850be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2851be6bf707SBarry Smith } else { 2852e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to retrieve values"); 2853be6bf707SBarry Smith } 2854be6bf707SBarry Smith PetscFunctionReturn(0); 2855be6bf707SBarry Smith } 2856be6bf707SBarry Smith 2857f83d6046SBarry Smith 2858be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 28594a2ae208SSatish Balay #undef __FUNCT__ 28604a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ" 286117ab2063SBarry Smith /*@C 2862682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 28630d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 28646e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 286551c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 28662bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 286717ab2063SBarry Smith 2868db81eaa0SLois Curfman McInnes Collective on MPI_Comm 2869db81eaa0SLois Curfman McInnes 287017ab2063SBarry Smith Input Parameters: 2871db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 287217ab2063SBarry Smith . m - number of rows 287317ab2063SBarry Smith . n - number of columns 287417ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 287551c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 28762bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 287717ab2063SBarry Smith 287817ab2063SBarry Smith Output Parameter: 2879416022c9SBarry Smith . A - the matrix 288017ab2063SBarry Smith 2881175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 2882ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 2883175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 2884175b88e8SBarry Smith 2885b259b22eSLois Curfman McInnes Notes: 288649a6f317SBarry Smith If nnz is given then nz is ignored 288749a6f317SBarry Smith 288817ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 288917ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 28900002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 289144cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 289217ab2063SBarry Smith 289317ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2894a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 28953d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 28966da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 289717ab2063SBarry Smith 2898682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 28994fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 2900682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 29016c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 29026c7ebb05SLois Curfman McInnes 29036c7ebb05SLois Curfman McInnes Options Database Keys: 2904698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2905698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2906db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 2907db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 2908db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 290917ab2063SBarry Smith 2910027ccd11SLois Curfman McInnes Level: intermediate 2911027ccd11SLois Curfman McInnes 291236db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 291336db0b34SBarry Smith 291417ab2063SBarry Smith @*/ 2915be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 291617ab2063SBarry Smith { 2917dfbe8321SBarry Smith PetscErrorCode ierr; 29186945ee14SBarry Smith 29193a40ed3dSBarry Smith PetscFunctionBegin; 2920f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 2921117016b1SBarry Smith ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 2922c4752a88SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 2923ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 2924273d9f13SBarry Smith PetscFunctionReturn(0); 2925273d9f13SBarry Smith } 2926273d9f13SBarry Smith 29274a2ae208SSatish Balay #undef __FUNCT__ 29284a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation" 2929273d9f13SBarry Smith /*@C 2930273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 2931273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 2932273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 2933273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 2934273d9f13SBarry Smith 2935273d9f13SBarry Smith Collective on MPI_Comm 2936273d9f13SBarry Smith 2937273d9f13SBarry Smith Input Parameters: 2938117016b1SBarry Smith + B - The matrix-free 2939273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 2940273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 2941273d9f13SBarry Smith (possibly different for each row) or PETSC_NULL 2942273d9f13SBarry Smith 2943273d9f13SBarry Smith Notes: 294449a6f317SBarry Smith If nnz is given then nz is ignored 294549a6f317SBarry Smith 2946273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2947273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2948273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2949273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 2950273d9f13SBarry Smith 2951273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2952273d9f13SBarry Smith Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 2953273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 2954273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 2955273d9f13SBarry Smith 2956aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 2957aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 2958aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 2959aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 2960aa95bbe8SBarry Smith 2961a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 2962a96a251dSBarry Smith entries or columns indices 2963a96a251dSBarry Smith 2964273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 2965273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 2966273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 2967273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2968273d9f13SBarry Smith 2969273d9f13SBarry Smith Options Database Keys: 2970698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2971698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2972273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2973273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2974273d9f13SBarry Smith the user still MUST index entries starting at 0! 2975273d9f13SBarry Smith 2976273d9f13SBarry Smith Level: intermediate 2977273d9f13SBarry Smith 2978aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo() 2979273d9f13SBarry Smith 2980273d9f13SBarry Smith @*/ 2981be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 2982273d9f13SBarry Smith { 298397f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]); 2984a23d5eceSKris Buschelman 2985a23d5eceSKris Buschelman PetscFunctionBegin; 2986a23d5eceSKris Buschelman ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr); 2987a23d5eceSKris Buschelman if (f) { 2988a23d5eceSKris Buschelman ierr = (*f)(B,nz,nnz);CHKERRQ(ierr); 2989a23d5eceSKris Buschelman } 2990a23d5eceSKris Buschelman PetscFunctionReturn(0); 2991a23d5eceSKris Buschelman } 2992a23d5eceSKris Buschelman 2993a23d5eceSKris Buschelman EXTERN_C_BEGIN 2994a23d5eceSKris Buschelman #undef __FUNCT__ 2995a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ" 2996be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz) 2997a23d5eceSKris Buschelman { 2998273d9f13SBarry Smith Mat_SeqAIJ *b; 2999a43ee2ecSKris Buschelman PetscTruth skipallocation = PETSC_FALSE; 30006849ba73SBarry Smith PetscErrorCode ierr; 300197f1f81fSBarry Smith PetscInt i; 3002273d9f13SBarry Smith 3003273d9f13SBarry Smith PetscFunctionBegin; 3004d5d45c9bSBarry Smith 3005a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 3006c461c341SBarry Smith skipallocation = PETSC_TRUE; 3007c461c341SBarry Smith nz = 0; 3008c461c341SBarry Smith } 3009c461c341SBarry Smith 301026283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 301126283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 301226283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 301326283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3014899cda47SBarry Smith 3015435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 3016e32f2f54SBarry Smith if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz); 3017b73539f3SBarry Smith if (nnz) { 3018d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3019e32f2f54SBarry Smith if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]); 3020e32f2f54SBarry Smith if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n); 3021b73539f3SBarry Smith } 3022b73539f3SBarry Smith } 3023b73539f3SBarry Smith 3024273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 3025273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 3026273d9f13SBarry Smith 3027ab93d7beSBarry Smith if (!skipallocation) { 30282ee49352SLisandro Dalcin if (!b->imax) { 3029d0f46423SBarry Smith ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr); 3030d0f46423SBarry Smith ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 30312ee49352SLisandro Dalcin } 3032273d9f13SBarry Smith if (!nnz) { 3033435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 3034273d9f13SBarry Smith else if (nz <= 0) nz = 1; 3035d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) b->imax[i] = nz; 3036d0f46423SBarry Smith nz = nz*B->rmap->n; 3037273d9f13SBarry Smith } else { 3038273d9f13SBarry Smith nz = 0; 3039d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 3040273d9f13SBarry Smith } 3041ab93d7beSBarry Smith /* b->ilen will count nonzeros in each row so far. */ 3042d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; } 3043ab93d7beSBarry Smith 3044273d9f13SBarry Smith /* allocate the matrix space */ 30452ee49352SLisandro Dalcin ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr); 3046d0f46423SBarry Smith ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr); 3047d0f46423SBarry Smith ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr); 3048bfeeae90SHong Zhang b->i[0] = 0; 3049d0f46423SBarry Smith for (i=1; i<B->rmap->n+1; i++) { 30505da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 30515da197adSKris Buschelman } 3052273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 3053e6b907acSBarry Smith b->free_a = PETSC_TRUE; 3054e6b907acSBarry Smith b->free_ij = PETSC_TRUE; 3055c461c341SBarry Smith } else { 3056e6b907acSBarry Smith b->free_a = PETSC_FALSE; 3057e6b907acSBarry Smith b->free_ij = PETSC_FALSE; 3058c461c341SBarry Smith } 3059273d9f13SBarry Smith 3060273d9f13SBarry Smith b->nz = 0; 3061273d9f13SBarry Smith b->maxnz = nz; 3062273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 3063273d9f13SBarry Smith PetscFunctionReturn(0); 3064273d9f13SBarry Smith } 3065a23d5eceSKris Buschelman EXTERN_C_END 3066273d9f13SBarry Smith 3067a1661176SMatthew Knepley #undef __FUNCT__ 3068a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR" 306958d36128SBarry Smith /*@ 3070a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 3071a1661176SMatthew Knepley 3072a1661176SMatthew Knepley Input Parameters: 3073a1661176SMatthew Knepley + B - the matrix 3074a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 3075a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 3076a1661176SMatthew Knepley - v - optional values in the matrix 3077a1661176SMatthew Knepley 3078a1661176SMatthew Knepley Level: developer 3079a1661176SMatthew Knepley 308058d36128SBarry Smith The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays() 308158d36128SBarry Smith 3082a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential 3083a1661176SMatthew Knepley 3084a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ 3085a1661176SMatthew Knepley @*/ 3086a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 3087a1661176SMatthew Knepley { 3088a1661176SMatthew Knepley PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]); 3089a1661176SMatthew Knepley PetscErrorCode ierr; 3090a1661176SMatthew Knepley 3091a1661176SMatthew Knepley PetscFunctionBegin; 30920700a824SBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,1); 3093a1661176SMatthew Knepley ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr); 3094a1661176SMatthew Knepley if (f) { 3095a1661176SMatthew Knepley ierr = (*f)(B,i,j,v);CHKERRQ(ierr); 3096a1661176SMatthew Knepley } 3097a1661176SMatthew Knepley PetscFunctionReturn(0); 3098a1661176SMatthew Knepley } 3099a1661176SMatthew Knepley 3100a1661176SMatthew Knepley EXTERN_C_BEGIN 3101a1661176SMatthew Knepley #undef __FUNCT__ 3102a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR_SeqAIJ" 3103b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3104a1661176SMatthew Knepley { 3105a1661176SMatthew Knepley PetscInt i; 3106a1661176SMatthew Knepley PetscInt m,n; 3107a1661176SMatthew Knepley PetscInt nz; 3108a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 3109a1661176SMatthew Knepley PetscScalar *values; 3110a1661176SMatthew Knepley PetscErrorCode ierr; 3111a1661176SMatthew Knepley 3112a1661176SMatthew Knepley PetscFunctionBegin; 3113a1661176SMatthew Knepley ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 3114a1661176SMatthew Knepley 311565e19b50SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]); 3116a1661176SMatthew Knepley ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr); 3117a1661176SMatthew Knepley for(i = 0; i < m; i++) { 3118b7940d39SSatish Balay nz = Ii[i+1]- Ii[i]; 3119a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 312065e19b50SBarry Smith if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 3121a1661176SMatthew Knepley nnz[i] = nz; 3122a1661176SMatthew Knepley } 3123a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 3124a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 3125a1661176SMatthew Knepley 3126a1661176SMatthew Knepley if (v) { 3127a1661176SMatthew Knepley values = (PetscScalar*) v; 3128a1661176SMatthew Knepley } else { 31290e83c824SBarry Smith ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr); 3130a1661176SMatthew Knepley ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3131a1661176SMatthew Knepley } 3132a1661176SMatthew Knepley 3133a1661176SMatthew Knepley for(i = 0; i < m; i++) { 3134b7940d39SSatish Balay nz = Ii[i+1] - Ii[i]; 3135b7940d39SSatish Balay ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr); 3136a1661176SMatthew Knepley } 3137a1661176SMatthew Knepley 3138a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3139a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3140a1661176SMatthew Knepley 3141a1661176SMatthew Knepley if (!v) { 3142a1661176SMatthew Knepley ierr = PetscFree(values);CHKERRQ(ierr); 3143a1661176SMatthew Knepley } 3144a1661176SMatthew Knepley PetscFunctionReturn(0); 3145a1661176SMatthew Knepley } 3146a1661176SMatthew Knepley EXTERN_C_END 3147a1661176SMatthew Knepley 31487c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h" 3149be7314b0SBarry Smith #include "private/petscaxpy.h" 3150170fe5c8SBarry Smith 3151170fe5c8SBarry Smith #undef __FUNCT__ 3152170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ" 3153170fe5c8SBarry Smith /* 3154170fe5c8SBarry Smith Computes (B'*A')' since computing B*A directly is untenable 3155170fe5c8SBarry Smith 3156170fe5c8SBarry Smith n p p 3157170fe5c8SBarry Smith ( ) ( ) ( ) 3158170fe5c8SBarry Smith m ( A ) * n ( B ) = m ( C ) 3159170fe5c8SBarry Smith ( ) ( ) ( ) 3160170fe5c8SBarry Smith 3161170fe5c8SBarry Smith */ 3162170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C) 3163170fe5c8SBarry Smith { 3164170fe5c8SBarry Smith PetscErrorCode ierr; 3165170fe5c8SBarry Smith Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data; 3166170fe5c8SBarry Smith Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data; 3167170fe5c8SBarry Smith Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data; 31681de00fd4SBarry Smith PetscInt i,n,m,q,p; 3169170fe5c8SBarry Smith const PetscInt *ii,*idx; 3170170fe5c8SBarry Smith const PetscScalar *b,*a,*a_q; 3171170fe5c8SBarry Smith PetscScalar *c,*c_q; 3172170fe5c8SBarry Smith 3173170fe5c8SBarry Smith PetscFunctionBegin; 3174d0f46423SBarry Smith m = A->rmap->n; 3175d0f46423SBarry Smith n = A->cmap->n; 3176d0f46423SBarry Smith p = B->cmap->n; 3177170fe5c8SBarry Smith a = sub_a->v; 3178170fe5c8SBarry Smith b = sub_b->a; 3179170fe5c8SBarry Smith c = sub_c->v; 3180170fe5c8SBarry Smith ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr); 3181170fe5c8SBarry Smith 3182170fe5c8SBarry Smith ii = sub_b->i; 3183170fe5c8SBarry Smith idx = sub_b->j; 3184170fe5c8SBarry Smith for (i=0; i<n; i++) { 3185170fe5c8SBarry Smith q = ii[i+1] - ii[i]; 3186170fe5c8SBarry Smith while (q-->0) { 3187170fe5c8SBarry Smith c_q = c + m*(*idx); 3188170fe5c8SBarry Smith a_q = a + m*i; 3189be7314b0SBarry Smith PetscAXPY(c_q,*b,a_q,m); 3190170fe5c8SBarry Smith idx++; 3191170fe5c8SBarry Smith b++; 3192170fe5c8SBarry Smith } 3193170fe5c8SBarry Smith } 3194170fe5c8SBarry Smith PetscFunctionReturn(0); 3195170fe5c8SBarry Smith } 3196170fe5c8SBarry Smith 3197170fe5c8SBarry Smith #undef __FUNCT__ 3198170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ" 3199170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 3200170fe5c8SBarry Smith { 3201170fe5c8SBarry Smith PetscErrorCode ierr; 3202d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 3203170fe5c8SBarry Smith Mat Cmat; 3204170fe5c8SBarry Smith 3205170fe5c8SBarry Smith PetscFunctionBegin; 3206e32f2f54SBarry Smith if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n); 320739804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 3208170fe5c8SBarry Smith ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr); 3209170fe5c8SBarry Smith ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr); 3210170fe5c8SBarry Smith ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 3211170fe5c8SBarry Smith Cmat->assembled = PETSC_TRUE; 3212170fe5c8SBarry Smith *C = Cmat; 3213170fe5c8SBarry Smith PetscFunctionReturn(0); 3214170fe5c8SBarry Smith } 3215170fe5c8SBarry Smith 3216170fe5c8SBarry Smith /* ----------------------------------------------------------------*/ 3217170fe5c8SBarry Smith #undef __FUNCT__ 3218170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ" 3219170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 3220170fe5c8SBarry Smith { 3221170fe5c8SBarry Smith PetscErrorCode ierr; 3222170fe5c8SBarry Smith 3223170fe5c8SBarry Smith PetscFunctionBegin; 3224170fe5c8SBarry Smith if (scall == MAT_INITIAL_MATRIX){ 3225170fe5c8SBarry Smith ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr); 3226170fe5c8SBarry Smith } 3227170fe5c8SBarry Smith ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr); 3228170fe5c8SBarry Smith PetscFunctionReturn(0); 3229170fe5c8SBarry Smith } 3230170fe5c8SBarry Smith 3231170fe5c8SBarry Smith 32320bad9183SKris Buschelman /*MC 3233fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 32340bad9183SKris Buschelman based on compressed sparse row format. 32350bad9183SKris Buschelman 32360bad9183SKris Buschelman Options Database Keys: 32370bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 32380bad9183SKris Buschelman 32390bad9183SKris Buschelman Level: beginner 32400bad9183SKris Buschelman 3241f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 32420bad9183SKris Buschelman M*/ 32430bad9183SKris Buschelman 3244a6175056SHong Zhang EXTERN_C_BEGIN 3245b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX) 3246b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*); 3247b5e56a35SBarry Smith #endif 324865460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE) 3249af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *); 3250af1023dbSSatish Balay #endif 325117667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*); 3252b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*); 3253a83b8d76SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*); 3254db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *); 3255611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 3256bccb9932SShri Abhyankar extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 3257611f576cSBarry Smith #endif 3258611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU) 32595c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*); 3260611f576cSBarry Smith #endif 3261f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST) 3262f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*); 3263f3c0ef26SHong Zhang #endif 3264611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 32655c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*); 3266611f576cSBarry Smith #endif 3267eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK) 3268eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*); 3269eb3b5408SSatish Balay #endif 3270586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD) 3271586621ddSJed Brown extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*); 3272586621ddSJed Brown #endif 3273719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL) 3274719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*); 3275719d5645SBarry Smith #endif 3276b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3277b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*); 3278b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEnginePut_SeqAIJ(PetscObject,void*); 3279b3866ffcSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatlabEngineGet_SeqAIJ(PetscObject,void*); 3280b3866ffcSBarry Smith #endif 328117667f90SBarry Smith EXTERN_C_END 328217667f90SBarry Smith 3283b24902e0SBarry Smith 328417667f90SBarry Smith EXTERN_C_BEGIN 32854a2ae208SSatish Balay #undef __FUNCT__ 32864a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ" 3287be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B) 3288273d9f13SBarry Smith { 3289273d9f13SBarry Smith Mat_SeqAIJ *b; 3290dfbe8321SBarry Smith PetscErrorCode ierr; 329138baddfdSBarry Smith PetscMPIInt size; 3292273d9f13SBarry Smith 3293273d9f13SBarry Smith PetscFunctionBegin; 32947adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 3295e32f2f54SBarry Smith if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 3296273d9f13SBarry Smith 329738f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr); 3298b0a32e0cSBarry Smith B->data = (void*)b; 3299549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 330090f02eecSBarry Smith B->mapping = 0; 3301416022c9SBarry Smith b->row = 0; 3302416022c9SBarry Smith b->col = 0; 330382bf6240SBarry Smith b->icol = 0; 3304b810aeb4SBarry Smith b->reallocs = 0; 330536db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 3306f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 3307416022c9SBarry Smith b->nonew = 0; 3308416022c9SBarry Smith b->diag = 0; 3309416022c9SBarry Smith b->solve_work = 0; 33102a1b7f2aSHong Zhang B->spptr = 0; 3311be6bf707SBarry Smith b->saved_values = 0; 3312d7f994e1SBarry Smith b->idiag = 0; 331371f1c65dSBarry Smith b->mdiag = 0; 331471f1c65dSBarry Smith b->ssor_work = 0; 331571f1c65dSBarry Smith b->omega = 1.0; 331671f1c65dSBarry Smith b->fshift = 0.0; 331771f1c65dSBarry Smith b->idiagvalid = PETSC_FALSE; 3318a9817697SBarry Smith b->keepnonzeropattern = PETSC_FALSE; 3319a30b2313SHong Zhang b->xtoy = 0; 3320a30b2313SHong Zhang b->XtoY = 0; 332173e7a558SHong Zhang b->compressedrow.use = PETSC_FALSE; 3322d0f46423SBarry Smith b->compressedrow.nrows = B->rmap->n; 3323d487561eSHong Zhang b->compressedrow.i = PETSC_NULL; 3324d487561eSHong Zhang b->compressedrow.rindex = PETSC_NULL; 3325d487561eSHong Zhang b->compressedrow.checked = PETSC_FALSE; 332688e51ccdSHong Zhang B->same_nonzero = PETSC_FALSE; 332717ab2063SBarry Smith 332835d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 3329b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3330b3866ffcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C", 3331b3866ffcSBarry Smith "MatGetFactor_seqaij_matlab", 3332b3866ffcSBarry Smith MatGetFactor_seqaij_matlab);CHKERRQ(ierr); 3333b3866ffcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr); 3334b3866ffcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr); 3335b3866ffcSBarry Smith #endif 3336b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX) 3337ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 3338b5e56a35SBarry Smith "MatGetFactor_seqaij_pastix", 3339b5e56a35SBarry Smith MatGetFactor_seqaij_pastix);CHKERRQ(ierr); 3340b5e56a35SBarry Smith #endif 334165460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE) 3342ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C", 3343719d5645SBarry Smith "MatGetFactor_seqaij_essl", 3344719d5645SBarry Smith MatGetFactor_seqaij_essl);CHKERRQ(ierr); 3345719d5645SBarry Smith #endif 3346611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU) 3347ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C", 33485c9eb25fSBarry Smith "MatGetFactor_seqaij_superlu", 33495c9eb25fSBarry Smith MatGetFactor_seqaij_superlu);CHKERRQ(ierr); 3350611f576cSBarry Smith #endif 3351f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST) 3352ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 3353f3c0ef26SHong Zhang "MatGetFactor_seqaij_superlu_dist", 3354f3c0ef26SHong Zhang MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr); 3355f3c0ef26SHong Zhang #endif 3356611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 3357ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C", 33585c9eb25fSBarry Smith "MatGetFactor_seqaij_spooles", 33595c9eb25fSBarry Smith MatGetFactor_seqaij_spooles);CHKERRQ(ierr); 3360611f576cSBarry Smith #endif 3361611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 3362ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 3363bccb9932SShri Abhyankar "MatGetFactor_aij_mumps", 3364bccb9932SShri Abhyankar MatGetFactor_aij_mumps);CHKERRQ(ierr); 3365611f576cSBarry Smith #endif 3366eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK) 3367ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C", 3368eb3b5408SSatish Balay "MatGetFactor_seqaij_umfpack", 3369eb3b5408SSatish Balay MatGetFactor_seqaij_umfpack);CHKERRQ(ierr); 3370eb3b5408SSatish Balay #endif 3371586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD) 3372586621ddSJed Brown ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C", 3373586621ddSJed Brown "MatGetFactor_seqaij_cholmod", 3374586621ddSJed Brown MatGetFactor_seqaij_cholmod);CHKERRQ(ierr); 3375586621ddSJed Brown #endif 3376719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL) 3377ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C", 3378719d5645SBarry Smith "MatGetFactor_seqaij_lusol", 3379719d5645SBarry Smith MatGetFactor_seqaij_lusol);CHKERRQ(ierr); 3380719d5645SBarry Smith #endif 3381ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C", 3382b24902e0SBarry Smith "MatGetFactor_seqaij_petsc", 3383b24902e0SBarry Smith MatGetFactor_seqaij_petsc);CHKERRQ(ierr); 3384ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C", 3385db4efbfdSBarry Smith "MatGetFactorAvailable_seqaij_petsc", 3386db4efbfdSBarry Smith MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr); 3387174acd27SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C", 3388a83b8d76SBarry Smith "MatGetFactor_seqaij_bas", 3389174acd27SBarry Smith MatGetFactor_seqaij_bas); 3390f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C", 3391bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 3392bc4b532fSSatish Balay MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 3393f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 3394be6bf707SBarry Smith "MatStoreValues_SeqAIJ", 3395bc4b532fSSatish Balay MatStoreValues_SeqAIJ);CHKERRQ(ierr); 3396f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 3397be6bf707SBarry Smith "MatRetrieveValues_SeqAIJ", 3398bc4b532fSSatish Balay MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 3399a6175056SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C", 3400a6175056SHong Zhang "MatConvert_SeqAIJ_SeqSBAIJ", 3401a6175056SHong Zhang MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 340285fc7724SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C", 340385fc7724SBarry Smith "MatConvert_SeqAIJ_SeqBAIJ", 340485fc7724SBarry Smith MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 3405ba03775dSHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C", 3406ba03775dSHong Zhang "MatConvert_SeqAIJ_SeqCSRPERM", 3407ba03775dSHong Zhang MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr); 340817667f90SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C", 340917667f90SBarry Smith "MatConvert_SeqAIJ_SeqCRL", 341017667f90SBarry Smith MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr); 34115fbd3699SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 34125fbd3699SBarry Smith "MatIsTranspose_SeqAIJ", 34135fbd3699SBarry Smith MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 34141cbb95d3SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C", 34151cbb95d3SBarry Smith "MatIsHermitianTranspose_SeqAIJ", 34161cbb95d3SBarry Smith MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 3417a23d5eceSKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C", 3418a23d5eceSKris Buschelman "MatSeqAIJSetPreallocation_SeqAIJ", 3419a23d5eceSKris Buschelman MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 3420a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C", 3421a1661176SMatthew Knepley "MatSeqAIJSetPreallocationCSR_SeqAIJ", 3422a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 342305b94e36SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C", 342405b94e36SKris Buschelman "MatReorderForNonzeroDiagonal_SeqAIJ", 342505b94e36SKris Buschelman MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 3426170fe5c8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C", 3427170fe5c8SBarry Smith "MatMatMult_SeqDense_SeqAIJ", 3428170fe5c8SBarry Smith MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr); 3429170fe5c8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C", 3430170fe5c8SBarry Smith "MatMatMultSymbolic_SeqDense_SeqAIJ", 3431170fe5c8SBarry Smith MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 3432170fe5c8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C", 3433170fe5c8SBarry Smith "MatMatMultNumeric_SeqDense_SeqAIJ", 3434170fe5c8SBarry Smith MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 34354108e4d5SBarry Smith ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr); 343617667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 34373a40ed3dSBarry Smith PetscFunctionReturn(0); 343817ab2063SBarry Smith } 3439273d9f13SBarry Smith EXTERN_C_END 344017ab2063SBarry Smith 34414a2ae208SSatish Balay #undef __FUNCT__ 3442b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ" 3443b24902e0SBarry Smith /* 3444b24902e0SBarry Smith Given a matrix generated with MatGetFactor() duplicates all the information in A into B 3445b24902e0SBarry Smith */ 3446f77e22a1SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace) 344717ab2063SBarry Smith { 3448416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 34496849ba73SBarry Smith PetscErrorCode ierr; 3450d0f46423SBarry Smith PetscInt i,m = A->rmap->n; 345117ab2063SBarry Smith 34523a40ed3dSBarry Smith PetscFunctionBegin; 3453273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 3454273d9f13SBarry Smith 3455d5f3da31SBarry Smith C->factortype = A->factortype; 3456416022c9SBarry Smith c->row = 0; 3457416022c9SBarry Smith c->col = 0; 345882bf6240SBarry Smith c->icol = 0; 34596ad4291fSHong Zhang c->reallocs = 0; 346017ab2063SBarry Smith 34616ad4291fSHong Zhang C->assembled = PETSC_TRUE; 346217ab2063SBarry Smith 346326283091SBarry Smith ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr); 346426283091SBarry Smith ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr); 346526283091SBarry Smith ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr); 346626283091SBarry Smith ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr); 3467eec197d1SBarry Smith 346833b91e9fSSatish Balay ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr); 34699518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr); 347017ab2063SBarry Smith for (i=0; i<m; i++) { 3471416022c9SBarry Smith c->imax[i] = a->imax[i]; 3472416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 347317ab2063SBarry Smith } 347417ab2063SBarry Smith 347517ab2063SBarry Smith /* allocate the matrix space */ 3476f77e22a1SHong Zhang if (mallocmatspace){ 3477a96a251dSBarry Smith ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr); 34789518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 3479f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 348097f1f81fSBarry Smith ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 348117ab2063SBarry Smith if (m > 0) { 348297f1f81fSBarry Smith ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr); 3483be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 3484bfeeae90SHong Zhang ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 3485be6bf707SBarry Smith } else { 3486bfeeae90SHong Zhang ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 348717ab2063SBarry Smith } 348808480c60SBarry Smith } 3489f77e22a1SHong Zhang } 349017ab2063SBarry Smith 34916ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 3492416022c9SBarry Smith c->roworiented = a->roworiented; 3493416022c9SBarry Smith c->nonew = a->nonew; 3494416022c9SBarry Smith if (a->diag) { 349597f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr); 349652e6d16bSBarry Smith ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 349717ab2063SBarry Smith for (i=0; i<m; i++) { 3498416022c9SBarry Smith c->diag[i] = a->diag[i]; 349917ab2063SBarry Smith } 35003a40ed3dSBarry Smith } else c->diag = 0; 35016ad4291fSHong Zhang c->solve_work = 0; 35026ad4291fSHong Zhang c->saved_values = 0; 35036ad4291fSHong Zhang c->idiag = 0; 350471f1c65dSBarry Smith c->ssor_work = 0; 3505a9817697SBarry Smith c->keepnonzeropattern = a->keepnonzeropattern; 3506e6b907acSBarry Smith c->free_a = PETSC_TRUE; 3507e6b907acSBarry Smith c->free_ij = PETSC_TRUE; 35086ad4291fSHong Zhang c->xtoy = 0; 35096ad4291fSHong Zhang c->XtoY = 0; 35106ad4291fSHong Zhang 3511416022c9SBarry Smith c->nz = a->nz; 35128ed568f8SMatthew G Knepley c->maxnz = a->nz; /* Since we allocate exactly the right amount */ 3513273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 3514754ec7b1SSatish Balay 35156ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 35166ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 35176ad4291fSHong Zhang c->compressedrow.checked = a->compressedrow.checked; 35186ad4291fSHong Zhang if (a->compressedrow.checked && a->compressedrow.use){ 35196ad4291fSHong Zhang i = a->compressedrow.nrows; 35200e83c824SBarry Smith ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr); 35216ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr); 35226ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr); 352327ea64f8SHong Zhang } else { 352427ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 352527ea64f8SHong Zhang c->compressedrow.i = PETSC_NULL; 352627ea64f8SHong Zhang c->compressedrow.rindex = PETSC_NULL; 35276ad4291fSHong Zhang } 352888e51ccdSHong Zhang C->same_nonzero = A->same_nonzero; 35294108e4d5SBarry Smith ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr); 35304846f1f5SKris Buschelman 35317adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr); 35323a40ed3dSBarry Smith PetscFunctionReturn(0); 353317ab2063SBarry Smith } 353417ab2063SBarry Smith 35354a2ae208SSatish Balay #undef __FUNCT__ 3536b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ" 3537b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 3538b24902e0SBarry Smith { 3539b24902e0SBarry Smith PetscErrorCode ierr; 3540b24902e0SBarry Smith 3541b24902e0SBarry Smith PetscFunctionBegin; 3542b24902e0SBarry Smith ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 35434b6263acSBarry Smith ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr); 3544b24902e0SBarry Smith ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr); 3545f77e22a1SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr); 3546b24902e0SBarry Smith PetscFunctionReturn(0); 3547b24902e0SBarry Smith } 3548b24902e0SBarry Smith 3549b24902e0SBarry Smith #undef __FUNCT__ 35504a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ" 3551a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A) 355217ab2063SBarry Smith { 3553416022c9SBarry Smith Mat_SeqAIJ *a; 3554416022c9SBarry Smith Mat B; 35556849ba73SBarry Smith PetscErrorCode ierr; 35563c601197SSatish Balay PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N; 355738baddfdSBarry Smith int fd; 355838baddfdSBarry Smith PetscMPIInt size; 3559bcd2baecSBarry Smith MPI_Comm comm; 356017ab2063SBarry Smith 35613a40ed3dSBarry Smith PetscFunctionBegin; 3562e864ced6SBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 3563d132466eSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3564e32f2f54SBarry Smith if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor"); 3565b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 35660752156aSBarry Smith ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 3567e32f2f54SBarry Smith if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 356817ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 356917ab2063SBarry Smith 3570d64ed03dSBarry Smith if (nz < 0) { 3571e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 3572d64ed03dSBarry Smith } 3573d64ed03dSBarry Smith 357417ab2063SBarry Smith /* read in row lengths */ 357597f1f81fSBarry Smith ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 35760752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 357717ab2063SBarry Smith 35783c601197SSatish Balay /* check if sum of rowlengths is same as nz */ 35793c601197SSatish Balay for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 3580e32f2f54SBarry Smith if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum); 35813c601197SSatish Balay 358217ab2063SBarry Smith /* create our matrix */ 3583f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B);CHKERRQ(ierr); 3584f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 3585b3a1e11cSKris Buschelman ierr = MatSetType(B,type);CHKERRQ(ierr); 3586ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr); 3587416022c9SBarry Smith a = (Mat_SeqAIJ*)B->data; 358817ab2063SBarry Smith 35890752156aSBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 359017ab2063SBarry Smith 359117ab2063SBarry Smith /* read in nonzero values */ 35920752156aSBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 359317ab2063SBarry Smith 359417ab2063SBarry Smith /* set matrix "i" values */ 3595efb16452SHong Zhang a->i[0] = 0; 359617ab2063SBarry Smith for (i=1; i<= M; i++) { 3597416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 3598416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 359917ab2063SBarry Smith } 3600606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 360117ab2063SBarry Smith 36026d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 36036d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3604b3a1e11cSKris Buschelman *A = B; 36053a40ed3dSBarry Smith PetscFunctionReturn(0); 360617ab2063SBarry Smith } 360717ab2063SBarry Smith 36084a2ae208SSatish Balay #undef __FUNCT__ 3609fbdbba38SShri Abhyankar #define __FUNCT__ "MatLoadnew_SeqAIJ" 3610fbdbba38SShri Abhyankar PetscErrorCode MatLoadnew_SeqAIJ(PetscViewer viewer,Mat newMat) 3611fbdbba38SShri Abhyankar { 3612fbdbba38SShri Abhyankar Mat_SeqAIJ *a; 3613fbdbba38SShri Abhyankar PetscErrorCode ierr; 3614fbdbba38SShri Abhyankar PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols; 3615fbdbba38SShri Abhyankar int fd; 3616fbdbba38SShri Abhyankar PetscMPIInt size; 3617fbdbba38SShri Abhyankar MPI_Comm comm; 3618fbdbba38SShri Abhyankar 3619fbdbba38SShri Abhyankar PetscFunctionBegin; 3620fbdbba38SShri Abhyankar ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 3621fbdbba38SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3622fbdbba38SShri Abhyankar if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor"); 3623fbdbba38SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 3624fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 3625fbdbba38SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 3626fbdbba38SShri Abhyankar M = header[1]; N = header[2]; nz = header[3]; 3627fbdbba38SShri Abhyankar 3628fbdbba38SShri Abhyankar if (nz < 0) { 3629fbdbba38SShri Abhyankar SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 3630fbdbba38SShri Abhyankar } 3631fbdbba38SShri Abhyankar 3632fbdbba38SShri Abhyankar /* read in row lengths */ 3633fbdbba38SShri Abhyankar ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 3634fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 3635fbdbba38SShri Abhyankar 3636fbdbba38SShri Abhyankar /* check if sum of rowlengths is same as nz */ 3637fbdbba38SShri Abhyankar for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 3638fbdbba38SShri Abhyankar if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum); 3639fbdbba38SShri Abhyankar 3640fbdbba38SShri Abhyankar /* set global size if not set already*/ 3641f501eaabSShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) { 3642fbdbba38SShri Abhyankar ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 3643aabbc4fbSShri Abhyankar } else { 3644fbdbba38SShri Abhyankar /* if sizes and type are already set, check if the vector global sizes are correct */ 3645fbdbba38SShri Abhyankar ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr); 3646f501eaabSShri Abhyankar if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols); 3647aabbc4fbSShri Abhyankar } 3648fbdbba38SShri Abhyankar ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr); 3649fbdbba38SShri Abhyankar a = (Mat_SeqAIJ*)newMat->data; 3650fbdbba38SShri Abhyankar 3651fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 3652fbdbba38SShri Abhyankar 3653fbdbba38SShri Abhyankar /* read in nonzero values */ 3654fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 3655fbdbba38SShri Abhyankar 3656fbdbba38SShri Abhyankar /* set matrix "i" values */ 3657fbdbba38SShri Abhyankar a->i[0] = 0; 3658fbdbba38SShri Abhyankar for (i=1; i<= M; i++) { 3659fbdbba38SShri Abhyankar a->i[i] = a->i[i-1] + rowlengths[i-1]; 3660fbdbba38SShri Abhyankar a->ilen[i-1] = rowlengths[i-1]; 3661fbdbba38SShri Abhyankar } 3662fbdbba38SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 3663fbdbba38SShri Abhyankar 3664fbdbba38SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3665fbdbba38SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3666fbdbba38SShri Abhyankar PetscFunctionReturn(0); 3667fbdbba38SShri Abhyankar } 3668fbdbba38SShri Abhyankar 3669fbdbba38SShri Abhyankar #undef __FUNCT__ 3670b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ" 3671dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg) 36727264ac53SSatish Balay { 36737264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data; 3674dfbe8321SBarry Smith PetscErrorCode ierr; 3675eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 3676eeffb40dSHong Zhang PetscInt k; 3677eeffb40dSHong Zhang #endif 36787264ac53SSatish Balay 36793a40ed3dSBarry Smith PetscFunctionBegin; 3680bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 3681d0f46423SBarry Smith if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) { 3682ca44d042SBarry Smith *flg = PETSC_FALSE; 3683ca44d042SBarry Smith PetscFunctionReturn(0); 3684bcd2baecSBarry Smith } 36857264ac53SSatish Balay 36867264ac53SSatish Balay /* if the a->i are the same */ 3687d0f46423SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3688abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 36897264ac53SSatish Balay 36907264ac53SSatish Balay /* if a->j are the same */ 369197f1f81fSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3692abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 3693bcd2baecSBarry Smith 3694bcd2baecSBarry Smith /* if a->a are the same */ 3695eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 3696eeffb40dSHong Zhang for (k=0; k<a->nz; k++){ 3697eeffb40dSHong Zhang if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){ 3698eeffb40dSHong Zhang *flg = PETSC_FALSE; 36993a40ed3dSBarry Smith PetscFunctionReturn(0); 3700eeffb40dSHong Zhang } 3701eeffb40dSHong Zhang } 3702eeffb40dSHong Zhang #else 3703eeffb40dSHong Zhang ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 3704eeffb40dSHong Zhang #endif 3705eeffb40dSHong Zhang PetscFunctionReturn(0); 37067264ac53SSatish Balay } 370736db0b34SBarry Smith 37084a2ae208SSatish Balay #undef __FUNCT__ 37094a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays" 371005869f15SSatish Balay /*@ 371136db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 371236db0b34SBarry Smith provided by the user. 371336db0b34SBarry Smith 3714c75a6043SHong Zhang Collective on MPI_Comm 371536db0b34SBarry Smith 371636db0b34SBarry Smith Input Parameters: 371736db0b34SBarry Smith + comm - must be an MPI communicator of size 1 371836db0b34SBarry Smith . m - number of rows 371936db0b34SBarry Smith . n - number of columns 372036db0b34SBarry Smith . i - row indices 372136db0b34SBarry Smith . j - column indices 372236db0b34SBarry Smith - a - matrix values 372336db0b34SBarry Smith 372436db0b34SBarry Smith Output Parameter: 372536db0b34SBarry Smith . mat - the matrix 372636db0b34SBarry Smith 372736db0b34SBarry Smith Level: intermediate 372836db0b34SBarry Smith 372936db0b34SBarry Smith Notes: 37300551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 373136db0b34SBarry Smith once the matrix is destroyed 373236db0b34SBarry Smith 373336db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 373436db0b34SBarry Smith 3735bfeeae90SHong Zhang The i and j indices are 0 based 373636db0b34SBarry Smith 3737a4552177SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 3738a4552177SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 3739a4552177SSatish Balay as shown: 3740a4552177SSatish Balay 3741a4552177SSatish Balay 1 0 0 3742a4552177SSatish Balay 2 0 3 3743a4552177SSatish Balay 4 5 6 3744a4552177SSatish Balay 3745a4552177SSatish Balay i = {0,1,3,6} [size = nrow+1 = 3+1] 37469985e31cSBarry Smith j = {0,0,2,0,1,2} [size = nz = 6]; values must be sorted for each row 3747a4552177SSatish Balay v = {1,2,3,4,5,6} [size = nz = 6] 3748a4552177SSatish Balay 37499985e31cSBarry Smith 37502fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 375136db0b34SBarry Smith 375236db0b34SBarry Smith @*/ 3753a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat) 375436db0b34SBarry Smith { 3755dfbe8321SBarry Smith PetscErrorCode ierr; 3756cbcfb4deSHong Zhang PetscInt ii; 375736db0b34SBarry Smith Mat_SeqAIJ *aij; 3758cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG) 3759cbcfb4deSHong Zhang PetscInt jj; 3760cbcfb4deSHong Zhang #endif 376136db0b34SBarry Smith 376236db0b34SBarry Smith PetscFunctionBegin; 3763a96a251dSBarry Smith if (i[0]) { 3764e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 376536db0b34SBarry Smith } 3766f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3767f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 3768ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 3769ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 3770ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 3771ab93d7beSBarry Smith ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr); 3772ab93d7beSBarry Smith 377336db0b34SBarry Smith aij->i = i; 377436db0b34SBarry Smith aij->j = j; 377536db0b34SBarry Smith aij->a = a; 377636db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 377736db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 3778e6b907acSBarry Smith aij->free_a = PETSC_FALSE; 3779e6b907acSBarry Smith aij->free_ij = PETSC_FALSE; 378036db0b34SBarry Smith 378136db0b34SBarry Smith for (ii=0; ii<m; ii++) { 378236db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 37832515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 3784e32f2f54SBarry Smith if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]); 37859985e31cSBarry Smith for (jj=i[ii]+1; jj<i[ii+1]; jj++) { 3786e32f2f54SBarry Smith if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii); 3787e32f2f54SBarry Smith if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii); 37889985e31cSBarry Smith } 378936db0b34SBarry Smith #endif 379036db0b34SBarry Smith } 37912515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 379236db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 3793e32f2f54SBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]); 3794e32f2f54SBarry Smith if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]); 379536db0b34SBarry Smith } 379636db0b34SBarry Smith #endif 379736db0b34SBarry Smith 3798b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3799b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 380036db0b34SBarry Smith PetscFunctionReturn(0); 380136db0b34SBarry Smith } 380236db0b34SBarry Smith 3803cc8ba8e1SBarry Smith #undef __FUNCT__ 3804ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ" 3805dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring) 3806cc8ba8e1SBarry Smith { 3807dfbe8321SBarry Smith PetscErrorCode ierr; 3808cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 380936db0b34SBarry Smith 3810cc8ba8e1SBarry Smith PetscFunctionBegin; 38118ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 3812cc8ba8e1SBarry Smith ierr = ISColoringReference(coloring);CHKERRQ(ierr); 3813cc8ba8e1SBarry Smith a->coloring = coloring; 381412c595b3SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 381597f1f81fSBarry Smith PetscInt i,*larray; 381612c595b3SBarry Smith ISColoring ocoloring; 381708b6dcc0SBarry Smith ISColoringValue *colors; 381812c595b3SBarry Smith 381912c595b3SBarry Smith /* set coloring for diagonal portion */ 38200e83c824SBarry Smith ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr); 3821d0f46423SBarry Smith for (i=0; i<A->cmap->n; i++) { 382212c595b3SBarry Smith larray[i] = i; 382312c595b3SBarry Smith } 3824d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 38250e83c824SBarry Smith ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 3826d0f46423SBarry Smith for (i=0; i<A->cmap->n; i++) { 382712c595b3SBarry Smith colors[i] = coloring->colors[larray[i]]; 382812c595b3SBarry Smith } 382912c595b3SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 3830d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 383112c595b3SBarry Smith a->coloring = ocoloring; 383212c595b3SBarry Smith } 3833cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3834cc8ba8e1SBarry Smith } 3835cc8ba8e1SBarry Smith 3836dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 3837ee4f033dSBarry Smith EXTERN_C_BEGIN 383829c1e371SBarry Smith #include "adic/ad_utils.h" 3839ee4f033dSBarry Smith EXTERN_C_END 3840cc8ba8e1SBarry Smith 3841cc8ba8e1SBarry Smith #undef __FUNCT__ 3842ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 3843dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 3844cc8ba8e1SBarry Smith { 3845cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3846d0f46423SBarry Smith PetscInt m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen; 38474440f671SBarry Smith PetscScalar *v = a->a,*values = ((PetscScalar*)advalues)+1; 384808b6dcc0SBarry Smith ISColoringValue *color; 3849cc8ba8e1SBarry Smith 3850cc8ba8e1SBarry Smith PetscFunctionBegin; 3851e32f2f54SBarry Smith if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 38524440f671SBarry Smith nlen = PetscADGetDerivTypeSize()/sizeof(PetscScalar); 3853cc8ba8e1SBarry Smith color = a->coloring->colors; 3854cc8ba8e1SBarry Smith /* loop over rows */ 3855cc8ba8e1SBarry Smith for (i=0; i<m; i++) { 3856cc8ba8e1SBarry Smith nz = ii[i+1] - ii[i]; 3857cc8ba8e1SBarry Smith /* loop over columns putting computed value into matrix */ 3858cc8ba8e1SBarry Smith for (j=0; j<nz; j++) { 3859cc8ba8e1SBarry Smith *v++ = values[color[*jj++]]; 3860cc8ba8e1SBarry Smith } 38614440f671SBarry Smith values += nlen; /* jump to next row of derivatives */ 3862ee4f033dSBarry Smith } 3863ee4f033dSBarry Smith PetscFunctionReturn(0); 3864ee4f033dSBarry Smith } 3865ee4f033dSBarry Smith #endif 3866ee4f033dSBarry Smith 3867ee4f033dSBarry Smith #undef __FUNCT__ 3868ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ" 386997f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues) 3870ee4f033dSBarry Smith { 3871ee4f033dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3872d0f46423SBarry Smith PetscInt m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j; 387354f21887SBarry Smith MatScalar *v = a->a; 387454f21887SBarry Smith PetscScalar *values = (PetscScalar *)advalues; 387508b6dcc0SBarry Smith ISColoringValue *color; 3876ee4f033dSBarry Smith 3877ee4f033dSBarry Smith PetscFunctionBegin; 3878e32f2f54SBarry Smith if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 3879ee4f033dSBarry Smith color = a->coloring->colors; 3880ee4f033dSBarry Smith /* loop over rows */ 3881ee4f033dSBarry Smith for (i=0; i<m; i++) { 3882ee4f033dSBarry Smith nz = ii[i+1] - ii[i]; 3883ee4f033dSBarry Smith /* loop over columns putting computed value into matrix */ 3884ee4f033dSBarry Smith for (j=0; j<nz; j++) { 3885ee4f033dSBarry Smith *v++ = values[color[*jj++]]; 3886ee4f033dSBarry Smith } 3887ee4f033dSBarry Smith values += nl; /* jump to next row of derivatives */ 3888cc8ba8e1SBarry Smith } 3889cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3890cc8ba8e1SBarry Smith } 389136db0b34SBarry Smith 389281824310SBarry Smith /* 389381824310SBarry Smith Special version for direct calls from Fortran 389481824310SBarry Smith */ 389581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 389681824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 389781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 389881824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 389981824310SBarry Smith #endif 390081824310SBarry Smith 390181824310SBarry Smith /* Change these macros so can be used in void function */ 390281824310SBarry Smith #undef CHKERRQ 39037adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr) 390481824310SBarry Smith #undef SETERRQ2 3905e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 390681824310SBarry Smith 390781824310SBarry Smith EXTERN_C_BEGIN 390881824310SBarry Smith #undef __FUNCT__ 390981824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_" 39101f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr) 391181824310SBarry Smith { 391281824310SBarry Smith Mat A = *AA; 391381824310SBarry Smith PetscInt m = *mm, n = *nn; 391481824310SBarry Smith InsertMode is = *isis; 391581824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 391681824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 391781824310SBarry Smith PetscInt *imax,*ai,*ailen; 391881824310SBarry Smith PetscErrorCode ierr; 391981824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 392054f21887SBarry Smith MatScalar *ap,value,*aa; 3921edb03aefSBarry Smith PetscTruth ignorezeroentries = a->ignorezeroentries; 392281824310SBarry Smith PetscTruth roworiented = a->roworiented; 392381824310SBarry Smith 392481824310SBarry Smith PetscFunctionBegin; 3925d9e2c085SLisandro Dalcin ierr = MatPreallocated(A);CHKERRQ(ierr); 392681824310SBarry Smith imax = a->imax; 392781824310SBarry Smith ai = a->i; 392881824310SBarry Smith ailen = a->ilen; 392981824310SBarry Smith aj = a->j; 393081824310SBarry Smith aa = a->a; 393181824310SBarry Smith 393281824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 393381824310SBarry Smith row = im[k]; 393481824310SBarry Smith if (row < 0) continue; 393581824310SBarry Smith #if defined(PETSC_USE_DEBUG) 3936d0f46423SBarry Smith if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 393781824310SBarry Smith #endif 393881824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 393981824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 394081824310SBarry Smith low = 0; 394181824310SBarry Smith high = nrow; 394281824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 394381824310SBarry Smith if (in[l] < 0) continue; 394481824310SBarry Smith #if defined(PETSC_USE_DEBUG) 3945d0f46423SBarry Smith if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 394681824310SBarry Smith #endif 394781824310SBarry Smith col = in[l]; 394881824310SBarry Smith if (roworiented) { 394981824310SBarry Smith value = v[l + k*n]; 395081824310SBarry Smith } else { 395181824310SBarry Smith value = v[k + l*m]; 395281824310SBarry Smith } 395381824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 395481824310SBarry Smith 395581824310SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 395681824310SBarry Smith lastcol = col; 395781824310SBarry Smith while (high-low > 5) { 395881824310SBarry Smith t = (low+high)/2; 395981824310SBarry Smith if (rp[t] > col) high = t; 396081824310SBarry Smith else low = t; 396181824310SBarry Smith } 396281824310SBarry Smith for (i=low; i<high; i++) { 396381824310SBarry Smith if (rp[i] > col) break; 396481824310SBarry Smith if (rp[i] == col) { 396581824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 396681824310SBarry Smith else ap[i] = value; 396781824310SBarry Smith goto noinsert; 396881824310SBarry Smith } 396981824310SBarry Smith } 397081824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 397181824310SBarry Smith if (nonew == 1) goto noinsert; 39727adad957SLisandro Dalcin if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 3973fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 397481824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 397581824310SBarry Smith /* shift up all the later entries in this row */ 397681824310SBarry Smith for (ii=N; ii>=i; ii--) { 397781824310SBarry Smith rp[ii+1] = rp[ii]; 397881824310SBarry Smith ap[ii+1] = ap[ii]; 397981824310SBarry Smith } 398081824310SBarry Smith rp[i] = col; 398181824310SBarry Smith ap[i] = value; 398281824310SBarry Smith noinsert:; 398381824310SBarry Smith low = i + 1; 398481824310SBarry Smith } 398581824310SBarry Smith ailen[row] = nrow; 398681824310SBarry Smith } 398781824310SBarry Smith A->same_nonzero = PETSC_FALSE; 398881824310SBarry Smith PetscFunctionReturnVoid(); 398981824310SBarry Smith } 399081824310SBarry Smith EXTERN_C_END 3991