1b377110cSBarry Smith 2d5d45c9bSBarry Smith /* 33369ce9aSBarry Smith Defines the basic matrix operations for the AIJ (compressed row) 4d5d45c9bSBarry Smith matrix storage format. 5d5d45c9bSBarry Smith */ 63369ce9aSBarry Smith 77c4f633dSBarry Smith 8c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h> /*I "petscmat.h" I*/ 9c6db04a5SJed Brown #include <petscblaslapack.h> 10c6db04a5SJed Brown #include <petscbt.h> 11d441b888SJed Brown #include <../src/mat/blocktranspose.h> 120716a85fSBarry Smith 130716a85fSBarry Smith #undef __FUNCT__ 140716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ" 150716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms) 160716a85fSBarry Smith { 170716a85fSBarry Smith PetscErrorCode ierr; 180716a85fSBarry Smith PetscInt i,m,n; 190716a85fSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data; 200716a85fSBarry Smith 210716a85fSBarry Smith PetscFunctionBegin; 220716a85fSBarry Smith ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr); 230716a85fSBarry Smith ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr); 240716a85fSBarry Smith if (type == NORM_2) { 250716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 260716a85fSBarry Smith norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]); 270716a85fSBarry Smith } 280716a85fSBarry Smith } else if (type == NORM_1) { 290716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 300716a85fSBarry Smith norms[aij->j[i]] += PetscAbsScalar(aij->a[i]); 310716a85fSBarry Smith } 320716a85fSBarry Smith } else if (type == NORM_INFINITY) { 330716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 340716a85fSBarry Smith norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]); 350716a85fSBarry Smith } 360716a85fSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType"); 370716a85fSBarry Smith 380716a85fSBarry Smith if (type == NORM_2) { 398f1a2a5eSBarry Smith for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]); 400716a85fSBarry Smith } 410716a85fSBarry Smith PetscFunctionReturn(0); 420716a85fSBarry Smith } 430716a85fSBarry Smith 444a2ae208SSatish Balay #undef __FUNCT__ 45*f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ_Private" 46*f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows) 476ce1633cSBarry Smith { 486ce1633cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 496ce1633cSBarry Smith const MatScalar *aa = a->a; 506ce1633cSBarry Smith PetscInt i,m=A->rmap->n,cnt = 0; 516ce1633cSBarry Smith const PetscInt *jj = a->j,*diag; 526ce1633cSBarry Smith PetscInt *rows; 536ce1633cSBarry Smith PetscErrorCode ierr; 546ce1633cSBarry Smith 556ce1633cSBarry Smith PetscFunctionBegin; 566ce1633cSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 576ce1633cSBarry Smith diag = a->diag; 586ce1633cSBarry Smith for (i=0; i<m; i++) { 596ce1633cSBarry Smith if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) { 606ce1633cSBarry Smith cnt++; 616ce1633cSBarry Smith } 626ce1633cSBarry Smith } 636ce1633cSBarry Smith ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr); 646ce1633cSBarry Smith cnt = 0; 656ce1633cSBarry Smith for (i=0; i<m; i++) { 666ce1633cSBarry Smith if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) { 676ce1633cSBarry Smith rows[cnt++] = i; 686ce1633cSBarry Smith } 696ce1633cSBarry Smith } 70*f1f41ecbSJed Brown *nrows = cnt; 71*f1f41ecbSJed Brown *zrows = rows; 72*f1f41ecbSJed Brown PetscFunctionReturn(0); 73*f1f41ecbSJed Brown } 74*f1f41ecbSJed Brown 75*f1f41ecbSJed Brown #undef __FUNCT__ 76*f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ" 77*f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows) 78*f1f41ecbSJed Brown { 79*f1f41ecbSJed Brown PetscInt nrows,*rows; 80*f1f41ecbSJed Brown PetscErrorCode ierr; 81*f1f41ecbSJed Brown 82*f1f41ecbSJed Brown PetscFunctionBegin; 83*f1f41ecbSJed Brown *zrows = PETSC_NULL; 84*f1f41ecbSJed Brown ierr = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr); 85*f1f41ecbSJed Brown ierr = ISCreateGeneral(((PetscObject)A)->comm,nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr); 866ce1633cSBarry Smith PetscFunctionReturn(0); 876ce1633cSBarry Smith } 886ce1633cSBarry Smith 896ce1633cSBarry Smith #undef __FUNCT__ 90b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ" 91b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows) 92b3a44c85SBarry Smith { 93b3a44c85SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 94b3a44c85SBarry Smith const MatScalar *aa; 95b3a44c85SBarry Smith PetscInt m=A->rmap->n,cnt = 0; 96b3a44c85SBarry Smith const PetscInt *ii; 97b3a44c85SBarry Smith PetscInt n,i,j,*rows; 98b3a44c85SBarry Smith PetscErrorCode ierr; 99b3a44c85SBarry Smith 100b3a44c85SBarry Smith PetscFunctionBegin; 101b3a44c85SBarry Smith *keptrows = 0; 102b3a44c85SBarry Smith ii = a->i; 103b3a44c85SBarry Smith for (i=0; i<m; i++) { 104b3a44c85SBarry Smith n = ii[i+1] - ii[i]; 105b3a44c85SBarry Smith if (!n) { 106b3a44c85SBarry Smith cnt++; 107b3a44c85SBarry Smith goto ok1; 108b3a44c85SBarry Smith } 109b3a44c85SBarry Smith aa = a->a + ii[i]; 110b3a44c85SBarry Smith for (j=0; j<n; j++) { 111b3a44c85SBarry Smith if (aa[j] != 0.0) goto ok1; 112b3a44c85SBarry Smith } 113b3a44c85SBarry Smith cnt++; 114b3a44c85SBarry Smith ok1:; 115b3a44c85SBarry Smith } 116b3a44c85SBarry Smith if (!cnt) PetscFunctionReturn(0); 117b3a44c85SBarry Smith ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr); 118b3a44c85SBarry Smith cnt = 0; 119b3a44c85SBarry Smith for (i=0; i<m; i++) { 120b3a44c85SBarry Smith n = ii[i+1] - ii[i]; 121b3a44c85SBarry Smith if (!n) continue; 122b3a44c85SBarry Smith aa = a->a + ii[i]; 123b3a44c85SBarry Smith for (j=0; j<n; j++) { 124b3a44c85SBarry Smith if (aa[j] != 0.0) { 125b3a44c85SBarry Smith rows[cnt++] = i; 126b3a44c85SBarry Smith break; 127b3a44c85SBarry Smith } 128b3a44c85SBarry Smith } 129b3a44c85SBarry Smith } 130b3a44c85SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr); 131b3a44c85SBarry Smith PetscFunctionReturn(0); 132b3a44c85SBarry Smith } 133b3a44c85SBarry Smith 134b3a44c85SBarry Smith #undef __FUNCT__ 13579299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ" 1367087cfbeSBarry Smith PetscErrorCode MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is) 13779299369SBarry Smith { 13879299369SBarry Smith PetscErrorCode ierr; 13979299369SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) Y->data; 140d0f46423SBarry Smith PetscInt i,*diag, m = Y->rmap->n; 14154f21887SBarry Smith MatScalar *aa = aij->a; 14254f21887SBarry Smith PetscScalar *v; 143ace3abfcSBarry Smith PetscBool missing; 14479299369SBarry Smith 14579299369SBarry Smith PetscFunctionBegin; 14609f38230SBarry Smith if (Y->assembled) { 14709f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr); 14809f38230SBarry Smith if (!missing) { 14979299369SBarry Smith diag = aij->diag; 15079299369SBarry Smith ierr = VecGetArray(D,&v);CHKERRQ(ierr); 15179299369SBarry Smith if (is == INSERT_VALUES) { 15279299369SBarry Smith for (i=0; i<m; i++) { 15379299369SBarry Smith aa[diag[i]] = v[i]; 15479299369SBarry Smith } 15579299369SBarry Smith } else { 15679299369SBarry Smith for (i=0; i<m; i++) { 15779299369SBarry Smith aa[diag[i]] += v[i]; 15879299369SBarry Smith } 15979299369SBarry Smith } 16079299369SBarry Smith ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 16179299369SBarry Smith PetscFunctionReturn(0); 16279299369SBarry Smith } 16386c113feSBarry Smith aij->idiagvalid = PETSC_FALSE; 16486c113feSBarry Smith aij->ibdiagvalid = PETSC_FALSE; 16509f38230SBarry Smith } 16609f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 16709f38230SBarry Smith PetscFunctionReturn(0); 16809f38230SBarry Smith } 16979299369SBarry Smith 17079299369SBarry Smith #undef __FUNCT__ 1714a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ" 172ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool *done) 17317ab2063SBarry Smith { 174416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 175dfbe8321SBarry Smith PetscErrorCode ierr; 17697f1f81fSBarry Smith PetscInt i,ishift; 17717ab2063SBarry Smith 1783a40ed3dSBarry Smith PetscFunctionBegin; 179d0f46423SBarry Smith *m = A->rmap->n; 1803a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 181bfeeae90SHong Zhang ishift = 0; 18253e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 183d0f46423SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 184bfeeae90SHong Zhang } else if (oshift == 1) { 185d0f46423SBarry Smith PetscInt nz = a->i[A->rmap->n]; 1863b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 187d0f46423SBarry Smith ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr); 188d0f46423SBarry Smith for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1; 189ecc77c7aSBarry Smith if (ja) { 19097f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr); 1913b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1; 192ecc77c7aSBarry Smith } 1936945ee14SBarry Smith } else { 194ecc77c7aSBarry Smith *ia = a->i; 195ecc77c7aSBarry Smith if (ja) *ja = a->j; 196a2ce50c7SBarry Smith } 1973a40ed3dSBarry Smith PetscFunctionReturn(0); 198a2744918SBarry Smith } 199a2744918SBarry Smith 2004a2ae208SSatish Balay #undef __FUNCT__ 2014a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ" 202ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool *done) 2036945ee14SBarry Smith { 204dfbe8321SBarry Smith PetscErrorCode ierr; 2056945ee14SBarry Smith 2063a40ed3dSBarry Smith PetscFunctionBegin; 2073a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 208bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 209606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 210ecc77c7aSBarry Smith if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);} 211bcd2baecSBarry Smith } 2123a40ed3dSBarry Smith PetscFunctionReturn(0); 21317ab2063SBarry Smith } 21417ab2063SBarry Smith 2154a2ae208SSatish Balay #undef __FUNCT__ 2164a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ" 217ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool *done) 2183b2fbd54SBarry Smith { 2193b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 220dfbe8321SBarry Smith PetscErrorCode ierr; 221d0f46423SBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 22297f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 2233b2fbd54SBarry Smith 2243a40ed3dSBarry Smith PetscFunctionBegin; 225899cda47SBarry Smith *nn = n; 2263a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 2273b2fbd54SBarry Smith if (symmetric) { 228d0f46423SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr); 2293b2fbd54SBarry Smith } else { 23097f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr); 23197f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 23297f1f81fSBarry Smith ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr); 23397f1f81fSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr); 2343b2fbd54SBarry Smith jj = a->j; 2353b2fbd54SBarry Smith for (i=0; i<nz; i++) { 236bfeeae90SHong Zhang collengths[jj[i]]++; 2373b2fbd54SBarry Smith } 2383b2fbd54SBarry Smith cia[0] = oshift; 2393b2fbd54SBarry Smith for (i=0; i<n; i++) { 2403b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 2413b2fbd54SBarry Smith } 24297f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 2433b2fbd54SBarry Smith jj = a->j; 244a93ec695SBarry Smith for (row=0; row<m; row++) { 245a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 246a93ec695SBarry Smith for (i=0; i<mr; i++) { 247bfeeae90SHong Zhang col = *jj++; 2483b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 2493b2fbd54SBarry Smith } 2503b2fbd54SBarry Smith } 251606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 2523b2fbd54SBarry Smith *ia = cia; *ja = cja; 2533b2fbd54SBarry Smith } 2543a40ed3dSBarry Smith PetscFunctionReturn(0); 2553b2fbd54SBarry Smith } 2563b2fbd54SBarry Smith 2574a2ae208SSatish Balay #undef __FUNCT__ 2584a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ" 259ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool *done) 2603b2fbd54SBarry Smith { 261dfbe8321SBarry Smith PetscErrorCode ierr; 262606d414cSSatish Balay 2633a40ed3dSBarry Smith PetscFunctionBegin; 2643a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 2653b2fbd54SBarry Smith 266606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 267606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 2683b2fbd54SBarry Smith 2693a40ed3dSBarry Smith PetscFunctionReturn(0); 2703b2fbd54SBarry Smith } 2713b2fbd54SBarry Smith 27287d4246cSBarry Smith #undef __FUNCT__ 27387d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ" 27487d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 27587d4246cSBarry Smith { 27687d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 27787d4246cSBarry Smith PetscInt *ai = a->i; 27887d4246cSBarry Smith PetscErrorCode ierr; 27987d4246cSBarry Smith 28087d4246cSBarry Smith PetscFunctionBegin; 28187d4246cSBarry Smith ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr); 28287d4246cSBarry Smith PetscFunctionReturn(0); 28387d4246cSBarry Smith } 28487d4246cSBarry Smith 2854a2ae208SSatish Balay #undef __FUNCT__ 2864a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ" 28797f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 28817ab2063SBarry Smith { 289416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 290e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 29197f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 2926849ba73SBarry Smith PetscErrorCode ierr; 293e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 29454f21887SBarry Smith MatScalar *ap,value,*aa = a->a; 295ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 296ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 29717ab2063SBarry Smith 2983a40ed3dSBarry Smith PetscFunctionBegin; 29971fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 30017ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 301416022c9SBarry Smith row = im[k]; 3025ef9f2a5SBarry Smith if (row < 0) continue; 3032515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 304e32f2f54SBarry Smith if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1); 3053b2fbd54SBarry Smith #endif 306bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 30717ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 308416022c9SBarry Smith low = 0; 309c71e6ed7SBarry Smith high = nrow; 31017ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 3115ef9f2a5SBarry Smith if (in[l] < 0) continue; 3122515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 313e32f2f54SBarry Smith if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1); 3143b2fbd54SBarry Smith #endif 315bfeeae90SHong Zhang col = in[l]; 31616371a99SBarry Smith if (v) { 3174b0e389bSBarry Smith if (roworiented) { 3185ef9f2a5SBarry Smith value = v[l + k*n]; 319bef8e0ddSBarry Smith } else { 3204b0e389bSBarry Smith value = v[k + l*m]; 3214b0e389bSBarry Smith } 32216371a99SBarry Smith } else { 32375567043SBarry Smith value = 0.; 32416371a99SBarry Smith } 325abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 32636db0b34SBarry Smith 3277cd84e04SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 328e2ee6c50SBarry Smith lastcol = col; 329416022c9SBarry Smith while (high-low > 5) { 330416022c9SBarry Smith t = (low+high)/2; 331416022c9SBarry Smith if (rp[t] > col) high = t; 332416022c9SBarry Smith else low = t; 33317ab2063SBarry Smith } 334416022c9SBarry Smith for (i=low; i<high; i++) { 33517ab2063SBarry Smith if (rp[i] > col) break; 33617ab2063SBarry Smith if (rp[i] == col) { 337416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 33817ab2063SBarry Smith else ap[i] = value; 339e44c0bd4SBarry Smith low = i + 1; 34017ab2063SBarry Smith goto noinsert; 34117ab2063SBarry Smith } 34217ab2063SBarry Smith } 343abc0a331SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 344c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 345e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 346fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 347c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 348416022c9SBarry Smith /* shift up all the later entries in this row */ 349416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 35017ab2063SBarry Smith rp[ii+1] = rp[ii]; 35117ab2063SBarry Smith ap[ii+1] = ap[ii]; 35217ab2063SBarry Smith } 35317ab2063SBarry Smith rp[i] = col; 35417ab2063SBarry Smith ap[i] = value; 355416022c9SBarry Smith low = i + 1; 356e44c0bd4SBarry Smith noinsert:; 35717ab2063SBarry Smith } 35817ab2063SBarry Smith ailen[row] = nrow; 35917ab2063SBarry Smith } 36088e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 3613a40ed3dSBarry Smith PetscFunctionReturn(0); 36217ab2063SBarry Smith } 36317ab2063SBarry Smith 36481824310SBarry Smith 3654a2ae208SSatish Balay #undef __FUNCT__ 3664a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ" 367a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 3687eb43aa7SLois Curfman McInnes { 3697eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 37097f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 37197f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 37254f21887SBarry Smith MatScalar *ap,*aa = a->a; 3737eb43aa7SLois Curfman McInnes 3743a40ed3dSBarry Smith PetscFunctionBegin; 3757eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 3767eb43aa7SLois Curfman McInnes row = im[k]; 377e32f2f54SBarry Smith if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */ 378e32f2f54SBarry Smith if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1); 379bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 3807eb43aa7SLois Curfman McInnes nrow = ailen[row]; 3817eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 382e32f2f54SBarry Smith if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */ 383e32f2f54SBarry Smith if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1); 384bfeeae90SHong Zhang col = in[l] ; 3857eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 3867eb43aa7SLois Curfman McInnes while (high-low > 5) { 3877eb43aa7SLois Curfman McInnes t = (low+high)/2; 3887eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 3897eb43aa7SLois Curfman McInnes else low = t; 3907eb43aa7SLois Curfman McInnes } 3917eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 3927eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 3937eb43aa7SLois Curfman McInnes if (rp[i] == col) { 394b49de8d1SLois Curfman McInnes *v++ = ap[i]; 3957eb43aa7SLois Curfman McInnes goto finished; 3967eb43aa7SLois Curfman McInnes } 3977eb43aa7SLois Curfman McInnes } 39897e567efSBarry Smith *v++ = 0.0; 3997eb43aa7SLois Curfman McInnes finished:; 4007eb43aa7SLois Curfman McInnes } 4017eb43aa7SLois Curfman McInnes } 4023a40ed3dSBarry Smith PetscFunctionReturn(0); 4037eb43aa7SLois Curfman McInnes } 4047eb43aa7SLois Curfman McInnes 40517ab2063SBarry Smith 4064a2ae208SSatish Balay #undef __FUNCT__ 4074a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary" 408dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 40917ab2063SBarry Smith { 410416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4116849ba73SBarry Smith PetscErrorCode ierr; 4126f69ff64SBarry Smith PetscInt i,*col_lens; 4136f69ff64SBarry Smith int fd; 41417ab2063SBarry Smith 4153a40ed3dSBarry Smith PetscFunctionBegin; 416b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 417d0f46423SBarry Smith ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr); 4180700a824SBarry Smith col_lens[0] = MAT_FILE_CLASSID; 419d0f46423SBarry Smith col_lens[1] = A->rmap->n; 420d0f46423SBarry Smith col_lens[2] = A->cmap->n; 421416022c9SBarry Smith col_lens[3] = a->nz; 422416022c9SBarry Smith 423416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 424d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 425416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 42617ab2063SBarry Smith } 427d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 428606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 429416022c9SBarry Smith 430416022c9SBarry Smith /* store column indices (zero start index) */ 4316f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 432416022c9SBarry Smith 433416022c9SBarry Smith /* store nonzero values */ 4346f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 4353a40ed3dSBarry Smith PetscFunctionReturn(0); 43617ab2063SBarry Smith } 437416022c9SBarry Smith 43809573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 439cd155464SBarry Smith 4404a2ae208SSatish Balay #undef __FUNCT__ 4414a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII" 442dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 443416022c9SBarry Smith { 444416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 445dfbe8321SBarry Smith PetscErrorCode ierr; 446d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,shift=0; 447e060cb09SBarry Smith const char *name; 448f3ef73ceSBarry Smith PetscViewerFormat format; 44917ab2063SBarry Smith 4503a40ed3dSBarry Smith PetscFunctionBegin; 451b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 45271c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 45397f1f81fSBarry Smith PetscInt nofinalvalue = 0; 454d0f46423SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) { 455d00d2cf4SBarry Smith nofinalvalue = 1; 456d00d2cf4SBarry Smith } 457d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 458d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr); 45977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 46077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 461b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 46217ab2063SBarry Smith 46317ab2063SBarry Smith for (i=0; i<m; i++) { 464416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 465aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 46677431f27SBarry 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); 46717ab2063SBarry Smith #else 46877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr); 46917ab2063SBarry Smith #endif 47017ab2063SBarry Smith } 47117ab2063SBarry Smith } 472d00d2cf4SBarry Smith if (nofinalvalue) { 473d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr); 474d00d2cf4SBarry Smith } 475317d6ea6SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 476fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 477d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 47868369a75SKris Buschelman } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) { 479cd155464SBarry Smith PetscFunctionReturn(0); 480fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 481d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 4827566de4bSShri Abhyankar ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr); 48344cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 48477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 48544cd7ae7SLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 486aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 48736db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 488a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 48936db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 490a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 49136db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 492a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 4936831982aSBarry Smith } 49444cd7ae7SLois Curfman McInnes #else 495a83599f4SBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);} 49644cd7ae7SLois Curfman McInnes #endif 49744cd7ae7SLois Curfman McInnes } 498b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 49944cd7ae7SLois Curfman McInnes } 500d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 501fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 50297f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 503d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 5047566de4bSShri Abhyankar ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr); 50597f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr); 506496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 507496be53dSLois Curfman McInnes sptr[i] = nzd+1; 508496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 509496be53dSLois Curfman McInnes if (a->j[j] >= i) { 510aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 51136db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 512496be53dSLois Curfman McInnes #else 513496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 514496be53dSLois Curfman McInnes #endif 515496be53dSLois Curfman McInnes } 516496be53dSLois Curfman McInnes } 517496be53dSLois Curfman McInnes } 5182e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 51977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 5202e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 52177431f27SBarry 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);} 52277431f27SBarry 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);} 52377431f27SBarry 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);} 52477431f27SBarry Smith else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);} 52577431f27SBarry Smith else if (i<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);} 52677431f27SBarry Smith else {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);} 527496be53dSLois Curfman McInnes } 528b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 529606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 530496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 531496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 53277431f27SBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);} 533496be53dSLois Curfman McInnes } 534b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 535496be53dSLois Curfman McInnes } 536b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 537496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 538496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 539496be53dSLois Curfman McInnes if (a->j[j] >= i) { 540aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 54136db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 542b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 5436831982aSBarry Smith } 544496be53dSLois Curfman McInnes #else 545b0a32e0cSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);} 546496be53dSLois Curfman McInnes #endif 547496be53dSLois Curfman McInnes } 548496be53dSLois Curfman McInnes } 549b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 550496be53dSLois Curfman McInnes } 551d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 552fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 55397f1f81fSBarry Smith PetscInt cnt = 0,jcnt; 55487828ca2SBarry Smith PetscScalar value; 55502594712SBarry Smith 556d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 5577566de4bSShri Abhyankar ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr); 55802594712SBarry Smith for (i=0; i<m; i++) { 55902594712SBarry Smith jcnt = 0; 560d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 561e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 56202594712SBarry Smith value = a->a[cnt++]; 563e24b481bSBarry Smith jcnt++; 56402594712SBarry Smith } else { 56502594712SBarry Smith value = 0.0; 56602594712SBarry Smith } 567aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 568b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr); 56902594712SBarry Smith #else 570b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr); 57102594712SBarry Smith #endif 57202594712SBarry Smith } 573b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 57402594712SBarry Smith } 575d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 5763c215bfdSMatthew Knepley } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) { 577d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 5787566de4bSShri Abhyankar ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr); 5793c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 5803c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr); 5813c215bfdSMatthew Knepley #else 5823c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr); 5833c215bfdSMatthew Knepley #endif 584d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr); 5853c215bfdSMatthew Knepley for (i=0; i<m; i++) { 5863c215bfdSMatthew Knepley for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 5873c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 5883c215bfdSMatthew Knepley if (PetscImaginaryPart(a->a[j]) > 0.0) { 5893c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 5903c215bfdSMatthew Knepley } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 5913c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G -%G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 5923c215bfdSMatthew Knepley } else { 5933c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 5943c215bfdSMatthew Knepley } 5953c215bfdSMatthew Knepley #else 5963c215bfdSMatthew Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr); 5973c215bfdSMatthew Knepley #endif 5983c215bfdSMatthew Knepley } 5993c215bfdSMatthew Knepley } 600d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 6013a40ed3dSBarry Smith } else { 602d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 6037566de4bSShri Abhyankar ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr); 604d5f3da31SBarry Smith if (A->factortype){ 60516cd7e1dSShri Abhyankar for (i=0; i<m; i++) { 60616cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 60716cd7e1dSShri Abhyankar /* L part */ 60816cd7e1dSShri Abhyankar for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 60916cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 61016cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 61116cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 61216cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 61316cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 61416cd7e1dSShri Abhyankar } else { 61516cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 61616cd7e1dSShri Abhyankar } 61716cd7e1dSShri Abhyankar #else 61816cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 61916cd7e1dSShri Abhyankar #endif 62016cd7e1dSShri Abhyankar } 62116cd7e1dSShri Abhyankar /* diagonal */ 62216cd7e1dSShri Abhyankar j = a->diag[i]; 62316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 62416cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 6252c990fa1SHong Zhang ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr); 62616cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 6272c990fa1SHong Zhang ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),-PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr); 62816cd7e1dSShri Abhyankar } else { 6292c990fa1SHong Zhang ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr); 63016cd7e1dSShri Abhyankar } 63116cd7e1dSShri Abhyankar #else 6322c990fa1SHong Zhang ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,1.0/a->a[j]);CHKERRQ(ierr); 63316cd7e1dSShri Abhyankar #endif 63416cd7e1dSShri Abhyankar 63516cd7e1dSShri Abhyankar /* U part */ 63616cd7e1dSShri Abhyankar for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) { 63716cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 63816cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 63916cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 64016cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 64116cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 64216cd7e1dSShri Abhyankar } else { 64316cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 64416cd7e1dSShri Abhyankar } 64516cd7e1dSShri Abhyankar #else 64616cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 64716cd7e1dSShri Abhyankar #endif 64816cd7e1dSShri Abhyankar } 64916cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 65016cd7e1dSShri Abhyankar } 65116cd7e1dSShri Abhyankar } else { 65217ab2063SBarry Smith for (i=0; i<m; i++) { 65377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 654416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 655aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 65636db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 657a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 65836db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 659a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 6603a40ed3dSBarry Smith } else { 661a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 66217ab2063SBarry Smith } 66317ab2063SBarry Smith #else 664a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 66517ab2063SBarry Smith #endif 66617ab2063SBarry Smith } 667b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 66817ab2063SBarry Smith } 66916cd7e1dSShri Abhyankar } 670d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 67117ab2063SBarry Smith } 672b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 6733a40ed3dSBarry Smith PetscFunctionReturn(0); 674416022c9SBarry Smith } 675416022c9SBarry Smith 6764a2ae208SSatish Balay #undef __FUNCT__ 6774a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom" 678dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 679416022c9SBarry Smith { 680480ef9eaSBarry Smith Mat A = (Mat) Aa; 681416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 682dfbe8321SBarry Smith PetscErrorCode ierr; 683d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,color; 68436db0b34SBarry Smith PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0; 685b0a32e0cSBarry Smith PetscViewer viewer; 686f3ef73ceSBarry Smith PetscViewerFormat format; 687cddf8d76SBarry Smith 6883a40ed3dSBarry Smith PetscFunctionBegin; 689480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 690b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 69119bcc07fSBarry Smith 692b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 693416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 6940513a670SBarry Smith 695fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 6960513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 697b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 698416022c9SBarry Smith for (i=0; i<m; i++) { 699cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 700bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 701bfeeae90SHong Zhang x_l = a->j[j] ; x_r = x_l + 1.0; 702aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 70336db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 704cddf8d76SBarry Smith #else 705cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 706cddf8d76SBarry Smith #endif 707b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 708cddf8d76SBarry Smith } 709cddf8d76SBarry Smith } 710b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 711cddf8d76SBarry Smith for (i=0; i<m; i++) { 712cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 713bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 714bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 715cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 716b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 717cddf8d76SBarry Smith } 718cddf8d76SBarry Smith } 719b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 720cddf8d76SBarry Smith for (i=0; i<m; i++) { 721cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 722bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 723bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 724aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 72536db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 726cddf8d76SBarry Smith #else 727cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 728cddf8d76SBarry Smith #endif 729b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 730416022c9SBarry Smith } 731416022c9SBarry Smith } 7320513a670SBarry Smith } else { 7330513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 7340513a670SBarry Smith /* first determine max of all nonzero values */ 73597f1f81fSBarry Smith PetscInt nz = a->nz,count; 736b0a32e0cSBarry Smith PetscDraw popup; 73736db0b34SBarry Smith PetscReal scale; 7380513a670SBarry Smith 7390513a670SBarry Smith for (i=0; i<nz; i++) { 7400513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 7410513a670SBarry Smith } 742b0a32e0cSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv; 743b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 744b0a32e0cSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);} 7450513a670SBarry Smith count = 0; 7460513a670SBarry Smith for (i=0; i<m; i++) { 7470513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 748bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 749bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 75097f1f81fSBarry Smith color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count])); 751b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 7520513a670SBarry Smith count++; 7530513a670SBarry Smith } 7540513a670SBarry Smith } 7550513a670SBarry Smith } 756480ef9eaSBarry Smith PetscFunctionReturn(0); 757480ef9eaSBarry Smith } 758cddf8d76SBarry Smith 7594a2ae208SSatish Balay #undef __FUNCT__ 7604a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw" 761dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 762480ef9eaSBarry Smith { 763dfbe8321SBarry Smith PetscErrorCode ierr; 764b0a32e0cSBarry Smith PetscDraw draw; 76536db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 766ace3abfcSBarry Smith PetscBool isnull; 767480ef9eaSBarry Smith 768480ef9eaSBarry Smith PetscFunctionBegin; 769b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 770b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 771480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 772480ef9eaSBarry Smith 773480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 774d0f46423SBarry Smith xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0; 775480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 776b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 777b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 778480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr); 7793a40ed3dSBarry Smith PetscFunctionReturn(0); 780416022c9SBarry Smith } 781416022c9SBarry Smith 7824a2ae208SSatish Balay #undef __FUNCT__ 7834a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ" 784dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 785416022c9SBarry Smith { 786dfbe8321SBarry Smith PetscErrorCode ierr; 787ace3abfcSBarry Smith PetscBool iascii,isbinary,isdraw; 788416022c9SBarry Smith 7893a40ed3dSBarry Smith PetscFunctionBegin; 790251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 791251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 792251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 793c45a1595SBarry Smith if (iascii) { 7943a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 7950f5bd95cSBarry Smith } else if (isbinary) { 7963a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 7970f5bd95cSBarry Smith } else if (isdraw) { 7983a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 799913ac41fSBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name); 8004108e4d5SBarry Smith ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr); 8013a40ed3dSBarry Smith PetscFunctionReturn(0); 80217ab2063SBarry Smith } 80319bcc07fSBarry Smith 8044a2ae208SSatish Balay #undef __FUNCT__ 8054a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ" 806dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 80717ab2063SBarry Smith { 808416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8096849ba73SBarry Smith PetscErrorCode ierr; 81097f1f81fSBarry Smith PetscInt fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax; 811d0f46423SBarry Smith PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0; 81254f21887SBarry Smith MatScalar *aa = a->a,*ap; 8133447b6efSHong Zhang PetscReal ratio=0.6; 81417ab2063SBarry Smith 8153a40ed3dSBarry Smith PetscFunctionBegin; 8163a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 81717ab2063SBarry Smith 81843ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 81917ab2063SBarry Smith for (i=1; i<m; i++) { 820416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 82117ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 82294a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 82317ab2063SBarry Smith if (fshift) { 824bfeeae90SHong Zhang ip = aj + ai[i] ; 825bfeeae90SHong Zhang ap = aa + ai[i] ; 82617ab2063SBarry Smith N = ailen[i]; 82717ab2063SBarry Smith for (j=0; j<N; j++) { 82817ab2063SBarry Smith ip[j-fshift] = ip[j]; 82917ab2063SBarry Smith ap[j-fshift] = ap[j]; 83017ab2063SBarry Smith } 83117ab2063SBarry Smith } 83217ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 83317ab2063SBarry Smith } 83417ab2063SBarry Smith if (m) { 83517ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 83617ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 83717ab2063SBarry Smith } 83817ab2063SBarry Smith /* reset ilen and imax for each row */ 83917ab2063SBarry Smith for (i=0; i<m; i++) { 84017ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 84117ab2063SBarry Smith } 842bfeeae90SHong Zhang a->nz = ai[m]; 84365e19b50SBarry Smith if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift); 84417ab2063SBarry Smith 84509f38230SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 846d0f46423SBarry Smith ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);CHKERRQ(ierr); 847ae15b995SBarry Smith ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr); 848ae15b995SBarry Smith ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr); 8498e58a170SBarry Smith A->info.mallocs += a->reallocs; 850dd5f02e7SSatish Balay a->reallocs = 0; 8514e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 85236db0b34SBarry Smith a->rmax = rmax; 8534e220ebcSLois Curfman McInnes 854cd6b891eSBarry Smith ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 85588e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 85671c2f376SKris Buschelman 8574108e4d5SBarry Smith ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr); 85871f1c65dSBarry Smith 85971f1c65dSBarry Smith a->idiagvalid = PETSC_FALSE; 860bbead8a2SBarry Smith a->ibdiagvalid = PETSC_FALSE; 8613a40ed3dSBarry Smith PetscFunctionReturn(0); 86217ab2063SBarry Smith } 86317ab2063SBarry Smith 8644a2ae208SSatish Balay #undef __FUNCT__ 86599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ" 86699cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 86799cafbc1SBarry Smith { 86899cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 86999cafbc1SBarry Smith PetscInt i,nz = a->nz; 87054f21887SBarry Smith MatScalar *aa = a->a; 87199cafbc1SBarry Smith 87299cafbc1SBarry Smith PetscFunctionBegin; 87399cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 87486c113feSBarry Smith a->idiagvalid = PETSC_FALSE; 87586c113feSBarry Smith a->ibdiagvalid = PETSC_FALSE; 87699cafbc1SBarry Smith PetscFunctionReturn(0); 87799cafbc1SBarry Smith } 87899cafbc1SBarry Smith 87999cafbc1SBarry Smith #undef __FUNCT__ 88099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ" 88199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 88299cafbc1SBarry Smith { 88399cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 88499cafbc1SBarry Smith PetscInt i,nz = a->nz; 88554f21887SBarry Smith MatScalar *aa = a->a; 88699cafbc1SBarry Smith 88799cafbc1SBarry Smith PetscFunctionBegin; 88899cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 88986c113feSBarry Smith a->idiagvalid = PETSC_FALSE; 89086c113feSBarry Smith a->ibdiagvalid = PETSC_FALSE; 89199cafbc1SBarry Smith PetscFunctionReturn(0); 89299cafbc1SBarry Smith } 89399cafbc1SBarry Smith 89499cafbc1SBarry Smith #undef __FUNCT__ 8954a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ" 896dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 89717ab2063SBarry Smith { 898416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 899dfbe8321SBarry Smith PetscErrorCode ierr; 9003a40ed3dSBarry Smith 9013a40ed3dSBarry Smith PetscFunctionBegin; 902d0f46423SBarry Smith ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 90386c113feSBarry Smith a->idiagvalid = PETSC_FALSE; 90486c113feSBarry Smith a->ibdiagvalid = PETSC_FALSE; 9053a40ed3dSBarry Smith PetscFunctionReturn(0); 90617ab2063SBarry Smith } 907416022c9SBarry Smith 9084a2ae208SSatish Balay #undef __FUNCT__ 9094a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ" 910dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 91117ab2063SBarry Smith { 912416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 913dfbe8321SBarry Smith PetscErrorCode ierr; 914d5d45c9bSBarry Smith 9153a40ed3dSBarry Smith PetscFunctionBegin; 916aa482453SBarry Smith #if defined(PETSC_USE_LOG) 917d0f46423SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz); 91817ab2063SBarry Smith #endif 919e6b907acSBarry Smith ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr); 9206bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 9216bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 92205b42c5fSBarry Smith ierr = PetscFree(a->diag);CHKERRQ(ierr); 923d48dcb14SBarry Smith ierr = PetscFree(a->ibdiag);CHKERRQ(ierr); 92405b42c5fSBarry Smith ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr); 92571f1c65dSBarry Smith ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr); 92605b42c5fSBarry Smith ierr = PetscFree(a->solve_work);CHKERRQ(ierr); 9276bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 92805b42c5fSBarry Smith ierr = PetscFree(a->saved_values);CHKERRQ(ierr); 9296bf464f9SBarry Smith ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr); 93005b42c5fSBarry Smith ierr = PetscFree(a->xtoy);CHKERRQ(ierr); 9316bf464f9SBarry Smith ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr); 932cd6b891eSBarry Smith ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr); 9330b7e3e3dSHong Zhang ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr); 934a30b2313SHong Zhang 9354108e4d5SBarry Smith ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr); 936bf0cc555SLisandro Dalcin ierr = PetscFree(A->data);CHKERRQ(ierr); 937901853e0SKris Buschelman 938dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr); 939901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr); 940901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 941901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 942901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr); 943901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr); 9445a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr); 945901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 946901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 947a1661176SMatthew Knepley ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 948901853e0SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr); 9493a40ed3dSBarry Smith PetscFunctionReturn(0); 95017ab2063SBarry Smith } 95117ab2063SBarry Smith 9524a2ae208SSatish Balay #undef __FUNCT__ 9534a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ" 954ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg) 95517ab2063SBarry Smith { 956416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 9574846f1f5SKris Buschelman PetscErrorCode ierr; 9583a40ed3dSBarry Smith 9593a40ed3dSBarry Smith PetscFunctionBegin; 960a65d3064SKris Buschelman switch (op) { 961a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 9624e0d8c25SBarry Smith a->roworiented = flg; 963a65d3064SKris Buschelman break; 964a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 965a9817697SBarry Smith a->keepnonzeropattern = flg; 966a65d3064SKris Buschelman break; 967512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 968512a5fc5SBarry Smith a->nonew = (flg ? 0 : 1); 969a65d3064SKris Buschelman break; 970a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 9714e0d8c25SBarry Smith a->nonew = (flg ? -1 : 0); 972a65d3064SKris Buschelman break; 973a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 9744e0d8c25SBarry Smith a->nonew = (flg ? -2 : 0); 975a65d3064SKris Buschelman break; 97628b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 97728b2fa4aSMatthew Knepley a->nounused = (flg ? -1 : 0); 97828b2fa4aSMatthew Knepley break; 979a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 9804e0d8c25SBarry Smith a->ignorezeroentries = flg; 9810df259c2SBarry Smith break; 982cd6b891eSBarry Smith case MAT_CHECK_COMPRESSED_ROW: 983cd6b891eSBarry Smith a->compressedrow.check = flg; 984d487561eSHong Zhang break; 9853d472b54SHong Zhang case MAT_SPD: 9863d472b54SHong Zhang A->spd_set = PETSC_TRUE; 9873d472b54SHong Zhang A->spd = flg; 9883d472b54SHong Zhang if (flg) { 9893d472b54SHong Zhang A->symmetric = PETSC_TRUE; 9903d472b54SHong Zhang A->structurally_symmetric = PETSC_TRUE; 9913d472b54SHong Zhang A->symmetric_set = PETSC_TRUE; 9923d472b54SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 9933d472b54SHong Zhang } 9943d472b54SHong Zhang break; 995b1646e73SJed Brown case MAT_SYMMETRIC: 996b1646e73SJed Brown case MAT_STRUCTURALLY_SYMMETRIC: 997b1646e73SJed Brown case MAT_HERMITIAN: 998b1646e73SJed Brown case MAT_SYMMETRY_ETERNAL: 9994e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1000a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 1001a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 1002290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 1003a65d3064SKris Buschelman break; 1004b87ac2d8SJed Brown case MAT_USE_INODES: 1005b87ac2d8SJed Brown /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */ 1006b87ac2d8SJed Brown break; 1007a65d3064SKris Buschelman default: 1008e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 1009a65d3064SKris Buschelman } 10104108e4d5SBarry Smith ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr); 10113a40ed3dSBarry Smith PetscFunctionReturn(0); 101217ab2063SBarry Smith } 101317ab2063SBarry Smith 10144a2ae208SSatish Balay #undef __FUNCT__ 10154a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ" 1016dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 101717ab2063SBarry Smith { 1018416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 10196849ba73SBarry Smith PetscErrorCode ierr; 1020d3e70bfaSHong Zhang PetscInt i,j,n,*ai=a->i,*aj=a->j,nz; 102135e7444dSHong Zhang PetscScalar *aa=a->a,*x,zero=0.0; 102217ab2063SBarry Smith 10233a40ed3dSBarry Smith PetscFunctionBegin; 1024d3e70bfaSHong Zhang ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 1025e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 102635e7444dSHong Zhang 1027d5f3da31SBarry Smith if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){ 1028d3e70bfaSHong Zhang PetscInt *diag=a->diag; 102935e7444dSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 10302c990fa1SHong Zhang for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]]; 103135e7444dSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 103235e7444dSHong Zhang PetscFunctionReturn(0); 103335e7444dSHong Zhang } 103435e7444dSHong Zhang 10352dcb1b2aSMatthew Knepley ierr = VecSet(v,zero);CHKERRQ(ierr); 10361ebc52fbSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 103735e7444dSHong Zhang for (i=0; i<n; i++) { 103835e7444dSHong Zhang nz = ai[i+1] - ai[i]; 10392f5a7c2eSBarry Smith if (!nz) x[i] = 0.0; 104035e7444dSHong Zhang for (j=ai[i]; j<ai[i+1]; j++){ 104135e7444dSHong Zhang if (aj[j] == i) { 104235e7444dSHong Zhang x[i] = aa[j]; 104317ab2063SBarry Smith break; 104417ab2063SBarry Smith } 104517ab2063SBarry Smith } 104617ab2063SBarry Smith } 10471ebc52fbSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 10483a40ed3dSBarry Smith PetscFunctionReturn(0); 104917ab2063SBarry Smith } 105017ab2063SBarry Smith 1051c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 10524a2ae208SSatish Balay #undef __FUNCT__ 10534a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ" 1054dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 105517ab2063SBarry Smith { 1056416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 10575c897100SBarry Smith PetscScalar *x,*y; 1058dfbe8321SBarry Smith PetscErrorCode ierr; 1059d0f46423SBarry Smith PetscInt m = A->rmap->n; 10605c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1061a77337e4SBarry Smith MatScalar *v; 1062a77337e4SBarry Smith PetscScalar alpha; 106304fbf559SBarry Smith PetscInt n,i,j,*idx,*ii,*ridx=PETSC_NULL; 10643447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 1065ace3abfcSBarry Smith PetscBool usecprow = cprow.use; 10665c897100SBarry Smith #endif 106717ab2063SBarry Smith 10683a40ed3dSBarry Smith PetscFunctionBegin; 10692e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 10701ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 10711ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 10725c897100SBarry Smith 10735c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1074bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 10755c897100SBarry Smith #else 10763447b6efSHong Zhang if (usecprow){ 10773447b6efSHong Zhang m = cprow.nrows; 10783447b6efSHong Zhang ii = cprow.i; 10797b2bb3b9SHong Zhang ridx = cprow.rindex; 10803447b6efSHong Zhang } else { 10813447b6efSHong Zhang ii = a->i; 10823447b6efSHong Zhang } 108317ab2063SBarry Smith for (i=0; i<m; i++) { 10843447b6efSHong Zhang idx = a->j + ii[i] ; 10853447b6efSHong Zhang v = a->a + ii[i] ; 10863447b6efSHong Zhang n = ii[i+1] - ii[i]; 10873447b6efSHong Zhang if (usecprow){ 10887b2bb3b9SHong Zhang alpha = x[ridx[i]]; 10893447b6efSHong Zhang } else { 109017ab2063SBarry Smith alpha = x[i]; 10913447b6efSHong Zhang } 109204fbf559SBarry Smith for (j=0; j<n; j++) y[idx[j]] += alpha*v[j]; 109317ab2063SBarry Smith } 10945c897100SBarry Smith #endif 1095dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 10961ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 10971ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 10983a40ed3dSBarry Smith PetscFunctionReturn(0); 109917ab2063SBarry Smith } 110017ab2063SBarry Smith 11014a2ae208SSatish Balay #undef __FUNCT__ 11025c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ" 1103dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 11045c897100SBarry Smith { 1105dfbe8321SBarry Smith PetscErrorCode ierr; 11065c897100SBarry Smith 11075c897100SBarry Smith PetscFunctionBegin; 1108170fe5c8SBarry Smith ierr = VecSet(yy,0.0);CHKERRQ(ierr); 11095c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 11105c897100SBarry Smith PetscFunctionReturn(0); 11115c897100SBarry Smith } 11125c897100SBarry Smith 1113c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 11145c897100SBarry Smith #undef __FUNCT__ 11154a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ" 1116dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 111717ab2063SBarry Smith { 1118416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1119d9fead3dSBarry Smith PetscScalar *y; 112054f21887SBarry Smith const PetscScalar *x; 112154f21887SBarry Smith const MatScalar *aa; 1122dfbe8321SBarry Smith PetscErrorCode ierr; 1123003131ecSBarry Smith PetscInt m=A->rmap->n; 1124003131ecSBarry Smith const PetscInt *aj,*ii,*ridx=PETSC_NULL; 11258aee2decSHong Zhang PetscInt n,i,nonzerorow=0; 1126362ced78SSatish Balay PetscScalar sum; 1127ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 112817ab2063SBarry Smith 1129b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 113097952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 1131fee21e36SBarry Smith #endif 1132fee21e36SBarry Smith 11333a40ed3dSBarry Smith PetscFunctionBegin; 11343649974fSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 11351ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 113697952fefSHong Zhang aj = a->j; 113797952fefSHong Zhang aa = a->a; 1138416022c9SBarry Smith ii = a->i; 11394eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 114097952fefSHong Zhang m = a->compressedrow.nrows; 114197952fefSHong Zhang ii = a->compressedrow.i; 114297952fefSHong Zhang ridx = a->compressedrow.rindex; 114397952fefSHong Zhang for (i=0; i<m; i++){ 114497952fefSHong Zhang n = ii[i+1] - ii[i]; 114597952fefSHong Zhang aj = a->j + ii[i]; 114697952fefSHong Zhang aa = a->a + ii[i]; 114797952fefSHong Zhang sum = 0.0; 1148a46b3154SVictor Eijkhout nonzerorow += (n>0); 1149003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 1150003131ecSBarry Smith /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 115197952fefSHong Zhang y[*ridx++] = sum; 115297952fefSHong Zhang } 115397952fefSHong Zhang } else { /* do not use compressed row format */ 1154b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 1155b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 1156b05257ddSBarry Smith #else 115717ab2063SBarry Smith for (i=0; i<m; i++) { 1158003131ecSBarry Smith n = ii[i+1] - ii[i]; 1159003131ecSBarry Smith aj = a->j + ii[i]; 1160003131ecSBarry Smith aa = a->a + ii[i]; 116117ab2063SBarry Smith sum = 0.0; 1162a46b3154SVictor Eijkhout nonzerorow += (n>0); 1163003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 116417ab2063SBarry Smith y[i] = sum; 116517ab2063SBarry Smith } 11668d195f9aSBarry Smith #endif 1167b05257ddSBarry Smith } 1168dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr); 11693649974fSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 11701ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 11713a40ed3dSBarry Smith PetscFunctionReturn(0); 117217ab2063SBarry Smith } 117317ab2063SBarry Smith 1174c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h> 11754a2ae208SSatish Balay #undef __FUNCT__ 11764a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ" 1177dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 117817ab2063SBarry Smith { 1179416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1180f15663dcSBarry Smith PetscScalar *y,*z; 1181f15663dcSBarry Smith const PetscScalar *x; 118254f21887SBarry Smith const MatScalar *aa; 1183dfbe8321SBarry Smith PetscErrorCode ierr; 1184d0f46423SBarry Smith PetscInt m = A->rmap->n,*aj,*ii; 1185f15663dcSBarry Smith PetscInt n,i,*ridx=PETSC_NULL; 1186362ced78SSatish Balay PetscScalar sum; 1187ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 11889ea0dfa2SSatish Balay 11893a40ed3dSBarry Smith PetscFunctionBegin; 1190f15663dcSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 11911ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 11922e8a6d31SBarry Smith if (zz != yy) { 11931ebc52fbSHong Zhang ierr = VecGetArray(zz,&z);CHKERRQ(ierr); 11942e8a6d31SBarry Smith } else { 11952e8a6d31SBarry Smith z = y; 11962e8a6d31SBarry Smith } 1197bfeeae90SHong Zhang 119897952fefSHong Zhang aj = a->j; 119997952fefSHong Zhang aa = a->a; 1200cddf8d76SBarry Smith ii = a->i; 12014eb6d288SHong Zhang if (usecprow){ /* use compressed row format */ 12024eb6d288SHong Zhang if (zz != yy){ 12034eb6d288SHong Zhang ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr); 12044eb6d288SHong Zhang } 120597952fefSHong Zhang m = a->compressedrow.nrows; 120697952fefSHong Zhang ii = a->compressedrow.i; 120797952fefSHong Zhang ridx = a->compressedrow.rindex; 120897952fefSHong Zhang for (i=0; i<m; i++){ 120997952fefSHong Zhang n = ii[i+1] - ii[i]; 121097952fefSHong Zhang aj = a->j + ii[i]; 121197952fefSHong Zhang aa = a->a + ii[i]; 121297952fefSHong Zhang sum = y[*ridx]; 1213f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 121497952fefSHong Zhang z[*ridx++] = sum; 121597952fefSHong Zhang } 121697952fefSHong Zhang } else { /* do not use compressed row format */ 1217f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 1218f15663dcSBarry Smith fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 1219f15663dcSBarry Smith #else 122017ab2063SBarry Smith for (i=0; i<m; i++) { 1221f15663dcSBarry Smith n = ii[i+1] - ii[i]; 1222f15663dcSBarry Smith aj = a->j + ii[i]; 1223f15663dcSBarry Smith aa = a->a + ii[i]; 122417ab2063SBarry Smith sum = y[i]; 1225f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 122617ab2063SBarry Smith z[i] = sum; 122717ab2063SBarry Smith } 122802ab625aSSatish Balay #endif 1229f15663dcSBarry Smith } 1230dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1231f15663dcSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 12321ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 12332e8a6d31SBarry Smith if (zz != yy) { 12341ebc52fbSHong Zhang ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr); 12352e8a6d31SBarry Smith } 12368154be41SBarry Smith #if defined(PETSC_HAVE_CUSP) 12376b375ea7SVictor Minden /* 1238918e98c3SVictor Minden ierr = VecView(xx,0);CHKERRQ(ierr); 1239918e98c3SVictor Minden ierr = VecView(zz,0);CHKERRQ(ierr); 1240918e98c3SVictor Minden ierr = MatView(A,0);CHKERRQ(ierr); 12416b375ea7SVictor Minden */ 1242918e98c3SVictor Minden #endif 12433a40ed3dSBarry Smith PetscFunctionReturn(0); 124417ab2063SBarry Smith } 124517ab2063SBarry Smith 124617ab2063SBarry Smith /* 124717ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 124817ab2063SBarry Smith */ 12494a2ae208SSatish Balay #undef __FUNCT__ 12504a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ" 1251dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 125217ab2063SBarry Smith { 1253416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12546849ba73SBarry Smith PetscErrorCode ierr; 1255d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n; 125617ab2063SBarry Smith 12573a40ed3dSBarry Smith PetscFunctionBegin; 125809f38230SBarry Smith if (!a->diag) { 125909f38230SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr); 12609518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr); 126109f38230SBarry Smith } 1262d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 126309f38230SBarry Smith a->diag[i] = a->i[i+1]; 1264bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1265bfeeae90SHong Zhang if (a->j[j] == i) { 126609f38230SBarry Smith a->diag[i] = j; 126717ab2063SBarry Smith break; 126817ab2063SBarry Smith } 126917ab2063SBarry Smith } 127017ab2063SBarry Smith } 12713a40ed3dSBarry Smith PetscFunctionReturn(0); 127217ab2063SBarry Smith } 127317ab2063SBarry Smith 1274be5855fcSBarry Smith /* 1275be5855fcSBarry Smith Checks for missing diagonals 1276be5855fcSBarry Smith */ 12774a2ae208SSatish Balay #undef __FUNCT__ 12784a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ" 1279ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool *missing,PetscInt *d) 1280be5855fcSBarry Smith { 1281be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 128297f1f81fSBarry Smith PetscInt *diag,*jj = a->j,i; 1283be5855fcSBarry Smith 1284be5855fcSBarry Smith PetscFunctionBegin; 128509f38230SBarry Smith *missing = PETSC_FALSE; 1286d0f46423SBarry Smith if (A->rmap->n > 0 && !jj) { 128709f38230SBarry Smith *missing = PETSC_TRUE; 128809f38230SBarry Smith if (d) *d = 0; 1289358d2f5dSShri Abhyankar PetscInfo(A,"Matrix has no entries therefore is missing diagonal"); 129009f38230SBarry Smith } else { 1291f1e2ffcdSBarry Smith diag = a->diag; 1292d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 1293bfeeae90SHong Zhang if (jj[diag[i]] != i) { 129409f38230SBarry Smith *missing = PETSC_TRUE; 129509f38230SBarry Smith if (d) *d = i; 129609f38230SBarry Smith PetscInfo1(A,"Matrix is missing diagonal number %D",i); 1297358d2f5dSShri Abhyankar break; 129809f38230SBarry Smith } 1299be5855fcSBarry Smith } 1300be5855fcSBarry Smith } 1301be5855fcSBarry Smith PetscFunctionReturn(0); 1302be5855fcSBarry Smith } 1303be5855fcSBarry Smith 130471f1c65dSBarry Smith EXTERN_C_BEGIN 130571f1c65dSBarry Smith #undef __FUNCT__ 130671f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ" 13077087cfbeSBarry Smith PetscErrorCode MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift) 130871f1c65dSBarry Smith { 130971f1c65dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 131071f1c65dSBarry Smith PetscErrorCode ierr; 1311d0f46423SBarry Smith PetscInt i,*diag,m = A->rmap->n; 131254f21887SBarry Smith MatScalar *v = a->a; 131354f21887SBarry Smith PetscScalar *idiag,*mdiag; 131471f1c65dSBarry Smith 131571f1c65dSBarry Smith PetscFunctionBegin; 131671f1c65dSBarry Smith if (a->idiagvalid) PetscFunctionReturn(0); 131771f1c65dSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 131871f1c65dSBarry Smith diag = a->diag; 131971f1c65dSBarry Smith if (!a->idiag) { 132071f1c65dSBarry Smith ierr = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr); 132171f1c65dSBarry Smith ierr = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr); 132271f1c65dSBarry Smith v = a->a; 132371f1c65dSBarry Smith } 132471f1c65dSBarry Smith mdiag = a->mdiag; 132571f1c65dSBarry Smith idiag = a->idiag; 132671f1c65dSBarry Smith 1327028cd4eaSSatish Balay if (omega == 1.0 && !PetscAbsScalar(fshift)) { 132871f1c65dSBarry Smith for (i=0; i<m; i++) { 132971f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 1330e32f2f54SBarry Smith if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i); 133171f1c65dSBarry Smith idiag[i] = 1.0/v[diag[i]]; 133271f1c65dSBarry Smith } 133371f1c65dSBarry Smith ierr = PetscLogFlops(m);CHKERRQ(ierr); 133471f1c65dSBarry Smith } else { 133571f1c65dSBarry Smith for (i=0; i<m; i++) { 133671f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 133771f1c65dSBarry Smith idiag[i] = omega/(fshift + v[diag[i]]); 133871f1c65dSBarry Smith } 1339dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr); 134071f1c65dSBarry Smith } 134171f1c65dSBarry Smith a->idiagvalid = PETSC_TRUE; 134271f1c65dSBarry Smith PetscFunctionReturn(0); 134371f1c65dSBarry Smith } 13445a9745a3SMatthew Knepley EXTERN_C_END 134571f1c65dSBarry Smith 1346c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h> 13474a2ae208SSatish Balay #undef __FUNCT__ 134841f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ" 134941f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 135017ab2063SBarry Smith { 1351416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1352e6d1f457SBarry Smith PetscScalar *x,d,sum,*t,scale; 1353e6d1f457SBarry Smith const MatScalar *v = a->a,*idiag=0,*mdiag; 135454f21887SBarry Smith const PetscScalar *b, *bs,*xb, *ts; 1355dfbe8321SBarry Smith PetscErrorCode ierr; 1356d0f46423SBarry Smith PetscInt n = A->cmap->n,m = A->rmap->n,i; 135797f1f81fSBarry Smith const PetscInt *idx,*diag; 135817ab2063SBarry Smith 13593a40ed3dSBarry Smith PetscFunctionBegin; 1360b965ef7fSBarry Smith its = its*lits; 136191723122SBarry Smith 136271f1c65dSBarry Smith if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */ 136371f1c65dSBarry Smith if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);} 136471f1c65dSBarry Smith a->fshift = fshift; 136571f1c65dSBarry Smith a->omega = omega; 1366ed480e8bSBarry Smith 136771f1c65dSBarry Smith diag = a->diag; 136871f1c65dSBarry Smith t = a->ssor_work; 1369ed480e8bSBarry Smith idiag = a->idiag; 137071f1c65dSBarry Smith mdiag = a->mdiag; 1371ed480e8bSBarry Smith 13721ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 13733649974fSBarry Smith ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr); 137471f1c65dSBarry Smith CHKMEMQ; 1375ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 137617ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 137717ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1378ed480e8bSBarry Smith bs = b; 137917ab2063SBarry Smith for (i=0; i<m; i++) { 138071f1c65dSBarry Smith d = fshift + mdiag[i]; 1381416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1382ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1383ed480e8bSBarry Smith v = a->a + diag[i] + 1; 138417ab2063SBarry Smith sum = b[i]*d/omega; 1385003131ecSBarry Smith PetscSparseDensePlusDot(sum,bs,v,idx,n); 138617ab2063SBarry Smith x[i] = sum; 138717ab2063SBarry Smith } 13881ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 13893649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1390efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 13913a40ed3dSBarry Smith PetscFunctionReturn(0); 139217ab2063SBarry Smith } 1393c783ea89SBarry Smith 139448af12d7SBarry Smith if (flag == SOR_APPLY_LOWER) { 1395e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 13963a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 139717ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 1398887ee2caSBarry Smith U is upper triangular, E = D/omega; This routine applies 139917ab2063SBarry Smith 140017ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 140117ab2063SBarry Smith 1402887ee2caSBarry Smith to a vector efficiently using Eisenstat's trick. 140317ab2063SBarry Smith */ 140417ab2063SBarry Smith scale = (2.0/omega) - 1.0; 140517ab2063SBarry Smith 140617ab2063SBarry Smith /* x = (E + U)^{-1} b */ 140717ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1408416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1409ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1410ed480e8bSBarry Smith v = a->a + diag[i] + 1; 141117ab2063SBarry Smith sum = b[i]; 1412e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1413ed480e8bSBarry Smith x[i] = sum*idiag[i]; 141417ab2063SBarry Smith } 141517ab2063SBarry Smith 141617ab2063SBarry Smith /* t = b - (2*E - D)x */ 1417416022c9SBarry Smith v = a->a; 1418ed480e8bSBarry Smith for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; } 141917ab2063SBarry Smith 142017ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1421ed480e8bSBarry Smith ts = t; 1422416022c9SBarry Smith diag = a->diag; 142317ab2063SBarry Smith for (i=0; i<m; i++) { 1424416022c9SBarry Smith n = diag[i] - a->i[i]; 1425ed480e8bSBarry Smith idx = a->j + a->i[i]; 1426ed480e8bSBarry Smith v = a->a + a->i[i]; 142717ab2063SBarry Smith sum = t[i]; 1428003131ecSBarry Smith PetscSparseDenseMinusDot(sum,ts,v,idx,n); 1429ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1430733d66baSBarry Smith /* x = x + t */ 1431733d66baSBarry Smith x[i] += t[i]; 143217ab2063SBarry Smith } 143317ab2063SBarry Smith 1434dc0b31edSSatish Balay ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr); 14351ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 14363649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 14373a40ed3dSBarry Smith PetscFunctionReturn(0); 143817ab2063SBarry Smith } 143917ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 144017ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 144117ab2063SBarry Smith for (i=0; i<m; i++) { 1442416022c9SBarry Smith n = diag[i] - a->i[i]; 1443ed480e8bSBarry Smith idx = a->j + a->i[i]; 1444ed480e8bSBarry Smith v = a->a + a->i[i]; 144517ab2063SBarry Smith sum = b[i]; 1446e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 14475c99c7daSBarry Smith t[i] = sum; 1448ed480e8bSBarry Smith x[i] = sum*idiag[i]; 144917ab2063SBarry Smith } 14505c99c7daSBarry Smith xb = t; 1451efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 14523a40ed3dSBarry Smith } else xb = b; 145317ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 145417ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1455416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1456ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1457ed480e8bSBarry Smith v = a->a + diag[i] + 1; 145817ab2063SBarry Smith sum = xb[i]; 1459e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 14605c99c7daSBarry Smith if (xb == b) { 1461ed480e8bSBarry Smith x[i] = sum*idiag[i]; 14625c99c7daSBarry Smith } else { 14635c99c7daSBarry Smith x[i] = (1-omega)*x[i] + sum*idiag[i]; 146417ab2063SBarry Smith } 14655c99c7daSBarry Smith } 1466efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 146717ab2063SBarry Smith } 146817ab2063SBarry Smith its--; 146917ab2063SBarry Smith } 147017ab2063SBarry Smith while (its--) { 147117ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 147217ab2063SBarry Smith for (i=0; i<m; i++) { 1473416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1474ed480e8bSBarry Smith idx = a->j + a->i[i]; 1475ed480e8bSBarry Smith v = a->a + a->i[i]; 147617ab2063SBarry Smith sum = b[i]; 1477e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1478ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 147917ab2063SBarry Smith } 14809f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 148117ab2063SBarry Smith } 148217ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 148317ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1484416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1485ed480e8bSBarry Smith idx = a->j + a->i[i]; 1486ed480e8bSBarry Smith v = a->a + a->i[i]; 148717ab2063SBarry Smith sum = b[i]; 1488e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1489ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 149017ab2063SBarry Smith } 14919f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 149217ab2063SBarry Smith } 149317ab2063SBarry Smith } 14941ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 14953649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 149671f1c65dSBarry Smith CHKMEMQ; PetscFunctionReturn(0); 149717ab2063SBarry Smith } 149817ab2063SBarry Smith 14992af78befSBarry Smith 15004a2ae208SSatish Balay #undef __FUNCT__ 15014a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ" 1502dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 150317ab2063SBarry Smith { 1504416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15054e220ebcSLois Curfman McInnes 15063a40ed3dSBarry Smith PetscFunctionBegin; 15074e220ebcSLois Curfman McInnes info->block_size = 1.0; 15084e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 15094e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 15104e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 15114e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 15128e58a170SBarry Smith info->mallocs = (double)A->info.mallocs; 15137adad957SLisandro Dalcin info->memory = ((PetscObject)A)->mem; 1514d5f3da31SBarry Smith if (A->factortype) { 15154e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 15164e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 15174e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 15184e220ebcSLois Curfman McInnes } else { 15194e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 15204e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 15214e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 15224e220ebcSLois Curfman McInnes } 15233a40ed3dSBarry Smith PetscFunctionReturn(0); 152417ab2063SBarry Smith } 152517ab2063SBarry Smith 15264a2ae208SSatish Balay #undef __FUNCT__ 15274a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ" 15282b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 152917ab2063SBarry Smith { 1530416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15313b98c0a2SBarry Smith PetscInt i,m = A->rmap->n - 1,d = 0; 15326849ba73SBarry Smith PetscErrorCode ierr; 153397b48c8fSBarry Smith const PetscScalar *xx; 153497b48c8fSBarry Smith PetscScalar *bb; 1535ace3abfcSBarry Smith PetscBool missing; 153617ab2063SBarry Smith 15373a40ed3dSBarry Smith PetscFunctionBegin; 153897b48c8fSBarry Smith if (x && b) { 153997b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 154097b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 154197b48c8fSBarry Smith for (i=0; i<N; i++) { 154297b48c8fSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 154397b48c8fSBarry Smith bb[rows[i]] = diag*xx[rows[i]]; 154497b48c8fSBarry Smith } 154597b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 154697b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 154797b48c8fSBarry Smith } 154897b48c8fSBarry Smith 1549a9817697SBarry Smith if (a->keepnonzeropattern) { 1550f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1551e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1552bfeeae90SHong Zhang ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1553f1e2ffcdSBarry Smith } 1554f4df32b1SMatthew Knepley if (diag != 0.0) { 155509f38230SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 1556e32f2f54SBarry Smith if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d); 1557f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1558f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 1559f1e2ffcdSBarry Smith } 1560f1e2ffcdSBarry Smith } 156188e51ccdSHong Zhang A->same_nonzero = PETSC_TRUE; 1562f1e2ffcdSBarry Smith } else { 1563f4df32b1SMatthew Knepley if (diag != 0.0) { 156417ab2063SBarry Smith for (i=0; i<N; i++) { 1565e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 15667ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1567416022c9SBarry Smith a->ilen[rows[i]] = 1; 1568f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 1569bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 15707ae801bdSBarry Smith } else { /* in case row was completely empty */ 1571f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 157217ab2063SBarry Smith } 157317ab2063SBarry Smith } 15743a40ed3dSBarry Smith } else { 157517ab2063SBarry Smith for (i=0; i<N; i++) { 1576e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1577416022c9SBarry Smith a->ilen[rows[i]] = 0; 157817ab2063SBarry Smith } 157917ab2063SBarry Smith } 158088e51ccdSHong Zhang A->same_nonzero = PETSC_FALSE; 1581f1e2ffcdSBarry Smith } 158243a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 15833a40ed3dSBarry Smith PetscFunctionReturn(0); 158417ab2063SBarry Smith } 158517ab2063SBarry Smith 15864a2ae208SSatish Balay #undef __FUNCT__ 15876e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ" 15886e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 15896e169961SBarry Smith { 15906e169961SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15916e169961SBarry Smith PetscInt i,j,m = A->rmap->n - 1,d = 0; 15926e169961SBarry Smith PetscErrorCode ierr; 15932b40b63fSBarry Smith PetscBool missing,*zeroed,vecs = PETSC_FALSE; 15946e169961SBarry Smith const PetscScalar *xx; 15956e169961SBarry Smith PetscScalar *bb; 15966e169961SBarry Smith 15976e169961SBarry Smith PetscFunctionBegin; 15986e169961SBarry Smith if (x && b) { 15996e169961SBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 16006e169961SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 16012b40b63fSBarry Smith vecs = PETSC_TRUE; 16026e169961SBarry Smith } 16036e169961SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr); 16046e169961SBarry Smith ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr); 16056e169961SBarry Smith for (i=0; i<N; i++) { 16066e169961SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 16076e169961SBarry Smith ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 16086e169961SBarry Smith zeroed[rows[i]] = PETSC_TRUE; 16096e169961SBarry Smith } 16106e169961SBarry Smith for (i=0; i<A->rmap->n; i++) { 16116e169961SBarry Smith if (!zeroed[i]) { 16126e169961SBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 16136e169961SBarry Smith if (zeroed[a->j[j]]) { 16142b40b63fSBarry Smith if (vecs) bb[i] -= a->a[j]*xx[a->j[j]]; 16156e169961SBarry Smith a->a[j] = 0.0; 16166e169961SBarry Smith } 16176e169961SBarry Smith } 16182b40b63fSBarry Smith } else if (vecs) bb[i] = diag*xx[i]; 16196e169961SBarry Smith } 16206e169961SBarry Smith if (x && b) { 16216e169961SBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 16226e169961SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 16236e169961SBarry Smith } 16246e169961SBarry Smith ierr = PetscFree(zeroed);CHKERRQ(ierr); 16256e169961SBarry Smith if (diag != 0.0) { 16266e169961SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 16276e169961SBarry Smith if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d); 16286e169961SBarry Smith for (i=0; i<N; i++) { 16296e169961SBarry Smith a->a[a->diag[rows[i]]] = diag; 16306e169961SBarry Smith } 16316e169961SBarry Smith } 16326e169961SBarry Smith A->same_nonzero = PETSC_TRUE; 16336e169961SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 16346e169961SBarry Smith PetscFunctionReturn(0); 16356e169961SBarry Smith } 16366e169961SBarry Smith 16376e169961SBarry Smith #undef __FUNCT__ 16384a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ" 1639a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 164017ab2063SBarry Smith { 1641416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 164297f1f81fSBarry Smith PetscInt *itmp; 164317ab2063SBarry Smith 16443a40ed3dSBarry Smith PetscFunctionBegin; 1645e32f2f54SBarry Smith if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 164617ab2063SBarry Smith 1647416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1648bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 164917ab2063SBarry Smith if (idx) { 1650bfeeae90SHong Zhang itmp = a->j + a->i[row]; 1651bfeeae90SHong Zhang if (*nz) { 16524e093b46SBarry Smith *idx = itmp; 165317ab2063SBarry Smith } 165417ab2063SBarry Smith else *idx = 0; 165517ab2063SBarry Smith } 16563a40ed3dSBarry Smith PetscFunctionReturn(0); 165717ab2063SBarry Smith } 165817ab2063SBarry Smith 1659bfeeae90SHong Zhang /* remove this function? */ 16604a2ae208SSatish Balay #undef __FUNCT__ 16614a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ" 1662a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 166317ab2063SBarry Smith { 16643a40ed3dSBarry Smith PetscFunctionBegin; 16653a40ed3dSBarry Smith PetscFunctionReturn(0); 166617ab2063SBarry Smith } 166717ab2063SBarry Smith 16684a2ae208SSatish Balay #undef __FUNCT__ 16694a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ" 1670dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 167117ab2063SBarry Smith { 1672416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 167354f21887SBarry Smith MatScalar *v = a->a; 167436db0b34SBarry Smith PetscReal sum = 0.0; 16756849ba73SBarry Smith PetscErrorCode ierr; 167697f1f81fSBarry Smith PetscInt i,j; 167717ab2063SBarry Smith 16783a40ed3dSBarry Smith PetscFunctionBegin; 167917ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1680416022c9SBarry Smith for (i=0; i<a->nz; i++) { 1681aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 168236db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 168317ab2063SBarry Smith #else 168417ab2063SBarry Smith sum += (*v)*(*v); v++; 168517ab2063SBarry Smith #endif 168617ab2063SBarry Smith } 16878f1a2a5eSBarry Smith *nrm = PetscSqrtReal(sum); 16883a40ed3dSBarry Smith } else if (type == NORM_1) { 168936db0b34SBarry Smith PetscReal *tmp; 169097f1f81fSBarry Smith PetscInt *jj = a->j; 1691d0f46423SBarry Smith ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1692d0f46423SBarry Smith ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr); 1693064f8208SBarry Smith *nrm = 0.0; 1694416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1695bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 169617ab2063SBarry Smith } 1697d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 1698064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 169917ab2063SBarry Smith } 1700606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 17013a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1702064f8208SBarry Smith *nrm = 0.0; 1703d0f46423SBarry Smith for (j=0; j<A->rmap->n; j++) { 1704bfeeae90SHong Zhang v = a->a + a->i[j]; 170517ab2063SBarry Smith sum = 0.0; 1706416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1707cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 170817ab2063SBarry Smith } 1709064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 171017ab2063SBarry Smith } 17113a40ed3dSBarry Smith } else { 1712e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm"); 171317ab2063SBarry Smith } 17143a40ed3dSBarry Smith PetscFunctionReturn(0); 171517ab2063SBarry Smith } 171617ab2063SBarry Smith 17174e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */ 17184e938277SHong Zhang #undef __FUNCT__ 17194e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ" 17204e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B) 17214e938277SHong Zhang { 17224e938277SHong Zhang PetscErrorCode ierr; 17234e938277SHong Zhang PetscInt i,j,anzj; 17244e938277SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ *)A->data,*b; 17254e938277SHong Zhang PetscInt an=A->cmap->N,am=A->rmap->N; 17264e938277SHong Zhang PetscInt *ati,*atj,*atfill,*ai=a->i,*aj=a->j; 17274e938277SHong Zhang 17284e938277SHong Zhang PetscFunctionBegin; 17294e938277SHong Zhang /* Allocate space for symbolic transpose info and work array */ 17304e938277SHong Zhang ierr = PetscMalloc((an+1)*sizeof(PetscInt),&ati);CHKERRQ(ierr); 17314e938277SHong Zhang ierr = PetscMalloc(ai[am]*sizeof(PetscInt),&atj);CHKERRQ(ierr); 17324e938277SHong Zhang ierr = PetscMalloc(an*sizeof(PetscInt),&atfill);CHKERRQ(ierr); 17334e938277SHong Zhang ierr = PetscMemzero(ati,(an+1)*sizeof(PetscInt));CHKERRQ(ierr); 17344e938277SHong Zhang 17354e938277SHong Zhang /* Walk through aj and count ## of non-zeros in each row of A^T. */ 17364e938277SHong Zhang /* Note: offset by 1 for fast conversion into csr format. */ 17374e938277SHong Zhang for (i=0;i<ai[am];i++) { 17384e938277SHong Zhang ati[aj[i]+1] += 1; 17394e938277SHong Zhang } 17404e938277SHong Zhang /* Form ati for csr format of A^T. */ 17414e938277SHong Zhang for (i=0;i<an;i++) { 17424e938277SHong Zhang ati[i+1] += ati[i]; 17434e938277SHong Zhang } 17444e938277SHong Zhang 17454e938277SHong Zhang /* Copy ati into atfill so we have locations of the next free space in atj */ 17464e938277SHong Zhang ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr); 17474e938277SHong Zhang 17484e938277SHong Zhang /* Walk through A row-wise and mark nonzero entries of A^T. */ 17494e938277SHong Zhang for (i=0;i<am;i++) { 17504e938277SHong Zhang anzj = ai[i+1] - ai[i]; 17514e938277SHong Zhang for (j=0;j<anzj;j++) { 17524e938277SHong Zhang atj[atfill[*aj]] = i; 17534e938277SHong Zhang atfill[*aj++] += 1; 17544e938277SHong Zhang } 17554e938277SHong Zhang } 17564e938277SHong Zhang 17574e938277SHong Zhang /* Clean up temporary space and complete requests. */ 17584e938277SHong Zhang ierr = PetscFree(atfill);CHKERRQ(ierr); 17594e938277SHong Zhang ierr = MatCreateSeqAIJWithArrays(((PetscObject)A)->comm,an,am,ati,atj,PETSC_NULL,B);CHKERRQ(ierr); 1760a2f3521dSMark F. Adams (*B)->rmap->bs = A->cmap->bs; 1761a2f3521dSMark F. Adams (*B)->cmap->bs = A->rmap->bs; 1762a2f3521dSMark F. Adams 17634e938277SHong Zhang b = (Mat_SeqAIJ *)((*B)->data); 17644e938277SHong Zhang b->free_a = PETSC_FALSE; 17654e938277SHong Zhang b->free_ij = PETSC_TRUE; 17664e938277SHong Zhang b->nonew = 0; 17674e938277SHong Zhang PetscFunctionReturn(0); 17684e938277SHong Zhang } 17694e938277SHong Zhang 17704a2ae208SSatish Balay #undef __FUNCT__ 17714a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ" 1772fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B) 177317ab2063SBarry Smith { 1774416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1775416022c9SBarry Smith Mat C; 17766849ba73SBarry Smith PetscErrorCode ierr; 1777d0f46423SBarry Smith PetscInt i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col; 177854f21887SBarry Smith MatScalar *array = a->a; 177917ab2063SBarry Smith 17803a40ed3dSBarry Smith PetscFunctionBegin; 1781e32f2f54SBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1782fc4dec0aSBarry Smith 1783fc4dec0aSBarry Smith if (reuse == MAT_INITIAL_MATRIX || *B == A) { 1784d0f46423SBarry Smith ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr); 1785d0f46423SBarry Smith ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr); 1786bfeeae90SHong Zhang 1787bfeeae90SHong Zhang for (i=0; i<ai[m]; i++) col[aj[i]] += 1; 17887adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 1789d0f46423SBarry Smith ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr); 1790a2f3521dSMark F. Adams ierr = MatSetBlockSizes(C,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr); 17917adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 1792ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr); 1793606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 1794a541d17aSBarry Smith } else { 1795a541d17aSBarry Smith C = *B; 1796a541d17aSBarry Smith } 1797a541d17aSBarry Smith 179817ab2063SBarry Smith for (i=0; i<m; i++) { 179917ab2063SBarry Smith len = ai[i+1]-ai[i]; 180087d4246cSBarry Smith ierr = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 1801b9b97703SBarry Smith array += len; 1802b9b97703SBarry Smith aj += len; 180317ab2063SBarry Smith } 18046d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 18056d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 180617ab2063SBarry Smith 1807815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *B != A) { 1808416022c9SBarry Smith *B = C; 180917ab2063SBarry Smith } else { 1810eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,C);CHKERRQ(ierr); 181117ab2063SBarry Smith } 18123a40ed3dSBarry Smith PetscFunctionReturn(0); 181317ab2063SBarry Smith } 181417ab2063SBarry Smith 1815cd0d46ebSvictorle EXTERN_C_BEGIN 1816cd0d46ebSvictorle #undef __FUNCT__ 18175fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ" 18187087cfbeSBarry Smith PetscErrorCode MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 1819cd0d46ebSvictorle { 1820cd0d46ebSvictorle Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 182154f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 182254f21887SBarry Smith MatScalar *va,*vb; 18236849ba73SBarry Smith PetscErrorCode ierr; 182497f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 1825cd0d46ebSvictorle 1826cd0d46ebSvictorle PetscFunctionBegin; 1827cd0d46ebSvictorle bij = (Mat_SeqAIJ *) B->data; 1828cd0d46ebSvictorle 1829cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 1830cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 18315485867bSBarry Smith if (ma!=nb || na!=mb){ 18325485867bSBarry Smith *f = PETSC_FALSE; 18335485867bSBarry Smith PetscFunctionReturn(0); 18345485867bSBarry Smith } 1835cd0d46ebSvictorle aii = aij->i; bii = bij->i; 1836cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 1837cd0d46ebSvictorle va = aij->a; vb = bij->a; 183897f1f81fSBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 183997f1f81fSBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 1840cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 1841cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 1842cd0d46ebSvictorle 1843cd0d46ebSvictorle *f = PETSC_TRUE; 1844cd0d46ebSvictorle for (i=0; i<ma; i++) { 1845cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 184697f1f81fSBarry Smith PetscInt idc,idr; 18475485867bSBarry Smith PetscScalar vc,vr; 1848cd0d46ebSvictorle /* column/row index/value */ 18495485867bSBarry Smith idc = adx[aptr[i]]; 18505485867bSBarry Smith idr = bdx[bptr[idc]]; 18515485867bSBarry Smith vc = va[aptr[i]]; 18525485867bSBarry Smith vr = vb[bptr[idc]]; 18535485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 18545485867bSBarry Smith *f = PETSC_FALSE; 18555485867bSBarry Smith goto done; 1856cd0d46ebSvictorle } else { 18575485867bSBarry Smith aptr[i]++; 18585485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 1859cd0d46ebSvictorle } 1860cd0d46ebSvictorle } 1861cd0d46ebSvictorle } 1862cd0d46ebSvictorle done: 1863cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 18643aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 1865cd0d46ebSvictorle PetscFunctionReturn(0); 1866cd0d46ebSvictorle } 1867cd0d46ebSvictorle EXTERN_C_END 1868cd0d46ebSvictorle 18691cbb95d3SBarry Smith EXTERN_C_BEGIN 18701cbb95d3SBarry Smith #undef __FUNCT__ 18711cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ" 18727087cfbeSBarry Smith PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 18731cbb95d3SBarry Smith { 18741cbb95d3SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data; 187554f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 187654f21887SBarry Smith MatScalar *va,*vb; 18771cbb95d3SBarry Smith PetscErrorCode ierr; 18781cbb95d3SBarry Smith PetscInt ma,na,mb,nb, i; 18791cbb95d3SBarry Smith 18801cbb95d3SBarry Smith PetscFunctionBegin; 18811cbb95d3SBarry Smith bij = (Mat_SeqAIJ *) B->data; 18821cbb95d3SBarry Smith 18831cbb95d3SBarry Smith ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 18841cbb95d3SBarry Smith ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 18851cbb95d3SBarry Smith if (ma!=nb || na!=mb){ 18861cbb95d3SBarry Smith *f = PETSC_FALSE; 18871cbb95d3SBarry Smith PetscFunctionReturn(0); 18881cbb95d3SBarry Smith } 18891cbb95d3SBarry Smith aii = aij->i; bii = bij->i; 18901cbb95d3SBarry Smith adx = aij->j; bdx = bij->j; 18911cbb95d3SBarry Smith va = aij->a; vb = bij->a; 18921cbb95d3SBarry Smith ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr); 18931cbb95d3SBarry Smith ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr); 18941cbb95d3SBarry Smith for (i=0; i<ma; i++) aptr[i] = aii[i]; 18951cbb95d3SBarry Smith for (i=0; i<mb; i++) bptr[i] = bii[i]; 18961cbb95d3SBarry Smith 18971cbb95d3SBarry Smith *f = PETSC_TRUE; 18981cbb95d3SBarry Smith for (i=0; i<ma; i++) { 18991cbb95d3SBarry Smith while (aptr[i]<aii[i+1]) { 19001cbb95d3SBarry Smith PetscInt idc,idr; 19011cbb95d3SBarry Smith PetscScalar vc,vr; 19021cbb95d3SBarry Smith /* column/row index/value */ 19031cbb95d3SBarry Smith idc = adx[aptr[i]]; 19041cbb95d3SBarry Smith idr = bdx[bptr[idc]]; 19051cbb95d3SBarry Smith vc = va[aptr[i]]; 19061cbb95d3SBarry Smith vr = vb[bptr[idc]]; 19071cbb95d3SBarry Smith if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) { 19081cbb95d3SBarry Smith *f = PETSC_FALSE; 19091cbb95d3SBarry Smith goto done; 19101cbb95d3SBarry Smith } else { 19111cbb95d3SBarry Smith aptr[i]++; 19121cbb95d3SBarry Smith if (B || i!=idc) bptr[idc]++; 19131cbb95d3SBarry Smith } 19141cbb95d3SBarry Smith } 19151cbb95d3SBarry Smith } 19161cbb95d3SBarry Smith done: 19171cbb95d3SBarry Smith ierr = PetscFree(aptr);CHKERRQ(ierr); 19181cbb95d3SBarry Smith ierr = PetscFree(bptr);CHKERRQ(ierr); 19191cbb95d3SBarry Smith PetscFunctionReturn(0); 19201cbb95d3SBarry Smith } 19211cbb95d3SBarry Smith EXTERN_C_END 19221cbb95d3SBarry Smith 19239e29f15eSvictorle #undef __FUNCT__ 19249e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ" 1925ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 19269e29f15eSvictorle { 1927dfbe8321SBarry Smith PetscErrorCode ierr; 19289e29f15eSvictorle PetscFunctionBegin; 19295485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 19309e29f15eSvictorle PetscFunctionReturn(0); 19319e29f15eSvictorle } 19329e29f15eSvictorle 19334a2ae208SSatish Balay #undef __FUNCT__ 19341cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ" 1935ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 19361cbb95d3SBarry Smith { 19371cbb95d3SBarry Smith PetscErrorCode ierr; 19381cbb95d3SBarry Smith PetscFunctionBegin; 19391cbb95d3SBarry Smith ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 19401cbb95d3SBarry Smith PetscFunctionReturn(0); 19411cbb95d3SBarry Smith } 19421cbb95d3SBarry Smith 19431cbb95d3SBarry Smith #undef __FUNCT__ 19444a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ" 1945dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 194617ab2063SBarry Smith { 1947416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 194854f21887SBarry Smith PetscScalar *l,*r,x; 194954f21887SBarry Smith MatScalar *v; 1950dfbe8321SBarry Smith PetscErrorCode ierr; 1951d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj; 195217ab2063SBarry Smith 19533a40ed3dSBarry Smith PetscFunctionBegin; 195417ab2063SBarry Smith if (ll) { 19553ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 19563ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1957e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1958e32f2f54SBarry Smith if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 19591ebc52fbSHong Zhang ierr = VecGetArray(ll,&l);CHKERRQ(ierr); 1960416022c9SBarry Smith v = a->a; 196117ab2063SBarry Smith for (i=0; i<m; i++) { 196217ab2063SBarry Smith x = l[i]; 1963416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 196417ab2063SBarry Smith for (j=0; j<M; j++) { (*v++) *= x;} 196517ab2063SBarry Smith } 19661ebc52fbSHong Zhang ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr); 1967efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 196817ab2063SBarry Smith } 196917ab2063SBarry Smith if (rr) { 1970e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1971e32f2f54SBarry Smith if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 19721ebc52fbSHong Zhang ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1973416022c9SBarry Smith v = a->a; jj = a->j; 197417ab2063SBarry Smith for (i=0; i<nz; i++) { 1975bfeeae90SHong Zhang (*v++) *= r[*jj++]; 197617ab2063SBarry Smith } 19771ebc52fbSHong Zhang ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 1978efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 197917ab2063SBarry Smith } 198086c113feSBarry Smith a->idiagvalid = PETSC_FALSE; 198186c113feSBarry Smith a->ibdiagvalid = PETSC_FALSE; 19823a40ed3dSBarry Smith PetscFunctionReturn(0); 198317ab2063SBarry Smith } 198417ab2063SBarry Smith 19854a2ae208SSatish Balay #undef __FUNCT__ 19864a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ" 198797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 198817ab2063SBarry Smith { 1989db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 19906849ba73SBarry Smith PetscErrorCode ierr; 1991d0f46423SBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens; 199297f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 19935d0c19d7SBarry Smith const PetscInt *irow,*icol; 19945d0c19d7SBarry Smith PetscInt nrows,ncols; 199597f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 199654f21887SBarry Smith MatScalar *a_new,*mat_a; 1997416022c9SBarry Smith Mat C; 1998ace3abfcSBarry Smith PetscBool stride,sorted; 199917ab2063SBarry Smith 20003a40ed3dSBarry Smith PetscFunctionBegin; 200114ca34e6SBarry Smith ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr); 2002e32f2f54SBarry Smith if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 200314ca34e6SBarry Smith ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr); 2004e32f2f54SBarry Smith if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); 200599141d43SSatish Balay 200617ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 2007b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 2008b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 200917ab2063SBarry Smith 2010fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 2011251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr); 2012fee21e36SBarry Smith if (stride && step == 1) { 201302834360SBarry Smith /* special case of contiguous rows */ 20140e83c824SBarry Smith ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr); 201502834360SBarry Smith /* loop over new rows determining lens and starting points */ 201602834360SBarry Smith for (i=0; i<nrows; i++) { 2017bfeeae90SHong Zhang kstart = ai[irow[i]]; 2018a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 201902834360SBarry Smith for (k=kstart; k<kend; k++) { 2020bfeeae90SHong Zhang if (aj[k] >= first) { 202102834360SBarry Smith starts[i] = k; 202202834360SBarry Smith break; 202302834360SBarry Smith } 202402834360SBarry Smith } 2025a2744918SBarry Smith sum = 0; 202602834360SBarry Smith while (k < kend) { 2027bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 2028a2744918SBarry Smith sum++; 202902834360SBarry Smith } 2030a2744918SBarry Smith lens[i] = sum; 203102834360SBarry Smith } 203202834360SBarry Smith /* create submatrix */ 2033cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 203497f1f81fSBarry Smith PetscInt n_cols,n_rows; 203508480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 2036e32f2f54SBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 2037d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 203808480c60SBarry Smith C = *B; 20393a40ed3dSBarry Smith } else { 20403bef6203SJed Brown PetscInt rbs,cbs; 20417adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 2042f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 20433bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 20443bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 20453bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 20467adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2047ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 204808480c60SBarry Smith } 2049db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 2050db02288aSLois Curfman McInnes 205102834360SBarry Smith /* loop over rows inserting into submatrix */ 2052db02288aSLois Curfman McInnes a_new = c->a; 2053db02288aSLois Curfman McInnes j_new = c->j; 2054db02288aSLois Curfman McInnes i_new = c->i; 2055bfeeae90SHong Zhang 205602834360SBarry Smith for (i=0; i<nrows; i++) { 2057a2744918SBarry Smith ii = starts[i]; 2058a2744918SBarry Smith lensi = lens[i]; 2059a2744918SBarry Smith for (k=0; k<lensi; k++) { 2060a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 206102834360SBarry Smith } 206287828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 2063a2744918SBarry Smith a_new += lensi; 2064a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 2065a2744918SBarry Smith c->ilen[i] = lensi; 206602834360SBarry Smith } 20670e83c824SBarry Smith ierr = PetscFree2(lens,starts);CHKERRQ(ierr); 20683a40ed3dSBarry Smith } else { 206902834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 20700e83c824SBarry Smith ierr = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr); 207197f1f81fSBarry Smith ierr = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr); 20720e83c824SBarry Smith ierr = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 20734dcab191SBarry Smith for (i=0; i<ncols; i++) { 20744dcab191SBarry Smith #if defined(PETSC_USE_DEBUG) 20754dcab191SBarry Smith if (icol[i] >= oldcols) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requesting column beyond largest column icol[%D] %D <= A->cmap->n %D",i,icol[i],oldcols); 20764dcab191SBarry Smith #endif 20774dcab191SBarry Smith smap[icol[i]] = i+1; 20784dcab191SBarry Smith } 20794dcab191SBarry Smith 208002834360SBarry Smith /* determine lens of each row */ 208102834360SBarry Smith for (i=0; i<nrows; i++) { 2082bfeeae90SHong Zhang kstart = ai[irow[i]]; 208302834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 208402834360SBarry Smith lens[i] = 0; 208502834360SBarry Smith for (k=kstart; k<kend; k++) { 2086bfeeae90SHong Zhang if (smap[aj[k]]) { 208702834360SBarry Smith lens[i]++; 208802834360SBarry Smith } 208902834360SBarry Smith } 209002834360SBarry Smith } 209117ab2063SBarry Smith /* Create and fill new matrix */ 2092a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 2093ace3abfcSBarry Smith PetscBool equal; 20940f5bd95cSBarry Smith 209599141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 2096e32f2f54SBarry Smith if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 2097d0f46423SBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr); 20980f5bd95cSBarry Smith if (!equal) { 2099e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 210099141d43SSatish Balay } 2101d0f46423SBarry Smith ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 210208480c60SBarry Smith C = *B; 21033a40ed3dSBarry Smith } else { 21043bef6203SJed Brown PetscInt rbs,cbs; 21057adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr); 2106f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 21073bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 21083bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 21093bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 21107adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2111ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 211208480c60SBarry Smith } 211399141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 211417ab2063SBarry Smith for (i=0; i<nrows; i++) { 211599141d43SSatish Balay row = irow[i]; 2116bfeeae90SHong Zhang kstart = ai[row]; 211799141d43SSatish Balay kend = kstart + a->ilen[row]; 2118bfeeae90SHong Zhang mat_i = c->i[i]; 211999141d43SSatish Balay mat_j = c->j + mat_i; 212099141d43SSatish Balay mat_a = c->a + mat_i; 212199141d43SSatish Balay mat_ilen = c->ilen + i; 212217ab2063SBarry Smith for (k=kstart; k<kend; k++) { 2123bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 2124ed480e8bSBarry Smith *mat_j++ = tcol - 1; 212599141d43SSatish Balay *mat_a++ = a->a[k]; 212699141d43SSatish Balay (*mat_ilen)++; 212799141d43SSatish Balay 212817ab2063SBarry Smith } 212917ab2063SBarry Smith } 213017ab2063SBarry Smith } 213102834360SBarry Smith /* Free work space */ 213202834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 2133606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 2134606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 213502834360SBarry Smith } 21366d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21376d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 213817ab2063SBarry Smith 213917ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 2140416022c9SBarry Smith *B = C; 21413a40ed3dSBarry Smith PetscFunctionReturn(0); 214217ab2063SBarry Smith } 214317ab2063SBarry Smith 21441df811f5SHong Zhang #undef __FUNCT__ 214582d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ" 2146fc08c53fSHong Zhang PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat* subMat) 214782d44351SHong Zhang { 214882d44351SHong Zhang PetscErrorCode ierr; 214982d44351SHong Zhang Mat B; 215082d44351SHong Zhang 215182d44351SHong Zhang PetscFunctionBegin; 215282d44351SHong Zhang ierr = MatCreate(subComm,&B);CHKERRQ(ierr); 215382d44351SHong Zhang ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr); 2154a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,mat->rmap->bs,mat->cmap->bs); CHKERRQ(ierr); 215582d44351SHong Zhang ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 215682d44351SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr); 215782d44351SHong Zhang *subMat = B; 215882d44351SHong Zhang PetscFunctionReturn(0); 215982d44351SHong Zhang } 216082d44351SHong Zhang 216182d44351SHong Zhang #undef __FUNCT__ 21624a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ" 21630481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info) 2164a871dcd8SBarry Smith { 216563b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2166dfbe8321SBarry Smith PetscErrorCode ierr; 216763b91edcSBarry Smith Mat outA; 2168ace3abfcSBarry Smith PetscBool row_identity,col_identity; 216963b91edcSBarry Smith 21703a40ed3dSBarry Smith PetscFunctionBegin; 2171e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 21721df811f5SHong Zhang 2173b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 2174b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 2175a871dcd8SBarry Smith 217663b91edcSBarry Smith outA = inA; 2177d5f3da31SBarry Smith outA->factortype = MAT_FACTOR_LU; 2178c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 21796bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 2180c3122656SLisandro Dalcin a->row = row; 2181c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 21826bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 2183c3122656SLisandro Dalcin a->col = col; 218463b91edcSBarry Smith 218536db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 21866bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 21874c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 218852e6d16bSBarry Smith ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr); 2189f0ec6fceSSatish Balay 219094a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 2191d0f46423SBarry Smith ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr); 2192d0f46423SBarry Smith ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 219394a9d846SBarry Smith } 219463b91edcSBarry Smith 2195f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 2196137fb511SHong Zhang if (row_identity && col_identity) { 2197ad04f41aSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr); 2198137fb511SHong Zhang } else { 2199719d5645SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr); 2200137fb511SHong Zhang } 22013a40ed3dSBarry Smith PetscFunctionReturn(0); 2202a871dcd8SBarry Smith } 2203a871dcd8SBarry Smith 22044a2ae208SSatish Balay #undef __FUNCT__ 22054a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ" 2206f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 2207f0b747eeSBarry Smith { 2208f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2209f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 2210efee365bSSatish Balay PetscErrorCode ierr; 22110805154bSBarry Smith PetscBLASInt one = 1,bnz = PetscBLASIntCast(a->nz); 22123a40ed3dSBarry Smith 22133a40ed3dSBarry Smith PetscFunctionBegin; 2214f4df32b1SMatthew Knepley BLASscal_(&bnz,&oalpha,a->a,&one); 2215efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 221686c113feSBarry Smith a->idiagvalid = PETSC_FALSE; 221786c113feSBarry Smith a->ibdiagvalid = PETSC_FALSE; 22183a40ed3dSBarry Smith PetscFunctionReturn(0); 2219f0b747eeSBarry Smith } 2220f0b747eeSBarry Smith 22214a2ae208SSatish Balay #undef __FUNCT__ 22224a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ" 222397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 2224cddf8d76SBarry Smith { 2225dfbe8321SBarry Smith PetscErrorCode ierr; 222697f1f81fSBarry Smith PetscInt i; 2227cddf8d76SBarry Smith 22283a40ed3dSBarry Smith PetscFunctionBegin; 2229cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 2230b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr); 2231cddf8d76SBarry Smith } 2232cddf8d76SBarry Smith 2233cddf8d76SBarry Smith for (i=0; i<n; i++) { 22346a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 2235cddf8d76SBarry Smith } 22363a40ed3dSBarry Smith PetscFunctionReturn(0); 2237cddf8d76SBarry Smith } 2238cddf8d76SBarry Smith 22394a2ae208SSatish Balay #undef __FUNCT__ 22404a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ" 224197f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 22424dcbc457SBarry Smith { 2243e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 22446849ba73SBarry Smith PetscErrorCode ierr; 22455d0c19d7SBarry Smith PetscInt row,i,j,k,l,m,n,*nidx,isz,val; 22465d0c19d7SBarry Smith const PetscInt *idx; 224797f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 2248f1af5d2fSBarry Smith PetscBT table; 2249bbd702dbSSatish Balay 22503a40ed3dSBarry Smith PetscFunctionBegin; 2251d0f46423SBarry Smith m = A->rmap->n; 2252e4d965acSSatish Balay ai = a->i; 2253bfeeae90SHong Zhang aj = a->j; 22548a047759SSatish Balay 2255e32f2f54SBarry Smith if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 225606763907SSatish Balay 225797f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr); 225853b8de81SBarry Smith ierr = PetscBTCreate(m,&table);CHKERRQ(ierr); 225906763907SSatish Balay 2260e4d965acSSatish Balay for (i=0; i<is_max; i++) { 2261b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 2262e4d965acSSatish Balay isz = 0; 22636831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 2264e4d965acSSatish Balay 2265e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 22664dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 2267b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 2268e4d965acSSatish Balay 2269dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 2270e4d965acSSatish Balay for (j=0; j<n ; ++j){ 2271f1af5d2fSBarry Smith if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];} 22724dcbc457SBarry Smith } 227306763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 22746bf464f9SBarry Smith ierr = ISDestroy(&is[i]);CHKERRQ(ierr); 2275e4d965acSSatish Balay 227604a348a9SBarry Smith k = 0; 227704a348a9SBarry Smith for (j=0; j<ov; j++){ /* for each overlap */ 227804a348a9SBarry Smith n = isz; 227906763907SSatish Balay for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 2280e4d965acSSatish Balay row = nidx[k]; 2281e4d965acSSatish Balay start = ai[row]; 2282e4d965acSSatish Balay end = ai[row+1]; 228304a348a9SBarry Smith for (l = start; l<end ; l++){ 2284efb16452SHong Zhang val = aj[l] ; 2285f1af5d2fSBarry Smith if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;} 2286e4d965acSSatish Balay } 2287e4d965acSSatish Balay } 2288e4d965acSSatish Balay } 228970b3c8c7SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr); 2290e4d965acSSatish Balay } 229194bacf5dSBarry Smith ierr = PetscBTDestroy(&table);CHKERRQ(ierr); 2292606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 22933a40ed3dSBarry Smith PetscFunctionReturn(0); 22944dcbc457SBarry Smith } 229517ab2063SBarry Smith 22960513a670SBarry Smith /* -------------------------------------------------------------- */ 22974a2ae208SSatish Balay #undef __FUNCT__ 22984a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ" 2299dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 23000513a670SBarry Smith { 23010513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 23026849ba73SBarry Smith PetscErrorCode ierr; 23033b98c0a2SBarry Smith PetscInt i,nz = 0,m = A->rmap->n,n = A->cmap->n; 23045d0c19d7SBarry Smith const PetscInt *row,*col; 23055d0c19d7SBarry Smith PetscInt *cnew,j,*lens; 230656cd22aeSBarry Smith IS icolp,irowp; 23073b98c0a2SBarry Smith PetscInt *cwork = PETSC_NULL; 23083b98c0a2SBarry Smith PetscScalar *vwork = PETSC_NULL; 23090513a670SBarry Smith 23103a40ed3dSBarry Smith PetscFunctionBegin; 23114c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 231256cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 23134c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 231456cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 23150513a670SBarry Smith 23160513a670SBarry Smith /* determine lengths of permuted rows */ 231797f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr); 23180513a670SBarry Smith for (i=0; i<m; i++) { 23190513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 23200513a670SBarry Smith } 23217adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 2322f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 2323a2f3521dSMark F. Adams ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 23247adad957SLisandro Dalcin ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2325ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 2326606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 23270513a670SBarry Smith 232897f1f81fSBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr); 23290513a670SBarry Smith for (i=0; i<m; i++) { 233032ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 23310513a670SBarry Smith for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];} 2332cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 233332ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 23340513a670SBarry Smith } 2335606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 23363c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 23370513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 23380513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 233956cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 234056cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 23416bf464f9SBarry Smith ierr = ISDestroy(&irowp);CHKERRQ(ierr); 23426bf464f9SBarry Smith ierr = ISDestroy(&icolp);CHKERRQ(ierr); 23433a40ed3dSBarry Smith PetscFunctionReturn(0); 23440513a670SBarry Smith } 23450513a670SBarry Smith 23464a2ae208SSatish Balay #undef __FUNCT__ 23474a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ" 2348dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 2349cb5b572fSBarry Smith { 2350dfbe8321SBarry Smith PetscErrorCode ierr; 2351cb5b572fSBarry Smith 2352cb5b572fSBarry Smith PetscFunctionBegin; 235333f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 235433f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 2355be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2356be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 2357be6bf707SBarry Smith 2358700c5bfcSBarry Smith if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different"); 2359d0f46423SBarry Smith ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 2360cb5b572fSBarry Smith } else { 2361cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2362cb5b572fSBarry Smith } 2363cb5b572fSBarry Smith PetscFunctionReturn(0); 2364cb5b572fSBarry Smith } 2365cb5b572fSBarry Smith 23664a2ae208SSatish Balay #undef __FUNCT__ 23674994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ" 23684994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A) 2369273d9f13SBarry Smith { 2370dfbe8321SBarry Smith PetscErrorCode ierr; 2371273d9f13SBarry Smith 2372273d9f13SBarry Smith PetscFunctionBegin; 2373ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 2374273d9f13SBarry Smith PetscFunctionReturn(0); 2375273d9f13SBarry Smith } 2376273d9f13SBarry Smith 23774a2ae208SSatish Balay #undef __FUNCT__ 23784a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ" 2379a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 23806c0721eeSBarry Smith { 23816c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 23826c0721eeSBarry Smith PetscFunctionBegin; 23836c0721eeSBarry Smith *array = a->a; 23846c0721eeSBarry Smith PetscFunctionReturn(0); 23856c0721eeSBarry Smith } 23866c0721eeSBarry Smith 23874a2ae208SSatish Balay #undef __FUNCT__ 23884a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ" 2389dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 23906c0721eeSBarry Smith { 23916c0721eeSBarry Smith PetscFunctionBegin; 23926c0721eeSBarry Smith PetscFunctionReturn(0); 23936c0721eeSBarry Smith } 2394273d9f13SBarry Smith 2395ee4f033dSBarry Smith #undef __FUNCT__ 2396ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ" 2397dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 2398ee4f033dSBarry Smith { 23996849ba73SBarry Smith PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f; 24006849ba73SBarry Smith PetscErrorCode ierr; 240197f1f81fSBarry Smith PetscInt k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2; 2402efb30889SBarry Smith PetscScalar dx,*y,*xx,*w3_array; 240387828ca2SBarry Smith PetscScalar *vscale_array; 2404ee4f033dSBarry Smith PetscReal epsilon = coloring->error_rel,umin = coloring->umin; 2405ee4f033dSBarry Smith Vec w1,w2,w3; 2406ee4f033dSBarry Smith void *fctx = coloring->fctx; 2407ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 2408ee4f033dSBarry Smith 2409ee4f033dSBarry Smith PetscFunctionBegin; 2410ee4f033dSBarry Smith if (!coloring->w1) { 2411ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr); 241252e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr); 2413ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr); 241452e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr); 2415ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 241652e6d16bSBarry Smith ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr); 2417ee4f033dSBarry Smith } 2418ee4f033dSBarry Smith w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 2419ee4f033dSBarry Smith 2420ee4f033dSBarry Smith ierr = MatSetUnfactored(J);CHKERRQ(ierr); 2421acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr); 2422ee4f033dSBarry Smith if (flg) { 2423ae15b995SBarry Smith ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr); 2424ee4f033dSBarry Smith } else { 2425ace3abfcSBarry Smith PetscBool assembled; 24260b9b6f31SBarry Smith ierr = MatAssembled(J,&assembled);CHKERRQ(ierr); 24270b9b6f31SBarry Smith if (assembled) { 2428ee4f033dSBarry Smith ierr = MatZeroEntries(J);CHKERRQ(ierr); 2429ee4f033dSBarry Smith } 24300b9b6f31SBarry Smith } 2431ee4f033dSBarry Smith 2432ee4f033dSBarry Smith ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr); 2433ee4f033dSBarry Smith ierr = VecGetSize(x1,&N);CHKERRQ(ierr); 2434ee4f033dSBarry Smith 2435ee4f033dSBarry Smith /* 243698bf7241SBarry Smith This is a horrible, horrible, hack. 2437ee4f033dSBarry Smith */ 2438ee4f033dSBarry Smith if (coloring->F) { 2439ee4f033dSBarry Smith ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr); 2440ee4f033dSBarry Smith ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr); 2441ee4f033dSBarry Smith if (m1 != m2) { 2442ee4f033dSBarry Smith coloring->F = 0; 2443ee4f033dSBarry Smith } 2444ee4f033dSBarry Smith } 2445ee4f033dSBarry Smith 2446ee4f033dSBarry Smith if (coloring->F) { 2447ee4f033dSBarry Smith w1 = coloring->F; 2448ee4f033dSBarry Smith coloring->F = 0; 2449ee4f033dSBarry Smith } else { 245066f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2451ee4f033dSBarry Smith ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 245266f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2453ee4f033dSBarry Smith } 2454ee4f033dSBarry Smith 2455ee4f033dSBarry Smith /* 2456ee4f033dSBarry Smith Compute all the scale factors and share with other processors 2457ee4f033dSBarry Smith */ 24581ebc52fbSHong Zhang ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start; 24591ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start; 2460ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 2461ee4f033dSBarry Smith /* 2462ee4f033dSBarry Smith Loop over each column associated with color adding the 2463ee4f033dSBarry Smith perturbation to the vector w3. 2464ee4f033dSBarry Smith */ 2465ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2466ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2467ee4f033dSBarry Smith dx = xx[col]; 2468ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 2469ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2470ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2471ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2472ee4f033dSBarry Smith #else 2473ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2474ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2475ee4f033dSBarry Smith #endif 2476ee4f033dSBarry Smith dx *= epsilon; 2477ee4f033dSBarry Smith vscale_array[col] = 1.0/dx; 2478ee4f033dSBarry Smith } 2479ee4f033dSBarry Smith } 24801ebc52fbSHong Zhang vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2481ee4f033dSBarry Smith ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2482ee4f033dSBarry Smith ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2483ee4f033dSBarry Smith 2484ee4f033dSBarry Smith /* ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD); 2485ee4f033dSBarry Smith ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/ 2486ee4f033dSBarry Smith 2487ee4f033dSBarry Smith if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow; 2488ee4f033dSBarry Smith else vscaleforrow = coloring->columnsforrow; 2489ee4f033dSBarry Smith 24901ebc52fbSHong Zhang ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2491ee4f033dSBarry Smith /* 2492ee4f033dSBarry Smith Loop over each color 2493ee4f033dSBarry Smith */ 2494ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 249549b058dcSBarry Smith coloring->currentcolor = k; 2496ee4f033dSBarry Smith ierr = VecCopy(x1,w3);CHKERRQ(ierr); 24971ebc52fbSHong Zhang ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start; 2498ee4f033dSBarry Smith /* 2499ee4f033dSBarry Smith Loop over each column associated with color adding the 2500ee4f033dSBarry Smith perturbation to the vector w3. 2501ee4f033dSBarry Smith */ 2502ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 2503ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 2504ee4f033dSBarry Smith dx = xx[col]; 25055b8514ebSBarry Smith if (dx == 0.0) dx = 1.0; 2506ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 2507ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 2508ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 2509ee4f033dSBarry Smith #else 2510ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 2511ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 2512ee4f033dSBarry Smith #endif 2513ee4f033dSBarry Smith dx *= epsilon; 2514e32f2f54SBarry Smith if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter"); 2515ee4f033dSBarry Smith w3_array[col] += dx; 2516ee4f033dSBarry Smith } 25171ebc52fbSHong Zhang w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 2518ee4f033dSBarry Smith 2519ee4f033dSBarry Smith /* 2520ee4f033dSBarry Smith Evaluate function at x1 + dx (here dx is a vector of perturbations) 2521ee4f033dSBarry Smith */ 2522ee4f033dSBarry Smith 252366f9b7ceSBarry Smith ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2524ee4f033dSBarry Smith ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 252566f9b7ceSBarry Smith ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr); 2526efb30889SBarry Smith ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr); 2527ee4f033dSBarry Smith 2528ee4f033dSBarry Smith /* 2529ee4f033dSBarry Smith Loop over rows of vector, putting results into Jacobian matrix 2530ee4f033dSBarry Smith */ 25311ebc52fbSHong Zhang ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 2532ee4f033dSBarry Smith for (l=0; l<coloring->nrows[k]; l++) { 2533ee4f033dSBarry Smith row = coloring->rows[k][l]; 2534ee4f033dSBarry Smith col = coloring->columnsforrow[k][l]; 2535ee4f033dSBarry Smith y[row] *= vscale_array[vscaleforrow[k][l]]; 2536ee4f033dSBarry Smith srow = row + start; 2537ee4f033dSBarry Smith ierr = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 2538ee4f033dSBarry Smith } 25391ebc52fbSHong Zhang ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 2540ee4f033dSBarry Smith } 254149b058dcSBarry Smith coloring->currentcolor = k; 25421ebc52fbSHong Zhang ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 25431ebc52fbSHong Zhang xx = xx + start; ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 2544ee4f033dSBarry Smith ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2545ee4f033dSBarry Smith ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2546ee4f033dSBarry Smith PetscFunctionReturn(0); 2547ee4f033dSBarry Smith } 2548ee4f033dSBarry Smith 25498229c054SShri Abhyankar /* 25508229c054SShri Abhyankar Computes the number of nonzeros per row needed for preallocation when X and Y 25518229c054SShri Abhyankar have different nonzero structure. 25528229c054SShri Abhyankar */ 2553ac90fabeSBarry Smith #undef __FUNCT__ 25548229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ" 25558229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz) 2556ec7775f6SShri Abhyankar { 25578229c054SShri Abhyankar PetscInt i,m=Y->rmap->N; 2558ec7775f6SShri Abhyankar Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 2559ec7775f6SShri Abhyankar Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 2560ec7775f6SShri Abhyankar const PetscInt *xi = x->i,*yi = y->i; 2561ec7775f6SShri Abhyankar 2562ec7775f6SShri Abhyankar PetscFunctionBegin; 2563ec7775f6SShri Abhyankar /* Set the number of nonzeros in the new matrix */ 2564ec7775f6SShri Abhyankar for(i=0; i<m; i++) { 25658af7cee1SJed Brown PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i]; 25668af7cee1SJed Brown const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i]; 25678af7cee1SJed Brown nnz[i] = 0; 25688af7cee1SJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 25698af7cee1SJed Brown for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */ 25708af7cee1SJed Brown if (k<nzy && yj[k]==xj[j]) k++; /* Skip duplicate */ 25718af7cee1SJed Brown nnz[i]++; 25728af7cee1SJed Brown } 25738af7cee1SJed Brown for (; k<nzy; k++) nnz[i]++; 2574ec7775f6SShri Abhyankar } 2575ec7775f6SShri Abhyankar PetscFunctionReturn(0); 2576ec7775f6SShri Abhyankar } 2577ec7775f6SShri Abhyankar 2578ec7775f6SShri Abhyankar #undef __FUNCT__ 2579ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ" 2580f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2581ac90fabeSBarry Smith { 2582dfbe8321SBarry Smith PetscErrorCode ierr; 258397f1f81fSBarry Smith PetscInt i; 2584ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data; 25850805154bSBarry Smith PetscBLASInt one=1,bnz = PetscBLASIntCast(x->nz); 2586ac90fabeSBarry Smith 2587ac90fabeSBarry Smith PetscFunctionBegin; 2588ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2589f4df32b1SMatthew Knepley PetscScalar alpha = a; 2590f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 259186c113feSBarry Smith y->idiagvalid = PETSC_FALSE; 259286c113feSBarry Smith y->ibdiagvalid = PETSC_FALSE; 2593c537a176SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 2594a30b2313SHong Zhang if (y->xtoy && y->XtoY != X) { 2595a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 25966bf464f9SBarry Smith ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr); 2597a30b2313SHong Zhang } 2598a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2599d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr); 2600a30b2313SHong Zhang y->XtoY = X; 2601407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr); 2602c537a176SHong Zhang } 2603f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 26048644d96aSMark F. Adams ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/(y->nz+1));CHKERRQ(ierr); 2605ac90fabeSBarry Smith } else { 26068229c054SShri Abhyankar Mat B; 26078229c054SShri Abhyankar PetscInt *nnz; 260816b2e9dcSShri Abhyankar ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr); 2609ec7775f6SShri Abhyankar ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr); 2610bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 26114aa94f47SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 2612a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr); 2613176df525SBarry Smith ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr); 26148229c054SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr); 2615ecd8bba6SJed Brown ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr); 2616ec7775f6SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 2617ec7775f6SShri Abhyankar ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr); 26188229c054SShri Abhyankar ierr = PetscFree(nnz);CHKERRQ(ierr); 2619ac90fabeSBarry Smith } 2620ac90fabeSBarry Smith PetscFunctionReturn(0); 2621ac90fabeSBarry Smith } 2622ac90fabeSBarry Smith 2623521d7252SBarry Smith #undef __FUNCT__ 2624354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ" 26257087cfbeSBarry Smith PetscErrorCode MatConjugate_SeqAIJ(Mat mat) 2626354c94deSBarry Smith { 2627354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2628354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2629354c94deSBarry Smith PetscInt i,nz; 2630354c94deSBarry Smith PetscScalar *a; 2631354c94deSBarry Smith 2632354c94deSBarry Smith PetscFunctionBegin; 2633354c94deSBarry Smith nz = aij->nz; 2634354c94deSBarry Smith a = aij->a; 2635354c94deSBarry Smith for (i=0; i<nz; i++) { 2636354c94deSBarry Smith a[i] = PetscConj(a[i]); 2637354c94deSBarry Smith } 2638354c94deSBarry Smith #else 2639354c94deSBarry Smith PetscFunctionBegin; 2640354c94deSBarry Smith #endif 2641354c94deSBarry Smith PetscFunctionReturn(0); 2642354c94deSBarry Smith } 2643354c94deSBarry Smith 2644e34fafa9SBarry Smith #undef __FUNCT__ 2645985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ" 2646985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2647e34fafa9SBarry Smith { 2648e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2649e34fafa9SBarry Smith PetscErrorCode ierr; 2650d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2651e34fafa9SBarry Smith PetscReal atmp; 2652985db425SBarry Smith PetscScalar *x; 2653e34fafa9SBarry Smith MatScalar *aa; 2654e34fafa9SBarry Smith 2655e34fafa9SBarry Smith PetscFunctionBegin; 2656e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2657e34fafa9SBarry Smith aa = a->a; 2658e34fafa9SBarry Smith ai = a->i; 2659e34fafa9SBarry Smith aj = a->j; 2660e34fafa9SBarry Smith 2661985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2662e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2663e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2664e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2665e34fafa9SBarry Smith for (i=0; i<m; i++) { 2666e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 26679189402eSHong Zhang x[i] = 0.0; 2668e34fafa9SBarry Smith for (j=0; j<ncols; j++){ 2669985db425SBarry Smith atmp = PetscAbsScalar(*aa); 2670985db425SBarry Smith if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2671985db425SBarry Smith aa++; aj++; 2672985db425SBarry Smith } 2673985db425SBarry Smith } 2674985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2675985db425SBarry Smith PetscFunctionReturn(0); 2676985db425SBarry Smith } 2677985db425SBarry Smith 2678985db425SBarry Smith #undef __FUNCT__ 2679985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ" 2680985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2681985db425SBarry Smith { 2682985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2683985db425SBarry Smith PetscErrorCode ierr; 2684d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2685985db425SBarry Smith PetscScalar *x; 2686985db425SBarry Smith MatScalar *aa; 2687985db425SBarry Smith 2688985db425SBarry Smith PetscFunctionBegin; 2689e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2690985db425SBarry Smith aa = a->a; 2691985db425SBarry Smith ai = a->i; 2692985db425SBarry Smith aj = a->j; 2693985db425SBarry Smith 2694985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2695985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2696985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2697e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2698985db425SBarry Smith for (i=0; i<m; i++) { 2699985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2700d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2701985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2702985db425SBarry Smith } else { /* row is sparse so already KNOW maximum is 0.0 or higher */ 2703985db425SBarry Smith x[i] = 0.0; 2704985db425SBarry Smith if (idx) { 2705985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2706985db425SBarry Smith for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */ 2707985db425SBarry Smith if (aj[j] > j) { 2708985db425SBarry Smith idx[i] = j; 2709985db425SBarry Smith break; 2710985db425SBarry Smith } 2711985db425SBarry Smith } 2712985db425SBarry Smith } 2713985db425SBarry Smith } 2714985db425SBarry Smith for (j=0; j<ncols; j++){ 2715985db425SBarry Smith if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2716985db425SBarry Smith aa++; aj++; 2717985db425SBarry Smith } 2718985db425SBarry Smith } 2719985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2720985db425SBarry Smith PetscFunctionReturn(0); 2721985db425SBarry Smith } 2722985db425SBarry Smith 2723985db425SBarry Smith #undef __FUNCT__ 2724c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ" 2725c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2726c87e5d42SMatthew Knepley { 2727c87e5d42SMatthew Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2728c87e5d42SMatthew Knepley PetscErrorCode ierr; 2729c87e5d42SMatthew Knepley PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2730c87e5d42SMatthew Knepley PetscReal atmp; 2731c87e5d42SMatthew Knepley PetscScalar *x; 2732c87e5d42SMatthew Knepley MatScalar *aa; 2733c87e5d42SMatthew Knepley 2734c87e5d42SMatthew Knepley PetscFunctionBegin; 2735e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2736c87e5d42SMatthew Knepley aa = a->a; 2737c87e5d42SMatthew Knepley ai = a->i; 2738c87e5d42SMatthew Knepley aj = a->j; 2739c87e5d42SMatthew Knepley 2740c87e5d42SMatthew Knepley ierr = VecSet(v,0.0);CHKERRQ(ierr); 2741c87e5d42SMatthew Knepley ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2742c87e5d42SMatthew Knepley ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 27433bb78c5cSMatthew G Knepley if (n != A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %d vs. %d rows", A->rmap->n, n); 2744c87e5d42SMatthew Knepley for (i=0; i<m; i++) { 2745c87e5d42SMatthew Knepley ncols = ai[1] - ai[0]; ai++; 2746289a08f5SMatthew Knepley if (ncols) { 2747289a08f5SMatthew Knepley /* Get first nonzero */ 2748289a08f5SMatthew Knepley for(j = 0; j < ncols; j++) { 2749289a08f5SMatthew Knepley atmp = PetscAbsScalar(aa[j]); 2750289a08f5SMatthew Knepley if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;} 2751289a08f5SMatthew Knepley } 275212431cb0SMatthew G Knepley if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;} 2753289a08f5SMatthew Knepley } else { 2754289a08f5SMatthew Knepley x[i] = 0.0; if (idx) idx[i] = 0; 2755289a08f5SMatthew Knepley } 2756c87e5d42SMatthew Knepley for(j = 0; j < ncols; j++) { 2757c87e5d42SMatthew Knepley atmp = PetscAbsScalar(*aa); 2758289a08f5SMatthew Knepley if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2759c87e5d42SMatthew Knepley aa++; aj++; 2760c87e5d42SMatthew Knepley } 2761c87e5d42SMatthew Knepley } 2762c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2763c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2764c87e5d42SMatthew Knepley } 2765c87e5d42SMatthew Knepley 2766c87e5d42SMatthew Knepley #undef __FUNCT__ 2767985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ" 2768985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2769985db425SBarry Smith { 2770985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2771985db425SBarry Smith PetscErrorCode ierr; 2772d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2773985db425SBarry Smith PetscScalar *x; 2774985db425SBarry Smith MatScalar *aa; 2775985db425SBarry Smith 2776985db425SBarry Smith PetscFunctionBegin; 2777e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2778985db425SBarry Smith aa = a->a; 2779985db425SBarry Smith ai = a->i; 2780985db425SBarry Smith aj = a->j; 2781985db425SBarry Smith 2782985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2783985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2784985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2785e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2786985db425SBarry Smith for (i=0; i<m; i++) { 2787985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2788d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2789985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2790985db425SBarry Smith } else { /* row is sparse so already KNOW minimum is 0.0 or lower */ 2791985db425SBarry Smith x[i] = 0.0; 2792985db425SBarry Smith if (idx) { /* find first implicit 0.0 in the row */ 2793985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2794985db425SBarry Smith for (j=0;j<ncols;j++) { 2795985db425SBarry Smith if (aj[j] > j) { 2796985db425SBarry Smith idx[i] = j; 2797985db425SBarry Smith break; 2798985db425SBarry Smith } 2799985db425SBarry Smith } 2800985db425SBarry Smith } 2801985db425SBarry Smith } 2802985db425SBarry Smith for (j=0; j<ncols; j++){ 2803985db425SBarry Smith if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2804985db425SBarry Smith aa++; aj++; 2805e34fafa9SBarry Smith } 2806e34fafa9SBarry Smith } 2807e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2808e34fafa9SBarry Smith PetscFunctionReturn(0); 2809e34fafa9SBarry Smith } 2810bbead8a2SBarry Smith 2811bbead8a2SBarry Smith #include <petscblaslapack.h> 2812bbead8a2SBarry Smith #include <../src/mat/blockinvert.h> 2813bbead8a2SBarry Smith 2814bbead8a2SBarry Smith #undef __FUNCT__ 2815bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ" 2816713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values) 2817bbead8a2SBarry Smith { 2818bbead8a2SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 2819bbead8a2SBarry Smith PetscErrorCode ierr; 282034fc4b71SJed Brown PetscInt i,bs = A->rmap->bs,mbs = A->rmap->n/A->rmap->bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j; 2821bbead8a2SBarry Smith MatScalar *diag,work[25],*v_work; 2822bbead8a2SBarry Smith PetscReal shift = 0.0; 2823bbead8a2SBarry Smith 2824bbead8a2SBarry Smith PetscFunctionBegin; 28254a0d0026SBarry Smith if (a->ibdiagvalid) { 28264a0d0026SBarry Smith if (values) *values = a->ibdiag; 28274a0d0026SBarry Smith PetscFunctionReturn(0); 28284a0d0026SBarry Smith } 2829bbead8a2SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 2830bbead8a2SBarry Smith if (!a->ibdiag) { 2831bbead8a2SBarry Smith ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr); 2832bbead8a2SBarry Smith ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr); 2833bbead8a2SBarry Smith } 2834bbead8a2SBarry Smith diag = a->ibdiag; 2835bbead8a2SBarry Smith if (values) *values = a->ibdiag; 2836bbead8a2SBarry Smith /* factor and invert each block */ 2837bbead8a2SBarry Smith switch (bs){ 2838bbead8a2SBarry Smith case 1: 2839bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2840bbead8a2SBarry Smith ierr = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr); 2841bbead8a2SBarry Smith diag[i] = (PetscScalar)1.0 / (diag[i] + shift); 2842bbead8a2SBarry Smith } 2843bbead8a2SBarry Smith break; 2844bbead8a2SBarry Smith case 2: 2845bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2846bbead8a2SBarry Smith ij[0] = 2*i; ij[1] = 2*i + 1; 2847bbead8a2SBarry Smith ierr = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr); 284896b95a6bSBarry Smith ierr = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr); 284996b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr); 2850bbead8a2SBarry Smith diag += 4; 2851bbead8a2SBarry Smith } 2852bbead8a2SBarry Smith break; 2853bbead8a2SBarry Smith case 3: 2854bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2855bbead8a2SBarry Smith ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2; 2856bbead8a2SBarry Smith ierr = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr); 285796b95a6bSBarry Smith ierr = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr); 285896b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr); 2859bbead8a2SBarry Smith diag += 9; 2860bbead8a2SBarry Smith } 2861bbead8a2SBarry Smith break; 2862bbead8a2SBarry Smith case 4: 2863bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2864bbead8a2SBarry Smith ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3; 2865bbead8a2SBarry Smith ierr = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr); 286696b95a6bSBarry Smith ierr = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr); 286796b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr); 2868bbead8a2SBarry Smith diag += 16; 2869bbead8a2SBarry Smith } 2870bbead8a2SBarry Smith break; 2871bbead8a2SBarry Smith case 5: 2872bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2873bbead8a2SBarry Smith ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4; 2874bbead8a2SBarry Smith ierr = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr); 287596b95a6bSBarry Smith ierr = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr); 287696b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr); 2877bbead8a2SBarry Smith diag += 25; 2878bbead8a2SBarry Smith } 2879bbead8a2SBarry Smith break; 2880bbead8a2SBarry Smith case 6: 2881bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2882bbead8a2SBarry Smith ij[0] = 6*i; ij[1] = 6*i + 1; ij[2] = 6*i + 2; ij[3] = 6*i + 3; ij[4] = 6*i + 4; ij[5] = 6*i + 5; 2883bbead8a2SBarry Smith ierr = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr); 288496b95a6bSBarry Smith ierr = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr); 288596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr); 2886bbead8a2SBarry Smith diag += 36; 2887bbead8a2SBarry Smith } 2888bbead8a2SBarry Smith break; 2889bbead8a2SBarry Smith case 7: 2890bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2891bbead8a2SBarry Smith ij[0] = 7*i; ij[1] = 7*i + 1; ij[2] = 7*i + 2; ij[3] = 7*i + 3; ij[4] = 7*i + 4; ij[5] = 7*i + 5; ij[5] = 7*i + 6; 2892bbead8a2SBarry Smith ierr = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr); 289396b95a6bSBarry Smith ierr = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr); 289496b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr); 2895bbead8a2SBarry Smith diag += 49; 2896bbead8a2SBarry Smith } 2897bbead8a2SBarry Smith break; 2898bbead8a2SBarry Smith default: 2899bbead8a2SBarry Smith ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr); 2900bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2901bbead8a2SBarry Smith for (j=0; j<bs; j++) { 2902bbead8a2SBarry Smith IJ[j] = bs*i + j; 2903bbead8a2SBarry Smith } 2904bbead8a2SBarry Smith ierr = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr); 290596b95a6bSBarry Smith ierr = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr); 290696b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr); 2907bbead8a2SBarry Smith diag += bs2; 2908bbead8a2SBarry Smith } 2909bbead8a2SBarry Smith ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr); 2910bbead8a2SBarry Smith } 2911bbead8a2SBarry Smith a->ibdiagvalid = PETSC_TRUE; 2912bbead8a2SBarry Smith PetscFunctionReturn(0); 2913bbead8a2SBarry Smith } 2914bbead8a2SBarry Smith 29157087cfbeSBarry Smith extern PetscErrorCode MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 2916682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 29170a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ, 2918cb5b572fSBarry Smith MatGetRow_SeqAIJ, 2919cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 2920cb5b572fSBarry Smith MatMult_SeqAIJ, 292197304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 29227c922b88SBarry Smith MatMultTranspose_SeqAIJ, 29237c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 2924db4efbfdSBarry Smith 0, 2925db4efbfdSBarry Smith 0, 2926db4efbfdSBarry Smith 0, 2927db4efbfdSBarry Smith /*10*/ 0, 2928cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 2929cb5b572fSBarry Smith 0, 293041f059aeSBarry Smith MatSOR_SeqAIJ, 293117ab2063SBarry Smith MatTranspose_SeqAIJ, 293297304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ, 2933cb5b572fSBarry Smith MatEqual_SeqAIJ, 2934cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 2935cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 2936cb5b572fSBarry Smith MatNorm_SeqAIJ, 293797304618SKris Buschelman /*20*/ 0, 2938cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 2939cb5b572fSBarry Smith MatSetOption_SeqAIJ, 2940cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 2941d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ, 2942db4efbfdSBarry Smith 0, 2943db4efbfdSBarry Smith 0, 2944db4efbfdSBarry Smith 0, 2945db4efbfdSBarry Smith 0, 29464994cf47SJed Brown /*29*/ MatSetUp_SeqAIJ, 2947db4efbfdSBarry Smith 0, 2948db4efbfdSBarry Smith 0, 29496c0721eeSBarry Smith MatGetArray_SeqAIJ, 29506c0721eeSBarry Smith MatRestoreArray_SeqAIJ, 2951d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ, 2952cb5b572fSBarry Smith 0, 2953cb5b572fSBarry Smith 0, 2954cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 2955cb5b572fSBarry Smith 0, 2956d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ, 2957cb5b572fSBarry Smith MatGetSubMatrices_SeqAIJ, 2958cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 2959cb5b572fSBarry Smith MatGetValues_SeqAIJ, 2960cb5b572fSBarry Smith MatCopy_SeqAIJ, 2961d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ, 2962cb5b572fSBarry Smith MatScale_SeqAIJ, 2963cb5b572fSBarry Smith 0, 296479299369SBarry Smith MatDiagonalSet_SeqAIJ, 29656e169961SBarry Smith MatZeroRowsColumns_SeqAIJ, 2966f73d5cc4SBarry Smith /*49*/ 0, 29673b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 29683b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 29693b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 2970a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 2971d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ, 2972b9617806SBarry Smith 0, 29730513a670SBarry Smith 0, 2974cda55fadSBarry Smith MatPermute_SeqAIJ, 2975cda55fadSBarry Smith 0, 2976d519adbfSMatthew Knepley /*59*/ 0, 2977b9b97703SBarry Smith MatDestroy_SeqAIJ, 2978b9b97703SBarry Smith MatView_SeqAIJ, 2979357abbc8SBarry Smith 0, 2980ee4f033dSBarry Smith 0, 2981d519adbfSMatthew Knepley /*64*/ 0, 2982ee4f033dSBarry Smith 0, 2983ee4f033dSBarry Smith 0, 2984ee4f033dSBarry Smith 0, 2985ee4f033dSBarry Smith 0, 2986d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ, 2987c87e5d42SMatthew Knepley MatGetRowMinAbs_SeqAIJ, 2988ee4f033dSBarry Smith 0, 2989ee4f033dSBarry Smith MatSetColoring_SeqAIJ, 2990dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2991ee4f033dSBarry Smith MatSetValuesAdic_SeqAIJ, 2992dcf5cc72SBarry Smith #else 2993dcf5cc72SBarry Smith 0, 2994dcf5cc72SBarry Smith #endif 2995d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ, 29963acb8795SBarry Smith MatFDColoringApply_AIJ, 299797304618SKris Buschelman 0, 299897304618SKris Buschelman 0, 299997304618SKris Buschelman 0, 30006ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ, 300197304618SKris Buschelman 0, 300297304618SKris Buschelman 0, 300397304618SKris Buschelman 0, 3004bc011b1eSHong Zhang MatLoad_SeqAIJ, 3005d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ, 30061cbb95d3SBarry Smith MatIsHermitian_SeqAIJ, 30076284ec50SHong Zhang 0, 30086284ec50SHong Zhang 0, 3009bc011b1eSHong Zhang 0, 3010d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ, 301126be0446SHong Zhang MatMatMultSymbolic_SeqAIJ_SeqAIJ, 301226be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 3013d439da42SKris Buschelman MatPtAP_Basic, 30147ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ, 3015d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ, 30166fc122caSHong Zhang MatMatTransposeMult_SeqAIJ_SeqAIJ, 30176fc122caSHong Zhang MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ, 30186fc122caSHong Zhang MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ, 30197ba1a0bfSKris Buschelman MatPtAPSymbolic_SeqAIJ_SeqAIJ, 3020d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ, 3021609c6c4dSKris Buschelman 0, 3022609c6c4dSKris Buschelman 0, 302387d4246cSBarry Smith MatConjugate_SeqAIJ, 302487d4246cSBarry Smith 0, 3025d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ, 302699cafbc1SBarry Smith MatRealPart_SeqAIJ, 3027f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 3028f5edf698SHong Zhang 0, 30292bebee5dSHong Zhang 0, 3030cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ, 3031985db425SBarry Smith 0, 30322af78befSBarry Smith MatGetRowMin_SeqAIJ, 30332af78befSBarry Smith 0, 3034599ef60dSHong Zhang MatMissingDiagonal_SeqAIJ, 3035d519adbfSMatthew Knepley /*114*/0, 3036599ef60dSHong Zhang 0, 30373c2a7987SHong Zhang 0, 3038fe97e370SBarry Smith 0, 3039fbdbba38SShri Abhyankar 0, 3040fbdbba38SShri Abhyankar /*119*/0, 3041fbdbba38SShri Abhyankar 0, 3042fbdbba38SShri Abhyankar 0, 304382d44351SHong Zhang 0, 3044b3a44c85SBarry Smith MatGetMultiProcBlock_SeqAIJ, 30450716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ, 3046bbead8a2SBarry Smith MatGetColumnNorms_SeqAIJ, 304737868618SMatthew G Knepley MatInvertBlockDiagonal_SeqAIJ, 304837868618SMatthew G Knepley 0, 304937868618SMatthew G Knepley 0, 30505df89d91SHong Zhang /*129*/0, 305175648e8dSHong Zhang MatTransposeMatMult_SeqAIJ_SeqAIJ, 305275648e8dSHong Zhang MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ, 305375648e8dSHong Zhang MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ, 3054b9af6bddSHong Zhang MatTransposeColoringCreate_SeqAIJ, 3055b9af6bddSHong Zhang /*134*/MatTransColoringApplySpToDen_SeqAIJ, 30562b8ad9a3SHong Zhang MatTransColoringApplyDenToSp_SeqAIJ, 30572b8ad9a3SHong Zhang MatRARt_SeqAIJ_SeqAIJ, 30582b8ad9a3SHong Zhang MatRARtSymbolic_SeqAIJ_SeqAIJ, 30592b8ad9a3SHong Zhang MatRARtNumeric_SeqAIJ_SeqAIJ 30609e29f15eSvictorle }; 306117ab2063SBarry Smith 3062fb2e594dSBarry Smith EXTERN_C_BEGIN 30634a2ae208SSatish Balay #undef __FUNCT__ 30644a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ" 30657087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 3066bef8e0ddSBarry Smith { 3067bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 306897f1f81fSBarry Smith PetscInt i,nz,n; 3069bef8e0ddSBarry Smith 3070bef8e0ddSBarry Smith PetscFunctionBegin; 3071bef8e0ddSBarry Smith 3072bef8e0ddSBarry Smith nz = aij->maxnz; 3073d0f46423SBarry Smith n = mat->rmap->n; 3074bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 3075bef8e0ddSBarry Smith aij->j[i] = indices[i]; 3076bef8e0ddSBarry Smith } 3077bef8e0ddSBarry Smith aij->nz = nz; 3078bef8e0ddSBarry Smith for (i=0; i<n; i++) { 3079bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 3080bef8e0ddSBarry Smith } 3081bef8e0ddSBarry Smith 3082bef8e0ddSBarry Smith PetscFunctionReturn(0); 3083bef8e0ddSBarry Smith } 3084fb2e594dSBarry Smith EXTERN_C_END 3085bef8e0ddSBarry Smith 30864a2ae208SSatish Balay #undef __FUNCT__ 30874a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices" 3088bef8e0ddSBarry Smith /*@ 3089bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 3090bef8e0ddSBarry Smith in the matrix. 3091bef8e0ddSBarry Smith 3092bef8e0ddSBarry Smith Input Parameters: 3093bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 3094bef8e0ddSBarry Smith - indices - the column indices 3095bef8e0ddSBarry Smith 309615091d37SBarry Smith Level: advanced 309715091d37SBarry Smith 3098bef8e0ddSBarry Smith Notes: 3099bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 3100bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 3101bef8e0ddSBarry Smith of the MatSetValues() operation. 3102bef8e0ddSBarry Smith 3103bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 3104d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 3105bef8e0ddSBarry Smith 3106bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 3107bef8e0ddSBarry Smith 3108b9617806SBarry Smith The indices should start with zero, not one. 3109b9617806SBarry Smith 3110bef8e0ddSBarry Smith @*/ 31117087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 3112bef8e0ddSBarry Smith { 31134ac538c5SBarry Smith PetscErrorCode ierr; 3114bef8e0ddSBarry Smith 3115bef8e0ddSBarry Smith PetscFunctionBegin; 31160700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 31174482741eSBarry Smith PetscValidPointer(indices,2); 31184ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr); 3119bef8e0ddSBarry Smith PetscFunctionReturn(0); 3120bef8e0ddSBarry Smith } 3121bef8e0ddSBarry Smith 3122be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 3123be6bf707SBarry Smith 3124fb2e594dSBarry Smith EXTERN_C_BEGIN 31254a2ae208SSatish Balay #undef __FUNCT__ 31264a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ" 31277087cfbeSBarry Smith PetscErrorCode MatStoreValues_SeqAIJ(Mat mat) 3128be6bf707SBarry Smith { 3129be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 31306849ba73SBarry Smith PetscErrorCode ierr; 3131d0f46423SBarry Smith size_t nz = aij->i[mat->rmap->n]; 3132be6bf707SBarry Smith 3133be6bf707SBarry Smith PetscFunctionBegin; 3134be6bf707SBarry Smith if (aij->nonew != 1) { 3135e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3136be6bf707SBarry Smith } 3137be6bf707SBarry Smith 3138be6bf707SBarry Smith /* allocate space for values if not already there */ 3139be6bf707SBarry Smith if (!aij->saved_values) { 314087828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr); 31419518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr); 3142be6bf707SBarry Smith } 3143be6bf707SBarry Smith 3144be6bf707SBarry Smith /* copy values over */ 314587828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 3146be6bf707SBarry Smith PetscFunctionReturn(0); 3147be6bf707SBarry Smith } 3148fb2e594dSBarry Smith EXTERN_C_END 3149be6bf707SBarry Smith 31504a2ae208SSatish Balay #undef __FUNCT__ 3151b9617806SBarry Smith #define __FUNCT__ "MatStoreValues" 3152be6bf707SBarry Smith /*@ 3153be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 3154be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3155be6bf707SBarry Smith nonlinear portion. 3156be6bf707SBarry Smith 3157be6bf707SBarry Smith Collect on Mat 3158be6bf707SBarry Smith 3159be6bf707SBarry Smith Input Parameters: 31600e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 3161be6bf707SBarry Smith 316215091d37SBarry Smith Level: advanced 316315091d37SBarry Smith 3164be6bf707SBarry Smith Common Usage, with SNESSolve(): 3165be6bf707SBarry Smith $ Create Jacobian matrix 3166be6bf707SBarry Smith $ Set linear terms into matrix 3167be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 3168be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 3169be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 3170512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3171be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3172be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 3173be6bf707SBarry Smith $ In your Jacobian routine 3174be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3175be6bf707SBarry Smith $ Set nonlinear terms in matrix 3176be6bf707SBarry Smith 3177be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 3178be6bf707SBarry Smith $ // build linear portion of Jacobian 3179512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3180be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3181be6bf707SBarry Smith $ loop over nonlinear iterations 3182be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3183be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 3184be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 3185be6bf707SBarry Smith $ Solve linear system with Jacobian 3186be6bf707SBarry Smith $ endloop 3187be6bf707SBarry Smith 3188be6bf707SBarry Smith Notes: 3189be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 3190512a5fc5SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before 3191be6bf707SBarry Smith calling this routine. 3192be6bf707SBarry Smith 31930c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 31940c468ba9SBarry Smith and does not allocated additional space. 31950c468ba9SBarry Smith 3196be6bf707SBarry Smith .seealso: MatRetrieveValues() 3197be6bf707SBarry Smith 3198be6bf707SBarry Smith @*/ 31997087cfbeSBarry Smith PetscErrorCode MatStoreValues(Mat mat) 3200be6bf707SBarry Smith { 32014ac538c5SBarry Smith PetscErrorCode ierr; 3202be6bf707SBarry Smith 3203be6bf707SBarry Smith PetscFunctionBegin; 32040700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3205e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3206e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 32074ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr); 3208be6bf707SBarry Smith PetscFunctionReturn(0); 3209be6bf707SBarry Smith } 3210be6bf707SBarry Smith 3211fb2e594dSBarry Smith EXTERN_C_BEGIN 32124a2ae208SSatish Balay #undef __FUNCT__ 32134a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ" 32147087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat) 3215be6bf707SBarry Smith { 3216be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 32176849ba73SBarry Smith PetscErrorCode ierr; 3218d0f46423SBarry Smith PetscInt nz = aij->i[mat->rmap->n]; 3219be6bf707SBarry Smith 3220be6bf707SBarry Smith PetscFunctionBegin; 3221be6bf707SBarry Smith if (aij->nonew != 1) { 3222e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3223be6bf707SBarry Smith } 3224be6bf707SBarry Smith if (!aij->saved_values) { 3225e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 3226be6bf707SBarry Smith } 3227be6bf707SBarry Smith /* copy values over */ 322887828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 3229be6bf707SBarry Smith PetscFunctionReturn(0); 3230be6bf707SBarry Smith } 3231fb2e594dSBarry Smith EXTERN_C_END 3232be6bf707SBarry Smith 32334a2ae208SSatish Balay #undef __FUNCT__ 32344a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues" 3235be6bf707SBarry Smith /*@ 3236be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 3237be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3238be6bf707SBarry Smith nonlinear portion. 3239be6bf707SBarry Smith 3240be6bf707SBarry Smith Collect on Mat 3241be6bf707SBarry Smith 3242be6bf707SBarry Smith Input Parameters: 3243be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 3244be6bf707SBarry Smith 324515091d37SBarry Smith Level: advanced 324615091d37SBarry Smith 3247be6bf707SBarry Smith .seealso: MatStoreValues() 3248be6bf707SBarry Smith 3249be6bf707SBarry Smith @*/ 32507087cfbeSBarry Smith PetscErrorCode MatRetrieveValues(Mat mat) 3251be6bf707SBarry Smith { 32524ac538c5SBarry Smith PetscErrorCode ierr; 3253be6bf707SBarry Smith 3254be6bf707SBarry Smith PetscFunctionBegin; 32550700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3256e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3257e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 32584ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr); 3259be6bf707SBarry Smith PetscFunctionReturn(0); 3260be6bf707SBarry Smith } 3261be6bf707SBarry Smith 3262f83d6046SBarry Smith 3263be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 32644a2ae208SSatish Balay #undef __FUNCT__ 32654a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ" 326617ab2063SBarry Smith /*@C 3267682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 32680d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 32696e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 327051c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 32712bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 327217ab2063SBarry Smith 3273db81eaa0SLois Curfman McInnes Collective on MPI_Comm 3274db81eaa0SLois Curfman McInnes 327517ab2063SBarry Smith Input Parameters: 3276db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 327717ab2063SBarry Smith . m - number of rows 327817ab2063SBarry Smith . n - number of columns 327917ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 328051c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 32812bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 328217ab2063SBarry Smith 328317ab2063SBarry Smith Output Parameter: 3284416022c9SBarry Smith . A - the matrix 328517ab2063SBarry Smith 3286175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3287ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3288175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3289175b88e8SBarry Smith 3290b259b22eSLois Curfman McInnes Notes: 329149a6f317SBarry Smith If nnz is given then nz is ignored 329249a6f317SBarry Smith 329317ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 329417ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 32950002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 329644cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 329717ab2063SBarry Smith 329817ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 3299a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 33003d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 33016da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 330217ab2063SBarry Smith 3303682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 33044fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 3305682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 33066c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 33076c7ebb05SLois Curfman McInnes 33086c7ebb05SLois Curfman McInnes Options Database Keys: 3309698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 33109db58ca8SBarry Smith - -mat_inode_limit <limit> - Sets inode limit (max limit=5) 331117ab2063SBarry Smith 3312027ccd11SLois Curfman McInnes Level: intermediate 3313027ccd11SLois Curfman McInnes 331469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 331536db0b34SBarry Smith 331617ab2063SBarry Smith @*/ 33177087cfbeSBarry Smith PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 331817ab2063SBarry Smith { 3319dfbe8321SBarry Smith PetscErrorCode ierr; 33206945ee14SBarry Smith 33213a40ed3dSBarry Smith PetscFunctionBegin; 3322f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 3323117016b1SBarry Smith ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 3324c4752a88SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 3325d28bb7d2SJed Brown ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 3326273d9f13SBarry Smith PetscFunctionReturn(0); 3327273d9f13SBarry Smith } 3328273d9f13SBarry Smith 33294a2ae208SSatish Balay #undef __FUNCT__ 33304a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation" 3331273d9f13SBarry Smith /*@C 3332273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 3333273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 3334273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 3335273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 3336273d9f13SBarry Smith 3337273d9f13SBarry Smith Collective on MPI_Comm 3338273d9f13SBarry Smith 3339273d9f13SBarry Smith Input Parameters: 3340117016b1SBarry Smith + B - The matrix-free 3341273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 3342273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 3343273d9f13SBarry Smith (possibly different for each row) or PETSC_NULL 3344273d9f13SBarry Smith 3345273d9f13SBarry Smith Notes: 334649a6f317SBarry Smith If nnz is given then nz is ignored 334749a6f317SBarry Smith 3348273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3349273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 3350273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 3351273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 3352273d9f13SBarry Smith 3353273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 3354273d9f13SBarry Smith Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 3355273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 3356273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 3357273d9f13SBarry Smith 3358aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3359aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3360aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3361aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3362aa95bbe8SBarry Smith 3363a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 3364a96a251dSBarry Smith entries or columns indices 3365a96a251dSBarry Smith 3366273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 3367273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 3368273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 3369273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3370273d9f13SBarry Smith 3371273d9f13SBarry Smith Options Database Keys: 3372698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 3373698d4c6aSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3374273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 3375273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 3376273d9f13SBarry Smith the user still MUST index entries starting at 0! 3377273d9f13SBarry Smith 3378273d9f13SBarry Smith Level: intermediate 3379273d9f13SBarry Smith 338069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo() 3381273d9f13SBarry Smith 3382273d9f13SBarry Smith @*/ 33837087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 3384273d9f13SBarry Smith { 33854ac538c5SBarry Smith PetscErrorCode ierr; 3386a23d5eceSKris Buschelman 3387a23d5eceSKris Buschelman PetscFunctionBegin; 33886ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 33896ba663aaSJed Brown PetscValidType(B,1); 33904ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr); 3391a23d5eceSKris Buschelman PetscFunctionReturn(0); 3392a23d5eceSKris Buschelman } 3393a23d5eceSKris Buschelman 3394a23d5eceSKris Buschelman EXTERN_C_BEGIN 3395a23d5eceSKris Buschelman #undef __FUNCT__ 3396a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ" 33977087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz) 3398a23d5eceSKris Buschelman { 3399273d9f13SBarry Smith Mat_SeqAIJ *b; 34002576faa2SJed Brown PetscBool skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE; 34016849ba73SBarry Smith PetscErrorCode ierr; 340297f1f81fSBarry Smith PetscInt i; 3403273d9f13SBarry Smith 3404273d9f13SBarry Smith PetscFunctionBegin; 34052576faa2SJed Brown if (nz >= 0 || nnz) realalloc = PETSC_TRUE; 3406a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 3407c461c341SBarry Smith skipallocation = PETSC_TRUE; 3408c461c341SBarry Smith nz = 0; 3409c461c341SBarry Smith } 3410c461c341SBarry Smith 341126283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 341226283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3413899cda47SBarry Smith 3414435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 3415e32f2f54SBarry Smith if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz); 3416b73539f3SBarry Smith if (nnz) { 3417d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3418e32f2f54SBarry Smith if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]); 3419e32f2f54SBarry Smith if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n); 3420b73539f3SBarry Smith } 3421b73539f3SBarry Smith } 3422b73539f3SBarry Smith 3423273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 3424273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 3425273d9f13SBarry Smith 3426ab93d7beSBarry Smith if (!skipallocation) { 34272ee49352SLisandro Dalcin if (!b->imax) { 3428d0f46423SBarry Smith ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr); 3429d0f46423SBarry Smith ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 34302ee49352SLisandro Dalcin } 3431273d9f13SBarry Smith if (!nnz) { 3432435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 3433c62bd62aSJed Brown else if (nz < 0) nz = 1; 3434d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) b->imax[i] = nz; 3435d0f46423SBarry Smith nz = nz*B->rmap->n; 3436273d9f13SBarry Smith } else { 3437273d9f13SBarry Smith nz = 0; 3438d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 3439273d9f13SBarry Smith } 3440ab93d7beSBarry Smith /* b->ilen will count nonzeros in each row so far. */ 3441d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; } 3442ab93d7beSBarry Smith 3443273d9f13SBarry Smith /* allocate the matrix space */ 34442ee49352SLisandro Dalcin ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr); 3445d0f46423SBarry Smith ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr); 3446d0f46423SBarry Smith ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr); 3447bfeeae90SHong Zhang b->i[0] = 0; 3448d0f46423SBarry Smith for (i=1; i<B->rmap->n+1; i++) { 34495da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 34505da197adSKris Buschelman } 3451273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 3452e6b907acSBarry Smith b->free_a = PETSC_TRUE; 3453e6b907acSBarry Smith b->free_ij = PETSC_TRUE; 3454c461c341SBarry Smith } else { 3455e6b907acSBarry Smith b->free_a = PETSC_FALSE; 3456e6b907acSBarry Smith b->free_ij = PETSC_FALSE; 3457c461c341SBarry Smith } 3458273d9f13SBarry Smith 3459273d9f13SBarry Smith b->nz = 0; 3460273d9f13SBarry Smith b->maxnz = nz; 3461273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 34622576faa2SJed Brown if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);} 3463273d9f13SBarry Smith PetscFunctionReturn(0); 3464273d9f13SBarry Smith } 3465a23d5eceSKris Buschelman EXTERN_C_END 3466273d9f13SBarry Smith 3467a1661176SMatthew Knepley #undef __FUNCT__ 3468a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR" 346958d36128SBarry Smith /*@ 3470a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 3471a1661176SMatthew Knepley 3472a1661176SMatthew Knepley Input Parameters: 3473a1661176SMatthew Knepley + B - the matrix 3474a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 3475a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 3476a1661176SMatthew Knepley - v - optional values in the matrix 3477a1661176SMatthew Knepley 3478a1661176SMatthew Knepley Level: developer 3479a1661176SMatthew Knepley 348058d36128SBarry Smith The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays() 348158d36128SBarry Smith 3482a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential 3483a1661176SMatthew Knepley 3484a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ 3485a1661176SMatthew Knepley @*/ 3486a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 3487a1661176SMatthew Knepley { 3488a1661176SMatthew Knepley PetscErrorCode ierr; 3489a1661176SMatthew Knepley 3490a1661176SMatthew Knepley PetscFunctionBegin; 34910700a824SBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,1); 34926ba663aaSJed Brown PetscValidType(B,1); 34934ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3494a1661176SMatthew Knepley PetscFunctionReturn(0); 3495a1661176SMatthew Knepley } 3496a1661176SMatthew Knepley 3497a1661176SMatthew Knepley EXTERN_C_BEGIN 3498a1661176SMatthew Knepley #undef __FUNCT__ 3499a1661176SMatthew Knepley #define __FUNCT__ "MatSeqAIJSetPreallocationCSR_SeqAIJ" 35007087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3501a1661176SMatthew Knepley { 3502a1661176SMatthew Knepley PetscInt i; 3503a1661176SMatthew Knepley PetscInt m,n; 3504a1661176SMatthew Knepley PetscInt nz; 3505a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 3506a1661176SMatthew Knepley PetscScalar *values; 3507a1661176SMatthew Knepley PetscErrorCode ierr; 3508a1661176SMatthew Knepley 3509a1661176SMatthew Knepley PetscFunctionBegin; 351065e19b50SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]); 3511779a8d59SSatish Balay 3512779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 3513779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3514779a8d59SSatish Balay 3515779a8d59SSatish Balay ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 3516a1661176SMatthew Knepley ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr); 3517a1661176SMatthew Knepley for(i = 0; i < m; i++) { 3518b7940d39SSatish Balay nz = Ii[i+1]- Ii[i]; 3519a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 352065e19b50SBarry Smith if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 3521a1661176SMatthew Knepley nnz[i] = nz; 3522a1661176SMatthew Knepley } 3523a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 3524a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 3525a1661176SMatthew Knepley 3526a1661176SMatthew Knepley if (v) { 3527a1661176SMatthew Knepley values = (PetscScalar*) v; 3528a1661176SMatthew Knepley } else { 35290e83c824SBarry Smith ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr); 3530a1661176SMatthew Knepley ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3531a1661176SMatthew Knepley } 3532a1661176SMatthew Knepley 3533a1661176SMatthew Knepley for(i = 0; i < m; i++) { 3534b7940d39SSatish Balay nz = Ii[i+1] - Ii[i]; 3535b7940d39SSatish Balay ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr); 3536a1661176SMatthew Knepley } 3537a1661176SMatthew Knepley 3538a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3539a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3540a1661176SMatthew Knepley 3541a1661176SMatthew Knepley if (!v) { 3542a1661176SMatthew Knepley ierr = PetscFree(values);CHKERRQ(ierr); 3543a1661176SMatthew Knepley } 35447827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 3545a1661176SMatthew Knepley PetscFunctionReturn(0); 3546a1661176SMatthew Knepley } 3547a1661176SMatthew Knepley EXTERN_C_END 3548a1661176SMatthew Knepley 3549c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h> 3550b45d2f2cSJed Brown #include <petsc-private/petscaxpy.h> 3551170fe5c8SBarry Smith 3552170fe5c8SBarry Smith #undef __FUNCT__ 3553170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ" 3554170fe5c8SBarry Smith /* 3555170fe5c8SBarry Smith Computes (B'*A')' since computing B*A directly is untenable 3556170fe5c8SBarry Smith 3557170fe5c8SBarry Smith n p p 3558170fe5c8SBarry Smith ( ) ( ) ( ) 3559170fe5c8SBarry Smith m ( A ) * n ( B ) = m ( C ) 3560170fe5c8SBarry Smith ( ) ( ) ( ) 3561170fe5c8SBarry Smith 3562170fe5c8SBarry Smith */ 3563170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C) 3564170fe5c8SBarry Smith { 3565170fe5c8SBarry Smith PetscErrorCode ierr; 3566170fe5c8SBarry Smith Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data; 3567170fe5c8SBarry Smith Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data; 3568170fe5c8SBarry Smith Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data; 35691de00fd4SBarry Smith PetscInt i,n,m,q,p; 3570170fe5c8SBarry Smith const PetscInt *ii,*idx; 3571170fe5c8SBarry Smith const PetscScalar *b,*a,*a_q; 3572170fe5c8SBarry Smith PetscScalar *c,*c_q; 3573170fe5c8SBarry Smith 3574170fe5c8SBarry Smith PetscFunctionBegin; 3575d0f46423SBarry Smith m = A->rmap->n; 3576d0f46423SBarry Smith n = A->cmap->n; 3577d0f46423SBarry Smith p = B->cmap->n; 3578170fe5c8SBarry Smith a = sub_a->v; 3579170fe5c8SBarry Smith b = sub_b->a; 3580170fe5c8SBarry Smith c = sub_c->v; 3581170fe5c8SBarry Smith ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr); 3582170fe5c8SBarry Smith 3583170fe5c8SBarry Smith ii = sub_b->i; 3584170fe5c8SBarry Smith idx = sub_b->j; 3585170fe5c8SBarry Smith for (i=0; i<n; i++) { 3586170fe5c8SBarry Smith q = ii[i+1] - ii[i]; 3587170fe5c8SBarry Smith while (q-->0) { 3588170fe5c8SBarry Smith c_q = c + m*(*idx); 3589170fe5c8SBarry Smith a_q = a + m*i; 3590be7314b0SBarry Smith PetscAXPY(c_q,*b,a_q,m); 3591170fe5c8SBarry Smith idx++; 3592170fe5c8SBarry Smith b++; 3593170fe5c8SBarry Smith } 3594170fe5c8SBarry Smith } 3595170fe5c8SBarry Smith PetscFunctionReturn(0); 3596170fe5c8SBarry Smith } 3597170fe5c8SBarry Smith 3598170fe5c8SBarry Smith #undef __FUNCT__ 3599170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ" 3600170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 3601170fe5c8SBarry Smith { 3602170fe5c8SBarry Smith PetscErrorCode ierr; 3603d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 3604170fe5c8SBarry Smith Mat Cmat; 3605170fe5c8SBarry Smith 3606170fe5c8SBarry Smith PetscFunctionBegin; 3607e32f2f54SBarry Smith if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n); 360839804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 3609170fe5c8SBarry Smith ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr); 3610a2f3521dSMark F. Adams ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 3611170fe5c8SBarry Smith ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr); 3612170fe5c8SBarry Smith ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 3613170fe5c8SBarry Smith Cmat->assembled = PETSC_TRUE; 36148cdbd757SHong Zhang Cmat->ops->matmult = MatMatMult_SeqDense_SeqAIJ; 3615170fe5c8SBarry Smith *C = Cmat; 3616170fe5c8SBarry Smith PetscFunctionReturn(0); 3617170fe5c8SBarry Smith } 3618170fe5c8SBarry Smith 3619170fe5c8SBarry Smith /* ----------------------------------------------------------------*/ 3620170fe5c8SBarry Smith #undef __FUNCT__ 3621170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ" 3622170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 3623170fe5c8SBarry Smith { 3624170fe5c8SBarry Smith PetscErrorCode ierr; 3625170fe5c8SBarry Smith 3626170fe5c8SBarry Smith PetscFunctionBegin; 3627170fe5c8SBarry Smith if (scall == MAT_INITIAL_MATRIX){ 3628170fe5c8SBarry Smith ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr); 3629170fe5c8SBarry Smith } 3630170fe5c8SBarry Smith ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr); 3631170fe5c8SBarry Smith PetscFunctionReturn(0); 3632170fe5c8SBarry Smith } 3633170fe5c8SBarry Smith 3634170fe5c8SBarry Smith 36350bad9183SKris Buschelman /*MC 3636fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 36370bad9183SKris Buschelman based on compressed sparse row format. 36380bad9183SKris Buschelman 36390bad9183SKris Buschelman Options Database Keys: 36400bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 36410bad9183SKris Buschelman 36420bad9183SKris Buschelman Level: beginner 36430bad9183SKris Buschelman 3644f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 36450bad9183SKris Buschelman M*/ 36460bad9183SKris Buschelman 3647a6175056SHong Zhang EXTERN_C_BEGIN 3648b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX) 3649b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*); 3650b5e56a35SBarry Smith #endif 3651ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) 3652af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *); 3653af1023dbSSatish Balay #endif 36547087cfbeSBarry Smith extern PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*); 36557087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*); 36567087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*); 36577087cfbeSBarry Smith extern PetscErrorCode MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool *); 3658611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 36597087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 3660611f576cSBarry Smith #endif 3661611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU) 36627087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*); 3663611f576cSBarry Smith #endif 3664f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST) 3665f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*); 3666f3c0ef26SHong Zhang #endif 3667611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 36687087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*); 3669611f576cSBarry Smith #endif 3670eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK) 36717087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*); 3672eb3b5408SSatish Balay #endif 3673586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD) 36747087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*); 3675586621ddSJed Brown #endif 3676719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL) 36777087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*); 3678719d5645SBarry Smith #endif 3679b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 36807087cfbeSBarry Smith extern PetscErrorCode MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*); 36817087cfbeSBarry Smith extern PetscErrorCode MatlabEnginePut_SeqAIJ(PetscObject,void*); 36827087cfbeSBarry Smith extern PetscErrorCode MatlabEngineGet_SeqAIJ(PetscObject,void*); 3683b3866ffcSBarry Smith #endif 368417667f90SBarry Smith EXTERN_C_END 368517667f90SBarry Smith 3686c0c8ee5eSDmitry Karpeev 368717667f90SBarry Smith EXTERN_C_BEGIN 36884a2ae208SSatish Balay #undef __FUNCT__ 36894a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ" 36907087cfbeSBarry Smith PetscErrorCode MatCreate_SeqAIJ(Mat B) 3691273d9f13SBarry Smith { 3692273d9f13SBarry Smith Mat_SeqAIJ *b; 3693dfbe8321SBarry Smith PetscErrorCode ierr; 369438baddfdSBarry Smith PetscMPIInt size; 3695273d9f13SBarry Smith 3696273d9f13SBarry Smith PetscFunctionBegin; 36977adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 3698e32f2f54SBarry Smith if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 3699273d9f13SBarry Smith 370038f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr); 3701b0a32e0cSBarry Smith B->data = (void*)b; 3702549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 3703416022c9SBarry Smith b->row = 0; 3704416022c9SBarry Smith b->col = 0; 370582bf6240SBarry Smith b->icol = 0; 3706b810aeb4SBarry Smith b->reallocs = 0; 370736db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 3708f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 3709416022c9SBarry Smith b->nonew = 0; 3710416022c9SBarry Smith b->diag = 0; 3711416022c9SBarry Smith b->solve_work = 0; 37122a1b7f2aSHong Zhang B->spptr = 0; 3713be6bf707SBarry Smith b->saved_values = 0; 3714d7f994e1SBarry Smith b->idiag = 0; 371571f1c65dSBarry Smith b->mdiag = 0; 371671f1c65dSBarry Smith b->ssor_work = 0; 371771f1c65dSBarry Smith b->omega = 1.0; 371871f1c65dSBarry Smith b->fshift = 0.0; 371971f1c65dSBarry Smith b->idiagvalid = PETSC_FALSE; 3720bbead8a2SBarry Smith b->ibdiagvalid = PETSC_FALSE; 3721a9817697SBarry Smith b->keepnonzeropattern = PETSC_FALSE; 3722a30b2313SHong Zhang b->xtoy = 0; 3723a30b2313SHong Zhang b->XtoY = 0; 372488e51ccdSHong Zhang B->same_nonzero = PETSC_FALSE; 372517ab2063SBarry Smith 372635d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 3727b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3728700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr); 3729b3866ffcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr); 3730b3866ffcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr); 3731b3866ffcSBarry Smith #endif 3732b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX) 3733700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr); 3734b5e56a35SBarry Smith #endif 3735ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) 3736700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr); 3737719d5645SBarry Smith #endif 3738611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU) 3739700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr); 3740611f576cSBarry Smith #endif 3741f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST) 3742700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr); 3743f3c0ef26SHong Zhang #endif 3744611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 3745700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr); 3746611f576cSBarry Smith #endif 3747611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 3748700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr); 3749611f576cSBarry Smith #endif 3750eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK) 3751700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr); 3752eb3b5408SSatish Balay #endif 3753586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD) 3754700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr); 3755586621ddSJed Brown #endif 3756719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL) 3757700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_seqaij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr); 3758719d5645SBarry Smith #endif 3759700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr); 3760700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr); 3761700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr); 3762700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 3763700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr); 3764700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 3765700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 3766700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 3767700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr); 3768700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr); 3769700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 3770700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 3771700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 3772700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 3773700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 3774700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr); 3775700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 3776700c5bfcSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 37774108e4d5SBarry Smith ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr); 377817667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 37793a40ed3dSBarry Smith PetscFunctionReturn(0); 378017ab2063SBarry Smith } 3781273d9f13SBarry Smith EXTERN_C_END 378217ab2063SBarry Smith 37834a2ae208SSatish Balay #undef __FUNCT__ 3784b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ" 3785b24902e0SBarry Smith /* 3786b24902e0SBarry Smith Given a matrix generated with MatGetFactor() duplicates all the information in A into B 3787b24902e0SBarry Smith */ 3788ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace) 378917ab2063SBarry Smith { 3790416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 37916849ba73SBarry Smith PetscErrorCode ierr; 3792d0f46423SBarry Smith PetscInt i,m = A->rmap->n; 379317ab2063SBarry Smith 37943a40ed3dSBarry Smith PetscFunctionBegin; 3795273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 3796273d9f13SBarry Smith 3797d5f3da31SBarry Smith C->factortype = A->factortype; 3798416022c9SBarry Smith c->row = 0; 3799416022c9SBarry Smith c->col = 0; 380082bf6240SBarry Smith c->icol = 0; 38016ad4291fSHong Zhang c->reallocs = 0; 380217ab2063SBarry Smith 38036ad4291fSHong Zhang C->assembled = PETSC_TRUE; 380417ab2063SBarry Smith 3805aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr); 3806aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr); 3807eec197d1SBarry Smith 380833b91e9fSSatish Balay ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr); 38099518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr); 381017ab2063SBarry Smith for (i=0; i<m; i++) { 3811416022c9SBarry Smith c->imax[i] = a->imax[i]; 3812416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 381317ab2063SBarry Smith } 381417ab2063SBarry Smith 381517ab2063SBarry Smith /* allocate the matrix space */ 3816f77e22a1SHong Zhang if (mallocmatspace){ 3817a96a251dSBarry Smith ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr); 38189518dbb4SMatthew Knepley ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 3819f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 382097f1f81fSBarry Smith ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 382117ab2063SBarry Smith if (m > 0) { 382297f1f81fSBarry Smith ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr); 3823be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 3824bfeeae90SHong Zhang ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 3825be6bf707SBarry Smith } else { 3826bfeeae90SHong Zhang ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 382717ab2063SBarry Smith } 382808480c60SBarry Smith } 3829f77e22a1SHong Zhang } 383017ab2063SBarry Smith 38316ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 3832416022c9SBarry Smith c->roworiented = a->roworiented; 3833416022c9SBarry Smith c->nonew = a->nonew; 3834416022c9SBarry Smith if (a->diag) { 383597f1f81fSBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr); 383652e6d16bSBarry Smith ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 383717ab2063SBarry Smith for (i=0; i<m; i++) { 3838416022c9SBarry Smith c->diag[i] = a->diag[i]; 383917ab2063SBarry Smith } 38403a40ed3dSBarry Smith } else c->diag = 0; 38416ad4291fSHong Zhang c->solve_work = 0; 38426ad4291fSHong Zhang c->saved_values = 0; 38436ad4291fSHong Zhang c->idiag = 0; 384471f1c65dSBarry Smith c->ssor_work = 0; 3845a9817697SBarry Smith c->keepnonzeropattern = a->keepnonzeropattern; 3846e6b907acSBarry Smith c->free_a = PETSC_TRUE; 3847e6b907acSBarry Smith c->free_ij = PETSC_TRUE; 38486ad4291fSHong Zhang c->xtoy = 0; 38496ad4291fSHong Zhang c->XtoY = 0; 38506ad4291fSHong Zhang 3851893ad86cSHong Zhang c->rmax = a->rmax; 3852416022c9SBarry Smith c->nz = a->nz; 38538ed568f8SMatthew G Knepley c->maxnz = a->nz; /* Since we allocate exactly the right amount */ 3854273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 3855754ec7b1SSatish Balay 38566ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 38576ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 3858cd6b891eSBarry Smith c->compressedrow.check = a->compressedrow.check; 3859cd6b891eSBarry Smith if (a->compressedrow.use){ 38606ad4291fSHong Zhang i = a->compressedrow.nrows; 38610e83c824SBarry Smith ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr); 38626ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr); 38636ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr); 386427ea64f8SHong Zhang } else { 386527ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 386627ea64f8SHong Zhang c->compressedrow.i = PETSC_NULL; 386727ea64f8SHong Zhang c->compressedrow.rindex = PETSC_NULL; 38686ad4291fSHong Zhang } 386988e51ccdSHong Zhang C->same_nonzero = A->same_nonzero; 38704108e4d5SBarry Smith ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr); 38714846f1f5SKris Buschelman 38727adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr); 38733a40ed3dSBarry Smith PetscFunctionReturn(0); 387417ab2063SBarry Smith } 387517ab2063SBarry Smith 38764a2ae208SSatish Balay #undef __FUNCT__ 3877b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ" 3878b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 3879b24902e0SBarry Smith { 3880b24902e0SBarry Smith PetscErrorCode ierr; 3881b24902e0SBarry Smith 3882b24902e0SBarry Smith PetscFunctionBegin; 3883b24902e0SBarry Smith ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr); 38844b6263acSBarry Smith ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr); 3885a2f3521dSMark F. Adams ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 3886a54f2f98SBarry Smith ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 3887f77e22a1SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr); 3888b24902e0SBarry Smith PetscFunctionReturn(0); 3889b24902e0SBarry Smith } 3890b24902e0SBarry Smith 3891b24902e0SBarry Smith #undef __FUNCT__ 38924a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ" 3893112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer) 3894fbdbba38SShri Abhyankar { 3895fbdbba38SShri Abhyankar Mat_SeqAIJ *a; 3896fbdbba38SShri Abhyankar PetscErrorCode ierr; 3897fbdbba38SShri Abhyankar PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols; 3898fbdbba38SShri Abhyankar int fd; 3899fbdbba38SShri Abhyankar PetscMPIInt size; 3900fbdbba38SShri Abhyankar MPI_Comm comm; 3901bbead8a2SBarry Smith PetscInt bs = 1; 3902fbdbba38SShri Abhyankar 3903fbdbba38SShri Abhyankar PetscFunctionBegin; 3904fbdbba38SShri Abhyankar ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 3905fbdbba38SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3906fbdbba38SShri Abhyankar if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor"); 3907bbead8a2SBarry Smith 3908bbead8a2SBarry Smith ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr); 3909bbead8a2SBarry Smith ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr); 3910bbead8a2SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3911bbead8a2SBarry Smith 3912fbdbba38SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 3913fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 3914fbdbba38SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 3915fbdbba38SShri Abhyankar M = header[1]; N = header[2]; nz = header[3]; 3916fbdbba38SShri Abhyankar 3917bbead8a2SBarry Smith if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 3918fbdbba38SShri Abhyankar 3919fbdbba38SShri Abhyankar /* read in row lengths */ 3920fbdbba38SShri Abhyankar ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 3921fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 3922fbdbba38SShri Abhyankar 3923fbdbba38SShri Abhyankar /* check if sum of rowlengths is same as nz */ 3924fbdbba38SShri Abhyankar for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 3925fbdbba38SShri Abhyankar if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum); 3926fbdbba38SShri Abhyankar 3927fbdbba38SShri Abhyankar /* set global size if not set already*/ 3928f501eaabSShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) { 3929fbdbba38SShri Abhyankar ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 3930aabbc4fbSShri Abhyankar } else { 3931fbdbba38SShri Abhyankar /* if sizes and type are already set, check if the vector global sizes are correct */ 3932fbdbba38SShri Abhyankar ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr); 39334c5b953cSHong Zhang if (rows < 0 && cols < 0){ /* user might provide local size instead of global size */ 39344c5b953cSHong Zhang ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr); 39354c5b953cSHong Zhang } 3936f501eaabSShri Abhyankar if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols); 3937aabbc4fbSShri Abhyankar } 3938fbdbba38SShri Abhyankar ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr); 3939fbdbba38SShri Abhyankar a = (Mat_SeqAIJ*)newMat->data; 3940fbdbba38SShri Abhyankar 3941fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 3942fbdbba38SShri Abhyankar 3943fbdbba38SShri Abhyankar /* read in nonzero values */ 3944fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 3945fbdbba38SShri Abhyankar 3946fbdbba38SShri Abhyankar /* set matrix "i" values */ 3947fbdbba38SShri Abhyankar a->i[0] = 0; 3948fbdbba38SShri Abhyankar for (i=1; i<= M; i++) { 3949fbdbba38SShri Abhyankar a->i[i] = a->i[i-1] + rowlengths[i-1]; 3950fbdbba38SShri Abhyankar a->ilen[i-1] = rowlengths[i-1]; 3951fbdbba38SShri Abhyankar } 3952fbdbba38SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 3953fbdbba38SShri Abhyankar 3954fbdbba38SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3955fbdbba38SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3956bbead8a2SBarry Smith if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);} 3957fbdbba38SShri Abhyankar PetscFunctionReturn(0); 3958fbdbba38SShri Abhyankar } 3959fbdbba38SShri Abhyankar 3960fbdbba38SShri Abhyankar #undef __FUNCT__ 3961b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ" 3962ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg) 39637264ac53SSatish Balay { 39647264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data; 3965dfbe8321SBarry Smith PetscErrorCode ierr; 3966eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 3967eeffb40dSHong Zhang PetscInt k; 3968eeffb40dSHong Zhang #endif 39697264ac53SSatish Balay 39703a40ed3dSBarry Smith PetscFunctionBegin; 3971bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 3972d0f46423SBarry Smith if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) { 3973ca44d042SBarry Smith *flg = PETSC_FALSE; 3974ca44d042SBarry Smith PetscFunctionReturn(0); 3975bcd2baecSBarry Smith } 39767264ac53SSatish Balay 39777264ac53SSatish Balay /* if the a->i are the same */ 3978d0f46423SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3979abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 39807264ac53SSatish Balay 39817264ac53SSatish Balay /* if a->j are the same */ 398297f1f81fSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr); 3983abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 3984bcd2baecSBarry Smith 3985bcd2baecSBarry Smith /* if a->a are the same */ 3986eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 3987eeffb40dSHong Zhang for (k=0; k<a->nz; k++){ 3988eeffb40dSHong Zhang if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){ 3989eeffb40dSHong Zhang *flg = PETSC_FALSE; 39903a40ed3dSBarry Smith PetscFunctionReturn(0); 3991eeffb40dSHong Zhang } 3992eeffb40dSHong Zhang } 3993eeffb40dSHong Zhang #else 3994eeffb40dSHong Zhang ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 3995eeffb40dSHong Zhang #endif 3996eeffb40dSHong Zhang PetscFunctionReturn(0); 39977264ac53SSatish Balay } 399836db0b34SBarry Smith 39994a2ae208SSatish Balay #undef __FUNCT__ 40004a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays" 400105869f15SSatish Balay /*@ 400236db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 400336db0b34SBarry Smith provided by the user. 400436db0b34SBarry Smith 4005c75a6043SHong Zhang Collective on MPI_Comm 400636db0b34SBarry Smith 400736db0b34SBarry Smith Input Parameters: 400836db0b34SBarry Smith + comm - must be an MPI communicator of size 1 400936db0b34SBarry Smith . m - number of rows 401036db0b34SBarry Smith . n - number of columns 401136db0b34SBarry Smith . i - row indices 401236db0b34SBarry Smith . j - column indices 401336db0b34SBarry Smith - a - matrix values 401436db0b34SBarry Smith 401536db0b34SBarry Smith Output Parameter: 401636db0b34SBarry Smith . mat - the matrix 401736db0b34SBarry Smith 401836db0b34SBarry Smith Level: intermediate 401936db0b34SBarry Smith 402036db0b34SBarry Smith Notes: 40210551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 4022292fb18eSBarry Smith once the matrix is destroyed and not before 402336db0b34SBarry Smith 402436db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 402536db0b34SBarry Smith 4026bfeeae90SHong Zhang The i and j indices are 0 based 402736db0b34SBarry Smith 4028a4552177SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 4029a4552177SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 4030a4552177SSatish Balay as shown: 4031a4552177SSatish Balay 4032a4552177SSatish Balay 1 0 0 4033a4552177SSatish Balay 2 0 3 4034a4552177SSatish Balay 4 5 6 4035a4552177SSatish Balay 4036a4552177SSatish Balay i = {0,1,3,6} [size = nrow+1 = 3+1] 40379985e31cSBarry Smith j = {0,0,2,0,1,2} [size = nz = 6]; values must be sorted for each row 4038a4552177SSatish Balay v = {1,2,3,4,5,6} [size = nz = 6] 4039a4552177SSatish Balay 40409985e31cSBarry Smith 404169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 404236db0b34SBarry Smith 404336db0b34SBarry Smith @*/ 40447087cfbeSBarry Smith PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat) 404536db0b34SBarry Smith { 4046dfbe8321SBarry Smith PetscErrorCode ierr; 4047cbcfb4deSHong Zhang PetscInt ii; 404836db0b34SBarry Smith Mat_SeqAIJ *aij; 4049cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG) 4050cbcfb4deSHong Zhang PetscInt jj; 4051cbcfb4deSHong Zhang #endif 405236db0b34SBarry Smith 405336db0b34SBarry Smith PetscFunctionBegin; 4054a96a251dSBarry Smith if (i[0]) { 4055e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 405636db0b34SBarry Smith } 4057f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4058f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 4059a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */ 4060ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 4061ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 4062ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 4063ab93d7beSBarry Smith ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr); 4064ab93d7beSBarry Smith 406536db0b34SBarry Smith aij->i = i; 406636db0b34SBarry Smith aij->j = j; 406736db0b34SBarry Smith aij->a = a; 406836db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 406936db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 4070e6b907acSBarry Smith aij->free_a = PETSC_FALSE; 4071e6b907acSBarry Smith aij->free_ij = PETSC_FALSE; 407236db0b34SBarry Smith 407336db0b34SBarry Smith for (ii=0; ii<m; ii++) { 407436db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 40752515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 4076e32f2f54SBarry Smith if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]); 40779985e31cSBarry Smith for (jj=i[ii]+1; jj<i[ii+1]; jj++) { 4078e32f2f54SBarry Smith if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii); 4079e32f2f54SBarry Smith if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii); 40809985e31cSBarry Smith } 408136db0b34SBarry Smith #endif 408236db0b34SBarry Smith } 40832515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 408436db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 4085e32f2f54SBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]); 4086e32f2f54SBarry Smith if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]); 408736db0b34SBarry Smith } 408836db0b34SBarry Smith #endif 408936db0b34SBarry Smith 4090b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4091b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 409236db0b34SBarry Smith PetscFunctionReturn(0); 409336db0b34SBarry Smith } 40948a0b0e6bSVictor Minden #undef __FUNCT__ 40958a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple" 409680ef6e79SMatthew G Knepley /*@C 4097d021a1c5SVictor Minden MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format) 40988a0b0e6bSVictor Minden provided by the user. 40998a0b0e6bSVictor Minden 41008a0b0e6bSVictor Minden Collective on MPI_Comm 41018a0b0e6bSVictor Minden 41028a0b0e6bSVictor Minden Input Parameters: 41038a0b0e6bSVictor Minden + comm - must be an MPI communicator of size 1 41048a0b0e6bSVictor Minden . m - number of rows 41058a0b0e6bSVictor Minden . n - number of columns 41068a0b0e6bSVictor Minden . i - row indices 41078a0b0e6bSVictor Minden . j - column indices 41081230e6d1SVictor Minden . a - matrix values 41091230e6d1SVictor Minden . nz - number of nonzeros 41101230e6d1SVictor Minden - idx - 0 or 1 based 41118a0b0e6bSVictor Minden 41128a0b0e6bSVictor Minden Output Parameter: 41138a0b0e6bSVictor Minden . mat - the matrix 41148a0b0e6bSVictor Minden 41158a0b0e6bSVictor Minden Level: intermediate 41168a0b0e6bSVictor Minden 41178a0b0e6bSVictor Minden Notes: 41188a0b0e6bSVictor Minden The i and j indices are 0 based 41198a0b0e6bSVictor Minden 41208a0b0e6bSVictor Minden The format which is used for the sparse matrix input, is equivalent to a 41218a0b0e6bSVictor Minden row-major ordering.. i.e for the following matrix, the input data expected is 41228a0b0e6bSVictor Minden as shown: 41238a0b0e6bSVictor Minden 41248a0b0e6bSVictor Minden 1 0 0 41258a0b0e6bSVictor Minden 2 0 3 41268a0b0e6bSVictor Minden 4 5 6 41278a0b0e6bSVictor Minden 41288a0b0e6bSVictor Minden i = {0,1,1,2,2,2} 41298a0b0e6bSVictor Minden j = {0,0,2,0,1,2} 41308a0b0e6bSVictor Minden v = {1,2,3,4,5,6} 41318a0b0e6bSVictor Minden 41328a0b0e6bSVictor Minden 413369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 41348a0b0e6bSVictor Minden 41358a0b0e6bSVictor Minden @*/ 41361230e6d1SVictor Minden PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx) 41378a0b0e6bSVictor Minden { 41388a0b0e6bSVictor Minden PetscErrorCode ierr; 4139d021a1c5SVictor Minden PetscInt ii, *nnz, one = 1,row,col; 41408a0b0e6bSVictor Minden 41418a0b0e6bSVictor Minden 41428a0b0e6bSVictor Minden PetscFunctionBegin; 4143d021a1c5SVictor Minden ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr); 41441230e6d1SVictor Minden ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr); 41451230e6d1SVictor Minden for (ii = 0; ii < nz; ii++){ 41461230e6d1SVictor Minden nnz[i[ii]] += 1; 41471230e6d1SVictor Minden } 41488a0b0e6bSVictor Minden ierr = MatCreate(comm,mat);CHKERRQ(ierr); 41498a0b0e6bSVictor Minden ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 4150a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */ 41518a0b0e6bSVictor Minden ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 41521230e6d1SVictor Minden ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr); 41531230e6d1SVictor Minden for (ii = 0; ii < nz; ii++){ 41541230e6d1SVictor Minden if (idx){ 41551230e6d1SVictor Minden row = i[ii] - 1; 41561230e6d1SVictor Minden col = j[ii] - 1; 41571230e6d1SVictor Minden } else { 41581230e6d1SVictor Minden row = i[ii]; 41591230e6d1SVictor Minden col = j[ii]; 41608a0b0e6bSVictor Minden } 41611230e6d1SVictor Minden ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr); 41628a0b0e6bSVictor Minden } 41638a0b0e6bSVictor Minden ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 41648a0b0e6bSVictor Minden ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4165d021a1c5SVictor Minden ierr = PetscFree(nnz);CHKERRQ(ierr); 41668a0b0e6bSVictor Minden PetscFunctionReturn(0); 41678a0b0e6bSVictor Minden } 416836db0b34SBarry Smith 4169cc8ba8e1SBarry Smith #undef __FUNCT__ 4170ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ" 4171dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring) 4172cc8ba8e1SBarry Smith { 4173dfbe8321SBarry Smith PetscErrorCode ierr; 4174cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 417536db0b34SBarry Smith 4176cc8ba8e1SBarry Smith PetscFunctionBegin; 41778ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 4178cc8ba8e1SBarry Smith ierr = ISColoringReference(coloring);CHKERRQ(ierr); 4179cc8ba8e1SBarry Smith a->coloring = coloring; 418012c595b3SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 418197f1f81fSBarry Smith PetscInt i,*larray; 418212c595b3SBarry Smith ISColoring ocoloring; 418308b6dcc0SBarry Smith ISColoringValue *colors; 418412c595b3SBarry Smith 418512c595b3SBarry Smith /* set coloring for diagonal portion */ 41860e83c824SBarry Smith ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4187d0f46423SBarry Smith for (i=0; i<A->cmap->n; i++) { 418812c595b3SBarry Smith larray[i] = i; 418912c595b3SBarry Smith } 4190992144d0SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 41910e83c824SBarry Smith ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4192d0f46423SBarry Smith for (i=0; i<A->cmap->n; i++) { 419312c595b3SBarry Smith colors[i] = coloring->colors[larray[i]]; 419412c595b3SBarry Smith } 419512c595b3SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4196d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 419712c595b3SBarry Smith a->coloring = ocoloring; 419812c595b3SBarry Smith } 4199cc8ba8e1SBarry Smith PetscFunctionReturn(0); 4200cc8ba8e1SBarry Smith } 4201cc8ba8e1SBarry Smith 4202dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 4203ee4f033dSBarry Smith EXTERN_C_BEGIN 4204c6db04a5SJed Brown #include <adic/ad_utils.h> 4205ee4f033dSBarry Smith EXTERN_C_END 4206cc8ba8e1SBarry Smith 4207cc8ba8e1SBarry Smith #undef __FUNCT__ 4208ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 4209dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 4210cc8ba8e1SBarry Smith { 4211cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4212d0f46423SBarry Smith PetscInt m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen; 42134440f671SBarry Smith PetscScalar *v = a->a,*values = ((PetscScalar*)advalues)+1; 421408b6dcc0SBarry Smith ISColoringValue *color; 4215cc8ba8e1SBarry Smith 4216cc8ba8e1SBarry Smith PetscFunctionBegin; 4217e32f2f54SBarry Smith if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 42184440f671SBarry Smith nlen = PetscADGetDerivTypeSize()/sizeof(PetscScalar); 4219cc8ba8e1SBarry Smith color = a->coloring->colors; 4220cc8ba8e1SBarry Smith /* loop over rows */ 4221cc8ba8e1SBarry Smith for (i=0; i<m; i++) { 4222cc8ba8e1SBarry Smith nz = ii[i+1] - ii[i]; 4223cc8ba8e1SBarry Smith /* loop over columns putting computed value into matrix */ 4224cc8ba8e1SBarry Smith for (j=0; j<nz; j++) { 4225cc8ba8e1SBarry Smith *v++ = values[color[*jj++]]; 4226cc8ba8e1SBarry Smith } 42274440f671SBarry Smith values += nlen; /* jump to next row of derivatives */ 4228ee4f033dSBarry Smith } 4229ee4f033dSBarry Smith PetscFunctionReturn(0); 4230ee4f033dSBarry Smith } 4231ee4f033dSBarry Smith #endif 4232ee4f033dSBarry Smith 4233ee4f033dSBarry Smith #undef __FUNCT__ 4234ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ" 423597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues) 4236ee4f033dSBarry Smith { 4237ee4f033dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 4238d0f46423SBarry Smith PetscInt m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j; 423954f21887SBarry Smith MatScalar *v = a->a; 424054f21887SBarry Smith PetscScalar *values = (PetscScalar *)advalues; 424108b6dcc0SBarry Smith ISColoringValue *color; 4242ee4f033dSBarry Smith 4243ee4f033dSBarry Smith PetscFunctionBegin; 4244e32f2f54SBarry Smith if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix"); 4245ee4f033dSBarry Smith color = a->coloring->colors; 4246ee4f033dSBarry Smith /* loop over rows */ 4247ee4f033dSBarry Smith for (i=0; i<m; i++) { 4248ee4f033dSBarry Smith nz = ii[i+1] - ii[i]; 4249ee4f033dSBarry Smith /* loop over columns putting computed value into matrix */ 4250ee4f033dSBarry Smith for (j=0; j<nz; j++) { 4251ee4f033dSBarry Smith *v++ = values[color[*jj++]]; 4252ee4f033dSBarry Smith } 4253ee4f033dSBarry Smith values += nl; /* jump to next row of derivatives */ 4254cc8ba8e1SBarry Smith } 4255cc8ba8e1SBarry Smith PetscFunctionReturn(0); 4256cc8ba8e1SBarry Smith } 425736db0b34SBarry Smith 425881824310SBarry Smith /* 425981824310SBarry Smith Special version for direct calls from Fortran 426081824310SBarry Smith */ 4261b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h> 426281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 426381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 426481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 426581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 426681824310SBarry Smith #endif 426781824310SBarry Smith 426881824310SBarry Smith /* Change these macros so can be used in void function */ 426981824310SBarry Smith #undef CHKERRQ 42707adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr) 427181824310SBarry Smith #undef SETERRQ2 4272e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 42734994cf47SJed Brown #undef SETERRQ3 42744994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 427581824310SBarry Smith 427681824310SBarry Smith EXTERN_C_BEGIN 427781824310SBarry Smith #undef __FUNCT__ 427881824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_" 42791f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr) 428081824310SBarry Smith { 428181824310SBarry Smith Mat A = *AA; 428281824310SBarry Smith PetscInt m = *mm, n = *nn; 428381824310SBarry Smith InsertMode is = *isis; 428481824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 428581824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 428681824310SBarry Smith PetscInt *imax,*ai,*ailen; 428781824310SBarry Smith PetscErrorCode ierr; 428881824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 428954f21887SBarry Smith MatScalar *ap,value,*aa; 4290ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 4291ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 429281824310SBarry Smith 429381824310SBarry Smith PetscFunctionBegin; 42944994cf47SJed Brown MatCheckPreallocated(A,1); 429581824310SBarry Smith imax = a->imax; 429681824310SBarry Smith ai = a->i; 429781824310SBarry Smith ailen = a->ilen; 429881824310SBarry Smith aj = a->j; 429981824310SBarry Smith aa = a->a; 430081824310SBarry Smith 430181824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 430281824310SBarry Smith row = im[k]; 430381824310SBarry Smith if (row < 0) continue; 430481824310SBarry Smith #if defined(PETSC_USE_DEBUG) 4305d0f46423SBarry Smith if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 430681824310SBarry Smith #endif 430781824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 430881824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 430981824310SBarry Smith low = 0; 431081824310SBarry Smith high = nrow; 431181824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 431281824310SBarry Smith if (in[l] < 0) continue; 431381824310SBarry Smith #if defined(PETSC_USE_DEBUG) 4314d0f46423SBarry Smith if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 431581824310SBarry Smith #endif 431681824310SBarry Smith col = in[l]; 431781824310SBarry Smith if (roworiented) { 431881824310SBarry Smith value = v[l + k*n]; 431981824310SBarry Smith } else { 432081824310SBarry Smith value = v[k + l*m]; 432181824310SBarry Smith } 432281824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 432381824310SBarry Smith 432481824310SBarry Smith if (col <= lastcol) low = 0; else high = nrow; 432581824310SBarry Smith lastcol = col; 432681824310SBarry Smith while (high-low > 5) { 432781824310SBarry Smith t = (low+high)/2; 432881824310SBarry Smith if (rp[t] > col) high = t; 432981824310SBarry Smith else low = t; 433081824310SBarry Smith } 433181824310SBarry Smith for (i=low; i<high; i++) { 433281824310SBarry Smith if (rp[i] > col) break; 433381824310SBarry Smith if (rp[i] == col) { 433481824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 433581824310SBarry Smith else ap[i] = value; 433681824310SBarry Smith goto noinsert; 433781824310SBarry Smith } 433881824310SBarry Smith } 433981824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 434081824310SBarry Smith if (nonew == 1) goto noinsert; 43417adad957SLisandro Dalcin if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 4342fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 434381824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 434481824310SBarry Smith /* shift up all the later entries in this row */ 434581824310SBarry Smith for (ii=N; ii>=i; ii--) { 434681824310SBarry Smith rp[ii+1] = rp[ii]; 434781824310SBarry Smith ap[ii+1] = ap[ii]; 434881824310SBarry Smith } 434981824310SBarry Smith rp[i] = col; 435081824310SBarry Smith ap[i] = value; 435181824310SBarry Smith noinsert:; 435281824310SBarry Smith low = i + 1; 435381824310SBarry Smith } 435481824310SBarry Smith ailen[row] = nrow; 435581824310SBarry Smith } 435681824310SBarry Smith A->same_nonzero = PETSC_FALSE; 435781824310SBarry Smith PetscFunctionReturnVoid(); 435881824310SBarry Smith } 435981824310SBarry Smith EXTERN_C_END 436062298a1eSBarry Smith 4361