1be1d678aSKris Buschelman #define PETSCMAT_DLL 2b3cc6726SBarry Smith 3d5d45c9bSBarry Smith /* 43369ce9aSBarry Smith Defines the basic matrix operations for the AIJ (compressed row) 5d5d45c9bSBarry Smith matrix storage format. 6d5d45c9bSBarry Smith */ 73369ce9aSBarry Smith 89e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h" /*I "petscmat.h" I*/ 9f5eb4b81SSatish Balay #include "src/inline/spops.h" 108d195f9aSBarry Smith #include "src/inline/dot.h" 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; 19899cda47SBarry Smith PetscInt i,*diag, m = Y->rmap.n; 2079299369SBarry Smith PetscScalar *v,*aa = aij->a; 2109f38230SBarry Smith PetscTruth missing; 2279299369SBarry Smith 2379299369SBarry Smith PetscFunctionBegin; 2409f38230SBarry Smith if (Y->assembled) { 2509f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr); 2609f38230SBarry Smith if (!missing) { 2779299369SBarry Smith diag = aij->diag; 2879299369SBarry Smith ierr = VecGetArray(D,&v);CHKERRQ(ierr); 2979299369SBarry Smith if (is == INSERT_VALUES) { 3079299369SBarry Smith for (i=0; i<m; i++) { 3179299369SBarry Smith aa[diag[i]] = v[i]; 3279299369SBarry Smith } 3379299369SBarry Smith } else { 3479299369SBarry Smith for (i=0; i<m; i++) { 3579299369SBarry Smith aa[diag[i]] += v[i]; 3679299369SBarry Smith } 3779299369SBarry Smith } 3879299369SBarry Smith ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 3979299369SBarry Smith PetscFunctionReturn(0); 4079299369SBarry Smith } 4109f38230SBarry Smith } 4209f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 4309f38230SBarry Smith PetscFunctionReturn(0); 4409f38230SBarry Smith } 4579299369SBarry Smith 4679299369SBarry Smith #undef __FUNCT__ 474a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ" 488f7157efSSatish Balay PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 4917ab2063SBarry Smith { 50416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 51dfbe8321SBarry Smith PetscErrorCode ierr; 5297f1f81fSBarry Smith PetscInt i,ishift; 5317ab2063SBarry Smith 543a40ed3dSBarry Smith PetscFunctionBegin; 55899cda47SBarry Smith *m = A->rmap.n; 563a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 57bfeeae90SHong Zhang ishift = 0; 5853e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 59899cda47SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 60bfeeae90SHong Zhang } else if (oshift == 1) { 61899cda47SBarry Smith PetscInt nz = a->i[A->rmap.n]; 623b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 63899cda47SBarry Smith ierr = PetscMalloc((A->rmap.n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr); 64ecc77c7aSBarry Smith for (i=0; i<A->rmap.n+1; i++) (*ia)[i] = a->i[i] + 1; 65ecc77c7aSBarry Smith if (ja) { 6697f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr); 673b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1; 68ecc77c7aSBarry Smith } 696945ee14SBarry Smith } else { 70ecc77c7aSBarry Smith *ia = a->i; 71ecc77c7aSBarry Smith if (ja) *ja = a->j; 72a2ce50c7SBarry Smith } 733a40ed3dSBarry Smith PetscFunctionReturn(0); 74a2744918SBarry Smith } 75a2744918SBarry Smith 764a2ae208SSatish Balay #undef __FUNCT__ 774a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ" 788f7157efSSatish Balay PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 796945ee14SBarry Smith { 80dfbe8321SBarry Smith PetscErrorCode ierr; 816945ee14SBarry Smith 823a40ed3dSBarry Smith PetscFunctionBegin; 833a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 84bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 85606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 86ecc77c7aSBarry Smith if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);} 87bcd2baecSBarry Smith } 883a40ed3dSBarry Smith PetscFunctionReturn(0); 8917ab2063SBarry Smith } 9017ab2063SBarry Smith 914a2ae208SSatish Balay #undef __FUNCT__ 924a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ" 938f7157efSSatish Balay PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 943b2fbd54SBarry Smith { 953b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 96dfbe8321SBarry Smith PetscErrorCode ierr; 97899cda47SBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->cmap.n,m = A->rmap.n; 9897f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 993b2fbd54SBarry Smith 1003a40ed3dSBarry Smith PetscFunctionBegin; 101899cda47SBarry Smith *nn = n; 1023a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1033b2fbd54SBarry Smith if (symmetric) { 104899cda47SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap.n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr); 1053b2fbd54SBarry Smith } else { 10697f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr); 10797f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 10897f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr); 10997f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr); 1103b2fbd54SBarry Smith jj = a->j; 1113b2fbd54SBarry Smith for (i=0; i<nz; i++) { 112bfeeae90SHong Zhang collengths[jj[i]]++; 1133b2fbd54SBarry Smith } 1143b2fbd54SBarry Smith cia[0] = oshift; 1153b2fbd54SBarry Smith for (i=0; i<n; i++) { 1163b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 1173b2fbd54SBarry Smith } 11897f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 1193b2fbd54SBarry Smith jj = a->j; 120a93ec695SBarry Smith for (row=0; row<m; row++) { 121a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 122a93ec695SBarry Smith for (i=0; i<mr; i++) { 123bfeeae90SHong Zhang col = *jj++; 1243b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 1253b2fbd54SBarry Smith } 1263b2fbd54SBarry Smith } 127606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 1283b2fbd54SBarry Smith *ia = cia; *ja = cja; 1293b2fbd54SBarry Smith } 1303a40ed3dSBarry Smith PetscFunctionReturn(0); 1313b2fbd54SBarry Smith } 1323b2fbd54SBarry Smith 1334a2ae208SSatish Balay #undef __FUNCT__ 1344a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ" 1358f7157efSSatish Balay PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 1363b2fbd54SBarry Smith { 137dfbe8321SBarry Smith PetscErrorCode ierr; 138606d414cSSatish Balay 1393a40ed3dSBarry Smith PetscFunctionBegin; 1403a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1413b2fbd54SBarry Smith 142606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 143606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 1443b2fbd54SBarry Smith 1453a40ed3dSBarry Smith PetscFunctionReturn(0); 1463b2fbd54SBarry Smith } 1473b2fbd54SBarry Smith 14887d4246cSBarry Smith #undef __FUNCT__ 14987d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ" 15087d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 15187d4246cSBarry Smith { 15287d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15387d4246cSBarry Smith PetscInt *ai = a->i; 15487d4246cSBarry Smith PetscErrorCode ierr; 15587d4246cSBarry Smith 15687d4246cSBarry Smith PetscFunctionBegin; 15787d4246cSBarry Smith ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr); 15887d4246cSBarry Smith PetscFunctionReturn(0); 15987d4246cSBarry Smith } 16087d4246cSBarry Smith 161227d817aSBarry Smith #define CHUNKSIZE 15 16217ab2063SBarry Smith 1634a2ae208SSatish Balay #undef __FUNCT__ 1644a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ" 16597f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 16617ab2063SBarry Smith { 167416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 168e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 16997f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 1706849ba73SBarry Smith PetscErrorCode ierr; 171e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 17287828ca2SBarry Smith PetscScalar *ap,value,*aa = a->a; 173edb03aefSBarry Smith PetscTruth ignorezeroentries = a->ignorezeroentries; 174273d9f13SBarry Smith PetscTruth roworiented = a->roworiented; 17517ab2063SBarry Smith 1763a40ed3dSBarry Smith PetscFunctionBegin; 17717ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 178416022c9SBarry Smith row = im[k]; 1795ef9f2a5SBarry Smith if (row < 0) continue; 1802515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 181899cda47SBarry Smith if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1); 1823b2fbd54SBarry Smith #endif 183bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 18417ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 185416022c9SBarry Smith low = 0; 186c71e6ed7SBarry Smith high = nrow; 18717ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 1885ef9f2a5SBarry Smith if (in[l] < 0) continue; 1892515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 190899cda47SBarry 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); 1913b2fbd54SBarry Smith #endif 192bfeeae90SHong Zhang col = in[l]; 1934b0e389bSBarry Smith if (roworiented) { 1945ef9f2a5SBarry Smith value = v[l + k*n]; 195bef8e0ddSBarry Smith } else { 1964b0e389bSBarry Smith value = v[k + l*m]; 1974b0e389bSBarry Smith } 198abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 19936db0b34SBarry Smith 2007cd84e04SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 201e2ee6c50SBarry Smith lastcol = col; 202416022c9SBarry Smith while (high-low > 5) { 203416022c9SBarry Smith t = (low+high)/2; 204416022c9SBarry Smith if (rp[t] > col) high = t; 205416022c9SBarry Smith else low = t; 20617ab2063SBarry Smith } 207416022c9SBarry Smith for (i=low; i<high; i++) { 20817ab2063SBarry Smith if (rp[i] > col) break; 20917ab2063SBarry Smith if (rp[i] == col) { 210416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 21117ab2063SBarry Smith else ap[i] = value; 21217ab2063SBarry Smith goto noinsert; 21317ab2063SBarry Smith } 21417ab2063SBarry Smith } 215abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 216c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 217cc72174dSBarry Smith if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 218421e10b8SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 219c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 220416022c9SBarry Smith /* shift up all the later entries in this row */ 221416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 22217ab2063SBarry Smith rp[ii+1] = rp[ii]; 22317ab2063SBarry Smith ap[ii+1] = ap[ii]; 22417ab2063SBarry Smith } 22517ab2063SBarry Smith rp[i] = col; 22617ab2063SBarry Smith ap[i] = value; 22717ab2063SBarry Smith noinsert:; 228416022c9SBarry Smith low = i + 1; 22917ab2063SBarry Smith } 23017ab2063SBarry Smith ailen[row] = nrow; 23117ab2063SBarry Smith } 23288e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 2333a40ed3dSBarry Smith PetscFunctionReturn(0); 23417ab2063SBarry Smith } 23517ab2063SBarry Smith 23681824310SBarry Smith 2374a2ae208SSatish Balay #undef __FUNCT__ 2384a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ" 23997f1f81fSBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 2407eb43aa7SLois Curfman McInnes { 2417eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 24297f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 24397f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 24497e567efSBarry Smith PetscScalar *ap,*aa = a->a; 2457eb43aa7SLois Curfman McInnes 2463a40ed3dSBarry Smith PetscFunctionBegin; 2477eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 2487eb43aa7SLois Curfman McInnes row = im[k]; 24997e567efSBarry Smith if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */ 250899cda47SBarry Smith if (row >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap.n-1); 251bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 2527eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2537eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 25497e567efSBarry Smith if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */ 255899cda47SBarry 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); 256bfeeae90SHong Zhang col = in[l] ; 2577eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2587eb43aa7SLois Curfman McInnes while (high-low > 5) { 2597eb43aa7SLois Curfman McInnes t = (low+high)/2; 2607eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2617eb43aa7SLois Curfman McInnes else low = t; 2627eb43aa7SLois Curfman McInnes } 2637eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 2647eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2657eb43aa7SLois Curfman McInnes if (rp[i] == col) { 266b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2677eb43aa7SLois Curfman McInnes goto finished; 2687eb43aa7SLois Curfman McInnes } 2697eb43aa7SLois Curfman McInnes } 27097e567efSBarry Smith *v++ = 0.0; 2717eb43aa7SLois Curfman McInnes finished:; 2727eb43aa7SLois Curfman McInnes } 2737eb43aa7SLois Curfman McInnes } 2743a40ed3dSBarry Smith PetscFunctionReturn(0); 2757eb43aa7SLois Curfman McInnes } 2767eb43aa7SLois Curfman McInnes 27717ab2063SBarry Smith 2784a2ae208SSatish Balay #undef __FUNCT__ 2794a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary" 280dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 28117ab2063SBarry Smith { 282416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2836849ba73SBarry Smith PetscErrorCode ierr; 2846f69ff64SBarry Smith PetscInt i,*col_lens; 2856f69ff64SBarry Smith int fd; 28617ab2063SBarry Smith 2873a40ed3dSBarry Smith PetscFunctionBegin; 288b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 289899cda47SBarry Smith ierr = PetscMalloc((4+A->rmap.n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr); 290552e946dSBarry Smith col_lens[0] = MAT_FILE_COOKIE; 291899cda47SBarry Smith col_lens[1] = A->rmap.n; 292899cda47SBarry Smith col_lens[2] = A->cmap.n; 293416022c9SBarry Smith col_lens[3] = a->nz; 294416022c9SBarry Smith 295416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 296899cda47SBarry Smith for (i=0; i<A->rmap.n; i++) { 297416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 29817ab2063SBarry Smith } 299899cda47SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap.n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 300606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 301416022c9SBarry Smith 302416022c9SBarry Smith /* store column indices (zero start index) */ 3036f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 304416022c9SBarry Smith 305416022c9SBarry Smith /* store nonzero values */ 3066f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 3073a40ed3dSBarry Smith PetscFunctionReturn(0); 30817ab2063SBarry Smith } 309416022c9SBarry Smith 310dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 311cd155464SBarry Smith 3124a2ae208SSatish Balay #undef __FUNCT__ 3134a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII" 314dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 315416022c9SBarry Smith { 316416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 317dfbe8321SBarry Smith PetscErrorCode ierr; 318899cda47SBarry Smith PetscInt i,j,m = A->rmap.n,shift=0; 319e060cb09SBarry Smith const char *name; 320f3ef73ceSBarry Smith PetscViewerFormat format; 32117ab2063SBarry Smith 3223a40ed3dSBarry Smith PetscFunctionBegin; 323435da068SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 324b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 32571c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 32697f1f81fSBarry Smith PetscInt nofinalvalue = 0; 327899cda47SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap.n-!shift)) { 328d00d2cf4SBarry Smith nofinalvalue = 1; 329d00d2cf4SBarry Smith } 330b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 331899cda47SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap.n);CHKERRQ(ierr); 33277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 33377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 334b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 33517ab2063SBarry Smith 33617ab2063SBarry Smith for (i=0; i<m; i++) { 337416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 338aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 33977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 34017ab2063SBarry Smith #else 34177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr); 34217ab2063SBarry Smith #endif 34317ab2063SBarry Smith } 34417ab2063SBarry Smith } 345d00d2cf4SBarry Smith if (nofinalvalue) { 346899cda47SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap.n,0.0);CHKERRQ(ierr); 347d00d2cf4SBarry Smith } 348fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 349b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 35068369a75SKris Buschelman } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) { 351cd155464SBarry Smith PetscFunctionReturn(0); 352fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 353b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 35444cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 35577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 35644cd7ae7SLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 357aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 35836db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 359a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 36036db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 361a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 36236db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 363a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 3646831982aSBarry Smith } 36544cd7ae7SLois Curfman McInnes #else 366a83599f4SBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);} 36744cd7ae7SLois Curfman McInnes #endif 36844cd7ae7SLois Curfman McInnes } 369b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 37044cd7ae7SLois Curfman McInnes } 371b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 372fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 37397f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 374b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 37597f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr); 376496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 377496be53dSLois Curfman McInnes sptr[i] = nzd+1; 378496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 379496be53dSLois Curfman McInnes if (a->j[j] >= i) { 380aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 38136db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 382496be53dSLois Curfman McInnes #else 383496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 384496be53dSLois Curfman McInnes #endif 385496be53dSLois Curfman McInnes } 386496be53dSLois Curfman McInnes } 387496be53dSLois Curfman McInnes } 3882e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 38977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 3902e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 39177431f27SBarry 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);} 39277431f27SBarry 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);} 39377431f27SBarry 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);} 39477431f27SBarry Smith else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);} 39577431f27SBarry Smith else if (i<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);} 39677431f27SBarry Smith else {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);} 397496be53dSLois Curfman McInnes } 398b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 399606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 400496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 401496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 40277431f27SBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);} 403496be53dSLois Curfman McInnes } 404b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 405496be53dSLois Curfman McInnes } 406b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");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++) { 409496be53dSLois Curfman McInnes if (a->j[j] >= i) { 410aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 41136db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 412b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4136831982aSBarry Smith } 414496be53dSLois Curfman McInnes #else 415b0a32e0cSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);} 416496be53dSLois Curfman McInnes #endif 417496be53dSLois Curfman McInnes } 418496be53dSLois Curfman McInnes } 419b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 420496be53dSLois Curfman McInnes } 421b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 422fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 42397f1f81fSBarry Smith PetscInt cnt = 0,jcnt; 42487828ca2SBarry Smith PetscScalar value; 42502594712SBarry Smith 426b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 42702594712SBarry Smith for (i=0; i<m; i++) { 42802594712SBarry Smith jcnt = 0; 429899cda47SBarry Smith for (j=0; j<A->cmap.n; j++) { 430e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 43102594712SBarry Smith value = a->a[cnt++]; 432e24b481bSBarry Smith jcnt++; 43302594712SBarry Smith } else { 43402594712SBarry Smith value = 0.0; 43502594712SBarry Smith } 436aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 437b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr); 43802594712SBarry Smith #else 439b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr); 44002594712SBarry Smith #endif 44102594712SBarry Smith } 442b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 44302594712SBarry Smith } 444b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 4453a40ed3dSBarry Smith } else { 446b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 44717ab2063SBarry Smith for (i=0; i<m; i++) { 44877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 449416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 450aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 45136db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 452a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 45336db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 454a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4553a40ed3dSBarry Smith } else { 456a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 45717ab2063SBarry Smith } 45817ab2063SBarry Smith #else 459a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 46017ab2063SBarry Smith #endif 46117ab2063SBarry Smith } 462b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 46317ab2063SBarry Smith } 464b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 46517ab2063SBarry Smith } 466b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 4673a40ed3dSBarry Smith PetscFunctionReturn(0); 468416022c9SBarry Smith } 469416022c9SBarry Smith 4704a2ae208SSatish Balay #undef __FUNCT__ 4714a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom" 472dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 473416022c9SBarry Smith { 474480ef9eaSBarry Smith Mat A = (Mat) Aa; 475416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 476dfbe8321SBarry Smith PetscErrorCode ierr; 477899cda47SBarry Smith PetscInt i,j,m = A->rmap.n,color; 47836db0b34SBarry Smith PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0; 479b0a32e0cSBarry Smith PetscViewer viewer; 480f3ef73ceSBarry Smith PetscViewerFormat format; 481cddf8d76SBarry Smith 4823a40ed3dSBarry Smith PetscFunctionBegin; 483480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 484b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 48519bcc07fSBarry Smith 486b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 487416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 4880513a670SBarry Smith 489fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 4900513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 491b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 492416022c9SBarry Smith for (i=0; i<m; i++) { 493cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 494bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 495bfeeae90SHong Zhang x_l = a->j[j] ; x_r = x_l + 1.0; 496aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 49736db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 498cddf8d76SBarry Smith #else 499cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 500cddf8d76SBarry Smith #endif 501b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 502cddf8d76SBarry Smith } 503cddf8d76SBarry Smith } 504b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 505cddf8d76SBarry Smith for (i=0; i<m; i++) { 506cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 507bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 508bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 509cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 510b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 511cddf8d76SBarry Smith } 512cddf8d76SBarry Smith } 513b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 514cddf8d76SBarry Smith for (i=0; i<m; i++) { 515cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 516bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 517bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 518aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 51936db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 520cddf8d76SBarry Smith #else 521cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 522cddf8d76SBarry Smith #endif 523b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 524416022c9SBarry Smith } 525416022c9SBarry Smith } 5260513a670SBarry Smith } else { 5270513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 5280513a670SBarry Smith /* first determine max of all nonzero values */ 52997f1f81fSBarry Smith PetscInt nz = a->nz,count; 530b0a32e0cSBarry Smith PetscDraw popup; 53136db0b34SBarry Smith PetscReal scale; 5320513a670SBarry Smith 5330513a670SBarry Smith for (i=0; i<nz; i++) { 5340513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 5350513a670SBarry Smith } 536b0a32e0cSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv; 537b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 538b0a32e0cSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);} 5390513a670SBarry Smith count = 0; 5400513a670SBarry Smith for (i=0; i<m; i++) { 5410513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 542bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 543bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 54497f1f81fSBarry Smith color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count])); 545b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 5460513a670SBarry Smith count++; 5470513a670SBarry Smith } 5480513a670SBarry Smith } 5490513a670SBarry Smith } 550480ef9eaSBarry Smith PetscFunctionReturn(0); 551480ef9eaSBarry Smith } 552cddf8d76SBarry Smith 5534a2ae208SSatish Balay #undef __FUNCT__ 5544a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw" 555dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 556480ef9eaSBarry Smith { 557dfbe8321SBarry Smith PetscErrorCode ierr; 558b0a32e0cSBarry Smith PetscDraw draw; 55936db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 560480ef9eaSBarry Smith PetscTruth isnull; 561480ef9eaSBarry Smith 562480ef9eaSBarry Smith PetscFunctionBegin; 563b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 564b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 565480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 566480ef9eaSBarry Smith 567480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 568899cda47SBarry Smith xr = A->cmap.n; yr = A->rmap.n; h = yr/10.0; w = xr/10.0; 569480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 570b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 571b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 572480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr); 5733a40ed3dSBarry Smith PetscFunctionReturn(0); 574416022c9SBarry Smith } 575416022c9SBarry Smith 5764a2ae208SSatish Balay #undef __FUNCT__ 5774a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ" 578dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 579416022c9SBarry Smith { 580dfbe8321SBarry Smith PetscErrorCode ierr; 5816805f65bSBarry Smith PetscTruth iascii,isbinary,isdraw; 582416022c9SBarry Smith 5833a40ed3dSBarry Smith PetscFunctionBegin; 58432077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 585fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr); 586fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr); 587c45a1595SBarry Smith if (iascii) { 5883a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 5890f5bd95cSBarry Smith } else if (isbinary) { 5903a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 5910f5bd95cSBarry Smith } else if (isdraw) { 5923a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 5935cd90555SBarry Smith } else { 594958c9bccSBarry Smith SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name); 59517ab2063SBarry Smith } 5964846f1f5SKris Buschelman ierr = MatView_Inode(A,viewer);CHKERRQ(ierr); 5973a40ed3dSBarry Smith PetscFunctionReturn(0); 59817ab2063SBarry Smith } 59919bcc07fSBarry Smith 6004a2ae208SSatish Balay #undef __FUNCT__ 6014a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ" 602dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 60317ab2063SBarry Smith { 604416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 6056849ba73SBarry Smith PetscErrorCode ierr; 60697f1f81fSBarry Smith PetscInt fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax; 607899cda47SBarry Smith PetscInt m = A->rmap.n,*ip,N,*ailen = a->ilen,rmax = 0; 60887828ca2SBarry Smith PetscScalar *aa = a->a,*ap; 6093447b6efSHong Zhang PetscReal ratio=0.6; 61017ab2063SBarry Smith 6113a40ed3dSBarry Smith PetscFunctionBegin; 6123a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 61317ab2063SBarry Smith 61443ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 61517ab2063SBarry Smith for (i=1; i<m; i++) { 616416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 61717ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 61894a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 61917ab2063SBarry Smith if (fshift) { 620bfeeae90SHong Zhang ip = aj + ai[i] ; 621bfeeae90SHong Zhang ap = aa + ai[i] ; 62217ab2063SBarry Smith N = ailen[i]; 62317ab2063SBarry Smith for (j=0; j<N; j++) { 62417ab2063SBarry Smith ip[j-fshift] = ip[j]; 62517ab2063SBarry Smith ap[j-fshift] = ap[j]; 62617ab2063SBarry Smith } 62717ab2063SBarry Smith } 62817ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 62917ab2063SBarry Smith } 63017ab2063SBarry Smith if (m) { 63117ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 63217ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 63317ab2063SBarry Smith } 63417ab2063SBarry Smith /* reset ilen and imax for each row */ 63517ab2063SBarry Smith for (i=0; i<m; i++) { 63617ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 63717ab2063SBarry Smith } 638bfeeae90SHong Zhang a->nz = ai[m]; 63917ab2063SBarry Smith 64009f38230SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 641899cda47SBarry 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); 642ae15b995SBarry Smith ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr); 643ae15b995SBarry Smith ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr); 644a0e1a203SBarry Smith 645dd5f02e7SSatish Balay a->reallocs = 0; 6464e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 64736db0b34SBarry Smith a->rmax = rmax; 6484e220ebcSLois Curfman McInnes 649cb5d8e9eSHong Zhang /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */ 650317fbc4cSHong Zhang ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 65188e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 65271c2f376SKris Buschelman 6534846f1f5SKris Buschelman ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr); 65471f1c65dSBarry Smith 65571f1c65dSBarry Smith a->idiagvalid = PETSC_FALSE; 6563a40ed3dSBarry Smith PetscFunctionReturn(0); 65717ab2063SBarry Smith } 65817ab2063SBarry Smith 6594a2ae208SSatish Balay #undef __FUNCT__ 66099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ" 66199cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 66299cafbc1SBarry Smith { 66399cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 66499cafbc1SBarry Smith PetscInt i,nz = a->nz; 66599cafbc1SBarry Smith PetscScalar *aa = a->a; 66699cafbc1SBarry Smith 66799cafbc1SBarry Smith PetscFunctionBegin; 66899cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 66999cafbc1SBarry Smith PetscFunctionReturn(0); 67099cafbc1SBarry Smith } 67199cafbc1SBarry Smith 67299cafbc1SBarry Smith #undef __FUNCT__ 67399cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ" 67499cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 67599cafbc1SBarry Smith { 67699cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 67799cafbc1SBarry Smith PetscInt i,nz = a->nz; 67899cafbc1SBarry Smith PetscScalar *aa = a->a; 67999cafbc1SBarry Smith 68099cafbc1SBarry Smith PetscFunctionBegin; 68199cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 68299cafbc1SBarry Smith PetscFunctionReturn(0); 68399cafbc1SBarry Smith } 68499cafbc1SBarry Smith 68599cafbc1SBarry Smith #undef __FUNCT__ 6864a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ" 687dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 68817ab2063SBarry Smith { 689416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 690dfbe8321SBarry Smith PetscErrorCode ierr; 6913a40ed3dSBarry Smith 6923a40ed3dSBarry Smith PetscFunctionBegin; 693899cda47SBarry Smith ierr = PetscMemzero(a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr); 6943a40ed3dSBarry Smith PetscFunctionReturn(0); 69517ab2063SBarry Smith } 696416022c9SBarry Smith 6974a2ae208SSatish Balay #undef __FUNCT__ 6984a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ" 699dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 70017ab2063SBarry Smith { 701416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 702dfbe8321SBarry Smith PetscErrorCode ierr; 703d5d45c9bSBarry Smith 7043a40ed3dSBarry Smith PetscFunctionBegin; 705aa482453SBarry Smith #if defined(PETSC_USE_LOG) 706899cda47SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap.n,A->cmap.n,a->nz); 70717ab2063SBarry Smith #endif 708e6b907acSBarry Smith ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr); 709c38d4ed2SBarry Smith if (a->row) { 710c38d4ed2SBarry Smith ierr = ISDestroy(a->row);CHKERRQ(ierr); 711c38d4ed2SBarry Smith } 712c38d4ed2SBarry Smith if (a->col) { 713c38d4ed2SBarry Smith ierr = ISDestroy(a->col);CHKERRQ(ierr); 714c38d4ed2SBarry Smith } 71505b42c5fSBarry Smith ierr = PetscFree(a->diag);CHKERRQ(ierr); 71605b42c5fSBarry Smith ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr); 71771f1c65dSBarry Smith ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr); 71805b42c5fSBarry Smith ierr = PetscFree(a->solve_work);CHKERRQ(ierr); 71982bf6240SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} 72005b42c5fSBarry Smith ierr = PetscFree(a->saved_values);CHKERRQ(ierr); 721cc8ba8e1SBarry Smith if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);} 72205b42c5fSBarry Smith ierr = PetscFree(a->xtoy);CHKERRQ(ierr); 723407f6b05SHong Zhang if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);} 724d487561eSHong Zhang if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);} 725a30b2313SHong Zhang 7264846f1f5SKris Buschelman ierr = MatDestroy_Inode(A);CHKERRQ(ierr); 7274846f1f5SKris Buschelman 728606d414cSSatish Balay ierr = PetscFree(a);CHKERRQ(ierr); 729901853e0SKris Buschelman 730dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr); 731901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr); 732901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 733901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 734901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr); 735901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr); 736ba03775dSHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr); 737901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 738901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 739a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 740901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr); 7413a40ed3dSBarry Smith PetscFunctionReturn(0); 74217ab2063SBarry Smith } 74317ab2063SBarry Smith 7444a2ae208SSatish Balay #undef __FUNCT__ 7454a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ" 746dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A) 74717ab2063SBarry Smith { 7483a40ed3dSBarry Smith PetscFunctionBegin; 7493a40ed3dSBarry Smith PetscFunctionReturn(0); 75017ab2063SBarry Smith } 75117ab2063SBarry Smith 7524a2ae208SSatish Balay #undef __FUNCT__ 7534a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ" 7544e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg) 75517ab2063SBarry Smith { 756416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 7574846f1f5SKris Buschelman PetscErrorCode ierr; 7583a40ed3dSBarry Smith 7593a40ed3dSBarry Smith PetscFunctionBegin; 760a65d3064SKris Buschelman switch (op) { 761a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 7624e0d8c25SBarry Smith a->roworiented = flg; 763a65d3064SKris Buschelman break; 764a65d3064SKris Buschelman case MAT_KEEP_ZEROED_ROWS: 7654e0d8c25SBarry Smith a->keepzeroedrows = flg; 766a65d3064SKris Buschelman break; 767512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 768512a5fc5SBarry Smith a->nonew = (flg ? 0 : 1); 769a65d3064SKris Buschelman break; 770a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 7714e0d8c25SBarry Smith a->nonew = (flg ? -1 : 0); 772a65d3064SKris Buschelman break; 773a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 7744e0d8c25SBarry Smith a->nonew = (flg ? -2 : 0); 775a65d3064SKris Buschelman break; 776a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 7774e0d8c25SBarry Smith a->ignorezeroentries = flg; 7780df259c2SBarry Smith break; 779d487561eSHong Zhang case MAT_USE_COMPRESSEDROW: 7804e0d8c25SBarry Smith a->compressedrow.use = flg; 781d487561eSHong Zhang break; 7824e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 783a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 784a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 785290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 786a65d3064SKris Buschelman break; 787a65d3064SKris Buschelman default: 78871c2f376SKris Buschelman break; 789a65d3064SKris Buschelman } 7904e0d8c25SBarry Smith ierr = MatSetOption_Inode(A,op,flg);CHKERRQ(ierr); 7913a40ed3dSBarry Smith PetscFunctionReturn(0); 79217ab2063SBarry Smith } 79317ab2063SBarry Smith 7944a2ae208SSatish Balay #undef __FUNCT__ 7954a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ" 796dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 79717ab2063SBarry Smith { 798416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 7996849ba73SBarry Smith PetscErrorCode ierr; 80097f1f81fSBarry Smith PetscInt i,j,n; 80187828ca2SBarry Smith PetscScalar *x,zero = 0.0; 80217ab2063SBarry Smith 8033a40ed3dSBarry Smith PetscFunctionBegin; 8042dcb1b2aSMatthew Knepley ierr = VecSet(v,zero);CHKERRQ(ierr); 8051ebc52fbSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 80636db0b34SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 807899cda47SBarry Smith if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 808899cda47SBarry Smith for (i=0; i<A->rmap.n; i++) { 809bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 810bfeeae90SHong Zhang if (a->j[j] == i) { 811416022c9SBarry Smith x[i] = a->a[j]; 81217ab2063SBarry Smith break; 81317ab2063SBarry Smith } 81417ab2063SBarry Smith } 81517ab2063SBarry Smith } 8161ebc52fbSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 8173a40ed3dSBarry Smith PetscFunctionReturn(0); 81817ab2063SBarry Smith } 81917ab2063SBarry Smith 8204a2ae208SSatish Balay #undef __FUNCT__ 8214a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ" 822dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 82317ab2063SBarry Smith { 824416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8255c897100SBarry Smith PetscScalar *x,*y; 826dfbe8321SBarry Smith PetscErrorCode ierr; 827899cda47SBarry Smith PetscInt m = A->rmap.n; 8285c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 8295c897100SBarry Smith PetscScalar *v,alpha; 8307b2bb3b9SHong Zhang PetscInt n,i,*idx,*ii,*ridx=PETSC_NULL; 8313447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 8324eb6d288SHong Zhang PetscTruth usecprow = cprow.use; 8335c897100SBarry Smith #endif 83417ab2063SBarry Smith 8353a40ed3dSBarry Smith PetscFunctionBegin; 8362e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 8371ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 8381ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 8395c897100SBarry Smith 8405c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 841bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 8425c897100SBarry Smith #else 8433447b6efSHong Zhang if (usecprow){ 8443447b6efSHong Zhang m = cprow.nrows; 8453447b6efSHong Zhang ii = cprow.i; 8467b2bb3b9SHong Zhang ridx = cprow.rindex; 8473447b6efSHong Zhang } else { 8483447b6efSHong Zhang ii = a->i; 8493447b6efSHong Zhang } 85017ab2063SBarry Smith for (i=0; i<m; i++) { 8513447b6efSHong Zhang idx = a->j + ii[i] ; 8523447b6efSHong Zhang v = a->a + ii[i] ; 8533447b6efSHong Zhang n = ii[i+1] - ii[i]; 8543447b6efSHong Zhang if (usecprow){ 8557b2bb3b9SHong Zhang alpha = x[ridx[i]]; 8563447b6efSHong Zhang } else { 85717ab2063SBarry Smith alpha = x[i]; 8583447b6efSHong Zhang } 85917ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 86017ab2063SBarry Smith } 8615c897100SBarry Smith #endif 862efee365bSSatish Balay ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr); 8631ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 8641ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 8653a40ed3dSBarry Smith PetscFunctionReturn(0); 86617ab2063SBarry Smith } 86717ab2063SBarry Smith 8684a2ae208SSatish Balay #undef __FUNCT__ 8695c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ" 870dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 8715c897100SBarry Smith { 8728d5b0100SBarry Smith PetscScalar zero = 0.0; 873dfbe8321SBarry Smith PetscErrorCode ierr; 8745c897100SBarry Smith 8755c897100SBarry Smith PetscFunctionBegin; 8762dcb1b2aSMatthew Knepley ierr = VecSet(yy,zero);CHKERRQ(ierr); 8775c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 8785c897100SBarry Smith PetscFunctionReturn(0); 8795c897100SBarry Smith } 8805c897100SBarry Smith 8815c897100SBarry Smith 8825c897100SBarry Smith #undef __FUNCT__ 8834a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ" 884dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 88517ab2063SBarry Smith { 886416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 887*d9fead3dSBarry Smith PetscScalar *y; 888*d9fead3dSBarry Smith const PetscScalar *x,*aa; 889dfbe8321SBarry Smith PetscErrorCode ierr; 890899cda47SBarry Smith PetscInt m=A->rmap.n,*aj,*ii; 891a46b3154SVictor Eijkhout PetscInt n,i,j,nonzerorow=0,*ridx=PETSC_NULL; 892362ced78SSatish Balay PetscScalar sum; 89397952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 894f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 895f2e71c43SSatish Balay PetscInt jrow; 896e36a17ebSSatish Balay #endif 89717ab2063SBarry Smith 898b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 89997952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 900fee21e36SBarry Smith #endif 901fee21e36SBarry Smith 9023a40ed3dSBarry Smith PetscFunctionBegin; 903*d9fead3dSBarry Smith ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr); 9041ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 90597952fefSHong Zhang aj = a->j; 90697952fefSHong Zhang aa = a->a; 907416022c9SBarry Smith ii = a->i; 9084eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 90997952fefSHong Zhang m = a->compressedrow.nrows; 91097952fefSHong Zhang ii = a->compressedrow.i; 91197952fefSHong Zhang ridx = a->compressedrow.rindex; 91297952fefSHong Zhang for (i=0; i<m; i++){ 91397952fefSHong Zhang n = ii[i+1] - ii[i]; 91497952fefSHong Zhang aj = a->j + ii[i]; 91597952fefSHong Zhang aa = a->a + ii[i]; 91697952fefSHong Zhang sum = 0.0; 917a46b3154SVictor Eijkhout nonzerorow += (n>0); 91897952fefSHong Zhang for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; 91997952fefSHong Zhang y[*ridx++] = sum; 92097952fefSHong Zhang } 92197952fefSHong Zhang } else { /* do not use compressed row format */ 922b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 923b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 924b05257ddSBarry Smith #else 92517ab2063SBarry Smith for (i=0; i<m; i++) { 9269ea0dfa2SSatish Balay jrow = ii[i]; 9279ea0dfa2SSatish Balay n = ii[i+1] - jrow; 92817ab2063SBarry Smith sum = 0.0; 929a46b3154SVictor Eijkhout nonzerorow += (n>0); 9309ea0dfa2SSatish Balay for (j=0; j<n; j++) { 93197952fefSHong Zhang sum += aa[jrow]*x[aj[jrow]]; jrow++; 9329ea0dfa2SSatish Balay } 93317ab2063SBarry Smith y[i] = sum; 93417ab2063SBarry Smith } 9358d195f9aSBarry Smith #endif 936b05257ddSBarry Smith } 937a46b3154SVictor Eijkhout ierr = PetscLogFlops(2*a->nz - nonzerorow);CHKERRQ(ierr); 938*d9fead3dSBarry Smith ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr); 9391ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9403a40ed3dSBarry Smith PetscFunctionReturn(0); 94117ab2063SBarry Smith } 94217ab2063SBarry Smith 9434a2ae208SSatish Balay #undef __FUNCT__ 9444a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ" 945dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 94617ab2063SBarry Smith { 947416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 94897952fefSHong Zhang PetscScalar *x,*y,*z,*aa; 949dfbe8321SBarry Smith PetscErrorCode ierr; 950899cda47SBarry Smith PetscInt m = A->rmap.n,*aj,*ii; 951aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 95297952fefSHong Zhang PetscInt n,i,jrow,j,*ridx=PETSC_NULL; 953362ced78SSatish Balay PetscScalar sum; 95497952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 955e36a17ebSSatish Balay #endif 9569ea0dfa2SSatish Balay 9573a40ed3dSBarry Smith PetscFunctionBegin; 9581ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 9591ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 9602e8a6d31SBarry Smith if (zz != yy) { 9611ebc52fbSHong Zhang ierr = VecGetArray(zz,&z);CHKERRQ(ierr); 9622e8a6d31SBarry Smith } else { 9632e8a6d31SBarry Smith z = y; 9642e8a6d31SBarry Smith } 965bfeeae90SHong Zhang 96697952fefSHong Zhang aj = a->j; 96797952fefSHong Zhang aa = a->a; 968cddf8d76SBarry Smith ii = a->i; 969aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 97097952fefSHong Zhang fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 97102ab625aSSatish Balay #else 9724eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 9734eb6d288SHong Zhang if (zz != yy){ 9744eb6d288SHong Zhang ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr); 9754eb6d288SHong Zhang } 97697952fefSHong Zhang m = a->compressedrow.nrows; 97797952fefSHong Zhang ii = a->compressedrow.i; 97897952fefSHong Zhang ridx = a->compressedrow.rindex; 97997952fefSHong Zhang for (i=0; i<m; i++){ 98097952fefSHong Zhang n = ii[i+1] - ii[i]; 98197952fefSHong Zhang aj = a->j + ii[i]; 98297952fefSHong Zhang aa = a->a + ii[i]; 98397952fefSHong Zhang sum = y[*ridx]; 98497952fefSHong Zhang for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; 98597952fefSHong Zhang z[*ridx++] = sum; 98697952fefSHong Zhang } 98797952fefSHong Zhang } else { /* do not use compressed row format */ 98817ab2063SBarry Smith for (i=0; i<m; i++) { 9899ea0dfa2SSatish Balay jrow = ii[i]; 9909ea0dfa2SSatish Balay n = ii[i+1] - jrow; 99117ab2063SBarry Smith sum = y[i]; 9929ea0dfa2SSatish Balay for (j=0; j<n; j++) { 99397952fefSHong Zhang sum += aa[jrow]*x[aj[jrow]]; jrow++; 9949ea0dfa2SSatish Balay } 99517ab2063SBarry Smith z[i] = sum; 99617ab2063SBarry Smith } 99797952fefSHong Zhang } 99802ab625aSSatish Balay #endif 999efee365bSSatish Balay ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr); 10001ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 10011ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 10022e8a6d31SBarry Smith if (zz != yy) { 10031ebc52fbSHong Zhang ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr); 10042e8a6d31SBarry Smith } 10053a40ed3dSBarry Smith PetscFunctionReturn(0); 100617ab2063SBarry Smith } 100717ab2063SBarry Smith 100817ab2063SBarry Smith /* 100917ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 101017ab2063SBarry Smith */ 10114a2ae208SSatish Balay #undef __FUNCT__ 10124a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ" 1013dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 101417ab2063SBarry Smith { 1015416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 10166849ba73SBarry Smith PetscErrorCode ierr; 101709f38230SBarry Smith PetscInt i,j,m = A->rmap.n; 101817ab2063SBarry Smith 10193a40ed3dSBarry Smith PetscFunctionBegin; 102009f38230SBarry Smith if (!a->diag) { 102109f38230SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr); 10229518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr); 102309f38230SBarry Smith } 1024899cda47SBarry Smith for (i=0; i<A->rmap.n; i++) { 102509f38230SBarry Smith a->diag[i] = a->i[i+1]; 1026bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1027bfeeae90SHong Zhang if (a->j[j] == i) { 102809f38230SBarry Smith a->diag[i] = j; 102917ab2063SBarry Smith break; 103017ab2063SBarry Smith } 103117ab2063SBarry Smith } 103217ab2063SBarry Smith } 10333a40ed3dSBarry Smith PetscFunctionReturn(0); 103417ab2063SBarry Smith } 103517ab2063SBarry Smith 1036be5855fcSBarry Smith /* 1037be5855fcSBarry Smith Checks for missing diagonals 1038be5855fcSBarry Smith */ 10394a2ae208SSatish Balay #undef __FUNCT__ 10404a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ" 104109f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d) 1042be5855fcSBarry Smith { 1043be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 104497f1f81fSBarry Smith PetscInt *diag,*jj = a->j,i; 1045be5855fcSBarry Smith 1046be5855fcSBarry Smith PetscFunctionBegin; 104709f38230SBarry Smith *missing = PETSC_FALSE; 104809f38230SBarry Smith if (A->rmap.n > 0 && !jj) { 104909f38230SBarry Smith *missing = PETSC_TRUE; 105009f38230SBarry Smith if (d) *d = 0; 105109f38230SBarry Smith PetscInfo(A,"Matrix has no entries therefor is missing diagonal"); 105209f38230SBarry Smith } else { 1053f1e2ffcdSBarry Smith diag = a->diag; 1054899cda47SBarry Smith for (i=0; i<A->rmap.n; i++) { 1055bfeeae90SHong Zhang if (jj[diag[i]] != i) { 105609f38230SBarry Smith *missing = PETSC_TRUE; 105709f38230SBarry Smith if (d) *d = i; 105809f38230SBarry Smith PetscInfo1(A,"Matrix is missing diagonal number %D",i); 105909f38230SBarry Smith } 1060be5855fcSBarry Smith } 1061be5855fcSBarry Smith } 1062be5855fcSBarry Smith PetscFunctionReturn(0); 1063be5855fcSBarry Smith } 1064be5855fcSBarry Smith 106571f1c65dSBarry Smith EXTERN_C_BEGIN 106671f1c65dSBarry Smith #undef __FUNCT__ 106771f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ" 106871f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift) 106971f1c65dSBarry Smith { 107071f1c65dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 107171f1c65dSBarry Smith PetscErrorCode ierr; 107271f1c65dSBarry Smith PetscInt i,*diag,m = A->rmap.n; 107371f1c65dSBarry Smith PetscScalar *v = a->a,*idiag,*mdiag; 107471f1c65dSBarry Smith 107571f1c65dSBarry Smith PetscFunctionBegin; 107671f1c65dSBarry Smith if (a->idiagvalid) PetscFunctionReturn(0); 107771f1c65dSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 107871f1c65dSBarry Smith diag = a->diag; 107971f1c65dSBarry Smith if (!a->idiag) { 108071f1c65dSBarry Smith ierr = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr); 108171f1c65dSBarry Smith ierr = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr); 108271f1c65dSBarry Smith v = a->a; 108371f1c65dSBarry Smith } 108471f1c65dSBarry Smith mdiag = a->mdiag; 108571f1c65dSBarry Smith idiag = a->idiag; 108671f1c65dSBarry Smith 1087028cd4eaSSatish Balay if (omega == 1.0 && !PetscAbsScalar(fshift)) { 108871f1c65dSBarry Smith for (i=0; i<m; i++) { 108971f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 109071f1c65dSBarry Smith if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i); 109171f1c65dSBarry Smith idiag[i] = 1.0/v[diag[i]]; 109271f1c65dSBarry Smith } 109371f1c65dSBarry Smith ierr = PetscLogFlops(m);CHKERRQ(ierr); 109471f1c65dSBarry Smith } else { 109571f1c65dSBarry Smith for (i=0; i<m; i++) { 109671f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 109771f1c65dSBarry Smith idiag[i] = omega/(fshift + v[diag[i]]); 109871f1c65dSBarry Smith } 109971f1c65dSBarry Smith ierr = PetscLogFlops(2*m);CHKERRQ(ierr); 110071f1c65dSBarry Smith } 110171f1c65dSBarry Smith a->idiagvalid = PETSC_TRUE; 110271f1c65dSBarry Smith PetscFunctionReturn(0); 110371f1c65dSBarry Smith } 11045a9745a3SMatthew Knepley EXTERN_C_END 110571f1c65dSBarry Smith 11064a2ae208SSatish Balay #undef __FUNCT__ 11074a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ" 110897f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 110917ab2063SBarry Smith { 1110416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1111beeb8507SBarry Smith PetscScalar *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag; 1112beeb8507SBarry Smith const PetscScalar *v = a->a, *b, *bs,*xb, *ts; 1113dfbe8321SBarry Smith PetscErrorCode ierr; 1114899cda47SBarry Smith PetscInt n = A->cmap.n,m = A->rmap.n,i; 111597f1f81fSBarry Smith const PetscInt *idx,*diag; 111617ab2063SBarry Smith 11173a40ed3dSBarry Smith PetscFunctionBegin; 1118b965ef7fSBarry Smith its = its*lits; 111991723122SBarry Smith 112071f1c65dSBarry Smith if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */ 112171f1c65dSBarry Smith if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);} 112271f1c65dSBarry Smith a->fshift = fshift; 112371f1c65dSBarry Smith a->omega = omega; 1124ed480e8bSBarry Smith 112571f1c65dSBarry Smith diag = a->diag; 112671f1c65dSBarry Smith t = a->ssor_work; 1127ed480e8bSBarry Smith idiag = a->idiag; 112871f1c65dSBarry Smith mdiag = a->mdiag; 1129ed480e8bSBarry Smith 11301ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1131fb2e594dSBarry Smith if (xx != bb) { 11321ebc52fbSHong Zhang ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1133fb2e594dSBarry Smith } else { 1134fb2e594dSBarry Smith b = x; 1135fb2e594dSBarry Smith } 113671f1c65dSBarry Smith CHKMEMQ; 1137ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 1138ed480e8bSBarry Smith xs = x; 113917ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 114017ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1141ed480e8bSBarry Smith bs = b; 114217ab2063SBarry Smith for (i=0; i<m; i++) { 114371f1c65dSBarry Smith d = fshift + mdiag[i]; 1144416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1145ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1146ed480e8bSBarry Smith v = a->a + diag[i] + 1; 114717ab2063SBarry Smith sum = b[i]*d/omega; 114817ab2063SBarry Smith SPARSEDENSEDOT(sum,bs,v,idx,n); 114917ab2063SBarry Smith x[i] = sum; 115017ab2063SBarry Smith } 11511ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 11521ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 1153efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 11543a40ed3dSBarry Smith PetscFunctionReturn(0); 115517ab2063SBarry Smith } 1156c783ea89SBarry Smith 115748af12d7SBarry Smith if (flag == SOR_APPLY_LOWER) { 115829bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 11593a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 116017ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 116117ab2063SBarry Smith U is upper triangular, E is diagonal; This routine applies 116217ab2063SBarry Smith 116317ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 116417ab2063SBarry Smith 116517ab2063SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 116617ab2063SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 116717ab2063SBarry Smith is the relaxation factor. 116817ab2063SBarry Smith */ 116917ab2063SBarry Smith scale = (2.0/omega) - 1.0; 117017ab2063SBarry Smith 117117ab2063SBarry Smith /* x = (E + U)^{-1} b */ 117217ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1173416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1174ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1175ed480e8bSBarry Smith v = a->a + diag[i] + 1; 117617ab2063SBarry Smith sum = b[i]; 117717ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1178ed480e8bSBarry Smith x[i] = sum*idiag[i]; 117917ab2063SBarry Smith } 118017ab2063SBarry Smith 118117ab2063SBarry Smith /* t = b - (2*E - D)x */ 1182416022c9SBarry Smith v = a->a; 1183ed480e8bSBarry Smith for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; } 118417ab2063SBarry Smith 118517ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1186ed480e8bSBarry Smith ts = t; 1187416022c9SBarry Smith diag = a->diag; 118817ab2063SBarry Smith for (i=0; i<m; i++) { 1189416022c9SBarry Smith n = diag[i] - a->i[i]; 1190ed480e8bSBarry Smith idx = a->j + a->i[i]; 1191ed480e8bSBarry Smith v = a->a + a->i[i]; 119217ab2063SBarry Smith sum = t[i]; 119317ab2063SBarry Smith SPARSEDENSEMDOT(sum,ts,v,idx,n); 1194ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1195733d66baSBarry Smith /* x = x + t */ 1196733d66baSBarry Smith x[i] += t[i]; 119717ab2063SBarry Smith } 119817ab2063SBarry Smith 1199efee365bSSatish Balay ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr); 12001ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 12011ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 12023a40ed3dSBarry Smith PetscFunctionReturn(0); 120317ab2063SBarry Smith } 120417ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 120517ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 120677d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 120797f1f81fSBarry Smith fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b); 120877d8c4bbSBarry Smith #else 120917ab2063SBarry Smith for (i=0; i<m; i++) { 1210416022c9SBarry Smith n = diag[i] - a->i[i]; 1211ed480e8bSBarry Smith idx = a->j + a->i[i]; 1212ed480e8bSBarry Smith v = a->a + a->i[i]; 121317ab2063SBarry Smith sum = b[i]; 121417ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1215ed480e8bSBarry Smith x[i] = sum*idiag[i]; 121617ab2063SBarry Smith } 121777d8c4bbSBarry Smith #endif 121817ab2063SBarry Smith xb = x; 1219efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 12203a40ed3dSBarry Smith } else xb = b; 122117ab2063SBarry Smith if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) && 122217ab2063SBarry Smith (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) { 122317ab2063SBarry Smith for (i=0; i<m; i++) { 1224ed480e8bSBarry Smith x[i] *= mdiag[i]; 122517ab2063SBarry Smith } 1226efee365bSSatish Balay ierr = PetscLogFlops(m);CHKERRQ(ierr); 122717ab2063SBarry Smith } 122817ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 122977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 123097f1f81fSBarry Smith fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb); 123177d8c4bbSBarry Smith #else 123217ab2063SBarry Smith for (i=m-1; i>=0; 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 = xb[i]; 123717ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1238ed480e8bSBarry Smith x[i] = sum*idiag[i]; 123917ab2063SBarry Smith } 124077d8c4bbSBarry Smith #endif 1241efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 124217ab2063SBarry Smith } 124317ab2063SBarry Smith its--; 124417ab2063SBarry Smith } 124517ab2063SBarry Smith while (its--) { 124617ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 124777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 124897f1f81fSBarry Smith fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b); 124977d8c4bbSBarry Smith #else 125017ab2063SBarry Smith for (i=0; i<m; i++) { 1251416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1252ed480e8bSBarry Smith idx = a->j + a->i[i]; 1253ed480e8bSBarry Smith v = a->a + a->i[i]; 125417ab2063SBarry Smith sum = b[i]; 125517ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1256ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 125717ab2063SBarry Smith } 125877d8c4bbSBarry Smith #endif 1259efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 126017ab2063SBarry Smith } 126117ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 126277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 126397f1f81fSBarry Smith fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b); 126477d8c4bbSBarry Smith #else 126517ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1266416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1267ed480e8bSBarry Smith idx = a->j + a->i[i]; 1268ed480e8bSBarry Smith v = a->a + a->i[i]; 126917ab2063SBarry Smith sum = b[i]; 127017ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1271ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 127217ab2063SBarry Smith } 127377d8c4bbSBarry Smith #endif 1274efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 127517ab2063SBarry Smith } 127617ab2063SBarry Smith } 12771ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 12781ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 127971f1c65dSBarry Smith CHKMEMQ; PetscFunctionReturn(0); 128017ab2063SBarry Smith } 128117ab2063SBarry Smith 12822af78befSBarry Smith 12834a2ae208SSatish Balay #undef __FUNCT__ 12844a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ" 1285dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 128617ab2063SBarry Smith { 1287416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12884e220ebcSLois Curfman McInnes 12893a40ed3dSBarry Smith PetscFunctionBegin; 1290899cda47SBarry Smith info->rows_global = (double)A->rmap.n; 1291899cda47SBarry Smith info->columns_global = (double)A->cmap.n; 1292899cda47SBarry Smith info->rows_local = (double)A->rmap.n; 1293899cda47SBarry Smith info->columns_local = (double)A->cmap.n; 12944e220ebcSLois Curfman McInnes info->block_size = 1.0; 12954e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 12964e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 12974e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 12984e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 12994e220ebcSLois Curfman McInnes info->mallocs = (double)a->reallocs; 13007adad957SLisandro Dalcin info->memory = ((PetscObject)A)->mem; 13014e220ebcSLois Curfman McInnes if (A->factor) { 13024e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 13034e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 13044e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 13054e220ebcSLois Curfman McInnes } else { 13064e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 13074e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 13084e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 13094e220ebcSLois Curfman McInnes } 13103a40ed3dSBarry Smith PetscFunctionReturn(0); 131117ab2063SBarry Smith } 131217ab2063SBarry Smith 13134a2ae208SSatish Balay #undef __FUNCT__ 13144a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ" 1315f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag) 131617ab2063SBarry Smith { 1317416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 131809f38230SBarry Smith PetscInt i,m = A->rmap.n - 1,d; 13196849ba73SBarry Smith PetscErrorCode ierr; 132009f38230SBarry Smith PetscTruth missing; 132117ab2063SBarry Smith 13223a40ed3dSBarry Smith PetscFunctionBegin; 1323f1e2ffcdSBarry Smith if (a->keepzeroedrows) { 1324f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 132577431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1326bfeeae90SHong Zhang ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1327f1e2ffcdSBarry Smith } 1328f4df32b1SMatthew Knepley if (diag != 0.0) { 132909f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 133009f38230SBarry Smith if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d); 1331f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1332f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 1333f1e2ffcdSBarry Smith } 1334f1e2ffcdSBarry Smith } 133588e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 1336f1e2ffcdSBarry Smith } else { 1337f4df32b1SMatthew Knepley if (diag != 0.0) { 133817ab2063SBarry Smith for (i=0; i<N; i++) { 133977431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 13407ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1341416022c9SBarry Smith a->ilen[rows[i]] = 1; 1342f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 1343bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 13447ae801bdSBarry Smith } else { /* in case row was completely empty */ 1345f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 134617ab2063SBarry Smith } 134717ab2063SBarry Smith } 13483a40ed3dSBarry Smith } else { 134917ab2063SBarry Smith for (i=0; i<N; i++) { 135077431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1351416022c9SBarry Smith a->ilen[rows[i]] = 0; 135217ab2063SBarry Smith } 135317ab2063SBarry Smith } 135488e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 1355f1e2ffcdSBarry Smith } 135643a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13573a40ed3dSBarry Smith PetscFunctionReturn(0); 135817ab2063SBarry Smith } 135917ab2063SBarry Smith 13604a2ae208SSatish Balay #undef __FUNCT__ 13614a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ" 136297f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 136317ab2063SBarry Smith { 1364416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 136597f1f81fSBarry Smith PetscInt *itmp; 136617ab2063SBarry Smith 13673a40ed3dSBarry Smith PetscFunctionBegin; 1368899cda47SBarry Smith if (row < 0 || row >= A->rmap.n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 136917ab2063SBarry Smith 1370416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1371bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 137217ab2063SBarry Smith if (idx) { 1373bfeeae90SHong Zhang itmp = a->j + a->i[row]; 1374bfeeae90SHong Zhang if (*nz) { 13754e093b46SBarry Smith *idx = itmp; 137617ab2063SBarry Smith } 137717ab2063SBarry Smith else *idx = 0; 137817ab2063SBarry Smith } 13793a40ed3dSBarry Smith PetscFunctionReturn(0); 138017ab2063SBarry Smith } 138117ab2063SBarry Smith 1382bfeeae90SHong Zhang /* remove this function? */ 13834a2ae208SSatish Balay #undef __FUNCT__ 13844a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ" 138597f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 138617ab2063SBarry Smith { 13873a40ed3dSBarry Smith PetscFunctionBegin; 13883a40ed3dSBarry Smith PetscFunctionReturn(0); 138917ab2063SBarry Smith } 139017ab2063SBarry Smith 13914a2ae208SSatish Balay #undef __FUNCT__ 13924a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ" 1393dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 139417ab2063SBarry Smith { 1395416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 139687828ca2SBarry Smith PetscScalar *v = a->a; 139736db0b34SBarry Smith PetscReal sum = 0.0; 13986849ba73SBarry Smith PetscErrorCode ierr; 139997f1f81fSBarry Smith PetscInt i,j; 140017ab2063SBarry Smith 14013a40ed3dSBarry Smith PetscFunctionBegin; 140217ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1403416022c9SBarry Smith for (i=0; i<a->nz; i++) { 1404aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 140536db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 140617ab2063SBarry Smith #else 140717ab2063SBarry Smith sum += (*v)*(*v); v++; 140817ab2063SBarry Smith #endif 140917ab2063SBarry Smith } 1410064f8208SBarry Smith *nrm = sqrt(sum); 14113a40ed3dSBarry Smith } else if (type == NORM_1) { 141236db0b34SBarry Smith PetscReal *tmp; 141397f1f81fSBarry Smith PetscInt *jj = a->j; 1414899cda47SBarry Smith ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1415899cda47SBarry Smith ierr = PetscMemzero(tmp,A->cmap.n*sizeof(PetscReal));CHKERRQ(ierr); 1416064f8208SBarry Smith *nrm = 0.0; 1417416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1418bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 141917ab2063SBarry Smith } 1420899cda47SBarry Smith for (j=0; j<A->cmap.n; j++) { 1421064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 142217ab2063SBarry Smith } 1423606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 14243a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1425064f8208SBarry Smith *nrm = 0.0; 1426899cda47SBarry Smith for (j=0; j<A->rmap.n; j++) { 1427bfeeae90SHong Zhang v = a->a + a->i[j]; 142817ab2063SBarry Smith sum = 0.0; 1429416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1430cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 143117ab2063SBarry Smith } 1432064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 143317ab2063SBarry Smith } 14343a40ed3dSBarry Smith } else { 143529bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"No support for two norm"); 143617ab2063SBarry Smith } 14373a40ed3dSBarry Smith PetscFunctionReturn(0); 143817ab2063SBarry Smith } 143917ab2063SBarry Smith 14404a2ae208SSatish Balay #undef __FUNCT__ 14414a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ" 1442dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B) 144317ab2063SBarry Smith { 1444416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1445416022c9SBarry Smith Mat C; 14466849ba73SBarry Smith PetscErrorCode ierr; 1447899cda47SBarry Smith PetscInt i,*aj = a->j,*ai = a->i,m = A->rmap.n,len,*col; 144887828ca2SBarry Smith PetscScalar *array = a->a; 144917ab2063SBarry Smith 14503a40ed3dSBarry Smith PetscFunctionBegin; 1451899cda47SBarry Smith if (!B && m != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1452899cda47SBarry Smith ierr = PetscMalloc((1+A->cmap.n)*sizeof(PetscInt),&col);CHKERRQ(ierr); 1453899cda47SBarry Smith ierr = PetscMemzero(col,(1+A->cmap.n)*sizeof(PetscInt));CHKERRQ(ierr); 1454bfeeae90SHong Zhang 1455bfeeae90SHong Zhang for (i=0; i<ai[m]; i++) col[aj[i]] += 1; 14567adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1457899cda47SBarry Smith ierr = MatSetSizes(C,A->cmap.n,m,A->cmap.n,m);CHKERRQ(ierr); 14587adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1459ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr); 1460606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 146117ab2063SBarry Smith for (i=0; i<m; i++) { 146217ab2063SBarry Smith len = ai[i+1]-ai[i]; 146387d4246cSBarry Smith ierr = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 1464b9b97703SBarry Smith array += len; 1465b9b97703SBarry Smith aj += len; 146617ab2063SBarry Smith } 146717ab2063SBarry Smith 14686d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14696d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 147017ab2063SBarry Smith 1471f1e2ffcdSBarry Smith if (B) { 1472416022c9SBarry Smith *B = C; 147317ab2063SBarry Smith } else { 1474273d9f13SBarry Smith ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 147517ab2063SBarry Smith } 14763a40ed3dSBarry Smith PetscFunctionReturn(0); 147717ab2063SBarry Smith } 147817ab2063SBarry Smith 1479cd0d46ebSvictorle EXTERN_C_BEGIN 1480cd0d46ebSvictorle #undef __FUNCT__ 14815fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ" 1482be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f) 1483cd0d46ebSvictorle { 1484cd0d46ebSvictorle Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 148597f1f81fSBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb; 14866849ba73SBarry Smith PetscErrorCode ierr; 148797f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 1488cd0d46ebSvictorle 1489cd0d46ebSvictorle PetscFunctionBegin; 1490cd0d46ebSvictorle bij = (Mat_SeqAIJ *) B->data; 1491cd0d46ebSvictorle 1492cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 1493cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 14945485867bSBarry Smith if (ma!=nb || na!=mb){ 14955485867bSBarry Smith *f = PETSC_FALSE; 14965485867bSBarry Smith PetscFunctionReturn(0); 14975485867bSBarry Smith } 1498cd0d46ebSvictorle aii = aij->i; bii = bij->i; 1499cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 1500cd0d46ebSvictorle va = aij->a; vb = bij->a; 150197f1f81fSBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 150297f1f81fSBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 1503cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 1504cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 1505cd0d46ebSvictorle 1506cd0d46ebSvictorle *f = PETSC_TRUE; 1507cd0d46ebSvictorle for (i=0; i<ma; i++) { 1508cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 150997f1f81fSBarry Smith PetscInt idc,idr; 15105485867bSBarry Smith PetscScalar vc,vr; 1511cd0d46ebSvictorle /* column/row index/value */ 15125485867bSBarry Smith idc = adx[aptr[i]]; 15135485867bSBarry Smith idr = bdx[bptr[idc]]; 15145485867bSBarry Smith vc = va[aptr[i]]; 15155485867bSBarry Smith vr = vb[bptr[idc]]; 15165485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 15175485867bSBarry Smith *f = PETSC_FALSE; 15185485867bSBarry Smith goto done; 1519cd0d46ebSvictorle } else { 15205485867bSBarry Smith aptr[i]++; 15215485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 1522cd0d46ebSvictorle } 1523cd0d46ebSvictorle } 1524cd0d46ebSvictorle } 1525cd0d46ebSvictorle done: 1526cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 15273aeef889SHong Zhang if (B) { 15283aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 15293aeef889SHong Zhang } 1530cd0d46ebSvictorle PetscFunctionReturn(0); 1531cd0d46ebSvictorle } 1532cd0d46ebSvictorle EXTERN_C_END 1533cd0d46ebSvictorle 15341cbb95d3SBarry Smith EXTERN_C_BEGIN 15351cbb95d3SBarry Smith #undef __FUNCT__ 15361cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ" 15371cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f) 15381cbb95d3SBarry Smith { 15391cbb95d3SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 15401cbb95d3SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb; 15411cbb95d3SBarry Smith PetscErrorCode ierr; 15421cbb95d3SBarry Smith PetscInt ma,na,mb,nb, i; 15431cbb95d3SBarry Smith 15441cbb95d3SBarry Smith PetscFunctionBegin; 15451cbb95d3SBarry Smith bij = (Mat_SeqAIJ *) B->data; 15461cbb95d3SBarry Smith 15471cbb95d3SBarry Smith ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 15481cbb95d3SBarry Smith ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 15491cbb95d3SBarry Smith if (ma!=nb || na!=mb){ 15501cbb95d3SBarry Smith *f = PETSC_FALSE; 15511cbb95d3SBarry Smith PetscFunctionReturn(0); 15521cbb95d3SBarry Smith } 15531cbb95d3SBarry Smith aii = aij->i; bii = bij->i; 15541cbb95d3SBarry Smith adx = aij->j; bdx = bij->j; 15551cbb95d3SBarry Smith va = aij->a; vb = bij->a; 15561cbb95d3SBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 15571cbb95d3SBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 15581cbb95d3SBarry Smith for (i=0; i<ma; i++) aptr[i] = aii[i]; 15591cbb95d3SBarry Smith for (i=0; i<mb; i++) bptr[i] = bii[i]; 15601cbb95d3SBarry Smith 15611cbb95d3SBarry Smith *f = PETSC_TRUE; 15621cbb95d3SBarry Smith for (i=0; i<ma; i++) { 15631cbb95d3SBarry Smith while (aptr[i]<aii[i+1]) { 15641cbb95d3SBarry Smith PetscInt idc,idr; 15651cbb95d3SBarry Smith PetscScalar vc,vr; 15661cbb95d3SBarry Smith /* column/row index/value */ 15671cbb95d3SBarry Smith idc = adx[aptr[i]]; 15681cbb95d3SBarry Smith idr = bdx[bptr[idc]]; 15691cbb95d3SBarry Smith vc = va[aptr[i]]; 15701cbb95d3SBarry Smith vr = vb[bptr[idc]]; 15711cbb95d3SBarry Smith if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) { 15721cbb95d3SBarry Smith *f = PETSC_FALSE; 15731cbb95d3SBarry Smith goto done; 15741cbb95d3SBarry Smith } else { 15751cbb95d3SBarry Smith aptr[i]++; 15761cbb95d3SBarry Smith if (B || i!=idc) bptr[idc]++; 15771cbb95d3SBarry Smith } 15781cbb95d3SBarry Smith } 15791cbb95d3SBarry Smith } 15801cbb95d3SBarry Smith done: 15811cbb95d3SBarry Smith ierr = PetscFree(aptr);CHKERRQ(ierr); 15821cbb95d3SBarry Smith if (B) { 15831cbb95d3SBarry Smith ierr = PetscFree(bptr);CHKERRQ(ierr); 15841cbb95d3SBarry Smith } 15851cbb95d3SBarry Smith PetscFunctionReturn(0); 15861cbb95d3SBarry Smith } 15871cbb95d3SBarry Smith EXTERN_C_END 15881cbb95d3SBarry Smith 15899e29f15eSvictorle #undef __FUNCT__ 15909e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ" 1591dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f) 15929e29f15eSvictorle { 1593dfbe8321SBarry Smith PetscErrorCode ierr; 15949e29f15eSvictorle PetscFunctionBegin; 15955485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 15969e29f15eSvictorle PetscFunctionReturn(0); 15979e29f15eSvictorle } 15989e29f15eSvictorle 15994a2ae208SSatish Balay #undef __FUNCT__ 16001cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ" 16011cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f) 16021cbb95d3SBarry Smith { 16031cbb95d3SBarry Smith PetscErrorCode ierr; 16041cbb95d3SBarry Smith PetscFunctionBegin; 16051cbb95d3SBarry Smith ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 16061cbb95d3SBarry Smith PetscFunctionReturn(0); 16071cbb95d3SBarry Smith } 16081cbb95d3SBarry Smith 16091cbb95d3SBarry Smith #undef __FUNCT__ 16104a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ" 1611dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 161217ab2063SBarry Smith { 1613416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 161487828ca2SBarry Smith PetscScalar *l,*r,x,*v; 1615dfbe8321SBarry Smith PetscErrorCode ierr; 1616899cda47SBarry Smith PetscInt i,j,m = A->rmap.n,n = A->cmap.n,M,nz = a->nz,*jj; 161717ab2063SBarry Smith 16183a40ed3dSBarry Smith PetscFunctionBegin; 161917ab2063SBarry Smith if (ll) { 16203ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 16213ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1622e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1623899cda47SBarry Smith if (m != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 16241ebc52fbSHong Zhang ierr = VecGetArray(ll,&l);CHKERRQ(ierr); 1625416022c9SBarry Smith v = a->a; 162617ab2063SBarry Smith for (i=0; i<m; i++) { 162717ab2063SBarry Smith x = l[i]; 1628416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 162917ab2063SBarry Smith for (j=0; j<M; j++) { (*v++) *= x;} 163017ab2063SBarry Smith } 16311ebc52fbSHong Zhang ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr); 1632efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 163317ab2063SBarry Smith } 163417ab2063SBarry Smith if (rr) { 1635e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1636899cda47SBarry Smith if (n != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 16371ebc52fbSHong Zhang ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1638416022c9SBarry Smith v = a->a; jj = a->j; 163917ab2063SBarry Smith for (i=0; i<nz; i++) { 1640bfeeae90SHong Zhang (*v++) *= r[*jj++]; 164117ab2063SBarry Smith } 16421ebc52fbSHong Zhang ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 1643efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 164417ab2063SBarry Smith } 16453a40ed3dSBarry Smith PetscFunctionReturn(0); 164617ab2063SBarry Smith } 164717ab2063SBarry Smith 16484a2ae208SSatish Balay #undef __FUNCT__ 16494a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ" 165097f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 165117ab2063SBarry Smith { 1652db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 16536849ba73SBarry Smith PetscErrorCode ierr; 1654899cda47SBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap.n,*lens; 165597f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 165697f1f81fSBarry Smith PetscInt *irow,*icol,nrows,ncols; 165797f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 165887828ca2SBarry Smith PetscScalar *a_new,*mat_a; 1659416022c9SBarry Smith Mat C; 1660fee21e36SBarry Smith PetscTruth stride; 166117ab2063SBarry Smith 16623a40ed3dSBarry Smith PetscFunctionBegin; 1663d64ed03dSBarry Smith ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr); 166429bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 1665d64ed03dSBarry Smith ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr); 166629bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); 166799141d43SSatish Balay 166817ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 1669b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 1670b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 167117ab2063SBarry Smith 1672fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 1673fee21e36SBarry Smith ierr = ISStride(iscol,&stride);CHKERRQ(ierr); 1674fee21e36SBarry Smith if (stride && step == 1) { 167502834360SBarry Smith /* special case of contiguous rows */ 167697f1f81fSBarry Smith ierr = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 167731ebf83bSSatish Balay starts = lens + nrows; 167802834360SBarry Smith /* loop over new rows determining lens and starting points */ 167902834360SBarry Smith for (i=0; i<nrows; i++) { 1680bfeeae90SHong Zhang kstart = ai[irow[i]]; 1681a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 168202834360SBarry Smith for (k=kstart; k<kend; k++) { 1683bfeeae90SHong Zhang if (aj[k] >= first) { 168402834360SBarry Smith starts[i] = k; 168502834360SBarry Smith break; 168602834360SBarry Smith } 168702834360SBarry Smith } 1688a2744918SBarry Smith sum = 0; 168902834360SBarry Smith while (k < kend) { 1690bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 1691a2744918SBarry Smith sum++; 169202834360SBarry Smith } 1693a2744918SBarry Smith lens[i] = sum; 169402834360SBarry Smith } 169502834360SBarry Smith /* create submatrix */ 1696cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 169797f1f81fSBarry Smith PetscInt n_cols,n_rows; 169808480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 169929bbc08cSBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 1700d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 170108480c60SBarry Smith C = *B; 17023a40ed3dSBarry Smith } else { 17037adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1704f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 17057adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1706ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 170708480c60SBarry Smith } 1708db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 1709db02288aSLois Curfman McInnes 171002834360SBarry Smith /* loop over rows inserting into submatrix */ 1711db02288aSLois Curfman McInnes a_new = c->a; 1712db02288aSLois Curfman McInnes j_new = c->j; 1713db02288aSLois Curfman McInnes i_new = c->i; 1714bfeeae90SHong Zhang 171502834360SBarry Smith for (i=0; i<nrows; i++) { 1716a2744918SBarry Smith ii = starts[i]; 1717a2744918SBarry Smith lensi = lens[i]; 1718a2744918SBarry Smith for (k=0; k<lensi; k++) { 1719a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 172002834360SBarry Smith } 172187828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 1722a2744918SBarry Smith a_new += lensi; 1723a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1724a2744918SBarry Smith c->ilen[i] = lensi; 172502834360SBarry Smith } 1726606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 17273a40ed3dSBarry Smith } else { 172802834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 172997f1f81fSBarry Smith ierr = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr); 1730bfeeae90SHong Zhang 173197f1f81fSBarry Smith ierr = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 173297f1f81fSBarry Smith ierr = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr); 173317ab2063SBarry Smith for (i=0; i<ncols; i++) smap[icol[i]] = i+1; 173402834360SBarry Smith /* determine lens of each row */ 173502834360SBarry Smith for (i=0; i<nrows; i++) { 1736bfeeae90SHong Zhang kstart = ai[irow[i]]; 173702834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 173802834360SBarry Smith lens[i] = 0; 173902834360SBarry Smith for (k=kstart; k<kend; k++) { 1740bfeeae90SHong Zhang if (smap[aj[k]]) { 174102834360SBarry Smith lens[i]++; 174202834360SBarry Smith } 174302834360SBarry Smith } 174402834360SBarry Smith } 174517ab2063SBarry Smith /* Create and fill new matrix */ 1746a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 17470f5bd95cSBarry Smith PetscTruth equal; 17480f5bd95cSBarry Smith 174999141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 1750899cda47SBarry Smith if ((*B)->rmap.n != nrows || (*B)->cmap.n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 1751899cda47SBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap.n*sizeof(PetscInt),&equal);CHKERRQ(ierr); 17520f5bd95cSBarry Smith if (!equal) { 175329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 175499141d43SSatish Balay } 1755899cda47SBarry Smith ierr = PetscMemzero(c->ilen,(*B)->rmap.n*sizeof(PetscInt));CHKERRQ(ierr); 175608480c60SBarry Smith C = *B; 17573a40ed3dSBarry Smith } else { 17587adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1759f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 17607adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1761ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 176208480c60SBarry Smith } 176399141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 176417ab2063SBarry Smith for (i=0; i<nrows; i++) { 176599141d43SSatish Balay row = irow[i]; 1766bfeeae90SHong Zhang kstart = ai[row]; 176799141d43SSatish Balay kend = kstart + a->ilen[row]; 1768bfeeae90SHong Zhang mat_i = c->i[i]; 176999141d43SSatish Balay mat_j = c->j + mat_i; 177099141d43SSatish Balay mat_a = c->a + mat_i; 177199141d43SSatish Balay mat_ilen = c->ilen + i; 177217ab2063SBarry Smith for (k=kstart; k<kend; k++) { 1773bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 1774ed480e8bSBarry Smith *mat_j++ = tcol - 1; 177599141d43SSatish Balay *mat_a++ = a->a[k]; 177699141d43SSatish Balay (*mat_ilen)++; 177799141d43SSatish Balay 177817ab2063SBarry Smith } 177917ab2063SBarry Smith } 178017ab2063SBarry Smith } 178102834360SBarry Smith /* Free work space */ 178202834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 1783606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 1784606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 178502834360SBarry Smith } 17866d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17876d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 178817ab2063SBarry Smith 178917ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 1790416022c9SBarry Smith *B = C; 17913a40ed3dSBarry Smith PetscFunctionReturn(0); 179217ab2063SBarry Smith } 179317ab2063SBarry Smith 1794a871dcd8SBarry Smith /* 1795a871dcd8SBarry Smith */ 17964a2ae208SSatish Balay #undef __FUNCT__ 17974a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ" 1798dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info) 1799a871dcd8SBarry Smith { 180063b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1801dfbe8321SBarry Smith PetscErrorCode ierr; 180263b91edcSBarry Smith Mat outA; 1803b8a78c4aSBarry Smith PetscTruth row_identity,col_identity; 180463b91edcSBarry Smith 18053a40ed3dSBarry Smith PetscFunctionBegin; 1806d3d32019SBarry Smith if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 1807b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 1808b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 1809a871dcd8SBarry Smith 181063b91edcSBarry Smith outA = inA; 181163b91edcSBarry Smith inA->factor = FACTOR_LU; 1812c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 1813c3122656SLisandro Dalcin if (a->row) { ierr = ISDestroy(a->row); CHKERRQ(ierr);} 1814c3122656SLisandro Dalcin a->row = row; 1815c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 1816c3122656SLisandro Dalcin if (a->col) { ierr = ISDestroy(a->col); CHKERRQ(ierr);} 1817c3122656SLisandro Dalcin a->col = col; 181863b91edcSBarry Smith 181936db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 1820b9b97703SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */ 18214c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 182252e6d16bSBarry Smith ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr); 1823f0ec6fceSSatish Balay 182494a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 1825899cda47SBarry Smith ierr = PetscMalloc((inA->rmap.n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr); 18269518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(inA, (inA->rmap.n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 182794a9d846SBarry Smith } 182863b91edcSBarry Smith 1829f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 1830137fb511SHong Zhang if (row_identity && col_identity) { 1831af281ebdSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr); 1832137fb511SHong Zhang } else { 1833137fb511SHong Zhang ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(inA,info,&outA);CHKERRQ(ierr); 1834137fb511SHong Zhang } 18353a40ed3dSBarry Smith PetscFunctionReturn(0); 1836a871dcd8SBarry Smith } 1837a871dcd8SBarry Smith 1838d9eff348SSatish Balay #include "petscblaslapack.h" 18394a2ae208SSatish Balay #undef __FUNCT__ 18404a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ" 1841f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 1842f0b747eeSBarry Smith { 1843f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 18444ce68768SBarry Smith PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1; 1845f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 1846efee365bSSatish Balay PetscErrorCode ierr; 1847efee365bSSatish Balay 18483a40ed3dSBarry Smith 18493a40ed3dSBarry Smith PetscFunctionBegin; 1850f4df32b1SMatthew Knepley BLASscal_(&bnz,&oalpha,a->a,&one); 1851efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 18523a40ed3dSBarry Smith PetscFunctionReturn(0); 1853f0b747eeSBarry Smith } 1854f0b747eeSBarry Smith 18554a2ae208SSatish Balay #undef __FUNCT__ 18564a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ" 185797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 1858cddf8d76SBarry Smith { 1859dfbe8321SBarry Smith PetscErrorCode ierr; 186097f1f81fSBarry Smith PetscInt i; 1861cddf8d76SBarry Smith 18623a40ed3dSBarry Smith PetscFunctionBegin; 1863cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 1864b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr); 1865cddf8d76SBarry Smith } 1866cddf8d76SBarry Smith 1867cddf8d76SBarry Smith for (i=0; i<n; i++) { 18686a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 1869cddf8d76SBarry Smith } 18703a40ed3dSBarry Smith PetscFunctionReturn(0); 1871cddf8d76SBarry Smith } 1872cddf8d76SBarry Smith 18734a2ae208SSatish Balay #undef __FUNCT__ 18744a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ" 187597f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 18764dcbc457SBarry Smith { 1877e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 18786849ba73SBarry Smith PetscErrorCode ierr; 187997f1f81fSBarry Smith PetscInt row,i,j,k,l,m,n,*idx,*nidx,isz,val; 188097f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 1881f1af5d2fSBarry Smith PetscBT table; 1882bbd702dbSSatish Balay 18833a40ed3dSBarry Smith PetscFunctionBegin; 1884899cda47SBarry Smith m = A->rmap.n; 1885e4d965acSSatish Balay ai = a->i; 1886bfeeae90SHong Zhang aj = a->j; 18878a047759SSatish Balay 1888a45adfd6SMatthew Knepley if (ov < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 188906763907SSatish Balay 189097f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr); 18916831982aSBarry Smith ierr = PetscBTCreate(m,table);CHKERRQ(ierr); 189206763907SSatish Balay 1893e4d965acSSatish Balay for (i=0; i<is_max; i++) { 1894b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1895e4d965acSSatish Balay isz = 0; 18966831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 1897e4d965acSSatish Balay 1898e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 18994dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 1900b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 1901e4d965acSSatish Balay 1902dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1903e4d965acSSatish Balay for (j=0; j<n ; ++j){ 1904f1af5d2fSBarry Smith if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];} 19054dcbc457SBarry Smith } 190606763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 190706763907SSatish Balay ierr = ISDestroy(is[i]);CHKERRQ(ierr); 1908e4d965acSSatish Balay 190904a348a9SBarry Smith k = 0; 191004a348a9SBarry Smith for (j=0; j<ov; j++){ /* for each overlap */ 191104a348a9SBarry Smith n = isz; 191206763907SSatish Balay for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 1913e4d965acSSatish Balay row = nidx[k]; 1914e4d965acSSatish Balay start = ai[row]; 1915e4d965acSSatish Balay end = ai[row+1]; 191604a348a9SBarry Smith for (l = start; l<end ; l++){ 1917efb16452SHong Zhang val = aj[l] ; 1918f1af5d2fSBarry Smith if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;} 1919e4d965acSSatish Balay } 1920e4d965acSSatish Balay } 1921e4d965acSSatish Balay } 1922029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr); 1923e4d965acSSatish Balay } 19246831982aSBarry Smith ierr = PetscBTDestroy(table);CHKERRQ(ierr); 1925606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 19263a40ed3dSBarry Smith PetscFunctionReturn(0); 19274dcbc457SBarry Smith } 192817ab2063SBarry Smith 19290513a670SBarry Smith /* -------------------------------------------------------------- */ 19304a2ae208SSatish Balay #undef __FUNCT__ 19314a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ" 1932dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 19330513a670SBarry Smith { 19340513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 19356849ba73SBarry Smith PetscErrorCode ierr; 1936899cda47SBarry Smith PetscInt i,nz,m = A->rmap.n,n = A->cmap.n,*col; 193797f1f81fSBarry Smith PetscInt *row,*cnew,j,*lens; 193856cd22aeSBarry Smith IS icolp,irowp; 193997f1f81fSBarry Smith PetscInt *cwork; 194032ec9ce4SBarry Smith PetscScalar *vwork; 19410513a670SBarry Smith 19423a40ed3dSBarry Smith PetscFunctionBegin; 19434c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 194456cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 19454c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 194656cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 19470513a670SBarry Smith 19480513a670SBarry Smith /* determine lengths of permuted rows */ 194997f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 19500513a670SBarry Smith for (i=0; i<m; i++) { 19510513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 19520513a670SBarry Smith } 19537adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 1954f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 19557adad957SLisandro Dalcin ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 1956ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 1957606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 19580513a670SBarry Smith 195997f1f81fSBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr); 19600513a670SBarry Smith for (i=0; i<m; i++) { 196132ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 19620513a670SBarry Smith for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];} 1963cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 196432ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 19650513a670SBarry Smith } 1966606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 19673c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 19680513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 19690513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 197056cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 197156cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 197256cd22aeSBarry Smith ierr = ISDestroy(irowp);CHKERRQ(ierr); 197356cd22aeSBarry Smith ierr = ISDestroy(icolp);CHKERRQ(ierr); 19743a40ed3dSBarry Smith PetscFunctionReturn(0); 19750513a670SBarry Smith } 19760513a670SBarry Smith 19774a2ae208SSatish Balay #undef __FUNCT__ 19784a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ" 1979dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 1980cb5b572fSBarry Smith { 1981dfbe8321SBarry Smith PetscErrorCode ierr; 1982cb5b572fSBarry Smith 1983cb5b572fSBarry Smith PetscFunctionBegin; 198433f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 198533f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 1986be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1987be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 1988be6bf707SBarry Smith 1989899cda47SBarry Smith if (a->i[A->rmap.n] != b->i[B->rmap.n]) { 1990634064b4SBarry Smith SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different"); 1991cb5b572fSBarry Smith } 1992899cda47SBarry Smith ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap.n])*sizeof(PetscScalar));CHKERRQ(ierr); 1993cb5b572fSBarry Smith } else { 1994cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1995cb5b572fSBarry Smith } 1996cb5b572fSBarry Smith PetscFunctionReturn(0); 1997cb5b572fSBarry Smith } 1998cb5b572fSBarry Smith 19994a2ae208SSatish Balay #undef __FUNCT__ 20004a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ" 2001dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A) 2002273d9f13SBarry Smith { 2003dfbe8321SBarry Smith PetscErrorCode ierr; 2004273d9f13SBarry Smith 2005273d9f13SBarry Smith PetscFunctionBegin; 2006ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 2007273d9f13SBarry Smith PetscFunctionReturn(0); 2008273d9f13SBarry Smith } 2009273d9f13SBarry Smith 20104a2ae208SSatish Balay #undef __FUNCT__ 20114a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ" 2012dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 20136c0721eeSBarry Smith { 20146c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 20156c0721eeSBarry Smith PetscFunctionBegin; 20166c0721eeSBarry Smith *array = a->a; 20176c0721eeSBarry Smith PetscFunctionReturn(0); 20186c0721eeSBarry Smith } 20196c0721eeSBarry Smith 20204a2ae208SSatish Balay #undef __FUNCT__ 20214a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ" 2022dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 20236c0721eeSBarry Smith { 20246c0721eeSBarry Smith PetscFunctionBegin; 20256c0721eeSBarry Smith PetscFunctionReturn(0); 20266c0721eeSBarry Smith } 2027273d9f13SBarry Smith 2028ee4f033dSBarry Smith #undef __FUNCT__ 2029ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ" 2030dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 2031ee4f033dSBarry Smith { 20326849ba73SBarry Smith PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f; 20336849ba73SBarry Smith PetscErrorCode ierr; 203497f1f81fSBarry Smith PetscInt k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2; 2035efb30889SBarry Smith PetscScalar dx,*y,*xx,*w3_array; 203687828ca2SBarry Smith PetscScalar *vscale_array; 2037ee4f033dSBarry Smith PetscReal epsilon = coloring->error_rel,umin = coloring->umin; 2038ee4f033dSBarry Smith Vec w1,w2,w3; 2039ee4f033dSBarry Smith void *fctx = coloring->fctx; 2040ee4f033dSBarry Smith PetscTruth flg; 2041ee4f033dSBarry Smith 2042ee4f033dSBarry Smith PetscFunctionBegin; 2043ee4f033dSBarry Smith if (!coloring->w1) { 2044ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr); 204552e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr); 2046ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr); 204752e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr); 2048ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 204952e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr); 2050ee4f033dSBarry Smith } 2051ee4f033dSBarry Smith w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 2052ee4f033dSBarry Smith 2053ee4f033dSBarry Smith ierr = MatSetUnfactored(J);CHKERRQ(ierr); 20547adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr); 2055ee4f033dSBarry Smith if (flg) { 2056ae15b995SBarry Smith ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr); 2057ee4f033dSBarry Smith } else { 20580b9b6f31SBarry Smith PetscTruth assembled; 20590b9b6f31SBarry Smith ierr = MatAssembled(J,&assembled);CHKERRQ(ierr); 20600b9b6f31SBarry Smith if (assembled) { 2061ee4f033dSBarry Smith ierr = MatZeroEntries(J);CHKERRQ(ierr); 2062ee4f033dSBarry Smith } 20630b9b6f31SBarry Smith } 2064ee4f033dSBarry Smith 2065ee4f033dSBarry Smith ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr); 2066ee4f033dSBarry Smith ierr = VecGetSize(x1,&N);CHKERRQ(ierr); 2067ee4f033dSBarry Smith 2068ee4f033dSBarry Smith /* 2069ee4f033dSBarry Smith This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets 2070ee4f033dSBarry Smith coloring->F for the coarser grids from the finest 2071ee4f033dSBarry Smith */ 2072ee4f033dSBarry Smith if (coloring->F) { 2073ee4f033dSBarry Smith ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr); 2074ee4f033dSBarry Smith ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr); 2075ee4f033dSBarry Smith if (m1 != m2) { 2076ee4f033dSBarry Smith coloring->F = 0; 2077ee4f033dSBarry Smith } 2078ee4f033dSBarry Smith } 2079ee4f033dSBarry Smith 2080ee4f033dSBarry Smith if (coloring->F) { 2081ee4f033dSBarry Smith w1 = coloring->F; 2082ee4f033dSBarry Smith coloring->F = 0; 2083ee4f033dSBarry Smith } else { 208466f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2085ee4f033dSBarry Smith ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 208666f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2087ee4f033dSBarry Smith } 2088ee4f033dSBarry Smith 2089ee4f033dSBarry Smith /* 2090ee4f033dSBarry Smith Compute all the scale factors and share with other processors 2091ee4f033dSBarry Smith */ 20921ebc52fbSHong Zhang ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start; 20931ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start; 2094ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 2095ee4f033dSBarry Smith /* 2096ee4f033dSBarry Smith Loop over each column associated with color adding the 2097ee4f033dSBarry Smith perturbation to the vector w3. 2098ee4f033dSBarry Smith */ 2099ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2100ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2101ee4f033dSBarry Smith dx = xx[col]; 2102ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 2103ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2104ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2105ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2106ee4f033dSBarry Smith #else 2107ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2108ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2109ee4f033dSBarry Smith #endif 2110ee4f033dSBarry Smith dx *= epsilon; 2111ee4f033dSBarry Smith vscale_array[col] = 1.0/dx; 2112ee4f033dSBarry Smith } 2113ee4f033dSBarry Smith } 21141ebc52fbSHong Zhang vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2115ee4f033dSBarry Smith ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2116ee4f033dSBarry Smith ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2117ee4f033dSBarry Smith 2118ee4f033dSBarry Smith /* ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD); 2119ee4f033dSBarry Smith ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/ 2120ee4f033dSBarry Smith 2121ee4f033dSBarry Smith if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow; 2122ee4f033dSBarry Smith else vscaleforrow = coloring->columnsforrow; 2123ee4f033dSBarry Smith 21241ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2125ee4f033dSBarry Smith /* 2126ee4f033dSBarry Smith Loop over each color 2127ee4f033dSBarry Smith */ 2128ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 212949b058dcSBarry Smith coloring->currentcolor = k; 2130ee4f033dSBarry Smith ierr = VecCopy(x1,w3);CHKERRQ(ierr); 21311ebc52fbSHong Zhang ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start; 2132ee4f033dSBarry Smith /* 2133ee4f033dSBarry Smith Loop over each column associated with color adding the 2134ee4f033dSBarry Smith perturbation to the vector w3. 2135ee4f033dSBarry Smith */ 2136ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2137ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2138ee4f033dSBarry Smith dx = xx[col]; 21395b8514ebSBarry Smith if (dx == 0.0) dx = 1.0; 2140ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2141ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2142ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2143ee4f033dSBarry Smith #else 2144ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2145ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2146ee4f033dSBarry Smith #endif 2147ee4f033dSBarry Smith dx *= epsilon; 2148634064b4SBarry Smith if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter"); 2149ee4f033dSBarry Smith w3_array[col] += dx; 2150ee4f033dSBarry Smith } 21511ebc52fbSHong Zhang w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 2152ee4f033dSBarry Smith 2153ee4f033dSBarry Smith /* 2154ee4f033dSBarry Smith Evaluate function at x1 + dx (here dx is a vector of perturbations) 2155ee4f033dSBarry Smith */ 2156ee4f033dSBarry Smith 215766f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2158ee4f033dSBarry Smith ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 215966f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2160efb30889SBarry Smith ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 2161ee4f033dSBarry Smith 2162ee4f033dSBarry Smith /* 2163ee4f033dSBarry Smith Loop over rows of vector, putting results into Jacobian matrix 2164ee4f033dSBarry Smith */ 21651ebc52fbSHong Zhang ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 2166ee4f033dSBarry Smith for (l=0; l<coloring->nrows[k]; l++) { 2167ee4f033dSBarry Smith row = coloring->rows[k][l]; 2168ee4f033dSBarry Smith col = coloring->columnsforrow[k][l]; 2169ee4f033dSBarry Smith y[row] *= vscale_array[vscaleforrow[k][l]]; 2170ee4f033dSBarry Smith srow = row + start; 2171ee4f033dSBarry Smith ierr = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 2172ee4f033dSBarry Smith } 21731ebc52fbSHong Zhang ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 2174ee4f033dSBarry Smith } 217549b058dcSBarry Smith coloring->currentcolor = k; 21761ebc52fbSHong Zhang ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 21771ebc52fbSHong Zhang xx = xx + start; ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 2178ee4f033dSBarry Smith ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2179ee4f033dSBarry Smith ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2180ee4f033dSBarry Smith PetscFunctionReturn(0); 2181ee4f033dSBarry Smith } 2182ee4f033dSBarry Smith 2183ac90fabeSBarry Smith #include "petscblaslapack.h" 2184ac90fabeSBarry Smith #undef __FUNCT__ 2185ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ" 2186f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2187ac90fabeSBarry Smith { 2188dfbe8321SBarry Smith PetscErrorCode ierr; 218997f1f81fSBarry Smith PetscInt i; 2190ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data; 21914ce68768SBarry Smith PetscBLASInt one=1,bnz = (PetscBLASInt)x->nz; 2192ac90fabeSBarry Smith 2193ac90fabeSBarry Smith PetscFunctionBegin; 2194ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2195f4df32b1SMatthew Knepley PetscScalar alpha = a; 2196f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2197c537a176SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 2198a30b2313SHong Zhang if (y->xtoy && y->XtoY != X) { 2199a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 2200a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 2201a30b2313SHong Zhang } 2202a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2203899cda47SBarry Smith ierr = MatAXPYGetxtoy_Private(X->rmap.n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr); 2204a30b2313SHong Zhang y->XtoY = X; 2205407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr); 2206c537a176SHong Zhang } 2207f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 22081e2582c4SBarry 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); 2209ac90fabeSBarry Smith } else { 2210f4df32b1SMatthew Knepley ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 2211ac90fabeSBarry Smith } 2212ac90fabeSBarry Smith PetscFunctionReturn(0); 2213ac90fabeSBarry Smith } 2214ac90fabeSBarry Smith 2215521d7252SBarry Smith #undef __FUNCT__ 2216521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ" 2217521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs) 2218521d7252SBarry Smith { 2219521d7252SBarry Smith PetscFunctionBegin; 2220521d7252SBarry Smith PetscFunctionReturn(0); 2221521d7252SBarry Smith } 2222521d7252SBarry Smith 2223354c94deSBarry Smith #undef __FUNCT__ 2224354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ" 2225354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat) 2226354c94deSBarry Smith { 2227354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2228354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2229354c94deSBarry Smith PetscInt i,nz; 2230354c94deSBarry Smith PetscScalar *a; 2231354c94deSBarry Smith 2232354c94deSBarry Smith PetscFunctionBegin; 2233354c94deSBarry Smith nz = aij->nz; 2234354c94deSBarry Smith a = aij->a; 2235354c94deSBarry Smith for (i=0; i<nz; i++) { 2236354c94deSBarry Smith a[i] = PetscConj(a[i]); 2237354c94deSBarry Smith } 2238354c94deSBarry Smith #else 2239354c94deSBarry Smith PetscFunctionBegin; 2240354c94deSBarry Smith #endif 2241354c94deSBarry Smith PetscFunctionReturn(0); 2242354c94deSBarry Smith } 2243354c94deSBarry Smith 2244e34fafa9SBarry Smith #undef __FUNCT__ 2245985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ" 2246985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2247e34fafa9SBarry Smith { 2248e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2249e34fafa9SBarry Smith PetscErrorCode ierr; 2250899cda47SBarry Smith PetscInt i,j,m = A->rmap.n,*ai,*aj,ncols,n; 2251e34fafa9SBarry Smith PetscReal atmp; 2252985db425SBarry Smith PetscScalar *x; 2253e34fafa9SBarry Smith MatScalar *aa; 2254e34fafa9SBarry Smith 2255e34fafa9SBarry Smith PetscFunctionBegin; 2256e34fafa9SBarry Smith if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2257e34fafa9SBarry Smith aa = a->a; 2258e34fafa9SBarry Smith ai = a->i; 2259e34fafa9SBarry Smith aj = a->j; 2260e34fafa9SBarry Smith 2261985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2262e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2263e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2264899cda47SBarry Smith if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2265e34fafa9SBarry Smith for (i=0; i<m; i++) { 2266e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 2267985db425SBarry Smith x[i] = 0.0; if (idx) idx[i] = 0; 2268e34fafa9SBarry Smith for (j=0; j<ncols; j++){ 2269985db425SBarry Smith atmp = PetscAbsScalar(*aa); 2270985db425SBarry Smith if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2271985db425SBarry Smith aa++; aj++; 2272985db425SBarry Smith } 2273985db425SBarry Smith } 2274985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2275985db425SBarry Smith PetscFunctionReturn(0); 2276985db425SBarry Smith } 2277985db425SBarry Smith 2278985db425SBarry Smith #undef __FUNCT__ 2279985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ" 2280985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2281985db425SBarry Smith { 2282985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2283985db425SBarry Smith PetscErrorCode ierr; 2284985db425SBarry Smith PetscInt i,j,m = A->rmap.n,*ai,*aj,ncols,n; 2285985db425SBarry Smith PetscScalar *x; 2286985db425SBarry Smith MatScalar *aa; 2287985db425SBarry Smith 2288985db425SBarry Smith PetscFunctionBegin; 2289985db425SBarry Smith if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2290985db425SBarry Smith aa = a->a; 2291985db425SBarry Smith ai = a->i; 2292985db425SBarry Smith aj = a->j; 2293985db425SBarry Smith 2294985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2295985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2296985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2297985db425SBarry Smith if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2298985db425SBarry Smith for (i=0; i<m; i++) { 2299985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2300985db425SBarry Smith if (ncols == A->cmap.n) { /* row is dense */ 2301985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2302985db425SBarry Smith } else { /* row is sparse so already KNOW maximum is 0.0 or higher */ 2303985db425SBarry Smith x[i] = 0.0; 2304985db425SBarry Smith if (idx) { 2305985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2306985db425SBarry Smith for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */ 2307985db425SBarry Smith if (aj[j] > j) { 2308985db425SBarry Smith idx[i] = j; 2309985db425SBarry Smith break; 2310985db425SBarry Smith } 2311985db425SBarry Smith } 2312985db425SBarry Smith } 2313985db425SBarry Smith } 2314985db425SBarry Smith for (j=0; j<ncols; j++){ 2315985db425SBarry Smith if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2316985db425SBarry Smith aa++; aj++; 2317985db425SBarry Smith } 2318985db425SBarry Smith } 2319985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2320985db425SBarry Smith PetscFunctionReturn(0); 2321985db425SBarry Smith } 2322985db425SBarry Smith 2323985db425SBarry Smith #undef __FUNCT__ 2324985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ" 2325985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2326985db425SBarry Smith { 2327985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2328985db425SBarry Smith PetscErrorCode ierr; 2329985db425SBarry Smith PetscInt i,j,m = A->rmap.n,*ai,*aj,ncols,n; 2330985db425SBarry Smith PetscScalar *x; 2331985db425SBarry Smith MatScalar *aa; 2332985db425SBarry Smith 2333985db425SBarry Smith PetscFunctionBegin; 2334985db425SBarry Smith if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2335985db425SBarry Smith aa = a->a; 2336985db425SBarry Smith ai = a->i; 2337985db425SBarry Smith aj = a->j; 2338985db425SBarry Smith 2339985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2340985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2341985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2342985db425SBarry Smith if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2343985db425SBarry Smith for (i=0; i<m; i++) { 2344985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2345985db425SBarry Smith if (ncols == A->cmap.n) { /* row is dense */ 2346985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2347985db425SBarry Smith } else { /* row is sparse so already KNOW minimum is 0.0 or lower */ 2348985db425SBarry Smith x[i] = 0.0; 2349985db425SBarry Smith if (idx) { /* find first implicit 0.0 in the row */ 2350985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2351985db425SBarry Smith for (j=0;j<ncols;j++) { 2352985db425SBarry Smith if (aj[j] > j) { 2353985db425SBarry Smith idx[i] = j; 2354985db425SBarry Smith break; 2355985db425SBarry Smith } 2356985db425SBarry Smith } 2357985db425SBarry Smith } 2358985db425SBarry Smith } 2359985db425SBarry Smith for (j=0; j<ncols; j++){ 2360985db425SBarry Smith if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2361985db425SBarry Smith aa++; aj++; 2362e34fafa9SBarry Smith } 2363e34fafa9SBarry Smith } 2364e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2365e34fafa9SBarry Smith PetscFunctionReturn(0); 2366e34fafa9SBarry Smith } 2367e34fafa9SBarry Smith 2368682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 23690a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ, 2370cb5b572fSBarry Smith MatGetRow_SeqAIJ, 2371cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 2372cb5b572fSBarry Smith MatMult_SeqAIJ, 237397304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 23747c922b88SBarry Smith MatMultTranspose_SeqAIJ, 23757c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 2376cb5b572fSBarry Smith MatSolve_SeqAIJ, 2377cb5b572fSBarry Smith MatSolveAdd_SeqAIJ, 23787c922b88SBarry Smith MatSolveTranspose_SeqAIJ, 237997304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ, 2380cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 2381cb5b572fSBarry Smith 0, 238217ab2063SBarry Smith MatRelax_SeqAIJ, 238317ab2063SBarry Smith MatTranspose_SeqAIJ, 238497304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ, 2385cb5b572fSBarry Smith MatEqual_SeqAIJ, 2386cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 2387cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 2388cb5b572fSBarry Smith MatNorm_SeqAIJ, 238997304618SKris Buschelman /*20*/ 0, 2390cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 239117ab2063SBarry Smith MatCompress_SeqAIJ, 2392cb5b572fSBarry Smith MatSetOption_SeqAIJ, 2393cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 239497304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ, 2395cb5b572fSBarry Smith MatLUFactorSymbolic_SeqAIJ, 2396cb5b572fSBarry Smith MatLUFactorNumeric_SeqAIJ, 2397f76d2b81SHong Zhang MatCholeskyFactorSymbolic_SeqAIJ, 2398a6175056SHong Zhang MatCholeskyFactorNumeric_SeqAIJ, 239997304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ, 2400cb5b572fSBarry Smith MatILUFactorSymbolic_SeqAIJ, 2401861ba921SHong Zhang MatICCFactorSymbolic_SeqAIJ, 24026c0721eeSBarry Smith MatGetArray_SeqAIJ, 24036c0721eeSBarry Smith MatRestoreArray_SeqAIJ, 240497304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ, 2405cb5b572fSBarry Smith 0, 2406cb5b572fSBarry Smith 0, 2407cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 2408cb5b572fSBarry Smith 0, 240997304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ, 2410cb5b572fSBarry Smith MatGetSubMatrices_SeqAIJ, 2411cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 2412cb5b572fSBarry Smith MatGetValues_SeqAIJ, 2413cb5b572fSBarry Smith MatCopy_SeqAIJ, 2414985db425SBarry Smith /*45*/ MatGetRowMax_SeqAIJ, 2415cb5b572fSBarry Smith MatScale_SeqAIJ, 2416cb5b572fSBarry Smith 0, 241779299369SBarry Smith MatDiagonalSet_SeqAIJ, 24186945ee14SBarry Smith MatILUDTFactor_SeqAIJ, 2419521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ, 24203b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 24213b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 24223b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 2423a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 242497304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ, 2425b9617806SBarry Smith 0, 24260513a670SBarry Smith 0, 2427cda55fadSBarry Smith MatPermute_SeqAIJ, 2428cda55fadSBarry Smith 0, 242997304618SKris Buschelman /*60*/ 0, 2430b9b97703SBarry Smith MatDestroy_SeqAIJ, 2431b9b97703SBarry Smith MatView_SeqAIJ, 2432357abbc8SBarry Smith 0, 2433ee4f033dSBarry Smith 0, 243497304618SKris Buschelman /*65*/ 0, 2435ee4f033dSBarry Smith 0, 2436ee4f033dSBarry Smith 0, 2437ee4f033dSBarry Smith 0, 2438ee4f033dSBarry Smith 0, 2439985db425SBarry Smith /*70*/ MatGetRowMaxAbs_SeqAIJ, 2440ee4f033dSBarry Smith 0, 2441ee4f033dSBarry Smith MatSetColoring_SeqAIJ, 2442dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2443ee4f033dSBarry Smith MatSetValuesAdic_SeqAIJ, 2444dcf5cc72SBarry Smith #else 2445dcf5cc72SBarry Smith 0, 2446dcf5cc72SBarry Smith #endif 2447ee4f033dSBarry Smith MatSetValuesAdifor_SeqAIJ, 2448b8f8c88eSHong Zhang /*75*/ 0, 244997304618SKris Buschelman 0, 245097304618SKris Buschelman 0, 245197304618SKris Buschelman 0, 245297304618SKris Buschelman 0, 245397304618SKris Buschelman /*80*/ 0, 245497304618SKris Buschelman 0, 245597304618SKris Buschelman 0, 245697304618SKris Buschelman 0, 2457bc011b1eSHong Zhang MatLoad_SeqAIJ, 2458bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ, 24591cbb95d3SBarry Smith MatIsHermitian_SeqAIJ, 24606284ec50SHong Zhang 0, 24616284ec50SHong Zhang 0, 2462bc011b1eSHong Zhang 0, 2463bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ, 246426be0446SHong Zhang MatMatMultSymbolic_SeqAIJ_SeqAIJ, 246526be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 2466d439da42SKris Buschelman MatPtAP_Basic, 24677ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ, 24687ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ, 2469bc011b1eSHong Zhang MatMatMultTranspose_SeqAIJ_SeqAIJ, 2470bc011b1eSHong Zhang MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ, 2471bc011b1eSHong Zhang MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ, 24727ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ_SeqAIJ, 24737ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ, 2474609c6c4dSKris Buschelman 0, 2475609c6c4dSKris Buschelman 0, 247687d4246cSBarry Smith MatConjugate_SeqAIJ, 247787d4246cSBarry Smith 0, 247899cafbc1SBarry Smith /*105*/MatSetValuesRow_SeqAIJ, 247999cafbc1SBarry Smith MatRealPart_SeqAIJ, 2480f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 2481f5edf698SHong Zhang 0, 24822bebee5dSHong Zhang 0, 2483985db425SBarry Smith /*110*/MatMatSolve_SeqAIJ, 2484985db425SBarry Smith 0, 24852af78befSBarry Smith MatGetRowMin_SeqAIJ, 24862af78befSBarry Smith 0, 24872af78befSBarry Smith MatMissingDiagonal_SeqAIJ 24889e29f15eSvictorle }; 248917ab2063SBarry Smith 2490fb2e594dSBarry Smith EXTERN_C_BEGIN 24914a2ae208SSatish Balay #undef __FUNCT__ 24924a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ" 2493be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 2494bef8e0ddSBarry Smith { 2495bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 249697f1f81fSBarry Smith PetscInt i,nz,n; 2497bef8e0ddSBarry Smith 2498bef8e0ddSBarry Smith PetscFunctionBegin; 2499bef8e0ddSBarry Smith 2500bef8e0ddSBarry Smith nz = aij->maxnz; 250111c9b30cSMatthew Knepley n = mat->rmap.n; 2502bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 2503bef8e0ddSBarry Smith aij->j[i] = indices[i]; 2504bef8e0ddSBarry Smith } 2505bef8e0ddSBarry Smith aij->nz = nz; 2506bef8e0ddSBarry Smith for (i=0; i<n; i++) { 2507bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 2508bef8e0ddSBarry Smith } 2509bef8e0ddSBarry Smith 2510bef8e0ddSBarry Smith PetscFunctionReturn(0); 2511bef8e0ddSBarry Smith } 2512fb2e594dSBarry Smith EXTERN_C_END 2513bef8e0ddSBarry Smith 25144a2ae208SSatish Balay #undef __FUNCT__ 25154a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices" 2516bef8e0ddSBarry Smith /*@ 2517bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 2518bef8e0ddSBarry Smith in the matrix. 2519bef8e0ddSBarry Smith 2520bef8e0ddSBarry Smith Input Parameters: 2521bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 2522bef8e0ddSBarry Smith - indices - the column indices 2523bef8e0ddSBarry Smith 252415091d37SBarry Smith Level: advanced 252515091d37SBarry Smith 2526bef8e0ddSBarry Smith Notes: 2527bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 2528bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 2529bef8e0ddSBarry Smith of the MatSetValues() operation. 2530bef8e0ddSBarry Smith 2531bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 2532d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 2533bef8e0ddSBarry Smith 2534bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 2535bef8e0ddSBarry Smith 2536b9617806SBarry Smith The indices should start with zero, not one. 2537b9617806SBarry Smith 2538bef8e0ddSBarry Smith @*/ 2539be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 2540bef8e0ddSBarry Smith { 254197f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt *); 2542bef8e0ddSBarry Smith 2543bef8e0ddSBarry Smith PetscFunctionBegin; 25444482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 25454482741eSBarry Smith PetscValidPointer(indices,2); 2546c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr); 2547bef8e0ddSBarry Smith if (f) { 2548bef8e0ddSBarry Smith ierr = (*f)(mat,indices);CHKERRQ(ierr); 2549bef8e0ddSBarry Smith } else { 2550634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices"); 2551bef8e0ddSBarry Smith } 2552bef8e0ddSBarry Smith PetscFunctionReturn(0); 2553bef8e0ddSBarry Smith } 2554bef8e0ddSBarry Smith 2555be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 2556be6bf707SBarry Smith 2557fb2e594dSBarry Smith EXTERN_C_BEGIN 25584a2ae208SSatish Balay #undef __FUNCT__ 25594a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ" 2560be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat) 2561be6bf707SBarry Smith { 2562be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 25636849ba73SBarry Smith PetscErrorCode ierr; 2564899cda47SBarry Smith size_t nz = aij->i[mat->rmap.n]; 2565be6bf707SBarry Smith 2566be6bf707SBarry Smith PetscFunctionBegin; 2567be6bf707SBarry Smith if (aij->nonew != 1) { 2568512a5fc5SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 2569be6bf707SBarry Smith } 2570be6bf707SBarry Smith 2571be6bf707SBarry Smith /* allocate space for values if not already there */ 2572be6bf707SBarry Smith if (!aij->saved_values) { 257387828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr); 25749518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr); 2575be6bf707SBarry Smith } 2576be6bf707SBarry Smith 2577be6bf707SBarry Smith /* copy values over */ 257887828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2579be6bf707SBarry Smith PetscFunctionReturn(0); 2580be6bf707SBarry Smith } 2581fb2e594dSBarry Smith EXTERN_C_END 2582be6bf707SBarry Smith 25834a2ae208SSatish Balay #undef __FUNCT__ 2584b9617806SBarry Smith #define __FUNCT__ "MatStoreValues" 2585be6bf707SBarry Smith /*@ 2586be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 2587be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2588be6bf707SBarry Smith nonlinear portion. 2589be6bf707SBarry Smith 2590be6bf707SBarry Smith Collect on Mat 2591be6bf707SBarry Smith 2592be6bf707SBarry Smith Input Parameters: 25930e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 2594be6bf707SBarry Smith 259515091d37SBarry Smith Level: advanced 259615091d37SBarry Smith 2597be6bf707SBarry Smith Common Usage, with SNESSolve(): 2598be6bf707SBarry Smith $ Create Jacobian matrix 2599be6bf707SBarry Smith $ Set linear terms into matrix 2600be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 2601be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 2602be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 2603512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 2604be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2605be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 2606be6bf707SBarry Smith $ In your Jacobian routine 2607be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2608be6bf707SBarry Smith $ Set nonlinear terms in matrix 2609be6bf707SBarry Smith 2610be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 2611be6bf707SBarry Smith $ // build linear portion of Jacobian 2612512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 2613be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2614be6bf707SBarry Smith $ loop over nonlinear iterations 2615be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2616be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 2617be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 2618be6bf707SBarry Smith $ Solve linear system with Jacobian 2619be6bf707SBarry Smith $ endloop 2620be6bf707SBarry Smith 2621be6bf707SBarry Smith Notes: 2622be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 2623512a5fc5SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before 2624be6bf707SBarry Smith calling this routine. 2625be6bf707SBarry Smith 26260c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 26270c468ba9SBarry Smith and does not allocated additional space. 26280c468ba9SBarry Smith 2629be6bf707SBarry Smith .seealso: MatRetrieveValues() 2630be6bf707SBarry Smith 2631be6bf707SBarry Smith @*/ 2632be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat) 2633be6bf707SBarry Smith { 2634dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2635be6bf707SBarry Smith 2636be6bf707SBarry Smith PetscFunctionBegin; 26374482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 263829bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 263929bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2640be6bf707SBarry Smith 2641c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2642be6bf707SBarry Smith if (f) { 2643be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2644be6bf707SBarry Smith } else { 2645634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values"); 2646be6bf707SBarry Smith } 2647be6bf707SBarry Smith PetscFunctionReturn(0); 2648be6bf707SBarry Smith } 2649be6bf707SBarry Smith 2650fb2e594dSBarry Smith EXTERN_C_BEGIN 26514a2ae208SSatish Balay #undef __FUNCT__ 26524a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ" 2653be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat) 2654be6bf707SBarry Smith { 2655be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 26566849ba73SBarry Smith PetscErrorCode ierr; 2657899cda47SBarry Smith PetscInt nz = aij->i[mat->rmap.n]; 2658be6bf707SBarry Smith 2659be6bf707SBarry Smith PetscFunctionBegin; 2660be6bf707SBarry Smith if (aij->nonew != 1) { 2661512a5fc5SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 2662be6bf707SBarry Smith } 2663be6bf707SBarry Smith if (!aij->saved_values) { 2664634064b4SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 2665be6bf707SBarry Smith } 2666be6bf707SBarry Smith /* copy values over */ 266787828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2668be6bf707SBarry Smith PetscFunctionReturn(0); 2669be6bf707SBarry Smith } 2670fb2e594dSBarry Smith EXTERN_C_END 2671be6bf707SBarry Smith 26724a2ae208SSatish Balay #undef __FUNCT__ 26734a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues" 2674be6bf707SBarry Smith /*@ 2675be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 2676be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2677be6bf707SBarry Smith nonlinear portion. 2678be6bf707SBarry Smith 2679be6bf707SBarry Smith Collect on Mat 2680be6bf707SBarry Smith 2681be6bf707SBarry Smith Input Parameters: 2682be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2683be6bf707SBarry Smith 268415091d37SBarry Smith Level: advanced 268515091d37SBarry Smith 2686be6bf707SBarry Smith .seealso: MatStoreValues() 2687be6bf707SBarry Smith 2688be6bf707SBarry Smith @*/ 2689be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat) 2690be6bf707SBarry Smith { 2691dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2692be6bf707SBarry Smith 2693be6bf707SBarry Smith PetscFunctionBegin; 26944482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 269529bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 269629bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2697be6bf707SBarry Smith 2698c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2699be6bf707SBarry Smith if (f) { 2700be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2701be6bf707SBarry Smith } else { 2702634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values"); 2703be6bf707SBarry Smith } 2704be6bf707SBarry Smith PetscFunctionReturn(0); 2705be6bf707SBarry Smith } 2706be6bf707SBarry Smith 2707f83d6046SBarry Smith 2708be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 27094a2ae208SSatish Balay #undef __FUNCT__ 27104a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ" 271117ab2063SBarry Smith /*@C 2712682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 27130d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 27146e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 271551c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 27162bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 271717ab2063SBarry Smith 2718db81eaa0SLois Curfman McInnes Collective on MPI_Comm 2719db81eaa0SLois Curfman McInnes 272017ab2063SBarry Smith Input Parameters: 2721db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 272217ab2063SBarry Smith . m - number of rows 272317ab2063SBarry Smith . n - number of columns 272417ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 272551c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 27262bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 272717ab2063SBarry Smith 272817ab2063SBarry Smith Output Parameter: 2729416022c9SBarry Smith . A - the matrix 273017ab2063SBarry Smith 2731175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 2732175b88e8SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. This is definitely 2733175b88e8SBarry Smith true if you plan to use the external direct solvers such as SuperLU, MUMPS or Spooles. 2734175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 2735175b88e8SBarry Smith 2736b259b22eSLois Curfman McInnes Notes: 273749a6f317SBarry Smith If nnz is given then nz is ignored 273849a6f317SBarry Smith 273917ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 274017ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 27410002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 274244cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 274317ab2063SBarry Smith 274417ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2745a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 27463d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 27476da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 274817ab2063SBarry Smith 2749682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 27504fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 2751682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 27526c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 27536c7ebb05SLois Curfman McInnes 27546c7ebb05SLois Curfman McInnes Options Database Keys: 2755698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2756698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2757db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 2758db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 2759db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 276017ab2063SBarry Smith 2761027ccd11SLois Curfman McInnes Level: intermediate 2762027ccd11SLois Curfman McInnes 276336db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 276436db0b34SBarry Smith 276517ab2063SBarry Smith @*/ 2766be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 276717ab2063SBarry Smith { 2768dfbe8321SBarry Smith PetscErrorCode ierr; 27696945ee14SBarry Smith 27703a40ed3dSBarry Smith PetscFunctionBegin; 2771f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 2772f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 2773273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 2774ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 2775273d9f13SBarry Smith PetscFunctionReturn(0); 2776273d9f13SBarry Smith } 2777273d9f13SBarry Smith 27784a2ae208SSatish Balay #undef __FUNCT__ 27794a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation" 2780273d9f13SBarry Smith /*@C 2781273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 2782273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 2783273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 2784273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 2785273d9f13SBarry Smith 2786273d9f13SBarry Smith Collective on MPI_Comm 2787273d9f13SBarry Smith 2788273d9f13SBarry Smith Input Parameters: 278945bfc511SMatthew Knepley + B - The matrix 2790273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 2791273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 2792273d9f13SBarry Smith (possibly different for each row) or PETSC_NULL 2793273d9f13SBarry Smith 2794273d9f13SBarry Smith Notes: 279549a6f317SBarry Smith If nnz is given then nz is ignored 279649a6f317SBarry Smith 2797273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2798273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2799273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2800273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 2801273d9f13SBarry Smith 2802273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2803273d9f13SBarry Smith Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 2804273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 2805273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 2806273d9f13SBarry Smith 2807aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 2808aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 2809aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 2810aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 2811aa95bbe8SBarry Smith 2812a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 2813a96a251dSBarry Smith entries or columns indices 2814a96a251dSBarry Smith 2815273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 2816273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 2817273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 2818273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2819273d9f13SBarry Smith 2820273d9f13SBarry Smith Options Database Keys: 2821698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2822698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2823273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2824273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2825273d9f13SBarry Smith the user still MUST index entries starting at 0! 2826273d9f13SBarry Smith 2827273d9f13SBarry Smith Level: intermediate 2828273d9f13SBarry Smith 2829aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo() 2830273d9f13SBarry Smith 2831273d9f13SBarry Smith @*/ 2832be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 2833273d9f13SBarry Smith { 283497f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]); 2835a23d5eceSKris Buschelman 2836a23d5eceSKris Buschelman PetscFunctionBegin; 2837a23d5eceSKris Buschelman ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr); 2838a23d5eceSKris Buschelman if (f) { 2839a23d5eceSKris Buschelman ierr = (*f)(B,nz,nnz);CHKERRQ(ierr); 2840a23d5eceSKris Buschelman } 2841a23d5eceSKris Buschelman PetscFunctionReturn(0); 2842a23d5eceSKris Buschelman } 2843a23d5eceSKris Buschelman 2844a23d5eceSKris Buschelman EXTERN_C_BEGIN 2845a23d5eceSKris Buschelman #undef __FUNCT__ 2846a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ" 2847be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz) 2848a23d5eceSKris Buschelman { 2849273d9f13SBarry Smith Mat_SeqAIJ *b; 2850a43ee2ecSKris Buschelman PetscTruth skipallocation = PETSC_FALSE; 28516849ba73SBarry Smith PetscErrorCode ierr; 285297f1f81fSBarry Smith PetscInt i; 2853273d9f13SBarry Smith 2854273d9f13SBarry Smith PetscFunctionBegin; 2855d5d45c9bSBarry Smith 2856a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 2857c461c341SBarry Smith skipallocation = PETSC_TRUE; 2858c461c341SBarry Smith nz = 0; 2859c461c341SBarry Smith } 2860c461c341SBarry Smith 2861899cda47SBarry Smith B->rmap.bs = B->cmap.bs = 1; 28626148ca0dSBarry Smith ierr = PetscMapSetUp(&B->rmap);CHKERRQ(ierr); 28636148ca0dSBarry Smith ierr = PetscMapSetUp(&B->cmap);CHKERRQ(ierr); 2864899cda47SBarry Smith 2865435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 2866435da068SBarry Smith if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz); 2867b73539f3SBarry Smith if (nnz) { 2868899cda47SBarry Smith for (i=0; i<B->rmap.n; i++) { 286929bbc08cSBarry Smith if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]); 2870899cda47SBarry 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); 2871b73539f3SBarry Smith } 2872b73539f3SBarry Smith } 2873b73539f3SBarry Smith 2874273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 2875273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 2876273d9f13SBarry Smith 2877ab93d7beSBarry Smith if (!skipallocation) { 28782ee49352SLisandro Dalcin if (!b->imax) { 2879899cda47SBarry Smith ierr = PetscMalloc2(B->rmap.n,PetscInt,&b->imax,B->rmap.n,PetscInt,&b->ilen);CHKERRQ(ierr); 28809518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(B,2*B->rmap.n*sizeof(PetscInt));CHKERRQ(ierr); 28812ee49352SLisandro Dalcin } 2882273d9f13SBarry Smith if (!nnz) { 2883435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 2884273d9f13SBarry Smith else if (nz <= 0) nz = 1; 2885899cda47SBarry Smith for (i=0; i<B->rmap.n; i++) b->imax[i] = nz; 2886899cda47SBarry Smith nz = nz*B->rmap.n; 2887273d9f13SBarry Smith } else { 2888273d9f13SBarry Smith nz = 0; 2889899cda47SBarry Smith for (i=0; i<B->rmap.n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 2890273d9f13SBarry Smith } 2891ab93d7beSBarry Smith /* b->ilen will count nonzeros in each row so far. */ 2892899cda47SBarry Smith for (i=0; i<B->rmap.n; i++) { b->ilen[i] = 0; } 2893ab93d7beSBarry Smith 2894273d9f13SBarry Smith /* allocate the matrix space */ 28952ee49352SLisandro Dalcin ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr); 2896899cda47SBarry Smith ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap.n+1,PetscInt,&b->i);CHKERRQ(ierr); 28979518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(B,(B->rmap.n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr); 2898bfeeae90SHong Zhang b->i[0] = 0; 2899899cda47SBarry Smith for (i=1; i<B->rmap.n+1; i++) { 29005da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 29015da197adSKris Buschelman } 2902273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 2903e6b907acSBarry Smith b->free_a = PETSC_TRUE; 2904e6b907acSBarry Smith b->free_ij = PETSC_TRUE; 2905c461c341SBarry Smith } else { 2906e6b907acSBarry Smith b->free_a = PETSC_FALSE; 2907e6b907acSBarry Smith b->free_ij = PETSC_FALSE; 2908c461c341SBarry Smith } 2909273d9f13SBarry Smith 2910273d9f13SBarry Smith b->nz = 0; 2911273d9f13SBarry Smith b->maxnz = nz; 2912273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 2913273d9f13SBarry Smith PetscFunctionReturn(0); 2914273d9f13SBarry Smith } 2915a23d5eceSKris Buschelman EXTERN_C_END 2916273d9f13SBarry Smith 2917a1661176SMatthew Knepley #undef __FUNCT__ 2918a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR" 291958d36128SBarry Smith /*@ 2920a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 2921a1661176SMatthew Knepley 2922a1661176SMatthew Knepley Input Parameters: 2923a1661176SMatthew Knepley + B - the matrix 2924a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 2925a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 2926a1661176SMatthew Knepley - v - optional values in the matrix 2927a1661176SMatthew Knepley 2928d58b2322SMatthew Knepley Contributed by: Lisandro Dalchin 2929d58b2322SMatthew Knepley 2930a1661176SMatthew Knepley Level: developer 2931a1661176SMatthew Knepley 293258d36128SBarry Smith The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays() 293358d36128SBarry Smith 2934a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential 2935a1661176SMatthew Knepley 2936a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ 2937a1661176SMatthew Knepley @*/ 2938a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 2939a1661176SMatthew Knepley { 2940a1661176SMatthew Knepley PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]); 2941a1661176SMatthew Knepley PetscErrorCode ierr; 2942a1661176SMatthew Knepley 2943a1661176SMatthew Knepley PetscFunctionBegin; 2944a1661176SMatthew Knepley PetscValidHeaderSpecific(B,MAT_COOKIE,1); 2945a1661176SMatthew Knepley ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr); 2946a1661176SMatthew Knepley if (f) { 2947a1661176SMatthew Knepley ierr = (*f)(B,i,j,v);CHKERRQ(ierr); 2948a1661176SMatthew Knepley } 2949a1661176SMatthew Knepley PetscFunctionReturn(0); 2950a1661176SMatthew Knepley } 2951a1661176SMatthew Knepley 2952a1661176SMatthew Knepley EXTERN_C_BEGIN 2953a1661176SMatthew Knepley #undef __FUNCT__ 2954a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR_SeqAIJ" 2955b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 2956a1661176SMatthew Knepley { 2957a1661176SMatthew Knepley PetscInt i; 2958a1661176SMatthew Knepley PetscInt m,n; 2959a1661176SMatthew Knepley PetscInt nz; 2960a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 2961a1661176SMatthew Knepley PetscScalar *values; 2962a1661176SMatthew Knepley PetscErrorCode ierr; 2963a1661176SMatthew Knepley 2964a1661176SMatthew Knepley PetscFunctionBegin; 2965a1661176SMatthew Knepley ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 2966a1661176SMatthew Knepley 2967b7940d39SSatish Balay if (Ii[0]) { 2968b7940d39SSatish Balay SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]); 2969a1661176SMatthew Knepley } 2970a1661176SMatthew Knepley ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr); 2971a1661176SMatthew Knepley for(i = 0; i < m; i++) { 2972b7940d39SSatish Balay nz = Ii[i+1]- Ii[i]; 2973a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 2974a1661176SMatthew Knepley if (nz < 0) { 2975a1661176SMatthew Knepley SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 2976a1661176SMatthew Knepley } 2977a1661176SMatthew Knepley nnz[i] = nz; 2978a1661176SMatthew Knepley } 2979a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 2980a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 2981a1661176SMatthew Knepley 2982a1661176SMatthew Knepley if (v) { 2983a1661176SMatthew Knepley values = (PetscScalar*) v; 2984a1661176SMatthew Knepley } else { 2985a1661176SMatthew Knepley ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr); 2986a1661176SMatthew Knepley ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr); 2987a1661176SMatthew Knepley } 2988a1661176SMatthew Knepley 2989a1661176SMatthew Knepley for(i = 0; i < m; i++) { 2990b7940d39SSatish Balay nz = Ii[i+1] - Ii[i]; 2991b7940d39SSatish Balay ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr); 2992a1661176SMatthew Knepley } 2993a1661176SMatthew Knepley 2994a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2995a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2996a1661176SMatthew Knepley 2997a1661176SMatthew Knepley if (!v) { 2998a1661176SMatthew Knepley ierr = PetscFree(values);CHKERRQ(ierr); 2999a1661176SMatthew Knepley } 3000a1661176SMatthew Knepley PetscFunctionReturn(0); 3001a1661176SMatthew Knepley } 3002a1661176SMatthew Knepley EXTERN_C_END 3003a1661176SMatthew Knepley 30040bad9183SKris Buschelman /*MC 3005fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 30060bad9183SKris Buschelman based on compressed sparse row format. 30070bad9183SKris Buschelman 30080bad9183SKris Buschelman Options Database Keys: 30090bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 30100bad9183SKris Buschelman 30110bad9183SKris Buschelman Level: beginner 30120bad9183SKris Buschelman 3013f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 30140bad9183SKris Buschelman M*/ 30150bad9183SKris Buschelman 3016a6175056SHong Zhang EXTERN_C_BEGIN 301717667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*); 301817667f90SBarry Smith EXTERN_C_END 301917667f90SBarry Smith 302017667f90SBarry Smith EXTERN_C_BEGIN 30214a2ae208SSatish Balay #undef __FUNCT__ 30224a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ" 3023be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B) 3024273d9f13SBarry Smith { 3025273d9f13SBarry Smith Mat_SeqAIJ *b; 3026dfbe8321SBarry Smith PetscErrorCode ierr; 302738baddfdSBarry Smith PetscMPIInt size; 3028273d9f13SBarry Smith 3029273d9f13SBarry Smith PetscFunctionBegin; 30307adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 3031273d9f13SBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 3032273d9f13SBarry Smith 303338f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr); 3034b0a32e0cSBarry Smith B->data = (void*)b; 3035549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 3036416022c9SBarry Smith B->factor = 0; 303790f02eecSBarry Smith B->mapping = 0; 3038416022c9SBarry Smith b->row = 0; 3039416022c9SBarry Smith b->col = 0; 304082bf6240SBarry Smith b->icol = 0; 3041b810aeb4SBarry Smith b->reallocs = 0; 304236db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 3043f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 3044416022c9SBarry Smith b->nonew = 0; 3045416022c9SBarry Smith b->diag = 0; 3046416022c9SBarry Smith b->solve_work = 0; 30472a1b7f2aSHong Zhang B->spptr = 0; 3048be6bf707SBarry Smith b->saved_values = 0; 3049d7f994e1SBarry Smith b->idiag = 0; 305071f1c65dSBarry Smith b->mdiag = 0; 305171f1c65dSBarry Smith b->ssor_work = 0; 305271f1c65dSBarry Smith b->omega = 1.0; 305371f1c65dSBarry Smith b->fshift = 0.0; 305471f1c65dSBarry Smith b->idiagvalid = PETSC_FALSE; 3055f1e2ffcdSBarry Smith b->keepzeroedrows = PETSC_FALSE; 3056a30b2313SHong Zhang b->xtoy = 0; 3057a30b2313SHong Zhang b->XtoY = 0; 305873e7a558SHong Zhang b->compressedrow.use = PETSC_FALSE; 3059899cda47SBarry Smith b->compressedrow.nrows = B->rmap.n; 3060d487561eSHong Zhang b->compressedrow.i = PETSC_NULL; 3061d487561eSHong Zhang b->compressedrow.rindex = PETSC_NULL; 3062d487561eSHong Zhang b->compressedrow.checked = PETSC_FALSE; 306388e51ccdSHong Zhang B->same_nonzero = PETSC_FALSE; 306417ab2063SBarry Smith 306535d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 3066f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C", 3067bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 3068bc4b532fSSatish Balay MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 3069f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 3070be6bf707SBarry Smith "MatStoreValues_SeqAIJ", 3071bc4b532fSSatish Balay MatStoreValues_SeqAIJ);CHKERRQ(ierr); 3072f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 3073be6bf707SBarry Smith "MatRetrieveValues_SeqAIJ", 3074bc4b532fSSatish Balay MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 3075a6175056SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C", 3076a6175056SHong Zhang "MatConvert_SeqAIJ_SeqSBAIJ", 3077a6175056SHong Zhang MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 307885fc7724SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C", 307985fc7724SBarry Smith "MatConvert_SeqAIJ_SeqBAIJ", 308085fc7724SBarry Smith MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 3081ba03775dSHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C", 3082ba03775dSHong Zhang "MatConvert_SeqAIJ_SeqCSRPERM", 3083ba03775dSHong Zhang MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr); 308417667f90SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C", 308517667f90SBarry Smith "MatConvert_SeqAIJ_SeqCRL", 308617667f90SBarry Smith MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr); 30875fbd3699SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 30885fbd3699SBarry Smith "MatIsTranspose_SeqAIJ", 30895fbd3699SBarry Smith MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 30901cbb95d3SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C", 30911cbb95d3SBarry Smith "MatIsHermitianTranspose_SeqAIJ", 30921cbb95d3SBarry Smith MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 3093a23d5eceSKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C", 3094a23d5eceSKris Buschelman "MatSeqAIJSetPreallocation_SeqAIJ", 3095a23d5eceSKris Buschelman MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 3096a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C", 3097a1661176SMatthew Knepley "MatSeqAIJSetPreallocationCSR_SeqAIJ", 3098a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 309905b94e36SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C", 310005b94e36SKris Buschelman "MatReorderForNonzeroDiagonal_SeqAIJ", 310105b94e36SKris Buschelman MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 31024846f1f5SKris Buschelman ierr = MatCreate_Inode(B);CHKERRQ(ierr); 310317667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 31043a40ed3dSBarry Smith PetscFunctionReturn(0); 310517ab2063SBarry Smith } 3106273d9f13SBarry Smith EXTERN_C_END 310717ab2063SBarry Smith 31084a2ae208SSatish Balay #undef __FUNCT__ 31094a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ" 3110dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 311117ab2063SBarry Smith { 3112416022c9SBarry Smith Mat C; 3113416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 31146849ba73SBarry Smith PetscErrorCode ierr; 3115899cda47SBarry Smith PetscInt i,m = A->rmap.n; 311617ab2063SBarry Smith 31173a40ed3dSBarry Smith PetscFunctionBegin; 31184043dd9cSLois Curfman McInnes *B = 0; 31197adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 3120899cda47SBarry Smith ierr = MatSetSizes(C,A->rmap.n,A->cmap.n,A->rmap.n,A->cmap.n);CHKERRQ(ierr); 31217adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 31221d5dac46SHong Zhang ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 31231d5dac46SHong Zhang 31247adad957SLisandro Dalcin ierr = PetscMapCopy(((PetscObject)A)->comm,&A->rmap,&C->rmap);CHKERRQ(ierr); 31257adad957SLisandro Dalcin ierr = PetscMapCopy(((PetscObject)A)->comm,&A->cmap,&C->cmap);CHKERRQ(ierr); 3126899cda47SBarry Smith 3127273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 3128273d9f13SBarry Smith 3129416022c9SBarry Smith C->factor = A->factor; 31306ad4291fSHong Zhang 3131416022c9SBarry Smith c->row = 0; 3132416022c9SBarry Smith c->col = 0; 313382bf6240SBarry Smith c->icol = 0; 31346ad4291fSHong Zhang c->reallocs = 0; 313517ab2063SBarry Smith 31366ad4291fSHong Zhang C->assembled = PETSC_TRUE; 313717ab2063SBarry Smith 313833b91e9fSSatish Balay ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr); 31399518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr); 314017ab2063SBarry Smith for (i=0; i<m; i++) { 3141416022c9SBarry Smith c->imax[i] = a->imax[i]; 3142416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 314317ab2063SBarry Smith } 314417ab2063SBarry Smith 314517ab2063SBarry Smith /* allocate the matrix space */ 3146a96a251dSBarry Smith ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr); 31479518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 3148f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 314997f1f81fSBarry Smith ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 315017ab2063SBarry Smith if (m > 0) { 315197f1f81fSBarry Smith ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr); 3152be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 3153bfeeae90SHong Zhang ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 3154be6bf707SBarry Smith } else { 3155bfeeae90SHong Zhang ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 315617ab2063SBarry Smith } 315708480c60SBarry Smith } 315817ab2063SBarry Smith 31596ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 3160416022c9SBarry Smith c->roworiented = a->roworiented; 3161416022c9SBarry Smith c->nonew = a->nonew; 3162416022c9SBarry Smith if (a->diag) { 316397f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr); 316452e6d16bSBarry Smith ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 316517ab2063SBarry Smith for (i=0; i<m; i++) { 3166416022c9SBarry Smith c->diag[i] = a->diag[i]; 316717ab2063SBarry Smith } 31683a40ed3dSBarry Smith } else c->diag = 0; 31696ad4291fSHong Zhang c->solve_work = 0; 31706ad4291fSHong Zhang c->saved_values = 0; 31716ad4291fSHong Zhang c->idiag = 0; 317271f1c65dSBarry Smith c->ssor_work = 0; 31736ad4291fSHong Zhang c->keepzeroedrows = a->keepzeroedrows; 3174e6b907acSBarry Smith c->free_a = PETSC_TRUE; 3175e6b907acSBarry Smith c->free_ij = PETSC_TRUE; 31766ad4291fSHong Zhang c->xtoy = 0; 31776ad4291fSHong Zhang c->XtoY = 0; 31786ad4291fSHong Zhang 3179416022c9SBarry Smith c->nz = a->nz; 3180416022c9SBarry Smith c->maxnz = a->maxnz; 3181273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 3182754ec7b1SSatish Balay 31836ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 31846ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 31856ad4291fSHong Zhang c->compressedrow.checked = a->compressedrow.checked; 31866ad4291fSHong Zhang if ( a->compressedrow.checked && a->compressedrow.use){ 31876ad4291fSHong Zhang i = a->compressedrow.nrows; 31886ad4291fSHong Zhang ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr); 31896ad4291fSHong Zhang c->compressedrow.rindex = c->compressedrow.i + i + 1; 31906ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr); 31916ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr); 319227ea64f8SHong Zhang } else { 319327ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 319427ea64f8SHong Zhang c->compressedrow.i = PETSC_NULL; 319527ea64f8SHong Zhang c->compressedrow.rindex = PETSC_NULL; 31966ad4291fSHong Zhang } 319788e51ccdSHong Zhang C->same_nonzero = A->same_nonzero; 31984846f1f5SKris Buschelman ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr); 31994846f1f5SKris Buschelman 3200416022c9SBarry Smith *B = C; 32017adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr); 32023a40ed3dSBarry Smith PetscFunctionReturn(0); 320317ab2063SBarry Smith } 320417ab2063SBarry Smith 32054a2ae208SSatish Balay #undef __FUNCT__ 32064a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ" 3207f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A) 320817ab2063SBarry Smith { 3209416022c9SBarry Smith Mat_SeqAIJ *a; 3210416022c9SBarry Smith Mat B; 32116849ba73SBarry Smith PetscErrorCode ierr; 32123c601197SSatish Balay PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N; 321338baddfdSBarry Smith int fd; 321438baddfdSBarry Smith PetscMPIInt size; 3215bcd2baecSBarry Smith MPI_Comm comm; 321617ab2063SBarry Smith 32173a40ed3dSBarry Smith PetscFunctionBegin; 3218e864ced6SBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 3219d132466eSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 322029bbc08cSBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor"); 3221b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 32220752156aSBarry Smith ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 3223552e946dSBarry Smith if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 322417ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 322517ab2063SBarry Smith 3226d64ed03dSBarry Smith if (nz < 0) { 322729bbc08cSBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 3228d64ed03dSBarry Smith } 3229d64ed03dSBarry Smith 323017ab2063SBarry Smith /* read in row lengths */ 323197f1f81fSBarry Smith ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 32320752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 323317ab2063SBarry Smith 32343c601197SSatish Balay /* check if sum of rowlengths is same as nz */ 32353c601197SSatish Balay for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 32363c601197SSatish Balay if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum); 32373c601197SSatish Balay 323817ab2063SBarry Smith /* create our matrix */ 3239f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B);CHKERRQ(ierr); 3240f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 3241b3a1e11cSKris Buschelman ierr = MatSetType(B,type);CHKERRQ(ierr); 3242ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr); 3243416022c9SBarry Smith a = (Mat_SeqAIJ*)B->data; 324417ab2063SBarry Smith 32450752156aSBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 324617ab2063SBarry Smith 324717ab2063SBarry Smith /* read in nonzero values */ 32480752156aSBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 324917ab2063SBarry Smith 325017ab2063SBarry Smith /* set matrix "i" values */ 3251efb16452SHong Zhang a->i[0] = 0; 325217ab2063SBarry Smith for (i=1; i<= M; i++) { 3253416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 3254416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 325517ab2063SBarry Smith } 3256606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 325717ab2063SBarry Smith 32586d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 32596d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3260b3a1e11cSKris Buschelman *A = B; 32613a40ed3dSBarry Smith PetscFunctionReturn(0); 326217ab2063SBarry Smith } 326317ab2063SBarry Smith 32644a2ae208SSatish Balay #undef __FUNCT__ 3265b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ" 3266dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg) 32677264ac53SSatish Balay { 32687264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data; 3269dfbe8321SBarry Smith PetscErrorCode ierr; 32707264ac53SSatish Balay 32713a40ed3dSBarry Smith PetscFunctionBegin; 3272bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 3273899cda47SBarry Smith if ((A->rmap.n != B->rmap.n) || (A->cmap.n != B->cmap.n) ||(a->nz != b->nz)) { 3274ca44d042SBarry Smith *flg = PETSC_FALSE; 3275ca44d042SBarry Smith PetscFunctionReturn(0); 3276bcd2baecSBarry Smith } 32777264ac53SSatish Balay 32787264ac53SSatish Balay /* if the a->i are the same */ 3279899cda47SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->rmap.n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3280abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 32817264ac53SSatish Balay 32827264ac53SSatish Balay /* if a->j are the same */ 328397f1f81fSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3284abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 3285bcd2baecSBarry Smith 3286bcd2baecSBarry Smith /* if a->a are the same */ 328787828ca2SBarry Smith ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 32880f5bd95cSBarry Smith 32893a40ed3dSBarry Smith PetscFunctionReturn(0); 32907264ac53SSatish Balay 32917264ac53SSatish Balay } 329236db0b34SBarry Smith 32934a2ae208SSatish Balay #undef __FUNCT__ 32944a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays" 329505869f15SSatish Balay /*@ 329636db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 329736db0b34SBarry Smith provided by the user. 329836db0b34SBarry Smith 3299c75a6043SHong Zhang Collective on MPI_Comm 330036db0b34SBarry Smith 330136db0b34SBarry Smith Input Parameters: 330236db0b34SBarry Smith + comm - must be an MPI communicator of size 1 330336db0b34SBarry Smith . m - number of rows 330436db0b34SBarry Smith . n - number of columns 330536db0b34SBarry Smith . i - row indices 330636db0b34SBarry Smith . j - column indices 330736db0b34SBarry Smith - a - matrix values 330836db0b34SBarry Smith 330936db0b34SBarry Smith Output Parameter: 331036db0b34SBarry Smith . mat - the matrix 331136db0b34SBarry Smith 331236db0b34SBarry Smith Level: intermediate 331336db0b34SBarry Smith 331436db0b34SBarry Smith Notes: 33150551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 331636db0b34SBarry Smith once the matrix is destroyed 331736db0b34SBarry Smith 331836db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 331936db0b34SBarry Smith 3320bfeeae90SHong Zhang The i and j indices are 0 based 332136db0b34SBarry Smith 3322a4552177SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 3323a4552177SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 3324a4552177SSatish Balay as shown: 3325a4552177SSatish Balay 3326a4552177SSatish Balay 1 0 0 3327a4552177SSatish Balay 2 0 3 3328a4552177SSatish Balay 4 5 6 3329a4552177SSatish Balay 3330a4552177SSatish Balay i = {0,1,3,6} [size = nrow+1 = 3+1] 33319985e31cSBarry Smith j = {0,0,2,0,1,2} [size = nz = 6]; values must be sorted for each row 3332a4552177SSatish Balay v = {1,2,3,4,5,6} [size = nz = 6] 3333a4552177SSatish Balay 33349985e31cSBarry Smith 33352fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 333636db0b34SBarry Smith 333736db0b34SBarry Smith @*/ 3338be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat) 333936db0b34SBarry Smith { 3340dfbe8321SBarry Smith PetscErrorCode ierr; 3341cbcfb4deSHong Zhang PetscInt ii; 334236db0b34SBarry Smith Mat_SeqAIJ *aij; 3343cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG) 3344cbcfb4deSHong Zhang PetscInt jj; 3345cbcfb4deSHong Zhang #endif 334636db0b34SBarry Smith 334736db0b34SBarry Smith PetscFunctionBegin; 3348a96a251dSBarry Smith if (i[0]) { 3349634064b4SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 335036db0b34SBarry Smith } 3351f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3352f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 3353ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 3354ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 3355ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 3356ab93d7beSBarry Smith ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr); 3357ab93d7beSBarry Smith 335836db0b34SBarry Smith aij->i = i; 335936db0b34SBarry Smith aij->j = j; 336036db0b34SBarry Smith aij->a = a; 336136db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 336236db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 3363e6b907acSBarry Smith aij->free_a = PETSC_FALSE; 3364e6b907acSBarry Smith aij->free_ij = PETSC_FALSE; 336536db0b34SBarry Smith 336636db0b34SBarry Smith for (ii=0; ii<m; ii++) { 336736db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 33682515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 336979a5c55eSBarry 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]); 33709985e31cSBarry Smith for (jj=i[ii]+1; jj<i[ii+1]; jj++) { 33719985e31cSBarry 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); 33729985e31cSBarry 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); 33739985e31cSBarry Smith } 337436db0b34SBarry Smith #endif 337536db0b34SBarry Smith } 33762515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 337736db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 337879a5c55eSBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]); 337979a5c55eSBarry Smith if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]); 338036db0b34SBarry Smith } 338136db0b34SBarry Smith #endif 338236db0b34SBarry Smith 3383b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3384b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 338536db0b34SBarry Smith PetscFunctionReturn(0); 338636db0b34SBarry Smith } 338736db0b34SBarry Smith 3388cc8ba8e1SBarry Smith #undef __FUNCT__ 3389ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ" 3390dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring) 3391cc8ba8e1SBarry Smith { 3392dfbe8321SBarry Smith PetscErrorCode ierr; 3393cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 339436db0b34SBarry Smith 3395cc8ba8e1SBarry Smith PetscFunctionBegin; 33968ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 3397cc8ba8e1SBarry Smith ierr = ISColoringReference(coloring);CHKERRQ(ierr); 3398cc8ba8e1SBarry Smith a->coloring = coloring; 339912c595b3SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 340097f1f81fSBarry Smith PetscInt i,*larray; 340112c595b3SBarry Smith ISColoring ocoloring; 340208b6dcc0SBarry Smith ISColoringValue *colors; 340312c595b3SBarry Smith 340412c595b3SBarry Smith /* set coloring for diagonal portion */ 3405899cda47SBarry Smith ierr = PetscMalloc((A->cmap.n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 3406899cda47SBarry Smith for (i=0; i<A->cmap.n; i++) { 340712c595b3SBarry Smith larray[i] = i; 340812c595b3SBarry Smith } 3409899cda47SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap.n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 3410899cda47SBarry Smith ierr = PetscMalloc((A->cmap.n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 3411899cda47SBarry Smith for (i=0; i<A->cmap.n; i++) { 341212c595b3SBarry Smith colors[i] = coloring->colors[larray[i]]; 341312c595b3SBarry Smith } 341412c595b3SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 341595a80f87SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap.n,colors,&ocoloring);CHKERRQ(ierr); 341612c595b3SBarry Smith a->coloring = ocoloring; 341712c595b3SBarry Smith } 3418cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3419cc8ba8e1SBarry Smith } 3420cc8ba8e1SBarry Smith 3421dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 3422ee4f033dSBarry Smith EXTERN_C_BEGIN 342329c1e371SBarry Smith #include "adic/ad_utils.h" 3424ee4f033dSBarry Smith EXTERN_C_END 3425cc8ba8e1SBarry Smith 3426cc8ba8e1SBarry Smith #undef __FUNCT__ 3427ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 3428dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 3429cc8ba8e1SBarry Smith { 3430cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3431899cda47SBarry Smith PetscInt m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j,nlen; 34324440f671SBarry Smith PetscScalar *v = a->a,*values = ((PetscScalar*)advalues)+1; 343308b6dcc0SBarry Smith ISColoringValue *color; 3434cc8ba8e1SBarry Smith 3435cc8ba8e1SBarry Smith PetscFunctionBegin; 3436e005ede5SBarry Smith if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 34374440f671SBarry Smith nlen = PetscADGetDerivTypeSize()/sizeof(PetscScalar); 3438cc8ba8e1SBarry Smith color = a->coloring->colors; 3439cc8ba8e1SBarry Smith /* loop over rows */ 3440cc8ba8e1SBarry Smith for (i=0; i<m; i++) { 3441cc8ba8e1SBarry Smith nz = ii[i+1] - ii[i]; 3442cc8ba8e1SBarry Smith /* loop over columns putting computed value into matrix */ 3443cc8ba8e1SBarry Smith for (j=0; j<nz; j++) { 3444cc8ba8e1SBarry Smith *v++ = values[color[*jj++]]; 3445cc8ba8e1SBarry Smith } 34464440f671SBarry Smith values += nlen; /* jump to next row of derivatives */ 3447ee4f033dSBarry Smith } 3448ee4f033dSBarry Smith PetscFunctionReturn(0); 3449ee4f033dSBarry Smith } 3450ee4f033dSBarry Smith #endif 3451ee4f033dSBarry Smith 3452ee4f033dSBarry Smith #undef __FUNCT__ 3453ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ" 345497f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues) 3455ee4f033dSBarry Smith { 3456ee4f033dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3457899cda47SBarry Smith PetscInt m = A->rmap.n,*ii = a->i,*jj = a->j,nz,i,j; 345887828ca2SBarry Smith PetscScalar *v = a->a,*values = (PetscScalar *)advalues; 345908b6dcc0SBarry Smith ISColoringValue *color; 3460ee4f033dSBarry Smith 3461ee4f033dSBarry Smith PetscFunctionBegin; 3462e005ede5SBarry Smith if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 3463ee4f033dSBarry Smith color = a->coloring->colors; 3464ee4f033dSBarry Smith /* loop over rows */ 3465ee4f033dSBarry Smith for (i=0; i<m; i++) { 3466ee4f033dSBarry Smith nz = ii[i+1] - ii[i]; 3467ee4f033dSBarry Smith /* loop over columns putting computed value into matrix */ 3468ee4f033dSBarry Smith for (j=0; j<nz; j++) { 3469ee4f033dSBarry Smith *v++ = values[color[*jj++]]; 3470ee4f033dSBarry Smith } 3471ee4f033dSBarry Smith values += nl; /* jump to next row of derivatives */ 3472cc8ba8e1SBarry Smith } 3473cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3474cc8ba8e1SBarry Smith } 347536db0b34SBarry Smith 347681824310SBarry Smith /* 347781824310SBarry Smith Special version for direct calls from Fortran 347881824310SBarry Smith */ 347981824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 348081824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 348181824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 348281824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 348381824310SBarry Smith #endif 348481824310SBarry Smith 348581824310SBarry Smith /* Change these macros so can be used in void function */ 348681824310SBarry Smith #undef CHKERRQ 34877adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr) 348881824310SBarry Smith #undef SETERRQ2 34897adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr) 349081824310SBarry Smith 349181824310SBarry Smith EXTERN_C_BEGIN 349281824310SBarry Smith #undef __FUNCT__ 349381824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_" 34941f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr) 349581824310SBarry Smith { 349681824310SBarry Smith Mat A = *AA; 349781824310SBarry Smith PetscInt m = *mm, n = *nn; 349881824310SBarry Smith InsertMode is = *isis; 349981824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 350081824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 350181824310SBarry Smith PetscInt *imax,*ai,*ailen; 350281824310SBarry Smith PetscErrorCode ierr; 350381824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 350481824310SBarry Smith PetscScalar *ap,value,*aa; 3505edb03aefSBarry Smith PetscTruth ignorezeroentries = a->ignorezeroentries; 350681824310SBarry Smith PetscTruth roworiented = a->roworiented; 350781824310SBarry Smith 350881824310SBarry Smith PetscFunctionBegin; 350981824310SBarry Smith MatPreallocated(A); 351081824310SBarry Smith imax = a->imax; 351181824310SBarry Smith ai = a->i; 351281824310SBarry Smith ailen = a->ilen; 351381824310SBarry Smith aj = a->j; 351481824310SBarry Smith aa = a->a; 351581824310SBarry Smith 351681824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 351781824310SBarry Smith row = im[k]; 351881824310SBarry Smith if (row < 0) continue; 351981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 35207adad957SLisandro Dalcin if (row >= A->rmap.n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 352181824310SBarry Smith #endif 352281824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 352381824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 352481824310SBarry Smith low = 0; 352581824310SBarry Smith high = nrow; 352681824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 352781824310SBarry Smith if (in[l] < 0) continue; 352881824310SBarry Smith #if defined(PETSC_USE_DEBUG) 35297adad957SLisandro Dalcin if (in[l] >= A->cmap.n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 353081824310SBarry Smith #endif 353181824310SBarry Smith col = in[l]; 353281824310SBarry Smith if (roworiented) { 353381824310SBarry Smith value = v[l + k*n]; 353481824310SBarry Smith } else { 353581824310SBarry Smith value = v[k + l*m]; 353681824310SBarry Smith } 353781824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 353881824310SBarry Smith 353981824310SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 354081824310SBarry Smith lastcol = col; 354181824310SBarry Smith while (high-low > 5) { 354281824310SBarry Smith t = (low+high)/2; 354381824310SBarry Smith if (rp[t] > col) high = t; 354481824310SBarry Smith else low = t; 354581824310SBarry Smith } 354681824310SBarry Smith for (i=low; i<high; i++) { 354781824310SBarry Smith if (rp[i] > col) break; 354881824310SBarry Smith if (rp[i] == col) { 354981824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 355081824310SBarry Smith else ap[i] = value; 355181824310SBarry Smith goto noinsert; 355281824310SBarry Smith } 355381824310SBarry Smith } 355481824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 355581824310SBarry Smith if (nonew == 1) goto noinsert; 35567adad957SLisandro Dalcin if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 3557421e10b8SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap.n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 355881824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 355981824310SBarry Smith /* shift up all the later entries in this row */ 356081824310SBarry Smith for (ii=N; ii>=i; ii--) { 356181824310SBarry Smith rp[ii+1] = rp[ii]; 356281824310SBarry Smith ap[ii+1] = ap[ii]; 356381824310SBarry Smith } 356481824310SBarry Smith rp[i] = col; 356581824310SBarry Smith ap[i] = value; 356681824310SBarry Smith noinsert:; 356781824310SBarry Smith low = i + 1; 356881824310SBarry Smith } 356981824310SBarry Smith ailen[row] = nrow; 357081824310SBarry Smith } 357181824310SBarry Smith A->same_nonzero = PETSC_FALSE; 357281824310SBarry Smith PetscFunctionReturnVoid(); 357381824310SBarry Smith } 357481824310SBarry Smith EXTERN_C_END 3575