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 /* only works if matrix has a full set of diagonal entries */ 1679299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is) 1779299369SBarry Smith { 1879299369SBarry Smith PetscErrorCode ierr; 1979299369SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) Y->data; 2079299369SBarry Smith PetscInt i,*diag, m = Y->m; 2179299369SBarry Smith PetscScalar *v,*aa = aij->a; 2279299369SBarry Smith 2379299369SBarry Smith PetscFunctionBegin; 2479299369SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(Y);CHKERRQ(ierr); 2579299369SBarry Smith diag = aij->diag; 2679299369SBarry Smith ierr = VecGetArray(D,&v);CHKERRQ(ierr); 2779299369SBarry Smith if (is == INSERT_VALUES) { 2879299369SBarry Smith for (i=0; i<m; i++) { 2979299369SBarry Smith aa[diag[i]] = v[i]; 3079299369SBarry Smith } 3179299369SBarry Smith } else { 3279299369SBarry Smith for (i=0; i<m; i++) { 3379299369SBarry Smith aa[diag[i]] += v[i]; 3479299369SBarry Smith } 3579299369SBarry Smith } 3679299369SBarry Smith ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 3779299369SBarry Smith PetscFunctionReturn(0); 3879299369SBarry Smith } 3979299369SBarry Smith 4079299369SBarry Smith #undef __FUNCT__ 414a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ" 4297f1f81fSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 4317ab2063SBarry Smith { 44416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 45dfbe8321SBarry Smith PetscErrorCode ierr; 4697f1f81fSBarry Smith PetscInt i,ishift; 4717ab2063SBarry Smith 483a40ed3dSBarry Smith PetscFunctionBegin; 4931625ec5SSatish Balay *m = A->m; 503a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 51bfeeae90SHong Zhang ishift = 0; 5253e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 53273d9f13SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 54bfeeae90SHong Zhang } else if (oshift == 1) { 5597f1f81fSBarry Smith PetscInt nz = a->i[A->m]; 563b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 5797f1f81fSBarry Smith ierr = PetscMalloc((A->m+1)*sizeof(PetscInt),ia);CHKERRQ(ierr); 5897f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr); 593b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1; 60273d9f13SBarry Smith for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1; 616945ee14SBarry Smith } else { 626945ee14SBarry Smith *ia = a->i; *ja = a->j; 63a2ce50c7SBarry Smith } 643a40ed3dSBarry Smith PetscFunctionReturn(0); 65a2744918SBarry Smith } 66a2744918SBarry Smith 674a2ae208SSatish Balay #undef __FUNCT__ 684a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ" 6997f1f81fSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 706945ee14SBarry Smith { 71dfbe8321SBarry Smith PetscErrorCode ierr; 726945ee14SBarry Smith 733a40ed3dSBarry Smith PetscFunctionBegin; 743a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 75bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 76606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 77606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 78bcd2baecSBarry Smith } 793a40ed3dSBarry Smith PetscFunctionReturn(0); 8017ab2063SBarry Smith } 8117ab2063SBarry Smith 824a2ae208SSatish Balay #undef __FUNCT__ 834a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ" 8497f1f81fSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 853b2fbd54SBarry Smith { 863b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 87dfbe8321SBarry Smith PetscErrorCode ierr; 8897f1f81fSBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->n,m = A->m; 8997f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 903b2fbd54SBarry Smith 913a40ed3dSBarry Smith PetscFunctionBegin; 923b2fbd54SBarry Smith *nn = A->n; 933a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 943b2fbd54SBarry Smith if (symmetric) { 95bfeeae90SHong Zhang ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr); 963b2fbd54SBarry Smith } else { 9797f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr); 9897f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 9997f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr); 10097f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr); 1013b2fbd54SBarry Smith jj = a->j; 1023b2fbd54SBarry Smith for (i=0; i<nz; i++) { 103bfeeae90SHong Zhang collengths[jj[i]]++; 1043b2fbd54SBarry Smith } 1053b2fbd54SBarry Smith cia[0] = oshift; 1063b2fbd54SBarry Smith for (i=0; i<n; i++) { 1073b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 1083b2fbd54SBarry Smith } 10997f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 1103b2fbd54SBarry Smith jj = a->j; 111a93ec695SBarry Smith for (row=0; row<m; row++) { 112a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 113a93ec695SBarry Smith for (i=0; i<mr; i++) { 114bfeeae90SHong Zhang col = *jj++; 1153b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 1163b2fbd54SBarry Smith } 1173b2fbd54SBarry Smith } 118606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 1193b2fbd54SBarry Smith *ia = cia; *ja = cja; 1203b2fbd54SBarry Smith } 1213a40ed3dSBarry Smith PetscFunctionReturn(0); 1223b2fbd54SBarry Smith } 1233b2fbd54SBarry Smith 1244a2ae208SSatish Balay #undef __FUNCT__ 1254a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ" 12697f1f81fSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done) 1273b2fbd54SBarry Smith { 128dfbe8321SBarry Smith PetscErrorCode ierr; 129606d414cSSatish Balay 1303a40ed3dSBarry Smith PetscFunctionBegin; 1313a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1323b2fbd54SBarry Smith 133606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 134606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 1353b2fbd54SBarry Smith 1363a40ed3dSBarry Smith PetscFunctionReturn(0); 1373b2fbd54SBarry Smith } 1383b2fbd54SBarry Smith 13987d4246cSBarry Smith #undef __FUNCT__ 14087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ" 14187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 14287d4246cSBarry Smith { 14387d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 14487d4246cSBarry Smith PetscInt *ai = a->i; 14587d4246cSBarry Smith PetscErrorCode ierr; 14687d4246cSBarry Smith 14787d4246cSBarry Smith PetscFunctionBegin; 14887d4246cSBarry Smith ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr); 14987d4246cSBarry Smith PetscFunctionReturn(0); 15087d4246cSBarry Smith } 15187d4246cSBarry Smith 152227d817aSBarry Smith #define CHUNKSIZE 15 15317ab2063SBarry Smith 1544a2ae208SSatish Balay #undef __FUNCT__ 1554a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ" 15697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 15717ab2063SBarry Smith { 158416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 159e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 16097f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 1616849ba73SBarry Smith PetscErrorCode ierr; 162e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 16387828ca2SBarry Smith PetscScalar *ap,value,*aa = a->a; 16436db0b34SBarry Smith PetscTruth ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE); 165273d9f13SBarry Smith PetscTruth roworiented = a->roworiented; 16617ab2063SBarry Smith 1673a40ed3dSBarry Smith PetscFunctionBegin; 16817ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 169416022c9SBarry Smith row = im[k]; 1705ef9f2a5SBarry Smith if (row < 0) continue; 1712515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 17277431f27SBarry Smith if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1); 1733b2fbd54SBarry Smith #endif 174bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 17517ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 176416022c9SBarry Smith low = 0; 177c71e6ed7SBarry Smith high = nrow; 17817ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 1795ef9f2a5SBarry Smith if (in[l] < 0) continue; 1802515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 18177431f27SBarry Smith if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1); 1823b2fbd54SBarry Smith #endif 183bfeeae90SHong Zhang col = in[l]; 1844b0e389bSBarry Smith if (roworiented) { 1855ef9f2a5SBarry Smith value = v[l + k*n]; 186bef8e0ddSBarry Smith } else { 1874b0e389bSBarry Smith value = v[k + l*m]; 1884b0e389bSBarry Smith } 189abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 19036db0b34SBarry Smith 1917cd84e04SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 192e2ee6c50SBarry Smith lastcol = col; 193416022c9SBarry Smith while (high-low > 5) { 194416022c9SBarry Smith t = (low+high)/2; 195416022c9SBarry Smith if (rp[t] > col) high = t; 196416022c9SBarry Smith else low = t; 19717ab2063SBarry Smith } 198416022c9SBarry Smith for (i=low; i<high; i++) { 19917ab2063SBarry Smith if (rp[i] > col) break; 20017ab2063SBarry Smith if (rp[i] == col) { 201416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 20217ab2063SBarry Smith else ap[i] = value; 20317ab2063SBarry Smith goto noinsert; 20417ab2063SBarry Smith } 20517ab2063SBarry Smith } 206abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 207c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 208cc72174dSBarry Smith if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 209ed1caa07SMatthew Knepley MatSeqXAIJReallocateAIJ(a,1,nrow,row,col,rmax,aa,ai,aj,A->m,rp,ap,imax,nonew); 210c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 211416022c9SBarry Smith /* shift up all the later entries in this row */ 212416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 21317ab2063SBarry Smith rp[ii+1] = rp[ii]; 21417ab2063SBarry Smith ap[ii+1] = ap[ii]; 21517ab2063SBarry Smith } 21617ab2063SBarry Smith rp[i] = col; 21717ab2063SBarry Smith ap[i] = value; 21817ab2063SBarry Smith noinsert:; 219416022c9SBarry Smith low = i + 1; 22017ab2063SBarry Smith } 22117ab2063SBarry Smith ailen[row] = nrow; 22217ab2063SBarry Smith } 22388e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 2243a40ed3dSBarry Smith PetscFunctionReturn(0); 22517ab2063SBarry Smith } 22617ab2063SBarry Smith 22781824310SBarry Smith 2284a2ae208SSatish Balay #undef __FUNCT__ 2294a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ" 23097f1f81fSBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 2317eb43aa7SLois Curfman McInnes { 2327eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 23397f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 23497f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 23587828ca2SBarry Smith PetscScalar *ap,*aa = a->a,zero = 0.0; 2367eb43aa7SLois Curfman McInnes 2373a40ed3dSBarry Smith PetscFunctionBegin; 2387eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 2397eb43aa7SLois Curfman McInnes row = im[k]; 24077431f27SBarry Smith if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); 24177431f27SBarry Smith if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->m-1); 242bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 2437eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2447eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 24577431f27SBarry Smith if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); 24677431f27SBarry Smith if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->n-1); 247bfeeae90SHong Zhang col = in[l] ; 2487eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2497eb43aa7SLois Curfman McInnes while (high-low > 5) { 2507eb43aa7SLois Curfman McInnes t = (low+high)/2; 2517eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2527eb43aa7SLois Curfman McInnes else low = t; 2537eb43aa7SLois Curfman McInnes } 2547eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 2557eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2567eb43aa7SLois Curfman McInnes if (rp[i] == col) { 257b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2587eb43aa7SLois Curfman McInnes goto finished; 2597eb43aa7SLois Curfman McInnes } 2607eb43aa7SLois Curfman McInnes } 261b49de8d1SLois Curfman McInnes *v++ = zero; 2627eb43aa7SLois Curfman McInnes finished:; 2637eb43aa7SLois Curfman McInnes } 2647eb43aa7SLois Curfman McInnes } 2653a40ed3dSBarry Smith PetscFunctionReturn(0); 2667eb43aa7SLois Curfman McInnes } 2677eb43aa7SLois Curfman McInnes 26817ab2063SBarry Smith 2694a2ae208SSatish Balay #undef __FUNCT__ 2704a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary" 271dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 27217ab2063SBarry Smith { 273416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2746849ba73SBarry Smith PetscErrorCode ierr; 2756f69ff64SBarry Smith PetscInt i,*col_lens; 2766f69ff64SBarry Smith int fd; 27717ab2063SBarry Smith 2783a40ed3dSBarry Smith PetscFunctionBegin; 279b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 28097f1f81fSBarry Smith ierr = PetscMalloc((4+A->m)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr); 281552e946dSBarry Smith col_lens[0] = MAT_FILE_COOKIE; 282273d9f13SBarry Smith col_lens[1] = A->m; 283273d9f13SBarry Smith col_lens[2] = A->n; 284416022c9SBarry Smith col_lens[3] = a->nz; 285416022c9SBarry Smith 286416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 287273d9f13SBarry Smith for (i=0; i<A->m; i++) { 288416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 28917ab2063SBarry Smith } 2906f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 291606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 292416022c9SBarry Smith 293416022c9SBarry Smith /* store column indices (zero start index) */ 2946f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 295416022c9SBarry Smith 296416022c9SBarry Smith /* store nonzero values */ 2976f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 2983a40ed3dSBarry Smith PetscFunctionReturn(0); 29917ab2063SBarry Smith } 300416022c9SBarry Smith 301dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 302cd155464SBarry Smith 3034a2ae208SSatish Balay #undef __FUNCT__ 3044a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII" 305dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 306416022c9SBarry Smith { 307416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 308dfbe8321SBarry Smith PetscErrorCode ierr; 30997f1f81fSBarry Smith PetscInt i,j,m = A->m,shift=0; 310e060cb09SBarry Smith const char *name; 311f3ef73ceSBarry Smith PetscViewerFormat format; 31217ab2063SBarry Smith 3133a40ed3dSBarry Smith PetscFunctionBegin; 314435da068SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 315b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 31671c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 31797f1f81fSBarry Smith PetscInt nofinalvalue = 0; 318273d9f13SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) { 319d00d2cf4SBarry Smith nofinalvalue = 1; 320d00d2cf4SBarry Smith } 321b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 32277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->n);CHKERRQ(ierr); 32377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 32477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 325b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 32617ab2063SBarry Smith 32717ab2063SBarry Smith for (i=0; i<m; i++) { 328416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 329aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 33077431f27SBarry 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); 33117ab2063SBarry Smith #else 33277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr); 33317ab2063SBarry Smith #endif 33417ab2063SBarry Smith } 33517ab2063SBarry Smith } 336d00d2cf4SBarry Smith if (nofinalvalue) { 33777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->n,0.0);CHKERRQ(ierr); 338d00d2cf4SBarry Smith } 339fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 340b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 34168369a75SKris Buschelman } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) { 342cd155464SBarry Smith PetscFunctionReturn(0); 343fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 344b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 34544cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 34677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 34744cd7ae7SLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 348aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 34936db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 35077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 35136db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 35277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 35336db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 35477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 3556831982aSBarry Smith } 35644cd7ae7SLois Curfman McInnes #else 35777431f27SBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);} 35844cd7ae7SLois Curfman McInnes #endif 35944cd7ae7SLois Curfman McInnes } 360b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 36144cd7ae7SLois Curfman McInnes } 362b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 363fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 36497f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 365b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 36697f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr); 367496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 368496be53dSLois Curfman McInnes sptr[i] = nzd+1; 369496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 370496be53dSLois Curfman McInnes if (a->j[j] >= i) { 371aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 37236db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 373496be53dSLois Curfman McInnes #else 374496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 375496be53dSLois Curfman McInnes #endif 376496be53dSLois Curfman McInnes } 377496be53dSLois Curfman McInnes } 378496be53dSLois Curfman McInnes } 3792e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 38077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 3812e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 38277431f27SBarry 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);} 38377431f27SBarry 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);} 38477431f27SBarry 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);} 38577431f27SBarry Smith else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);} 38677431f27SBarry Smith else if (i<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);} 38777431f27SBarry Smith else {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);} 388496be53dSLois Curfman McInnes } 389b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 390606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 391496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 392496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 39377431f27SBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);} 394496be53dSLois Curfman McInnes } 395b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 396496be53dSLois Curfman McInnes } 397b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 398496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 399496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 400496be53dSLois Curfman McInnes if (a->j[j] >= i) { 401aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 40236db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 403b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4046831982aSBarry Smith } 405496be53dSLois Curfman McInnes #else 406b0a32e0cSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);} 407496be53dSLois Curfman McInnes #endif 408496be53dSLois Curfman McInnes } 409496be53dSLois Curfman McInnes } 410b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 411496be53dSLois Curfman McInnes } 412b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 413fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 41497f1f81fSBarry Smith PetscInt cnt = 0,jcnt; 41587828ca2SBarry Smith PetscScalar value; 41602594712SBarry Smith 417b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 41802594712SBarry Smith for (i=0; i<m; i++) { 41902594712SBarry Smith jcnt = 0; 420273d9f13SBarry Smith for (j=0; j<A->n; j++) { 421e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 42202594712SBarry Smith value = a->a[cnt++]; 423e24b481bSBarry Smith jcnt++; 42402594712SBarry Smith } else { 42502594712SBarry Smith value = 0.0; 42602594712SBarry Smith } 427aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 428b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr); 42902594712SBarry Smith #else 430b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr); 43102594712SBarry Smith #endif 43202594712SBarry Smith } 433b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 43402594712SBarry Smith } 435b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 4363a40ed3dSBarry Smith } else { 437b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 43817ab2063SBarry Smith for (i=0; i<m; i++) { 43977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 440416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 441aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 44236db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 44377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 44436db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 44577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4463a40ed3dSBarry Smith } else { 44777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 44817ab2063SBarry Smith } 44917ab2063SBarry Smith #else 45077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 45117ab2063SBarry Smith #endif 45217ab2063SBarry Smith } 453b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 45417ab2063SBarry Smith } 455b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 45617ab2063SBarry Smith } 457b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 4583a40ed3dSBarry Smith PetscFunctionReturn(0); 459416022c9SBarry Smith } 460416022c9SBarry Smith 4614a2ae208SSatish Balay #undef __FUNCT__ 4624a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom" 463dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 464416022c9SBarry Smith { 465480ef9eaSBarry Smith Mat A = (Mat) Aa; 466416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 467dfbe8321SBarry Smith PetscErrorCode ierr; 46897f1f81fSBarry Smith PetscInt i,j,m = A->m,color; 46936db0b34SBarry Smith PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0; 470b0a32e0cSBarry Smith PetscViewer viewer; 471f3ef73ceSBarry Smith PetscViewerFormat format; 472cddf8d76SBarry Smith 4733a40ed3dSBarry Smith PetscFunctionBegin; 474480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 475b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 47619bcc07fSBarry Smith 477b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 478416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 4790513a670SBarry Smith 480fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 4810513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 482b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 483416022c9SBarry Smith for (i=0; i<m; i++) { 484cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 485bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 486bfeeae90SHong Zhang x_l = a->j[j] ; x_r = x_l + 1.0; 487aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 48836db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 489cddf8d76SBarry Smith #else 490cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 491cddf8d76SBarry Smith #endif 492b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 493cddf8d76SBarry Smith } 494cddf8d76SBarry Smith } 495b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 496cddf8d76SBarry Smith for (i=0; i<m; i++) { 497cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 498bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 499bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 500cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 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_RED; 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; 509aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 51036db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 511cddf8d76SBarry Smith #else 512cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 513cddf8d76SBarry Smith #endif 514b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 515416022c9SBarry Smith } 516416022c9SBarry Smith } 5170513a670SBarry Smith } else { 5180513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 5190513a670SBarry Smith /* first determine max of all nonzero values */ 52097f1f81fSBarry Smith PetscInt nz = a->nz,count; 521b0a32e0cSBarry Smith PetscDraw popup; 52236db0b34SBarry Smith PetscReal scale; 5230513a670SBarry Smith 5240513a670SBarry Smith for (i=0; i<nz; i++) { 5250513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 5260513a670SBarry Smith } 527b0a32e0cSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv; 528b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 529b0a32e0cSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);} 5300513a670SBarry Smith count = 0; 5310513a670SBarry Smith for (i=0; i<m; i++) { 5320513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 533bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 534bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 53597f1f81fSBarry Smith color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count])); 536b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 5370513a670SBarry Smith count++; 5380513a670SBarry Smith } 5390513a670SBarry Smith } 5400513a670SBarry Smith } 541480ef9eaSBarry Smith PetscFunctionReturn(0); 542480ef9eaSBarry Smith } 543cddf8d76SBarry Smith 5444a2ae208SSatish Balay #undef __FUNCT__ 5454a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw" 546dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 547480ef9eaSBarry Smith { 548dfbe8321SBarry Smith PetscErrorCode ierr; 549b0a32e0cSBarry Smith PetscDraw draw; 55036db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 551480ef9eaSBarry Smith PetscTruth isnull; 552480ef9eaSBarry Smith 553480ef9eaSBarry Smith PetscFunctionBegin; 554b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 555b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 556480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 557480ef9eaSBarry Smith 558480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 559273d9f13SBarry Smith xr = A->n; yr = A->m; h = yr/10.0; w = xr/10.0; 560480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 561b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 562b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 563480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr); 5643a40ed3dSBarry Smith PetscFunctionReturn(0); 565416022c9SBarry Smith } 566416022c9SBarry Smith 5674a2ae208SSatish Balay #undef __FUNCT__ 5684a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ" 569dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 570416022c9SBarry Smith { 571dfbe8321SBarry Smith PetscErrorCode ierr; 57232077d6dSBarry Smith PetscTruth issocket,iascii,isbinary,isdraw; 573416022c9SBarry Smith 5743a40ed3dSBarry Smith PetscFunctionBegin; 575b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr); 57632077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 577fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr); 578fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr); 579c45a1595SBarry Smith if (iascii) { 5803a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 5814cf18111SSatish Balay #if defined(PETSC_USE_SOCKET_VIEWER) 582c45a1595SBarry Smith } else if (issocket) { 5834cf18111SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 584c45a1595SBarry Smith ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr); 585c45a1595SBarry Smith #endif 5860f5bd95cSBarry Smith } else if (isbinary) { 5873a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 5880f5bd95cSBarry Smith } else if (isdraw) { 5893a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 5905cd90555SBarry Smith } else { 591958c9bccSBarry Smith SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name); 59217ab2063SBarry Smith } 5934846f1f5SKris Buschelman ierr = MatView_Inode(A,viewer);CHKERRQ(ierr); 5943a40ed3dSBarry Smith PetscFunctionReturn(0); 59517ab2063SBarry Smith } 59619bcc07fSBarry Smith 5974a2ae208SSatish Balay #undef __FUNCT__ 5984a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ" 599dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 60017ab2063SBarry Smith { 601416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 6026849ba73SBarry Smith PetscErrorCode ierr; 60397f1f81fSBarry Smith PetscInt fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax; 60497f1f81fSBarry Smith PetscInt m = A->m,*ip,N,*ailen = a->ilen,rmax = 0; 60587828ca2SBarry Smith PetscScalar *aa = a->a,*ap; 6063447b6efSHong Zhang PetscReal ratio=0.6; 60717ab2063SBarry Smith 6083a40ed3dSBarry Smith PetscFunctionBegin; 6093a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 61017ab2063SBarry Smith 61143ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 61217ab2063SBarry Smith for (i=1; i<m; i++) { 613416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 61417ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 61594a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 61617ab2063SBarry Smith if (fshift) { 617bfeeae90SHong Zhang ip = aj + ai[i] ; 618bfeeae90SHong Zhang ap = aa + ai[i] ; 61917ab2063SBarry Smith N = ailen[i]; 62017ab2063SBarry Smith for (j=0; j<N; j++) { 62117ab2063SBarry Smith ip[j-fshift] = ip[j]; 62217ab2063SBarry Smith ap[j-fshift] = ap[j]; 62317ab2063SBarry Smith } 62417ab2063SBarry Smith } 62517ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 62617ab2063SBarry Smith } 62717ab2063SBarry Smith if (m) { 62817ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 62917ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 63017ab2063SBarry Smith } 63117ab2063SBarry Smith /* reset ilen and imax for each row */ 63217ab2063SBarry Smith for (i=0; i<m; i++) { 63317ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 63417ab2063SBarry Smith } 635bfeeae90SHong Zhang a->nz = ai[m]; 63617ab2063SBarry Smith 63717ab2063SBarry Smith /* diagonals may have moved, so kill the diagonal pointers */ 638416022c9SBarry Smith if (fshift && a->diag) { 639606d414cSSatish Balay ierr = PetscFree(a->diag);CHKERRQ(ierr); 64052e6d16bSBarry Smith ierr = PetscLogObjectMemory(A,-(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 641416022c9SBarry Smith a->diag = 0; 64217ab2063SBarry Smith } 64363ba0a88SBarry Smith ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->n,fshift,a->nz));CHKERRQ(ierr); 64463ba0a88SBarry Smith ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %D\n",a->reallocs));CHKERRQ(ierr); 64563ba0a88SBarry Smith ierr = PetscLogInfo((A,"MatAssemblyEnd_SeqAIJ:Maximum nonzeros in any row is %D\n",rmax));CHKERRQ(ierr); 646dd5f02e7SSatish Balay a->reallocs = 0; 6474e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 64836db0b34SBarry Smith a->rmax = rmax; 6494e220ebcSLois Curfman McInnes 650cb5d8e9eSHong Zhang /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */ 651317fbc4cSHong Zhang ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 65288e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 65371c2f376SKris Buschelman 6544846f1f5SKris Buschelman ierr = MatAssemblyEnd_Inode(A,mode);CHKERRQ(ierr); 6553a40ed3dSBarry Smith PetscFunctionReturn(0); 65617ab2063SBarry Smith } 65717ab2063SBarry Smith 6584a2ae208SSatish Balay #undef __FUNCT__ 65999cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ" 66099cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 66199cafbc1SBarry Smith { 66299cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 66399cafbc1SBarry Smith PetscInt i,nz = a->nz; 66499cafbc1SBarry Smith PetscScalar *aa = a->a; 66599cafbc1SBarry Smith 66699cafbc1SBarry Smith PetscFunctionBegin; 66799cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 66899cafbc1SBarry Smith PetscFunctionReturn(0); 66999cafbc1SBarry Smith } 67099cafbc1SBarry Smith 67199cafbc1SBarry Smith #undef __FUNCT__ 67299cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ" 67399cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 67499cafbc1SBarry Smith { 67599cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 67699cafbc1SBarry Smith PetscInt i,nz = a->nz; 67799cafbc1SBarry Smith PetscScalar *aa = a->a; 67899cafbc1SBarry Smith 67999cafbc1SBarry Smith PetscFunctionBegin; 68099cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 68199cafbc1SBarry Smith PetscFunctionReturn(0); 68299cafbc1SBarry Smith } 68399cafbc1SBarry Smith 68499cafbc1SBarry Smith #undef __FUNCT__ 6854a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ" 686dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 68717ab2063SBarry Smith { 688416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 689dfbe8321SBarry Smith PetscErrorCode ierr; 6903a40ed3dSBarry Smith 6913a40ed3dSBarry Smith PetscFunctionBegin; 692bfeeae90SHong Zhang ierr = PetscMemzero(a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr); 6933a40ed3dSBarry Smith PetscFunctionReturn(0); 69417ab2063SBarry Smith } 695416022c9SBarry Smith 6964a2ae208SSatish Balay #undef __FUNCT__ 6974a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ" 698dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 69917ab2063SBarry Smith { 700416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 701dfbe8321SBarry Smith PetscErrorCode ierr; 702d5d45c9bSBarry Smith 7033a40ed3dSBarry Smith PetscFunctionBegin; 704aa482453SBarry Smith #if defined(PETSC_USE_LOG) 70577431f27SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->m,A->n,a->nz); 70617ab2063SBarry Smith #endif 7073cbb7e54SHong Zhang if (a->freedata){ 708085a36d4SBarry Smith ierr = MatSeqXAIJFreeAIJ(a->singlemalloc,&a->a,&a->j,&a->i);CHKERRQ(ierr); 7093cbb7e54SHong Zhang } 710c38d4ed2SBarry Smith if (a->row) { 711c38d4ed2SBarry Smith ierr = ISDestroy(a->row);CHKERRQ(ierr); 712c38d4ed2SBarry Smith } 713c38d4ed2SBarry Smith if (a->col) { 714c38d4ed2SBarry Smith ierr = ISDestroy(a->col);CHKERRQ(ierr); 715c38d4ed2SBarry Smith } 716606d414cSSatish Balay if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);} 717ab93d7beSBarry Smith if (a->ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);} 718273d9f13SBarry Smith if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);} 719606d414cSSatish Balay if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);} 72082bf6240SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} 721606d414cSSatish Balay if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);} 722cc8ba8e1SBarry Smith if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);} 723a30b2313SHong Zhang if (a->xtoy) {ierr = PetscFree(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 730901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr); 731901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 732901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 733901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr); 734901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr); 735901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 736901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 737a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 738901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr); 7393a40ed3dSBarry Smith PetscFunctionReturn(0); 74017ab2063SBarry Smith } 74117ab2063SBarry Smith 7424a2ae208SSatish Balay #undef __FUNCT__ 7434a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ" 744dfbe8321SBarry Smith PetscErrorCode MatCompress_SeqAIJ(Mat A) 74517ab2063SBarry Smith { 7463a40ed3dSBarry Smith PetscFunctionBegin; 7473a40ed3dSBarry Smith PetscFunctionReturn(0); 74817ab2063SBarry Smith } 74917ab2063SBarry Smith 7504a2ae208SSatish Balay #undef __FUNCT__ 7514a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ" 752dfbe8321SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op) 75317ab2063SBarry Smith { 754416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 7554846f1f5SKris Buschelman PetscErrorCode ierr; 7563a40ed3dSBarry Smith 7573a40ed3dSBarry Smith PetscFunctionBegin; 758a65d3064SKris Buschelman switch (op) { 759a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 760a65d3064SKris Buschelman a->roworiented = PETSC_TRUE; 761a65d3064SKris Buschelman break; 762a65d3064SKris Buschelman case MAT_KEEP_ZEROED_ROWS: 763a65d3064SKris Buschelman a->keepzeroedrows = PETSC_TRUE; 764a65d3064SKris Buschelman break; 765a65d3064SKris Buschelman case MAT_COLUMN_ORIENTED: 766a65d3064SKris Buschelman a->roworiented = PETSC_FALSE; 767a65d3064SKris Buschelman break; 768a65d3064SKris Buschelman case MAT_COLUMNS_SORTED: 769a65d3064SKris Buschelman a->sorted = PETSC_TRUE; 770a65d3064SKris Buschelman break; 771a65d3064SKris Buschelman case MAT_COLUMNS_UNSORTED: 772a65d3064SKris Buschelman a->sorted = PETSC_FALSE; 773a65d3064SKris Buschelman break; 774a65d3064SKris Buschelman case MAT_NO_NEW_NONZERO_LOCATIONS: 775a65d3064SKris Buschelman a->nonew = 1; 776a65d3064SKris Buschelman break; 777a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 778a65d3064SKris Buschelman a->nonew = -1; 779a65d3064SKris Buschelman break; 780a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 781a65d3064SKris Buschelman a->nonew = -2; 782a65d3064SKris Buschelman break; 783a65d3064SKris Buschelman case MAT_YES_NEW_NONZERO_LOCATIONS: 784a65d3064SKris Buschelman a->nonew = 0; 785a65d3064SKris Buschelman break; 786a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 787a65d3064SKris Buschelman a->ignorezeroentries = PETSC_TRUE; 788a65d3064SKris Buschelman break; 789d487561eSHong Zhang case MAT_USE_COMPRESSEDROW: 790d487561eSHong Zhang a->compressedrow.use = PETSC_TRUE; 791d487561eSHong Zhang break; 792d487561eSHong Zhang case MAT_DO_NOT_USE_COMPRESSEDROW: 793d487561eSHong Zhang a->compressedrow.use = PETSC_FALSE; 794d487561eSHong Zhang break; 795a65d3064SKris Buschelman case MAT_ROWS_SORTED: 796a65d3064SKris Buschelman case MAT_ROWS_UNSORTED: 797a65d3064SKris Buschelman case MAT_YES_NEW_DIAGONALS: 798a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 799a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 80063ba0a88SBarry Smith ierr = PetscLogInfo((A,"MatSetOption_SeqAIJ:Option ignored\n"));CHKERRQ(ierr); 801a65d3064SKris Buschelman break; 802a65d3064SKris Buschelman case MAT_NO_NEW_DIAGONALS: 80329bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS"); 804a65d3064SKris Buschelman default: 80571c2f376SKris Buschelman break; 806a65d3064SKris Buschelman } 8074846f1f5SKris Buschelman ierr = MatSetOption_Inode(A,op);CHKERRQ(ierr); 8083a40ed3dSBarry Smith PetscFunctionReturn(0); 80917ab2063SBarry Smith } 81017ab2063SBarry Smith 8114a2ae208SSatish Balay #undef __FUNCT__ 8124a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ" 813dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 81417ab2063SBarry Smith { 815416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8166849ba73SBarry Smith PetscErrorCode ierr; 81797f1f81fSBarry Smith PetscInt i,j,n; 81887828ca2SBarry Smith PetscScalar *x,zero = 0.0; 81917ab2063SBarry Smith 8203a40ed3dSBarry Smith PetscFunctionBegin; 8212dcb1b2aSMatthew Knepley ierr = VecSet(v,zero);CHKERRQ(ierr); 8221ebc52fbSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 82336db0b34SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 824273d9f13SBarry Smith if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 825273d9f13SBarry Smith for (i=0; i<A->m; i++) { 826bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 827bfeeae90SHong Zhang if (a->j[j] == i) { 828416022c9SBarry Smith x[i] = a->a[j]; 82917ab2063SBarry Smith break; 83017ab2063SBarry Smith } 83117ab2063SBarry Smith } 83217ab2063SBarry Smith } 8331ebc52fbSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 8343a40ed3dSBarry Smith PetscFunctionReturn(0); 83517ab2063SBarry Smith } 83617ab2063SBarry Smith 8374a2ae208SSatish Balay #undef __FUNCT__ 8384a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ" 839dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 84017ab2063SBarry Smith { 841416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8425c897100SBarry Smith PetscScalar *x,*y; 843dfbe8321SBarry Smith PetscErrorCode ierr; 84497f1f81fSBarry Smith PetscInt m = A->m; 8455c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 8465c897100SBarry Smith PetscScalar *v,alpha; 8477b2bb3b9SHong Zhang PetscInt n,i,*idx,*ii,*ridx=PETSC_NULL; 8483447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 8494eb6d288SHong Zhang PetscTruth usecprow = cprow.use; 8505c897100SBarry Smith #endif 85117ab2063SBarry Smith 8523a40ed3dSBarry Smith PetscFunctionBegin; 8532e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 8541ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 8551ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 8565c897100SBarry Smith 8575c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 858bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 8595c897100SBarry Smith #else 8603447b6efSHong Zhang if (usecprow){ 8613447b6efSHong Zhang m = cprow.nrows; 8623447b6efSHong Zhang ii = cprow.i; 8637b2bb3b9SHong Zhang ridx = cprow.rindex; 8643447b6efSHong Zhang } else { 8653447b6efSHong Zhang ii = a->i; 8663447b6efSHong Zhang } 86717ab2063SBarry Smith for (i=0; i<m; i++) { 8683447b6efSHong Zhang idx = a->j + ii[i] ; 8693447b6efSHong Zhang v = a->a + ii[i] ; 8703447b6efSHong Zhang n = ii[i+1] - ii[i]; 8713447b6efSHong Zhang if (usecprow){ 8727b2bb3b9SHong Zhang alpha = x[ridx[i]]; 8733447b6efSHong Zhang } else { 87417ab2063SBarry Smith alpha = x[i]; 8753447b6efSHong Zhang } 87617ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 87717ab2063SBarry Smith } 8785c897100SBarry Smith #endif 879efee365bSSatish Balay ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr); 8801ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 8811ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 8823a40ed3dSBarry Smith PetscFunctionReturn(0); 88317ab2063SBarry Smith } 88417ab2063SBarry Smith 8854a2ae208SSatish Balay #undef __FUNCT__ 8865c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ" 887dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 8885c897100SBarry Smith { 8898d5b0100SBarry Smith PetscScalar zero = 0.0; 890dfbe8321SBarry Smith PetscErrorCode ierr; 8915c897100SBarry Smith 8925c897100SBarry Smith PetscFunctionBegin; 8932dcb1b2aSMatthew Knepley ierr = VecSet(yy,zero);CHKERRQ(ierr); 8945c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 8955c897100SBarry Smith PetscFunctionReturn(0); 8965c897100SBarry Smith } 8975c897100SBarry Smith 8985c897100SBarry Smith 8995c897100SBarry Smith #undef __FUNCT__ 9004a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ" 901dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 90217ab2063SBarry Smith { 903416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 90497952fefSHong Zhang PetscScalar *x,*y,*aa; 905dfbe8321SBarry Smith PetscErrorCode ierr; 90697952fefSHong Zhang PetscInt m=A->m,*aj,*ii; 907f2e71c43SSatish Balay PetscInt n,i,j,*ridx=PETSC_NULL; 908362ced78SSatish Balay PetscScalar sum; 90997952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 910f2e71c43SSatish Balay #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 911f2e71c43SSatish Balay PetscInt jrow; 912e36a17ebSSatish Balay #endif 91317ab2063SBarry Smith 914b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 91597952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 916fee21e36SBarry Smith #endif 917fee21e36SBarry Smith 9183a40ed3dSBarry Smith PetscFunctionBegin; 9191ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 9201ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 92197952fefSHong Zhang aj = a->j; 92297952fefSHong Zhang aa = a->a; 923416022c9SBarry Smith ii = a->i; 9244eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 92597952fefSHong Zhang m = a->compressedrow.nrows; 92697952fefSHong Zhang ii = a->compressedrow.i; 92797952fefSHong Zhang ridx = a->compressedrow.rindex; 92897952fefSHong Zhang for (i=0; i<m; i++){ 92997952fefSHong Zhang n = ii[i+1] - ii[i]; 93097952fefSHong Zhang aj = a->j + ii[i]; 93197952fefSHong Zhang aa = a->a + ii[i]; 93297952fefSHong Zhang sum = 0.0; 93397952fefSHong Zhang for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; 93497952fefSHong Zhang y[*ridx++] = sum; 93597952fefSHong Zhang } 93697952fefSHong Zhang } else { /* do not use compressed row format */ 937b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 938b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 939b05257ddSBarry Smith #else 94017ab2063SBarry Smith for (i=0; i<m; i++) { 9419ea0dfa2SSatish Balay jrow = ii[i]; 9429ea0dfa2SSatish Balay n = ii[i+1] - jrow; 94317ab2063SBarry Smith sum = 0.0; 9449ea0dfa2SSatish Balay for (j=0; j<n; j++) { 94597952fefSHong Zhang sum += aa[jrow]*x[aj[jrow]]; jrow++; 9469ea0dfa2SSatish Balay } 94717ab2063SBarry Smith y[i] = sum; 94817ab2063SBarry Smith } 9498d195f9aSBarry Smith #endif 950b05257ddSBarry Smith } 951efee365bSSatish Balay ierr = PetscLogFlops(2*a->nz - m);CHKERRQ(ierr); 9521ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 9531ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9543a40ed3dSBarry Smith PetscFunctionReturn(0); 95517ab2063SBarry Smith } 95617ab2063SBarry Smith 9574a2ae208SSatish Balay #undef __FUNCT__ 9584a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ" 959dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 96017ab2063SBarry Smith { 961416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 96297952fefSHong Zhang PetscScalar *x,*y,*z,*aa; 963dfbe8321SBarry Smith PetscErrorCode ierr; 96497952fefSHong Zhang PetscInt m = A->m,*aj,*ii; 965aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 96697952fefSHong Zhang PetscInt n,i,jrow,j,*ridx=PETSC_NULL; 967362ced78SSatish Balay PetscScalar sum; 96897952fefSHong Zhang PetscTruth usecprow=a->compressedrow.use; 969e36a17ebSSatish Balay #endif 9709ea0dfa2SSatish Balay 9713a40ed3dSBarry Smith PetscFunctionBegin; 9721ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 9731ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 9742e8a6d31SBarry Smith if (zz != yy) { 9751ebc52fbSHong Zhang ierr = VecGetArray(zz,&z);CHKERRQ(ierr); 9762e8a6d31SBarry Smith } else { 9772e8a6d31SBarry Smith z = y; 9782e8a6d31SBarry Smith } 979bfeeae90SHong Zhang 98097952fefSHong Zhang aj = a->j; 98197952fefSHong Zhang aa = a->a; 982cddf8d76SBarry Smith ii = a->i; 983aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 98497952fefSHong Zhang fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 98502ab625aSSatish Balay #else 9864eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 9874eb6d288SHong Zhang if (zz != yy){ 9884eb6d288SHong Zhang ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr); 9894eb6d288SHong Zhang } 99097952fefSHong Zhang m = a->compressedrow.nrows; 99197952fefSHong Zhang ii = a->compressedrow.i; 99297952fefSHong Zhang ridx = a->compressedrow.rindex; 99397952fefSHong Zhang for (i=0; i<m; i++){ 99497952fefSHong Zhang n = ii[i+1] - ii[i]; 99597952fefSHong Zhang aj = a->j + ii[i]; 99697952fefSHong Zhang aa = a->a + ii[i]; 99797952fefSHong Zhang sum = y[*ridx]; 99897952fefSHong Zhang for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; 99997952fefSHong Zhang z[*ridx++] = sum; 100097952fefSHong Zhang } 100197952fefSHong Zhang } else { /* do not use compressed row format */ 100217ab2063SBarry Smith for (i=0; i<m; i++) { 10039ea0dfa2SSatish Balay jrow = ii[i]; 10049ea0dfa2SSatish Balay n = ii[i+1] - jrow; 100517ab2063SBarry Smith sum = y[i]; 10069ea0dfa2SSatish Balay for (j=0; j<n; j++) { 100797952fefSHong Zhang sum += aa[jrow]*x[aj[jrow]]; jrow++; 10089ea0dfa2SSatish Balay } 100917ab2063SBarry Smith z[i] = sum; 101017ab2063SBarry Smith } 101197952fefSHong Zhang } 101202ab625aSSatish Balay #endif 1013efee365bSSatish Balay ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr); 10141ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 10151ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 10162e8a6d31SBarry Smith if (zz != yy) { 10171ebc52fbSHong Zhang ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr); 10182e8a6d31SBarry Smith } 10193a40ed3dSBarry Smith PetscFunctionReturn(0); 102017ab2063SBarry Smith } 102117ab2063SBarry Smith 102217ab2063SBarry Smith /* 102317ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 102417ab2063SBarry Smith */ 10254a2ae208SSatish Balay #undef __FUNCT__ 10264a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ" 1027dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 102817ab2063SBarry Smith { 1029416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 10306849ba73SBarry Smith PetscErrorCode ierr; 103197f1f81fSBarry Smith PetscInt i,j,*diag,m = A->m; 103217ab2063SBarry Smith 10333a40ed3dSBarry Smith PetscFunctionBegin; 1034f1e2ffcdSBarry Smith if (a->diag) PetscFunctionReturn(0); 1035f1e2ffcdSBarry Smith 103697f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&diag);CHKERRQ(ierr); 103752e6d16bSBarry Smith ierr = PetscLogObjectMemory(A,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 1038273d9f13SBarry Smith for (i=0; i<A->m; i++) { 103935b0346bSBarry Smith diag[i] = a->i[i+1]; 1040bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1041bfeeae90SHong Zhang if (a->j[j] == i) { 1042bfeeae90SHong Zhang diag[i] = j; 104317ab2063SBarry Smith break; 104417ab2063SBarry Smith } 104517ab2063SBarry Smith } 104617ab2063SBarry Smith } 1047416022c9SBarry Smith a->diag = diag; 10483a40ed3dSBarry Smith PetscFunctionReturn(0); 104917ab2063SBarry Smith } 105017ab2063SBarry Smith 1051be5855fcSBarry Smith /* 1052be5855fcSBarry Smith Checks for missing diagonals 1053be5855fcSBarry Smith */ 10544a2ae208SSatish Balay #undef __FUNCT__ 10554a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ" 1056dfbe8321SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A) 1057be5855fcSBarry Smith { 1058be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 10596849ba73SBarry Smith PetscErrorCode ierr; 106097f1f81fSBarry Smith PetscInt *diag,*jj = a->j,i; 1061be5855fcSBarry Smith 1062be5855fcSBarry Smith PetscFunctionBegin; 1063c69eeb13SBarry Smith if (A->m > 0 && !jj) SETERRQ(PETSC_ERR_PLIB,"Matrix has no entries therefor is missing diagonal"); 1064f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1065f1e2ffcdSBarry Smith diag = a->diag; 1066273d9f13SBarry Smith for (i=0; i<A->m; i++) { 1067bfeeae90SHong Zhang if (jj[diag[i]] != i) { 106877431f27SBarry Smith SETERRQ1(PETSC_ERR_PLIB,"Matrix is missing diagonal number %D",i); 1069be5855fcSBarry Smith } 1070be5855fcSBarry Smith } 1071be5855fcSBarry Smith PetscFunctionReturn(0); 1072be5855fcSBarry Smith } 1073be5855fcSBarry Smith 10744a2ae208SSatish Balay #undef __FUNCT__ 10754a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ" 107697f1f81fSBarry Smith PetscErrorCode MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 107717ab2063SBarry Smith { 1078416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1079beeb8507SBarry Smith PetscScalar *x,d,*xs,sum,*t,scale,*idiag=0,*mdiag; 1080beeb8507SBarry Smith const PetscScalar *v = a->a, *b, *bs,*xb, *ts; 1081dfbe8321SBarry Smith PetscErrorCode ierr; 108297f1f81fSBarry Smith PetscInt n = A->n,m = A->m,i; 108397f1f81fSBarry Smith const PetscInt *idx,*diag; 108417ab2063SBarry Smith 10853a40ed3dSBarry Smith PetscFunctionBegin; 1086b965ef7fSBarry Smith its = its*lits; 108777431f27SBarry Smith if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits); 108891723122SBarry Smith 1089ed480e8bSBarry Smith if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);} 1090ed480e8bSBarry Smith diag = a->diag; 1091ed480e8bSBarry Smith if (!a->idiag) { 1092ed480e8bSBarry Smith ierr = PetscMalloc(3*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr); 1093ed480e8bSBarry Smith a->ssor = a->idiag + m; 1094ed480e8bSBarry Smith mdiag = a->ssor + m; 1095ed480e8bSBarry Smith 1096ed480e8bSBarry Smith v = a->a; 1097ed480e8bSBarry Smith 1098ed480e8bSBarry Smith /* this is wrong when fshift omega changes each iteration */ 1099958c9bccSBarry Smith if (omega == 1.0 && !fshift) { 1100ed480e8bSBarry Smith for (i=0; i<m; i++) { 1101ed480e8bSBarry Smith mdiag[i] = v[diag[i]]; 1102ed480e8bSBarry Smith a->idiag[i] = 1.0/v[diag[i]]; 1103ed480e8bSBarry Smith } 1104efee365bSSatish Balay ierr = PetscLogFlops(m);CHKERRQ(ierr); 1105ed480e8bSBarry Smith } else { 1106ed480e8bSBarry Smith for (i=0; i<m; i++) { 1107ed480e8bSBarry Smith mdiag[i] = v[diag[i]]; 1108beeb8507SBarry Smith a->idiag[i] = omega/(fshift + v[diag[i]]); 1109ed480e8bSBarry Smith } 1110efee365bSSatish Balay ierr = PetscLogFlops(2*m);CHKERRQ(ierr); 1111beeb8507SBarry Smith } 1112ed480e8bSBarry Smith } 1113ed480e8bSBarry Smith t = a->ssor; 1114ed480e8bSBarry Smith idiag = a->idiag; 1115ed480e8bSBarry Smith mdiag = a->idiag + 2*m; 1116ed480e8bSBarry Smith 11171ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1118fb2e594dSBarry Smith if (xx != bb) { 11191ebc52fbSHong Zhang ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr); 1120fb2e594dSBarry Smith } else { 1121fb2e594dSBarry Smith b = x; 1122fb2e594dSBarry Smith } 1123fb2e594dSBarry Smith 1124ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 1125ed480e8bSBarry Smith xs = x; 112617ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 112717ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1128ed480e8bSBarry Smith bs = b; 112917ab2063SBarry Smith for (i=0; i<m; i++) { 1130ed480e8bSBarry Smith d = fshift + a->a[diag[i]]; 1131416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1132ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1133ed480e8bSBarry Smith v = a->a + diag[i] + 1; 113417ab2063SBarry Smith sum = b[i]*d/omega; 113517ab2063SBarry Smith SPARSEDENSEDOT(sum,bs,v,idx,n); 113617ab2063SBarry Smith x[i] = sum; 113717ab2063SBarry Smith } 11381ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 11391ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 1140efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 11413a40ed3dSBarry Smith PetscFunctionReturn(0); 114217ab2063SBarry Smith } 1143c783ea89SBarry Smith 1144ed480e8bSBarry Smith 1145fc3d8934SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 1146fc3d8934SBarry Smith U is upper triangular, E is diagonal; This routine applies 1147fc3d8934SBarry Smith 1148fc3d8934SBarry Smith (L + E)^{-1} A (U + E)^{-1} 1149fc3d8934SBarry Smith 1150fc3d8934SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 1151fc3d8934SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 115248af12d7SBarry Smith is the relaxation factor. 1153fc3d8934SBarry Smith */ 1154fc3d8934SBarry Smith 115548af12d7SBarry Smith if (flag == SOR_APPLY_LOWER) { 115629bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 11573a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 115817ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 115917ab2063SBarry Smith U is upper triangular, E is diagonal; This routine applies 116017ab2063SBarry Smith 116117ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 116217ab2063SBarry Smith 116317ab2063SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 116417ab2063SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 116517ab2063SBarry Smith is the relaxation factor. 116617ab2063SBarry Smith */ 116717ab2063SBarry Smith scale = (2.0/omega) - 1.0; 116817ab2063SBarry Smith 116917ab2063SBarry Smith /* x = (E + U)^{-1} b */ 117017ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1171416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1172ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1173ed480e8bSBarry Smith v = a->a + diag[i] + 1; 117417ab2063SBarry Smith sum = b[i]; 117517ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1176ed480e8bSBarry Smith x[i] = sum*idiag[i]; 117717ab2063SBarry Smith } 117817ab2063SBarry Smith 117917ab2063SBarry Smith /* t = b - (2*E - D)x */ 1180416022c9SBarry Smith v = a->a; 1181ed480e8bSBarry Smith for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; } 118217ab2063SBarry Smith 118317ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1184ed480e8bSBarry Smith ts = t; 1185416022c9SBarry Smith diag = a->diag; 118617ab2063SBarry Smith for (i=0; i<m; i++) { 1187416022c9SBarry Smith n = diag[i] - a->i[i]; 1188ed480e8bSBarry Smith idx = a->j + a->i[i]; 1189ed480e8bSBarry Smith v = a->a + a->i[i]; 119017ab2063SBarry Smith sum = t[i]; 119117ab2063SBarry Smith SPARSEDENSEMDOT(sum,ts,v,idx,n); 1192ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1193733d66baSBarry Smith /* x = x + t */ 1194733d66baSBarry Smith x[i] += t[i]; 119517ab2063SBarry Smith } 119617ab2063SBarry Smith 1197efee365bSSatish Balay ierr = PetscLogFlops(6*m-1 + 2*a->nz);CHKERRQ(ierr); 11981ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 11991ebc52fbSHong Zhang if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);} 12003a40ed3dSBarry Smith PetscFunctionReturn(0); 120117ab2063SBarry Smith } 120217ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 120317ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 120477d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 120597f1f81fSBarry Smith fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)b); 120677d8c4bbSBarry Smith #else 120717ab2063SBarry Smith for (i=0; i<m; i++) { 1208416022c9SBarry Smith n = diag[i] - a->i[i]; 1209ed480e8bSBarry Smith idx = a->j + a->i[i]; 1210ed480e8bSBarry Smith v = a->a + a->i[i]; 121117ab2063SBarry Smith sum = b[i]; 121217ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1213ed480e8bSBarry Smith x[i] = sum*idiag[i]; 121417ab2063SBarry Smith } 121577d8c4bbSBarry Smith #endif 121617ab2063SBarry Smith xb = x; 1217efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 12183a40ed3dSBarry Smith } else xb = b; 121917ab2063SBarry Smith if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) && 122017ab2063SBarry Smith (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) { 122117ab2063SBarry Smith for (i=0; i<m; i++) { 1222ed480e8bSBarry Smith x[i] *= mdiag[i]; 122317ab2063SBarry Smith } 1224efee365bSSatish Balay ierr = PetscLogFlops(m);CHKERRQ(ierr); 122517ab2063SBarry Smith } 122617ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 122777d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 122897f1f81fSBarry Smith fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,idiag,a->a,(void*)xb); 122977d8c4bbSBarry Smith #else 123017ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1231416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1232ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1233ed480e8bSBarry Smith v = a->a + diag[i] + 1; 123417ab2063SBarry Smith sum = xb[i]; 123517ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1236ed480e8bSBarry Smith x[i] = sum*idiag[i]; 123717ab2063SBarry Smith } 123877d8c4bbSBarry Smith #endif 1239efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 124017ab2063SBarry Smith } 124117ab2063SBarry Smith its--; 124217ab2063SBarry Smith } 124317ab2063SBarry Smith while (its--) { 124417ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 124577d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 124697f1f81fSBarry Smith fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b); 124777d8c4bbSBarry Smith #else 124817ab2063SBarry Smith for (i=0; i<m; i++) { 1249ed480e8bSBarry Smith d = fshift + a->a[diag[i]]; 1250416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1251ed480e8bSBarry Smith idx = a->j + a->i[i]; 1252ed480e8bSBarry Smith v = a->a + a->i[i]; 125317ab2063SBarry Smith sum = b[i]; 125417ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1255ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 125617ab2063SBarry Smith } 125777d8c4bbSBarry Smith #endif 1258efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 125917ab2063SBarry Smith } 126017ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 126177d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 126297f1f81fSBarry Smith fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,(PetscInt*)diag,a->a,(void*)b); 126377d8c4bbSBarry Smith #else 126417ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1265ed480e8bSBarry Smith d = fshift + a->a[diag[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);} 12793a40ed3dSBarry Smith PetscFunctionReturn(0); 128017ab2063SBarry Smith } 128117ab2063SBarry Smith 12824a2ae208SSatish Balay #undef __FUNCT__ 12834a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ" 1284dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 128517ab2063SBarry Smith { 1286416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12874e220ebcSLois Curfman McInnes 12883a40ed3dSBarry Smith PetscFunctionBegin; 1289273d9f13SBarry Smith info->rows_global = (double)A->m; 1290273d9f13SBarry Smith info->columns_global = (double)A->n; 1291273d9f13SBarry Smith info->rows_local = (double)A->m; 1292273d9f13SBarry Smith info->columns_local = (double)A->n; 12934e220ebcSLois Curfman McInnes info->block_size = 1.0; 12944e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 12954e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 12964e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 12974e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 12984e220ebcSLois Curfman McInnes info->mallocs = (double)a->reallocs; 12994e220ebcSLois Curfman McInnes info->memory = A->mem; 13004e220ebcSLois Curfman McInnes if (A->factor) { 13014e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 13024e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 13034e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 13044e220ebcSLois Curfman McInnes } else { 13054e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 13064e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 13074e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 13084e220ebcSLois Curfman McInnes } 13093a40ed3dSBarry Smith PetscFunctionReturn(0); 131017ab2063SBarry Smith } 131117ab2063SBarry Smith 13124a2ae208SSatish Balay #undef __FUNCT__ 13134a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ" 1314f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag) 131517ab2063SBarry Smith { 1316416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1317f4df32b1SMatthew Knepley PetscInt i,m = A->m - 1; 13186849ba73SBarry Smith PetscErrorCode ierr; 131917ab2063SBarry Smith 13203a40ed3dSBarry Smith PetscFunctionBegin; 1321f1e2ffcdSBarry Smith if (a->keepzeroedrows) { 1322f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 132377431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1324bfeeae90SHong Zhang ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1325f1e2ffcdSBarry Smith } 1326f4df32b1SMatthew Knepley if (diag != 0.0) { 1327f1e2ffcdSBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1328f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1329f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1330f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 1331f1e2ffcdSBarry Smith } 1332f1e2ffcdSBarry Smith } 133388e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 1334f1e2ffcdSBarry Smith } else { 1335f4df32b1SMatthew Knepley if (diag != 0.0) { 133617ab2063SBarry Smith for (i=0; i<N; i++) { 133777431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 13387ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1339416022c9SBarry Smith a->ilen[rows[i]] = 1; 1340f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 1341bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 13427ae801bdSBarry Smith } else { /* in case row was completely empty */ 1343f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 134417ab2063SBarry Smith } 134517ab2063SBarry Smith } 13463a40ed3dSBarry Smith } else { 134717ab2063SBarry Smith for (i=0; i<N; i++) { 134877431f27SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1349416022c9SBarry Smith a->ilen[rows[i]] = 0; 135017ab2063SBarry Smith } 135117ab2063SBarry Smith } 135288e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 1353f1e2ffcdSBarry Smith } 135443a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13553a40ed3dSBarry Smith PetscFunctionReturn(0); 135617ab2063SBarry Smith } 135717ab2063SBarry Smith 13584a2ae208SSatish Balay #undef __FUNCT__ 13594a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ" 136097f1f81fSBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 136117ab2063SBarry Smith { 1362416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 136397f1f81fSBarry Smith PetscInt *itmp; 136417ab2063SBarry Smith 13653a40ed3dSBarry Smith PetscFunctionBegin; 136677431f27SBarry Smith if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 136717ab2063SBarry Smith 1368416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1369bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 137017ab2063SBarry Smith if (idx) { 1371bfeeae90SHong Zhang itmp = a->j + a->i[row]; 1372bfeeae90SHong Zhang if (*nz) { 13734e093b46SBarry Smith *idx = itmp; 137417ab2063SBarry Smith } 137517ab2063SBarry Smith else *idx = 0; 137617ab2063SBarry Smith } 13773a40ed3dSBarry Smith PetscFunctionReturn(0); 137817ab2063SBarry Smith } 137917ab2063SBarry Smith 1380bfeeae90SHong Zhang /* remove this function? */ 13814a2ae208SSatish Balay #undef __FUNCT__ 13824a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ" 138397f1f81fSBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 138417ab2063SBarry Smith { 13853a40ed3dSBarry Smith PetscFunctionBegin; 13863a40ed3dSBarry Smith PetscFunctionReturn(0); 138717ab2063SBarry Smith } 138817ab2063SBarry Smith 13894a2ae208SSatish Balay #undef __FUNCT__ 13904a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ" 1391dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 139217ab2063SBarry Smith { 1393416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 139487828ca2SBarry Smith PetscScalar *v = a->a; 139536db0b34SBarry Smith PetscReal sum = 0.0; 13966849ba73SBarry Smith PetscErrorCode ierr; 139797f1f81fSBarry Smith PetscInt i,j; 139817ab2063SBarry Smith 13993a40ed3dSBarry Smith PetscFunctionBegin; 140017ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1401416022c9SBarry Smith for (i=0; i<a->nz; i++) { 1402aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 140336db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 140417ab2063SBarry Smith #else 140517ab2063SBarry Smith sum += (*v)*(*v); v++; 140617ab2063SBarry Smith #endif 140717ab2063SBarry Smith } 1408064f8208SBarry Smith *nrm = sqrt(sum); 14093a40ed3dSBarry Smith } else if (type == NORM_1) { 141036db0b34SBarry Smith PetscReal *tmp; 141197f1f81fSBarry Smith PetscInt *jj = a->j; 1412b0a32e0cSBarry Smith ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1413273d9f13SBarry Smith ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr); 1414064f8208SBarry Smith *nrm = 0.0; 1415416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1416bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 141717ab2063SBarry Smith } 1418273d9f13SBarry Smith for (j=0; j<A->n; j++) { 1419064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 142017ab2063SBarry Smith } 1421606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 14223a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1423064f8208SBarry Smith *nrm = 0.0; 1424273d9f13SBarry Smith for (j=0; j<A->m; j++) { 1425bfeeae90SHong Zhang v = a->a + a->i[j]; 142617ab2063SBarry Smith sum = 0.0; 1427416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1428cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 142917ab2063SBarry Smith } 1430064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 143117ab2063SBarry Smith } 14323a40ed3dSBarry Smith } else { 143329bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"No support for two norm"); 143417ab2063SBarry Smith } 14353a40ed3dSBarry Smith PetscFunctionReturn(0); 143617ab2063SBarry Smith } 143717ab2063SBarry Smith 14384a2ae208SSatish Balay #undef __FUNCT__ 14394a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ" 1440dfbe8321SBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,Mat *B) 144117ab2063SBarry Smith { 1442416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1443416022c9SBarry Smith Mat C; 14446849ba73SBarry Smith PetscErrorCode ierr; 144597f1f81fSBarry Smith PetscInt i,*aj = a->j,*ai = a->i,m = A->m,len,*col; 144687828ca2SBarry Smith PetscScalar *array = a->a; 144717ab2063SBarry Smith 14483a40ed3dSBarry Smith PetscFunctionBegin; 1449273d9f13SBarry Smith if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 145097f1f81fSBarry Smith ierr = PetscMalloc((1+A->n)*sizeof(PetscInt),&col);CHKERRQ(ierr); 145197f1f81fSBarry Smith ierr = PetscMemzero(col,(1+A->n)*sizeof(PetscInt));CHKERRQ(ierr); 1452bfeeae90SHong Zhang 1453bfeeae90SHong Zhang for (i=0; i<ai[m]; i++) col[aj[i]] += 1; 1454f69a0ea3SMatthew Knepley ierr = MatCreate(A->comm,&C);CHKERRQ(ierr); 1455f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,A->n,m,A->n,m);CHKERRQ(ierr); 1456f204ca49SKris Buschelman ierr = MatSetType(C,A->type_name);CHKERRQ(ierr); 1457ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr); 1458606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 145917ab2063SBarry Smith for (i=0; i<m; i++) { 146017ab2063SBarry Smith len = ai[i+1]-ai[i]; 146187d4246cSBarry Smith ierr = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 1462b9b97703SBarry Smith array += len; 1463b9b97703SBarry Smith aj += len; 146417ab2063SBarry Smith } 146517ab2063SBarry Smith 14666d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14676d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 146817ab2063SBarry Smith 1469f1e2ffcdSBarry Smith if (B) { 1470416022c9SBarry Smith *B = C; 147117ab2063SBarry Smith } else { 1472273d9f13SBarry Smith ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 147317ab2063SBarry Smith } 14743a40ed3dSBarry Smith PetscFunctionReturn(0); 147517ab2063SBarry Smith } 147617ab2063SBarry Smith 1477cd0d46ebSvictorle EXTERN_C_BEGIN 1478cd0d46ebSvictorle #undef __FUNCT__ 14795fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ" 1480be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f) 1481cd0d46ebSvictorle { 1482cd0d46ebSvictorle Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 148397f1f81fSBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; PetscScalar *va,*vb; 14846849ba73SBarry Smith PetscErrorCode ierr; 148597f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 1486cd0d46ebSvictorle 1487cd0d46ebSvictorle PetscFunctionBegin; 1488cd0d46ebSvictorle bij = (Mat_SeqAIJ *) B->data; 1489cd0d46ebSvictorle 1490cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 1491cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 14925485867bSBarry Smith if (ma!=nb || na!=mb){ 14935485867bSBarry Smith *f = PETSC_FALSE; 14945485867bSBarry Smith PetscFunctionReturn(0); 14955485867bSBarry Smith } 1496cd0d46ebSvictorle aii = aij->i; bii = bij->i; 1497cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 1498cd0d46ebSvictorle va = aij->a; vb = bij->a; 149997f1f81fSBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 150097f1f81fSBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 1501cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 1502cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 1503cd0d46ebSvictorle 1504cd0d46ebSvictorle *f = PETSC_TRUE; 1505cd0d46ebSvictorle for (i=0; i<ma; i++) { 1506cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 150797f1f81fSBarry Smith PetscInt idc,idr; 15085485867bSBarry Smith PetscScalar vc,vr; 1509cd0d46ebSvictorle /* column/row index/value */ 15105485867bSBarry Smith idc = adx[aptr[i]]; 15115485867bSBarry Smith idr = bdx[bptr[idc]]; 15125485867bSBarry Smith vc = va[aptr[i]]; 15135485867bSBarry Smith vr = vb[bptr[idc]]; 15145485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 15155485867bSBarry Smith *f = PETSC_FALSE; 15165485867bSBarry Smith goto done; 1517cd0d46ebSvictorle } else { 15185485867bSBarry Smith aptr[i]++; 15195485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 1520cd0d46ebSvictorle } 1521cd0d46ebSvictorle } 1522cd0d46ebSvictorle } 1523cd0d46ebSvictorle done: 1524cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 15253aeef889SHong Zhang if (B) { 15263aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 15273aeef889SHong Zhang } 1528cd0d46ebSvictorle PetscFunctionReturn(0); 1529cd0d46ebSvictorle } 1530cd0d46ebSvictorle EXTERN_C_END 1531cd0d46ebSvictorle 15329e29f15eSvictorle #undef __FUNCT__ 15339e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ" 1534dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f) 15359e29f15eSvictorle { 1536dfbe8321SBarry Smith PetscErrorCode ierr; 15379e29f15eSvictorle PetscFunctionBegin; 15385485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 15399e29f15eSvictorle PetscFunctionReturn(0); 15409e29f15eSvictorle } 15419e29f15eSvictorle 15424a2ae208SSatish Balay #undef __FUNCT__ 15434a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ" 1544dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 154517ab2063SBarry Smith { 1546416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 154787828ca2SBarry Smith PetscScalar *l,*r,x,*v; 1548dfbe8321SBarry Smith PetscErrorCode ierr; 154997f1f81fSBarry Smith PetscInt i,j,m = A->m,n = A->n,M,nz = a->nz,*jj; 155017ab2063SBarry Smith 15513a40ed3dSBarry Smith PetscFunctionBegin; 155217ab2063SBarry Smith if (ll) { 15533ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 15543ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1555e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1556273d9f13SBarry Smith if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 15571ebc52fbSHong Zhang ierr = VecGetArray(ll,&l);CHKERRQ(ierr); 1558416022c9SBarry Smith v = a->a; 155917ab2063SBarry Smith for (i=0; i<m; i++) { 156017ab2063SBarry Smith x = l[i]; 1561416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 156217ab2063SBarry Smith for (j=0; j<M; j++) { (*v++) *= x;} 156317ab2063SBarry Smith } 15641ebc52fbSHong Zhang ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr); 1565efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 156617ab2063SBarry Smith } 156717ab2063SBarry Smith if (rr) { 1568e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1569273d9f13SBarry Smith if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 15701ebc52fbSHong Zhang ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1571416022c9SBarry Smith v = a->a; jj = a->j; 157217ab2063SBarry Smith for (i=0; i<nz; i++) { 1573bfeeae90SHong Zhang (*v++) *= r[*jj++]; 157417ab2063SBarry Smith } 15751ebc52fbSHong Zhang ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 1576efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 157717ab2063SBarry Smith } 15783a40ed3dSBarry Smith PetscFunctionReturn(0); 157917ab2063SBarry Smith } 158017ab2063SBarry Smith 15814a2ae208SSatish Balay #undef __FUNCT__ 15824a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ" 158397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 158417ab2063SBarry Smith { 1585db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 15866849ba73SBarry Smith PetscErrorCode ierr; 158797f1f81fSBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->n,*lens; 158897f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 158997f1f81fSBarry Smith PetscInt *irow,*icol,nrows,ncols; 159097f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 159187828ca2SBarry Smith PetscScalar *a_new,*mat_a; 1592416022c9SBarry Smith Mat C; 1593fee21e36SBarry Smith PetscTruth stride; 159417ab2063SBarry Smith 15953a40ed3dSBarry Smith PetscFunctionBegin; 1596d64ed03dSBarry Smith ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr); 159729bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 1598d64ed03dSBarry Smith ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr); 159929bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); 160099141d43SSatish Balay 160117ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 1602b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 1603b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 160417ab2063SBarry Smith 1605fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 1606fee21e36SBarry Smith ierr = ISStride(iscol,&stride);CHKERRQ(ierr); 1607fee21e36SBarry Smith if (stride && step == 1) { 160802834360SBarry Smith /* special case of contiguous rows */ 160997f1f81fSBarry Smith ierr = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 161031ebf83bSSatish Balay starts = lens + nrows; 161102834360SBarry Smith /* loop over new rows determining lens and starting points */ 161202834360SBarry Smith for (i=0; i<nrows; i++) { 1613bfeeae90SHong Zhang kstart = ai[irow[i]]; 1614a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 161502834360SBarry Smith for (k=kstart; k<kend; k++) { 1616bfeeae90SHong Zhang if (aj[k] >= first) { 161702834360SBarry Smith starts[i] = k; 161802834360SBarry Smith break; 161902834360SBarry Smith } 162002834360SBarry Smith } 1621a2744918SBarry Smith sum = 0; 162202834360SBarry Smith while (k < kend) { 1623bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 1624a2744918SBarry Smith sum++; 162502834360SBarry Smith } 1626a2744918SBarry Smith lens[i] = sum; 162702834360SBarry Smith } 162802834360SBarry Smith /* create submatrix */ 1629cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 163097f1f81fSBarry Smith PetscInt n_cols,n_rows; 163108480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 163229bbc08cSBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 1633d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 163408480c60SBarry Smith C = *B; 16353a40ed3dSBarry Smith } else { 1636f69a0ea3SMatthew Knepley ierr = MatCreate(A->comm,&C);CHKERRQ(ierr); 1637f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 1638e2d9671bSKris Buschelman ierr = MatSetType(C,A->type_name);CHKERRQ(ierr); 1639ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 164008480c60SBarry Smith } 1641db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 1642db02288aSLois Curfman McInnes 164302834360SBarry Smith /* loop over rows inserting into submatrix */ 1644db02288aSLois Curfman McInnes a_new = c->a; 1645db02288aSLois Curfman McInnes j_new = c->j; 1646db02288aSLois Curfman McInnes i_new = c->i; 1647bfeeae90SHong Zhang 164802834360SBarry Smith for (i=0; i<nrows; i++) { 1649a2744918SBarry Smith ii = starts[i]; 1650a2744918SBarry Smith lensi = lens[i]; 1651a2744918SBarry Smith for (k=0; k<lensi; k++) { 1652a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 165302834360SBarry Smith } 165487828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 1655a2744918SBarry Smith a_new += lensi; 1656a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1657a2744918SBarry Smith c->ilen[i] = lensi; 165802834360SBarry Smith } 1659606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 16603a40ed3dSBarry Smith } else { 166102834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 166297f1f81fSBarry Smith ierr = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr); 1663bfeeae90SHong Zhang 166497f1f81fSBarry Smith ierr = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 166597f1f81fSBarry Smith ierr = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr); 166617ab2063SBarry Smith for (i=0; i<ncols; i++) smap[icol[i]] = i+1; 166702834360SBarry Smith /* determine lens of each row */ 166802834360SBarry Smith for (i=0; i<nrows; i++) { 1669bfeeae90SHong Zhang kstart = ai[irow[i]]; 167002834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 167102834360SBarry Smith lens[i] = 0; 167202834360SBarry Smith for (k=kstart; k<kend; k++) { 1673bfeeae90SHong Zhang if (smap[aj[k]]) { 167402834360SBarry Smith lens[i]++; 167502834360SBarry Smith } 167602834360SBarry Smith } 167702834360SBarry Smith } 167817ab2063SBarry Smith /* Create and fill new matrix */ 1679a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 16800f5bd95cSBarry Smith PetscTruth equal; 16810f5bd95cSBarry Smith 168299141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 1683273d9f13SBarry Smith if ((*B)->m != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 168497f1f81fSBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(PetscInt),&equal);CHKERRQ(ierr); 16850f5bd95cSBarry Smith if (!equal) { 168629bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 168799141d43SSatish Balay } 168897f1f81fSBarry Smith ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(PetscInt));CHKERRQ(ierr); 168908480c60SBarry Smith C = *B; 16903a40ed3dSBarry Smith } else { 1691f69a0ea3SMatthew Knepley ierr = MatCreate(A->comm,&C);CHKERRQ(ierr); 1692f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 1693e2d9671bSKris Buschelman ierr = MatSetType(C,A->type_name);CHKERRQ(ierr); 1694ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 169508480c60SBarry Smith } 169699141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 169717ab2063SBarry Smith for (i=0; i<nrows; i++) { 169899141d43SSatish Balay row = irow[i]; 1699bfeeae90SHong Zhang kstart = ai[row]; 170099141d43SSatish Balay kend = kstart + a->ilen[row]; 1701bfeeae90SHong Zhang mat_i = c->i[i]; 170299141d43SSatish Balay mat_j = c->j + mat_i; 170399141d43SSatish Balay mat_a = c->a + mat_i; 170499141d43SSatish Balay mat_ilen = c->ilen + i; 170517ab2063SBarry Smith for (k=kstart; k<kend; k++) { 1706bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 1707ed480e8bSBarry Smith *mat_j++ = tcol - 1; 170899141d43SSatish Balay *mat_a++ = a->a[k]; 170999141d43SSatish Balay (*mat_ilen)++; 171099141d43SSatish Balay 171117ab2063SBarry Smith } 171217ab2063SBarry Smith } 171317ab2063SBarry Smith } 171402834360SBarry Smith /* Free work space */ 171502834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 1716606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 1717606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 171802834360SBarry Smith } 17196d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17206d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 172117ab2063SBarry Smith 172217ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 1723416022c9SBarry Smith *B = C; 17243a40ed3dSBarry Smith PetscFunctionReturn(0); 172517ab2063SBarry Smith } 172617ab2063SBarry Smith 1727a871dcd8SBarry Smith /* 1728a871dcd8SBarry Smith */ 17294a2ae208SSatish Balay #undef __FUNCT__ 17304a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ" 1731dfbe8321SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatFactorInfo *info) 1732a871dcd8SBarry Smith { 173363b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1734dfbe8321SBarry Smith PetscErrorCode ierr; 173563b91edcSBarry Smith Mat outA; 1736b8a78c4aSBarry Smith PetscTruth row_identity,col_identity; 173763b91edcSBarry Smith 17383a40ed3dSBarry Smith PetscFunctionBegin; 1739d3d32019SBarry Smith if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 1740b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 1741b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 1742b8a78c4aSBarry Smith if (!row_identity || !col_identity) { 1743634064b4SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for in-place ILU"); 1744b8a78c4aSBarry Smith } 1745a871dcd8SBarry Smith 174663b91edcSBarry Smith outA = inA; 174763b91edcSBarry Smith inA->factor = FACTOR_LU; 174863b91edcSBarry Smith a->row = row; 174963b91edcSBarry Smith a->col = col; 1750c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 1751c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 175263b91edcSBarry Smith 175336db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 1754b9b97703SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */ 17554c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 175652e6d16bSBarry Smith ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr); 1757f0ec6fceSSatish Balay 175894a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 175987828ca2SBarry Smith ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr); 176094a9d846SBarry Smith } 176163b91edcSBarry Smith 176208480c60SBarry Smith if (!a->diag) { 1763f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 176463b91edcSBarry Smith } 1765af281ebdSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ(inA,info,&outA);CHKERRQ(ierr); 17663a40ed3dSBarry Smith PetscFunctionReturn(0); 1767a871dcd8SBarry Smith } 1768a871dcd8SBarry Smith 1769d9eff348SSatish Balay #include "petscblaslapack.h" 17704a2ae208SSatish Balay #undef __FUNCT__ 17714a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ" 1772f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 1773f0b747eeSBarry Smith { 1774f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 17754ce68768SBarry Smith PetscBLASInt bnz = (PetscBLASInt)a->nz,one = 1; 1776f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 1777efee365bSSatish Balay PetscErrorCode ierr; 1778efee365bSSatish Balay 17793a40ed3dSBarry Smith 17803a40ed3dSBarry Smith PetscFunctionBegin; 1781f4df32b1SMatthew Knepley BLASscal_(&bnz,&oalpha,a->a,&one); 1782efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 17833a40ed3dSBarry Smith PetscFunctionReturn(0); 1784f0b747eeSBarry Smith } 1785f0b747eeSBarry Smith 17864a2ae208SSatish Balay #undef __FUNCT__ 17874a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ" 178897f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 1789cddf8d76SBarry Smith { 1790dfbe8321SBarry Smith PetscErrorCode ierr; 179197f1f81fSBarry Smith PetscInt i; 1792cddf8d76SBarry Smith 17933a40ed3dSBarry Smith PetscFunctionBegin; 1794cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 1795b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr); 1796cddf8d76SBarry Smith } 1797cddf8d76SBarry Smith 1798cddf8d76SBarry Smith for (i=0; i<n; i++) { 17996a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 1800cddf8d76SBarry Smith } 18013a40ed3dSBarry Smith PetscFunctionReturn(0); 1802cddf8d76SBarry Smith } 1803cddf8d76SBarry Smith 18044a2ae208SSatish Balay #undef __FUNCT__ 18054a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ" 180697f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 18074dcbc457SBarry Smith { 1808e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 18096849ba73SBarry Smith PetscErrorCode ierr; 181097f1f81fSBarry Smith PetscInt row,i,j,k,l,m,n,*idx,*nidx,isz,val; 181197f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 1812f1af5d2fSBarry Smith PetscBT table; 1813bbd702dbSSatish Balay 18143a40ed3dSBarry Smith PetscFunctionBegin; 1815273d9f13SBarry Smith m = A->m; 1816e4d965acSSatish Balay ai = a->i; 1817bfeeae90SHong Zhang aj = a->j; 18188a047759SSatish Balay 1819a45adfd6SMatthew Knepley if (ov < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 182006763907SSatish Balay 182197f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr); 18226831982aSBarry Smith ierr = PetscBTCreate(m,table);CHKERRQ(ierr); 182306763907SSatish Balay 1824e4d965acSSatish Balay for (i=0; i<is_max; i++) { 1825b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1826e4d965acSSatish Balay isz = 0; 18276831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 1828e4d965acSSatish Balay 1829e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 18304dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 1831b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 1832e4d965acSSatish Balay 1833dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1834e4d965acSSatish Balay for (j=0; j<n ; ++j){ 1835f1af5d2fSBarry Smith if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];} 18364dcbc457SBarry Smith } 183706763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 183806763907SSatish Balay ierr = ISDestroy(is[i]);CHKERRQ(ierr); 1839e4d965acSSatish Balay 184004a348a9SBarry Smith k = 0; 184104a348a9SBarry Smith for (j=0; j<ov; j++){ /* for each overlap */ 184204a348a9SBarry Smith n = isz; 184306763907SSatish Balay for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 1844e4d965acSSatish Balay row = nidx[k]; 1845e4d965acSSatish Balay start = ai[row]; 1846e4d965acSSatish Balay end = ai[row+1]; 184704a348a9SBarry Smith for (l = start; l<end ; l++){ 1848efb16452SHong Zhang val = aj[l] ; 1849f1af5d2fSBarry Smith if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;} 1850e4d965acSSatish Balay } 1851e4d965acSSatish Balay } 1852e4d965acSSatish Balay } 1853029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr); 1854e4d965acSSatish Balay } 18556831982aSBarry Smith ierr = PetscBTDestroy(table);CHKERRQ(ierr); 1856606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 18573a40ed3dSBarry Smith PetscFunctionReturn(0); 18584dcbc457SBarry Smith } 185917ab2063SBarry Smith 18600513a670SBarry Smith /* -------------------------------------------------------------- */ 18614a2ae208SSatish Balay #undef __FUNCT__ 18624a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ" 1863dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 18640513a670SBarry Smith { 18650513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 18666849ba73SBarry Smith PetscErrorCode ierr; 186797f1f81fSBarry Smith PetscInt i,nz,m = A->m,n = A->n,*col; 186897f1f81fSBarry Smith PetscInt *row,*cnew,j,*lens; 186956cd22aeSBarry Smith IS icolp,irowp; 187097f1f81fSBarry Smith PetscInt *cwork; 187132ec9ce4SBarry Smith PetscScalar *vwork; 18720513a670SBarry Smith 18733a40ed3dSBarry Smith PetscFunctionBegin; 18744c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 187556cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 18764c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 187756cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 18780513a670SBarry Smith 18790513a670SBarry Smith /* determine lengths of permuted rows */ 188097f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 18810513a670SBarry Smith for (i=0; i<m; i++) { 18820513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 18830513a670SBarry Smith } 1884f69a0ea3SMatthew Knepley ierr = MatCreate(A->comm,B);CHKERRQ(ierr); 1885f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 1886f204ca49SKris Buschelman ierr = MatSetType(*B,A->type_name);CHKERRQ(ierr); 1887ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 1888606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 18890513a670SBarry Smith 189097f1f81fSBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr); 18910513a670SBarry Smith for (i=0; i<m; i++) { 189232ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 18930513a670SBarry Smith for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];} 1894cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 189532ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 18960513a670SBarry Smith } 1897606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 18983c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 18990513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 19000513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 190156cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 190256cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 190356cd22aeSBarry Smith ierr = ISDestroy(irowp);CHKERRQ(ierr); 190456cd22aeSBarry Smith ierr = ISDestroy(icolp);CHKERRQ(ierr); 19053a40ed3dSBarry Smith PetscFunctionReturn(0); 19060513a670SBarry Smith } 19070513a670SBarry Smith 19084a2ae208SSatish Balay #undef __FUNCT__ 19094a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ" 1910dfbe8321SBarry Smith PetscErrorCode MatPrintHelp_SeqAIJ(Mat A) 1911682d7d0cSBarry Smith { 1912c38d4ed2SBarry Smith static PetscTruth called = PETSC_FALSE; 1913682d7d0cSBarry Smith MPI_Comm comm = A->comm; 1914dfbe8321SBarry Smith PetscErrorCode ierr; 1915682d7d0cSBarry Smith 19163a40ed3dSBarry Smith PetscFunctionBegin; 19174846f1f5SKris Buschelman ierr = MatPrintHelp_Inode(A);CHKERRQ(ierr); 1918c38d4ed2SBarry Smith if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE; 1919d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr); 1920d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr); 1921d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr); 192273e7a558SHong Zhang ierr = (*PetscHelpPrintf)(comm," -mat_no_compressedrow: Do not use compressedrow\n");CHKERRQ(ierr); 19233a40ed3dSBarry Smith PetscFunctionReturn(0); 1924682d7d0cSBarry Smith } 192597304618SKris Buschelman 19264a2ae208SSatish Balay #undef __FUNCT__ 19274a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ" 1928dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 1929cb5b572fSBarry Smith { 1930dfbe8321SBarry Smith PetscErrorCode ierr; 1931cb5b572fSBarry Smith 1932cb5b572fSBarry Smith PetscFunctionBegin; 193333f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 193433f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 1935be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1936be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 1937be6bf707SBarry Smith 1938bfeeae90SHong Zhang if (a->i[A->m] != b->i[B->m]) { 1939634064b4SBarry Smith SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different"); 1940cb5b572fSBarry Smith } 1941bfeeae90SHong Zhang ierr = PetscMemcpy(b->a,a->a,(a->i[A->m])*sizeof(PetscScalar));CHKERRQ(ierr); 1942cb5b572fSBarry Smith } else { 1943cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1944cb5b572fSBarry Smith } 1945cb5b572fSBarry Smith PetscFunctionReturn(0); 1946cb5b572fSBarry Smith } 1947cb5b572fSBarry Smith 19484a2ae208SSatish Balay #undef __FUNCT__ 19494a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ" 1950dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A) 1951273d9f13SBarry Smith { 1952dfbe8321SBarry Smith PetscErrorCode ierr; 1953273d9f13SBarry Smith 1954273d9f13SBarry Smith PetscFunctionBegin; 1955ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 1956273d9f13SBarry Smith PetscFunctionReturn(0); 1957273d9f13SBarry Smith } 1958273d9f13SBarry Smith 19594a2ae208SSatish Balay #undef __FUNCT__ 19604a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ" 1961dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 19626c0721eeSBarry Smith { 19636c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 19646c0721eeSBarry Smith PetscFunctionBegin; 19656c0721eeSBarry Smith *array = a->a; 19666c0721eeSBarry Smith PetscFunctionReturn(0); 19676c0721eeSBarry Smith } 19686c0721eeSBarry Smith 19694a2ae208SSatish Balay #undef __FUNCT__ 19704a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ" 1971dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 19726c0721eeSBarry Smith { 19736c0721eeSBarry Smith PetscFunctionBegin; 19746c0721eeSBarry Smith PetscFunctionReturn(0); 19756c0721eeSBarry Smith } 1976273d9f13SBarry Smith 1977ee4f033dSBarry Smith #undef __FUNCT__ 1978ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ" 1979dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 1980ee4f033dSBarry Smith { 19816849ba73SBarry Smith PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f; 19826849ba73SBarry Smith PetscErrorCode ierr; 198397f1f81fSBarry Smith PetscInt k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2; 1984efb30889SBarry Smith PetscScalar dx,*y,*xx,*w3_array; 198587828ca2SBarry Smith PetscScalar *vscale_array; 1986ee4f033dSBarry Smith PetscReal epsilon = coloring->error_rel,umin = coloring->umin; 1987ee4f033dSBarry Smith Vec w1,w2,w3; 1988ee4f033dSBarry Smith void *fctx = coloring->fctx; 1989ee4f033dSBarry Smith PetscTruth flg; 1990ee4f033dSBarry Smith 1991ee4f033dSBarry Smith PetscFunctionBegin; 1992ee4f033dSBarry Smith if (!coloring->w1) { 1993ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr); 199452e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr); 1995ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr); 199652e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr); 1997ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 199852e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr); 1999ee4f033dSBarry Smith } 2000ee4f033dSBarry Smith w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 2001ee4f033dSBarry Smith 2002ee4f033dSBarry Smith ierr = MatSetUnfactored(J);CHKERRQ(ierr); 2003e82a3eeeSBarry Smith ierr = PetscOptionsHasName(coloring->prefix,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr); 2004ee4f033dSBarry Smith if (flg) { 200563ba0a88SBarry Smith ierr = PetscLogInfo((coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"));CHKERRQ(ierr); 2006ee4f033dSBarry Smith } else { 20070b9b6f31SBarry Smith PetscTruth assembled; 20080b9b6f31SBarry Smith ierr = MatAssembled(J,&assembled);CHKERRQ(ierr); 20090b9b6f31SBarry Smith if (assembled) { 2010ee4f033dSBarry Smith ierr = MatZeroEntries(J);CHKERRQ(ierr); 2011ee4f033dSBarry Smith } 20120b9b6f31SBarry Smith } 2013ee4f033dSBarry Smith 2014ee4f033dSBarry Smith ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr); 2015ee4f033dSBarry Smith ierr = VecGetSize(x1,&N);CHKERRQ(ierr); 2016ee4f033dSBarry Smith 2017ee4f033dSBarry Smith /* 2018ee4f033dSBarry Smith This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets 2019ee4f033dSBarry Smith coloring->F for the coarser grids from the finest 2020ee4f033dSBarry Smith */ 2021ee4f033dSBarry Smith if (coloring->F) { 2022ee4f033dSBarry Smith ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr); 2023ee4f033dSBarry Smith ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr); 2024ee4f033dSBarry Smith if (m1 != m2) { 2025ee4f033dSBarry Smith coloring->F = 0; 2026ee4f033dSBarry Smith } 2027ee4f033dSBarry Smith } 2028ee4f033dSBarry Smith 2029ee4f033dSBarry Smith if (coloring->F) { 2030ee4f033dSBarry Smith w1 = coloring->F; 2031ee4f033dSBarry Smith coloring->F = 0; 2032ee4f033dSBarry Smith } else { 203366f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2034ee4f033dSBarry Smith ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 203566f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2036ee4f033dSBarry Smith } 2037ee4f033dSBarry Smith 2038ee4f033dSBarry Smith /* 2039ee4f033dSBarry Smith Compute all the scale factors and share with other processors 2040ee4f033dSBarry Smith */ 20411ebc52fbSHong Zhang ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start; 20421ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start; 2043ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 2044ee4f033dSBarry Smith /* 2045ee4f033dSBarry Smith Loop over each column associated with color adding the 2046ee4f033dSBarry Smith perturbation to the vector w3. 2047ee4f033dSBarry Smith */ 2048ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2049ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2050ee4f033dSBarry Smith dx = xx[col]; 2051ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 2052ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2053ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2054ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2055ee4f033dSBarry Smith #else 2056ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2057ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2058ee4f033dSBarry Smith #endif 2059ee4f033dSBarry Smith dx *= epsilon; 2060ee4f033dSBarry Smith vscale_array[col] = 1.0/dx; 2061ee4f033dSBarry Smith } 2062ee4f033dSBarry Smith } 20631ebc52fbSHong Zhang vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2064ee4f033dSBarry Smith ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2065ee4f033dSBarry Smith ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2066ee4f033dSBarry Smith 2067ee4f033dSBarry Smith /* ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD); 2068ee4f033dSBarry Smith ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/ 2069ee4f033dSBarry Smith 2070ee4f033dSBarry Smith if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow; 2071ee4f033dSBarry Smith else vscaleforrow = coloring->columnsforrow; 2072ee4f033dSBarry Smith 20731ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2074ee4f033dSBarry Smith /* 2075ee4f033dSBarry Smith Loop over each color 2076ee4f033dSBarry Smith */ 2077ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 207849b058dcSBarry Smith coloring->currentcolor = k; 2079ee4f033dSBarry Smith ierr = VecCopy(x1,w3);CHKERRQ(ierr); 20801ebc52fbSHong Zhang ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start; 2081ee4f033dSBarry Smith /* 2082ee4f033dSBarry Smith Loop over each column associated with color adding the 2083ee4f033dSBarry Smith perturbation to the vector w3. 2084ee4f033dSBarry Smith */ 2085ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2086ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2087ee4f033dSBarry Smith dx = xx[col]; 20885b8514ebSBarry Smith if (dx == 0.0) dx = 1.0; 2089ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2090ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2091ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2092ee4f033dSBarry Smith #else 2093ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2094ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2095ee4f033dSBarry Smith #endif 2096ee4f033dSBarry Smith dx *= epsilon; 2097634064b4SBarry Smith if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter"); 2098ee4f033dSBarry Smith w3_array[col] += dx; 2099ee4f033dSBarry Smith } 21001ebc52fbSHong Zhang w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 2101ee4f033dSBarry Smith 2102ee4f033dSBarry Smith /* 2103ee4f033dSBarry Smith Evaluate function at x1 + dx (here dx is a vector of perturbations) 2104ee4f033dSBarry Smith */ 2105ee4f033dSBarry Smith 210666f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2107ee4f033dSBarry Smith ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 210866f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2109efb30889SBarry Smith ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 2110ee4f033dSBarry Smith 2111ee4f033dSBarry Smith /* 2112ee4f033dSBarry Smith Loop over rows of vector, putting results into Jacobian matrix 2113ee4f033dSBarry Smith */ 21141ebc52fbSHong Zhang ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 2115ee4f033dSBarry Smith for (l=0; l<coloring->nrows[k]; l++) { 2116ee4f033dSBarry Smith row = coloring->rows[k][l]; 2117ee4f033dSBarry Smith col = coloring->columnsforrow[k][l]; 2118ee4f033dSBarry Smith y[row] *= vscale_array[vscaleforrow[k][l]]; 2119ee4f033dSBarry Smith srow = row + start; 2120ee4f033dSBarry Smith ierr = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 2121ee4f033dSBarry Smith } 21221ebc52fbSHong Zhang ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 2123ee4f033dSBarry Smith } 212449b058dcSBarry Smith coloring->currentcolor = k; 21251ebc52fbSHong Zhang ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 21261ebc52fbSHong Zhang xx = xx + start; ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 2127ee4f033dSBarry Smith ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2128ee4f033dSBarry Smith ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2129ee4f033dSBarry Smith PetscFunctionReturn(0); 2130ee4f033dSBarry Smith } 2131ee4f033dSBarry Smith 2132ac90fabeSBarry Smith #include "petscblaslapack.h" 2133ac90fabeSBarry Smith #undef __FUNCT__ 2134ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ" 2135f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2136ac90fabeSBarry Smith { 2137dfbe8321SBarry Smith PetscErrorCode ierr; 213897f1f81fSBarry Smith PetscInt i; 2139ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data; 21404ce68768SBarry Smith PetscBLASInt one=1,bnz = (PetscBLASInt)x->nz; 2141ac90fabeSBarry Smith 2142ac90fabeSBarry Smith PetscFunctionBegin; 2143ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2144f4df32b1SMatthew Knepley PetscScalar alpha = a; 2145f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2146c537a176SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 2147a30b2313SHong Zhang if (y->xtoy && y->XtoY != X) { 2148a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 2149a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 2150a30b2313SHong Zhang } 2151a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 215224f910e3SHong Zhang ierr = MatAXPYGetxtoy_Private(X->m,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr); 2153a30b2313SHong Zhang y->XtoY = X; 2154c537a176SHong Zhang } 2155f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 215663ba0a88SBarry Smith ierr = PetscLogInfo((0,"MatAXPY_SeqAIJ: ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz));CHKERRQ(ierr); 2157ac90fabeSBarry Smith } else { 2158f4df32b1SMatthew Knepley ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 2159ac90fabeSBarry Smith } 2160ac90fabeSBarry Smith PetscFunctionReturn(0); 2161ac90fabeSBarry Smith } 2162ac90fabeSBarry Smith 2163521d7252SBarry Smith #undef __FUNCT__ 2164521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ" 2165521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs) 2166521d7252SBarry Smith { 2167521d7252SBarry Smith PetscFunctionBegin; 2168521d7252SBarry Smith PetscFunctionReturn(0); 2169521d7252SBarry Smith } 2170521d7252SBarry Smith 2171354c94deSBarry Smith #undef __FUNCT__ 2172354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ" 2173354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat) 2174354c94deSBarry Smith { 2175354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2176354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2177354c94deSBarry Smith PetscInt i,nz; 2178354c94deSBarry Smith PetscScalar *a; 2179354c94deSBarry Smith 2180354c94deSBarry Smith PetscFunctionBegin; 2181354c94deSBarry Smith nz = aij->nz; 2182354c94deSBarry Smith a = aij->a; 2183354c94deSBarry Smith for (i=0; i<nz; i++) { 2184354c94deSBarry Smith a[i] = PetscConj(a[i]); 2185354c94deSBarry Smith } 2186354c94deSBarry Smith #else 2187354c94deSBarry Smith PetscFunctionBegin; 2188354c94deSBarry Smith #endif 2189354c94deSBarry Smith PetscFunctionReturn(0); 2190354c94deSBarry Smith } 2191354c94deSBarry Smith 2192e34fafa9SBarry Smith #undef __FUNCT__ 2193e34fafa9SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ" 2194e34fafa9SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v) 2195e34fafa9SBarry Smith { 2196e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2197e34fafa9SBarry Smith PetscErrorCode ierr; 2198e34fafa9SBarry Smith PetscInt i,j,m = A->m,*ai,*aj,ncols,n; 2199e34fafa9SBarry Smith PetscReal atmp; 2200e34fafa9SBarry Smith PetscScalar *x,zero = 0.0; 2201e34fafa9SBarry Smith MatScalar *aa; 2202e34fafa9SBarry Smith 2203e34fafa9SBarry Smith PetscFunctionBegin; 2204e34fafa9SBarry Smith if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2205e34fafa9SBarry Smith aa = a->a; 2206e34fafa9SBarry Smith ai = a->i; 2207e34fafa9SBarry Smith aj = a->j; 2208e34fafa9SBarry Smith 220943e18e04SHong Zhang ierr = VecSet(v,zero);CHKERRQ(ierr); 2210e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2211e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2212e34fafa9SBarry Smith if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2213e34fafa9SBarry Smith for (i=0; i<m; i++) { 2214e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 2215e34fafa9SBarry Smith for (j=0; j<ncols; j++){ 2216e34fafa9SBarry Smith atmp = PetscAbsScalar(*aa); aa++; 2217e34fafa9SBarry Smith if (PetscAbsScalar(x[i]) < atmp) x[i] = atmp; 2218e34fafa9SBarry Smith aj++; 2219e34fafa9SBarry Smith } 2220e34fafa9SBarry Smith } 2221e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2222e34fafa9SBarry Smith PetscFunctionReturn(0); 2223e34fafa9SBarry Smith } 2224e34fafa9SBarry Smith 2225682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 22260a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ, 2227cb5b572fSBarry Smith MatGetRow_SeqAIJ, 2228cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 2229cb5b572fSBarry Smith MatMult_SeqAIJ, 223097304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 22317c922b88SBarry Smith MatMultTranspose_SeqAIJ, 22327c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 2233cb5b572fSBarry Smith MatSolve_SeqAIJ, 2234cb5b572fSBarry Smith MatSolveAdd_SeqAIJ, 22357c922b88SBarry Smith MatSolveTranspose_SeqAIJ, 223697304618SKris Buschelman /*10*/ MatSolveTransposeAdd_SeqAIJ, 2237cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 2238cb5b572fSBarry Smith 0, 223917ab2063SBarry Smith MatRelax_SeqAIJ, 224017ab2063SBarry Smith MatTranspose_SeqAIJ, 224197304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ, 2242cb5b572fSBarry Smith MatEqual_SeqAIJ, 2243cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 2244cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 2245cb5b572fSBarry Smith MatNorm_SeqAIJ, 224697304618SKris Buschelman /*20*/ 0, 2247cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 224817ab2063SBarry Smith MatCompress_SeqAIJ, 2249cb5b572fSBarry Smith MatSetOption_SeqAIJ, 2250cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 225197304618SKris Buschelman /*25*/ MatZeroRows_SeqAIJ, 2252cb5b572fSBarry Smith MatLUFactorSymbolic_SeqAIJ, 2253cb5b572fSBarry Smith MatLUFactorNumeric_SeqAIJ, 2254f76d2b81SHong Zhang MatCholeskyFactorSymbolic_SeqAIJ, 2255a6175056SHong Zhang MatCholeskyFactorNumeric_SeqAIJ, 225697304618SKris Buschelman /*30*/ MatSetUpPreallocation_SeqAIJ, 2257cb5b572fSBarry Smith MatILUFactorSymbolic_SeqAIJ, 2258861ba921SHong Zhang MatICCFactorSymbolic_SeqAIJ, 22596c0721eeSBarry Smith MatGetArray_SeqAIJ, 22606c0721eeSBarry Smith MatRestoreArray_SeqAIJ, 226197304618SKris Buschelman /*35*/ MatDuplicate_SeqAIJ, 2262cb5b572fSBarry Smith 0, 2263cb5b572fSBarry Smith 0, 2264cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 2265cb5b572fSBarry Smith 0, 226697304618SKris Buschelman /*40*/ MatAXPY_SeqAIJ, 2267cb5b572fSBarry Smith MatGetSubMatrices_SeqAIJ, 2268cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 2269cb5b572fSBarry Smith MatGetValues_SeqAIJ, 2270cb5b572fSBarry Smith MatCopy_SeqAIJ, 227197304618SKris Buschelman /*45*/ MatPrintHelp_SeqAIJ, 2272cb5b572fSBarry Smith MatScale_SeqAIJ, 2273cb5b572fSBarry Smith 0, 227479299369SBarry Smith MatDiagonalSet_SeqAIJ, 22756945ee14SBarry Smith MatILUDTFactor_SeqAIJ, 2276521d7252SBarry Smith /*50*/ MatSetBlockSize_SeqAIJ, 22773b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 22783b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 22793b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 2280a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 228197304618SKris Buschelman /*55*/ MatFDColoringCreate_SeqAIJ, 2282b9617806SBarry Smith 0, 22830513a670SBarry Smith 0, 2284cda55fadSBarry Smith MatPermute_SeqAIJ, 2285cda55fadSBarry Smith 0, 228697304618SKris Buschelman /*60*/ 0, 2287b9b97703SBarry Smith MatDestroy_SeqAIJ, 2288b9b97703SBarry Smith MatView_SeqAIJ, 22898a124369SBarry Smith MatGetPetscMaps_Petsc, 2290ee4f033dSBarry Smith 0, 229197304618SKris Buschelman /*65*/ 0, 2292ee4f033dSBarry Smith 0, 2293ee4f033dSBarry Smith 0, 2294ee4f033dSBarry Smith 0, 2295ee4f033dSBarry Smith 0, 2296e34fafa9SBarry Smith /*70*/ MatGetRowMax_SeqAIJ, 2297ee4f033dSBarry Smith 0, 2298ee4f033dSBarry Smith MatSetColoring_SeqAIJ, 2299dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2300ee4f033dSBarry Smith MatSetValuesAdic_SeqAIJ, 2301dcf5cc72SBarry Smith #else 2302dcf5cc72SBarry Smith 0, 2303dcf5cc72SBarry Smith #endif 2304ee4f033dSBarry Smith MatSetValuesAdifor_SeqAIJ, 230597304618SKris Buschelman /*75*/ MatFDColoringApply_SeqAIJ, 230697304618SKris Buschelman 0, 230797304618SKris Buschelman 0, 230897304618SKris Buschelman 0, 230997304618SKris Buschelman 0, 231097304618SKris Buschelman /*80*/ 0, 231197304618SKris Buschelman 0, 231297304618SKris Buschelman 0, 231397304618SKris Buschelman 0, 2314bc011b1eSHong Zhang MatLoad_SeqAIJ, 2315bc011b1eSHong Zhang /*85*/ MatIsSymmetric_SeqAIJ, 23166284ec50SHong Zhang 0, 23176284ec50SHong Zhang 0, 23186284ec50SHong Zhang 0, 2319bc011b1eSHong Zhang 0, 2320bc011b1eSHong Zhang /*90*/ MatMatMult_SeqAIJ_SeqAIJ, 232126be0446SHong Zhang MatMatMultSymbolic_SeqAIJ_SeqAIJ, 232226be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 2323d439da42SKris Buschelman MatPtAP_Basic, 23247ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ, 23257ba1a0bfSKris Buschelman /*95*/ MatPtAPNumeric_SeqAIJ, 2326bc011b1eSHong Zhang MatMatMultTranspose_SeqAIJ_SeqAIJ, 2327bc011b1eSHong Zhang MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ, 2328bc011b1eSHong Zhang MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ, 23297ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ_SeqAIJ, 23307ba1a0bfSKris Buschelman /*100*/MatPtAPNumeric_SeqAIJ_SeqAIJ, 2331609c6c4dSKris Buschelman 0, 2332609c6c4dSKris Buschelman 0, 233387d4246cSBarry Smith MatConjugate_SeqAIJ, 233487d4246cSBarry Smith 0, 233599cafbc1SBarry Smith /*105*/MatSetValuesRow_SeqAIJ, 233699cafbc1SBarry Smith MatRealPart_SeqAIJ, 2337f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 2338f5edf698SHong Zhang 0, 2339*2bebee5dSHong Zhang 0, 2340*2bebee5dSHong Zhang /*110*/MatMatSolve_SeqAIJ 23419e29f15eSvictorle }; 234217ab2063SBarry Smith 2343fb2e594dSBarry Smith EXTERN_C_BEGIN 23444a2ae208SSatish Balay #undef __FUNCT__ 23454a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ" 2346be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 2347bef8e0ddSBarry Smith { 2348bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 234997f1f81fSBarry Smith PetscInt i,nz,n; 2350bef8e0ddSBarry Smith 2351bef8e0ddSBarry Smith PetscFunctionBegin; 2352bef8e0ddSBarry Smith 2353bef8e0ddSBarry Smith nz = aij->maxnz; 2354273d9f13SBarry Smith n = mat->n; 2355bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 2356bef8e0ddSBarry Smith aij->j[i] = indices[i]; 2357bef8e0ddSBarry Smith } 2358bef8e0ddSBarry Smith aij->nz = nz; 2359bef8e0ddSBarry Smith for (i=0; i<n; i++) { 2360bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 2361bef8e0ddSBarry Smith } 2362bef8e0ddSBarry Smith 2363bef8e0ddSBarry Smith PetscFunctionReturn(0); 2364bef8e0ddSBarry Smith } 2365fb2e594dSBarry Smith EXTERN_C_END 2366bef8e0ddSBarry Smith 23674a2ae208SSatish Balay #undef __FUNCT__ 23684a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices" 2369bef8e0ddSBarry Smith /*@ 2370bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 2371bef8e0ddSBarry Smith in the matrix. 2372bef8e0ddSBarry Smith 2373bef8e0ddSBarry Smith Input Parameters: 2374bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 2375bef8e0ddSBarry Smith - indices - the column indices 2376bef8e0ddSBarry Smith 237715091d37SBarry Smith Level: advanced 237815091d37SBarry Smith 2379bef8e0ddSBarry Smith Notes: 2380bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 2381bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 2382bef8e0ddSBarry Smith of the MatSetValues() operation. 2383bef8e0ddSBarry Smith 2384bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 2385d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 2386bef8e0ddSBarry Smith 2387bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 2388bef8e0ddSBarry Smith 2389b9617806SBarry Smith The indices should start with zero, not one. 2390b9617806SBarry Smith 2391bef8e0ddSBarry Smith @*/ 2392be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 2393bef8e0ddSBarry Smith { 239497f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt *); 2395bef8e0ddSBarry Smith 2396bef8e0ddSBarry Smith PetscFunctionBegin; 23974482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 23984482741eSBarry Smith PetscValidPointer(indices,2); 2399c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr); 2400bef8e0ddSBarry Smith if (f) { 2401bef8e0ddSBarry Smith ierr = (*f)(mat,indices);CHKERRQ(ierr); 2402bef8e0ddSBarry Smith } else { 2403634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices"); 2404bef8e0ddSBarry Smith } 2405bef8e0ddSBarry Smith PetscFunctionReturn(0); 2406bef8e0ddSBarry Smith } 2407bef8e0ddSBarry Smith 2408be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 2409be6bf707SBarry Smith 2410fb2e594dSBarry Smith EXTERN_C_BEGIN 24114a2ae208SSatish Balay #undef __FUNCT__ 24124a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ" 2413be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat) 2414be6bf707SBarry Smith { 2415be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 24166849ba73SBarry Smith PetscErrorCode ierr; 24176849ba73SBarry Smith size_t nz = aij->i[mat->m]; 2418be6bf707SBarry Smith 2419be6bf707SBarry Smith PetscFunctionBegin; 2420be6bf707SBarry Smith if (aij->nonew != 1) { 2421634064b4SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first"); 2422be6bf707SBarry Smith } 2423be6bf707SBarry Smith 2424be6bf707SBarry Smith /* allocate space for values if not already there */ 2425be6bf707SBarry Smith if (!aij->saved_values) { 242687828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr); 2427be6bf707SBarry Smith } 2428be6bf707SBarry Smith 2429be6bf707SBarry Smith /* copy values over */ 243087828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2431be6bf707SBarry Smith PetscFunctionReturn(0); 2432be6bf707SBarry Smith } 2433fb2e594dSBarry Smith EXTERN_C_END 2434be6bf707SBarry Smith 24354a2ae208SSatish Balay #undef __FUNCT__ 2436b9617806SBarry Smith #define __FUNCT__ "MatStoreValues" 2437be6bf707SBarry Smith /*@ 2438be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 2439be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2440be6bf707SBarry Smith nonlinear portion. 2441be6bf707SBarry Smith 2442be6bf707SBarry Smith Collect on Mat 2443be6bf707SBarry Smith 2444be6bf707SBarry Smith Input Parameters: 24450e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 2446be6bf707SBarry Smith 244715091d37SBarry Smith Level: advanced 244815091d37SBarry Smith 2449be6bf707SBarry Smith Common Usage, with SNESSolve(): 2450be6bf707SBarry Smith $ Create Jacobian matrix 2451be6bf707SBarry Smith $ Set linear terms into matrix 2452be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 2453be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 2454be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 2455be6bf707SBarry Smith $ ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); 2456be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2457be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 2458be6bf707SBarry Smith $ In your Jacobian routine 2459be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2460be6bf707SBarry Smith $ Set nonlinear terms in matrix 2461be6bf707SBarry Smith 2462be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 2463be6bf707SBarry Smith $ // build linear portion of Jacobian 2464be6bf707SBarry Smith $ ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); 2465be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2466be6bf707SBarry Smith $ loop over nonlinear iterations 2467be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2468be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 2469be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 2470be6bf707SBarry Smith $ Solve linear system with Jacobian 2471be6bf707SBarry Smith $ endloop 2472be6bf707SBarry Smith 2473be6bf707SBarry Smith Notes: 2474be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 2475be6bf707SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before 2476be6bf707SBarry Smith calling this routine. 2477be6bf707SBarry Smith 24780c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 24790c468ba9SBarry Smith and does not allocated additional space. 24800c468ba9SBarry Smith 2481be6bf707SBarry Smith .seealso: MatRetrieveValues() 2482be6bf707SBarry Smith 2483be6bf707SBarry Smith @*/ 2484be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat) 2485be6bf707SBarry Smith { 2486dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2487be6bf707SBarry Smith 2488be6bf707SBarry Smith PetscFunctionBegin; 24894482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 249029bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 249129bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2492be6bf707SBarry Smith 2493c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2494be6bf707SBarry Smith if (f) { 2495be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2496be6bf707SBarry Smith } else { 2497634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values"); 2498be6bf707SBarry Smith } 2499be6bf707SBarry Smith PetscFunctionReturn(0); 2500be6bf707SBarry Smith } 2501be6bf707SBarry Smith 2502fb2e594dSBarry Smith EXTERN_C_BEGIN 25034a2ae208SSatish Balay #undef __FUNCT__ 25044a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ" 2505be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat) 2506be6bf707SBarry Smith { 2507be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 25086849ba73SBarry Smith PetscErrorCode ierr; 250997f1f81fSBarry Smith PetscInt nz = aij->i[mat->m]; 2510be6bf707SBarry Smith 2511be6bf707SBarry Smith PetscFunctionBegin; 2512be6bf707SBarry Smith if (aij->nonew != 1) { 2513634064b4SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first"); 2514be6bf707SBarry Smith } 2515be6bf707SBarry Smith if (!aij->saved_values) { 2516634064b4SBarry Smith SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 2517be6bf707SBarry Smith } 2518be6bf707SBarry Smith /* copy values over */ 251987828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2520be6bf707SBarry Smith PetscFunctionReturn(0); 2521be6bf707SBarry Smith } 2522fb2e594dSBarry Smith EXTERN_C_END 2523be6bf707SBarry Smith 25244a2ae208SSatish Balay #undef __FUNCT__ 25254a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues" 2526be6bf707SBarry Smith /*@ 2527be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 2528be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2529be6bf707SBarry Smith nonlinear portion. 2530be6bf707SBarry Smith 2531be6bf707SBarry Smith Collect on Mat 2532be6bf707SBarry Smith 2533be6bf707SBarry Smith Input Parameters: 2534be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2535be6bf707SBarry Smith 253615091d37SBarry Smith Level: advanced 253715091d37SBarry Smith 2538be6bf707SBarry Smith .seealso: MatStoreValues() 2539be6bf707SBarry Smith 2540be6bf707SBarry Smith @*/ 2541be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat) 2542be6bf707SBarry Smith { 2543dfbe8321SBarry Smith PetscErrorCode ierr,(*f)(Mat); 2544be6bf707SBarry Smith 2545be6bf707SBarry Smith PetscFunctionBegin; 25464482741eSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE,1); 254729bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 254829bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2549be6bf707SBarry Smith 2550c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2551be6bf707SBarry Smith if (f) { 2552be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2553be6bf707SBarry Smith } else { 2554634064b4SBarry Smith SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values"); 2555be6bf707SBarry Smith } 2556be6bf707SBarry Smith PetscFunctionReturn(0); 2557be6bf707SBarry Smith } 2558be6bf707SBarry Smith 2559f83d6046SBarry Smith 2560be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 25614a2ae208SSatish Balay #undef __FUNCT__ 25624a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ" 256317ab2063SBarry Smith /*@C 2564682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 25650d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 25666e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 256751c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 25682bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 256917ab2063SBarry Smith 2570db81eaa0SLois Curfman McInnes Collective on MPI_Comm 2571db81eaa0SLois Curfman McInnes 257217ab2063SBarry Smith Input Parameters: 2573db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 257417ab2063SBarry Smith . m - number of rows 257517ab2063SBarry Smith . n - number of columns 257617ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 257751c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 25782bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 257917ab2063SBarry Smith 258017ab2063SBarry Smith Output Parameter: 2581416022c9SBarry Smith . A - the matrix 258217ab2063SBarry Smith 2583b259b22eSLois Curfman McInnes Notes: 258449a6f317SBarry Smith If nnz is given then nz is ignored 258549a6f317SBarry Smith 258617ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 258717ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 25880002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 258944cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 259017ab2063SBarry Smith 259117ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2592a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 25933d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 25946da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 259517ab2063SBarry Smith 2596682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 25974fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 2598682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 25996c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 26006c7ebb05SLois Curfman McInnes 26016c7ebb05SLois Curfman McInnes Options Database Keys: 2602698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2603698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2604db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 2605db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 2606db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 260717ab2063SBarry Smith 2608027ccd11SLois Curfman McInnes Level: intermediate 2609027ccd11SLois Curfman McInnes 261036db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 261136db0b34SBarry Smith 261217ab2063SBarry Smith @*/ 2613be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 261417ab2063SBarry Smith { 2615dfbe8321SBarry Smith PetscErrorCode ierr; 26166945ee14SBarry Smith 26173a40ed3dSBarry Smith PetscFunctionBegin; 2618f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 2619f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 2620273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 2621ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr); 2622273d9f13SBarry Smith PetscFunctionReturn(0); 2623273d9f13SBarry Smith } 2624273d9f13SBarry Smith 26254a2ae208SSatish Balay #undef __FUNCT__ 26264a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation" 2627273d9f13SBarry Smith /*@C 2628273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 2629273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 2630273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 2631273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 2632273d9f13SBarry Smith 2633273d9f13SBarry Smith Collective on MPI_Comm 2634273d9f13SBarry Smith 2635273d9f13SBarry Smith Input Parameters: 263645bfc511SMatthew Knepley + B - The matrix 2637273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 2638273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 2639273d9f13SBarry Smith (possibly different for each row) or PETSC_NULL 2640273d9f13SBarry Smith 2641273d9f13SBarry Smith Notes: 264249a6f317SBarry Smith If nnz is given then nz is ignored 264349a6f317SBarry Smith 2644273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2645273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2646273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2647273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 2648273d9f13SBarry Smith 2649273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2650273d9f13SBarry Smith Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 2651273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 2652273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 2653273d9f13SBarry Smith 2654a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 2655a96a251dSBarry Smith entries or columns indices 2656a96a251dSBarry Smith 2657273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 2658273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 2659273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 2660273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2661273d9f13SBarry Smith 2662273d9f13SBarry Smith Options Database Keys: 2663698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 2664698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 2665273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2666273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2667273d9f13SBarry Smith the user still MUST index entries starting at 0! 2668273d9f13SBarry Smith 2669273d9f13SBarry Smith Level: intermediate 2670273d9f13SBarry Smith 2671273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 2672273d9f13SBarry Smith 2673273d9f13SBarry Smith @*/ 2674be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 2675273d9f13SBarry Smith { 267697f1f81fSBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]); 2677a23d5eceSKris Buschelman 2678a23d5eceSKris Buschelman PetscFunctionBegin; 2679a23d5eceSKris Buschelman ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr); 2680a23d5eceSKris Buschelman if (f) { 2681a23d5eceSKris Buschelman ierr = (*f)(B,nz,nnz);CHKERRQ(ierr); 2682a23d5eceSKris Buschelman } 2683a23d5eceSKris Buschelman PetscFunctionReturn(0); 2684a23d5eceSKris Buschelman } 2685a23d5eceSKris Buschelman 2686a23d5eceSKris Buschelman EXTERN_C_BEGIN 2687a23d5eceSKris Buschelman #undef __FUNCT__ 2688a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ" 2689be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz) 2690a23d5eceSKris Buschelman { 2691273d9f13SBarry Smith Mat_SeqAIJ *b; 2692a43ee2ecSKris Buschelman PetscTruth skipallocation = PETSC_FALSE; 26936849ba73SBarry Smith PetscErrorCode ierr; 269497f1f81fSBarry Smith PetscInt i; 2695273d9f13SBarry Smith 2696273d9f13SBarry Smith PetscFunctionBegin; 2697d5d45c9bSBarry Smith 2698a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 2699c461c341SBarry Smith skipallocation = PETSC_TRUE; 2700c461c341SBarry Smith nz = 0; 2701c461c341SBarry Smith } 2702c461c341SBarry Smith 2703435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 2704435da068SBarry Smith if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz); 2705b73539f3SBarry Smith if (nnz) { 2706273d9f13SBarry Smith for (i=0; i<B->m; i++) { 270729bbc08cSBarry Smith if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]); 27083a7fca6bSBarry Smith if (nnz[i] > B->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->n); 2709b73539f3SBarry Smith } 2710b73539f3SBarry Smith } 2711b73539f3SBarry Smith 2712273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 2713273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 2714273d9f13SBarry Smith 2715ab93d7beSBarry Smith if (!skipallocation) { 2716ab93d7beSBarry Smith ierr = PetscMalloc2(B->m,PetscInt,&b->imax,B->m,PetscInt,&b->ilen);CHKERRQ(ierr); 2717273d9f13SBarry Smith if (!nnz) { 2718435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 2719273d9f13SBarry Smith else if (nz <= 0) nz = 1; 2720273d9f13SBarry Smith for (i=0; i<B->m; i++) b->imax[i] = nz; 2721273d9f13SBarry Smith nz = nz*B->m; 2722273d9f13SBarry Smith } else { 2723273d9f13SBarry Smith nz = 0; 2724273d9f13SBarry Smith for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 2725273d9f13SBarry Smith } 2726273d9f13SBarry Smith 2727ab93d7beSBarry Smith /* b->ilen will count nonzeros in each row so far. */ 2728ab93d7beSBarry Smith for (i=0; i<B->m; i++) { b->ilen[i] = 0;} 2729ab93d7beSBarry Smith 2730273d9f13SBarry Smith /* allocate the matrix space */ 2731a96a251dSBarry Smith ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->m+1,PetscInt,&b->i);CHKERRQ(ierr); 2732bfeeae90SHong Zhang b->i[0] = 0; 27335da197adSKris Buschelman for (i=1; i<B->m+1; i++) { 27345da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 27355da197adSKris Buschelman } 2736273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 2737273d9f13SBarry Smith b->freedata = PETSC_TRUE; 2738c461c341SBarry Smith } else { 2739c461c341SBarry Smith b->freedata = PETSC_FALSE; 2740c461c341SBarry Smith } 2741273d9f13SBarry Smith 2742273d9f13SBarry Smith b->nz = 0; 2743273d9f13SBarry Smith b->maxnz = nz; 2744273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 2745273d9f13SBarry Smith PetscFunctionReturn(0); 2746273d9f13SBarry Smith } 2747a23d5eceSKris Buschelman EXTERN_C_END 2748273d9f13SBarry Smith 2749a1661176SMatthew Knepley #undef __FUNCT__ 2750a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR" 2751a1661176SMatthew Knepley /*@C 2752a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 2753a1661176SMatthew Knepley 2754a1661176SMatthew Knepley Input Parameters: 2755a1661176SMatthew Knepley + B - the matrix 2756a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 2757a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 2758a1661176SMatthew Knepley - v - optional values in the matrix 2759a1661176SMatthew Knepley 2760d58b2322SMatthew Knepley Contributed by: Lisandro Dalchin 2761d58b2322SMatthew Knepley 2762a1661176SMatthew Knepley Level: developer 2763a1661176SMatthew Knepley 2764a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential 2765a1661176SMatthew Knepley 2766a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ 2767a1661176SMatthew Knepley @*/ 2768a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 2769a1661176SMatthew Knepley { 2770a1661176SMatthew Knepley PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]); 2771a1661176SMatthew Knepley PetscErrorCode ierr; 2772a1661176SMatthew Knepley 2773a1661176SMatthew Knepley PetscFunctionBegin; 2774a1661176SMatthew Knepley PetscValidHeaderSpecific(B,MAT_COOKIE,1); 2775a1661176SMatthew Knepley ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr); 2776a1661176SMatthew Knepley if (f) { 2777a1661176SMatthew Knepley ierr = (*f)(B,i,j,v);CHKERRQ(ierr); 2778a1661176SMatthew Knepley } 2779a1661176SMatthew Knepley PetscFunctionReturn(0); 2780a1661176SMatthew Knepley } 2781a1661176SMatthew Knepley 2782a1661176SMatthew Knepley EXTERN_C_BEGIN 2783a1661176SMatthew Knepley #undef __FUNCT__ 2784a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR_SeqAIJ" 2785a1661176SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt I[],const PetscInt J[],const PetscScalar v[]) 2786a1661176SMatthew Knepley { 2787a1661176SMatthew Knepley PetscInt i; 2788a1661176SMatthew Knepley PetscInt m,n; 2789a1661176SMatthew Knepley PetscInt nz; 2790a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 2791a1661176SMatthew Knepley PetscScalar *values; 2792a1661176SMatthew Knepley PetscErrorCode ierr; 2793a1661176SMatthew Knepley 2794a1661176SMatthew Knepley PetscFunctionBegin; 2795a1661176SMatthew Knepley ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 2796a1661176SMatthew Knepley 2797a1661176SMatthew Knepley if (I[0]) { 2798a1661176SMatthew Knepley SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "I[0] must be 0 it is %D", I[0]); 2799a1661176SMatthew Knepley } 2800a1661176SMatthew Knepley ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr); 2801a1661176SMatthew Knepley for(i = 0; i < m; i++) { 2802a1661176SMatthew Knepley nz = I[i+1]- I[i]; 2803a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 2804a1661176SMatthew Knepley if (nz < 0) { 2805a1661176SMatthew Knepley SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 2806a1661176SMatthew Knepley } 2807a1661176SMatthew Knepley nnz[i] = nz; 2808a1661176SMatthew Knepley } 2809a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 2810a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 2811a1661176SMatthew Knepley 2812a1661176SMatthew Knepley if (v) { 2813a1661176SMatthew Knepley values = (PetscScalar*) v; 2814a1661176SMatthew Knepley } else { 2815a1661176SMatthew Knepley ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr); 2816a1661176SMatthew Knepley ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr); 2817a1661176SMatthew Knepley } 2818a1661176SMatthew Knepley 2819a1661176SMatthew Knepley ierr = MatSetOption(B,MAT_COLUMNS_SORTED);CHKERRQ(ierr); 2820a1661176SMatthew Knepley 2821a1661176SMatthew Knepley for(i = 0; i < m; i++) { 2822a1661176SMatthew Knepley nz = I[i+1] - I[i]; 2823a1661176SMatthew Knepley ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+I[i], values + (v ? I[i] : 0), INSERT_VALUES);CHKERRQ(ierr); 2824a1661176SMatthew Knepley } 2825a1661176SMatthew Knepley 2826a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2827a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2828a1661176SMatthew Knepley ierr = MatSetOption(B,MAT_COLUMNS_UNSORTED);CHKERRQ(ierr); 2829a1661176SMatthew Knepley 2830a1661176SMatthew Knepley if (!v) { 2831a1661176SMatthew Knepley ierr = PetscFree(values);CHKERRQ(ierr); 2832a1661176SMatthew Knepley } 2833a1661176SMatthew Knepley PetscFunctionReturn(0); 2834a1661176SMatthew Knepley } 2835a1661176SMatthew Knepley EXTERN_C_END 2836a1661176SMatthew Knepley 28370bad9183SKris Buschelman /*MC 2838fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 28390bad9183SKris Buschelman based on compressed sparse row format. 28400bad9183SKris Buschelman 28410bad9183SKris Buschelman Options Database Keys: 28420bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 28430bad9183SKris Buschelman 28440bad9183SKris Buschelman Level: beginner 28450bad9183SKris Buschelman 2846f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 28470bad9183SKris Buschelman M*/ 28480bad9183SKris Buschelman 2849a6175056SHong Zhang EXTERN_C_BEGIN 28504a2ae208SSatish Balay #undef __FUNCT__ 28514a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ" 2852be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B) 2853273d9f13SBarry Smith { 2854273d9f13SBarry Smith Mat_SeqAIJ *b; 2855dfbe8321SBarry Smith PetscErrorCode ierr; 285638baddfdSBarry Smith PetscMPIInt size; 2857273d9f13SBarry Smith 2858273d9f13SBarry Smith PetscFunctionBegin; 2859273d9f13SBarry Smith ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr); 2860273d9f13SBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 2861273d9f13SBarry Smith 2862273d9f13SBarry Smith B->m = B->M = PetscMax(B->m,B->M); 2863273d9f13SBarry Smith B->n = B->N = PetscMax(B->n,B->N); 2864273d9f13SBarry Smith 2865b0a32e0cSBarry Smith ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr); 2866b0a32e0cSBarry Smith B->data = (void*)b; 2867549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 2868416022c9SBarry Smith B->factor = 0; 286990f02eecSBarry Smith B->mapping = 0; 2870416022c9SBarry Smith b->row = 0; 2871416022c9SBarry Smith b->col = 0; 287282bf6240SBarry Smith b->icol = 0; 2873b810aeb4SBarry Smith b->reallocs = 0; 287417ab2063SBarry Smith 28758a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr); 28768a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr); 2877a5ae1ecdSBarry Smith 2878f1e2ffcdSBarry Smith b->sorted = PETSC_FALSE; 287936db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 2880f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 2881416022c9SBarry Smith b->nonew = 0; 2882416022c9SBarry Smith b->diag = 0; 2883416022c9SBarry Smith b->solve_work = 0; 28842a1b7f2aSHong Zhang B->spptr = 0; 2885be6bf707SBarry Smith b->saved_values = 0; 2886d7f994e1SBarry Smith b->idiag = 0; 2887d7f994e1SBarry Smith b->ssor = 0; 2888f1e2ffcdSBarry Smith b->keepzeroedrows = PETSC_FALSE; 2889a30b2313SHong Zhang b->xtoy = 0; 2890a30b2313SHong Zhang b->XtoY = 0; 289173e7a558SHong Zhang b->compressedrow.use = PETSC_FALSE; 2892d487561eSHong Zhang b->compressedrow.nrows = B->m; 2893d487561eSHong Zhang b->compressedrow.i = PETSC_NULL; 2894d487561eSHong Zhang b->compressedrow.rindex = PETSC_NULL; 2895d487561eSHong Zhang b->compressedrow.checked = PETSC_FALSE; 289688e51ccdSHong Zhang B->same_nonzero = PETSC_FALSE; 289717ab2063SBarry Smith 289835d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 289935d8aa7fSBarry Smith 2900f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C", 2901bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 2902bc4b532fSSatish Balay MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 2903f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 2904be6bf707SBarry Smith "MatStoreValues_SeqAIJ", 2905bc4b532fSSatish Balay MatStoreValues_SeqAIJ);CHKERRQ(ierr); 2906f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 2907be6bf707SBarry Smith "MatRetrieveValues_SeqAIJ", 2908bc4b532fSSatish Balay MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 2909a6175056SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C", 2910a6175056SHong Zhang "MatConvert_SeqAIJ_SeqSBAIJ", 2911a6175056SHong Zhang MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 291285fc7724SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C", 291385fc7724SBarry Smith "MatConvert_SeqAIJ_SeqBAIJ", 291485fc7724SBarry Smith MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 29155fbd3699SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 29165fbd3699SBarry Smith "MatIsTranspose_SeqAIJ", 29175fbd3699SBarry Smith MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 2918a23d5eceSKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C", 2919a23d5eceSKris Buschelman "MatSeqAIJSetPreallocation_SeqAIJ", 2920a23d5eceSKris Buschelman MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 2921a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C", 2922a1661176SMatthew Knepley "MatSeqAIJSetPreallocationCSR_SeqAIJ", 2923a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 292405b94e36SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C", 292505b94e36SKris Buschelman "MatReorderForNonzeroDiagonal_SeqAIJ", 292605b94e36SKris Buschelman MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 29274846f1f5SKris Buschelman ierr = MatCreate_Inode(B);CHKERRQ(ierr); 29283a40ed3dSBarry Smith PetscFunctionReturn(0); 292917ab2063SBarry Smith } 2930273d9f13SBarry Smith EXTERN_C_END 293117ab2063SBarry Smith 29324a2ae208SSatish Balay #undef __FUNCT__ 29334a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ" 2934dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 293517ab2063SBarry Smith { 2936416022c9SBarry Smith Mat C; 2937416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 29386849ba73SBarry Smith PetscErrorCode ierr; 293997f1f81fSBarry Smith PetscInt i,m = A->m; 294017ab2063SBarry Smith 29413a40ed3dSBarry Smith PetscFunctionBegin; 29424043dd9cSLois Curfman McInnes *B = 0; 2943f69a0ea3SMatthew Knepley ierr = MatCreate(A->comm,&C);CHKERRQ(ierr); 2944f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,A->m,A->n,A->m,A->n);CHKERRQ(ierr); 2945be5d1d56SKris Buschelman ierr = MatSetType(C,A->type_name);CHKERRQ(ierr); 29461d5dac46SHong Zhang ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 29471d5dac46SHong Zhang 2948273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 2949273d9f13SBarry Smith 2950416022c9SBarry Smith C->factor = A->factor; 29516ad4291fSHong Zhang 2952416022c9SBarry Smith c->row = 0; 2953416022c9SBarry Smith c->col = 0; 295482bf6240SBarry Smith c->icol = 0; 29556ad4291fSHong Zhang c->reallocs = 0; 295617ab2063SBarry Smith 29576ad4291fSHong Zhang C->assembled = PETSC_TRUE; 295817ab2063SBarry Smith 295933b91e9fSSatish Balay ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr); 296017ab2063SBarry Smith for (i=0; i<m; i++) { 2961416022c9SBarry Smith c->imax[i] = a->imax[i]; 2962416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 296317ab2063SBarry Smith } 296417ab2063SBarry Smith 296517ab2063SBarry Smith /* allocate the matrix space */ 2966a96a251dSBarry Smith ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr); 2967f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 296897f1f81fSBarry Smith ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 296917ab2063SBarry Smith if (m > 0) { 297097f1f81fSBarry Smith ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr); 2971be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 2972bfeeae90SHong Zhang ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 2973be6bf707SBarry Smith } else { 2974bfeeae90SHong Zhang ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 297517ab2063SBarry Smith } 297608480c60SBarry Smith } 297717ab2063SBarry Smith 2978416022c9SBarry Smith c->sorted = a->sorted; 29796ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 2980416022c9SBarry Smith c->roworiented = a->roworiented; 2981416022c9SBarry Smith c->nonew = a->nonew; 2982416022c9SBarry Smith if (a->diag) { 298397f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr); 298452e6d16bSBarry Smith ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 298517ab2063SBarry Smith for (i=0; i<m; i++) { 2986416022c9SBarry Smith c->diag[i] = a->diag[i]; 298717ab2063SBarry Smith } 29883a40ed3dSBarry Smith } else c->diag = 0; 29896ad4291fSHong Zhang c->solve_work = 0; 29906ad4291fSHong Zhang c->saved_values = 0; 29916ad4291fSHong Zhang c->idiag = 0; 29926ad4291fSHong Zhang c->ssor = 0; 29936ad4291fSHong Zhang c->keepzeroedrows = a->keepzeroedrows; 29946ad4291fSHong Zhang c->freedata = PETSC_TRUE; 29956ad4291fSHong Zhang c->xtoy = 0; 29966ad4291fSHong Zhang c->XtoY = 0; 29976ad4291fSHong Zhang 2998416022c9SBarry Smith c->nz = a->nz; 2999416022c9SBarry Smith c->maxnz = a->maxnz; 3000273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 3001754ec7b1SSatish Balay 30026ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 30036ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 30046ad4291fSHong Zhang c->compressedrow.checked = a->compressedrow.checked; 30056ad4291fSHong Zhang if ( a->compressedrow.checked && a->compressedrow.use){ 30066ad4291fSHong Zhang i = a->compressedrow.nrows; 30076ad4291fSHong Zhang ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr); 30086ad4291fSHong Zhang c->compressedrow.rindex = c->compressedrow.i + i + 1; 30096ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr); 30106ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr); 301127ea64f8SHong Zhang } else { 301227ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 301327ea64f8SHong Zhang c->compressedrow.i = PETSC_NULL; 301427ea64f8SHong Zhang c->compressedrow.rindex = PETSC_NULL; 30156ad4291fSHong Zhang } 301688e51ccdSHong Zhang C->same_nonzero = A->same_nonzero; 30176ad4291fSHong Zhang 30184846f1f5SKris Buschelman ierr = MatDuplicate_Inode(A,cpvalues,&C);CHKERRQ(ierr); 30194846f1f5SKris Buschelman 3020416022c9SBarry Smith *B = C; 3021b0a32e0cSBarry Smith ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr); 30223a40ed3dSBarry Smith PetscFunctionReturn(0); 302317ab2063SBarry Smith } 302417ab2063SBarry Smith 30254a2ae208SSatish Balay #undef __FUNCT__ 30264a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ" 3027f69a0ea3SMatthew Knepley PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, MatType type,Mat *A) 302817ab2063SBarry Smith { 3029416022c9SBarry Smith Mat_SeqAIJ *a; 3030416022c9SBarry Smith Mat B; 30316849ba73SBarry Smith PetscErrorCode ierr; 30323c601197SSatish Balay PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N; 303338baddfdSBarry Smith int fd; 303438baddfdSBarry Smith PetscMPIInt size; 3035bcd2baecSBarry Smith MPI_Comm comm; 303617ab2063SBarry Smith 30373a40ed3dSBarry Smith PetscFunctionBegin; 3038e864ced6SBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 3039d132466eSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 304029bbc08cSBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor"); 3041b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 30420752156aSBarry Smith ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 3043552e946dSBarry Smith if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 304417ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 304517ab2063SBarry Smith 3046d64ed03dSBarry Smith if (nz < 0) { 304729bbc08cSBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 3048d64ed03dSBarry Smith } 3049d64ed03dSBarry Smith 305017ab2063SBarry Smith /* read in row lengths */ 305197f1f81fSBarry Smith ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 30520752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 305317ab2063SBarry Smith 30543c601197SSatish Balay /* check if sum of rowlengths is same as nz */ 30553c601197SSatish Balay for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 30563c601197SSatish Balay if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum); 30573c601197SSatish Balay 305817ab2063SBarry Smith /* create our matrix */ 3059f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B);CHKERRQ(ierr); 3060f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 3061b3a1e11cSKris Buschelman ierr = MatSetType(B,type);CHKERRQ(ierr); 3062ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr); 3063416022c9SBarry Smith a = (Mat_SeqAIJ*)B->data; 306417ab2063SBarry Smith 306517ab2063SBarry Smith /* read in column indices and adjust for Fortran indexing*/ 30660752156aSBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 306717ab2063SBarry Smith 306817ab2063SBarry Smith /* read in nonzero values */ 30690752156aSBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 307017ab2063SBarry Smith 307117ab2063SBarry Smith /* set matrix "i" values */ 3072efb16452SHong Zhang a->i[0] = 0; 307317ab2063SBarry Smith for (i=1; i<= M; i++) { 3074416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 3075416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 307617ab2063SBarry Smith } 3077606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 307817ab2063SBarry Smith 30796d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 30806d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3081b3a1e11cSKris Buschelman *A = B; 30823a40ed3dSBarry Smith PetscFunctionReturn(0); 308317ab2063SBarry Smith } 308417ab2063SBarry Smith 30854a2ae208SSatish Balay #undef __FUNCT__ 3086b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ" 3087dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg) 30887264ac53SSatish Balay { 30897264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data; 3090dfbe8321SBarry Smith PetscErrorCode ierr; 30917264ac53SSatish Balay 30923a40ed3dSBarry Smith PetscFunctionBegin; 3093bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 3094bfeeae90SHong Zhang if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)) { 3095ca44d042SBarry Smith *flg = PETSC_FALSE; 3096ca44d042SBarry Smith PetscFunctionReturn(0); 3097bcd2baecSBarry Smith } 30987264ac53SSatish Balay 30997264ac53SSatish Balay /* if the a->i are the same */ 310097f1f81fSBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3101abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 31027264ac53SSatish Balay 31037264ac53SSatish Balay /* if a->j are the same */ 310497f1f81fSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3105abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 3106bcd2baecSBarry Smith 3107bcd2baecSBarry Smith /* if a->a are the same */ 310887828ca2SBarry Smith ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 31090f5bd95cSBarry Smith 31103a40ed3dSBarry Smith PetscFunctionReturn(0); 31117264ac53SSatish Balay 31127264ac53SSatish Balay } 311336db0b34SBarry Smith 31144a2ae208SSatish Balay #undef __FUNCT__ 31154a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays" 311605869f15SSatish Balay /*@ 311736db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 311836db0b34SBarry Smith provided by the user. 311936db0b34SBarry Smith 312036db0b34SBarry Smith Coolective on MPI_Comm 312136db0b34SBarry Smith 312236db0b34SBarry Smith Input Parameters: 312336db0b34SBarry Smith + comm - must be an MPI communicator of size 1 312436db0b34SBarry Smith . m - number of rows 312536db0b34SBarry Smith . n - number of columns 312636db0b34SBarry Smith . i - row indices 312736db0b34SBarry Smith . j - column indices 312836db0b34SBarry Smith - a - matrix values 312936db0b34SBarry Smith 313036db0b34SBarry Smith Output Parameter: 313136db0b34SBarry Smith . mat - the matrix 313236db0b34SBarry Smith 313336db0b34SBarry Smith Level: intermediate 313436db0b34SBarry Smith 313536db0b34SBarry Smith Notes: 31360551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 313736db0b34SBarry Smith once the matrix is destroyed 313836db0b34SBarry Smith 313936db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 314036db0b34SBarry Smith 3141bfeeae90SHong Zhang The i and j indices are 0 based 314236db0b34SBarry Smith 314336db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ() 314436db0b34SBarry Smith 314536db0b34SBarry Smith @*/ 3146be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat) 314736db0b34SBarry Smith { 3148dfbe8321SBarry Smith PetscErrorCode ierr; 314997f1f81fSBarry Smith PetscInt ii; 315036db0b34SBarry Smith Mat_SeqAIJ *aij; 315136db0b34SBarry Smith 315236db0b34SBarry Smith PetscFunctionBegin; 3153a96a251dSBarry Smith if (i[0]) { 3154634064b4SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 315536db0b34SBarry Smith } 3156f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3157f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 3158ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 3159ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 3160ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 3161ab93d7beSBarry Smith ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr); 3162ab93d7beSBarry Smith 316336db0b34SBarry Smith aij->i = i; 316436db0b34SBarry Smith aij->j = j; 316536db0b34SBarry Smith aij->a = a; 316636db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 316736db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 316836db0b34SBarry Smith aij->freedata = PETSC_FALSE; 316936db0b34SBarry Smith 317036db0b34SBarry Smith for (ii=0; ii<m; ii++) { 317136db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 31722515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 317379a5c55eSBarry 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]); 317436db0b34SBarry Smith #endif 317536db0b34SBarry Smith } 31762515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 317736db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 317879a5c55eSBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]); 317979a5c55eSBarry Smith if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]); 318036db0b34SBarry Smith } 318136db0b34SBarry Smith #endif 318236db0b34SBarry Smith 3183b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3184b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 318536db0b34SBarry Smith PetscFunctionReturn(0); 318636db0b34SBarry Smith } 318736db0b34SBarry Smith 3188cc8ba8e1SBarry Smith #undef __FUNCT__ 3189ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ" 3190dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring) 3191cc8ba8e1SBarry Smith { 3192dfbe8321SBarry Smith PetscErrorCode ierr; 3193cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 319436db0b34SBarry Smith 3195cc8ba8e1SBarry Smith PetscFunctionBegin; 319612c595b3SBarry Smith if (coloring->ctype == IS_COLORING_LOCAL) { 3197cc8ba8e1SBarry Smith ierr = ISColoringReference(coloring);CHKERRQ(ierr); 3198cc8ba8e1SBarry Smith a->coloring = coloring; 319912c595b3SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 320097f1f81fSBarry Smith PetscInt i,*larray; 320112c595b3SBarry Smith ISColoring ocoloring; 320208b6dcc0SBarry Smith ISColoringValue *colors; 320312c595b3SBarry Smith 320412c595b3SBarry Smith /* set coloring for diagonal portion */ 320597f1f81fSBarry Smith ierr = PetscMalloc((A->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 320612c595b3SBarry Smith for (i=0; i<A->n; i++) { 320712c595b3SBarry Smith larray[i] = i; 320812c595b3SBarry Smith } 320912c595b3SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 321008b6dcc0SBarry Smith ierr = PetscMalloc((A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 321112c595b3SBarry Smith for (i=0; i<A->n; i++) { 321212c595b3SBarry Smith colors[i] = coloring->colors[larray[i]]; 321312c595b3SBarry Smith } 321412c595b3SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 321512c595b3SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,A->n,colors,&ocoloring);CHKERRQ(ierr); 321612c595b3SBarry Smith a->coloring = ocoloring; 321712c595b3SBarry Smith } 3218cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3219cc8ba8e1SBarry Smith } 3220cc8ba8e1SBarry Smith 3221dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 3222ee4f033dSBarry Smith EXTERN_C_BEGIN 322329c1e371SBarry Smith #include "adic/ad_utils.h" 3224ee4f033dSBarry Smith EXTERN_C_END 3225cc8ba8e1SBarry Smith 3226cc8ba8e1SBarry Smith #undef __FUNCT__ 3227ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 3228dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 3229cc8ba8e1SBarry Smith { 3230cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 323197f1f81fSBarry Smith PetscInt m = A->m,*ii = a->i,*jj = a->j,nz,i,j,nlen; 32324440f671SBarry Smith PetscScalar *v = a->a,*values = ((PetscScalar*)advalues)+1; 323308b6dcc0SBarry Smith ISColoringValue *color; 3234cc8ba8e1SBarry Smith 3235cc8ba8e1SBarry Smith PetscFunctionBegin; 3236e005ede5SBarry Smith if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 32374440f671SBarry Smith nlen = PetscADGetDerivTypeSize()/sizeof(PetscScalar); 3238cc8ba8e1SBarry Smith color = a->coloring->colors; 3239cc8ba8e1SBarry Smith /* loop over rows */ 3240cc8ba8e1SBarry Smith for (i=0; i<m; i++) { 3241cc8ba8e1SBarry Smith nz = ii[i+1] - ii[i]; 3242cc8ba8e1SBarry Smith /* loop over columns putting computed value into matrix */ 3243cc8ba8e1SBarry Smith for (j=0; j<nz; j++) { 3244cc8ba8e1SBarry Smith *v++ = values[color[*jj++]]; 3245cc8ba8e1SBarry Smith } 32464440f671SBarry Smith values += nlen; /* jump to next row of derivatives */ 3247ee4f033dSBarry Smith } 3248ee4f033dSBarry Smith PetscFunctionReturn(0); 3249ee4f033dSBarry Smith } 3250ee4f033dSBarry Smith #endif 3251ee4f033dSBarry Smith 3252ee4f033dSBarry Smith #undef __FUNCT__ 3253ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ" 325497f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues) 3255ee4f033dSBarry Smith { 3256ee4f033dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 325797f1f81fSBarry Smith PetscInt m = A->m,*ii = a->i,*jj = a->j,nz,i,j; 325887828ca2SBarry Smith PetscScalar *v = a->a,*values = (PetscScalar *)advalues; 325908b6dcc0SBarry Smith ISColoringValue *color; 3260ee4f033dSBarry Smith 3261ee4f033dSBarry Smith PetscFunctionBegin; 3262e005ede5SBarry Smith if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 3263ee4f033dSBarry Smith color = a->coloring->colors; 3264ee4f033dSBarry Smith /* loop over rows */ 3265ee4f033dSBarry Smith for (i=0; i<m; i++) { 3266ee4f033dSBarry Smith nz = ii[i+1] - ii[i]; 3267ee4f033dSBarry Smith /* loop over columns putting computed value into matrix */ 3268ee4f033dSBarry Smith for (j=0; j<nz; j++) { 3269ee4f033dSBarry Smith *v++ = values[color[*jj++]]; 3270ee4f033dSBarry Smith } 3271ee4f033dSBarry Smith values += nl; /* jump to next row of derivatives */ 3272cc8ba8e1SBarry Smith } 3273cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3274cc8ba8e1SBarry Smith } 327536db0b34SBarry Smith 327681824310SBarry Smith /* 327781824310SBarry Smith Special version for direct calls from Fortran 327881824310SBarry Smith */ 327981824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 328081824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 328181824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 328281824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 328381824310SBarry Smith #endif 328481824310SBarry Smith 328581824310SBarry Smith /* Change these macros so can be used in void function */ 328681824310SBarry Smith #undef CHKERRQ 328781824310SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(A->comm,ierr) 328881824310SBarry Smith #undef SETERRQ2 328981824310SBarry Smith #define SETERRQ2(ierr,b,c,d) CHKERRABORT(A->comm,ierr) 329081824310SBarry Smith 329181824310SBarry Smith EXTERN_C_BEGIN 329281824310SBarry Smith #undef __FUNCT__ 329381824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_" 329481824310SBarry Smith void PETSCMAT_DLLEXPORT matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis) 329581824310SBarry Smith { 329681824310SBarry Smith Mat A = *AA; 329781824310SBarry Smith PetscInt m = *mm, n = *nn; 329881824310SBarry Smith InsertMode is = *isis; 329981824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 330081824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 330181824310SBarry Smith PetscInt *imax,*ai,*ailen; 330281824310SBarry Smith PetscErrorCode ierr; 330381824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 330481824310SBarry Smith PetscScalar *ap,value,*aa; 330581824310SBarry Smith PetscTruth ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE); 330681824310SBarry Smith PetscTruth roworiented = a->roworiented; 330781824310SBarry Smith 330881824310SBarry Smith PetscFunctionBegin; 330981824310SBarry Smith MatPreallocated(A); 331081824310SBarry Smith imax = a->imax; 331181824310SBarry Smith ai = a->i; 331281824310SBarry Smith ailen = a->ilen; 331381824310SBarry Smith aj = a->j; 331481824310SBarry Smith aa = a->a; 331581824310SBarry Smith 331681824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 331781824310SBarry Smith row = im[k]; 331881824310SBarry Smith if (row < 0) continue; 331981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 332081824310SBarry Smith if (row >= A->m) SETERRABORT(A->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 332181824310SBarry Smith #endif 332281824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 332381824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 332481824310SBarry Smith low = 0; 332581824310SBarry Smith high = nrow; 332681824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 332781824310SBarry Smith if (in[l] < 0) continue; 332881824310SBarry Smith #if defined(PETSC_USE_DEBUG) 332981824310SBarry Smith if (in[l] >= A->n) SETERRABORT(A->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 333081824310SBarry Smith #endif 333181824310SBarry Smith col = in[l]; 333281824310SBarry Smith if (roworiented) { 333381824310SBarry Smith value = v[l + k*n]; 333481824310SBarry Smith } else { 333581824310SBarry Smith value = v[k + l*m]; 333681824310SBarry Smith } 333781824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 333881824310SBarry Smith 333981824310SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 334081824310SBarry Smith lastcol = col; 334181824310SBarry Smith while (high-low > 5) { 334281824310SBarry Smith t = (low+high)/2; 334381824310SBarry Smith if (rp[t] > col) high = t; 334481824310SBarry Smith else low = t; 334581824310SBarry Smith } 334681824310SBarry Smith for (i=low; i<high; i++) { 334781824310SBarry Smith if (rp[i] > col) break; 334881824310SBarry Smith if (rp[i] == col) { 334981824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 335081824310SBarry Smith else ap[i] = value; 335181824310SBarry Smith goto noinsert; 335281824310SBarry Smith } 335381824310SBarry Smith } 335481824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 335581824310SBarry Smith if (nonew == 1) goto noinsert; 335681824310SBarry Smith if (nonew == -1) SETERRABORT(A->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 335781824310SBarry Smith MatSeqXAIJReallocateAIJ(a,1,nrow,row,col,rmax,aa,ai,aj,A->m,rp,ap,imax,nonew); 335881824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 335981824310SBarry Smith /* shift up all the later entries in this row */ 336081824310SBarry Smith for (ii=N; ii>=i; ii--) { 336181824310SBarry Smith rp[ii+1] = rp[ii]; 336281824310SBarry Smith ap[ii+1] = ap[ii]; 336381824310SBarry Smith } 336481824310SBarry Smith rp[i] = col; 336581824310SBarry Smith ap[i] = value; 336681824310SBarry Smith noinsert:; 336781824310SBarry Smith low = i + 1; 336881824310SBarry Smith } 336981824310SBarry Smith ailen[row] = nrow; 337081824310SBarry Smith } 337181824310SBarry Smith A->same_nonzero = PETSC_FALSE; 337281824310SBarry Smith PetscFunctionReturnVoid(); 337381824310SBarry Smith } 337481824310SBarry Smith EXTERN_C_END 3375