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*/ 110a835dfdSSatish Balay #include "petscbt.h" 1217ab2063SBarry Smith 134a2ae208SSatish Balay #undef __FUNCT__ 1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ" 1579299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is) 1679299369SBarry Smith { 1779299369SBarry Smith PetscErrorCode ierr; 1879299369SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) Y->data; 19d0f46423SBarry Smith PetscInt i,*diag, m = Y->rmap->n; 2054f21887SBarry Smith MatScalar *aa = aij->a; 2154f21887SBarry Smith PetscScalar *v; 2209f38230SBarry Smith PetscTruth missing; 2379299369SBarry Smith 2479299369SBarry Smith PetscFunctionBegin; 2509f38230SBarry Smith if (Y->assembled) { 2609f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr); 2709f38230SBarry Smith if (!missing) { 2879299369SBarry Smith diag = aij->diag; 2979299369SBarry Smith ierr = VecGetArray(D,&v);CHKERRQ(ierr); 3079299369SBarry Smith if (is == INSERT_VALUES) { 3179299369SBarry Smith for (i=0; i<m; i++) { 3279299369SBarry Smith aa[diag[i]] = v[i]; 3379299369SBarry Smith } 3479299369SBarry Smith } else { 3579299369SBarry Smith for (i=0; i<m; i++) { 3679299369SBarry Smith aa[diag[i]] += v[i]; 3779299369SBarry Smith } 3879299369SBarry Smith } 3979299369SBarry Smith ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 4079299369SBarry Smith PetscFunctionReturn(0); 4179299369SBarry Smith } 4209f38230SBarry Smith } 4309f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 4409f38230SBarry Smith PetscFunctionReturn(0); 4509f38230SBarry Smith } 4679299369SBarry Smith 4779299369SBarry Smith #undef __FUNCT__ 484a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ" 493acb8795SBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 5017ab2063SBarry Smith { 51416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 52dfbe8321SBarry Smith PetscErrorCode ierr; 5397f1f81fSBarry Smith PetscInt i,ishift; 5417ab2063SBarry Smith 553a40ed3dSBarry Smith PetscFunctionBegin; 56d0f46423SBarry Smith *m = A->rmap->n; 573a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 58bfeeae90SHong Zhang ishift = 0; 5953e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 60d0f46423SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 61bfeeae90SHong Zhang } else if (oshift == 1) { 62d0f46423SBarry Smith PetscInt nz = a->i[A->rmap->n]; 633b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 64d0f46423SBarry Smith ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr); 65d0f46423SBarry Smith for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1; 66ecc77c7aSBarry Smith if (ja) { 6797f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr); 683b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1; 69ecc77c7aSBarry Smith } 706945ee14SBarry Smith } else { 71ecc77c7aSBarry Smith *ia = a->i; 72ecc77c7aSBarry Smith if (ja) *ja = a->j; 73a2ce50c7SBarry Smith } 743a40ed3dSBarry Smith PetscFunctionReturn(0); 75a2744918SBarry Smith } 76a2744918SBarry Smith 774a2ae208SSatish Balay #undef __FUNCT__ 784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ" 793acb8795SBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 806945ee14SBarry Smith { 81dfbe8321SBarry Smith PetscErrorCode ierr; 826945ee14SBarry Smith 833a40ed3dSBarry Smith PetscFunctionBegin; 843a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 85bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 86606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 87ecc77c7aSBarry Smith if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);} 88bcd2baecSBarry Smith } 893a40ed3dSBarry Smith PetscFunctionReturn(0); 9017ab2063SBarry Smith } 9117ab2063SBarry Smith 924a2ae208SSatish Balay #undef __FUNCT__ 934a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ" 943acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 953b2fbd54SBarry Smith { 963b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 97dfbe8321SBarry Smith PetscErrorCode ierr; 98d0f46423SBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 9997f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 1003b2fbd54SBarry Smith 1013a40ed3dSBarry Smith PetscFunctionBegin; 102899cda47SBarry Smith *nn = n; 1033a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1043b2fbd54SBarry Smith if (symmetric) { 105d0f46423SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr); 1063b2fbd54SBarry Smith } else { 10797f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr); 10897f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 10997f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr); 11097f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr); 1113b2fbd54SBarry Smith jj = a->j; 1123b2fbd54SBarry Smith for (i=0; i<nz; i++) { 113bfeeae90SHong Zhang collengths[jj[i]]++; 1143b2fbd54SBarry Smith } 1153b2fbd54SBarry Smith cia[0] = oshift; 1163b2fbd54SBarry Smith for (i=0; i<n; i++) { 1173b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 1183b2fbd54SBarry Smith } 11997f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 1203b2fbd54SBarry Smith jj = a->j; 121a93ec695SBarry Smith for (row=0; row<m; row++) { 122a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 123a93ec695SBarry Smith for (i=0; i<mr; i++) { 124bfeeae90SHong Zhang col = *jj++; 1253b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 1263b2fbd54SBarry Smith } 1273b2fbd54SBarry Smith } 128606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 1293b2fbd54SBarry Smith *ia = cia; *ja = cja; 1303b2fbd54SBarry Smith } 1313a40ed3dSBarry Smith PetscFunctionReturn(0); 1323b2fbd54SBarry Smith } 1333b2fbd54SBarry Smith 1344a2ae208SSatish Balay #undef __FUNCT__ 1354a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ" 1363acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 1373b2fbd54SBarry Smith { 138dfbe8321SBarry Smith PetscErrorCode ierr; 139606d414cSSatish Balay 1403a40ed3dSBarry Smith PetscFunctionBegin; 1413a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1423b2fbd54SBarry Smith 143606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 144606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 1453b2fbd54SBarry Smith 1463a40ed3dSBarry Smith PetscFunctionReturn(0); 1473b2fbd54SBarry Smith } 1483b2fbd54SBarry Smith 14987d4246cSBarry Smith #undef __FUNCT__ 15087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ" 15187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 15287d4246cSBarry Smith { 15387d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15487d4246cSBarry Smith PetscInt *ai = a->i; 15587d4246cSBarry Smith PetscErrorCode ierr; 15687d4246cSBarry Smith 15787d4246cSBarry Smith PetscFunctionBegin; 15887d4246cSBarry Smith ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr); 15987d4246cSBarry Smith PetscFunctionReturn(0); 16087d4246cSBarry Smith } 16187d4246cSBarry Smith 162227d817aSBarry Smith #define CHUNKSIZE 15 16317ab2063SBarry Smith 1644a2ae208SSatish Balay #undef __FUNCT__ 1654a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ" 16697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 16717ab2063SBarry Smith { 168416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 169e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 17097f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 1716849ba73SBarry Smith PetscErrorCode ierr; 172e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 17354f21887SBarry Smith MatScalar *ap,value,*aa = a->a; 174edb03aefSBarry Smith PetscTruth ignorezeroentries = a->ignorezeroentries; 175273d9f13SBarry Smith PetscTruth roworiented = a->roworiented; 17617ab2063SBarry Smith 1773a40ed3dSBarry Smith PetscFunctionBegin; 17871fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 17917ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 180416022c9SBarry Smith row = im[k]; 1815ef9f2a5SBarry Smith if (row < 0) continue; 1822515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 183d0f46423SBarry Smith if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1); 1843b2fbd54SBarry Smith #endif 185bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 18617ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 187416022c9SBarry Smith low = 0; 188c71e6ed7SBarry Smith high = nrow; 18917ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 1905ef9f2a5SBarry Smith if (in[l] < 0) continue; 1912515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 192d0f46423SBarry Smith if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1); 1933b2fbd54SBarry Smith #endif 194bfeeae90SHong Zhang col = in[l]; 19516371a99SBarry Smith if (v) { 1964b0e389bSBarry Smith if (roworiented) { 1975ef9f2a5SBarry Smith value = v[l + k*n]; 198bef8e0ddSBarry Smith } else { 1994b0e389bSBarry Smith value = v[k + l*m]; 2004b0e389bSBarry Smith } 20116371a99SBarry Smith } else { 20275567043SBarry Smith value = 0.; 20316371a99SBarry Smith } 204abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 20536db0b34SBarry Smith 2067cd84e04SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 207e2ee6c50SBarry Smith lastcol = col; 208416022c9SBarry Smith while (high-low > 5) { 209416022c9SBarry Smith t = (low+high)/2; 210416022c9SBarry Smith if (rp[t] > col) high = t; 211416022c9SBarry Smith else low = t; 21217ab2063SBarry Smith } 213416022c9SBarry Smith for (i=low; i<high; i++) { 21417ab2063SBarry Smith if (rp[i] > col) break; 21517ab2063SBarry Smith if (rp[i] == col) { 216416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 21717ab2063SBarry Smith else ap[i] = value; 218e44c0bd4SBarry Smith low = i + 1; 21917ab2063SBarry Smith goto noinsert; 22017ab2063SBarry Smith } 22117ab2063SBarry Smith } 222abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 223c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 224cc72174dSBarry Smith if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 225d0f46423SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 226c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 227416022c9SBarry Smith /* shift up all the later entries in this row */ 228416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 22917ab2063SBarry Smith rp[ii+1] = rp[ii]; 23017ab2063SBarry Smith ap[ii+1] = ap[ii]; 23117ab2063SBarry Smith } 23217ab2063SBarry Smith rp[i] = col; 23317ab2063SBarry Smith ap[i] = value; 234416022c9SBarry Smith low = i + 1; 235e44c0bd4SBarry Smith noinsert:; 23617ab2063SBarry Smith } 23717ab2063SBarry Smith ailen[row] = nrow; 23817ab2063SBarry Smith } 23988e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 2403a40ed3dSBarry Smith PetscFunctionReturn(0); 24117ab2063SBarry Smith } 24217ab2063SBarry Smith 24381824310SBarry Smith 2444a2ae208SSatish Balay #undef __FUNCT__ 2454a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ" 246a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 2477eb43aa7SLois Curfman McInnes { 2487eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 24997f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 25097f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 25154f21887SBarry Smith MatScalar *ap,*aa = a->a; 2527eb43aa7SLois Curfman McInnes 2533a40ed3dSBarry Smith PetscFunctionBegin; 2547eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 2557eb43aa7SLois Curfman McInnes row = im[k]; 25697e567efSBarry Smith if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */ 257d0f46423SBarry Smith if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1); 258bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 2597eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2607eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 26197e567efSBarry Smith if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */ 262d0f46423SBarry Smith if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1); 263bfeeae90SHong Zhang col = in[l] ; 2647eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2657eb43aa7SLois Curfman McInnes while (high-low > 5) { 2667eb43aa7SLois Curfman McInnes t = (low+high)/2; 2677eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2687eb43aa7SLois Curfman McInnes else low = t; 2697eb43aa7SLois Curfman McInnes } 2707eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 2717eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2727eb43aa7SLois Curfman McInnes if (rp[i] == col) { 273b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2747eb43aa7SLois Curfman McInnes goto finished; 2757eb43aa7SLois Curfman McInnes } 2767eb43aa7SLois Curfman McInnes } 27797e567efSBarry Smith *v++ = 0.0; 2787eb43aa7SLois Curfman McInnes finished:; 2797eb43aa7SLois Curfman McInnes } 2807eb43aa7SLois Curfman McInnes } 2813a40ed3dSBarry Smith PetscFunctionReturn(0); 2827eb43aa7SLois Curfman McInnes } 2837eb43aa7SLois Curfman McInnes 28417ab2063SBarry Smith 2854a2ae208SSatish Balay #undef __FUNCT__ 2864a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary" 287dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 28817ab2063SBarry Smith { 289416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2906849ba73SBarry Smith PetscErrorCode ierr; 2916f69ff64SBarry Smith PetscInt i,*col_lens; 2926f69ff64SBarry Smith int fd; 29317ab2063SBarry Smith 2943a40ed3dSBarry Smith PetscFunctionBegin; 295b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 296d0f46423SBarry Smith ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr); 297552e946dSBarry Smith col_lens[0] = MAT_FILE_COOKIE; 298d0f46423SBarry Smith col_lens[1] = A->rmap->n; 299d0f46423SBarry Smith col_lens[2] = A->cmap->n; 300416022c9SBarry Smith col_lens[3] = a->nz; 301416022c9SBarry Smith 302416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 303d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 304416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 30517ab2063SBarry Smith } 306d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 307606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 308416022c9SBarry Smith 309416022c9SBarry Smith /* store column indices (zero start index) */ 3106f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 311416022c9SBarry Smith 312416022c9SBarry Smith /* store nonzero values */ 3136f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 3143a40ed3dSBarry Smith PetscFunctionReturn(0); 31517ab2063SBarry Smith } 316416022c9SBarry Smith 317dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 318cd155464SBarry Smith 3194a2ae208SSatish Balay #undef __FUNCT__ 3204a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII" 321dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 322416022c9SBarry Smith { 323416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 324dfbe8321SBarry Smith PetscErrorCode ierr; 325d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,shift=0; 326e060cb09SBarry Smith const char *name; 327f3ef73ceSBarry Smith PetscViewerFormat format; 32817ab2063SBarry Smith 3293a40ed3dSBarry Smith PetscFunctionBegin; 330435da068SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 331b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 33271c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 33397f1f81fSBarry Smith PetscInt nofinalvalue = 0; 334d0f46423SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) { 335d00d2cf4SBarry Smith nofinalvalue = 1; 336d00d2cf4SBarry Smith } 337b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 338d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr); 33977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 34077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 341b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 34217ab2063SBarry Smith 34317ab2063SBarry Smith for (i=0; i<m; i++) { 344416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 345aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 34677431f27SBarry 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); 34717ab2063SBarry Smith #else 34877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr); 34917ab2063SBarry Smith #endif 35017ab2063SBarry Smith } 35117ab2063SBarry Smith } 352d00d2cf4SBarry Smith if (nofinalvalue) { 353d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr); 354d00d2cf4SBarry Smith } 355fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 356b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 35768369a75SKris Buschelman } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) { 358cd155464SBarry Smith PetscFunctionReturn(0); 359fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 360b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 36144cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 36277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 36344cd7ae7SLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 364aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 36536db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 366a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 36736db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 368a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 36936db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 370a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 3716831982aSBarry Smith } 37244cd7ae7SLois Curfman McInnes #else 373a83599f4SBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);} 37444cd7ae7SLois Curfman McInnes #endif 37544cd7ae7SLois Curfman McInnes } 376b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 37744cd7ae7SLois Curfman McInnes } 378b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 379fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 38097f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 381b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 38297f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr); 383496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 384496be53dSLois Curfman McInnes sptr[i] = nzd+1; 385496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 386496be53dSLois Curfman McInnes if (a->j[j] >= i) { 387aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 38836db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 389496be53dSLois Curfman McInnes #else 390496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 391496be53dSLois Curfman McInnes #endif 392496be53dSLois Curfman McInnes } 393496be53dSLois Curfman McInnes } 394496be53dSLois Curfman McInnes } 3952e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 39677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 3972e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 39877431f27SBarry 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);} 39977431f27SBarry 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);} 40077431f27SBarry 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);} 40177431f27SBarry Smith else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);} 40277431f27SBarry Smith else if (i<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);} 40377431f27SBarry Smith else {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);} 404496be53dSLois Curfman McInnes } 405b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 406606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 407496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 408496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 40977431f27SBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);} 410496be53dSLois Curfman McInnes } 411b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 412496be53dSLois Curfman McInnes } 413b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 414496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 415496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 416496be53dSLois Curfman McInnes if (a->j[j] >= i) { 417aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 41836db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 419b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4206831982aSBarry Smith } 421496be53dSLois Curfman McInnes #else 422b0a32e0cSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);} 423496be53dSLois Curfman McInnes #endif 424496be53dSLois Curfman McInnes } 425496be53dSLois Curfman McInnes } 426b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 427496be53dSLois Curfman McInnes } 428b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 429fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 43097f1f81fSBarry Smith PetscInt cnt = 0,jcnt; 43187828ca2SBarry Smith PetscScalar value; 43202594712SBarry Smith 433b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 43402594712SBarry Smith for (i=0; i<m; i++) { 43502594712SBarry Smith jcnt = 0; 436d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 437e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 43802594712SBarry Smith value = a->a[cnt++]; 439e24b481bSBarry Smith jcnt++; 44002594712SBarry Smith } else { 44102594712SBarry Smith value = 0.0; 44202594712SBarry Smith } 443aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 444b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr); 44502594712SBarry Smith #else 446b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr); 44702594712SBarry Smith #endif 44802594712SBarry Smith } 449b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 45002594712SBarry Smith } 451b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 4523c215bfdSMatthew Knepley } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) { 4533c215bfdSMatthew Knepley ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 4543c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 4553c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr); 4563c215bfdSMatthew Knepley #else 4573c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr); 4583c215bfdSMatthew Knepley #endif 459d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr); 4603c215bfdSMatthew Knepley for (i=0; i<m; i++) { 4613c215bfdSMatthew Knepley for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 4623c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 4633c215bfdSMatthew Knepley if (PetscImaginaryPart(a->a[j]) > 0.0) { 4643c215bfdSMatthew 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); 4653c215bfdSMatthew Knepley } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 4663c215bfdSMatthew 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); 4673c215bfdSMatthew Knepley } else { 4683c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 4693c215bfdSMatthew Knepley } 4703c215bfdSMatthew Knepley #else 4713c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr); 4723c215bfdSMatthew Knepley #endif 4733c215bfdSMatthew Knepley } 4743c215bfdSMatthew Knepley } 4753c215bfdSMatthew Knepley ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 4763a40ed3dSBarry Smith } else { 477d6e78c31SSatish Balay ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 4780a3c351aSHong Zhang if (A->factor){ 47916cd7e1dSShri Abhyankar for (i=0; i<m; i++) { 48016cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 48116cd7e1dSShri Abhyankar /* L part */ 48216cd7e1dSShri Abhyankar for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 48316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 48416cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 48516cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 48616cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 48716cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 48816cd7e1dSShri Abhyankar } else { 48916cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 49016cd7e1dSShri Abhyankar } 49116cd7e1dSShri Abhyankar #else 49216cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 49316cd7e1dSShri Abhyankar #endif 49416cd7e1dSShri Abhyankar } 49516cd7e1dSShri Abhyankar /* diagonal */ 49616cd7e1dSShri Abhyankar j = a->diag[i]; 49716cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 49816cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 49916cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 50016cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 50116cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 50216cd7e1dSShri Abhyankar } else { 50316cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 50416cd7e1dSShri Abhyankar } 50516cd7e1dSShri Abhyankar #else 50616cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 50716cd7e1dSShri Abhyankar #endif 50816cd7e1dSShri Abhyankar 50916cd7e1dSShri Abhyankar /* U part */ 51016cd7e1dSShri Abhyankar for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) { 51116cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 51216cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 51316cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 51416cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 51516cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 51616cd7e1dSShri Abhyankar } else { 51716cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 51816cd7e1dSShri Abhyankar } 51916cd7e1dSShri Abhyankar #else 52016cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 52116cd7e1dSShri Abhyankar #endif 52216cd7e1dSShri Abhyankar } 52316cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 52416cd7e1dSShri Abhyankar } 52516cd7e1dSShri Abhyankar } else { 52617ab2063SBarry Smith for (i=0; i<m; i++) { 52777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 528416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 529aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 53036db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 531a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 53236db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 533a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 5343a40ed3dSBarry Smith } else { 535a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 53617ab2063SBarry Smith } 53717ab2063SBarry Smith #else 538a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 53917ab2063SBarry Smith #endif 54017ab2063SBarry Smith } 541b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 54217ab2063SBarry Smith } 54316cd7e1dSShri Abhyankar } 544b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 54517ab2063SBarry Smith } 546b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 5473a40ed3dSBarry Smith PetscFunctionReturn(0); 548416022c9SBarry Smith } 549416022c9SBarry Smith 5504a2ae208SSatish Balay #undef __FUNCT__ 5514a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom" 552dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 553416022c9SBarry Smith { 554480ef9eaSBarry Smith Mat A = (Mat) Aa; 555416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 556dfbe8321SBarry Smith PetscErrorCode ierr; 557d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,color; 55836db0b34SBarry Smith PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0; 559b0a32e0cSBarry Smith PetscViewer viewer; 560f3ef73ceSBarry Smith PetscViewerFormat format; 561cddf8d76SBarry Smith 5623a40ed3dSBarry Smith PetscFunctionBegin; 563480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 564b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 56519bcc07fSBarry Smith 566b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 567416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 5680513a670SBarry Smith 569fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 5700513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 571b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 572416022c9SBarry Smith for (i=0; i<m; i++) { 573cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 574bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 575bfeeae90SHong Zhang x_l = a->j[j] ; x_r = x_l + 1.0; 576aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 57736db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 578cddf8d76SBarry Smith #else 579cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 580cddf8d76SBarry Smith #endif 581b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 582cddf8d76SBarry Smith } 583cddf8d76SBarry Smith } 584b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 585cddf8d76SBarry Smith for (i=0; i<m; i++) { 586cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 587bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 588bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 589cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 590b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 591cddf8d76SBarry Smith } 592cddf8d76SBarry Smith } 593b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 594cddf8d76SBarry Smith for (i=0; i<m; i++) { 595cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 596bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 597bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 598aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 59936db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 600cddf8d76SBarry Smith #else 601cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 602cddf8d76SBarry Smith #endif 603b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 604416022c9SBarry Smith } 605416022c9SBarry Smith } 6060513a670SBarry Smith } else { 6070513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 6080513a670SBarry Smith /* first determine max of all nonzero values */ 60997f1f81fSBarry Smith PetscInt nz = a->nz,count; 610b0a32e0cSBarry Smith PetscDraw popup; 61136db0b34SBarry Smith PetscReal scale; 6120513a670SBarry Smith 6130513a670SBarry Smith for (i=0; i<nz; i++) { 6140513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 6150513a670SBarry Smith } 616b0a32e0cSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv; 617b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 618b0a32e0cSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);} 6190513a670SBarry Smith count = 0; 6200513a670SBarry Smith for (i=0; i<m; i++) { 6210513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 622bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 623bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 62497f1f81fSBarry Smith color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count])); 625b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 6260513a670SBarry Smith count++; 6270513a670SBarry Smith } 6280513a670SBarry Smith } 6290513a670SBarry Smith } 630480ef9eaSBarry Smith PetscFunctionReturn(0); 631480ef9eaSBarry Smith } 632cddf8d76SBarry Smith 6334a2ae208SSatish Balay #undef __FUNCT__ 6344a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw" 635dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 636480ef9eaSBarry Smith { 637dfbe8321SBarry Smith PetscErrorCode ierr; 638b0a32e0cSBarry Smith PetscDraw draw; 63936db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 640480ef9eaSBarry Smith PetscTruth isnull; 641480ef9eaSBarry Smith 642480ef9eaSBarry Smith PetscFunctionBegin; 643b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 644b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 645480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 646480ef9eaSBarry Smith 647480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 648d0f46423SBarry Smith xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0; 649480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 650b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 651b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 652480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr); 6533a40ed3dSBarry Smith PetscFunctionReturn(0); 654416022c9SBarry Smith } 655416022c9SBarry Smith 6564a2ae208SSatish Balay #undef __FUNCT__ 6574a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ" 658dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 659416022c9SBarry Smith { 660dfbe8321SBarry Smith PetscErrorCode ierr; 6616805f65bSBarry Smith PetscTruth iascii,isbinary,isdraw; 662416022c9SBarry Smith 6633a40ed3dSBarry Smith PetscFunctionBegin; 66432077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 665fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr); 666fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr); 667c45a1595SBarry Smith if (iascii) { 6683a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 6690f5bd95cSBarry Smith } else if (isbinary) { 6703a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 6710f5bd95cSBarry Smith } else if (isdraw) { 6723a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 6735cd90555SBarry Smith } else { 674958c9bccSBarry Smith SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name); 67517ab2063SBarry Smith } 6764108e4d5SBarry Smith ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr); 6773a40ed3dSBarry Smith PetscFunctionReturn(0); 67817ab2063SBarry Smith } 67919bcc07fSBarry Smith 6804a2ae208SSatish Balay #undef __FUNCT__ 6814a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ" 682dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 68317ab2063SBarry Smith { 684416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 6856849ba73SBarry Smith PetscErrorCode ierr; 68697f1f81fSBarry Smith PetscInt fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax; 687d0f46423SBarry Smith PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0; 68854f21887SBarry Smith MatScalar *aa = a->a,*ap; 6893447b6efSHong Zhang PetscReal ratio=0.6; 69017ab2063SBarry Smith 6913a40ed3dSBarry Smith PetscFunctionBegin; 6923a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 69317ab2063SBarry Smith 69443ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 69517ab2063SBarry Smith for (i=1; i<m; i++) { 696416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 69717ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 69894a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 69917ab2063SBarry Smith if (fshift) { 700bfeeae90SHong Zhang ip = aj + ai[i] ; 701bfeeae90SHong Zhang ap = aa + ai[i] ; 70217ab2063SBarry Smith N = ailen[i]; 70317ab2063SBarry Smith for (j=0; j<N; j++) { 70417ab2063SBarry Smith ip[j-fshift] = ip[j]; 70517ab2063SBarry Smith ap[j-fshift] = ap[j]; 70617ab2063SBarry Smith } 70717ab2063SBarry Smith } 70817ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 70917ab2063SBarry Smith } 71017ab2063SBarry Smith if (m) { 71117ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 71217ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 71317ab2063SBarry Smith } 71417ab2063SBarry Smith /* reset ilen and imax for each row */ 71517ab2063SBarry Smith for (i=0; i<m; i++) { 71617ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 71717ab2063SBarry Smith } 718bfeeae90SHong Zhang a->nz = ai[m]; 71928b2fa4aSMatthew Knepley if (fshift && a->nounused == -1) { 72028b2fa4aSMatthew Knepley SETERRQ3(PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift); 72128b2fa4aSMatthew Knepley } 72217ab2063SBarry Smith 72309f38230SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 724d0f46423SBarry 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); 725ae15b995SBarry Smith ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr); 726ae15b995SBarry Smith ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr); 727a0e1a203SBarry Smith 728dd5f02e7SSatish Balay a->reallocs = 0; 7294e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 73036db0b34SBarry Smith a->rmax = rmax; 7314e220ebcSLois Curfman McInnes 732cb5d8e9eSHong Zhang /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */ 733317fbc4cSHong Zhang ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 73488e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 73571c2f376SKris Buschelman 7364108e4d5SBarry Smith ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr); 73771f1c65dSBarry Smith 73871f1c65dSBarry Smith a->idiagvalid = PETSC_FALSE; 7393a40ed3dSBarry Smith PetscFunctionReturn(0); 74017ab2063SBarry Smith } 74117ab2063SBarry Smith 7424a2ae208SSatish Balay #undef __FUNCT__ 74399cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ" 74499cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 74599cafbc1SBarry Smith { 74699cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 74799cafbc1SBarry Smith PetscInt i,nz = a->nz; 74854f21887SBarry Smith MatScalar *aa = a->a; 74999cafbc1SBarry Smith 75099cafbc1SBarry Smith PetscFunctionBegin; 75199cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 75299cafbc1SBarry Smith PetscFunctionReturn(0); 75399cafbc1SBarry Smith } 75499cafbc1SBarry Smith 75599cafbc1SBarry Smith #undef __FUNCT__ 75699cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ" 75799cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 75899cafbc1SBarry Smith { 75999cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 76099cafbc1SBarry Smith PetscInt i,nz = a->nz; 76154f21887SBarry Smith MatScalar *aa = a->a; 76299cafbc1SBarry Smith 76399cafbc1SBarry Smith PetscFunctionBegin; 76499cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 76599cafbc1SBarry Smith PetscFunctionReturn(0); 76699cafbc1SBarry Smith } 76799cafbc1SBarry Smith 76899cafbc1SBarry Smith #undef __FUNCT__ 7694a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ" 770dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 77117ab2063SBarry Smith { 772416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 773dfbe8321SBarry Smith PetscErrorCode ierr; 7743a40ed3dSBarry Smith 7753a40ed3dSBarry Smith PetscFunctionBegin; 776d0f46423SBarry Smith ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 7773a40ed3dSBarry Smith PetscFunctionReturn(0); 77817ab2063SBarry Smith } 779416022c9SBarry Smith 7804a2ae208SSatish Balay #undef __FUNCT__ 7814a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ" 782dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 78317ab2063SBarry Smith { 784416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 785dfbe8321SBarry Smith PetscErrorCode ierr; 786d5d45c9bSBarry Smith 7873a40ed3dSBarry Smith PetscFunctionBegin; 788aa482453SBarry Smith #if defined(PETSC_USE_LOG) 789d0f46423SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz); 79017ab2063SBarry Smith #endif 791e6b907acSBarry Smith ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr); 792c38d4ed2SBarry Smith if (a->row) { 793c38d4ed2SBarry Smith ierr = ISDestroy(a->row);CHKERRQ(ierr); 794c38d4ed2SBarry Smith } 795c38d4ed2SBarry Smith if (a->col) { 796c38d4ed2SBarry Smith ierr = ISDestroy(a->col);CHKERRQ(ierr); 797c38d4ed2SBarry Smith } 79805b42c5fSBarry Smith ierr = PetscFree(a->diag);CHKERRQ(ierr); 79905b42c5fSBarry Smith ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr); 80071f1c65dSBarry Smith ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr); 80105b42c5fSBarry Smith ierr = PetscFree(a->solve_work);CHKERRQ(ierr); 80282bf6240SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} 80305b42c5fSBarry Smith ierr = PetscFree(a->saved_values);CHKERRQ(ierr); 804cc8ba8e1SBarry Smith if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);} 80505b42c5fSBarry Smith ierr = PetscFree(a->xtoy);CHKERRQ(ierr); 806407f6b05SHong Zhang if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);} 8070e83c824SBarry Smith if (a->compressedrow.checked && a->compressedrow.use){ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);} 808a30b2313SHong Zhang 8094108e4d5SBarry Smith ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr); 8104846f1f5SKris Buschelman 811606d414cSSatish Balay ierr = PetscFree(a);CHKERRQ(ierr); 812901853e0SKris Buschelman 813dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr); 814901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr); 815901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 816901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 817901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr); 818901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr); 819ba03775dSHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr); 820901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 821901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 822a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 823901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr); 8243a40ed3dSBarry Smith PetscFunctionReturn(0); 82517ab2063SBarry Smith } 82617ab2063SBarry Smith 8274a2ae208SSatish Balay #undef __FUNCT__ 8284a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ" 8294e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg) 83017ab2063SBarry Smith { 831416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8324846f1f5SKris Buschelman PetscErrorCode ierr; 8333a40ed3dSBarry Smith 8343a40ed3dSBarry Smith PetscFunctionBegin; 835a65d3064SKris Buschelman switch (op) { 836a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 8374e0d8c25SBarry Smith a->roworiented = flg; 838a65d3064SKris Buschelman break; 839a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 840a9817697SBarry Smith a->keepnonzeropattern = flg; 841a65d3064SKris Buschelman break; 842512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 843512a5fc5SBarry Smith a->nonew = (flg ? 0 : 1); 844a65d3064SKris Buschelman break; 845a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 8464e0d8c25SBarry Smith a->nonew = (flg ? -1 : 0); 847a65d3064SKris Buschelman break; 848a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 8494e0d8c25SBarry Smith a->nonew = (flg ? -2 : 0); 850a65d3064SKris Buschelman break; 85128b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 85228b2fa4aSMatthew Knepley a->nounused = (flg ? -1 : 0); 85328b2fa4aSMatthew Knepley break; 854a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 8554e0d8c25SBarry Smith a->ignorezeroentries = flg; 8560df259c2SBarry Smith break; 857d487561eSHong Zhang case MAT_USE_COMPRESSEDROW: 8584e0d8c25SBarry Smith a->compressedrow.use = flg; 859d487561eSHong Zhang break; 860b1646e73SJed Brown case MAT_SYMMETRIC: 861b1646e73SJed Brown case MAT_STRUCTURALLY_SYMMETRIC: 862b1646e73SJed Brown case MAT_HERMITIAN: 863b1646e73SJed Brown case MAT_SYMMETRY_ETERNAL: 8644e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 865a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 866a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 867290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 868a65d3064SKris Buschelman break; 869b87ac2d8SJed Brown case MAT_USE_INODES: 870b87ac2d8SJed Brown /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */ 871b87ac2d8SJed Brown break; 872a65d3064SKris Buschelman default: 87377b7860eSJed Brown SETERRQ1(PETSC_ERR_SUP,"unknown option %d",op); 874a65d3064SKris Buschelman } 8754108e4d5SBarry Smith ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr); 8763a40ed3dSBarry Smith PetscFunctionReturn(0); 87717ab2063SBarry Smith } 87817ab2063SBarry Smith 8794a2ae208SSatish Balay #undef __FUNCT__ 8804a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ" 881dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 88217ab2063SBarry Smith { 883416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8846849ba73SBarry Smith PetscErrorCode ierr; 88597f1f81fSBarry Smith PetscInt i,j,n; 88687828ca2SBarry Smith PetscScalar *x,zero = 0.0; 88717ab2063SBarry Smith 8883a40ed3dSBarry Smith PetscFunctionBegin; 8892dcb1b2aSMatthew Knepley ierr = VecSet(v,zero);CHKERRQ(ierr); 8901ebc52fbSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 89136db0b34SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 892d0f46423SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 893d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 894bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 895bfeeae90SHong Zhang if (a->j[j] == i) { 896416022c9SBarry Smith x[i] = a->a[j]; 89717ab2063SBarry Smith break; 89817ab2063SBarry Smith } 89917ab2063SBarry Smith } 90017ab2063SBarry Smith } 9011ebc52fbSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 9023a40ed3dSBarry Smith PetscFunctionReturn(0); 90317ab2063SBarry Smith } 90417ab2063SBarry Smith 905b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h" 9064a2ae208SSatish Balay #undef __FUNCT__ 9074a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ" 908dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 90917ab2063SBarry Smith { 910416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 9115c897100SBarry Smith PetscScalar *x,*y; 912dfbe8321SBarry Smith PetscErrorCode ierr; 913d0f46423SBarry Smith PetscInt m = A->rmap->n; 9145c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 915a77337e4SBarry Smith MatScalar *v; 916a77337e4SBarry Smith PetscScalar alpha; 91704fbf559SBarry Smith PetscInt n,i,j,*idx,*ii,*ridx=PETSC_NULL; 9183447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 9194eb6d288SHong Zhang PetscTruth usecprow = cprow.use; 9205c897100SBarry Smith #endif 92117ab2063SBarry Smith 9223a40ed3dSBarry Smith PetscFunctionBegin; 9232e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 9241ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 9251ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 9265c897100SBarry Smith 9275c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 928bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 9295c897100SBarry Smith #else 9303447b6efSHong Zhang if (usecprow){ 9313447b6efSHong Zhang m = cprow.nrows; 9323447b6efSHong Zhang ii = cprow.i; 9337b2bb3b9SHong Zhang ridx = cprow.rindex; 9343447b6efSHong Zhang } else { 9353447b6efSHong Zhang ii = a->i; 9363447b6efSHong Zhang } 93717ab2063SBarry Smith for (i=0; i<m; i++) { 9383447b6efSHong Zhang idx = a->j + ii[i] ; 9393447b6efSHong Zhang v = a->a + ii[i] ; 9403447b6efSHong Zhang n = ii[i+1] - ii[i]; 9413447b6efSHong Zhang if (usecprow){ 9427b2bb3b9SHong Zhang alpha = x[ridx[i]]; 9433447b6efSHong Zhang } else { 94417ab2063SBarry Smith alpha = x[i]; 9453447b6efSHong Zhang } 94604fbf559SBarry Smith for (j=0; j<n; j++) y[idx[j]] += alpha*v[j]; 94717ab2063SBarry Smith } 9485c897100SBarry Smith #endif 949dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 9501ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 9511ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9523a40ed3dSBarry Smith PetscFunctionReturn(0); 95317ab2063SBarry Smith } 95417ab2063SBarry Smith 9554a2ae208SSatish Balay #undef __FUNCT__ 9565c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ" 957dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 9585c897100SBarry Smith { 959dfbe8321SBarry Smith PetscErrorCode ierr; 9605c897100SBarry Smith 9615c897100SBarry Smith PetscFunctionBegin; 962170fe5c8SBarry Smith ierr = VecSet(yy,0.0);CHKERRQ(ierr); 9635c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 9645c897100SBarry Smith PetscFunctionReturn(0); 9655c897100SBarry Smith } 9665c897100SBarry Smith 967a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h" 9685c897100SBarry Smith #undef __FUNCT__ 9694a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ" 970dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 97117ab2063SBarry Smith { 972416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 973d9fead3dSBarry Smith PetscScalar *y; 97454f21887SBarry Smith const PetscScalar *x; 97554f21887SBarry Smith const MatScalar *aa; 976dfbe8321SBarry Smith PetscErrorCode ierr; 977003131ecSBarry Smith PetscInt m=A->rmap->n; 978003131ecSBarry Smith const PetscInt *aj,*ii,*ridx=PETSC_NULL; 9798aee2decSHong Zhang PetscInt n,i,nonzerorow=0; 980362ced78SSatish Balay PetscScalar sum; 98197952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 98217ab2063SBarry Smith 983b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 98497952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 985fee21e36SBarry Smith #endif 986fee21e36SBarry Smith 9873a40ed3dSBarry Smith PetscFunctionBegin; 988d9fead3dSBarry Smith ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr); 9891ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 99097952fefSHong Zhang aj = a->j; 99197952fefSHong Zhang aa = a->a; 992416022c9SBarry Smith ii = a->i; 9934eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 99497952fefSHong Zhang m = a->compressedrow.nrows; 99597952fefSHong Zhang ii = a->compressedrow.i; 99697952fefSHong Zhang ridx = a->compressedrow.rindex; 99797952fefSHong Zhang for (i=0; i<m; i++){ 99897952fefSHong Zhang n = ii[i+1] - ii[i]; 99997952fefSHong Zhang aj = a->j + ii[i]; 100097952fefSHong Zhang aa = a->a + ii[i]; 100197952fefSHong Zhang sum = 0.0; 1002a46b3154SVictor Eijkhout nonzerorow += (n>0); 1003003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 1004003131ecSBarry Smith /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 100597952fefSHong Zhang y[*ridx++] = sum; 100697952fefSHong Zhang } 100797952fefSHong Zhang } else { /* do not use compressed row format */ 1008b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 1009b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 1010b05257ddSBarry Smith #else 101117ab2063SBarry Smith for (i=0; i<m; i++) { 1012003131ecSBarry Smith n = ii[i+1] - ii[i]; 1013003131ecSBarry Smith aj = a->j + ii[i]; 1014003131ecSBarry Smith aa = a->a + ii[i]; 101517ab2063SBarry Smith sum = 0.0; 1016a46b3154SVictor Eijkhout nonzerorow += (n>0); 1017003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 101817ab2063SBarry Smith y[i] = sum; 101917ab2063SBarry Smith } 10208d195f9aSBarry Smith #endif 1021b05257ddSBarry Smith } 1022dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr); 1023d9fead3dSBarry Smith ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr); 10241ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 10253a40ed3dSBarry Smith PetscFunctionReturn(0); 102617ab2063SBarry Smith } 102717ab2063SBarry Smith 1028a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h" 10294a2ae208SSatish Balay #undef __FUNCT__ 10304a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ" 1031dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 103217ab2063SBarry Smith { 1033416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 103454f21887SBarry Smith PetscScalar *x,*y,*z; 103554f21887SBarry Smith const MatScalar *aa; 1036dfbe8321SBarry Smith PetscErrorCode ierr; 1037d0f46423SBarry Smith PetscInt m = A->rmap->n,*aj,*ii; 1038aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 103997952fefSHong Zhang PetscInt n,i,jrow,j,*ridx=PETSC_NULL; 1040362ced78SSatish Balay PetscScalar sum; 104197952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 1042e36a17ebSSatish Balay #endif 10439ea0dfa2SSatish Balay 10443a40ed3dSBarry Smith PetscFunctionBegin; 10451ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 10461ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 10472e8a6d31SBarry Smith if (zz != yy) { 10481ebc52fbSHong Zhang ierr = VecGetArray(zz,&z);CHKERRQ(ierr); 10492e8a6d31SBarry Smith } else { 10502e8a6d31SBarry Smith z = y; 10512e8a6d31SBarry Smith } 1052bfeeae90SHong Zhang 105397952fefSHong Zhang aj = a->j; 105497952fefSHong Zhang aa = a->a; 1055cddf8d76SBarry Smith ii = a->i; 1056aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 105797952fefSHong Zhang fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 105802ab625aSSatish Balay #else 10594eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 10604eb6d288SHong Zhang if (zz != yy){ 10614eb6d288SHong Zhang ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr); 10624eb6d288SHong Zhang } 106397952fefSHong Zhang m = a->compressedrow.nrows; 106497952fefSHong Zhang ii = a->compressedrow.i; 106597952fefSHong Zhang ridx = a->compressedrow.rindex; 106697952fefSHong Zhang for (i=0; i<m; i++){ 106797952fefSHong Zhang n = ii[i+1] - ii[i]; 106897952fefSHong Zhang aj = a->j + ii[i]; 106997952fefSHong Zhang aa = a->a + ii[i]; 107097952fefSHong Zhang sum = y[*ridx]; 107197952fefSHong Zhang for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; 107297952fefSHong Zhang z[*ridx++] = sum; 107397952fefSHong Zhang } 107497952fefSHong Zhang } else { /* do not use compressed row format */ 107517ab2063SBarry Smith for (i=0; i<m; i++) { 10769ea0dfa2SSatish Balay jrow = ii[i]; 10779ea0dfa2SSatish Balay n = ii[i+1] - jrow; 107817ab2063SBarry Smith sum = y[i]; 10799ea0dfa2SSatish Balay for (j=0; j<n; j++) { 108097952fefSHong Zhang sum += aa[jrow]*x[aj[jrow]]; jrow++; 10819ea0dfa2SSatish Balay } 108217ab2063SBarry Smith z[i] = sum; 108317ab2063SBarry Smith } 108497952fefSHong Zhang } 108502ab625aSSatish Balay #endif 1086dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 10871ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 10881ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 10892e8a6d31SBarry Smith if (zz != yy) { 10901ebc52fbSHong Zhang ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr); 10912e8a6d31SBarry Smith } 10923a40ed3dSBarry Smith PetscFunctionReturn(0); 109317ab2063SBarry Smith } 109417ab2063SBarry Smith 109517ab2063SBarry Smith /* 109617ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 109717ab2063SBarry Smith */ 10984a2ae208SSatish Balay #undef __FUNCT__ 10994a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ" 1100dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 110117ab2063SBarry Smith { 1102416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 11036849ba73SBarry Smith PetscErrorCode ierr; 1104d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n; 110517ab2063SBarry Smith 11063a40ed3dSBarry Smith PetscFunctionBegin; 110709f38230SBarry Smith if (!a->diag) { 110809f38230SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr); 11099518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr); 111009f38230SBarry Smith } 1111d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 111209f38230SBarry Smith a->diag[i] = a->i[i+1]; 1113bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1114bfeeae90SHong Zhang if (a->j[j] == i) { 111509f38230SBarry Smith a->diag[i] = j; 111617ab2063SBarry Smith break; 111717ab2063SBarry Smith } 111817ab2063SBarry Smith } 111917ab2063SBarry Smith } 11203a40ed3dSBarry Smith PetscFunctionReturn(0); 112117ab2063SBarry Smith } 112217ab2063SBarry Smith 1123be5855fcSBarry Smith /* 1124be5855fcSBarry Smith Checks for missing diagonals 1125be5855fcSBarry Smith */ 11264a2ae208SSatish Balay #undef __FUNCT__ 11274a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ" 112809f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d) 1129be5855fcSBarry Smith { 1130be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 113197f1f81fSBarry Smith PetscInt *diag,*jj = a->j,i; 1132be5855fcSBarry Smith 1133be5855fcSBarry Smith PetscFunctionBegin; 113409f38230SBarry Smith *missing = PETSC_FALSE; 1135d0f46423SBarry Smith if (A->rmap->n > 0 && !jj) { 113609f38230SBarry Smith *missing = PETSC_TRUE; 113709f38230SBarry Smith if (d) *d = 0; 113809f38230SBarry Smith PetscInfo(A,"Matrix has no entries therefor is missing diagonal"); 113909f38230SBarry Smith } else { 1140f1e2ffcdSBarry Smith diag = a->diag; 1141d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 1142bfeeae90SHong Zhang if (jj[diag[i]] != i) { 114309f38230SBarry Smith *missing = PETSC_TRUE; 114409f38230SBarry Smith if (d) *d = i; 114509f38230SBarry Smith PetscInfo1(A,"Matrix is missing diagonal number %D",i); 114609f38230SBarry Smith } 1147be5855fcSBarry Smith } 1148be5855fcSBarry Smith } 1149be5855fcSBarry Smith PetscFunctionReturn(0); 1150be5855fcSBarry Smith } 1151be5855fcSBarry Smith 115271f1c65dSBarry Smith EXTERN_C_BEGIN 115371f1c65dSBarry Smith #undef __FUNCT__ 115471f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ" 115571f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift) 115671f1c65dSBarry Smith { 115771f1c65dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 115871f1c65dSBarry Smith PetscErrorCode ierr; 1159d0f46423SBarry Smith PetscInt i,*diag,m = A->rmap->n; 116054f21887SBarry Smith MatScalar *v = a->a; 116154f21887SBarry Smith PetscScalar *idiag,*mdiag; 116271f1c65dSBarry Smith 116371f1c65dSBarry Smith PetscFunctionBegin; 116471f1c65dSBarry Smith if (a->idiagvalid) PetscFunctionReturn(0); 116571f1c65dSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 116671f1c65dSBarry Smith diag = a->diag; 116771f1c65dSBarry Smith if (!a->idiag) { 116871f1c65dSBarry Smith ierr = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr); 116971f1c65dSBarry Smith ierr = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr); 117071f1c65dSBarry Smith v = a->a; 117171f1c65dSBarry Smith } 117271f1c65dSBarry Smith mdiag = a->mdiag; 117371f1c65dSBarry Smith idiag = a->idiag; 117471f1c65dSBarry Smith 1175028cd4eaSSatish Balay if (omega == 1.0 && !PetscAbsScalar(fshift)) { 117671f1c65dSBarry Smith for (i=0; i<m; i++) { 117771f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 117871f1c65dSBarry Smith if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i); 117971f1c65dSBarry Smith idiag[i] = 1.0/v[diag[i]]; 118071f1c65dSBarry Smith } 118171f1c65dSBarry Smith ierr = PetscLogFlops(m);CHKERRQ(ierr); 118271f1c65dSBarry Smith } else { 118371f1c65dSBarry Smith for (i=0; i<m; i++) { 118471f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 118571f1c65dSBarry Smith idiag[i] = omega/(fshift + v[diag[i]]); 118671f1c65dSBarry Smith } 1187dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr); 118871f1c65dSBarry Smith } 118971f1c65dSBarry Smith a->idiagvalid = PETSC_TRUE; 119071f1c65dSBarry Smith PetscFunctionReturn(0); 119171f1c65dSBarry Smith } 11925a9745a3SMatthew Knepley EXTERN_C_END 119371f1c65dSBarry Smith 1194a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h" 11954a2ae208SSatish Balay #undef __FUNCT__ 119641f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ" 119741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 119817ab2063SBarry Smith { 1199416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1200e6d1f457SBarry Smith PetscScalar *x,d,sum,*t,scale; 1201e6d1f457SBarry Smith const MatScalar *v = a->a,*idiag=0,*mdiag; 120254f21887SBarry Smith const PetscScalar *b, *bs,*xb, *ts; 1203dfbe8321SBarry Smith PetscErrorCode ierr; 1204d0f46423SBarry Smith PetscInt n = A->cmap->n,m = A->rmap->n,i; 120597f1f81fSBarry Smith const PetscInt *idx,*diag; 120617ab2063SBarry Smith 12073a40ed3dSBarry Smith PetscFunctionBegin; 1208b965ef7fSBarry Smith its = its*lits; 120991723122SBarry Smith 121071f1c65dSBarry Smith if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */ 121171f1c65dSBarry Smith if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);} 121271f1c65dSBarry Smith a->fshift = fshift; 121371f1c65dSBarry Smith a->omega = omega; 1214ed480e8bSBarry Smith 121571f1c65dSBarry Smith diag = a->diag; 121671f1c65dSBarry Smith t = a->ssor_work; 1217ed480e8bSBarry Smith idiag = a->idiag; 121871f1c65dSBarry Smith mdiag = a->mdiag; 1219ed480e8bSBarry Smith 12201ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1221fb2e594dSBarry Smith if (xx != bb) { 12221ebc52fbSHong Zhang ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1223fb2e594dSBarry Smith } else { 1224fb2e594dSBarry Smith b = x; 1225fb2e594dSBarry Smith } 122671f1c65dSBarry Smith CHKMEMQ; 1227ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 122817ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 122917ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1230ed480e8bSBarry Smith bs = b; 123117ab2063SBarry Smith for (i=0; i<m; i++) { 123271f1c65dSBarry Smith d = fshift + mdiag[i]; 1233416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1234ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1235ed480e8bSBarry Smith v = a->a + diag[i] + 1; 123617ab2063SBarry Smith sum = b[i]*d/omega; 1237003131ecSBarry Smith PetscSparseDensePlusDot(sum,bs,v,idx,n); 123817ab2063SBarry Smith x[i] = sum; 123917ab2063SBarry Smith } 12401ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 12411ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 1242efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 12433a40ed3dSBarry Smith PetscFunctionReturn(0); 124417ab2063SBarry Smith } 1245c783ea89SBarry Smith 124648af12d7SBarry Smith if (flag == SOR_APPLY_LOWER) { 124729bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 12483a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 124917ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 1250887ee2caSBarry Smith U is upper triangular, E = D/omega; This routine applies 125117ab2063SBarry Smith 125217ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 125317ab2063SBarry Smith 1254887ee2caSBarry Smith to a vector efficiently using Eisenstat's trick. 125517ab2063SBarry Smith */ 125617ab2063SBarry Smith scale = (2.0/omega) - 1.0; 125717ab2063SBarry Smith 125817ab2063SBarry Smith /* x = (E + U)^{-1} b */ 125917ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1260416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1261ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1262ed480e8bSBarry Smith v = a->a + diag[i] + 1; 126317ab2063SBarry Smith sum = b[i]; 1264e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1265ed480e8bSBarry Smith x[i] = sum*idiag[i]; 126617ab2063SBarry Smith } 126717ab2063SBarry Smith 126817ab2063SBarry Smith /* t = b - (2*E - D)x */ 1269416022c9SBarry Smith v = a->a; 1270ed480e8bSBarry Smith for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; } 127117ab2063SBarry Smith 127217ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1273ed480e8bSBarry Smith ts = t; 1274416022c9SBarry Smith diag = a->diag; 127517ab2063SBarry Smith for (i=0; i<m; i++) { 1276416022c9SBarry Smith n = diag[i] - a->i[i]; 1277ed480e8bSBarry Smith idx = a->j + a->i[i]; 1278ed480e8bSBarry Smith v = a->a + a->i[i]; 127917ab2063SBarry Smith sum = t[i]; 1280003131ecSBarry Smith PetscSparseDenseMinusDot(sum,ts,v,idx,n); 1281ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1282733d66baSBarry Smith /* x = x + t */ 1283733d66baSBarry Smith x[i] += t[i]; 128417ab2063SBarry Smith } 128517ab2063SBarry Smith 1286dc0b31edSSatish Balay ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr); 12871ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 12881ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 12893a40ed3dSBarry Smith PetscFunctionReturn(0); 129017ab2063SBarry Smith } 129117ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 129217ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 129317ab2063SBarry Smith for (i=0; i<m; i++) { 1294416022c9SBarry Smith n = diag[i] - a->i[i]; 1295ed480e8bSBarry Smith idx = a->j + a->i[i]; 1296ed480e8bSBarry Smith v = a->a + a->i[i]; 129717ab2063SBarry Smith sum = b[i]; 1298e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 12995c99c7daSBarry Smith t[i] = sum; 1300ed480e8bSBarry Smith x[i] = sum*idiag[i]; 130117ab2063SBarry Smith } 13025c99c7daSBarry Smith xb = t; 1303efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 13043a40ed3dSBarry Smith } else xb = b; 130517ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 130617ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1307416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1308ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1309ed480e8bSBarry Smith v = a->a + diag[i] + 1; 131017ab2063SBarry Smith sum = xb[i]; 1311e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 13125c99c7daSBarry Smith if (xb == b) { 1313ed480e8bSBarry Smith x[i] = sum*idiag[i]; 13145c99c7daSBarry Smith } else { 13155c99c7daSBarry Smith x[i] = (1-omega)*x[i] + sum*idiag[i]; 131617ab2063SBarry Smith } 13175c99c7daSBarry Smith } 1318efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 131917ab2063SBarry Smith } 132017ab2063SBarry Smith its--; 132117ab2063SBarry Smith } 132217ab2063SBarry Smith while (its--) { 132317ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 132417ab2063SBarry Smith for (i=0; i<m; i++) { 1325416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1326ed480e8bSBarry Smith idx = a->j + a->i[i]; 1327ed480e8bSBarry Smith v = a->a + a->i[i]; 132817ab2063SBarry Smith sum = b[i]; 1329e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1330ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 133117ab2063SBarry Smith } 13329f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 133317ab2063SBarry Smith } 133417ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 133517ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1336416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1337ed480e8bSBarry Smith idx = a->j + a->i[i]; 1338ed480e8bSBarry Smith v = a->a + a->i[i]; 133917ab2063SBarry Smith sum = b[i]; 1340e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1341ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 134217ab2063SBarry Smith } 13439f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 134417ab2063SBarry Smith } 134517ab2063SBarry Smith } 13461ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 13471ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 134871f1c65dSBarry Smith CHKMEMQ; PetscFunctionReturn(0); 134917ab2063SBarry Smith } 135017ab2063SBarry Smith 13512af78befSBarry Smith 13524a2ae208SSatish Balay #undef __FUNCT__ 13534a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ" 1354dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 135517ab2063SBarry Smith { 1356416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 13574e220ebcSLois Curfman McInnes 13583a40ed3dSBarry Smith PetscFunctionBegin; 13594e220ebcSLois Curfman McInnes info->block_size = 1.0; 13604e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 13614e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 13624e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 13634e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 13644e220ebcSLois Curfman McInnes info->mallocs = (double)a->reallocs; 13657adad957SLisandro Dalcin info->memory = ((PetscObject)A)->mem; 13664e220ebcSLois Curfman McInnes if (A->factor) { 13674e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 13684e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 13694e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 13704e220ebcSLois Curfman McInnes } else { 13714e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 13724e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 13734e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 13744e220ebcSLois Curfman McInnes } 13753a40ed3dSBarry Smith PetscFunctionReturn(0); 137617ab2063SBarry Smith } 137717ab2063SBarry Smith 13784a2ae208SSatish Balay #undef __FUNCT__ 13794a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ" 1380f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag) 138117ab2063SBarry Smith { 1382416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1383d0f46423SBarry Smith PetscInt i,m = A->rmap->n - 1,d; 13846849ba73SBarry Smith PetscErrorCode ierr; 138509f38230SBarry Smith PetscTruth missing; 138617ab2063SBarry Smith 13873a40ed3dSBarry Smith PetscFunctionBegin; 1388a9817697SBarry Smith if (a->keepnonzeropattern) { 1389f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 139077431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1391bfeeae90SHong Zhang ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1392f1e2ffcdSBarry Smith } 1393f4df32b1SMatthew Knepley if (diag != 0.0) { 139409f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 139509f38230SBarry Smith if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d); 1396f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1397f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 1398f1e2ffcdSBarry Smith } 1399f1e2ffcdSBarry Smith } 140088e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 1401f1e2ffcdSBarry Smith } else { 1402f4df32b1SMatthew Knepley if (diag != 0.0) { 140317ab2063SBarry Smith for (i=0; i<N; i++) { 140477431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 14057ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1406416022c9SBarry Smith a->ilen[rows[i]] = 1; 1407f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 1408bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 14097ae801bdSBarry Smith } else { /* in case row was completely empty */ 1410f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 141117ab2063SBarry Smith } 141217ab2063SBarry Smith } 14133a40ed3dSBarry Smith } else { 141417ab2063SBarry Smith for (i=0; i<N; i++) { 141577431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1416416022c9SBarry Smith a->ilen[rows[i]] = 0; 141717ab2063SBarry Smith } 141817ab2063SBarry Smith } 141988e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 1420f1e2ffcdSBarry Smith } 142143a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14223a40ed3dSBarry Smith PetscFunctionReturn(0); 142317ab2063SBarry Smith } 142417ab2063SBarry Smith 14254a2ae208SSatish Balay #undef __FUNCT__ 14264a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ" 1427a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 142817ab2063SBarry Smith { 1429416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 143097f1f81fSBarry Smith PetscInt *itmp; 143117ab2063SBarry Smith 14323a40ed3dSBarry Smith PetscFunctionBegin; 1433d0f46423SBarry Smith if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 143417ab2063SBarry Smith 1435416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1436bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 143717ab2063SBarry Smith if (idx) { 1438bfeeae90SHong Zhang itmp = a->j + a->i[row]; 1439bfeeae90SHong Zhang if (*nz) { 14404e093b46SBarry Smith *idx = itmp; 144117ab2063SBarry Smith } 144217ab2063SBarry Smith else *idx = 0; 144317ab2063SBarry Smith } 14443a40ed3dSBarry Smith PetscFunctionReturn(0); 144517ab2063SBarry Smith } 144617ab2063SBarry Smith 1447bfeeae90SHong Zhang /* remove this function? */ 14484a2ae208SSatish Balay #undef __FUNCT__ 14494a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ" 1450a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 145117ab2063SBarry Smith { 14523a40ed3dSBarry Smith PetscFunctionBegin; 14533a40ed3dSBarry Smith PetscFunctionReturn(0); 145417ab2063SBarry Smith } 145517ab2063SBarry Smith 14564a2ae208SSatish Balay #undef __FUNCT__ 14574a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ" 1458dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 145917ab2063SBarry Smith { 1460416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 146154f21887SBarry Smith MatScalar *v = a->a; 146236db0b34SBarry Smith PetscReal sum = 0.0; 14636849ba73SBarry Smith PetscErrorCode ierr; 146497f1f81fSBarry Smith PetscInt i,j; 146517ab2063SBarry Smith 14663a40ed3dSBarry Smith PetscFunctionBegin; 146717ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1468416022c9SBarry Smith for (i=0; i<a->nz; i++) { 1469aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 147036db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 147117ab2063SBarry Smith #else 147217ab2063SBarry Smith sum += (*v)*(*v); v++; 147317ab2063SBarry Smith #endif 147417ab2063SBarry Smith } 1475064f8208SBarry Smith *nrm = sqrt(sum); 14763a40ed3dSBarry Smith } else if (type == NORM_1) { 147736db0b34SBarry Smith PetscReal *tmp; 147897f1f81fSBarry Smith PetscInt *jj = a->j; 1479d0f46423SBarry Smith ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1480d0f46423SBarry Smith ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr); 1481064f8208SBarry Smith *nrm = 0.0; 1482416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1483bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 148417ab2063SBarry Smith } 1485d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 1486064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 148717ab2063SBarry Smith } 1488606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 14893a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1490064f8208SBarry Smith *nrm = 0.0; 1491d0f46423SBarry Smith for (j=0; j<A->rmap->n; j++) { 1492bfeeae90SHong Zhang v = a->a + a->i[j]; 149317ab2063SBarry Smith sum = 0.0; 1494416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1495cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 149617ab2063SBarry Smith } 1497064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 149817ab2063SBarry Smith } 14993a40ed3dSBarry Smith } else { 150029bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"No support for two norm"); 150117ab2063SBarry Smith } 15023a40ed3dSBarry Smith PetscFunctionReturn(0); 150317ab2063SBarry Smith } 150417ab2063SBarry Smith 15054a2ae208SSatish Balay #undef __FUNCT__ 15064a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ" 1507fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B) 150817ab2063SBarry Smith { 1509416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1510416022c9SBarry Smith Mat C; 15116849ba73SBarry Smith PetscErrorCode ierr; 1512d0f46423SBarry Smith PetscInt i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col; 151354f21887SBarry Smith MatScalar *array = a->a; 151417ab2063SBarry Smith 15153a40ed3dSBarry Smith PetscFunctionBegin; 1516d0f46423SBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1517fc4dec0aSBarry Smith 1518fc4dec0aSBarry Smith if (reuse == MAT_INITIAL_MATRIX || *B == A) { 1519d0f46423SBarry Smith ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr); 1520d0f46423SBarry Smith ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr); 1521bfeeae90SHong Zhang 1522bfeeae90SHong Zhang for (i=0; i<ai[m]; i++) col[aj[i]] += 1; 15237adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1524d0f46423SBarry Smith ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr); 15257adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1526ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr); 1527606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 1528a541d17aSBarry Smith } else { 1529a541d17aSBarry Smith C = *B; 1530a541d17aSBarry Smith } 1531a541d17aSBarry Smith 153217ab2063SBarry Smith for (i=0; i<m; i++) { 153317ab2063SBarry Smith len = ai[i+1]-ai[i]; 153487d4246cSBarry Smith ierr = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 1535b9b97703SBarry Smith array += len; 1536b9b97703SBarry Smith aj += len; 153717ab2063SBarry Smith } 15386d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 15396d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 154017ab2063SBarry Smith 1541815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *B != A) { 1542416022c9SBarry Smith *B = C; 154317ab2063SBarry Smith } else { 1544273d9f13SBarry Smith ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 154517ab2063SBarry Smith } 15463a40ed3dSBarry Smith PetscFunctionReturn(0); 154717ab2063SBarry Smith } 154817ab2063SBarry Smith 1549cd0d46ebSvictorle EXTERN_C_BEGIN 1550cd0d46ebSvictorle #undef __FUNCT__ 15515fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ" 1552be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f) 1553cd0d46ebSvictorle { 1554cd0d46ebSvictorle Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 155554f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 155654f21887SBarry Smith MatScalar *va,*vb; 15576849ba73SBarry Smith PetscErrorCode ierr; 155897f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 1559cd0d46ebSvictorle 1560cd0d46ebSvictorle PetscFunctionBegin; 1561cd0d46ebSvictorle bij = (Mat_SeqAIJ *) B->data; 1562cd0d46ebSvictorle 1563cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 1564cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 15655485867bSBarry Smith if (ma!=nb || na!=mb){ 15665485867bSBarry Smith *f = PETSC_FALSE; 15675485867bSBarry Smith PetscFunctionReturn(0); 15685485867bSBarry Smith } 1569cd0d46ebSvictorle aii = aij->i; bii = bij->i; 1570cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 1571cd0d46ebSvictorle va = aij->a; vb = bij->a; 157297f1f81fSBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 157397f1f81fSBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 1574cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 1575cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 1576cd0d46ebSvictorle 1577cd0d46ebSvictorle *f = PETSC_TRUE; 1578cd0d46ebSvictorle for (i=0; i<ma; i++) { 1579cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 158097f1f81fSBarry Smith PetscInt idc,idr; 15815485867bSBarry Smith PetscScalar vc,vr; 1582cd0d46ebSvictorle /* column/row index/value */ 15835485867bSBarry Smith idc = adx[aptr[i]]; 15845485867bSBarry Smith idr = bdx[bptr[idc]]; 15855485867bSBarry Smith vc = va[aptr[i]]; 15865485867bSBarry Smith vr = vb[bptr[idc]]; 15875485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 15885485867bSBarry Smith *f = PETSC_FALSE; 15895485867bSBarry Smith goto done; 1590cd0d46ebSvictorle } else { 15915485867bSBarry Smith aptr[i]++; 15925485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 1593cd0d46ebSvictorle } 1594cd0d46ebSvictorle } 1595cd0d46ebSvictorle } 1596cd0d46ebSvictorle done: 1597cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 15983aeef889SHong Zhang if (B) { 15993aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 16003aeef889SHong Zhang } 1601cd0d46ebSvictorle PetscFunctionReturn(0); 1602cd0d46ebSvictorle } 1603cd0d46ebSvictorle EXTERN_C_END 1604cd0d46ebSvictorle 16051cbb95d3SBarry Smith EXTERN_C_BEGIN 16061cbb95d3SBarry Smith #undef __FUNCT__ 16071cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ" 16081cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f) 16091cbb95d3SBarry Smith { 16101cbb95d3SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 161154f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 161254f21887SBarry Smith MatScalar *va,*vb; 16131cbb95d3SBarry Smith PetscErrorCode ierr; 16141cbb95d3SBarry Smith PetscInt ma,na,mb,nb, i; 16151cbb95d3SBarry Smith 16161cbb95d3SBarry Smith PetscFunctionBegin; 16171cbb95d3SBarry Smith bij = (Mat_SeqAIJ *) B->data; 16181cbb95d3SBarry Smith 16191cbb95d3SBarry Smith ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 16201cbb95d3SBarry Smith ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 16211cbb95d3SBarry Smith if (ma!=nb || na!=mb){ 16221cbb95d3SBarry Smith *f = PETSC_FALSE; 16231cbb95d3SBarry Smith PetscFunctionReturn(0); 16241cbb95d3SBarry Smith } 16251cbb95d3SBarry Smith aii = aij->i; bii = bij->i; 16261cbb95d3SBarry Smith adx = aij->j; bdx = bij->j; 16271cbb95d3SBarry Smith va = aij->a; vb = bij->a; 16281cbb95d3SBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 16291cbb95d3SBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 16301cbb95d3SBarry Smith for (i=0; i<ma; i++) aptr[i] = aii[i]; 16311cbb95d3SBarry Smith for (i=0; i<mb; i++) bptr[i] = bii[i]; 16321cbb95d3SBarry Smith 16331cbb95d3SBarry Smith *f = PETSC_TRUE; 16341cbb95d3SBarry Smith for (i=0; i<ma; i++) { 16351cbb95d3SBarry Smith while (aptr[i]<aii[i+1]) { 16361cbb95d3SBarry Smith PetscInt idc,idr; 16371cbb95d3SBarry Smith PetscScalar vc,vr; 16381cbb95d3SBarry Smith /* column/row index/value */ 16391cbb95d3SBarry Smith idc = adx[aptr[i]]; 16401cbb95d3SBarry Smith idr = bdx[bptr[idc]]; 16411cbb95d3SBarry Smith vc = va[aptr[i]]; 16421cbb95d3SBarry Smith vr = vb[bptr[idc]]; 16431cbb95d3SBarry Smith if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) { 16441cbb95d3SBarry Smith *f = PETSC_FALSE; 16451cbb95d3SBarry Smith goto done; 16461cbb95d3SBarry Smith } else { 16471cbb95d3SBarry Smith aptr[i]++; 16481cbb95d3SBarry Smith if (B || i!=idc) bptr[idc]++; 16491cbb95d3SBarry Smith } 16501cbb95d3SBarry Smith } 16511cbb95d3SBarry Smith } 16521cbb95d3SBarry Smith done: 16531cbb95d3SBarry Smith ierr = PetscFree(aptr);CHKERRQ(ierr); 16541cbb95d3SBarry Smith if (B) { 16551cbb95d3SBarry Smith ierr = PetscFree(bptr);CHKERRQ(ierr); 16561cbb95d3SBarry Smith } 16571cbb95d3SBarry Smith PetscFunctionReturn(0); 16581cbb95d3SBarry Smith } 16591cbb95d3SBarry Smith EXTERN_C_END 16601cbb95d3SBarry Smith 16619e29f15eSvictorle #undef __FUNCT__ 16629e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ" 1663dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f) 16649e29f15eSvictorle { 1665dfbe8321SBarry Smith PetscErrorCode ierr; 16669e29f15eSvictorle PetscFunctionBegin; 16675485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 16689e29f15eSvictorle PetscFunctionReturn(0); 16699e29f15eSvictorle } 16709e29f15eSvictorle 16714a2ae208SSatish Balay #undef __FUNCT__ 16721cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ" 16731cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f) 16741cbb95d3SBarry Smith { 16751cbb95d3SBarry Smith PetscErrorCode ierr; 16761cbb95d3SBarry Smith PetscFunctionBegin; 16771cbb95d3SBarry Smith ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 16781cbb95d3SBarry Smith PetscFunctionReturn(0); 16791cbb95d3SBarry Smith } 16801cbb95d3SBarry Smith 16811cbb95d3SBarry Smith #undef __FUNCT__ 16824a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ" 1683dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 168417ab2063SBarry Smith { 1685416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 168654f21887SBarry Smith PetscScalar *l,*r,x; 168754f21887SBarry Smith MatScalar *v; 1688dfbe8321SBarry Smith PetscErrorCode ierr; 1689d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj; 169017ab2063SBarry Smith 16913a40ed3dSBarry Smith PetscFunctionBegin; 169217ab2063SBarry Smith if (ll) { 16933ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 16943ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1695e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1696d0f46423SBarry Smith if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 16971ebc52fbSHong Zhang ierr = VecGetArray(ll,&l);CHKERRQ(ierr); 1698416022c9SBarry Smith v = a->a; 169917ab2063SBarry Smith for (i=0; i<m; i++) { 170017ab2063SBarry Smith x = l[i]; 1701416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 170217ab2063SBarry Smith for (j=0; j<M; j++) { (*v++) *= x;} 170317ab2063SBarry Smith } 17041ebc52fbSHong Zhang ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr); 1705efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 170617ab2063SBarry Smith } 170717ab2063SBarry Smith if (rr) { 1708e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1709d0f46423SBarry Smith if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 17101ebc52fbSHong Zhang ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1711416022c9SBarry Smith v = a->a; jj = a->j; 171217ab2063SBarry Smith for (i=0; i<nz; i++) { 1713bfeeae90SHong Zhang (*v++) *= r[*jj++]; 171417ab2063SBarry Smith } 17151ebc52fbSHong Zhang ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 1716efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 171717ab2063SBarry Smith } 17183a40ed3dSBarry Smith PetscFunctionReturn(0); 171917ab2063SBarry Smith } 172017ab2063SBarry Smith 17214a2ae208SSatish Balay #undef __FUNCT__ 17224a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ" 172397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 172417ab2063SBarry Smith { 1725db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 17266849ba73SBarry Smith PetscErrorCode ierr; 1727d0f46423SBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens; 172897f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 17295d0c19d7SBarry Smith const PetscInt *irow,*icol; 17305d0c19d7SBarry Smith PetscInt nrows,ncols; 173197f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 173254f21887SBarry Smith MatScalar *a_new,*mat_a; 1733416022c9SBarry Smith Mat C; 173414ca34e6SBarry Smith PetscTruth stride,sorted; 173517ab2063SBarry Smith 17363a40ed3dSBarry Smith PetscFunctionBegin; 173714ca34e6SBarry Smith ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr); 173814ca34e6SBarry Smith if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 173914ca34e6SBarry Smith ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr); 174014ca34e6SBarry Smith if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); 174199141d43SSatish Balay 174217ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 1743b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 1744b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 174517ab2063SBarry Smith 1746fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 1747fee21e36SBarry Smith ierr = ISStride(iscol,&stride);CHKERRQ(ierr); 1748fee21e36SBarry Smith if (stride && step == 1) { 174902834360SBarry Smith /* special case of contiguous rows */ 17500e83c824SBarry Smith ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr); 175102834360SBarry Smith /* loop over new rows determining lens and starting points */ 175202834360SBarry Smith for (i=0; i<nrows; i++) { 1753bfeeae90SHong Zhang kstart = ai[irow[i]]; 1754a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 175502834360SBarry Smith for (k=kstart; k<kend; k++) { 1756bfeeae90SHong Zhang if (aj[k] >= first) { 175702834360SBarry Smith starts[i] = k; 175802834360SBarry Smith break; 175902834360SBarry Smith } 176002834360SBarry Smith } 1761a2744918SBarry Smith sum = 0; 176202834360SBarry Smith while (k < kend) { 1763bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 1764a2744918SBarry Smith sum++; 176502834360SBarry Smith } 1766a2744918SBarry Smith lens[i] = sum; 176702834360SBarry Smith } 176802834360SBarry Smith /* create submatrix */ 1769cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 177097f1f81fSBarry Smith PetscInt n_cols,n_rows; 177108480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 177229bbc08cSBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 1773d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 177408480c60SBarry Smith C = *B; 17753a40ed3dSBarry Smith } else { 17767adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1777f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 17787adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1779ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 178008480c60SBarry Smith } 1781db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 1782db02288aSLois Curfman McInnes 178302834360SBarry Smith /* loop over rows inserting into submatrix */ 1784db02288aSLois Curfman McInnes a_new = c->a; 1785db02288aSLois Curfman McInnes j_new = c->j; 1786db02288aSLois Curfman McInnes i_new = c->i; 1787bfeeae90SHong Zhang 178802834360SBarry Smith for (i=0; i<nrows; i++) { 1789a2744918SBarry Smith ii = starts[i]; 1790a2744918SBarry Smith lensi = lens[i]; 1791a2744918SBarry Smith for (k=0; k<lensi; k++) { 1792a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 179302834360SBarry Smith } 179487828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 1795a2744918SBarry Smith a_new += lensi; 1796a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1797a2744918SBarry Smith c->ilen[i] = lensi; 179802834360SBarry Smith } 17990e83c824SBarry Smith ierr = PetscFree2(lens,starts);CHKERRQ(ierr); 18003a40ed3dSBarry Smith } else { 180102834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 18020e83c824SBarry Smith ierr = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr); 180397f1f81fSBarry Smith ierr = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr); 18040e83c824SBarry Smith ierr = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 180517ab2063SBarry Smith for (i=0; i<ncols; i++) smap[icol[i]] = i+1; 180602834360SBarry Smith /* determine lens of each row */ 180702834360SBarry Smith for (i=0; i<nrows; i++) { 1808bfeeae90SHong Zhang kstart = ai[irow[i]]; 180902834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 181002834360SBarry Smith lens[i] = 0; 181102834360SBarry Smith for (k=kstart; k<kend; k++) { 1812bfeeae90SHong Zhang if (smap[aj[k]]) { 181302834360SBarry Smith lens[i]++; 181402834360SBarry Smith } 181502834360SBarry Smith } 181602834360SBarry Smith } 181717ab2063SBarry Smith /* Create and fill new matrix */ 1818a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 18190f5bd95cSBarry Smith PetscTruth equal; 18200f5bd95cSBarry Smith 182199141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 1822d0f46423SBarry Smith if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 1823d0f46423SBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr); 18240f5bd95cSBarry Smith if (!equal) { 182529bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 182699141d43SSatish Balay } 1827d0f46423SBarry Smith ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 182808480c60SBarry Smith C = *B; 18293a40ed3dSBarry Smith } else { 18307adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1831f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 18327adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1833ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 183408480c60SBarry Smith } 183599141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 183617ab2063SBarry Smith for (i=0; i<nrows; i++) { 183799141d43SSatish Balay row = irow[i]; 1838bfeeae90SHong Zhang kstart = ai[row]; 183999141d43SSatish Balay kend = kstart + a->ilen[row]; 1840bfeeae90SHong Zhang mat_i = c->i[i]; 184199141d43SSatish Balay mat_j = c->j + mat_i; 184299141d43SSatish Balay mat_a = c->a + mat_i; 184399141d43SSatish Balay mat_ilen = c->ilen + i; 184417ab2063SBarry Smith for (k=kstart; k<kend; k++) { 1845bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 1846ed480e8bSBarry Smith *mat_j++ = tcol - 1; 184799141d43SSatish Balay *mat_a++ = a->a[k]; 184899141d43SSatish Balay (*mat_ilen)++; 184999141d43SSatish Balay 185017ab2063SBarry Smith } 185117ab2063SBarry Smith } 185217ab2063SBarry Smith } 185302834360SBarry Smith /* Free work space */ 185402834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 1855606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 1856606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 185702834360SBarry Smith } 18586d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 18596d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 186017ab2063SBarry Smith 186117ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 1862416022c9SBarry Smith *B = C; 18633a40ed3dSBarry Smith PetscFunctionReturn(0); 186417ab2063SBarry Smith } 186517ab2063SBarry Smith 18661df811f5SHong Zhang #undef __FUNCT__ 18674a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ" 18680481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info) 1869a871dcd8SBarry Smith { 187063b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1871dfbe8321SBarry Smith PetscErrorCode ierr; 187263b91edcSBarry Smith Mat outA; 1873b8a78c4aSBarry Smith PetscTruth row_identity,col_identity; 187463b91edcSBarry Smith 18753a40ed3dSBarry Smith PetscFunctionBegin; 1876d3d32019SBarry Smith if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 18771df811f5SHong Zhang 1878b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 1879b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 1880a871dcd8SBarry Smith 188163b91edcSBarry Smith outA = inA; 18821df811f5SHong Zhang outA->factor = MAT_FACTOR_LU; 1883c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 1884c3122656SLisandro Dalcin if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);} 1885c3122656SLisandro Dalcin a->row = row; 1886c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 1887c3122656SLisandro Dalcin if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);} 1888c3122656SLisandro Dalcin a->col = col; 188963b91edcSBarry Smith 189036db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 1891b9b97703SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */ 18924c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 189352e6d16bSBarry Smith ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr); 1894f0ec6fceSSatish Balay 189594a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 1896d0f46423SBarry Smith ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr); 1897d0f46423SBarry Smith ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 189894a9d846SBarry Smith } 189963b91edcSBarry Smith 1900f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 1901137fb511SHong Zhang if (row_identity && col_identity) { 1902ad04f41aSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr); 1903137fb511SHong Zhang } else { 1904719d5645SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr); 1905137fb511SHong Zhang } 19063a40ed3dSBarry Smith PetscFunctionReturn(0); 1907a871dcd8SBarry Smith } 1908a871dcd8SBarry Smith 1909d9eff348SSatish Balay #include "petscblaslapack.h" 19104a2ae208SSatish Balay #undef __FUNCT__ 19114a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ" 1912f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 1913f0b747eeSBarry Smith { 1914f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1915f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 1916efee365bSSatish Balay PetscErrorCode ierr; 19170805154bSBarry Smith PetscBLASInt one = 1,bnz = PetscBLASIntCast(a->nz); 19183a40ed3dSBarry Smith 19193a40ed3dSBarry Smith PetscFunctionBegin; 1920f4df32b1SMatthew Knepley BLASscal_(&bnz,&oalpha,a->a,&one); 1921efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 19223a40ed3dSBarry Smith PetscFunctionReturn(0); 1923f0b747eeSBarry Smith } 1924f0b747eeSBarry Smith 19254a2ae208SSatish Balay #undef __FUNCT__ 19264a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ" 192797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 1928cddf8d76SBarry Smith { 1929dfbe8321SBarry Smith PetscErrorCode ierr; 193097f1f81fSBarry Smith PetscInt i; 1931cddf8d76SBarry Smith 19323a40ed3dSBarry Smith PetscFunctionBegin; 1933cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 1934b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr); 1935cddf8d76SBarry Smith } 1936cddf8d76SBarry Smith 1937cddf8d76SBarry Smith for (i=0; i<n; i++) { 19386a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 1939cddf8d76SBarry Smith } 19403a40ed3dSBarry Smith PetscFunctionReturn(0); 1941cddf8d76SBarry Smith } 1942cddf8d76SBarry Smith 19434a2ae208SSatish Balay #undef __FUNCT__ 19444a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ" 194597f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 19464dcbc457SBarry Smith { 1947e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 19486849ba73SBarry Smith PetscErrorCode ierr; 19495d0c19d7SBarry Smith PetscInt row,i,j,k,l,m,n,*nidx,isz,val; 19505d0c19d7SBarry Smith const PetscInt *idx; 195197f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 1952f1af5d2fSBarry Smith PetscBT table; 1953bbd702dbSSatish Balay 19543a40ed3dSBarry Smith PetscFunctionBegin; 1955d0f46423SBarry Smith m = A->rmap->n; 1956e4d965acSSatish Balay ai = a->i; 1957bfeeae90SHong Zhang aj = a->j; 19588a047759SSatish Balay 1959a45adfd6SMatthew Knepley if (ov < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 196006763907SSatish Balay 196197f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr); 19626831982aSBarry Smith ierr = PetscBTCreate(m,table);CHKERRQ(ierr); 196306763907SSatish Balay 1964e4d965acSSatish Balay for (i=0; i<is_max; i++) { 1965b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1966e4d965acSSatish Balay isz = 0; 19676831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 1968e4d965acSSatish Balay 1969e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 19704dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 1971b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 1972e4d965acSSatish Balay 1973dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1974e4d965acSSatish Balay for (j=0; j<n ; ++j){ 1975f1af5d2fSBarry Smith if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];} 19764dcbc457SBarry Smith } 197706763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 197806763907SSatish Balay ierr = ISDestroy(is[i]);CHKERRQ(ierr); 1979e4d965acSSatish Balay 198004a348a9SBarry Smith k = 0; 198104a348a9SBarry Smith for (j=0; j<ov; j++){ /* for each overlap */ 198204a348a9SBarry Smith n = isz; 198306763907SSatish Balay for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 1984e4d965acSSatish Balay row = nidx[k]; 1985e4d965acSSatish Balay start = ai[row]; 1986e4d965acSSatish Balay end = ai[row+1]; 198704a348a9SBarry Smith for (l = start; l<end ; l++){ 1988efb16452SHong Zhang val = aj[l] ; 1989f1af5d2fSBarry Smith if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;} 1990e4d965acSSatish Balay } 1991e4d965acSSatish Balay } 1992e4d965acSSatish Balay } 1993029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr); 1994e4d965acSSatish Balay } 19956831982aSBarry Smith ierr = PetscBTDestroy(table);CHKERRQ(ierr); 1996606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 19973a40ed3dSBarry Smith PetscFunctionReturn(0); 19984dcbc457SBarry Smith } 199917ab2063SBarry Smith 20000513a670SBarry Smith /* -------------------------------------------------------------- */ 20014a2ae208SSatish Balay #undef __FUNCT__ 20024a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ" 2003dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 20040513a670SBarry Smith { 20050513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 20066849ba73SBarry Smith PetscErrorCode ierr; 20075d0c19d7SBarry Smith PetscInt i,nz,m = A->rmap->n,n = A->cmap->n; 20085d0c19d7SBarry Smith const PetscInt *row,*col; 20095d0c19d7SBarry Smith PetscInt *cnew,j,*lens; 201056cd22aeSBarry Smith IS icolp,irowp; 201197f1f81fSBarry Smith PetscInt *cwork; 201232ec9ce4SBarry Smith PetscScalar *vwork; 20130513a670SBarry Smith 20143a40ed3dSBarry Smith PetscFunctionBegin; 20154c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 201656cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 20174c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 201856cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 20190513a670SBarry Smith 20200513a670SBarry Smith /* determine lengths of permuted rows */ 202197f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 20220513a670SBarry Smith for (i=0; i<m; i++) { 20230513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 20240513a670SBarry Smith } 20257adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 2026f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 20277adad957SLisandro Dalcin ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2028ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 2029606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 20300513a670SBarry Smith 203197f1f81fSBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr); 20320513a670SBarry Smith for (i=0; i<m; i++) { 203332ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 20340513a670SBarry Smith for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];} 2035cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 203632ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 20370513a670SBarry Smith } 2038606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 20393c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 20400513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20410513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 204256cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 204356cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 204456cd22aeSBarry Smith ierr = ISDestroy(irowp);CHKERRQ(ierr); 204556cd22aeSBarry Smith ierr = ISDestroy(icolp);CHKERRQ(ierr); 20463a40ed3dSBarry Smith PetscFunctionReturn(0); 20470513a670SBarry Smith } 20480513a670SBarry Smith 20494a2ae208SSatish Balay #undef __FUNCT__ 20504a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ" 2051dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 2052cb5b572fSBarry Smith { 2053dfbe8321SBarry Smith PetscErrorCode ierr; 2054cb5b572fSBarry Smith 2055cb5b572fSBarry Smith PetscFunctionBegin; 205633f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 205733f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 2058be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2059be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 2060be6bf707SBarry Smith 2061d0f46423SBarry Smith if (a->i[A->rmap->n] != b->i[B->rmap->n]) { 2062634064b4SBarry Smith SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different"); 2063cb5b572fSBarry Smith } 2064d0f46423SBarry Smith ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 2065cb5b572fSBarry Smith } else { 2066cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2067cb5b572fSBarry Smith } 2068cb5b572fSBarry Smith PetscFunctionReturn(0); 2069cb5b572fSBarry Smith } 2070cb5b572fSBarry Smith 20714a2ae208SSatish Balay #undef __FUNCT__ 20724a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ" 2073dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A) 2074273d9f13SBarry Smith { 2075dfbe8321SBarry Smith PetscErrorCode ierr; 2076273d9f13SBarry Smith 2077273d9f13SBarry Smith PetscFunctionBegin; 2078ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 2079273d9f13SBarry Smith PetscFunctionReturn(0); 2080273d9f13SBarry Smith } 2081273d9f13SBarry Smith 20824a2ae208SSatish Balay #undef __FUNCT__ 20834a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ" 2084a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 20856c0721eeSBarry Smith { 20866c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 20876c0721eeSBarry Smith PetscFunctionBegin; 20886c0721eeSBarry Smith *array = a->a; 20896c0721eeSBarry Smith PetscFunctionReturn(0); 20906c0721eeSBarry Smith } 20916c0721eeSBarry Smith 20924a2ae208SSatish Balay #undef __FUNCT__ 20934a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ" 2094dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 20956c0721eeSBarry Smith { 20966c0721eeSBarry Smith PetscFunctionBegin; 20976c0721eeSBarry Smith PetscFunctionReturn(0); 20986c0721eeSBarry Smith } 2099273d9f13SBarry Smith 2100ee4f033dSBarry Smith #undef __FUNCT__ 2101ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ" 2102dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 2103ee4f033dSBarry Smith { 21046849ba73SBarry Smith PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f; 21056849ba73SBarry Smith PetscErrorCode ierr; 210697f1f81fSBarry Smith PetscInt k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2; 2107efb30889SBarry Smith PetscScalar dx,*y,*xx,*w3_array; 210887828ca2SBarry Smith PetscScalar *vscale_array; 2109ee4f033dSBarry Smith PetscReal epsilon = coloring->error_rel,umin = coloring->umin; 2110ee4f033dSBarry Smith Vec w1,w2,w3; 2111ee4f033dSBarry Smith void *fctx = coloring->fctx; 211290d69ab7SBarry Smith PetscTruth flg = PETSC_FALSE; 2113ee4f033dSBarry Smith 2114ee4f033dSBarry Smith PetscFunctionBegin; 2115ee4f033dSBarry Smith if (!coloring->w1) { 2116ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr); 211752e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr); 2118ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr); 211952e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr); 2120ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 212152e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr); 2122ee4f033dSBarry Smith } 2123ee4f033dSBarry Smith w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 2124ee4f033dSBarry Smith 2125ee4f033dSBarry Smith ierr = MatSetUnfactored(J);CHKERRQ(ierr); 212690d69ab7SBarry Smith ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr); 2127ee4f033dSBarry Smith if (flg) { 2128ae15b995SBarry Smith ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr); 2129ee4f033dSBarry Smith } else { 21300b9b6f31SBarry Smith PetscTruth assembled; 21310b9b6f31SBarry Smith ierr = MatAssembled(J,&assembled);CHKERRQ(ierr); 21320b9b6f31SBarry Smith if (assembled) { 2133ee4f033dSBarry Smith ierr = MatZeroEntries(J);CHKERRQ(ierr); 2134ee4f033dSBarry Smith } 21350b9b6f31SBarry Smith } 2136ee4f033dSBarry Smith 2137ee4f033dSBarry Smith ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr); 2138ee4f033dSBarry Smith ierr = VecGetSize(x1,&N);CHKERRQ(ierr); 2139ee4f033dSBarry Smith 2140ee4f033dSBarry Smith /* 2141ee4f033dSBarry Smith This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets 2142ee4f033dSBarry Smith coloring->F for the coarser grids from the finest 2143ee4f033dSBarry Smith */ 2144ee4f033dSBarry Smith if (coloring->F) { 2145ee4f033dSBarry Smith ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr); 2146ee4f033dSBarry Smith ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr); 2147ee4f033dSBarry Smith if (m1 != m2) { 2148ee4f033dSBarry Smith coloring->F = 0; 2149ee4f033dSBarry Smith } 2150ee4f033dSBarry Smith } 2151ee4f033dSBarry Smith 2152ee4f033dSBarry Smith if (coloring->F) { 2153ee4f033dSBarry Smith w1 = coloring->F; 2154ee4f033dSBarry Smith coloring->F = 0; 2155ee4f033dSBarry Smith } else { 215666f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2157ee4f033dSBarry Smith ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 215866f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2159ee4f033dSBarry Smith } 2160ee4f033dSBarry Smith 2161ee4f033dSBarry Smith /* 2162ee4f033dSBarry Smith Compute all the scale factors and share with other processors 2163ee4f033dSBarry Smith */ 21641ebc52fbSHong Zhang ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start; 21651ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start; 2166ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 2167ee4f033dSBarry Smith /* 2168ee4f033dSBarry Smith Loop over each column associated with color adding the 2169ee4f033dSBarry Smith perturbation to the vector w3. 2170ee4f033dSBarry Smith */ 2171ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2172ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2173ee4f033dSBarry Smith dx = xx[col]; 2174ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 2175ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2176ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2177ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2178ee4f033dSBarry Smith #else 2179ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2180ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2181ee4f033dSBarry Smith #endif 2182ee4f033dSBarry Smith dx *= epsilon; 2183ee4f033dSBarry Smith vscale_array[col] = 1.0/dx; 2184ee4f033dSBarry Smith } 2185ee4f033dSBarry Smith } 21861ebc52fbSHong Zhang vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2187ee4f033dSBarry Smith ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2188ee4f033dSBarry Smith ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2189ee4f033dSBarry Smith 2190ee4f033dSBarry Smith /* ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD); 2191ee4f033dSBarry Smith ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/ 2192ee4f033dSBarry Smith 2193ee4f033dSBarry Smith if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow; 2194ee4f033dSBarry Smith else vscaleforrow = coloring->columnsforrow; 2195ee4f033dSBarry Smith 21961ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2197ee4f033dSBarry Smith /* 2198ee4f033dSBarry Smith Loop over each color 2199ee4f033dSBarry Smith */ 2200ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 220149b058dcSBarry Smith coloring->currentcolor = k; 2202ee4f033dSBarry Smith ierr = VecCopy(x1,w3);CHKERRQ(ierr); 22031ebc52fbSHong Zhang ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start; 2204ee4f033dSBarry Smith /* 2205ee4f033dSBarry Smith Loop over each column associated with color adding the 2206ee4f033dSBarry Smith perturbation to the vector w3. 2207ee4f033dSBarry Smith */ 2208ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2209ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2210ee4f033dSBarry Smith dx = xx[col]; 22115b8514ebSBarry Smith if (dx == 0.0) dx = 1.0; 2212ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2213ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2214ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2215ee4f033dSBarry Smith #else 2216ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2217ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2218ee4f033dSBarry Smith #endif 2219ee4f033dSBarry Smith dx *= epsilon; 2220634064b4SBarry Smith if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter"); 2221ee4f033dSBarry Smith w3_array[col] += dx; 2222ee4f033dSBarry Smith } 22231ebc52fbSHong Zhang w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 2224ee4f033dSBarry Smith 2225ee4f033dSBarry Smith /* 2226ee4f033dSBarry Smith Evaluate function at x1 + dx (here dx is a vector of perturbations) 2227ee4f033dSBarry Smith */ 2228ee4f033dSBarry Smith 222966f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2230ee4f033dSBarry Smith ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 223166f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2232efb30889SBarry Smith ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 2233ee4f033dSBarry Smith 2234ee4f033dSBarry Smith /* 2235ee4f033dSBarry Smith Loop over rows of vector, putting results into Jacobian matrix 2236ee4f033dSBarry Smith */ 22371ebc52fbSHong Zhang ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 2238ee4f033dSBarry Smith for (l=0; l<coloring->nrows[k]; l++) { 2239ee4f033dSBarry Smith row = coloring->rows[k][l]; 2240ee4f033dSBarry Smith col = coloring->columnsforrow[k][l]; 2241ee4f033dSBarry Smith y[row] *= vscale_array[vscaleforrow[k][l]]; 2242ee4f033dSBarry Smith srow = row + start; 2243ee4f033dSBarry Smith ierr = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 2244ee4f033dSBarry Smith } 22451ebc52fbSHong Zhang ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 2246ee4f033dSBarry Smith } 224749b058dcSBarry Smith coloring->currentcolor = k; 22481ebc52fbSHong Zhang ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 22491ebc52fbSHong Zhang xx = xx + start; ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 2250ee4f033dSBarry Smith ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2251ee4f033dSBarry Smith ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2252ee4f033dSBarry Smith PetscFunctionReturn(0); 2253ee4f033dSBarry Smith } 2254ee4f033dSBarry Smith 2255ac90fabeSBarry Smith #include "petscblaslapack.h" 2256ac90fabeSBarry Smith #undef __FUNCT__ 2257ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ" 2258f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2259ac90fabeSBarry Smith { 2260dfbe8321SBarry Smith PetscErrorCode ierr; 226197f1f81fSBarry Smith PetscInt i; 2262ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data; 22630805154bSBarry Smith PetscBLASInt one=1,bnz = PetscBLASIntCast(x->nz); 2264ac90fabeSBarry Smith 2265ac90fabeSBarry Smith PetscFunctionBegin; 2266ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2267f4df32b1SMatthew Knepley PetscScalar alpha = a; 2268f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2269c537a176SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 2270a30b2313SHong Zhang if (y->xtoy && y->XtoY != X) { 2271a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 2272a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 2273a30b2313SHong Zhang } 2274a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2275d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr); 2276a30b2313SHong Zhang y->XtoY = X; 2277407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr); 2278c537a176SHong Zhang } 2279f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 22801e2582c4SBarry 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); 2281ac90fabeSBarry Smith } else { 2282f4df32b1SMatthew Knepley ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 2283ac90fabeSBarry Smith } 2284ac90fabeSBarry Smith PetscFunctionReturn(0); 2285ac90fabeSBarry Smith } 2286ac90fabeSBarry Smith 2287521d7252SBarry Smith #undef __FUNCT__ 2288521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ" 2289521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs) 2290521d7252SBarry Smith { 229141c166b1SJed Brown PetscErrorCode ierr; 229241c166b1SJed Brown 2293521d7252SBarry Smith PetscFunctionBegin; 229441c166b1SJed Brown ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr); 229541c166b1SJed Brown ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr); 2296521d7252SBarry Smith PetscFunctionReturn(0); 2297521d7252SBarry Smith } 2298521d7252SBarry Smith 2299354c94deSBarry Smith #undef __FUNCT__ 2300354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ" 2301354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat) 2302354c94deSBarry Smith { 2303354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2304354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2305354c94deSBarry Smith PetscInt i,nz; 2306354c94deSBarry Smith PetscScalar *a; 2307354c94deSBarry Smith 2308354c94deSBarry Smith PetscFunctionBegin; 2309354c94deSBarry Smith nz = aij->nz; 2310354c94deSBarry Smith a = aij->a; 2311354c94deSBarry Smith for (i=0; i<nz; i++) { 2312354c94deSBarry Smith a[i] = PetscConj(a[i]); 2313354c94deSBarry Smith } 2314354c94deSBarry Smith #else 2315354c94deSBarry Smith PetscFunctionBegin; 2316354c94deSBarry Smith #endif 2317354c94deSBarry Smith PetscFunctionReturn(0); 2318354c94deSBarry Smith } 2319354c94deSBarry Smith 2320e34fafa9SBarry Smith #undef __FUNCT__ 2321985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ" 2322985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2323e34fafa9SBarry Smith { 2324e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2325e34fafa9SBarry Smith PetscErrorCode ierr; 2326d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2327e34fafa9SBarry Smith PetscReal atmp; 2328985db425SBarry Smith PetscScalar *x; 2329e34fafa9SBarry Smith MatScalar *aa; 2330e34fafa9SBarry Smith 2331e34fafa9SBarry Smith PetscFunctionBegin; 2332e34fafa9SBarry Smith if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2333e34fafa9SBarry Smith aa = a->a; 2334e34fafa9SBarry Smith ai = a->i; 2335e34fafa9SBarry Smith aj = a->j; 2336e34fafa9SBarry Smith 2337985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2338e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2339e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2340d0f46423SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2341e34fafa9SBarry Smith for (i=0; i<m; i++) { 2342e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 23439189402eSHong Zhang x[i] = 0.0; 2344e34fafa9SBarry Smith for (j=0; j<ncols; j++){ 2345985db425SBarry Smith atmp = PetscAbsScalar(*aa); 2346985db425SBarry Smith if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2347985db425SBarry Smith aa++; aj++; 2348985db425SBarry Smith } 2349985db425SBarry Smith } 2350985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2351985db425SBarry Smith PetscFunctionReturn(0); 2352985db425SBarry Smith } 2353985db425SBarry Smith 2354985db425SBarry Smith #undef __FUNCT__ 2355985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ" 2356985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2357985db425SBarry Smith { 2358985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2359985db425SBarry Smith PetscErrorCode ierr; 2360d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2361985db425SBarry Smith PetscScalar *x; 2362985db425SBarry Smith MatScalar *aa; 2363985db425SBarry Smith 2364985db425SBarry Smith PetscFunctionBegin; 2365985db425SBarry Smith if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2366985db425SBarry Smith aa = a->a; 2367985db425SBarry Smith ai = a->i; 2368985db425SBarry Smith aj = a->j; 2369985db425SBarry Smith 2370985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2371985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2372985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2373d0f46423SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2374985db425SBarry Smith for (i=0; i<m; i++) { 2375985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2376d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2377985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2378985db425SBarry Smith } else { /* row is sparse so already KNOW maximum is 0.0 or higher */ 2379985db425SBarry Smith x[i] = 0.0; 2380985db425SBarry Smith if (idx) { 2381985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2382985db425SBarry Smith for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */ 2383985db425SBarry Smith if (aj[j] > j) { 2384985db425SBarry Smith idx[i] = j; 2385985db425SBarry Smith break; 2386985db425SBarry Smith } 2387985db425SBarry Smith } 2388985db425SBarry Smith } 2389985db425SBarry Smith } 2390985db425SBarry Smith for (j=0; j<ncols; j++){ 2391985db425SBarry Smith if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2392985db425SBarry Smith aa++; aj++; 2393985db425SBarry Smith } 2394985db425SBarry Smith } 2395985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2396985db425SBarry Smith PetscFunctionReturn(0); 2397985db425SBarry Smith } 2398985db425SBarry Smith 2399985db425SBarry Smith #undef __FUNCT__ 2400c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ" 2401c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2402c87e5d42SMatthew Knepley { 2403c87e5d42SMatthew Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2404c87e5d42SMatthew Knepley PetscErrorCode ierr; 2405c87e5d42SMatthew Knepley PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2406c87e5d42SMatthew Knepley PetscReal atmp; 2407c87e5d42SMatthew Knepley PetscScalar *x; 2408c87e5d42SMatthew Knepley MatScalar *aa; 2409c87e5d42SMatthew Knepley 2410c87e5d42SMatthew Knepley PetscFunctionBegin; 2411c87e5d42SMatthew Knepley if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2412c87e5d42SMatthew Knepley aa = a->a; 2413c87e5d42SMatthew Knepley ai = a->i; 2414c87e5d42SMatthew Knepley aj = a->j; 2415c87e5d42SMatthew Knepley 2416c87e5d42SMatthew Knepley ierr = VecSet(v,0.0);CHKERRQ(ierr); 2417c87e5d42SMatthew Knepley ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2418c87e5d42SMatthew Knepley ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2419c87e5d42SMatthew Knepley if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2420c87e5d42SMatthew Knepley for (i=0; i<m; i++) { 2421c87e5d42SMatthew Knepley ncols = ai[1] - ai[0]; ai++; 2422289a08f5SMatthew Knepley if (ncols) { 2423289a08f5SMatthew Knepley /* Get first nonzero */ 2424289a08f5SMatthew Knepley for(j = 0; j < ncols; j++) { 2425289a08f5SMatthew Knepley atmp = PetscAbsScalar(aa[j]); 2426289a08f5SMatthew Knepley if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;} 2427289a08f5SMatthew Knepley } 2428289a08f5SMatthew Knepley if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;} 2429289a08f5SMatthew Knepley } else { 2430289a08f5SMatthew Knepley x[i] = 0.0; if (idx) idx[i] = 0; 2431289a08f5SMatthew Knepley } 2432c87e5d42SMatthew Knepley for(j = 0; j < ncols; j++) { 2433c87e5d42SMatthew Knepley atmp = PetscAbsScalar(*aa); 2434289a08f5SMatthew Knepley if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2435c87e5d42SMatthew Knepley aa++; aj++; 2436c87e5d42SMatthew Knepley } 2437c87e5d42SMatthew Knepley } 2438c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2439c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2440c87e5d42SMatthew Knepley } 2441c87e5d42SMatthew Knepley 2442c87e5d42SMatthew Knepley #undef __FUNCT__ 2443985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ" 2444985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2445985db425SBarry Smith { 2446985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2447985db425SBarry Smith PetscErrorCode ierr; 2448d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2449985db425SBarry Smith PetscScalar *x; 2450985db425SBarry Smith MatScalar *aa; 2451985db425SBarry Smith 2452985db425SBarry Smith PetscFunctionBegin; 2453985db425SBarry Smith if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2454985db425SBarry Smith aa = a->a; 2455985db425SBarry Smith ai = a->i; 2456985db425SBarry Smith aj = a->j; 2457985db425SBarry Smith 2458985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2459985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2460985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2461d0f46423SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2462985db425SBarry Smith for (i=0; i<m; i++) { 2463985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2464d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2465985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2466985db425SBarry Smith } else { /* row is sparse so already KNOW minimum is 0.0 or lower */ 2467985db425SBarry Smith x[i] = 0.0; 2468985db425SBarry Smith if (idx) { /* find first implicit 0.0 in the row */ 2469985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2470985db425SBarry Smith for (j=0;j<ncols;j++) { 2471985db425SBarry Smith if (aj[j] > j) { 2472985db425SBarry Smith idx[i] = j; 2473985db425SBarry Smith break; 2474985db425SBarry Smith } 2475985db425SBarry Smith } 2476985db425SBarry Smith } 2477985db425SBarry Smith } 2478985db425SBarry Smith for (j=0; j<ncols; j++){ 2479985db425SBarry Smith if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2480985db425SBarry Smith aa++; aj++; 2481e34fafa9SBarry Smith } 2482e34fafa9SBarry Smith } 2483e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2484e34fafa9SBarry Smith PetscFunctionReturn(0); 2485e34fafa9SBarry Smith } 24863acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 2487682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 24880a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ, 2489cb5b572fSBarry Smith MatGetRow_SeqAIJ, 2490cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 2491cb5b572fSBarry Smith MatMult_SeqAIJ, 249297304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 24937c922b88SBarry Smith MatMultTranspose_SeqAIJ, 24947c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 2495db4efbfdSBarry Smith 0, 2496db4efbfdSBarry Smith 0, 2497db4efbfdSBarry Smith 0, 2498db4efbfdSBarry Smith /*10*/ 0, 2499cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 2500cb5b572fSBarry Smith 0, 250141f059aeSBarry Smith MatSOR_SeqAIJ, 250217ab2063SBarry Smith MatTranspose_SeqAIJ, 250397304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ, 2504cb5b572fSBarry Smith MatEqual_SeqAIJ, 2505cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 2506cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 2507cb5b572fSBarry Smith MatNorm_SeqAIJ, 250897304618SKris Buschelman /*20*/ 0, 2509cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 2510cb5b572fSBarry Smith MatSetOption_SeqAIJ, 2511cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 2512d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ, 2513db4efbfdSBarry Smith 0, 2514db4efbfdSBarry Smith 0, 2515db4efbfdSBarry Smith 0, 2516db4efbfdSBarry Smith 0, 2517d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ, 2518db4efbfdSBarry Smith 0, 2519db4efbfdSBarry Smith 0, 25206c0721eeSBarry Smith MatGetArray_SeqAIJ, 25216c0721eeSBarry Smith MatRestoreArray_SeqAIJ, 2522d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ, 2523cb5b572fSBarry Smith 0, 2524cb5b572fSBarry Smith 0, 2525cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 2526cb5b572fSBarry Smith 0, 2527d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ, 2528cb5b572fSBarry Smith MatGetSubMatrices_SeqAIJ, 2529cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 2530cb5b572fSBarry Smith MatGetValues_SeqAIJ, 2531cb5b572fSBarry Smith MatCopy_SeqAIJ, 2532d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ, 2533cb5b572fSBarry Smith MatScale_SeqAIJ, 2534cb5b572fSBarry Smith 0, 253579299369SBarry Smith MatDiagonalSet_SeqAIJ, 25366945ee14SBarry Smith MatILUDTFactor_SeqAIJ, 2537d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ, 25383b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 25393b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 25403b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 2541a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 2542d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ, 2543b9617806SBarry Smith 0, 25440513a670SBarry Smith 0, 2545cda55fadSBarry Smith MatPermute_SeqAIJ, 2546cda55fadSBarry Smith 0, 2547d519adbfSMatthew Knepley /*59*/ 0, 2548b9b97703SBarry Smith MatDestroy_SeqAIJ, 2549b9b97703SBarry Smith MatView_SeqAIJ, 2550357abbc8SBarry Smith 0, 2551ee4f033dSBarry Smith 0, 2552d519adbfSMatthew Knepley /*64*/ 0, 2553ee4f033dSBarry Smith 0, 2554ee4f033dSBarry Smith 0, 2555ee4f033dSBarry Smith 0, 2556ee4f033dSBarry Smith 0, 2557d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ, 2558c87e5d42SMatthew Knepley MatGetRowMinAbs_SeqAIJ, 2559ee4f033dSBarry Smith 0, 2560ee4f033dSBarry Smith MatSetColoring_SeqAIJ, 2561dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2562ee4f033dSBarry Smith MatSetValuesAdic_SeqAIJ, 2563dcf5cc72SBarry Smith #else 2564dcf5cc72SBarry Smith 0, 2565dcf5cc72SBarry Smith #endif 2566d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ, 25673acb8795SBarry Smith MatFDColoringApply_AIJ, 256897304618SKris Buschelman 0, 256997304618SKris Buschelman 0, 257097304618SKris Buschelman 0, 2571d519adbfSMatthew Knepley /*79*/ 0, 257297304618SKris Buschelman 0, 257397304618SKris Buschelman 0, 257497304618SKris Buschelman 0, 2575bc011b1eSHong Zhang MatLoad_SeqAIJ, 2576d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ, 25771cbb95d3SBarry Smith MatIsHermitian_SeqAIJ, 25786284ec50SHong Zhang 0, 25796284ec50SHong Zhang 0, 2580bc011b1eSHong Zhang 0, 2581d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ, 258226be0446SHong Zhang MatMatMultSymbolic_SeqAIJ_SeqAIJ, 258326be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 2584d439da42SKris Buschelman MatPtAP_Basic, 25857ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ, 2586d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ, 2587bc011b1eSHong Zhang MatMatMultTranspose_SeqAIJ_SeqAIJ, 2588bc011b1eSHong Zhang MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ, 2589bc011b1eSHong Zhang MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ, 25907ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ_SeqAIJ, 2591d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ, 2592609c6c4dSKris Buschelman 0, 2593609c6c4dSKris Buschelman 0, 259487d4246cSBarry Smith MatConjugate_SeqAIJ, 259587d4246cSBarry Smith 0, 2596d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ, 259799cafbc1SBarry Smith MatRealPart_SeqAIJ, 2598f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 2599f5edf698SHong Zhang 0, 26002bebee5dSHong Zhang 0, 2601d519adbfSMatthew Knepley /*109*/0, 2602985db425SBarry Smith 0, 26032af78befSBarry Smith MatGetRowMin_SeqAIJ, 26042af78befSBarry Smith 0, 2605599ef60dSHong Zhang MatMissingDiagonal_SeqAIJ, 2606d519adbfSMatthew Knepley /*114*/0, 2607599ef60dSHong Zhang 0, 26083c2a7987SHong Zhang 0, 26093c2a7987SHong Zhang MatILUDTFactorSymbolic_SeqAIJ, 26103c2a7987SHong Zhang MatILUDTFactorNumeric_SeqAIJ 26119e29f15eSvictorle }; 261217ab2063SBarry Smith 2613fb2e594dSBarry Smith EXTERN_C_BEGIN 26144a2ae208SSatish Balay #undef __FUNCT__ 26154a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ" 2616be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 2617bef8e0ddSBarry Smith { 2618bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 261997f1f81fSBarry Smith PetscInt i,nz,n; 2620bef8e0ddSBarry Smith 2621bef8e0ddSBarry Smith PetscFunctionBegin; 2622bef8e0ddSBarry Smith 2623bef8e0ddSBarry Smith nz = aij->maxnz; 2624d0f46423SBarry Smith n = mat->rmap->n; 2625bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 2626bef8e0ddSBarry Smith aij->j[i] = indices[i]; 2627bef8e0ddSBarry Smith } 2628bef8e0ddSBarry Smith aij->nz = nz; 2629bef8e0ddSBarry Smith for (i=0; i<n; i++) { 2630bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 2631bef8e0ddSBarry Smith } 2632bef8e0ddSBarry Smith 2633bef8e0ddSBarry Smith PetscFunctionReturn(0); 2634bef8e0ddSBarry Smith } 2635fb2e594dSBarry Smith EXTERN_C_END 2636bef8e0ddSBarry Smith 26374a2ae208SSatish Balay #undef __FUNCT__ 26384a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices" 2639bef8e0ddSBarry Smith /*@ 2640bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 2641bef8e0ddSBarry Smith in the matrix. 2642bef8e0ddSBarry Smith 2643bef8e0ddSBarry Smith Input Parameters: 2644bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 2645bef8e0ddSBarry Smith - indices - the column indices 2646bef8e0ddSBarry Smith 264715091d37SBarry Smith Level: advanced 264815091d37SBarry Smith 2649bef8e0ddSBarry Smith Notes: 2650bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 2651bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 2652bef8e0ddSBarry Smith of the MatSetValues() operation. 2653bef8e0ddSBarry Smith 2654bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 2655d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 2656bef8e0ddSBarry Smith 2657bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 2658bef8e0ddSBarry Smith 2659b9617806SBarry Smith The indices should start with zero, not one. 2660b9617806SBarry Smith 2661bef8e0ddSBarry Smith @*/ 2662be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 2663bef8e0ddSBarry Smith { 266497f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt *); 2665bef8e0ddSBarry Smith 2666bef8e0ddSBarry Smith PetscFunctionBegin; 26674482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 26684482741eSBarry Smith PetscValidPointer(indices,2); 2669c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr); 2670bef8e0ddSBarry Smith if (f) { 2671bef8e0ddSBarry Smith ierr = (*f)(mat,indices);CHKERRQ(ierr); 2672bef8e0ddSBarry Smith } else { 2673634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices"); 2674bef8e0ddSBarry Smith } 2675bef8e0ddSBarry Smith PetscFunctionReturn(0); 2676bef8e0ddSBarry Smith } 2677bef8e0ddSBarry Smith 2678be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 2679be6bf707SBarry Smith 2680fb2e594dSBarry Smith EXTERN_C_BEGIN 26814a2ae208SSatish Balay #undef __FUNCT__ 26824a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ" 2683be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat) 2684be6bf707SBarry Smith { 2685be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 26866849ba73SBarry Smith PetscErrorCode ierr; 2687d0f46423SBarry Smith size_t nz = aij->i[mat->rmap->n]; 2688be6bf707SBarry Smith 2689be6bf707SBarry Smith PetscFunctionBegin; 2690be6bf707SBarry Smith if (aij->nonew != 1) { 2691512a5fc5SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 2692be6bf707SBarry Smith } 2693be6bf707SBarry Smith 2694be6bf707SBarry Smith /* allocate space for values if not already there */ 2695be6bf707SBarry Smith if (!aij->saved_values) { 269687828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr); 26979518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr); 2698be6bf707SBarry Smith } 2699be6bf707SBarry Smith 2700be6bf707SBarry Smith /* copy values over */ 270187828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2702be6bf707SBarry Smith PetscFunctionReturn(0); 2703be6bf707SBarry Smith } 2704fb2e594dSBarry Smith EXTERN_C_END 2705be6bf707SBarry Smith 27064a2ae208SSatish Balay #undef __FUNCT__ 2707b9617806SBarry Smith #define __FUNCT__ "MatStoreValues" 2708be6bf707SBarry Smith /*@ 2709be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 2710be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2711be6bf707SBarry Smith nonlinear portion. 2712be6bf707SBarry Smith 2713be6bf707SBarry Smith Collect on Mat 2714be6bf707SBarry Smith 2715be6bf707SBarry Smith Input Parameters: 27160e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 2717be6bf707SBarry Smith 271815091d37SBarry Smith Level: advanced 271915091d37SBarry Smith 2720be6bf707SBarry Smith Common Usage, with SNESSolve(): 2721be6bf707SBarry Smith $ Create Jacobian matrix 2722be6bf707SBarry Smith $ Set linear terms into matrix 2723be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 2724be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 2725be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 2726512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 2727be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2728be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 2729be6bf707SBarry Smith $ In your Jacobian routine 2730be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2731be6bf707SBarry Smith $ Set nonlinear terms in matrix 2732be6bf707SBarry Smith 2733be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 2734be6bf707SBarry Smith $ // build linear portion of Jacobian 2735512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 2736be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2737be6bf707SBarry Smith $ loop over nonlinear iterations 2738be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2739be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 2740be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 2741be6bf707SBarry Smith $ Solve linear system with Jacobian 2742be6bf707SBarry Smith $ endloop 2743be6bf707SBarry Smith 2744be6bf707SBarry Smith Notes: 2745be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 2746512a5fc5SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before 2747be6bf707SBarry Smith calling this routine. 2748be6bf707SBarry Smith 27490c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 27500c468ba9SBarry Smith and does not allocated additional space. 27510c468ba9SBarry Smith 2752be6bf707SBarry Smith .seealso: MatRetrieveValues() 2753be6bf707SBarry Smith 2754be6bf707SBarry Smith @*/ 2755be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat) 2756be6bf707SBarry Smith { 2757dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2758be6bf707SBarry Smith 2759be6bf707SBarry Smith PetscFunctionBegin; 27604482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 276129bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 276229bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2763be6bf707SBarry Smith 2764c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2765be6bf707SBarry Smith if (f) { 2766be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2767be6bf707SBarry Smith } else { 2768634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values"); 2769be6bf707SBarry Smith } 2770be6bf707SBarry Smith PetscFunctionReturn(0); 2771be6bf707SBarry Smith } 2772be6bf707SBarry Smith 2773fb2e594dSBarry Smith EXTERN_C_BEGIN 27744a2ae208SSatish Balay #undef __FUNCT__ 27754a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ" 2776be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat) 2777be6bf707SBarry Smith { 2778be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 27796849ba73SBarry Smith PetscErrorCode ierr; 2780d0f46423SBarry Smith PetscInt nz = aij->i[mat->rmap->n]; 2781be6bf707SBarry Smith 2782be6bf707SBarry Smith PetscFunctionBegin; 2783be6bf707SBarry Smith if (aij->nonew != 1) { 2784512a5fc5SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 2785be6bf707SBarry Smith } 2786be6bf707SBarry Smith if (!aij->saved_values) { 2787634064b4SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 2788be6bf707SBarry Smith } 2789be6bf707SBarry Smith /* copy values over */ 279087828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2791be6bf707SBarry Smith PetscFunctionReturn(0); 2792be6bf707SBarry Smith } 2793fb2e594dSBarry Smith EXTERN_C_END 2794be6bf707SBarry Smith 27954a2ae208SSatish Balay #undef __FUNCT__ 27964a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues" 2797be6bf707SBarry Smith /*@ 2798be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 2799be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2800be6bf707SBarry Smith nonlinear portion. 2801be6bf707SBarry Smith 2802be6bf707SBarry Smith Collect on Mat 2803be6bf707SBarry Smith 2804be6bf707SBarry Smith Input Parameters: 2805be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2806be6bf707SBarry Smith 280715091d37SBarry Smith Level: advanced 280815091d37SBarry Smith 2809be6bf707SBarry Smith .seealso: MatStoreValues() 2810be6bf707SBarry Smith 2811be6bf707SBarry Smith @*/ 2812be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat) 2813be6bf707SBarry Smith { 2814dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2815be6bf707SBarry Smith 2816be6bf707SBarry Smith PetscFunctionBegin; 28174482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 281829bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 281929bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2820be6bf707SBarry Smith 2821c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2822be6bf707SBarry Smith if (f) { 2823be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2824be6bf707SBarry Smith } else { 2825634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values"); 2826be6bf707SBarry Smith } 2827be6bf707SBarry Smith PetscFunctionReturn(0); 2828be6bf707SBarry Smith } 2829be6bf707SBarry Smith 2830f83d6046SBarry Smith 2831be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 28324a2ae208SSatish Balay #undef __FUNCT__ 28334a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ" 283417ab2063SBarry Smith /*@C 2835682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 28360d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 28376e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 283851c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 28392bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 284017ab2063SBarry Smith 2841db81eaa0SLois Curfman McInnes Collective on MPI_Comm 2842db81eaa0SLois Curfman McInnes 284317ab2063SBarry Smith Input Parameters: 2844db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 284517ab2063SBarry Smith . m - number of rows 284617ab2063SBarry Smith . n - number of columns 284717ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 284851c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 28492bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 285017ab2063SBarry Smith 285117ab2063SBarry Smith Output Parameter: 2852416022c9SBarry Smith . A - the matrix 285317ab2063SBarry Smith 2854175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 2855ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 2856175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 2857175b88e8SBarry Smith 2858b259b22eSLois Curfman McInnes Notes: 285949a6f317SBarry Smith If nnz is given then nz is ignored 286049a6f317SBarry Smith 286117ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 286217ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 28630002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 286444cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 286517ab2063SBarry Smith 286617ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2867a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 28683d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 28696da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 287017ab2063SBarry Smith 2871682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 28724fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 2873682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 28746c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 28756c7ebb05SLois Curfman McInnes 28766c7ebb05SLois Curfman McInnes Options Database Keys: 2877698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2878698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2879db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 2880db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 2881db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 288217ab2063SBarry Smith 2883027ccd11SLois Curfman McInnes Level: intermediate 2884027ccd11SLois Curfman McInnes 288536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 288636db0b34SBarry Smith 288717ab2063SBarry Smith @*/ 2888be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 288917ab2063SBarry Smith { 2890dfbe8321SBarry Smith PetscErrorCode ierr; 28916945ee14SBarry Smith 28923a40ed3dSBarry Smith PetscFunctionBegin; 2893f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 2894117016b1SBarry Smith ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 2895c4752a88SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 2896ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 2897273d9f13SBarry Smith PetscFunctionReturn(0); 2898273d9f13SBarry Smith } 2899273d9f13SBarry Smith 29004a2ae208SSatish Balay #undef __FUNCT__ 29014a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation" 2902273d9f13SBarry Smith /*@C 2903273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 2904273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 2905273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 2906273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 2907273d9f13SBarry Smith 2908273d9f13SBarry Smith Collective on MPI_Comm 2909273d9f13SBarry Smith 2910273d9f13SBarry Smith Input Parameters: 2911117016b1SBarry Smith + B - The matrix-free 2912273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 2913273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 2914273d9f13SBarry Smith (possibly different for each row) or PETSC_NULL 2915273d9f13SBarry Smith 2916273d9f13SBarry Smith Notes: 291749a6f317SBarry Smith If nnz is given then nz is ignored 291849a6f317SBarry Smith 2919273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2920273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2921273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2922273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 2923273d9f13SBarry Smith 2924273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2925273d9f13SBarry Smith Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 2926273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 2927273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 2928273d9f13SBarry Smith 2929aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 2930aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 2931aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 2932aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 2933aa95bbe8SBarry Smith 2934a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 2935a96a251dSBarry Smith entries or columns indices 2936a96a251dSBarry Smith 2937273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 2938273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 2939273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 2940273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2941273d9f13SBarry Smith 2942273d9f13SBarry Smith Options Database Keys: 2943698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2944698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2945273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2946273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2947273d9f13SBarry Smith the user still MUST index entries starting at 0! 2948273d9f13SBarry Smith 2949273d9f13SBarry Smith Level: intermediate 2950273d9f13SBarry Smith 2951aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo() 2952273d9f13SBarry Smith 2953273d9f13SBarry Smith @*/ 2954be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 2955273d9f13SBarry Smith { 295697f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]); 2957a23d5eceSKris Buschelman 2958a23d5eceSKris Buschelman PetscFunctionBegin; 2959a23d5eceSKris Buschelman ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr); 2960a23d5eceSKris Buschelman if (f) { 2961a23d5eceSKris Buschelman ierr = (*f)(B,nz,nnz);CHKERRQ(ierr); 2962a23d5eceSKris Buschelman } 2963a23d5eceSKris Buschelman PetscFunctionReturn(0); 2964a23d5eceSKris Buschelman } 2965a23d5eceSKris Buschelman 2966a23d5eceSKris Buschelman EXTERN_C_BEGIN 2967a23d5eceSKris Buschelman #undef __FUNCT__ 2968a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ" 2969be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz) 2970a23d5eceSKris Buschelman { 2971273d9f13SBarry Smith Mat_SeqAIJ *b; 2972a43ee2ecSKris Buschelman PetscTruth skipallocation = PETSC_FALSE; 29736849ba73SBarry Smith PetscErrorCode ierr; 297497f1f81fSBarry Smith PetscInt i; 2975273d9f13SBarry Smith 2976273d9f13SBarry Smith PetscFunctionBegin; 2977d5d45c9bSBarry Smith 2978a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 2979c461c341SBarry Smith skipallocation = PETSC_TRUE; 2980c461c341SBarry Smith nz = 0; 2981c461c341SBarry Smith } 2982c461c341SBarry Smith 298326283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 298426283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 298526283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 298626283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 2987899cda47SBarry Smith 2988435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 2989435da068SBarry Smith if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz); 2990b73539f3SBarry Smith if (nnz) { 2991d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 299229bbc08cSBarry Smith if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]); 2993d0f46423SBarry Smith if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n); 2994b73539f3SBarry Smith } 2995b73539f3SBarry Smith } 2996b73539f3SBarry Smith 2997273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 2998273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 2999273d9f13SBarry Smith 3000ab93d7beSBarry Smith if (!skipallocation) { 30012ee49352SLisandro Dalcin if (!b->imax) { 3002d0f46423SBarry Smith ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr); 3003d0f46423SBarry Smith ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 30042ee49352SLisandro Dalcin } 3005273d9f13SBarry Smith if (!nnz) { 3006435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 3007273d9f13SBarry Smith else if (nz <= 0) nz = 1; 3008d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) b->imax[i] = nz; 3009d0f46423SBarry Smith nz = nz*B->rmap->n; 3010273d9f13SBarry Smith } else { 3011273d9f13SBarry Smith nz = 0; 3012d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 3013273d9f13SBarry Smith } 3014ab93d7beSBarry Smith /* b->ilen will count nonzeros in each row so far. */ 3015d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; } 3016ab93d7beSBarry Smith 3017273d9f13SBarry Smith /* allocate the matrix space */ 30182ee49352SLisandro Dalcin ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr); 3019d0f46423SBarry Smith ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr); 3020d0f46423SBarry Smith ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr); 3021bfeeae90SHong Zhang b->i[0] = 0; 3022d0f46423SBarry Smith for (i=1; i<B->rmap->n+1; i++) { 30235da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 30245da197adSKris Buschelman } 3025273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 3026e6b907acSBarry Smith b->free_a = PETSC_TRUE; 3027e6b907acSBarry Smith b->free_ij = PETSC_TRUE; 3028c461c341SBarry Smith } else { 3029e6b907acSBarry Smith b->free_a = PETSC_FALSE; 3030e6b907acSBarry Smith b->free_ij = PETSC_FALSE; 3031c461c341SBarry Smith } 3032273d9f13SBarry Smith 3033273d9f13SBarry Smith b->nz = 0; 3034273d9f13SBarry Smith b->maxnz = nz; 3035273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 3036273d9f13SBarry Smith PetscFunctionReturn(0); 3037273d9f13SBarry Smith } 3038a23d5eceSKris Buschelman EXTERN_C_END 3039273d9f13SBarry Smith 3040a1661176SMatthew Knepley #undef __FUNCT__ 3041a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR" 304258d36128SBarry Smith /*@ 3043a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 3044a1661176SMatthew Knepley 3045a1661176SMatthew Knepley Input Parameters: 3046a1661176SMatthew Knepley + B - the matrix 3047a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 3048a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 3049a1661176SMatthew Knepley - v - optional values in the matrix 3050a1661176SMatthew Knepley 3051a1661176SMatthew Knepley Level: developer 3052a1661176SMatthew Knepley 305358d36128SBarry Smith The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays() 305458d36128SBarry Smith 3055a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential 3056a1661176SMatthew Knepley 3057a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ 3058a1661176SMatthew Knepley @*/ 3059a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 3060a1661176SMatthew Knepley { 3061a1661176SMatthew Knepley PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]); 3062a1661176SMatthew Knepley PetscErrorCode ierr; 3063a1661176SMatthew Knepley 3064a1661176SMatthew Knepley PetscFunctionBegin; 3065a1661176SMatthew Knepley PetscValidHeaderSpecific(B,MAT_COOKIE,1); 3066a1661176SMatthew Knepley ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr); 3067a1661176SMatthew Knepley if (f) { 3068a1661176SMatthew Knepley ierr = (*f)(B,i,j,v);CHKERRQ(ierr); 3069a1661176SMatthew Knepley } 3070a1661176SMatthew Knepley PetscFunctionReturn(0); 3071a1661176SMatthew Knepley } 3072a1661176SMatthew Knepley 3073a1661176SMatthew Knepley EXTERN_C_BEGIN 3074a1661176SMatthew Knepley #undef __FUNCT__ 3075a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR_SeqAIJ" 3076b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3077a1661176SMatthew Knepley { 3078a1661176SMatthew Knepley PetscInt i; 3079a1661176SMatthew Knepley PetscInt m,n; 3080a1661176SMatthew Knepley PetscInt nz; 3081a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 3082a1661176SMatthew Knepley PetscScalar *values; 3083a1661176SMatthew Knepley PetscErrorCode ierr; 3084a1661176SMatthew Knepley 3085a1661176SMatthew Knepley PetscFunctionBegin; 3086a1661176SMatthew Knepley ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 3087a1661176SMatthew Knepley 3088b7940d39SSatish Balay if (Ii[0]) { 3089b7940d39SSatish Balay SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]); 3090a1661176SMatthew Knepley } 3091a1661176SMatthew Knepley ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr); 3092a1661176SMatthew Knepley for(i = 0; i < m; i++) { 3093b7940d39SSatish Balay nz = Ii[i+1]- Ii[i]; 3094a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 3095a1661176SMatthew Knepley if (nz < 0) { 3096a1661176SMatthew Knepley SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 3097a1661176SMatthew Knepley } 3098a1661176SMatthew Knepley nnz[i] = nz; 3099a1661176SMatthew Knepley } 3100a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 3101a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 3102a1661176SMatthew Knepley 3103a1661176SMatthew Knepley if (v) { 3104a1661176SMatthew Knepley values = (PetscScalar*) v; 3105a1661176SMatthew Knepley } else { 31060e83c824SBarry Smith ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr); 3107a1661176SMatthew Knepley ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3108a1661176SMatthew Knepley } 3109a1661176SMatthew Knepley 3110a1661176SMatthew Knepley for(i = 0; i < m; i++) { 3111b7940d39SSatish Balay nz = Ii[i+1] - Ii[i]; 3112b7940d39SSatish Balay ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr); 3113a1661176SMatthew Knepley } 3114a1661176SMatthew Knepley 3115a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3116a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3117a1661176SMatthew Knepley 3118a1661176SMatthew Knepley if (!v) { 3119a1661176SMatthew Knepley ierr = PetscFree(values);CHKERRQ(ierr); 3120a1661176SMatthew Knepley } 3121a1661176SMatthew Knepley PetscFunctionReturn(0); 3122a1661176SMatthew Knepley } 3123a1661176SMatthew Knepley EXTERN_C_END 3124a1661176SMatthew Knepley 31257c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h" 3126be7314b0SBarry Smith #include "private/petscaxpy.h" 3127170fe5c8SBarry Smith 3128170fe5c8SBarry Smith #undef __FUNCT__ 3129170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ" 3130170fe5c8SBarry Smith /* 3131170fe5c8SBarry Smith Computes (B'*A')' since computing B*A directly is untenable 3132170fe5c8SBarry Smith 3133170fe5c8SBarry Smith n p p 3134170fe5c8SBarry Smith ( ) ( ) ( ) 3135170fe5c8SBarry Smith m ( A ) * n ( B ) = m ( C ) 3136170fe5c8SBarry Smith ( ) ( ) ( ) 3137170fe5c8SBarry Smith 3138170fe5c8SBarry Smith */ 3139170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C) 3140170fe5c8SBarry Smith { 3141170fe5c8SBarry Smith PetscErrorCode ierr; 3142170fe5c8SBarry Smith Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data; 3143170fe5c8SBarry Smith Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data; 3144170fe5c8SBarry Smith Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data; 31451de00fd4SBarry Smith PetscInt i,n,m,q,p; 3146170fe5c8SBarry Smith const PetscInt *ii,*idx; 3147170fe5c8SBarry Smith const PetscScalar *b,*a,*a_q; 3148170fe5c8SBarry Smith PetscScalar *c,*c_q; 3149170fe5c8SBarry Smith 3150170fe5c8SBarry Smith PetscFunctionBegin; 3151d0f46423SBarry Smith m = A->rmap->n; 3152d0f46423SBarry Smith n = A->cmap->n; 3153d0f46423SBarry Smith p = B->cmap->n; 3154170fe5c8SBarry Smith a = sub_a->v; 3155170fe5c8SBarry Smith b = sub_b->a; 3156170fe5c8SBarry Smith c = sub_c->v; 3157170fe5c8SBarry Smith ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr); 3158170fe5c8SBarry Smith 3159170fe5c8SBarry Smith ii = sub_b->i; 3160170fe5c8SBarry Smith idx = sub_b->j; 3161170fe5c8SBarry Smith for (i=0; i<n; i++) { 3162170fe5c8SBarry Smith q = ii[i+1] - ii[i]; 3163170fe5c8SBarry Smith while (q-->0) { 3164170fe5c8SBarry Smith c_q = c + m*(*idx); 3165170fe5c8SBarry Smith a_q = a + m*i; 3166be7314b0SBarry Smith PetscAXPY(c_q,*b,a_q,m); 3167170fe5c8SBarry Smith idx++; 3168170fe5c8SBarry Smith b++; 3169170fe5c8SBarry Smith } 3170170fe5c8SBarry Smith } 3171170fe5c8SBarry Smith PetscFunctionReturn(0); 3172170fe5c8SBarry Smith } 3173170fe5c8SBarry Smith 3174170fe5c8SBarry Smith #undef __FUNCT__ 3175170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ" 3176170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 3177170fe5c8SBarry Smith { 3178170fe5c8SBarry Smith PetscErrorCode ierr; 3179d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 3180170fe5c8SBarry Smith Mat Cmat; 3181170fe5c8SBarry Smith 3182170fe5c8SBarry Smith PetscFunctionBegin; 3183d0f46423SBarry Smith if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n); 318439804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 3185170fe5c8SBarry Smith ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr); 3186170fe5c8SBarry Smith ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr); 3187170fe5c8SBarry Smith ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 3188170fe5c8SBarry Smith Cmat->assembled = PETSC_TRUE; 3189170fe5c8SBarry Smith *C = Cmat; 3190170fe5c8SBarry Smith PetscFunctionReturn(0); 3191170fe5c8SBarry Smith } 3192170fe5c8SBarry Smith 3193170fe5c8SBarry Smith /* ----------------------------------------------------------------*/ 3194170fe5c8SBarry Smith #undef __FUNCT__ 3195170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ" 3196170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 3197170fe5c8SBarry Smith { 3198170fe5c8SBarry Smith PetscErrorCode ierr; 3199170fe5c8SBarry Smith 3200170fe5c8SBarry Smith PetscFunctionBegin; 3201170fe5c8SBarry Smith if (scall == MAT_INITIAL_MATRIX){ 3202170fe5c8SBarry Smith ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr); 3203170fe5c8SBarry Smith } 3204170fe5c8SBarry Smith ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr); 3205170fe5c8SBarry Smith PetscFunctionReturn(0); 3206170fe5c8SBarry Smith } 3207170fe5c8SBarry Smith 3208170fe5c8SBarry Smith 32090bad9183SKris Buschelman /*MC 3210fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 32110bad9183SKris Buschelman based on compressed sparse row format. 32120bad9183SKris Buschelman 32130bad9183SKris Buschelman Options Database Keys: 32140bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 32150bad9183SKris Buschelman 32160bad9183SKris Buschelman Level: beginner 32170bad9183SKris Buschelman 3218f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 32190bad9183SKris Buschelman M*/ 32200bad9183SKris Buschelman 3221a6175056SHong Zhang EXTERN_C_BEGIN 3222b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX) 3223b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*); 3224b5e56a35SBarry Smith #endif 322565460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE) 3226af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *); 3227af1023dbSSatish Balay #endif 322817667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*); 3229b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*); 3230*a83b8d76SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*); 3231db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *); 3232611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 32335c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*); 3234611f576cSBarry Smith #endif 3235611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU) 32365c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*); 3237611f576cSBarry Smith #endif 3238f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST) 3239f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*); 3240f3c0ef26SHong Zhang #endif 3241611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 32425c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*); 3243611f576cSBarry Smith #endif 3244eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK) 3245eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*); 3246eb3b5408SSatish Balay #endif 3247719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL) 3248719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*); 3249719d5645SBarry Smith #endif 325017667f90SBarry Smith EXTERN_C_END 325117667f90SBarry Smith 3252b24902e0SBarry Smith 325317667f90SBarry Smith EXTERN_C_BEGIN 32544a2ae208SSatish Balay #undef __FUNCT__ 32554a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ" 3256be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B) 3257273d9f13SBarry Smith { 3258273d9f13SBarry Smith Mat_SeqAIJ *b; 3259dfbe8321SBarry Smith PetscErrorCode ierr; 326038baddfdSBarry Smith PetscMPIInt size; 3261273d9f13SBarry Smith 3262273d9f13SBarry Smith PetscFunctionBegin; 32637adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 3264273d9f13SBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 3265273d9f13SBarry Smith 326638f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr); 3267b0a32e0cSBarry Smith B->data = (void*)b; 3268549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 326990f02eecSBarry Smith B->mapping = 0; 3270416022c9SBarry Smith b->row = 0; 3271416022c9SBarry Smith b->col = 0; 327282bf6240SBarry Smith b->icol = 0; 3273b810aeb4SBarry Smith b->reallocs = 0; 327436db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 3275f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 3276416022c9SBarry Smith b->nonew = 0; 3277416022c9SBarry Smith b->diag = 0; 3278416022c9SBarry Smith b->solve_work = 0; 32792a1b7f2aSHong Zhang B->spptr = 0; 3280be6bf707SBarry Smith b->saved_values = 0; 3281d7f994e1SBarry Smith b->idiag = 0; 328271f1c65dSBarry Smith b->mdiag = 0; 328371f1c65dSBarry Smith b->ssor_work = 0; 328471f1c65dSBarry Smith b->omega = 1.0; 328571f1c65dSBarry Smith b->fshift = 0.0; 328671f1c65dSBarry Smith b->idiagvalid = PETSC_FALSE; 3287a9817697SBarry Smith b->keepnonzeropattern = PETSC_FALSE; 3288a30b2313SHong Zhang b->xtoy = 0; 3289a30b2313SHong Zhang b->XtoY = 0; 329073e7a558SHong Zhang b->compressedrow.use = PETSC_FALSE; 3291d0f46423SBarry Smith b->compressedrow.nrows = B->rmap->n; 3292d487561eSHong Zhang b->compressedrow.i = PETSC_NULL; 3293d487561eSHong Zhang b->compressedrow.rindex = PETSC_NULL; 3294d487561eSHong Zhang b->compressedrow.checked = PETSC_FALSE; 329588e51ccdSHong Zhang B->same_nonzero = PETSC_FALSE; 329617ab2063SBarry Smith 329735d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 3298b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX) 3299ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 3300b5e56a35SBarry Smith "MatGetFactor_seqaij_pastix", 3301b5e56a35SBarry Smith MatGetFactor_seqaij_pastix);CHKERRQ(ierr); 3302b5e56a35SBarry Smith #endif 330365460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE) 3304ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C", 3305719d5645SBarry Smith "MatGetFactor_seqaij_essl", 3306719d5645SBarry Smith MatGetFactor_seqaij_essl);CHKERRQ(ierr); 3307719d5645SBarry Smith #endif 3308611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU) 3309ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C", 33105c9eb25fSBarry Smith "MatGetFactor_seqaij_superlu", 33115c9eb25fSBarry Smith MatGetFactor_seqaij_superlu);CHKERRQ(ierr); 3312611f576cSBarry Smith #endif 3313f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST) 3314ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 3315f3c0ef26SHong Zhang "MatGetFactor_seqaij_superlu_dist", 3316f3c0ef26SHong Zhang MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr); 3317f3c0ef26SHong Zhang #endif 3318611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 3319ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C", 33205c9eb25fSBarry Smith "MatGetFactor_seqaij_spooles", 33215c9eb25fSBarry Smith MatGetFactor_seqaij_spooles);CHKERRQ(ierr); 3322611f576cSBarry Smith #endif 3323611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 3324ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 33255c9eb25fSBarry Smith "MatGetFactor_seqaij_mumps", 33265c9eb25fSBarry Smith MatGetFactor_seqaij_mumps);CHKERRQ(ierr); 3327611f576cSBarry Smith #endif 3328eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK) 3329ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C", 3330eb3b5408SSatish Balay "MatGetFactor_seqaij_umfpack", 3331eb3b5408SSatish Balay MatGetFactor_seqaij_umfpack);CHKERRQ(ierr); 3332eb3b5408SSatish Balay #endif 3333719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL) 3334ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C", 3335719d5645SBarry Smith "MatGetFactor_seqaij_lusol", 3336719d5645SBarry Smith MatGetFactor_seqaij_lusol);CHKERRQ(ierr); 3337719d5645SBarry Smith #endif 3338ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C", 3339b24902e0SBarry Smith "MatGetFactor_seqaij_petsc", 3340b24902e0SBarry Smith MatGetFactor_seqaij_petsc);CHKERRQ(ierr); 3341ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C", 3342db4efbfdSBarry Smith "MatGetFactorAvailable_seqaij_petsc", 3343db4efbfdSBarry Smith MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr); 3344*a83b8d76SBarry Smith /* ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C", 3345*a83b8d76SBarry Smith "MatGetFactor_seqaij_bas", 3346*a83b8d76SBarry Smith MatGetFactor_seqaij_bas); */ 3347f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C", 3348bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 3349bc4b532fSSatish Balay MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 3350f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 3351be6bf707SBarry Smith "MatStoreValues_SeqAIJ", 3352bc4b532fSSatish Balay MatStoreValues_SeqAIJ);CHKERRQ(ierr); 3353f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 3354be6bf707SBarry Smith "MatRetrieveValues_SeqAIJ", 3355bc4b532fSSatish Balay MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 3356a6175056SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C", 3357a6175056SHong Zhang "MatConvert_SeqAIJ_SeqSBAIJ", 3358a6175056SHong Zhang MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 335985fc7724SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C", 336085fc7724SBarry Smith "MatConvert_SeqAIJ_SeqBAIJ", 336185fc7724SBarry Smith MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 3362ba03775dSHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C", 3363ba03775dSHong Zhang "MatConvert_SeqAIJ_SeqCSRPERM", 3364ba03775dSHong Zhang MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr); 336517667f90SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C", 336617667f90SBarry Smith "MatConvert_SeqAIJ_SeqCRL", 336717667f90SBarry Smith MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr); 33685fbd3699SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 33695fbd3699SBarry Smith "MatIsTranspose_SeqAIJ", 33705fbd3699SBarry Smith MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 33711cbb95d3SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C", 33721cbb95d3SBarry Smith "MatIsHermitianTranspose_SeqAIJ", 33731cbb95d3SBarry Smith MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 3374a23d5eceSKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C", 3375a23d5eceSKris Buschelman "MatSeqAIJSetPreallocation_SeqAIJ", 3376a23d5eceSKris Buschelman MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 3377a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C", 3378a1661176SMatthew Knepley "MatSeqAIJSetPreallocationCSR_SeqAIJ", 3379a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 338005b94e36SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C", 338105b94e36SKris Buschelman "MatReorderForNonzeroDiagonal_SeqAIJ", 338205b94e36SKris Buschelman MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 3383170fe5c8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C", 3384170fe5c8SBarry Smith "MatMatMult_SeqDense_SeqAIJ", 3385170fe5c8SBarry Smith MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr); 3386170fe5c8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C", 3387170fe5c8SBarry Smith "MatMatMultSymbolic_SeqDense_SeqAIJ", 3388170fe5c8SBarry Smith MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 3389170fe5c8SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C", 3390170fe5c8SBarry Smith "MatMatMultNumeric_SeqDense_SeqAIJ", 3391170fe5c8SBarry Smith MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 33924108e4d5SBarry Smith ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr); 339317667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 33943a40ed3dSBarry Smith PetscFunctionReturn(0); 339517ab2063SBarry Smith } 3396273d9f13SBarry Smith EXTERN_C_END 339717ab2063SBarry Smith 33984a2ae208SSatish Balay #undef __FUNCT__ 3399b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ" 3400b24902e0SBarry Smith /* 3401b24902e0SBarry Smith Given a matrix generated with MatGetFactor() duplicates all the information in A into B 3402b24902e0SBarry Smith */ 3403f77e22a1SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace) 340417ab2063SBarry Smith { 3405416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 34066849ba73SBarry Smith PetscErrorCode ierr; 3407d0f46423SBarry Smith PetscInt i,m = A->rmap->n; 340817ab2063SBarry Smith 34093a40ed3dSBarry Smith PetscFunctionBegin; 3410273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 3411273d9f13SBarry Smith 3412416022c9SBarry Smith C->factor = A->factor; 34136ad4291fSHong Zhang 3414416022c9SBarry Smith c->row = 0; 3415416022c9SBarry Smith c->col = 0; 341682bf6240SBarry Smith c->icol = 0; 34176ad4291fSHong Zhang c->reallocs = 0; 341817ab2063SBarry Smith 34196ad4291fSHong Zhang C->assembled = PETSC_TRUE; 342017ab2063SBarry Smith 342126283091SBarry Smith ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr); 342226283091SBarry Smith ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr); 342326283091SBarry Smith ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr); 342426283091SBarry Smith ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr); 3425eec197d1SBarry Smith 342633b91e9fSSatish Balay ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr); 34279518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr); 342817ab2063SBarry Smith for (i=0; i<m; i++) { 3429416022c9SBarry Smith c->imax[i] = a->imax[i]; 3430416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 343117ab2063SBarry Smith } 343217ab2063SBarry Smith 343317ab2063SBarry Smith /* allocate the matrix space */ 3434f77e22a1SHong Zhang if (mallocmatspace){ 3435a96a251dSBarry Smith ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr); 34369518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 3437f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 343897f1f81fSBarry Smith ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 343917ab2063SBarry Smith if (m > 0) { 344097f1f81fSBarry Smith ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr); 3441be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 3442bfeeae90SHong Zhang ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 3443be6bf707SBarry Smith } else { 3444bfeeae90SHong Zhang ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 344517ab2063SBarry Smith } 344608480c60SBarry Smith } 3447f77e22a1SHong Zhang } 344817ab2063SBarry Smith 34496ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 3450416022c9SBarry Smith c->roworiented = a->roworiented; 3451416022c9SBarry Smith c->nonew = a->nonew; 3452416022c9SBarry Smith if (a->diag) { 345397f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr); 345452e6d16bSBarry Smith ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 345517ab2063SBarry Smith for (i=0; i<m; i++) { 3456416022c9SBarry Smith c->diag[i] = a->diag[i]; 345717ab2063SBarry Smith } 34583a40ed3dSBarry Smith } else c->diag = 0; 34596ad4291fSHong Zhang c->solve_work = 0; 34606ad4291fSHong Zhang c->saved_values = 0; 34616ad4291fSHong Zhang c->idiag = 0; 346271f1c65dSBarry Smith c->ssor_work = 0; 3463a9817697SBarry Smith c->keepnonzeropattern = a->keepnonzeropattern; 3464e6b907acSBarry Smith c->free_a = PETSC_TRUE; 3465e6b907acSBarry Smith c->free_ij = PETSC_TRUE; 34666ad4291fSHong Zhang c->xtoy = 0; 34676ad4291fSHong Zhang c->XtoY = 0; 34686ad4291fSHong Zhang 3469416022c9SBarry Smith c->nz = a->nz; 3470416022c9SBarry Smith c->maxnz = a->maxnz; 3471273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 3472754ec7b1SSatish Balay 34736ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 34746ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 34756ad4291fSHong Zhang c->compressedrow.checked = a->compressedrow.checked; 34766ad4291fSHong Zhang if (a->compressedrow.checked && a->compressedrow.use){ 34776ad4291fSHong Zhang i = a->compressedrow.nrows; 34780e83c824SBarry Smith ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr); 34796ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr); 34806ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr); 348127ea64f8SHong Zhang } else { 348227ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 348327ea64f8SHong Zhang c->compressedrow.i = PETSC_NULL; 348427ea64f8SHong Zhang c->compressedrow.rindex = PETSC_NULL; 34856ad4291fSHong Zhang } 348688e51ccdSHong Zhang C->same_nonzero = A->same_nonzero; 34874108e4d5SBarry Smith ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr); 34884846f1f5SKris Buschelman 34897adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr); 34903a40ed3dSBarry Smith PetscFunctionReturn(0); 349117ab2063SBarry Smith } 349217ab2063SBarry Smith 34934a2ae208SSatish Balay #undef __FUNCT__ 3494b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ" 3495b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 3496b24902e0SBarry Smith { 3497b24902e0SBarry Smith PetscErrorCode ierr; 3498b24902e0SBarry Smith 3499b24902e0SBarry Smith PetscFunctionBegin; 3500b24902e0SBarry Smith ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 35014b6263acSBarry Smith ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr); 3502b24902e0SBarry Smith ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr); 3503f77e22a1SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr); 3504b24902e0SBarry Smith PetscFunctionReturn(0); 3505b24902e0SBarry Smith } 3506b24902e0SBarry Smith 3507b24902e0SBarry Smith #undef __FUNCT__ 35084a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ" 3509a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A) 351017ab2063SBarry Smith { 3511416022c9SBarry Smith Mat_SeqAIJ *a; 3512416022c9SBarry Smith Mat B; 35136849ba73SBarry Smith PetscErrorCode ierr; 35143c601197SSatish Balay PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N; 351538baddfdSBarry Smith int fd; 351638baddfdSBarry Smith PetscMPIInt size; 3517bcd2baecSBarry Smith MPI_Comm comm; 351817ab2063SBarry Smith 35193a40ed3dSBarry Smith PetscFunctionBegin; 3520e864ced6SBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 3521d132466eSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 352229bbc08cSBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor"); 3523b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 35240752156aSBarry Smith ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 3525552e946dSBarry Smith if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 352617ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 352717ab2063SBarry Smith 3528d64ed03dSBarry Smith if (nz < 0) { 352929bbc08cSBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 3530d64ed03dSBarry Smith } 3531d64ed03dSBarry Smith 353217ab2063SBarry Smith /* read in row lengths */ 353397f1f81fSBarry Smith ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 35340752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 353517ab2063SBarry Smith 35363c601197SSatish Balay /* check if sum of rowlengths is same as nz */ 35373c601197SSatish Balay for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 35383c601197SSatish Balay if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum); 35393c601197SSatish Balay 354017ab2063SBarry Smith /* create our matrix */ 3541f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B);CHKERRQ(ierr); 3542f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 3543b3a1e11cSKris Buschelman ierr = MatSetType(B,type);CHKERRQ(ierr); 3544ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr); 3545416022c9SBarry Smith a = (Mat_SeqAIJ*)B->data; 354617ab2063SBarry Smith 35470752156aSBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 354817ab2063SBarry Smith 354917ab2063SBarry Smith /* read in nonzero values */ 35500752156aSBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 355117ab2063SBarry Smith 355217ab2063SBarry Smith /* set matrix "i" values */ 3553efb16452SHong Zhang a->i[0] = 0; 355417ab2063SBarry Smith for (i=1; i<= M; i++) { 3555416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 3556416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 355717ab2063SBarry Smith } 3558606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 355917ab2063SBarry Smith 35606d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 35616d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3562b3a1e11cSKris Buschelman *A = B; 35633a40ed3dSBarry Smith PetscFunctionReturn(0); 356417ab2063SBarry Smith } 356517ab2063SBarry Smith 35664a2ae208SSatish Balay #undef __FUNCT__ 3567b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ" 3568dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg) 35697264ac53SSatish Balay { 35707264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data; 3571dfbe8321SBarry Smith PetscErrorCode ierr; 3572eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 3573eeffb40dSHong Zhang PetscInt k; 3574eeffb40dSHong Zhang #endif 35757264ac53SSatish Balay 35763a40ed3dSBarry Smith PetscFunctionBegin; 3577bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 3578d0f46423SBarry Smith if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) { 3579ca44d042SBarry Smith *flg = PETSC_FALSE; 3580ca44d042SBarry Smith PetscFunctionReturn(0); 3581bcd2baecSBarry Smith } 35827264ac53SSatish Balay 35837264ac53SSatish Balay /* if the a->i are the same */ 3584d0f46423SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3585abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 35867264ac53SSatish Balay 35877264ac53SSatish Balay /* if a->j are the same */ 358897f1f81fSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3589abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 3590bcd2baecSBarry Smith 3591bcd2baecSBarry Smith /* if a->a are the same */ 3592eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 3593eeffb40dSHong Zhang for (k=0; k<a->nz; k++){ 3594eeffb40dSHong Zhang if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){ 3595eeffb40dSHong Zhang *flg = PETSC_FALSE; 35963a40ed3dSBarry Smith PetscFunctionReturn(0); 3597eeffb40dSHong Zhang } 3598eeffb40dSHong Zhang } 3599eeffb40dSHong Zhang #else 3600eeffb40dSHong Zhang ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 3601eeffb40dSHong Zhang #endif 3602eeffb40dSHong Zhang PetscFunctionReturn(0); 36037264ac53SSatish Balay } 360436db0b34SBarry Smith 36054a2ae208SSatish Balay #undef __FUNCT__ 36064a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays" 360705869f15SSatish Balay /*@ 360836db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 360936db0b34SBarry Smith provided by the user. 361036db0b34SBarry Smith 3611c75a6043SHong Zhang Collective on MPI_Comm 361236db0b34SBarry Smith 361336db0b34SBarry Smith Input Parameters: 361436db0b34SBarry Smith + comm - must be an MPI communicator of size 1 361536db0b34SBarry Smith . m - number of rows 361636db0b34SBarry Smith . n - number of columns 361736db0b34SBarry Smith . i - row indices 361836db0b34SBarry Smith . j - column indices 361936db0b34SBarry Smith - a - matrix values 362036db0b34SBarry Smith 362136db0b34SBarry Smith Output Parameter: 362236db0b34SBarry Smith . mat - the matrix 362336db0b34SBarry Smith 362436db0b34SBarry Smith Level: intermediate 362536db0b34SBarry Smith 362636db0b34SBarry Smith Notes: 36270551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 362836db0b34SBarry Smith once the matrix is destroyed 362936db0b34SBarry Smith 363036db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 363136db0b34SBarry Smith 3632bfeeae90SHong Zhang The i and j indices are 0 based 363336db0b34SBarry Smith 3634a4552177SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 3635a4552177SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 3636a4552177SSatish Balay as shown: 3637a4552177SSatish Balay 3638a4552177SSatish Balay 1 0 0 3639a4552177SSatish Balay 2 0 3 3640a4552177SSatish Balay 4 5 6 3641a4552177SSatish Balay 3642a4552177SSatish Balay i = {0,1,3,6} [size = nrow+1 = 3+1] 36439985e31cSBarry Smith j = {0,0,2,0,1,2} [size = nz = 6]; values must be sorted for each row 3644a4552177SSatish Balay v = {1,2,3,4,5,6} [size = nz = 6] 3645a4552177SSatish Balay 36469985e31cSBarry Smith 36472fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 364836db0b34SBarry Smith 364936db0b34SBarry Smith @*/ 3650a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat) 365136db0b34SBarry Smith { 3652dfbe8321SBarry Smith PetscErrorCode ierr; 3653cbcfb4deSHong Zhang PetscInt ii; 365436db0b34SBarry Smith Mat_SeqAIJ *aij; 3655cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG) 3656cbcfb4deSHong Zhang PetscInt jj; 3657cbcfb4deSHong Zhang #endif 365836db0b34SBarry Smith 365936db0b34SBarry Smith PetscFunctionBegin; 3660a96a251dSBarry Smith if (i[0]) { 3661634064b4SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 366236db0b34SBarry Smith } 3663f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3664f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 3665ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 3666ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 3667ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 3668ab93d7beSBarry Smith ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr); 3669ab93d7beSBarry Smith 367036db0b34SBarry Smith aij->i = i; 367136db0b34SBarry Smith aij->j = j; 367236db0b34SBarry Smith aij->a = a; 367336db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 367436db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 3675e6b907acSBarry Smith aij->free_a = PETSC_FALSE; 3676e6b907acSBarry Smith aij->free_ij = PETSC_FALSE; 367736db0b34SBarry Smith 367836db0b34SBarry Smith for (ii=0; ii<m; ii++) { 367936db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 36802515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 368179a5c55eSBarry Smith if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]); 36829985e31cSBarry Smith for (jj=i[ii]+1; jj<i[ii+1]; jj++) { 36839985e31cSBarry Smith if (j[jj] < j[jj-1]) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii); 36849985e31cSBarry Smith if (j[jj] == j[jj]-1) SETERRQ3(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); 36859985e31cSBarry Smith } 368636db0b34SBarry Smith #endif 368736db0b34SBarry Smith } 36882515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 368936db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 369079a5c55eSBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]); 369179a5c55eSBarry Smith if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]); 369236db0b34SBarry Smith } 369336db0b34SBarry Smith #endif 369436db0b34SBarry Smith 3695b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3696b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 369736db0b34SBarry Smith PetscFunctionReturn(0); 369836db0b34SBarry Smith } 369936db0b34SBarry Smith 3700cc8ba8e1SBarry Smith #undef __FUNCT__ 3701ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ" 3702dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring) 3703cc8ba8e1SBarry Smith { 3704dfbe8321SBarry Smith PetscErrorCode ierr; 3705cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 370636db0b34SBarry Smith 3707cc8ba8e1SBarry Smith PetscFunctionBegin; 37088ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 3709cc8ba8e1SBarry Smith ierr = ISColoringReference(coloring);CHKERRQ(ierr); 3710cc8ba8e1SBarry Smith a->coloring = coloring; 371112c595b3SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 371297f1f81fSBarry Smith PetscInt i,*larray; 371312c595b3SBarry Smith ISColoring ocoloring; 371408b6dcc0SBarry Smith ISColoringValue *colors; 371512c595b3SBarry Smith 371612c595b3SBarry Smith /* set coloring for diagonal portion */ 37170e83c824SBarry Smith ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr); 3718d0f46423SBarry Smith for (i=0; i<A->cmap->n; i++) { 371912c595b3SBarry Smith larray[i] = i; 372012c595b3SBarry Smith } 3721d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 37220e83c824SBarry Smith ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 3723d0f46423SBarry Smith for (i=0; i<A->cmap->n; i++) { 372412c595b3SBarry Smith colors[i] = coloring->colors[larray[i]]; 372512c595b3SBarry Smith } 372612c595b3SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 3727d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 372812c595b3SBarry Smith a->coloring = ocoloring; 372912c595b3SBarry Smith } 3730cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3731cc8ba8e1SBarry Smith } 3732cc8ba8e1SBarry Smith 3733dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 3734ee4f033dSBarry Smith EXTERN_C_BEGIN 373529c1e371SBarry Smith #include "adic/ad_utils.h" 3736ee4f033dSBarry Smith EXTERN_C_END 3737cc8ba8e1SBarry Smith 3738cc8ba8e1SBarry Smith #undef __FUNCT__ 3739ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 3740dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 3741cc8ba8e1SBarry Smith { 3742cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3743d0f46423SBarry Smith PetscInt m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen; 37444440f671SBarry Smith PetscScalar *v = a->a,*values = ((PetscScalar*)advalues)+1; 374508b6dcc0SBarry Smith ISColoringValue *color; 3746cc8ba8e1SBarry Smith 3747cc8ba8e1SBarry Smith PetscFunctionBegin; 3748e005ede5SBarry Smith if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 37494440f671SBarry Smith nlen = PetscADGetDerivTypeSize()/sizeof(PetscScalar); 3750cc8ba8e1SBarry Smith color = a->coloring->colors; 3751cc8ba8e1SBarry Smith /* loop over rows */ 3752cc8ba8e1SBarry Smith for (i=0; i<m; i++) { 3753cc8ba8e1SBarry Smith nz = ii[i+1] - ii[i]; 3754cc8ba8e1SBarry Smith /* loop over columns putting computed value into matrix */ 3755cc8ba8e1SBarry Smith for (j=0; j<nz; j++) { 3756cc8ba8e1SBarry Smith *v++ = values[color[*jj++]]; 3757cc8ba8e1SBarry Smith } 37584440f671SBarry Smith values += nlen; /* jump to next row of derivatives */ 3759ee4f033dSBarry Smith } 3760ee4f033dSBarry Smith PetscFunctionReturn(0); 3761ee4f033dSBarry Smith } 3762ee4f033dSBarry Smith #endif 3763ee4f033dSBarry Smith 3764ee4f033dSBarry Smith #undef __FUNCT__ 3765ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ" 376697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues) 3767ee4f033dSBarry Smith { 3768ee4f033dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3769d0f46423SBarry Smith PetscInt m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j; 377054f21887SBarry Smith MatScalar *v = a->a; 377154f21887SBarry Smith PetscScalar *values = (PetscScalar *)advalues; 377208b6dcc0SBarry Smith ISColoringValue *color; 3773ee4f033dSBarry Smith 3774ee4f033dSBarry Smith PetscFunctionBegin; 3775e005ede5SBarry Smith if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 3776ee4f033dSBarry Smith color = a->coloring->colors; 3777ee4f033dSBarry Smith /* loop over rows */ 3778ee4f033dSBarry Smith for (i=0; i<m; i++) { 3779ee4f033dSBarry Smith nz = ii[i+1] - ii[i]; 3780ee4f033dSBarry Smith /* loop over columns putting computed value into matrix */ 3781ee4f033dSBarry Smith for (j=0; j<nz; j++) { 3782ee4f033dSBarry Smith *v++ = values[color[*jj++]]; 3783ee4f033dSBarry Smith } 3784ee4f033dSBarry Smith values += nl; /* jump to next row of derivatives */ 3785cc8ba8e1SBarry Smith } 3786cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3787cc8ba8e1SBarry Smith } 378836db0b34SBarry Smith 378981824310SBarry Smith /* 379081824310SBarry Smith Special version for direct calls from Fortran 379181824310SBarry Smith */ 379281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 379381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 379481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 379581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 379681824310SBarry Smith #endif 379781824310SBarry Smith 379881824310SBarry Smith /* Change these macros so can be used in void function */ 379981824310SBarry Smith #undef CHKERRQ 38007adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr) 380181824310SBarry Smith #undef SETERRQ2 38027adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr) 380381824310SBarry Smith 380481824310SBarry Smith EXTERN_C_BEGIN 380581824310SBarry Smith #undef __FUNCT__ 380681824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_" 38071f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr) 380881824310SBarry Smith { 380981824310SBarry Smith Mat A = *AA; 381081824310SBarry Smith PetscInt m = *mm, n = *nn; 381181824310SBarry Smith InsertMode is = *isis; 381281824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 381381824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 381481824310SBarry Smith PetscInt *imax,*ai,*ailen; 381581824310SBarry Smith PetscErrorCode ierr; 381681824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 381754f21887SBarry Smith MatScalar *ap,value,*aa; 3818edb03aefSBarry Smith PetscTruth ignorezeroentries = a->ignorezeroentries; 381981824310SBarry Smith PetscTruth roworiented = a->roworiented; 382081824310SBarry Smith 382181824310SBarry Smith PetscFunctionBegin; 3822d9e2c085SLisandro Dalcin ierr = MatPreallocated(A);CHKERRQ(ierr); 382381824310SBarry Smith imax = a->imax; 382481824310SBarry Smith ai = a->i; 382581824310SBarry Smith ailen = a->ilen; 382681824310SBarry Smith aj = a->j; 382781824310SBarry Smith aa = a->a; 382881824310SBarry Smith 382981824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 383081824310SBarry Smith row = im[k]; 383181824310SBarry Smith if (row < 0) continue; 383281824310SBarry Smith #if defined(PETSC_USE_DEBUG) 3833d0f46423SBarry Smith if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 383481824310SBarry Smith #endif 383581824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 383681824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 383781824310SBarry Smith low = 0; 383881824310SBarry Smith high = nrow; 383981824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 384081824310SBarry Smith if (in[l] < 0) continue; 384181824310SBarry Smith #if defined(PETSC_USE_DEBUG) 3842d0f46423SBarry Smith if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 384381824310SBarry Smith #endif 384481824310SBarry Smith col = in[l]; 384581824310SBarry Smith if (roworiented) { 384681824310SBarry Smith value = v[l + k*n]; 384781824310SBarry Smith } else { 384881824310SBarry Smith value = v[k + l*m]; 384981824310SBarry Smith } 385081824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 385181824310SBarry Smith 385281824310SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 385381824310SBarry Smith lastcol = col; 385481824310SBarry Smith while (high-low > 5) { 385581824310SBarry Smith t = (low+high)/2; 385681824310SBarry Smith if (rp[t] > col) high = t; 385781824310SBarry Smith else low = t; 385881824310SBarry Smith } 385981824310SBarry Smith for (i=low; i<high; i++) { 386081824310SBarry Smith if (rp[i] > col) break; 386181824310SBarry Smith if (rp[i] == col) { 386281824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 386381824310SBarry Smith else ap[i] = value; 386481824310SBarry Smith goto noinsert; 386581824310SBarry Smith } 386681824310SBarry Smith } 386781824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 386881824310SBarry Smith if (nonew == 1) goto noinsert; 38697adad957SLisandro Dalcin if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 3870d0f46423SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 387181824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 387281824310SBarry Smith /* shift up all the later entries in this row */ 387381824310SBarry Smith for (ii=N; ii>=i; ii--) { 387481824310SBarry Smith rp[ii+1] = rp[ii]; 387581824310SBarry Smith ap[ii+1] = ap[ii]; 387681824310SBarry Smith } 387781824310SBarry Smith rp[i] = col; 387881824310SBarry Smith ap[i] = value; 387981824310SBarry Smith noinsert:; 388081824310SBarry Smith low = i + 1; 388181824310SBarry Smith } 388281824310SBarry Smith ailen[row] = nrow; 388381824310SBarry Smith } 388481824310SBarry Smith A->same_nonzero = PETSC_FALSE; 388581824310SBarry Smith PetscFunctionReturnVoid(); 388681824310SBarry Smith } 388781824310SBarry Smith EXTERN_C_END 3888