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> 11af0996ceSBarry Smith #include <petsc/private/kernels/blocktranspose.h> 120716a85fSBarry Smith 134099cc6bSBarry Smith PetscErrorCode MatSeqAIJSetTypeFromOptions(Mat A) 144099cc6bSBarry Smith { 154099cc6bSBarry Smith PetscErrorCode ierr; 164099cc6bSBarry Smith PetscBool flg; 174099cc6bSBarry Smith char type[256]; 184099cc6bSBarry Smith 194099cc6bSBarry Smith PetscFunctionBegin; 204099cc6bSBarry Smith ierr = PetscObjectOptionsBegin((PetscObject)A); 214099cc6bSBarry Smith ierr = PetscOptionsFList("-mat_seqaij_type","Matrix SeqAIJ type","MatSeqAIJSetType",MatSeqAIJList,"seqaij",type,256,&flg);CHKERRQ(ierr); 224099cc6bSBarry Smith if (flg) { 234099cc6bSBarry Smith ierr = MatSeqAIJSetType(A,type);CHKERRQ(ierr); 244099cc6bSBarry Smith } 254099cc6bSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 264099cc6bSBarry Smith PetscFunctionReturn(0); 274099cc6bSBarry Smith } 284099cc6bSBarry Smith 290716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms) 300716a85fSBarry Smith { 310716a85fSBarry Smith PetscErrorCode ierr; 320716a85fSBarry Smith PetscInt i,m,n; 330716a85fSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data; 340716a85fSBarry Smith 350716a85fSBarry Smith PetscFunctionBegin; 360716a85fSBarry Smith ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr); 370716a85fSBarry Smith ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr); 380716a85fSBarry Smith if (type == NORM_2) { 390716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 400716a85fSBarry Smith norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]); 410716a85fSBarry Smith } 420716a85fSBarry Smith } else if (type == NORM_1) { 430716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 440716a85fSBarry Smith norms[aij->j[i]] += PetscAbsScalar(aij->a[i]); 450716a85fSBarry Smith } 460716a85fSBarry Smith } else if (type == NORM_INFINITY) { 470716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 480716a85fSBarry Smith norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]); 490716a85fSBarry Smith } 500716a85fSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType"); 510716a85fSBarry Smith 520716a85fSBarry Smith if (type == NORM_2) { 538f1a2a5eSBarry Smith for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]); 540716a85fSBarry Smith } 550716a85fSBarry Smith PetscFunctionReturn(0); 560716a85fSBarry Smith } 570716a85fSBarry Smith 583a062f41SBarry Smith PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is) 593a062f41SBarry Smith { 603a062f41SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 613a062f41SBarry Smith PetscInt i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs; 623a062f41SBarry Smith const PetscInt *jj = a->j,*ii = a->i; 633a062f41SBarry Smith PetscInt *rows; 643a062f41SBarry Smith PetscErrorCode ierr; 653a062f41SBarry Smith 663a062f41SBarry Smith PetscFunctionBegin; 673a062f41SBarry Smith for (i=0; i<m; i++) { 683a062f41SBarry Smith if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) { 693a062f41SBarry Smith cnt++; 703a062f41SBarry Smith } 713a062f41SBarry Smith } 723a062f41SBarry Smith ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr); 733a062f41SBarry Smith cnt = 0; 743a062f41SBarry Smith for (i=0; i<m; i++) { 753a062f41SBarry Smith if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) { 763a062f41SBarry Smith rows[cnt] = i; 773a062f41SBarry Smith cnt++; 783a062f41SBarry Smith } 793a062f41SBarry Smith } 803a062f41SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);CHKERRQ(ierr); 813a062f41SBarry Smith PetscFunctionReturn(0); 823a062f41SBarry Smith } 833a062f41SBarry Smith 84f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows) 856ce1633cSBarry Smith { 866ce1633cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 876ce1633cSBarry Smith const MatScalar *aa = a->a; 886ce1633cSBarry Smith PetscInt i,m=A->rmap->n,cnt = 0; 89b2db7409Sstefano_zampini const PetscInt *ii = a->i,*jj = a->j,*diag; 906ce1633cSBarry Smith PetscInt *rows; 916ce1633cSBarry Smith PetscErrorCode ierr; 926ce1633cSBarry Smith 936ce1633cSBarry Smith PetscFunctionBegin; 946ce1633cSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 956ce1633cSBarry Smith diag = a->diag; 966ce1633cSBarry Smith for (i=0; i<m; i++) { 97b2db7409Sstefano_zampini if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) { 986ce1633cSBarry Smith cnt++; 996ce1633cSBarry Smith } 1006ce1633cSBarry Smith } 101785e854fSJed Brown ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr); 1026ce1633cSBarry Smith cnt = 0; 1036ce1633cSBarry Smith for (i=0; i<m; i++) { 104b2db7409Sstefano_zampini if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) { 1056ce1633cSBarry Smith rows[cnt++] = i; 1066ce1633cSBarry Smith } 1076ce1633cSBarry Smith } 108f1f41ecbSJed Brown *nrows = cnt; 109f1f41ecbSJed Brown *zrows = rows; 110f1f41ecbSJed Brown PetscFunctionReturn(0); 111f1f41ecbSJed Brown } 112f1f41ecbSJed Brown 113f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows) 114f1f41ecbSJed Brown { 115f1f41ecbSJed Brown PetscInt nrows,*rows; 116f1f41ecbSJed Brown PetscErrorCode ierr; 117f1f41ecbSJed Brown 118f1f41ecbSJed Brown PetscFunctionBegin; 1190298fd71SBarry Smith *zrows = NULL; 120f1f41ecbSJed Brown ierr = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr); 121ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr); 1226ce1633cSBarry Smith PetscFunctionReturn(0); 1236ce1633cSBarry Smith } 1246ce1633cSBarry Smith 125b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows) 126b3a44c85SBarry Smith { 127b3a44c85SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 128b3a44c85SBarry Smith const MatScalar *aa; 129b3a44c85SBarry Smith PetscInt m=A->rmap->n,cnt = 0; 130b3a44c85SBarry Smith const PetscInt *ii; 131b3a44c85SBarry Smith PetscInt n,i,j,*rows; 132b3a44c85SBarry Smith PetscErrorCode ierr; 133b3a44c85SBarry Smith 134b3a44c85SBarry Smith PetscFunctionBegin; 135b3a44c85SBarry Smith *keptrows = 0; 136b3a44c85SBarry Smith ii = a->i; 137b3a44c85SBarry Smith for (i=0; i<m; i++) { 138b3a44c85SBarry Smith n = ii[i+1] - ii[i]; 139b3a44c85SBarry Smith if (!n) { 140b3a44c85SBarry Smith cnt++; 141b3a44c85SBarry Smith goto ok1; 142b3a44c85SBarry Smith } 143b3a44c85SBarry Smith aa = a->a + ii[i]; 144b3a44c85SBarry Smith for (j=0; j<n; j++) { 145b3a44c85SBarry Smith if (aa[j] != 0.0) goto ok1; 146b3a44c85SBarry Smith } 147b3a44c85SBarry Smith cnt++; 148b3a44c85SBarry Smith ok1:; 149b3a44c85SBarry Smith } 150b3a44c85SBarry Smith if (!cnt) PetscFunctionReturn(0); 151854ce69bSBarry Smith ierr = PetscMalloc1(A->rmap->n-cnt,&rows);CHKERRQ(ierr); 152b3a44c85SBarry Smith cnt = 0; 153b3a44c85SBarry Smith for (i=0; i<m; i++) { 154b3a44c85SBarry Smith n = ii[i+1] - ii[i]; 155b3a44c85SBarry Smith if (!n) continue; 156b3a44c85SBarry Smith aa = a->a + ii[i]; 157b3a44c85SBarry Smith for (j=0; j<n; j++) { 158b3a44c85SBarry Smith if (aa[j] != 0.0) { 159b3a44c85SBarry Smith rows[cnt++] = i; 160b3a44c85SBarry Smith break; 161b3a44c85SBarry Smith } 162b3a44c85SBarry Smith } 163b3a44c85SBarry Smith } 164b3a44c85SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr); 165b3a44c85SBarry Smith PetscFunctionReturn(0); 166b3a44c85SBarry Smith } 167b3a44c85SBarry Smith 1687087cfbeSBarry Smith PetscErrorCode MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is) 16979299369SBarry Smith { 17079299369SBarry Smith PetscErrorCode ierr; 17179299369SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) Y->data; 17299e65526SBarry Smith PetscInt i,m = Y->rmap->n; 17399e65526SBarry Smith const PetscInt *diag; 17454f21887SBarry Smith MatScalar *aa = aij->a; 17599e65526SBarry Smith const PetscScalar *v; 176ace3abfcSBarry Smith PetscBool missing; 17779299369SBarry Smith 17879299369SBarry Smith PetscFunctionBegin; 17909f38230SBarry Smith if (Y->assembled) { 1800298fd71SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr); 18109f38230SBarry Smith if (!missing) { 18279299369SBarry Smith diag = aij->diag; 18399e65526SBarry Smith ierr = VecGetArrayRead(D,&v);CHKERRQ(ierr); 18479299369SBarry Smith if (is == INSERT_VALUES) { 18579299369SBarry Smith for (i=0; i<m; i++) { 18679299369SBarry Smith aa[diag[i]] = v[i]; 18779299369SBarry Smith } 18879299369SBarry Smith } else { 18979299369SBarry Smith for (i=0; i<m; i++) { 19079299369SBarry Smith aa[diag[i]] += v[i]; 19179299369SBarry Smith } 19279299369SBarry Smith } 19399e65526SBarry Smith ierr = VecRestoreArrayRead(D,&v);CHKERRQ(ierr); 19479299369SBarry Smith PetscFunctionReturn(0); 19579299369SBarry Smith } 196acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr); 19709f38230SBarry Smith } 19809f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 19909f38230SBarry Smith PetscFunctionReturn(0); 20009f38230SBarry Smith } 20179299369SBarry Smith 2021a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 20317ab2063SBarry Smith { 204416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 205dfbe8321SBarry Smith PetscErrorCode ierr; 20697f1f81fSBarry Smith PetscInt i,ishift; 20717ab2063SBarry Smith 2083a40ed3dSBarry Smith PetscFunctionBegin; 209d0f46423SBarry Smith *m = A->rmap->n; 2103a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 211bfeeae90SHong Zhang ishift = 0; 21253e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 2132462f5fdSStefano Zampini ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr); 214bfeeae90SHong Zhang } else if (oshift == 1) { 2151a83f524SJed Brown PetscInt *tia; 216d0f46423SBarry Smith PetscInt nz = a->i[A->rmap->n]; 2173b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 218854ce69bSBarry Smith ierr = PetscMalloc1(A->rmap->n+1,&tia);CHKERRQ(ierr); 2191a83f524SJed Brown for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1; 2201a83f524SJed Brown *ia = tia; 221ecc77c7aSBarry Smith if (ja) { 2221a83f524SJed Brown PetscInt *tja; 223854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&tja);CHKERRQ(ierr); 2241a83f524SJed Brown for (i=0; i<nz; i++) tja[i] = a->j[i] + 1; 2251a83f524SJed Brown *ja = tja; 226ecc77c7aSBarry Smith } 2276945ee14SBarry Smith } else { 228ecc77c7aSBarry Smith *ia = a->i; 229ecc77c7aSBarry Smith if (ja) *ja = a->j; 230a2ce50c7SBarry Smith } 2313a40ed3dSBarry Smith PetscFunctionReturn(0); 232a2744918SBarry Smith } 233a2744918SBarry Smith 2341a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2356945ee14SBarry Smith { 236dfbe8321SBarry Smith PetscErrorCode ierr; 2376945ee14SBarry Smith 2383a40ed3dSBarry Smith PetscFunctionBegin; 2393a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 240bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 241606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 242ecc77c7aSBarry Smith if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);} 243bcd2baecSBarry Smith } 2443a40ed3dSBarry Smith PetscFunctionReturn(0); 24517ab2063SBarry Smith } 24617ab2063SBarry Smith 2471a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2483b2fbd54SBarry Smith { 2493b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 250dfbe8321SBarry Smith PetscErrorCode ierr; 251d0f46423SBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 25297f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 2533b2fbd54SBarry Smith 2543a40ed3dSBarry Smith PetscFunctionBegin; 255899cda47SBarry Smith *nn = n; 2563a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 2573b2fbd54SBarry Smith if (symmetric) { 2582462f5fdSStefano Zampini ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr); 2593b2fbd54SBarry Smith } else { 2601795a4d1SJed Brown ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr); 261854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr); 262854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr); 2633b2fbd54SBarry Smith jj = a->j; 2643b2fbd54SBarry Smith for (i=0; i<nz; i++) { 265bfeeae90SHong Zhang collengths[jj[i]]++; 2663b2fbd54SBarry Smith } 2673b2fbd54SBarry Smith cia[0] = oshift; 2683b2fbd54SBarry Smith for (i=0; i<n; i++) { 2693b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 2703b2fbd54SBarry Smith } 27197f1f81fSBarry Smith ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 2723b2fbd54SBarry Smith jj = a->j; 273a93ec695SBarry Smith for (row=0; row<m; row++) { 274a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 275a93ec695SBarry Smith for (i=0; i<mr; i++) { 276bfeeae90SHong Zhang col = *jj++; 2772205254eSKarl Rupp 2783b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 2793b2fbd54SBarry Smith } 2803b2fbd54SBarry Smith } 281606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 2823b2fbd54SBarry Smith *ia = cia; *ja = cja; 2833b2fbd54SBarry Smith } 2843a40ed3dSBarry Smith PetscFunctionReturn(0); 2853b2fbd54SBarry Smith } 2863b2fbd54SBarry Smith 2871a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2883b2fbd54SBarry Smith { 289dfbe8321SBarry Smith PetscErrorCode ierr; 290606d414cSSatish Balay 2913a40ed3dSBarry Smith PetscFunctionBegin; 2923a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 2933b2fbd54SBarry Smith 294606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 295606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 2963a40ed3dSBarry Smith PetscFunctionReturn(0); 2973b2fbd54SBarry Smith } 2983b2fbd54SBarry Smith 2997cee066cSHong Zhang /* 3007cee066cSHong Zhang MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from 3017cee066cSHong Zhang MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output 302040ebd07SHong Zhang spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ() 3037cee066cSHong Zhang */ 3047cee066cSHong Zhang PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool *done) 3057cee066cSHong Zhang { 3067cee066cSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3077cee066cSHong Zhang PetscErrorCode ierr; 3087cee066cSHong Zhang PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 3097cee066cSHong Zhang PetscInt nz = a->i[m],row,*jj,mr,col; 3107cee066cSHong Zhang PetscInt *cspidx; 3117cee066cSHong Zhang 3127cee066cSHong Zhang PetscFunctionBegin; 3137cee066cSHong Zhang *nn = n; 3147cee066cSHong Zhang if (!ia) PetscFunctionReturn(0); 315625f6d37SHong Zhang 3161795a4d1SJed Brown ierr = PetscCalloc1(n+1,&collengths);CHKERRQ(ierr); 317854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr); 318854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&cja);CHKERRQ(ierr); 319854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&cspidx);CHKERRQ(ierr); 3207cee066cSHong Zhang jj = a->j; 3217cee066cSHong Zhang for (i=0; i<nz; i++) { 3227cee066cSHong Zhang collengths[jj[i]]++; 3237cee066cSHong Zhang } 3247cee066cSHong Zhang cia[0] = oshift; 3257cee066cSHong Zhang for (i=0; i<n; i++) { 3267cee066cSHong Zhang cia[i+1] = cia[i] + collengths[i]; 3277cee066cSHong Zhang } 3287cee066cSHong Zhang ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr); 3297cee066cSHong Zhang jj = a->j; 3307cee066cSHong Zhang for (row=0; row<m; row++) { 3317cee066cSHong Zhang mr = a->i[row+1] - a->i[row]; 3327cee066cSHong Zhang for (i=0; i<mr; i++) { 3337cee066cSHong Zhang col = *jj++; 3347cee066cSHong Zhang cspidx[cia[col] + collengths[col] - oshift] = a->i[row] + i; /* index of a->j */ 3357cee066cSHong Zhang cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 3367cee066cSHong Zhang } 3377cee066cSHong Zhang } 3387cee066cSHong Zhang ierr = PetscFree(collengths);CHKERRQ(ierr); 3397cee066cSHong Zhang *ia = cia; *ja = cja; 3407cee066cSHong Zhang *spidx = cspidx; 3417cee066cSHong Zhang PetscFunctionReturn(0); 3427cee066cSHong Zhang } 3437cee066cSHong Zhang 3447cee066cSHong Zhang PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool *done) 3457cee066cSHong Zhang { 3467cee066cSHong Zhang PetscErrorCode ierr; 3477cee066cSHong Zhang 3487cee066cSHong Zhang PetscFunctionBegin; 3495243ef75SHong Zhang ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 3507cee066cSHong Zhang ierr = PetscFree(*spidx);CHKERRQ(ierr); 3517cee066cSHong Zhang PetscFunctionReturn(0); 3527cee066cSHong Zhang } 3537cee066cSHong Zhang 35487d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 35587d4246cSBarry Smith { 35687d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 35787d4246cSBarry Smith PetscInt *ai = a->i; 35887d4246cSBarry Smith PetscErrorCode ierr; 35987d4246cSBarry Smith 36087d4246cSBarry Smith PetscFunctionBegin; 36187d4246cSBarry Smith ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr); 36287d4246cSBarry Smith PetscFunctionReturn(0); 36387d4246cSBarry Smith } 36487d4246cSBarry Smith 365bd04181cSBarry Smith /* 366bd04181cSBarry Smith MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions 367bd04181cSBarry Smith 368bd04181cSBarry Smith - a single row of values is set with each call 369bd04181cSBarry Smith - no row or column indices are negative or (in error) larger than the number of rows or columns 370bd04181cSBarry Smith - the values are always added to the matrix, not set 371bd04181cSBarry Smith - no new locations are introduced in the nonzero structure of the matrix 372bd04181cSBarry Smith 3731f763a69SBarry Smith This does NOT assume the global column indices are sorted 374bd04181cSBarry Smith 3751f763a69SBarry Smith */ 376bd04181cSBarry Smith 377af0996ceSBarry Smith #include <petsc/private/isimpl.h> 378189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 379189e4007SBarry Smith { 380189e4007SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3811f763a69SBarry Smith PetscInt low,high,t,row,nrow,i,col,l; 3821f763a69SBarry Smith const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j; 3831f763a69SBarry Smith PetscInt lastcol = -1; 384189e4007SBarry Smith MatScalar *ap,value,*aa = a->a; 385189e4007SBarry Smith const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices; 386189e4007SBarry Smith 387f38dd0b8SBarry Smith row = ridx[im[0]]; 3881f763a69SBarry Smith rp = aj + ai[row]; 3891f763a69SBarry Smith ap = aa + ai[row]; 3901f763a69SBarry Smith nrow = ailen[row]; 391189e4007SBarry Smith low = 0; 392189e4007SBarry Smith high = nrow; 393189e4007SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 394189e4007SBarry Smith col = cidx[in[l]]; 395f38dd0b8SBarry Smith value = v[l]; 396189e4007SBarry Smith 397189e4007SBarry Smith if (col <= lastcol) low = 0; 398189e4007SBarry Smith else high = nrow; 399189e4007SBarry Smith lastcol = col; 400189e4007SBarry Smith while (high-low > 5) { 401189e4007SBarry Smith t = (low+high)/2; 402189e4007SBarry Smith if (rp[t] > col) high = t; 403189e4007SBarry Smith else low = t; 404189e4007SBarry Smith } 405189e4007SBarry Smith for (i=low; i<high; i++) { 406189e4007SBarry Smith if (rp[i] == col) { 4071f763a69SBarry Smith ap[i] += value; 408189e4007SBarry Smith low = i + 1; 4091f763a69SBarry Smith break; 410189e4007SBarry Smith } 411189e4007SBarry Smith } 412189e4007SBarry Smith } 413f38dd0b8SBarry Smith return 0; 414189e4007SBarry Smith } 415189e4007SBarry Smith 41697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 41717ab2063SBarry Smith { 418416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 419e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 42097f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 4216849ba73SBarry Smith PetscErrorCode ierr; 422e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 423d8cdefa3SHong Zhang MatScalar *ap=NULL,value=0.0,*aa = a->a; 424ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 425ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 42617ab2063SBarry Smith 4273a40ed3dSBarry Smith PetscFunctionBegin; 42817ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 429416022c9SBarry Smith row = im[k]; 4305ef9f2a5SBarry Smith if (row < 0) continue; 4312515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 432e32f2f54SBarry 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); 4333b2fbd54SBarry Smith #endif 434720833daSHong Zhang rp = aj + ai[row]; 435876c6284SHong Zhang if (!A->structure_only) ap = aa + ai[row]; 43617ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 437416022c9SBarry Smith low = 0; 438c71e6ed7SBarry Smith high = nrow; 43917ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 4405ef9f2a5SBarry Smith if (in[l] < 0) continue; 4412515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 442e32f2f54SBarry 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); 4433b2fbd54SBarry Smith #endif 444bfeeae90SHong Zhang col = in[l]; 445720833daSHong Zhang if (!A->structure_only) { 4464b0e389bSBarry Smith if (roworiented) { 4475ef9f2a5SBarry Smith value = v[l + k*n]; 448bef8e0ddSBarry Smith } else { 4494b0e389bSBarry Smith value = v[k + l*m]; 4504b0e389bSBarry Smith } 451720833daSHong Zhang } else { /* A->structure_only */ 452720833daSHong Zhang value = 1; /* avoid 'continue' below? */ 453720833daSHong Zhang } 454dcd36c23SBarry Smith if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES) && row != col) continue; 45536db0b34SBarry Smith 4562205254eSKarl Rupp if (col <= lastcol) low = 0; 4572205254eSKarl Rupp else high = nrow; 458e2ee6c50SBarry Smith lastcol = col; 459416022c9SBarry Smith while (high-low > 5) { 460416022c9SBarry Smith t = (low+high)/2; 461416022c9SBarry Smith if (rp[t] > col) high = t; 462416022c9SBarry Smith else low = t; 46317ab2063SBarry Smith } 464416022c9SBarry Smith for (i=low; i<high; i++) { 46517ab2063SBarry Smith if (rp[i] > col) break; 46617ab2063SBarry Smith if (rp[i] == col) { 467876c6284SHong Zhang if (!A->structure_only) { 468416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 46917ab2063SBarry Smith else ap[i] = value; 470720833daSHong Zhang } 471e44c0bd4SBarry Smith low = i + 1; 47217ab2063SBarry Smith goto noinsert; 47317ab2063SBarry Smith } 47417ab2063SBarry Smith } 475dcd36c23SBarry Smith if (value == 0.0 && ignorezeroentries && row != col) goto noinsert; 476c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 477e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 478720833daSHong Zhang if (A->structure_only) { 479876c6284SHong Zhang MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar); 480720833daSHong Zhang } else { 481fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 482720833daSHong Zhang } 483c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 484416022c9SBarry Smith /* shift up all the later entries in this row */ 485416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 48617ab2063SBarry Smith rp[ii+1] = rp[ii]; 487876c6284SHong Zhang if (!A->structure_only) ap[ii+1] = ap[ii]; 488720833daSHong Zhang } 48917ab2063SBarry Smith rp[i] = col; 490876c6284SHong Zhang if (!A->structure_only) ap[i] = value; 491416022c9SBarry Smith low = i + 1; 492e56f5c9eSBarry Smith A->nonzerostate++; 493e44c0bd4SBarry Smith noinsert:; 49417ab2063SBarry Smith } 49517ab2063SBarry Smith ailen[row] = nrow; 49617ab2063SBarry Smith } 4973a40ed3dSBarry Smith PetscFunctionReturn(0); 49817ab2063SBarry Smith } 49917ab2063SBarry Smith 50081824310SBarry Smith 501a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 5027eb43aa7SLois Curfman McInnes { 5037eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 50497f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 50597f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 50654f21887SBarry Smith MatScalar *ap,*aa = a->a; 5077eb43aa7SLois Curfman McInnes 5083a40ed3dSBarry Smith PetscFunctionBegin; 5097eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 5107eb43aa7SLois Curfman McInnes row = im[k]; 511e32f2f54SBarry Smith if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */ 512e32f2f54SBarry 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); 513bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 5147eb43aa7SLois Curfman McInnes nrow = ailen[row]; 5157eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 516e32f2f54SBarry Smith if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */ 517e32f2f54SBarry 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); 518bfeeae90SHong Zhang col = in[l]; 5197eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 5207eb43aa7SLois Curfman McInnes while (high-low > 5) { 5217eb43aa7SLois Curfman McInnes t = (low+high)/2; 5227eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 5237eb43aa7SLois Curfman McInnes else low = t; 5247eb43aa7SLois Curfman McInnes } 5257eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 5267eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 5277eb43aa7SLois Curfman McInnes if (rp[i] == col) { 528b49de8d1SLois Curfman McInnes *v++ = ap[i]; 5297eb43aa7SLois Curfman McInnes goto finished; 5307eb43aa7SLois Curfman McInnes } 5317eb43aa7SLois Curfman McInnes } 53297e567efSBarry Smith *v++ = 0.0; 5337eb43aa7SLois Curfman McInnes finished:; 5347eb43aa7SLois Curfman McInnes } 5357eb43aa7SLois Curfman McInnes } 5363a40ed3dSBarry Smith PetscFunctionReturn(0); 5377eb43aa7SLois Curfman McInnes } 5387eb43aa7SLois Curfman McInnes 53917ab2063SBarry Smith 540dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 54117ab2063SBarry Smith { 542416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 5436849ba73SBarry Smith PetscErrorCode ierr; 5446f69ff64SBarry Smith PetscInt i,*col_lens; 5456f69ff64SBarry Smith int fd; 546b37d52dbSMark F. Adams FILE *file; 54717ab2063SBarry Smith 5483a40ed3dSBarry Smith PetscFunctionBegin; 549b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 550854ce69bSBarry Smith ierr = PetscMalloc1(4+A->rmap->n,&col_lens);CHKERRQ(ierr); 5512205254eSKarl Rupp 5520700a824SBarry Smith col_lens[0] = MAT_FILE_CLASSID; 553d0f46423SBarry Smith col_lens[1] = A->rmap->n; 554d0f46423SBarry Smith col_lens[2] = A->cmap->n; 555416022c9SBarry Smith col_lens[3] = a->nz; 556416022c9SBarry Smith 557416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 558d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 559416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 56017ab2063SBarry Smith } 561d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 562606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 563416022c9SBarry Smith 564416022c9SBarry Smith /* store column indices (zero start index) */ 5656f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 566416022c9SBarry Smith 567416022c9SBarry Smith /* store nonzero values */ 5686f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 569b37d52dbSMark F. Adams 570b37d52dbSMark F. Adams ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 571b37d52dbSMark F. Adams if (file) { 57233d57670SJed Brown fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs)); 573b37d52dbSMark F. Adams } 5743a40ed3dSBarry Smith PetscFunctionReturn(0); 57517ab2063SBarry Smith } 576416022c9SBarry Smith 5777dc0baabSHong Zhang static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer) 5787dc0baabSHong Zhang { 5797dc0baabSHong Zhang PetscErrorCode ierr; 5807dc0baabSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 5817dc0baabSHong Zhang PetscInt i,k,m=A->rmap->N; 5827dc0baabSHong Zhang 5837dc0baabSHong Zhang PetscFunctionBegin; 5847dc0baabSHong Zhang ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 5857dc0baabSHong Zhang for (i=0; i<m; i++) { 5867dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 5877dc0baabSHong Zhang for (k=a->i[i]; k<a->i[i+1]; k++) { 5887dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);CHKERRQ(ierr); 5897dc0baabSHong Zhang } 5907dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 5917dc0baabSHong Zhang } 5927dc0baabSHong Zhang ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 5937dc0baabSHong Zhang PetscFunctionReturn(0); 5947dc0baabSHong Zhang } 5957dc0baabSHong Zhang 59609573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 597cd155464SBarry Smith 598dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 599416022c9SBarry Smith { 600416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 601dfbe8321SBarry Smith PetscErrorCode ierr; 60260e0710aSBarry Smith PetscInt i,j,m = A->rmap->n; 603e060cb09SBarry Smith const char *name; 604f3ef73ceSBarry Smith PetscViewerFormat format; 60517ab2063SBarry Smith 6063a40ed3dSBarry Smith PetscFunctionBegin; 6077dc0baabSHong Zhang if (A->structure_only) { 6087dc0baabSHong Zhang ierr = MatView_SeqAIJ_ASCII_structonly(A,viewer);CHKERRQ(ierr); 6097dc0baabSHong Zhang PetscFunctionReturn(0); 6107dc0baabSHong Zhang } 61143e49210SHong Zhang 612b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 61371c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 61497f1f81fSBarry Smith PetscInt nofinalvalue = 0; 61560e0710aSBarry Smith if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) { 616c337ccceSJed Brown /* Need a dummy value to ensure the dimension of the matrix. */ 617d00d2cf4SBarry Smith nofinalvalue = 1; 618d00d2cf4SBarry Smith } 619d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 620d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr); 62177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 622fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX) 623fbfe6fa7SJed Brown ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 624fbfe6fa7SJed Brown #else 62577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 626fbfe6fa7SJed Brown #endif 627b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 62817ab2063SBarry Smith 62917ab2063SBarry Smith for (i=0; i<m; i++) { 63060e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 631aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 632a9bf72d8SJed Brown ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e %18.16e\n",i+1,a->j[j]+1,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 63317ab2063SBarry Smith #else 63460e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr); 63517ab2063SBarry Smith #endif 63617ab2063SBarry Smith } 63717ab2063SBarry Smith } 638d00d2cf4SBarry Smith if (nofinalvalue) { 639c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX) 640c337ccceSJed Brown ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr); 641c337ccceSJed Brown #else 642d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr); 643c337ccceSJed Brown #endif 644d00d2cf4SBarry Smith } 645317d6ea6SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 646fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 647d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 64868369a75SKris Buschelman } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) { 649cd155464SBarry Smith PetscFunctionReturn(0); 650fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 651d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 65244cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 65377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 65460e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 655aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 65636db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 65760e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 65836db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 65960e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 66036db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 66160e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 6626831982aSBarry Smith } 66344cd7ae7SLois Curfman McInnes #else 66460e0710aSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);} 66544cd7ae7SLois Curfman McInnes #endif 66644cd7ae7SLois Curfman McInnes } 667b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 66844cd7ae7SLois Curfman McInnes } 669d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 670fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 67197f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 672d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 673854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr); 674496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 675496be53dSLois Curfman McInnes sptr[i] = nzd+1; 67660e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 677496be53dSLois Curfman McInnes if (a->j[j] >= i) { 678aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 67936db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 680496be53dSLois Curfman McInnes #else 681496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 682496be53dSLois Curfman McInnes #endif 683496be53dSLois Curfman McInnes } 684496be53dSLois Curfman McInnes } 685496be53dSLois Curfman McInnes } 6862e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 68777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 6882e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 6892205254eSKarl Rupp if (i+4<m) { 6902205254eSKarl Rupp 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); 6912205254eSKarl Rupp } else if (i+3<m) { 6922205254eSKarl Rupp 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); 6932205254eSKarl Rupp } else if (i+2<m) { 6942205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr); 6952205254eSKarl Rupp } else if (i+1<m) { 6962205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr); 6972205254eSKarl Rupp } else if (i<m) { 6982205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr); 6992205254eSKarl Rupp } else { 7002205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr); 7012205254eSKarl Rupp } 702496be53dSLois Curfman McInnes } 703b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 704606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 705496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 70660e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 70777431f27SBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);} 708496be53dSLois Curfman McInnes } 709b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 710496be53dSLois Curfman McInnes } 711b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 712496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 71360e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 714496be53dSLois Curfman McInnes if (a->j[j] >= i) { 715aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 71636db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 71760e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 7186831982aSBarry Smith } 719496be53dSLois Curfman McInnes #else 72060e0710aSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);} 721496be53dSLois Curfman McInnes #endif 722496be53dSLois Curfman McInnes } 723496be53dSLois Curfman McInnes } 724b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 725496be53dSLois Curfman McInnes } 726d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 727fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 72897f1f81fSBarry Smith PetscInt cnt = 0,jcnt; 72987828ca2SBarry Smith PetscScalar value; 73068f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX) 73168f1ed48SBarry Smith PetscBool realonly = PETSC_TRUE; 73268f1ed48SBarry Smith 73368f1ed48SBarry Smith for (i=0; i<a->i[m]; i++) { 73468f1ed48SBarry Smith if (PetscImaginaryPart(a->a[i]) != 0.0) { 73568f1ed48SBarry Smith realonly = PETSC_FALSE; 73668f1ed48SBarry Smith break; 73768f1ed48SBarry Smith } 73868f1ed48SBarry Smith } 73968f1ed48SBarry Smith #endif 74002594712SBarry Smith 741d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 74202594712SBarry Smith for (i=0; i<m; i++) { 74302594712SBarry Smith jcnt = 0; 744d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 745e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 74602594712SBarry Smith value = a->a[cnt++]; 747e24b481bSBarry Smith jcnt++; 74802594712SBarry Smith } else { 74902594712SBarry Smith value = 0.0; 75002594712SBarry Smith } 751aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 75268f1ed48SBarry Smith if (realonly) { 75360e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr); 75468f1ed48SBarry Smith } else { 75560e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr); 75668f1ed48SBarry Smith } 75702594712SBarry Smith #else 75860e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr); 75902594712SBarry Smith #endif 76002594712SBarry Smith } 761b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 76202594712SBarry Smith } 763d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 7643c215bfdSMatthew Knepley } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) { 765150b93efSMatthew G. Knepley PetscInt fshift=1; 766d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 7673c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 76819303e72SJonathan Guyer ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr); 7693c215bfdSMatthew Knepley #else 77019303e72SJonathan Guyer ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr); 7713c215bfdSMatthew Knepley #endif 772d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr); 7733c215bfdSMatthew Knepley for (i=0; i<m; i++) { 77460e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 7753c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 776a9a0e077SKarl Rupp ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 7773c215bfdSMatthew Knepley #else 778150b93efSMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr); 7793c215bfdSMatthew Knepley #endif 7803c215bfdSMatthew Knepley } 7813c215bfdSMatthew Knepley } 782d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 7833a40ed3dSBarry Smith } else { 784d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 785d5f3da31SBarry Smith if (A->factortype) { 78616cd7e1dSShri Abhyankar for (i=0; i<m; i++) { 78716cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 78816cd7e1dSShri Abhyankar /* L part */ 78960e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 79016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 79116cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 79260e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 79316cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 7946712e2f1SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr); 79516cd7e1dSShri Abhyankar } else { 79660e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 79716cd7e1dSShri Abhyankar } 79816cd7e1dSShri Abhyankar #else 79960e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 80016cd7e1dSShri Abhyankar #endif 80116cd7e1dSShri Abhyankar } 80216cd7e1dSShri Abhyankar /* diagonal */ 80316cd7e1dSShri Abhyankar j = a->diag[i]; 80416cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 80516cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 80660e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr); 80716cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 8086712e2f1SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));CHKERRQ(ierr); 80916cd7e1dSShri Abhyankar } else { 81060e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr); 81116cd7e1dSShri Abhyankar } 81216cd7e1dSShri Abhyankar #else 81360e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr); 81416cd7e1dSShri Abhyankar #endif 81516cd7e1dSShri Abhyankar 81616cd7e1dSShri Abhyankar /* U part */ 81760e0710aSBarry Smith for (j=a->diag[i+1]+1; j<a->diag[i]; j++) { 81816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 81916cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 82060e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 82116cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 82222ab088eSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr); 82316cd7e1dSShri Abhyankar } else { 82460e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 82516cd7e1dSShri Abhyankar } 82616cd7e1dSShri Abhyankar #else 82760e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 82816cd7e1dSShri Abhyankar #endif 82916cd7e1dSShri Abhyankar } 83016cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 83116cd7e1dSShri Abhyankar } 83216cd7e1dSShri Abhyankar } else { 83317ab2063SBarry Smith for (i=0; i<m; i++) { 83477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 83560e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 836aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 83736db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 83860e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 83936db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 84060e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 8413a40ed3dSBarry Smith } else { 84260e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 84317ab2063SBarry Smith } 84417ab2063SBarry Smith #else 84560e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 84617ab2063SBarry Smith #endif 84717ab2063SBarry Smith } 848b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 84917ab2063SBarry Smith } 85016cd7e1dSShri Abhyankar } 851d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 85217ab2063SBarry Smith } 853b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 8543a40ed3dSBarry Smith PetscFunctionReturn(0); 855416022c9SBarry Smith } 856416022c9SBarry Smith 8579804daf3SBarry Smith #include <petscdraw.h> 858dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 859416022c9SBarry Smith { 860480ef9eaSBarry Smith Mat A = (Mat) Aa; 861416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 862dfbe8321SBarry Smith PetscErrorCode ierr; 863383922c3SLisandro Dalcin PetscInt i,j,m = A->rmap->n; 864383922c3SLisandro Dalcin int color; 865b05fc000SLisandro Dalcin PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r; 866b0a32e0cSBarry Smith PetscViewer viewer; 867f3ef73ceSBarry Smith PetscViewerFormat format; 868cddf8d76SBarry Smith 8693a40ed3dSBarry Smith PetscFunctionBegin; 870480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 871b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 872b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 873383922c3SLisandro Dalcin 874416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 8750513a670SBarry Smith 876fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 877383922c3SLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 8780513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 879b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 880416022c9SBarry Smith for (i=0; i<m; i++) { 881cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 882bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 883bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 88436db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 885b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 886cddf8d76SBarry Smith } 887cddf8d76SBarry Smith } 888b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 889cddf8d76SBarry Smith for (i=0; i<m; i++) { 890cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 891bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 892bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 893cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 894b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 895cddf8d76SBarry Smith } 896cddf8d76SBarry Smith } 897b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 898cddf8d76SBarry Smith for (i=0; i<m; i++) { 899cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 900bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 901bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 90236db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 903b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 904416022c9SBarry Smith } 905416022c9SBarry Smith } 906383922c3SLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 9070513a670SBarry Smith } else { 9080513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 9090513a670SBarry Smith /* first determine max of all nonzero values */ 910b05fc000SLisandro Dalcin PetscReal minv = 0.0, maxv = 0.0; 911383922c3SLisandro Dalcin PetscInt nz = a->nz, count = 0; 912b0a32e0cSBarry Smith PetscDraw popup; 9130513a670SBarry Smith 9140513a670SBarry Smith for (i=0; i<nz; i++) { 9150513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 9160513a670SBarry Smith } 917383922c3SLisandro Dalcin if (minv >= maxv) maxv = minv + PETSC_SMALL; 918b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 91945f3bb6eSLisandro Dalcin ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr); 920383922c3SLisandro Dalcin 921383922c3SLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 9220513a670SBarry Smith for (i=0; i<m; i++) { 923383922c3SLisandro Dalcin y_l = m - i - 1.0; 924383922c3SLisandro Dalcin y_r = y_l + 1.0; 925bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 926383922c3SLisandro Dalcin x_l = a->j[j]; 927383922c3SLisandro Dalcin x_r = x_l + 1.0; 928b05fc000SLisandro Dalcin color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv); 929b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 9300513a670SBarry Smith count++; 9310513a670SBarry Smith } 9320513a670SBarry Smith } 933383922c3SLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 9340513a670SBarry Smith } 935480ef9eaSBarry Smith PetscFunctionReturn(0); 936480ef9eaSBarry Smith } 937cddf8d76SBarry Smith 9389804daf3SBarry Smith #include <petscdraw.h> 939dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 940480ef9eaSBarry Smith { 941dfbe8321SBarry Smith PetscErrorCode ierr; 942b0a32e0cSBarry Smith PetscDraw draw; 94336db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 944ace3abfcSBarry Smith PetscBool isnull; 945480ef9eaSBarry Smith 946480ef9eaSBarry Smith PetscFunctionBegin; 947b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 948b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 949480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 950480ef9eaSBarry Smith 951d0f46423SBarry Smith xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0; 952480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 953b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 954832b7cebSLisandro Dalcin ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 955b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 9560298fd71SBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr); 957832b7cebSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 9583a40ed3dSBarry Smith PetscFunctionReturn(0); 959416022c9SBarry Smith } 960416022c9SBarry Smith 961dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 962416022c9SBarry Smith { 963dfbe8321SBarry Smith PetscErrorCode ierr; 964ace3abfcSBarry Smith PetscBool iascii,isbinary,isdraw; 965416022c9SBarry Smith 9663a40ed3dSBarry Smith PetscFunctionBegin; 967251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 968251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 969251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 970c45a1595SBarry Smith if (iascii) { 9713a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 9720f5bd95cSBarry Smith } else if (isbinary) { 9733a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 9740f5bd95cSBarry Smith } else if (isdraw) { 9753a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 97611aeaf0aSBarry Smith } 9774108e4d5SBarry Smith ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr); 9783a40ed3dSBarry Smith PetscFunctionReturn(0); 97917ab2063SBarry Smith } 98019bcc07fSBarry Smith 981dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 98217ab2063SBarry Smith { 983416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 9846849ba73SBarry Smith PetscErrorCode ierr; 98597f1f81fSBarry Smith PetscInt fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax; 986d0f46423SBarry Smith PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0; 98754f21887SBarry Smith MatScalar *aa = a->a,*ap; 9883447b6efSHong Zhang PetscReal ratio = 0.6; 98917ab2063SBarry Smith 9903a40ed3dSBarry Smith PetscFunctionBegin; 9913a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 99217ab2063SBarry Smith 99343ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 99417ab2063SBarry Smith for (i=1; i<m; i++) { 995416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 99617ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 99794a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 99817ab2063SBarry Smith if (fshift) { 999bfeeae90SHong Zhang ip = aj + ai[i]; 1000bfeeae90SHong Zhang ap = aa + ai[i]; 100117ab2063SBarry Smith N = ailen[i]; 100217ab2063SBarry Smith for (j=0; j<N; j++) { 100317ab2063SBarry Smith ip[j-fshift] = ip[j]; 1004876c6284SHong Zhang if (!A->structure_only) ap[j-fshift] = ap[j]; 100517ab2063SBarry Smith } 100617ab2063SBarry Smith } 100717ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 100817ab2063SBarry Smith } 100917ab2063SBarry Smith if (m) { 101017ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 101117ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 101217ab2063SBarry Smith } 10137b083b7cSBarry Smith 101417ab2063SBarry Smith /* reset ilen and imax for each row */ 10157b083b7cSBarry Smith a->nonzerorowcnt = 0; 1016396832f4SHong Zhang if (A->structure_only) { 1017396832f4SHong Zhang ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr); 1018396832f4SHong Zhang } else { /* !A->structure_only */ 101917ab2063SBarry Smith for (i=0; i<m; i++) { 102017ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 10217b083b7cSBarry Smith a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0); 102217ab2063SBarry Smith } 1023396832f4SHong Zhang } 1024bfeeae90SHong Zhang a->nz = ai[m]; 102565e19b50SBarry 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); 102617ab2063SBarry Smith 102709f38230SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1028d0f46423SBarry 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); 1029ae15b995SBarry Smith ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr); 1030ae15b995SBarry Smith ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr); 10312205254eSKarl Rupp 10328e58a170SBarry Smith A->info.mallocs += a->reallocs; 1033dd5f02e7SSatish Balay a->reallocs = 0; 10346712e2f1SBarry Smith A->info.nz_unneeded = (PetscReal)fshift; 103536db0b34SBarry Smith a->rmax = rmax; 10364e220ebcSLois Curfman McInnes 1037396832f4SHong Zhang if (!A->structure_only) { 103811e456e1SBarry Smith ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 1039396832f4SHong Zhang } 10404108e4d5SBarry Smith ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr); 1041acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 10423a40ed3dSBarry Smith PetscFunctionReturn(0); 104317ab2063SBarry Smith } 104417ab2063SBarry Smith 104599cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 104699cafbc1SBarry Smith { 104799cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 104899cafbc1SBarry Smith PetscInt i,nz = a->nz; 104954f21887SBarry Smith MatScalar *aa = a->a; 1050acf2f550SJed Brown PetscErrorCode ierr; 105199cafbc1SBarry Smith 105299cafbc1SBarry Smith PetscFunctionBegin; 105399cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 1054acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 105599cafbc1SBarry Smith PetscFunctionReturn(0); 105699cafbc1SBarry Smith } 105799cafbc1SBarry Smith 105899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 105999cafbc1SBarry Smith { 106099cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 106199cafbc1SBarry Smith PetscInt i,nz = a->nz; 106254f21887SBarry Smith MatScalar *aa = a->a; 1063acf2f550SJed Brown PetscErrorCode ierr; 106499cafbc1SBarry Smith 106599cafbc1SBarry Smith PetscFunctionBegin; 106699cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 1067acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 106899cafbc1SBarry Smith PetscFunctionReturn(0); 106999cafbc1SBarry Smith } 107099cafbc1SBarry Smith 1071dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 107217ab2063SBarry Smith { 1073416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1074dfbe8321SBarry Smith PetscErrorCode ierr; 10753a40ed3dSBarry Smith 10763a40ed3dSBarry Smith PetscFunctionBegin; 1077d0f46423SBarry Smith ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 1078acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 10793a40ed3dSBarry Smith PetscFunctionReturn(0); 108017ab2063SBarry Smith } 1081416022c9SBarry Smith 1082dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 108317ab2063SBarry Smith { 1084416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1085dfbe8321SBarry Smith PetscErrorCode ierr; 1086d5d45c9bSBarry Smith 10873a40ed3dSBarry Smith PetscFunctionBegin; 1088aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1089d0f46423SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz); 109017ab2063SBarry Smith #endif 1091e6b907acSBarry Smith ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr); 10926bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 10936bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 109405b42c5fSBarry Smith ierr = PetscFree(a->diag);CHKERRQ(ierr); 1095d48dcb14SBarry Smith ierr = PetscFree(a->ibdiag);CHKERRQ(ierr); 109605b42c5fSBarry Smith ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr); 1097846b4da1SFande Kong ierr = PetscFree(a->ipre);CHKERRQ(ierr); 109871f1c65dSBarry Smith ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr); 109905b42c5fSBarry Smith ierr = PetscFree(a->solve_work);CHKERRQ(ierr); 11006bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 110105b42c5fSBarry Smith ierr = PetscFree(a->saved_values);CHKERRQ(ierr); 11026bf464f9SBarry Smith ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr); 1103cd6b891eSBarry Smith ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr); 11040b7e3e3dSHong Zhang ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr); 1105a30b2313SHong Zhang 11064108e4d5SBarry Smith ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr); 1107bf0cc555SLisandro Dalcin ierr = PetscFree(A->data);CHKERRQ(ierr); 1108901853e0SKris Buschelman 1109dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr); 1110bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr); 1111bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr); 1112bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr); 1113bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr); 1114bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr); 1115bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr); 1116af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 1117af8000cdSHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr); 1118af8000cdSHong Zhang #endif 111963c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE) 112063c07aadSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);CHKERRQ(ierr); 11213dad0653Sstefano_zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);CHKERRQ(ierr); 112263c07aadSStefano Zampini #endif 1123b49cda9fSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr); 1124bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr); 1125bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr); 1126846b4da1SFande Kong ierr = PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);CHKERRQ(ierr); 1127bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr); 1128bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr); 112975d48cdbSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);CHKERRQ(ierr); 11303a40ed3dSBarry Smith PetscFunctionReturn(0); 113117ab2063SBarry Smith } 113217ab2063SBarry Smith 1133ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg) 113417ab2063SBarry Smith { 1135416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 11364846f1f5SKris Buschelman PetscErrorCode ierr; 11373a40ed3dSBarry Smith 11383a40ed3dSBarry Smith PetscFunctionBegin; 1139a65d3064SKris Buschelman switch (op) { 1140a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 11414e0d8c25SBarry Smith a->roworiented = flg; 1142a65d3064SKris Buschelman break; 1143a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 1144a9817697SBarry Smith a->keepnonzeropattern = flg; 1145a65d3064SKris Buschelman break; 1146512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 1147512a5fc5SBarry Smith a->nonew = (flg ? 0 : 1); 1148a65d3064SKris Buschelman break; 1149a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 11504e0d8c25SBarry Smith a->nonew = (flg ? -1 : 0); 1151a65d3064SKris Buschelman break; 1152a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 11534e0d8c25SBarry Smith a->nonew = (flg ? -2 : 0); 1154a65d3064SKris Buschelman break; 115528b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 115628b2fa4aSMatthew Knepley a->nounused = (flg ? -1 : 0); 115728b2fa4aSMatthew Knepley break; 1158a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 11594e0d8c25SBarry Smith a->ignorezeroentries = flg; 11600df259c2SBarry Smith break; 11613d472b54SHong Zhang case MAT_SPD: 1162b1646e73SJed Brown case MAT_SYMMETRIC: 1163b1646e73SJed Brown case MAT_STRUCTURALLY_SYMMETRIC: 1164b1646e73SJed Brown case MAT_HERMITIAN: 1165b1646e73SJed Brown case MAT_SYMMETRY_ETERNAL: 1166957cac9fSHong Zhang case MAT_STRUCTURE_ONLY: 11675021d80fSJed Brown /* These options are handled directly by MatSetOption() */ 11685021d80fSJed Brown break; 11694e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1170a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 1171a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 1172290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 1173a65d3064SKris Buschelman break; 1174b87ac2d8SJed Brown case MAT_USE_INODES: 1175b87ac2d8SJed Brown /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */ 1176b87ac2d8SJed Brown break; 1177c10200c1SHong Zhang case MAT_SUBMAT_SINGLEIS: 1178c10200c1SHong Zhang A->submat_singleis = flg; 1179c10200c1SHong Zhang break; 1180a65d3064SKris Buschelman default: 1181e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 1182a65d3064SKris Buschelman } 11834108e4d5SBarry Smith ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr); 11843a40ed3dSBarry Smith PetscFunctionReturn(0); 118517ab2063SBarry Smith } 118617ab2063SBarry Smith 1187dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 118817ab2063SBarry Smith { 1189416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 11906849ba73SBarry Smith PetscErrorCode ierr; 1191d3e70bfaSHong Zhang PetscInt i,j,n,*ai=a->i,*aj=a->j,nz; 119235e7444dSHong Zhang PetscScalar *aa=a->a,*x,zero=0.0; 119317ab2063SBarry Smith 11943a40ed3dSBarry Smith PetscFunctionBegin; 1195d3e70bfaSHong Zhang ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 1196e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 119735e7444dSHong Zhang 1198d5f3da31SBarry Smith if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) { 1199d3e70bfaSHong Zhang PetscInt *diag=a->diag; 120035e7444dSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 12012c990fa1SHong Zhang for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]]; 120235e7444dSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 120335e7444dSHong Zhang PetscFunctionReturn(0); 120435e7444dSHong Zhang } 120535e7444dSHong Zhang 12062dcb1b2aSMatthew Knepley ierr = VecSet(v,zero);CHKERRQ(ierr); 12071ebc52fbSHong Zhang ierr = VecGetArray(v,&x);CHKERRQ(ierr); 120835e7444dSHong Zhang for (i=0; i<n; i++) { 120935e7444dSHong Zhang nz = ai[i+1] - ai[i]; 12102f5a7c2eSBarry Smith if (!nz) x[i] = 0.0; 121135e7444dSHong Zhang for (j=ai[i]; j<ai[i+1]; j++) { 121235e7444dSHong Zhang if (aj[j] == i) { 121335e7444dSHong Zhang x[i] = aa[j]; 121417ab2063SBarry Smith break; 121517ab2063SBarry Smith } 121617ab2063SBarry Smith } 121717ab2063SBarry Smith } 12181ebc52fbSHong Zhang ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 12193a40ed3dSBarry Smith PetscFunctionReturn(0); 122017ab2063SBarry Smith } 122117ab2063SBarry Smith 1222c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 1223dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 122417ab2063SBarry Smith { 1225416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1226d9ca1df4SBarry Smith PetscScalar *y; 1227d9ca1df4SBarry Smith const PetscScalar *x; 1228dfbe8321SBarry Smith PetscErrorCode ierr; 1229d0f46423SBarry Smith PetscInt m = A->rmap->n; 12305c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1231d9ca1df4SBarry Smith const MatScalar *v; 1232a77337e4SBarry Smith PetscScalar alpha; 1233d9ca1df4SBarry Smith PetscInt n,i,j; 1234d9ca1df4SBarry Smith const PetscInt *idx,*ii,*ridx=NULL; 12353447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 1236ace3abfcSBarry Smith PetscBool usecprow = cprow.use; 12375c897100SBarry Smith #endif 123817ab2063SBarry Smith 12393a40ed3dSBarry Smith PetscFunctionBegin; 12402e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 1241d9ca1df4SBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 12421ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 12435c897100SBarry Smith 12445c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1245bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 12465c897100SBarry Smith #else 12473447b6efSHong Zhang if (usecprow) { 12483447b6efSHong Zhang m = cprow.nrows; 12493447b6efSHong Zhang ii = cprow.i; 12507b2bb3b9SHong Zhang ridx = cprow.rindex; 12513447b6efSHong Zhang } else { 12523447b6efSHong Zhang ii = a->i; 12533447b6efSHong Zhang } 125417ab2063SBarry Smith for (i=0; i<m; i++) { 12553447b6efSHong Zhang idx = a->j + ii[i]; 12563447b6efSHong Zhang v = a->a + ii[i]; 12573447b6efSHong Zhang n = ii[i+1] - ii[i]; 12583447b6efSHong Zhang if (usecprow) { 12597b2bb3b9SHong Zhang alpha = x[ridx[i]]; 12603447b6efSHong Zhang } else { 126117ab2063SBarry Smith alpha = x[i]; 12623447b6efSHong Zhang } 126304fbf559SBarry Smith for (j=0; j<n; j++) y[idx[j]] += alpha*v[j]; 126417ab2063SBarry Smith } 12655c897100SBarry Smith #endif 1266dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1267d9ca1df4SBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 12681ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 12693a40ed3dSBarry Smith PetscFunctionReturn(0); 127017ab2063SBarry Smith } 127117ab2063SBarry Smith 1272dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 12735c897100SBarry Smith { 1274dfbe8321SBarry Smith PetscErrorCode ierr; 12755c897100SBarry Smith 12765c897100SBarry Smith PetscFunctionBegin; 1277170fe5c8SBarry Smith ierr = VecSet(yy,0.0);CHKERRQ(ierr); 12785c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 12795c897100SBarry Smith PetscFunctionReturn(0); 12805c897100SBarry Smith } 12815c897100SBarry Smith 1282c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 128378b84d54SShri Abhyankar 1284dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 128517ab2063SBarry Smith { 1286416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1287d9fead3dSBarry Smith PetscScalar *y; 128854f21887SBarry Smith const PetscScalar *x; 128954f21887SBarry Smith const MatScalar *aa; 1290dfbe8321SBarry Smith PetscErrorCode ierr; 1291003131ecSBarry Smith PetscInt m=A->rmap->n; 12920298fd71SBarry Smith const PetscInt *aj,*ii,*ridx=NULL; 12937b083b7cSBarry Smith PetscInt n,i; 1294362ced78SSatish Balay PetscScalar sum; 1295ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 129617ab2063SBarry Smith 1297b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 129897952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 1299fee21e36SBarry Smith #endif 1300fee21e36SBarry Smith 13013a40ed3dSBarry Smith PetscFunctionBegin; 13023649974fSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 13031ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 1304416022c9SBarry Smith ii = a->i; 13054eb6d288SHong Zhang if (usecprow) { /* use compressed row format */ 13064f390cb1SBarry Smith ierr = PetscMemzero(y,m*sizeof(PetscScalar));CHKERRQ(ierr); 130797952fefSHong Zhang m = a->compressedrow.nrows; 130897952fefSHong Zhang ii = a->compressedrow.i; 130997952fefSHong Zhang ridx = a->compressedrow.rindex; 131097952fefSHong Zhang for (i=0; i<m; i++) { 131197952fefSHong Zhang n = ii[i+1] - ii[i]; 131297952fefSHong Zhang aj = a->j + ii[i]; 131397952fefSHong Zhang aa = a->a + ii[i]; 131497952fefSHong Zhang sum = 0.0; 1315003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 1316003131ecSBarry Smith /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 131797952fefSHong Zhang y[*ridx++] = sum; 131897952fefSHong Zhang } 131997952fefSHong Zhang } else { /* do not use compressed row format */ 1320b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 13213d3eaba7SBarry Smith aj = a->j; 13223d3eaba7SBarry Smith aa = a->a; 1323b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 1324b05257ddSBarry Smith #else 132517ab2063SBarry Smith for (i=0; i<m; i++) { 1326003131ecSBarry Smith n = ii[i+1] - ii[i]; 1327003131ecSBarry Smith aj = a->j + ii[i]; 1328003131ecSBarry Smith aa = a->a + ii[i]; 132917ab2063SBarry Smith sum = 0.0; 1330003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 133117ab2063SBarry Smith y[i] = sum; 133217ab2063SBarry Smith } 13338d195f9aSBarry Smith #endif 1334b05257ddSBarry Smith } 13357b083b7cSBarry Smith ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 13363649974fSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 13371ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 13383a40ed3dSBarry Smith PetscFunctionReturn(0); 133917ab2063SBarry Smith } 134017ab2063SBarry Smith 1341b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy) 1342b434eb95SMatthew G. Knepley { 1343b434eb95SMatthew G. Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1344b434eb95SMatthew G. Knepley PetscScalar *y; 1345b434eb95SMatthew G. Knepley const PetscScalar *x; 1346b434eb95SMatthew G. Knepley const MatScalar *aa; 1347b434eb95SMatthew G. Knepley PetscErrorCode ierr; 1348b434eb95SMatthew G. Knepley PetscInt m=A->rmap->n; 1349b434eb95SMatthew G. Knepley const PetscInt *aj,*ii,*ridx=NULL; 1350b434eb95SMatthew G. Knepley PetscInt n,i,nonzerorow=0; 1351b434eb95SMatthew G. Knepley PetscScalar sum; 1352b434eb95SMatthew G. Knepley PetscBool usecprow=a->compressedrow.use; 1353b434eb95SMatthew G. Knepley 1354b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 1355b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa) 1356b434eb95SMatthew G. Knepley #endif 1357b434eb95SMatthew G. Knepley 1358b434eb95SMatthew G. Knepley PetscFunctionBegin; 1359b434eb95SMatthew G. Knepley ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1360b434eb95SMatthew G. Knepley ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 1361b434eb95SMatthew G. Knepley if (usecprow) { /* use compressed row format */ 1362b434eb95SMatthew G. Knepley m = a->compressedrow.nrows; 1363b434eb95SMatthew G. Knepley ii = a->compressedrow.i; 1364b434eb95SMatthew G. Knepley ridx = a->compressedrow.rindex; 1365b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1366b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1367b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1368b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1369b434eb95SMatthew G. Knepley sum = 0.0; 1370b434eb95SMatthew G. Knepley nonzerorow += (n>0); 1371b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1372b434eb95SMatthew G. Knepley /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 1373b434eb95SMatthew G. Knepley y[*ridx++] = sum; 1374b434eb95SMatthew G. Knepley } 1375b434eb95SMatthew G. Knepley } else { /* do not use compressed row format */ 13763d3eaba7SBarry Smith ii = a->i; 1377b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1378b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1379b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1380b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1381b434eb95SMatthew G. Knepley sum = 0.0; 1382b434eb95SMatthew G. Knepley nonzerorow += (n>0); 1383b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1384b434eb95SMatthew G. Knepley y[i] = sum; 1385b434eb95SMatthew G. Knepley } 1386b434eb95SMatthew G. Knepley } 1387b434eb95SMatthew G. Knepley ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr); 1388b434eb95SMatthew G. Knepley ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1389b434eb95SMatthew G. Knepley ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 1390b434eb95SMatthew G. Knepley PetscFunctionReturn(0); 1391b434eb95SMatthew G. Knepley } 1392b434eb95SMatthew G. Knepley 1393b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1394b434eb95SMatthew G. Knepley { 1395b434eb95SMatthew G. Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1396b434eb95SMatthew G. Knepley PetscScalar *y,*z; 1397b434eb95SMatthew G. Knepley const PetscScalar *x; 1398b434eb95SMatthew G. Knepley const MatScalar *aa; 1399b434eb95SMatthew G. Knepley PetscErrorCode ierr; 1400b434eb95SMatthew G. Knepley PetscInt m = A->rmap->n,*aj,*ii; 1401b434eb95SMatthew G. Knepley PetscInt n,i,*ridx=NULL; 1402b434eb95SMatthew G. Knepley PetscScalar sum; 1403b434eb95SMatthew G. Knepley PetscBool usecprow=a->compressedrow.use; 1404b434eb95SMatthew G. Knepley 1405b434eb95SMatthew G. Knepley PetscFunctionBegin; 1406b434eb95SMatthew G. Knepley ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1407d9ca1df4SBarry Smith ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 1408b434eb95SMatthew G. Knepley if (usecprow) { /* use compressed row format */ 1409b434eb95SMatthew G. Knepley if (zz != yy) { 1410b434eb95SMatthew G. Knepley ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr); 1411b434eb95SMatthew G. Knepley } 1412b434eb95SMatthew G. Knepley m = a->compressedrow.nrows; 1413b434eb95SMatthew G. Knepley ii = a->compressedrow.i; 1414b434eb95SMatthew G. Knepley ridx = a->compressedrow.rindex; 1415b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1416b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1417b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1418b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1419b434eb95SMatthew G. Knepley sum = y[*ridx]; 1420b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1421b434eb95SMatthew G. Knepley z[*ridx++] = sum; 1422b434eb95SMatthew G. Knepley } 1423b434eb95SMatthew G. Knepley } else { /* do not use compressed row format */ 14243d3eaba7SBarry Smith ii = a->i; 1425b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1426b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1427b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1428b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1429b434eb95SMatthew G. Knepley sum = y[i]; 1430b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1431b434eb95SMatthew G. Knepley z[i] = sum; 1432b434eb95SMatthew G. Knepley } 1433b434eb95SMatthew G. Knepley } 1434b434eb95SMatthew G. Knepley ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1435b434eb95SMatthew G. Knepley ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1436d9ca1df4SBarry Smith ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 1437b434eb95SMatthew G. Knepley PetscFunctionReturn(0); 1438b434eb95SMatthew G. Knepley } 1439b434eb95SMatthew G. Knepley 1440c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h> 1441dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 144217ab2063SBarry Smith { 1443416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1444f15663dcSBarry Smith PetscScalar *y,*z; 1445f15663dcSBarry Smith const PetscScalar *x; 144654f21887SBarry Smith const MatScalar *aa; 1447dfbe8321SBarry Smith PetscErrorCode ierr; 1448d9ca1df4SBarry Smith const PetscInt *aj,*ii,*ridx=NULL; 1449d9ca1df4SBarry Smith PetscInt m = A->rmap->n,n,i; 1450362ced78SSatish Balay PetscScalar sum; 1451ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 14529ea0dfa2SSatish Balay 14533a40ed3dSBarry Smith PetscFunctionBegin; 1454f15663dcSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1455d9ca1df4SBarry Smith ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 14564eb6d288SHong Zhang if (usecprow) { /* use compressed row format */ 14574eb6d288SHong Zhang if (zz != yy) { 14584eb6d288SHong Zhang ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr); 14594eb6d288SHong Zhang } 146097952fefSHong Zhang m = a->compressedrow.nrows; 146197952fefSHong Zhang ii = a->compressedrow.i; 146297952fefSHong Zhang ridx = a->compressedrow.rindex; 146397952fefSHong Zhang for (i=0; i<m; i++) { 146497952fefSHong Zhang n = ii[i+1] - ii[i]; 146597952fefSHong Zhang aj = a->j + ii[i]; 146697952fefSHong Zhang aa = a->a + ii[i]; 146797952fefSHong Zhang sum = y[*ridx]; 1468f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 146997952fefSHong Zhang z[*ridx++] = sum; 147097952fefSHong Zhang } 147197952fefSHong Zhang } else { /* do not use compressed row format */ 14723d3eaba7SBarry Smith ii = a->i; 1473f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 14743d3eaba7SBarry Smith aj = a->j; 14753d3eaba7SBarry Smith aa = a->a; 1476f15663dcSBarry Smith fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 1477f15663dcSBarry Smith #else 147817ab2063SBarry Smith for (i=0; i<m; i++) { 1479f15663dcSBarry Smith n = ii[i+1] - ii[i]; 1480f15663dcSBarry Smith aj = a->j + ii[i]; 1481f15663dcSBarry Smith aa = a->a + ii[i]; 148217ab2063SBarry Smith sum = y[i]; 1483f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 148417ab2063SBarry Smith z[i] = sum; 148517ab2063SBarry Smith } 148602ab625aSSatish Balay #endif 1487f15663dcSBarry Smith } 1488dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1489f15663dcSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1490d9ca1df4SBarry Smith ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 14913a40ed3dSBarry Smith PetscFunctionReturn(0); 149217ab2063SBarry Smith } 149317ab2063SBarry Smith 149417ab2063SBarry Smith /* 149517ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 149617ab2063SBarry Smith */ 1497dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 149817ab2063SBarry Smith { 1499416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15006849ba73SBarry Smith PetscErrorCode ierr; 1501d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n; 150217ab2063SBarry Smith 15033a40ed3dSBarry Smith PetscFunctionBegin; 150409f38230SBarry Smith if (!a->diag) { 1505785e854fSJed Brown ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr); 15063bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr); 150709f38230SBarry Smith } 1508d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 150909f38230SBarry Smith a->diag[i] = a->i[i+1]; 1510bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1511bfeeae90SHong Zhang if (a->j[j] == i) { 151209f38230SBarry Smith a->diag[i] = j; 151317ab2063SBarry Smith break; 151417ab2063SBarry Smith } 151517ab2063SBarry Smith } 151617ab2063SBarry Smith } 15173a40ed3dSBarry Smith PetscFunctionReturn(0); 151817ab2063SBarry Smith } 151917ab2063SBarry Smith 1520be5855fcSBarry Smith /* 1521be5855fcSBarry Smith Checks for missing diagonals 1522be5855fcSBarry Smith */ 1523ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool *missing,PetscInt *d) 1524be5855fcSBarry Smith { 1525be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15267734d3b5SMatthew G. Knepley PetscInt *diag,*ii = a->i,i; 1527*994fe344SLisandro Dalcin PetscErrorCode ierr; 1528be5855fcSBarry Smith 1529be5855fcSBarry Smith PetscFunctionBegin; 153009f38230SBarry Smith *missing = PETSC_FALSE; 15317734d3b5SMatthew G. Knepley if (A->rmap->n > 0 && !ii) { 153209f38230SBarry Smith *missing = PETSC_TRUE; 153309f38230SBarry Smith if (d) *d = 0; 1534*994fe344SLisandro Dalcin ierr = PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");CHKERRQ(ierr); 153509f38230SBarry Smith } else { 1536f1e2ffcdSBarry Smith diag = a->diag; 1537d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 15387734d3b5SMatthew G. Knepley if (diag[i] >= ii[i+1]) { 153909f38230SBarry Smith *missing = PETSC_TRUE; 154009f38230SBarry Smith if (d) *d = i; 1541*994fe344SLisandro Dalcin ierr = PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);CHKERRQ(ierr); 1542358d2f5dSShri Abhyankar break; 154309f38230SBarry Smith } 1544be5855fcSBarry Smith } 1545be5855fcSBarry Smith } 1546be5855fcSBarry Smith PetscFunctionReturn(0); 1547be5855fcSBarry Smith } 1548be5855fcSBarry Smith 1549422a814eSBarry Smith /* 1550422a814eSBarry Smith Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways 1551422a814eSBarry Smith */ 15527087cfbeSBarry Smith PetscErrorCode MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift) 155371f1c65dSBarry Smith { 155471f1c65dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 155571f1c65dSBarry Smith PetscErrorCode ierr; 1556d0f46423SBarry Smith PetscInt i,*diag,m = A->rmap->n; 155754f21887SBarry Smith MatScalar *v = a->a; 155854f21887SBarry Smith PetscScalar *idiag,*mdiag; 155971f1c65dSBarry Smith 156071f1c65dSBarry Smith PetscFunctionBegin; 156171f1c65dSBarry Smith if (a->idiagvalid) PetscFunctionReturn(0); 156271f1c65dSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 156371f1c65dSBarry Smith diag = a->diag; 156471f1c65dSBarry Smith if (!a->idiag) { 1565dcca6d9dSJed Brown ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr); 15663bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr); 156771f1c65dSBarry Smith v = a->a; 156871f1c65dSBarry Smith } 156971f1c65dSBarry Smith mdiag = a->mdiag; 157071f1c65dSBarry Smith idiag = a->idiag; 157171f1c65dSBarry Smith 1572422a814eSBarry Smith if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) { 157371f1c65dSBarry Smith for (i=0; i<m; i++) { 157471f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 1575899639b0SHong Zhang if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */ 1576899639b0SHong Zhang if (PetscRealPart(fshift)) { 1577899639b0SHong Zhang ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr); 15787b6c816cSBarry Smith A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 15797b6c816cSBarry Smith A->factorerror_zeropivot_value = 0.0; 15807b6c816cSBarry Smith A->factorerror_zeropivot_row = i; 1581a6fa060aSHong Zhang } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i); 1582899639b0SHong Zhang } 158371f1c65dSBarry Smith idiag[i] = 1.0/v[diag[i]]; 158471f1c65dSBarry Smith } 158571f1c65dSBarry Smith ierr = PetscLogFlops(m);CHKERRQ(ierr); 158671f1c65dSBarry Smith } else { 158771f1c65dSBarry Smith for (i=0; i<m; i++) { 158871f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 158971f1c65dSBarry Smith idiag[i] = omega/(fshift + v[diag[i]]); 159071f1c65dSBarry Smith } 1591dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr); 159271f1c65dSBarry Smith } 159371f1c65dSBarry Smith a->idiagvalid = PETSC_TRUE; 159471f1c65dSBarry Smith PetscFunctionReturn(0); 159571f1c65dSBarry Smith } 159671f1c65dSBarry Smith 1597c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h> 159841f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 159917ab2063SBarry Smith { 1600416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1601e6d1f457SBarry Smith PetscScalar *x,d,sum,*t,scale; 16023d3eaba7SBarry Smith const MatScalar *v,*idiag=0,*mdiag; 160354f21887SBarry Smith const PetscScalar *b, *bs,*xb, *ts; 1604dfbe8321SBarry Smith PetscErrorCode ierr; 16053d3eaba7SBarry Smith PetscInt n,m = A->rmap->n,i; 160697f1f81fSBarry Smith const PetscInt *idx,*diag; 160717ab2063SBarry Smith 16083a40ed3dSBarry Smith PetscFunctionBegin; 1609b965ef7fSBarry Smith its = its*lits; 161091723122SBarry Smith 161171f1c65dSBarry Smith if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */ 161271f1c65dSBarry Smith if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);} 161371f1c65dSBarry Smith a->fshift = fshift; 161471f1c65dSBarry Smith a->omega = omega; 1615ed480e8bSBarry Smith 161671f1c65dSBarry Smith diag = a->diag; 161771f1c65dSBarry Smith t = a->ssor_work; 1618ed480e8bSBarry Smith idiag = a->idiag; 161971f1c65dSBarry Smith mdiag = a->mdiag; 1620ed480e8bSBarry Smith 16211ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 16223649974fSBarry Smith ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr); 1623ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 162417ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 162517ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1626ed480e8bSBarry Smith bs = b; 162717ab2063SBarry Smith for (i=0; i<m; i++) { 162871f1c65dSBarry Smith d = fshift + mdiag[i]; 1629416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1630ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1631ed480e8bSBarry Smith v = a->a + diag[i] + 1; 163217ab2063SBarry Smith sum = b[i]*d/omega; 1633003131ecSBarry Smith PetscSparseDensePlusDot(sum,bs,v,idx,n); 163417ab2063SBarry Smith x[i] = sum; 163517ab2063SBarry Smith } 16361ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 16373649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1638efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 16393a40ed3dSBarry Smith PetscFunctionReturn(0); 164017ab2063SBarry Smith } 1641c783ea89SBarry Smith 16422205254eSKarl Rupp if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 16432205254eSKarl Rupp else if (flag & SOR_EISENSTAT) { 164417ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 1645887ee2caSBarry Smith U is upper triangular, E = D/omega; This routine applies 164617ab2063SBarry Smith 164717ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 164817ab2063SBarry Smith 1649887ee2caSBarry Smith to a vector efficiently using Eisenstat's trick. 165017ab2063SBarry Smith */ 165117ab2063SBarry Smith scale = (2.0/omega) - 1.0; 165217ab2063SBarry Smith 165317ab2063SBarry Smith /* x = (E + U)^{-1} b */ 165417ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1655416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1656ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1657ed480e8bSBarry Smith v = a->a + diag[i] + 1; 165817ab2063SBarry Smith sum = b[i]; 1659e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1660ed480e8bSBarry Smith x[i] = sum*idiag[i]; 166117ab2063SBarry Smith } 166217ab2063SBarry Smith 166317ab2063SBarry Smith /* t = b - (2*E - D)x */ 1664416022c9SBarry Smith v = a->a; 16652205254eSKarl Rupp for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i]; 166617ab2063SBarry Smith 166717ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1668ed480e8bSBarry Smith ts = t; 1669416022c9SBarry Smith diag = a->diag; 167017ab2063SBarry Smith for (i=0; i<m; i++) { 1671416022c9SBarry Smith n = diag[i] - a->i[i]; 1672ed480e8bSBarry Smith idx = a->j + a->i[i]; 1673ed480e8bSBarry Smith v = a->a + a->i[i]; 167417ab2063SBarry Smith sum = t[i]; 1675003131ecSBarry Smith PetscSparseDenseMinusDot(sum,ts,v,idx,n); 1676ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1677733d66baSBarry Smith /* x = x + t */ 1678733d66baSBarry Smith x[i] += t[i]; 167917ab2063SBarry Smith } 168017ab2063SBarry Smith 1681dc0b31edSSatish Balay ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr); 16821ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 16833649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 16843a40ed3dSBarry Smith PetscFunctionReturn(0); 168517ab2063SBarry Smith } 168617ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 168717ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) { 168817ab2063SBarry Smith for (i=0; i<m; i++) { 1689416022c9SBarry Smith n = diag[i] - a->i[i]; 1690ed480e8bSBarry Smith idx = a->j + a->i[i]; 1691ed480e8bSBarry Smith v = a->a + a->i[i]; 169217ab2063SBarry Smith sum = b[i]; 1693e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 16945c99c7daSBarry Smith t[i] = sum; 1695ed480e8bSBarry Smith x[i] = sum*idiag[i]; 169617ab2063SBarry Smith } 16975c99c7daSBarry Smith xb = t; 1698efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 16993a40ed3dSBarry Smith } else xb = b; 170017ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) { 170117ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1702416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1703ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1704ed480e8bSBarry Smith v = a->a + diag[i] + 1; 170517ab2063SBarry Smith sum = xb[i]; 1706e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 17075c99c7daSBarry Smith if (xb == b) { 1708ed480e8bSBarry Smith x[i] = sum*idiag[i]; 17095c99c7daSBarry Smith } else { 1710b19a5dc2SMark Adams x[i] = (1-omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 171117ab2063SBarry Smith } 17125c99c7daSBarry Smith } 1713b19a5dc2SMark Adams ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */ 171417ab2063SBarry Smith } 171517ab2063SBarry Smith its--; 171617ab2063SBarry Smith } 171717ab2063SBarry Smith while (its--) { 171817ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) { 171917ab2063SBarry Smith for (i=0; i<m; i++) { 1720b19a5dc2SMark Adams /* lower */ 1721b19a5dc2SMark Adams n = diag[i] - a->i[i]; 1722ed480e8bSBarry Smith idx = a->j + a->i[i]; 1723ed480e8bSBarry Smith v = a->a + a->i[i]; 172417ab2063SBarry Smith sum = b[i]; 1725e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1726b19a5dc2SMark Adams t[i] = sum; /* save application of the lower-triangular part */ 1727b19a5dc2SMark Adams /* upper */ 1728b19a5dc2SMark Adams n = a->i[i+1] - diag[i] - 1; 1729b19a5dc2SMark Adams idx = a->j + diag[i] + 1; 1730b19a5dc2SMark Adams v = a->a + diag[i] + 1; 1731b19a5dc2SMark Adams PetscSparseDenseMinusDot(sum,x,v,idx,n); 1732b19a5dc2SMark Adams x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 173317ab2063SBarry Smith } 1734b19a5dc2SMark Adams xb = t; 17359f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1736b19a5dc2SMark Adams } else xb = b; 173717ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) { 173817ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1739b19a5dc2SMark Adams sum = xb[i]; 1740b19a5dc2SMark Adams if (xb == b) { 1741b19a5dc2SMark Adams /* whole matrix (no checkpointing available) */ 1742416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1743ed480e8bSBarry Smith idx = a->j + a->i[i]; 1744ed480e8bSBarry Smith v = a->a + a->i[i]; 1745e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1746ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 1747b19a5dc2SMark Adams } else { /* lower-triangular part has been saved, so only apply upper-triangular */ 1748b19a5dc2SMark Adams n = a->i[i+1] - diag[i] - 1; 1749b19a5dc2SMark Adams idx = a->j + diag[i] + 1; 1750b19a5dc2SMark Adams v = a->a + diag[i] + 1; 1751b19a5dc2SMark Adams PetscSparseDenseMinusDot(sum,x,v,idx,n); 1752b19a5dc2SMark Adams x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 175317ab2063SBarry Smith } 1754b19a5dc2SMark Adams } 1755b19a5dc2SMark Adams if (xb == b) { 17569f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1757b19a5dc2SMark Adams } else { 1758b19a5dc2SMark Adams ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */ 1759b19a5dc2SMark Adams } 176017ab2063SBarry Smith } 176117ab2063SBarry Smith } 17621ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 17633649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1764365a8a9eSBarry Smith PetscFunctionReturn(0); 176517ab2063SBarry Smith } 176617ab2063SBarry Smith 17672af78befSBarry Smith 1768dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 176917ab2063SBarry Smith { 1770416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 17714e220ebcSLois Curfman McInnes 17723a40ed3dSBarry Smith PetscFunctionBegin; 17734e220ebcSLois Curfman McInnes info->block_size = 1.0; 17744e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 17754e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 17764e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 17774e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 17788e58a170SBarry Smith info->mallocs = (double)A->info.mallocs; 17797adad957SLisandro Dalcin info->memory = ((PetscObject)A)->mem; 1780d5f3da31SBarry Smith if (A->factortype) { 17814e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 17824e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 17834e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 17844e220ebcSLois Curfman McInnes } else { 17854e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 17864e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 17874e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 17884e220ebcSLois Curfman McInnes } 17893a40ed3dSBarry Smith PetscFunctionReturn(0); 179017ab2063SBarry Smith } 179117ab2063SBarry Smith 17922b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 179317ab2063SBarry Smith { 1794416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1795c7da8527SEric Chamberland PetscInt i,m = A->rmap->n - 1; 17966849ba73SBarry Smith PetscErrorCode ierr; 179797b48c8fSBarry Smith const PetscScalar *xx; 179897b48c8fSBarry Smith PetscScalar *bb; 1799c7da8527SEric Chamberland PetscInt d = 0; 180017ab2063SBarry Smith 18013a40ed3dSBarry Smith PetscFunctionBegin; 180297b48c8fSBarry Smith if (x && b) { 180397b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 180497b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 180597b48c8fSBarry Smith for (i=0; i<N; i++) { 180697b48c8fSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 180797b48c8fSBarry Smith bb[rows[i]] = diag*xx[rows[i]]; 180897b48c8fSBarry Smith } 180997b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 181097b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 181197b48c8fSBarry Smith } 181297b48c8fSBarry Smith 1813a9817697SBarry Smith if (a->keepnonzeropattern) { 1814f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1815e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1816bfeeae90SHong Zhang ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1817f1e2ffcdSBarry Smith } 1818f4df32b1SMatthew Knepley if (diag != 0.0) { 1819c7da8527SEric Chamberland for (i=0; i<N; i++) { 1820c7da8527SEric Chamberland d = rows[i]; 1821c7da8527SEric Chamberland if (a->diag[d] >= a->i[d+1]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in the zeroed row %D",d); 1822c7da8527SEric Chamberland } 1823f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1824f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 1825f1e2ffcdSBarry Smith } 1826f1e2ffcdSBarry Smith } 1827f1e2ffcdSBarry Smith } else { 1828f4df32b1SMatthew Knepley if (diag != 0.0) { 182917ab2063SBarry Smith for (i=0; i<N; i++) { 1830e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 18317ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1832416022c9SBarry Smith a->ilen[rows[i]] = 1; 1833f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 1834bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 18357ae801bdSBarry Smith } else { /* in case row was completely empty */ 1836f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 183717ab2063SBarry Smith } 183817ab2063SBarry Smith } 18393a40ed3dSBarry Smith } else { 184017ab2063SBarry Smith for (i=0; i<N; i++) { 1841e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 1842416022c9SBarry Smith a->ilen[rows[i]] = 0; 184317ab2063SBarry Smith } 184417ab2063SBarry Smith } 1845e56f5c9eSBarry Smith A->nonzerostate++; 1846f1e2ffcdSBarry Smith } 18474099cc6bSBarry Smith ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 18483a40ed3dSBarry Smith PetscFunctionReturn(0); 184917ab2063SBarry Smith } 185017ab2063SBarry Smith 18516e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 18526e169961SBarry Smith { 18536e169961SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 18546e169961SBarry Smith PetscInt i,j,m = A->rmap->n - 1,d = 0; 18556e169961SBarry Smith PetscErrorCode ierr; 18562b40b63fSBarry Smith PetscBool missing,*zeroed,vecs = PETSC_FALSE; 18576e169961SBarry Smith const PetscScalar *xx; 18586e169961SBarry Smith PetscScalar *bb; 18596e169961SBarry Smith 18606e169961SBarry Smith PetscFunctionBegin; 18616e169961SBarry Smith if (x && b) { 18626e169961SBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 18636e169961SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 18642b40b63fSBarry Smith vecs = PETSC_TRUE; 18656e169961SBarry Smith } 18661795a4d1SJed Brown ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr); 18676e169961SBarry Smith for (i=0; i<N; i++) { 18686e169961SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 18696e169961SBarry Smith ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 18702205254eSKarl Rupp 18716e169961SBarry Smith zeroed[rows[i]] = PETSC_TRUE; 18726e169961SBarry Smith } 18736e169961SBarry Smith for (i=0; i<A->rmap->n; i++) { 18746e169961SBarry Smith if (!zeroed[i]) { 18756e169961SBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 18766e169961SBarry Smith if (zeroed[a->j[j]]) { 18772b40b63fSBarry Smith if (vecs) bb[i] -= a->a[j]*xx[a->j[j]]; 18786e169961SBarry Smith a->a[j] = 0.0; 18796e169961SBarry Smith } 18806e169961SBarry Smith } 18812b40b63fSBarry Smith } else if (vecs) bb[i] = diag*xx[i]; 18826e169961SBarry Smith } 18836e169961SBarry Smith if (x && b) { 18846e169961SBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 18856e169961SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 18866e169961SBarry Smith } 18876e169961SBarry Smith ierr = PetscFree(zeroed);CHKERRQ(ierr); 18886e169961SBarry Smith if (diag != 0.0) { 18896e169961SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 18901d5a398dSstefano_zampini if (missing) { 18911d5a398dSstefano_zampini if (a->nonew) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d); 18921d5a398dSstefano_zampini else { 18931d5a398dSstefano_zampini for (i=0; i<N; i++) { 18941d5a398dSstefano_zampini ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 18951d5a398dSstefano_zampini } 18961d5a398dSstefano_zampini } 18971d5a398dSstefano_zampini } else { 18986e169961SBarry Smith for (i=0; i<N; i++) { 18996e169961SBarry Smith a->a[a->diag[rows[i]]] = diag; 19006e169961SBarry Smith } 19016e169961SBarry Smith } 19021d5a398dSstefano_zampini } 19034099cc6bSBarry Smith ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 19046e169961SBarry Smith PetscFunctionReturn(0); 19056e169961SBarry Smith } 19066e169961SBarry Smith 1907a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 190817ab2063SBarry Smith { 1909416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 191097f1f81fSBarry Smith PetscInt *itmp; 191117ab2063SBarry Smith 19123a40ed3dSBarry Smith PetscFunctionBegin; 1913e32f2f54SBarry Smith if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 191417ab2063SBarry Smith 1915416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1916bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 191717ab2063SBarry Smith if (idx) { 1918bfeeae90SHong Zhang itmp = a->j + a->i[row]; 191926fbe8dcSKarl Rupp if (*nz) *idx = itmp; 192017ab2063SBarry Smith else *idx = 0; 192117ab2063SBarry Smith } 19223a40ed3dSBarry Smith PetscFunctionReturn(0); 192317ab2063SBarry Smith } 192417ab2063SBarry Smith 1925bfeeae90SHong Zhang /* remove this function? */ 1926a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 192717ab2063SBarry Smith { 19283a40ed3dSBarry Smith PetscFunctionBegin; 19293a40ed3dSBarry Smith PetscFunctionReturn(0); 193017ab2063SBarry Smith } 193117ab2063SBarry Smith 1932dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 193317ab2063SBarry Smith { 1934416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 193554f21887SBarry Smith MatScalar *v = a->a; 193636db0b34SBarry Smith PetscReal sum = 0.0; 19376849ba73SBarry Smith PetscErrorCode ierr; 193897f1f81fSBarry Smith PetscInt i,j; 193917ab2063SBarry Smith 19403a40ed3dSBarry Smith PetscFunctionBegin; 194117ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1942570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FP16) 1943570b7f6dSBarry Smith PetscBLASInt one = 1,nz = a->nz; 1944570b7f6dSBarry Smith *nrm = BLASnrm2_(&nz,v,&one); 1945570b7f6dSBarry Smith #else 1946416022c9SBarry Smith for (i=0; i<a->nz; i++) { 194736db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 194817ab2063SBarry Smith } 19498f1a2a5eSBarry Smith *nrm = PetscSqrtReal(sum); 1950570b7f6dSBarry Smith #endif 195151f70360SJed Brown ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr); 19523a40ed3dSBarry Smith } else if (type == NORM_1) { 195336db0b34SBarry Smith PetscReal *tmp; 195497f1f81fSBarry Smith PetscInt *jj = a->j; 19551795a4d1SJed Brown ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr); 1956064f8208SBarry Smith *nrm = 0.0; 1957416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1958bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 195917ab2063SBarry Smith } 1960d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 1961064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 196217ab2063SBarry Smith } 1963606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 196451f70360SJed Brown ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr); 19653a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1966064f8208SBarry Smith *nrm = 0.0; 1967d0f46423SBarry Smith for (j=0; j<A->rmap->n; j++) { 1968bfeeae90SHong Zhang v = a->a + a->i[j]; 196917ab2063SBarry Smith sum = 0.0; 1970416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1971cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 197217ab2063SBarry Smith } 1973064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 197417ab2063SBarry Smith } 197551f70360SJed Brown ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr); 1976f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm"); 19773a40ed3dSBarry Smith PetscFunctionReturn(0); 197817ab2063SBarry Smith } 197917ab2063SBarry Smith 19804e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */ 19814e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B) 19824e938277SHong Zhang { 19834e938277SHong Zhang PetscErrorCode ierr; 19844e938277SHong Zhang PetscInt i,j,anzj; 19854e938277SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b; 19864e938277SHong Zhang PetscInt an=A->cmap->N,am=A->rmap->N; 19874e938277SHong Zhang PetscInt *ati,*atj,*atfill,*ai=a->i,*aj=a->j; 19884e938277SHong Zhang 19894e938277SHong Zhang PetscFunctionBegin; 19904e938277SHong Zhang /* Allocate space for symbolic transpose info and work array */ 1991854ce69bSBarry Smith ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr); 1992785e854fSJed Brown ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr); 1993785e854fSJed Brown ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr); 19944e938277SHong Zhang 19954e938277SHong Zhang /* Walk through aj and count ## of non-zeros in each row of A^T. */ 19964e938277SHong Zhang /* Note: offset by 1 for fast conversion into csr format. */ 199726fbe8dcSKarl Rupp for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1; 19984e938277SHong Zhang /* Form ati for csr format of A^T. */ 199926fbe8dcSKarl Rupp for (i=0;i<an;i++) ati[i+1] += ati[i]; 20004e938277SHong Zhang 20014e938277SHong Zhang /* Copy ati into atfill so we have locations of the next free space in atj */ 20024e938277SHong Zhang ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr); 20034e938277SHong Zhang 20044e938277SHong Zhang /* Walk through A row-wise and mark nonzero entries of A^T. */ 20054e938277SHong Zhang for (i=0;i<am;i++) { 20064e938277SHong Zhang anzj = ai[i+1] - ai[i]; 20074e938277SHong Zhang for (j=0;j<anzj;j++) { 20084e938277SHong Zhang atj[atfill[*aj]] = i; 20094e938277SHong Zhang atfill[*aj++] += 1; 20104e938277SHong Zhang } 20114e938277SHong Zhang } 20124e938277SHong Zhang 20134e938277SHong Zhang /* Clean up temporary space and complete requests. */ 20144e938277SHong Zhang ierr = PetscFree(atfill);CHKERRQ(ierr); 2015ce94432eSBarry Smith ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr); 201633d57670SJed Brown ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr); 2017a2f3521dSMark F. Adams 20184e938277SHong Zhang b = (Mat_SeqAIJ*)((*B)->data); 20194e938277SHong Zhang b->free_a = PETSC_FALSE; 20204e938277SHong Zhang b->free_ij = PETSC_TRUE; 20214e938277SHong Zhang b->nonew = 0; 20224e938277SHong Zhang PetscFunctionReturn(0); 20234e938277SHong Zhang } 20244e938277SHong Zhang 2025fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B) 202617ab2063SBarry Smith { 2027416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2028416022c9SBarry Smith Mat C; 20296849ba73SBarry Smith PetscErrorCode ierr; 2030d0f46423SBarry Smith PetscInt i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col; 203154f21887SBarry Smith MatScalar *array = a->a; 203217ab2063SBarry Smith 20333a40ed3dSBarry Smith PetscFunctionBegin; 2034cf37664fSBarry Smith if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) { 2035854ce69bSBarry Smith ierr = PetscCalloc1(1+A->cmap->n,&col);CHKERRQ(ierr); 2036bfeeae90SHong Zhang 2037bfeeae90SHong Zhang for (i=0; i<ai[m]; i++) col[aj[i]] += 1; 2038ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2039d0f46423SBarry Smith ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr); 204033d57670SJed Brown ierr = MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr); 20417adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2042ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr); 2043606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 2044a541d17aSBarry Smith } else { 2045a541d17aSBarry Smith C = *B; 2046a541d17aSBarry Smith } 2047a541d17aSBarry Smith 204817ab2063SBarry Smith for (i=0; i<m; i++) { 204917ab2063SBarry Smith len = ai[i+1]-ai[i]; 205087d4246cSBarry Smith ierr = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 2051b9b97703SBarry Smith array += len; 2052b9b97703SBarry Smith aj += len; 205317ab2063SBarry Smith } 20546d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20556d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 205617ab2063SBarry Smith 2057cf37664fSBarry Smith if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) { 2058416022c9SBarry Smith *B = C; 205917ab2063SBarry Smith } else { 206028be2f97SBarry Smith ierr = MatHeaderMerge(A,&C);CHKERRQ(ierr); 206117ab2063SBarry Smith } 20623a40ed3dSBarry Smith PetscFunctionReturn(0); 206317ab2063SBarry Smith } 206417ab2063SBarry Smith 20657087cfbeSBarry Smith PetscErrorCode MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 2066cd0d46ebSvictorle { 20673d3eaba7SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data; 206854f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 206954f21887SBarry Smith MatScalar *va,*vb; 20706849ba73SBarry Smith PetscErrorCode ierr; 207197f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 2072cd0d46ebSvictorle 2073cd0d46ebSvictorle PetscFunctionBegin; 2074cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 2075cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 20765485867bSBarry Smith if (ma!=nb || na!=mb) { 20775485867bSBarry Smith *f = PETSC_FALSE; 20785485867bSBarry Smith PetscFunctionReturn(0); 20795485867bSBarry Smith } 2080cd0d46ebSvictorle aii = aij->i; bii = bij->i; 2081cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 2082cd0d46ebSvictorle va = aij->a; vb = bij->a; 2083785e854fSJed Brown ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr); 2084785e854fSJed Brown ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr); 2085cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 2086cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 2087cd0d46ebSvictorle 2088cd0d46ebSvictorle *f = PETSC_TRUE; 2089cd0d46ebSvictorle for (i=0; i<ma; i++) { 2090cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 209197f1f81fSBarry Smith PetscInt idc,idr; 20925485867bSBarry Smith PetscScalar vc,vr; 2093cd0d46ebSvictorle /* column/row index/value */ 20945485867bSBarry Smith idc = adx[aptr[i]]; 20955485867bSBarry Smith idr = bdx[bptr[idc]]; 20965485867bSBarry Smith vc = va[aptr[i]]; 20975485867bSBarry Smith vr = vb[bptr[idc]]; 20985485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 20995485867bSBarry Smith *f = PETSC_FALSE; 21005485867bSBarry Smith goto done; 2101cd0d46ebSvictorle } else { 21025485867bSBarry Smith aptr[i]++; 21035485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 2104cd0d46ebSvictorle } 2105cd0d46ebSvictorle } 2106cd0d46ebSvictorle } 2107cd0d46ebSvictorle done: 2108cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 21093aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 2110cd0d46ebSvictorle PetscFunctionReturn(0); 2111cd0d46ebSvictorle } 2112cd0d46ebSvictorle 21137087cfbeSBarry Smith PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 21141cbb95d3SBarry Smith { 21153d3eaba7SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data; 211654f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 211754f21887SBarry Smith MatScalar *va,*vb; 21181cbb95d3SBarry Smith PetscErrorCode ierr; 21191cbb95d3SBarry Smith PetscInt ma,na,mb,nb, i; 21201cbb95d3SBarry Smith 21211cbb95d3SBarry Smith PetscFunctionBegin; 21221cbb95d3SBarry Smith ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 21231cbb95d3SBarry Smith ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 21241cbb95d3SBarry Smith if (ma!=nb || na!=mb) { 21251cbb95d3SBarry Smith *f = PETSC_FALSE; 21261cbb95d3SBarry Smith PetscFunctionReturn(0); 21271cbb95d3SBarry Smith } 21281cbb95d3SBarry Smith aii = aij->i; bii = bij->i; 21291cbb95d3SBarry Smith adx = aij->j; bdx = bij->j; 21301cbb95d3SBarry Smith va = aij->a; vb = bij->a; 2131785e854fSJed Brown ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr); 2132785e854fSJed Brown ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr); 21331cbb95d3SBarry Smith for (i=0; i<ma; i++) aptr[i] = aii[i]; 21341cbb95d3SBarry Smith for (i=0; i<mb; i++) bptr[i] = bii[i]; 21351cbb95d3SBarry Smith 21361cbb95d3SBarry Smith *f = PETSC_TRUE; 21371cbb95d3SBarry Smith for (i=0; i<ma; i++) { 21381cbb95d3SBarry Smith while (aptr[i]<aii[i+1]) { 21391cbb95d3SBarry Smith PetscInt idc,idr; 21401cbb95d3SBarry Smith PetscScalar vc,vr; 21411cbb95d3SBarry Smith /* column/row index/value */ 21421cbb95d3SBarry Smith idc = adx[aptr[i]]; 21431cbb95d3SBarry Smith idr = bdx[bptr[idc]]; 21441cbb95d3SBarry Smith vc = va[aptr[i]]; 21451cbb95d3SBarry Smith vr = vb[bptr[idc]]; 21461cbb95d3SBarry Smith if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) { 21471cbb95d3SBarry Smith *f = PETSC_FALSE; 21481cbb95d3SBarry Smith goto done; 21491cbb95d3SBarry Smith } else { 21501cbb95d3SBarry Smith aptr[i]++; 21511cbb95d3SBarry Smith if (B || i!=idc) bptr[idc]++; 21521cbb95d3SBarry Smith } 21531cbb95d3SBarry Smith } 21541cbb95d3SBarry Smith } 21551cbb95d3SBarry Smith done: 21561cbb95d3SBarry Smith ierr = PetscFree(aptr);CHKERRQ(ierr); 21571cbb95d3SBarry Smith ierr = PetscFree(bptr);CHKERRQ(ierr); 21581cbb95d3SBarry Smith PetscFunctionReturn(0); 21591cbb95d3SBarry Smith } 21601cbb95d3SBarry Smith 2161ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 21629e29f15eSvictorle { 2163dfbe8321SBarry Smith PetscErrorCode ierr; 21646e111a19SKarl Rupp 21659e29f15eSvictorle PetscFunctionBegin; 21665485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 21679e29f15eSvictorle PetscFunctionReturn(0); 21689e29f15eSvictorle } 21699e29f15eSvictorle 2170ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 21711cbb95d3SBarry Smith { 21721cbb95d3SBarry Smith PetscErrorCode ierr; 21736e111a19SKarl Rupp 21741cbb95d3SBarry Smith PetscFunctionBegin; 21751cbb95d3SBarry Smith ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 21761cbb95d3SBarry Smith PetscFunctionReturn(0); 21771cbb95d3SBarry Smith } 21781cbb95d3SBarry Smith 2179dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 218017ab2063SBarry Smith { 2181416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2182fff8e43fSBarry Smith const PetscScalar *l,*r; 2183fff8e43fSBarry Smith PetscScalar x; 218454f21887SBarry Smith MatScalar *v; 2185dfbe8321SBarry Smith PetscErrorCode ierr; 2186fff8e43fSBarry Smith PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz; 2187fff8e43fSBarry Smith const PetscInt *jj; 218817ab2063SBarry Smith 21893a40ed3dSBarry Smith PetscFunctionBegin; 219017ab2063SBarry Smith if (ll) { 21913ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 21923ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 2193e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 2194e32f2f54SBarry Smith if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 2195fff8e43fSBarry Smith ierr = VecGetArrayRead(ll,&l);CHKERRQ(ierr); 2196416022c9SBarry Smith v = a->a; 219717ab2063SBarry Smith for (i=0; i<m; i++) { 219817ab2063SBarry Smith x = l[i]; 2199416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 22002205254eSKarl Rupp for (j=0; j<M; j++) (*v++) *= x; 220117ab2063SBarry Smith } 2202fff8e43fSBarry Smith ierr = VecRestoreArrayRead(ll,&l);CHKERRQ(ierr); 2203efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 220417ab2063SBarry Smith } 220517ab2063SBarry Smith if (rr) { 2206e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 2207e32f2f54SBarry Smith if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 2208fff8e43fSBarry Smith ierr = VecGetArrayRead(rr,&r);CHKERRQ(ierr); 2209416022c9SBarry Smith v = a->a; jj = a->j; 22102205254eSKarl Rupp for (i=0; i<nz; i++) (*v++) *= r[*jj++]; 2211fff8e43fSBarry Smith ierr = VecRestoreArrayRead(rr,&r);CHKERRQ(ierr); 2212efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 221317ab2063SBarry Smith } 2214acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 22153a40ed3dSBarry Smith PetscFunctionReturn(0); 221617ab2063SBarry Smith } 221717ab2063SBarry Smith 22187dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 221917ab2063SBarry Smith { 2220db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 22216849ba73SBarry Smith PetscErrorCode ierr; 2222d0f46423SBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens; 222397f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 22245d0c19d7SBarry Smith const PetscInt *irow,*icol; 22255d0c19d7SBarry Smith PetscInt nrows,ncols; 222697f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 222754f21887SBarry Smith MatScalar *a_new,*mat_a; 2228416022c9SBarry Smith Mat C; 2229cdc6f3adSToby Isaac PetscBool stride; 223017ab2063SBarry Smith 22313a40ed3dSBarry Smith PetscFunctionBegin; 223299141d43SSatish Balay 223317ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 2234b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 2235b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 223617ab2063SBarry Smith 2237251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr); 2238ff718158SBarry Smith if (stride) { 2239ff718158SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 2240ff718158SBarry Smith } else { 2241ff718158SBarry Smith first = 0; 2242ff718158SBarry Smith step = 0; 2243ff718158SBarry Smith } 2244fee21e36SBarry Smith if (stride && step == 1) { 224502834360SBarry Smith /* special case of contiguous rows */ 2246dcca6d9dSJed Brown ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr); 224702834360SBarry Smith /* loop over new rows determining lens and starting points */ 224802834360SBarry Smith for (i=0; i<nrows; i++) { 2249bfeeae90SHong Zhang kstart = ai[irow[i]]; 2250a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 2251a91a9bebSLisandro Dalcin starts[i] = kstart; 225202834360SBarry Smith for (k=kstart; k<kend; k++) { 2253bfeeae90SHong Zhang if (aj[k] >= first) { 225402834360SBarry Smith starts[i] = k; 225502834360SBarry Smith break; 225602834360SBarry Smith } 225702834360SBarry Smith } 2258a2744918SBarry Smith sum = 0; 225902834360SBarry Smith while (k < kend) { 2260bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 2261a2744918SBarry Smith sum++; 226202834360SBarry Smith } 2263a2744918SBarry Smith lens[i] = sum; 226402834360SBarry Smith } 226502834360SBarry Smith /* create submatrix */ 2266cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 226797f1f81fSBarry Smith PetscInt n_cols,n_rows; 226808480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 2269e32f2f54SBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 2270d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 227108480c60SBarry Smith C = *B; 22723a40ed3dSBarry Smith } else { 22733bef6203SJed Brown PetscInt rbs,cbs; 2274ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2275f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 22763bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 22773bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 22783bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 22797adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2280ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 228108480c60SBarry Smith } 2282db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 2283db02288aSLois Curfman McInnes 228402834360SBarry Smith /* loop over rows inserting into submatrix */ 2285db02288aSLois Curfman McInnes a_new = c->a; 2286db02288aSLois Curfman McInnes j_new = c->j; 2287db02288aSLois Curfman McInnes i_new = c->i; 2288bfeeae90SHong Zhang 228902834360SBarry Smith for (i=0; i<nrows; i++) { 2290a2744918SBarry Smith ii = starts[i]; 2291a2744918SBarry Smith lensi = lens[i]; 2292a2744918SBarry Smith for (k=0; k<lensi; k++) { 2293a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 229402834360SBarry Smith } 229587828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 2296a2744918SBarry Smith a_new += lensi; 2297a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 2298a2744918SBarry Smith c->ilen[i] = lensi; 229902834360SBarry Smith } 23000e83c824SBarry Smith ierr = PetscFree2(lens,starts);CHKERRQ(ierr); 23013a40ed3dSBarry Smith } else { 230202834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 23031795a4d1SJed Brown ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr); 2304854ce69bSBarry Smith ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr); 23054dcab191SBarry Smith for (i=0; i<ncols; i++) { 23064dcab191SBarry Smith #if defined(PETSC_USE_DEBUG) 23074dcab191SBarry 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); 23084dcab191SBarry Smith #endif 23094dcab191SBarry Smith smap[icol[i]] = i+1; 23104dcab191SBarry Smith } 23114dcab191SBarry Smith 231202834360SBarry Smith /* determine lens of each row */ 231302834360SBarry Smith for (i=0; i<nrows; i++) { 2314bfeeae90SHong Zhang kstart = ai[irow[i]]; 231502834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 231602834360SBarry Smith lens[i] = 0; 231702834360SBarry Smith for (k=kstart; k<kend; k++) { 2318bfeeae90SHong Zhang if (smap[aj[k]]) { 231902834360SBarry Smith lens[i]++; 232002834360SBarry Smith } 232102834360SBarry Smith } 232202834360SBarry Smith } 232317ab2063SBarry Smith /* Create and fill new matrix */ 2324a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 2325ace3abfcSBarry Smith PetscBool equal; 23260f5bd95cSBarry Smith 232799141d43SSatish Balay c = (Mat_SeqAIJ*)((*B)->data); 2328e32f2f54SBarry Smith if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 2329d0f46423SBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr); 2330f23aa3ddSBarry Smith if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 2331d0f46423SBarry Smith ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 233208480c60SBarry Smith C = *B; 23333a40ed3dSBarry Smith } else { 23343bef6203SJed Brown PetscInt rbs,cbs; 2335ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2336f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 23373bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 23383bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 23393bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 23407adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2341ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 234208480c60SBarry Smith } 234399141d43SSatish Balay c = (Mat_SeqAIJ*)(C->data); 234417ab2063SBarry Smith for (i=0; i<nrows; i++) { 234599141d43SSatish Balay row = irow[i]; 2346bfeeae90SHong Zhang kstart = ai[row]; 234799141d43SSatish Balay kend = kstart + a->ilen[row]; 2348bfeeae90SHong Zhang mat_i = c->i[i]; 234999141d43SSatish Balay mat_j = c->j + mat_i; 235099141d43SSatish Balay mat_a = c->a + mat_i; 235199141d43SSatish Balay mat_ilen = c->ilen + i; 235217ab2063SBarry Smith for (k=kstart; k<kend; k++) { 2353bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 2354ed480e8bSBarry Smith *mat_j++ = tcol - 1; 235599141d43SSatish Balay *mat_a++ = a->a[k]; 235699141d43SSatish Balay (*mat_ilen)++; 235799141d43SSatish Balay 235817ab2063SBarry Smith } 235917ab2063SBarry Smith } 236017ab2063SBarry Smith } 236102834360SBarry Smith /* Free work space */ 236202834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 2363606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 2364606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 2365cdc6f3adSToby Isaac /* sort */ 2366cdc6f3adSToby Isaac for (i = 0; i < nrows; i++) { 2367cdc6f3adSToby Isaac PetscInt ilen; 2368cdc6f3adSToby Isaac 2369cdc6f3adSToby Isaac mat_i = c->i[i]; 2370cdc6f3adSToby Isaac mat_j = c->j + mat_i; 2371cdc6f3adSToby Isaac mat_a = c->a + mat_i; 2372cdc6f3adSToby Isaac ilen = c->ilen[i]; 2373390e1bf2SBarry Smith ierr = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr); 2374cdc6f3adSToby Isaac } 237502834360SBarry Smith } 23766d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 23776d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 237817ab2063SBarry Smith 237917ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 2380416022c9SBarry Smith *B = C; 23813a40ed3dSBarry Smith PetscFunctionReturn(0); 238217ab2063SBarry Smith } 238317ab2063SBarry Smith 2384fc08c53fSHong Zhang PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat) 238582d44351SHong Zhang { 238682d44351SHong Zhang PetscErrorCode ierr; 238782d44351SHong Zhang Mat B; 238882d44351SHong Zhang 238982d44351SHong Zhang PetscFunctionBegin; 2390c2d650bdSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 239182d44351SHong Zhang ierr = MatCreate(subComm,&B);CHKERRQ(ierr); 239282d44351SHong Zhang ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr); 239333d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr); 239482d44351SHong Zhang ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 239582d44351SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr); 239682d44351SHong Zhang *subMat = B; 2397c2d650bdSHong Zhang } else { 2398c2d650bdSHong Zhang ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 2399c2d650bdSHong Zhang } 240082d44351SHong Zhang PetscFunctionReturn(0); 240182d44351SHong Zhang } 240282d44351SHong Zhang 24039a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info) 2404a871dcd8SBarry Smith { 240563b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2406dfbe8321SBarry Smith PetscErrorCode ierr; 240763b91edcSBarry Smith Mat outA; 2408ace3abfcSBarry Smith PetscBool row_identity,col_identity; 240963b91edcSBarry Smith 24103a40ed3dSBarry Smith PetscFunctionBegin; 2411e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 24121df811f5SHong Zhang 2413b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 2414b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 2415a871dcd8SBarry Smith 241663b91edcSBarry Smith outA = inA; 2417d5f3da31SBarry Smith outA->factortype = MAT_FACTOR_LU; 2418f6224b95SHong Zhang ierr = PetscFree(inA->solvertype);CHKERRQ(ierr); 2419f6224b95SHong Zhang ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr); 24202205254eSKarl Rupp 2421c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 24226bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 24232205254eSKarl Rupp 2424c3122656SLisandro Dalcin a->row = row; 24252205254eSKarl Rupp 2426c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 24276bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 24282205254eSKarl Rupp 2429c3122656SLisandro Dalcin a->col = col; 243063b91edcSBarry Smith 243136db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 24326bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 24334c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 24343bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr); 2435f0ec6fceSSatish Balay 243694a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 2437854ce69bSBarry Smith ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr); 24383bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 243994a9d846SBarry Smith } 244063b91edcSBarry Smith 2441f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 2442137fb511SHong Zhang if (row_identity && col_identity) { 2443ad04f41aSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr); 2444137fb511SHong Zhang } else { 2445719d5645SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr); 2446137fb511SHong Zhang } 24473a40ed3dSBarry Smith PetscFunctionReturn(0); 2448a871dcd8SBarry Smith } 2449a871dcd8SBarry Smith 2450f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 2451f0b747eeSBarry Smith { 2452f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2453f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 2454efee365bSSatish Balay PetscErrorCode ierr; 2455c5df96a5SBarry Smith PetscBLASInt one = 1,bnz; 24563a40ed3dSBarry Smith 24573a40ed3dSBarry Smith PetscFunctionBegin; 2458c5df96a5SBarry Smith ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr); 24598b83055fSJed Brown PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one)); 2460efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 2461acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr); 24623a40ed3dSBarry Smith PetscFunctionReturn(0); 2463f0b747eeSBarry Smith } 2464f0b747eeSBarry Smith 2465f68bb481SHong Zhang PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj) 246616b64355SHong Zhang { 246716b64355SHong Zhang PetscErrorCode ierr; 246816b64355SHong Zhang PetscInt i; 246916b64355SHong Zhang 247016b64355SHong Zhang PetscFunctionBegin; 247116b64355SHong Zhang if (!submatj->id) { /* delete data that are linked only to submats[id=0] */ 247216b64355SHong Zhang ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr); 247316b64355SHong Zhang 247416b64355SHong Zhang for (i=0; i<submatj->nrqr; ++i) { 247516b64355SHong Zhang ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr); 247616b64355SHong Zhang } 247716b64355SHong Zhang ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr); 247816b64355SHong Zhang 247916b64355SHong Zhang if (submatj->rbuf1) { 248016b64355SHong Zhang ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr); 248116b64355SHong Zhang ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr); 248216b64355SHong Zhang } 248316b64355SHong Zhang 248416b64355SHong Zhang for (i=0; i<submatj->nrqs; ++i) { 248516b64355SHong Zhang ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr); 248616b64355SHong Zhang } 248716b64355SHong Zhang ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr); 248816b64355SHong Zhang ierr = PetscFree(submatj->pa);CHKERRQ(ierr); 248916b64355SHong Zhang } 249016b64355SHong Zhang 249116b64355SHong Zhang #if defined(PETSC_USE_CTABLE) 249216b64355SHong Zhang ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr); 249316b64355SHong Zhang if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);} 249416b64355SHong Zhang ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr); 249516b64355SHong Zhang #else 249616b64355SHong Zhang ierr = PetscFree(submatj->rmap);CHKERRQ(ierr); 249716b64355SHong Zhang #endif 249816b64355SHong Zhang 249916b64355SHong Zhang if (!submatj->allcolumns) { 250016b64355SHong Zhang #if defined(PETSC_USE_CTABLE) 250116b64355SHong Zhang ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr); 250216b64355SHong Zhang #else 250316b64355SHong Zhang ierr = PetscFree(submatj->cmap);CHKERRQ(ierr); 250416b64355SHong Zhang #endif 250516b64355SHong Zhang } 250616b64355SHong Zhang ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr); 250716b64355SHong Zhang 250816b64355SHong Zhang ierr = PetscFree(submatj);CHKERRQ(ierr); 250916b64355SHong Zhang PetscFunctionReturn(0); 251016b64355SHong Zhang } 251116b64355SHong Zhang 25120fb991dcSHong Zhang PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C) 251316b64355SHong Zhang { 251416b64355SHong Zhang PetscErrorCode ierr; 251516b64355SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data; 25165c39f6d9SHong Zhang Mat_SubSppt *submatj = c->submatis1; 251716b64355SHong Zhang 251816b64355SHong Zhang PetscFunctionBegin; 251916b64355SHong Zhang ierr = submatj->destroy(C);CHKERRQ(ierr); 2520f68bb481SHong Zhang ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr); 252116b64355SHong Zhang PetscFunctionReturn(0); 252216b64355SHong Zhang } 252316b64355SHong Zhang 25242d033e1fSHong Zhang PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[]) 25252d033e1fSHong Zhang { 25262d033e1fSHong Zhang PetscErrorCode ierr; 25272d033e1fSHong Zhang PetscInt i; 25280fb991dcSHong Zhang Mat C; 25290fb991dcSHong Zhang Mat_SeqAIJ *c; 25300fb991dcSHong Zhang Mat_SubSppt *submatj; 25312d033e1fSHong Zhang 25322d033e1fSHong Zhang PetscFunctionBegin; 25332d033e1fSHong Zhang for (i=0; i<n; i++) { 25340fb991dcSHong Zhang C = (*mat)[i]; 25350fb991dcSHong Zhang c = (Mat_SeqAIJ*)C->data; 25360fb991dcSHong Zhang submatj = c->submatis1; 25372d033e1fSHong Zhang if (submatj) { 2538682e4c99SStefano Zampini if (--((PetscObject)C)->refct <= 0) { 2539682e4c99SStefano Zampini ierr = (submatj->destroy)(C);CHKERRQ(ierr); 2540f68bb481SHong Zhang ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr); 25412d033e1fSHong Zhang ierr = PetscLayoutDestroy(&C->rmap);CHKERRQ(ierr); 25422d033e1fSHong Zhang ierr = PetscLayoutDestroy(&C->cmap);CHKERRQ(ierr); 25432d033e1fSHong Zhang ierr = PetscHeaderDestroy(&C);CHKERRQ(ierr); 2544682e4c99SStefano Zampini } 25452d033e1fSHong Zhang } else { 25462d033e1fSHong Zhang ierr = MatDestroy(&C);CHKERRQ(ierr); 25472d033e1fSHong Zhang } 25482d033e1fSHong Zhang } 254986e85357SHong Zhang 25502d033e1fSHong Zhang ierr = PetscFree(*mat);CHKERRQ(ierr); 25512d033e1fSHong Zhang PetscFunctionReturn(0); 25522d033e1fSHong Zhang } 25532d033e1fSHong Zhang 25547dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 2555cddf8d76SBarry Smith { 2556dfbe8321SBarry Smith PetscErrorCode ierr; 255797f1f81fSBarry Smith PetscInt i; 2558cddf8d76SBarry Smith 25593a40ed3dSBarry Smith PetscFunctionBegin; 2560cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 2561df750dc8SHong Zhang ierr = PetscCalloc1(n+1,B);CHKERRQ(ierr); 2562cddf8d76SBarry Smith } 2563cddf8d76SBarry Smith 2564cddf8d76SBarry Smith for (i=0; i<n; i++) { 25657dae84e0SHong Zhang ierr = MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 2566cddf8d76SBarry Smith } 25673a40ed3dSBarry Smith PetscFunctionReturn(0); 2568cddf8d76SBarry Smith } 2569cddf8d76SBarry Smith 257097f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 25714dcbc457SBarry Smith { 2572e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 25736849ba73SBarry Smith PetscErrorCode ierr; 25745d0c19d7SBarry Smith PetscInt row,i,j,k,l,m,n,*nidx,isz,val; 25755d0c19d7SBarry Smith const PetscInt *idx; 257697f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 2577f1af5d2fSBarry Smith PetscBT table; 2578bbd702dbSSatish Balay 25793a40ed3dSBarry Smith PetscFunctionBegin; 2580d0f46423SBarry Smith m = A->rmap->n; 2581e4d965acSSatish Balay ai = a->i; 2582bfeeae90SHong Zhang aj = a->j; 25838a047759SSatish Balay 2584e32f2f54SBarry Smith if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 258506763907SSatish Balay 2586854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr); 258753b8de81SBarry Smith ierr = PetscBTCreate(m,&table);CHKERRQ(ierr); 258806763907SSatish Balay 2589e4d965acSSatish Balay for (i=0; i<is_max; i++) { 2590b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 2591e4d965acSSatish Balay isz = 0; 25926831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 2593e4d965acSSatish Balay 2594e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 25954dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 2596b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 2597e4d965acSSatish Balay 2598dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 2599e4d965acSSatish Balay for (j=0; j<n; ++j) { 26002205254eSKarl Rupp if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j]; 26014dcbc457SBarry Smith } 260206763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 26036bf464f9SBarry Smith ierr = ISDestroy(&is[i]);CHKERRQ(ierr); 2604e4d965acSSatish Balay 260504a348a9SBarry Smith k = 0; 260604a348a9SBarry Smith for (j=0; j<ov; j++) { /* for each overlap */ 260704a348a9SBarry Smith n = isz; 260806763907SSatish Balay for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */ 2609e4d965acSSatish Balay row = nidx[k]; 2610e4d965acSSatish Balay start = ai[row]; 2611e4d965acSSatish Balay end = ai[row+1]; 261204a348a9SBarry Smith for (l = start; l<end; l++) { 2613efb16452SHong Zhang val = aj[l]; 26142205254eSKarl Rupp if (!PetscBTLookupSet(table,val)) nidx[isz++] = val; 2615e4d965acSSatish Balay } 2616e4d965acSSatish Balay } 2617e4d965acSSatish Balay } 261870b3c8c7SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr); 2619e4d965acSSatish Balay } 262094bacf5dSBarry Smith ierr = PetscBTDestroy(&table);CHKERRQ(ierr); 2621606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 26223a40ed3dSBarry Smith PetscFunctionReturn(0); 26234dcbc457SBarry Smith } 262417ab2063SBarry Smith 26250513a670SBarry Smith /* -------------------------------------------------------------- */ 2626dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 26270513a670SBarry Smith { 26280513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 26296849ba73SBarry Smith PetscErrorCode ierr; 26303b98c0a2SBarry Smith PetscInt i,nz = 0,m = A->rmap->n,n = A->cmap->n; 26315d0c19d7SBarry Smith const PetscInt *row,*col; 26325d0c19d7SBarry Smith PetscInt *cnew,j,*lens; 263356cd22aeSBarry Smith IS icolp,irowp; 26340298fd71SBarry Smith PetscInt *cwork = NULL; 26350298fd71SBarry Smith PetscScalar *vwork = NULL; 26360513a670SBarry Smith 26373a40ed3dSBarry Smith PetscFunctionBegin; 26384c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 263956cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 26404c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 264156cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 26420513a670SBarry Smith 26430513a670SBarry Smith /* determine lengths of permuted rows */ 2644854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr); 26452205254eSKarl Rupp for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i]; 2646ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 2647f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 264833d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 26497adad957SLisandro Dalcin ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2650ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 2651606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 26520513a670SBarry Smith 2653785e854fSJed Brown ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr); 26540513a670SBarry Smith for (i=0; i<m; i++) { 265532ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 26562205254eSKarl Rupp for (j=0; j<nz; j++) cnew[j] = col[cwork[j]]; 2657cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 265832ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 26590513a670SBarry Smith } 2660606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 26612205254eSKarl Rupp 26623c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 26632205254eSKarl Rupp 26640513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 26650513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 266656cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 266756cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 26686bf464f9SBarry Smith ierr = ISDestroy(&irowp);CHKERRQ(ierr); 26696bf464f9SBarry Smith ierr = ISDestroy(&icolp);CHKERRQ(ierr); 26703a40ed3dSBarry Smith PetscFunctionReturn(0); 26710513a670SBarry Smith } 26720513a670SBarry Smith 2673dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 2674cb5b572fSBarry Smith { 2675dfbe8321SBarry Smith PetscErrorCode ierr; 2676cb5b572fSBarry Smith 2677cb5b572fSBarry Smith PetscFunctionBegin; 267833f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 267933f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 2680be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2681be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 2682be6bf707SBarry Smith 2683700c5bfcSBarry 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"); 2684d0f46423SBarry Smith ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr); 2685cdc753b6SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 2686cb5b572fSBarry Smith } else { 2687cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2688cb5b572fSBarry Smith } 2689cb5b572fSBarry Smith PetscFunctionReturn(0); 2690cb5b572fSBarry Smith } 2691cb5b572fSBarry Smith 26924994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A) 2693273d9f13SBarry Smith { 2694dfbe8321SBarry Smith PetscErrorCode ierr; 2695273d9f13SBarry Smith 2696273d9f13SBarry Smith PetscFunctionBegin; 2697ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 2698273d9f13SBarry Smith PetscFunctionReturn(0); 2699273d9f13SBarry Smith } 2700273d9f13SBarry Smith 27018c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 27026c0721eeSBarry Smith { 27036c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 27046e111a19SKarl Rupp 27056c0721eeSBarry Smith PetscFunctionBegin; 27066c0721eeSBarry Smith *array = a->a; 27076c0721eeSBarry Smith PetscFunctionReturn(0); 27086c0721eeSBarry Smith } 27096c0721eeSBarry Smith 27108c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 27116c0721eeSBarry Smith { 27126c0721eeSBarry Smith PetscFunctionBegin; 27136c0721eeSBarry Smith PetscFunctionReturn(0); 27146c0721eeSBarry Smith } 2715273d9f13SBarry Smith 27168229c054SShri Abhyankar /* 27178229c054SShri Abhyankar Computes the number of nonzeros per row needed for preallocation when X and Y 27188229c054SShri Abhyankar have different nonzero structure. 27198229c054SShri Abhyankar */ 2720b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz) 2721ec7775f6SShri Abhyankar { 2722b264fe52SHong Zhang PetscInt i,j,k,nzx,nzy; 2723ec7775f6SShri Abhyankar 2724ec7775f6SShri Abhyankar PetscFunctionBegin; 2725ec7775f6SShri Abhyankar /* Set the number of nonzeros in the new matrix */ 2726ec7775f6SShri Abhyankar for (i=0; i<m; i++) { 2727b264fe52SHong Zhang const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i]; 2728b264fe52SHong Zhang nzx = xi[i+1] - xi[i]; 2729b264fe52SHong Zhang nzy = yi[i+1] - yi[i]; 27308af7cee1SJed Brown nnz[i] = 0; 27318af7cee1SJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 2732b264fe52SHong Zhang for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */ 2733b264fe52SHong Zhang if (k<nzy && yjj[k]==xjj[j]) k++; /* Skip duplicate */ 27348af7cee1SJed Brown nnz[i]++; 27358af7cee1SJed Brown } 27368af7cee1SJed Brown for (; k<nzy; k++) nnz[i]++; 2737ec7775f6SShri Abhyankar } 2738ec7775f6SShri Abhyankar PetscFunctionReturn(0); 2739ec7775f6SShri Abhyankar } 2740ec7775f6SShri Abhyankar 2741b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz) 2742b264fe52SHong Zhang { 2743b264fe52SHong Zhang PetscInt m = Y->rmap->N; 2744b264fe52SHong Zhang Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 2745b264fe52SHong Zhang Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 2746b264fe52SHong Zhang PetscErrorCode ierr; 2747b264fe52SHong Zhang 2748b264fe52SHong Zhang PetscFunctionBegin; 2749b264fe52SHong Zhang /* Set the number of nonzeros in the new matrix */ 2750b264fe52SHong Zhang ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr); 2751b264fe52SHong Zhang PetscFunctionReturn(0); 2752b264fe52SHong Zhang } 2753b264fe52SHong Zhang 2754f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2755ac90fabeSBarry Smith { 2756dfbe8321SBarry Smith PetscErrorCode ierr; 2757ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data; 2758c5df96a5SBarry Smith PetscBLASInt one=1,bnz; 2759ac90fabeSBarry Smith 2760ac90fabeSBarry Smith PetscFunctionBegin; 2761c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 2762ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2763f4df32b1SMatthew Knepley PetscScalar alpha = a; 27648b83055fSJed Brown PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2765acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr); 2766a3fa217bSJose E. Roman ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); 2767ab784542SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 2768ab784542SHong Zhang ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 2769ac90fabeSBarry Smith } else { 27708229c054SShri Abhyankar Mat B; 27718229c054SShri Abhyankar PetscInt *nnz; 2772785e854fSJed Brown ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr); 2773ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr); 2774bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 27754aa94f47SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 277633d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr); 2777176df525SBarry Smith ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr); 27788229c054SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr); 2779ecd8bba6SJed Brown ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr); 2780ec7775f6SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 278128be2f97SBarry Smith ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr); 27828229c054SShri Abhyankar ierr = PetscFree(nnz);CHKERRQ(ierr); 2783ac90fabeSBarry Smith } 2784ac90fabeSBarry Smith PetscFunctionReturn(0); 2785ac90fabeSBarry Smith } 2786ac90fabeSBarry Smith 27877087cfbeSBarry Smith PetscErrorCode MatConjugate_SeqAIJ(Mat mat) 2788354c94deSBarry Smith { 2789354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2790354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 2791354c94deSBarry Smith PetscInt i,nz; 2792354c94deSBarry Smith PetscScalar *a; 2793354c94deSBarry Smith 2794354c94deSBarry Smith PetscFunctionBegin; 2795354c94deSBarry Smith nz = aij->nz; 2796354c94deSBarry Smith a = aij->a; 27972205254eSKarl Rupp for (i=0; i<nz; i++) a[i] = PetscConj(a[i]); 2798354c94deSBarry Smith #else 2799354c94deSBarry Smith PetscFunctionBegin; 2800354c94deSBarry Smith #endif 2801354c94deSBarry Smith PetscFunctionReturn(0); 2802354c94deSBarry Smith } 2803354c94deSBarry Smith 2804985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2805e34fafa9SBarry Smith { 2806e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2807e34fafa9SBarry Smith PetscErrorCode ierr; 2808d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2809e34fafa9SBarry Smith PetscReal atmp; 2810985db425SBarry Smith PetscScalar *x; 2811e34fafa9SBarry Smith MatScalar *aa; 2812e34fafa9SBarry Smith 2813e34fafa9SBarry Smith PetscFunctionBegin; 2814e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2815e34fafa9SBarry Smith aa = a->a; 2816e34fafa9SBarry Smith ai = a->i; 2817e34fafa9SBarry Smith aj = a->j; 2818e34fafa9SBarry Smith 2819985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2820e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2821e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2822e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2823e34fafa9SBarry Smith for (i=0; i<m; i++) { 2824e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 28259189402eSHong Zhang x[i] = 0.0; 2826e34fafa9SBarry Smith for (j=0; j<ncols; j++) { 2827985db425SBarry Smith atmp = PetscAbsScalar(*aa); 2828985db425SBarry Smith if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2829985db425SBarry Smith aa++; aj++; 2830985db425SBarry Smith } 2831985db425SBarry Smith } 2832985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2833985db425SBarry Smith PetscFunctionReturn(0); 2834985db425SBarry Smith } 2835985db425SBarry Smith 2836985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2837985db425SBarry Smith { 2838985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2839985db425SBarry Smith PetscErrorCode ierr; 2840d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2841985db425SBarry Smith PetscScalar *x; 2842985db425SBarry Smith MatScalar *aa; 2843985db425SBarry Smith 2844985db425SBarry Smith PetscFunctionBegin; 2845e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2846985db425SBarry Smith aa = a->a; 2847985db425SBarry Smith ai = a->i; 2848985db425SBarry Smith aj = a->j; 2849985db425SBarry Smith 2850985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2851985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2852985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2853e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2854985db425SBarry Smith for (i=0; i<m; i++) { 2855985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2856d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2857985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2858985db425SBarry Smith } else { /* row is sparse so already KNOW maximum is 0.0 or higher */ 2859985db425SBarry Smith x[i] = 0.0; 2860985db425SBarry Smith if (idx) { 2861985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2862985db425SBarry Smith for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */ 2863985db425SBarry Smith if (aj[j] > j) { 2864985db425SBarry Smith idx[i] = j; 2865985db425SBarry Smith break; 2866985db425SBarry Smith } 2867985db425SBarry Smith } 2868985db425SBarry Smith } 2869985db425SBarry Smith } 2870985db425SBarry Smith for (j=0; j<ncols; j++) { 2871985db425SBarry Smith if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2872985db425SBarry Smith aa++; aj++; 2873985db425SBarry Smith } 2874985db425SBarry Smith } 2875985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2876985db425SBarry Smith PetscFunctionReturn(0); 2877985db425SBarry Smith } 2878985db425SBarry Smith 2879c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2880c87e5d42SMatthew Knepley { 2881c87e5d42SMatthew Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2882c87e5d42SMatthew Knepley PetscErrorCode ierr; 2883c87e5d42SMatthew Knepley PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 2884c87e5d42SMatthew Knepley PetscReal atmp; 2885c87e5d42SMatthew Knepley PetscScalar *x; 2886c87e5d42SMatthew Knepley MatScalar *aa; 2887c87e5d42SMatthew Knepley 2888c87e5d42SMatthew Knepley PetscFunctionBegin; 2889e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2890c87e5d42SMatthew Knepley aa = a->a; 2891c87e5d42SMatthew Knepley ai = a->i; 2892c87e5d42SMatthew Knepley aj = a->j; 2893c87e5d42SMatthew Knepley 2894c87e5d42SMatthew Knepley ierr = VecSet(v,0.0);CHKERRQ(ierr); 2895c87e5d42SMatthew Knepley ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2896c87e5d42SMatthew Knepley ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 289760e0710aSBarry Smith 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); 2898c87e5d42SMatthew Knepley for (i=0; i<m; i++) { 2899c87e5d42SMatthew Knepley ncols = ai[1] - ai[0]; ai++; 2900289a08f5SMatthew Knepley if (ncols) { 2901289a08f5SMatthew Knepley /* Get first nonzero */ 2902289a08f5SMatthew Knepley for (j = 0; j < ncols; j++) { 2903289a08f5SMatthew Knepley atmp = PetscAbsScalar(aa[j]); 29042205254eSKarl Rupp if (atmp > 1.0e-12) { 29052205254eSKarl Rupp x[i] = atmp; 29062205254eSKarl Rupp if (idx) idx[i] = aj[j]; 29072205254eSKarl Rupp break; 29082205254eSKarl Rupp } 2909289a08f5SMatthew Knepley } 291012431cb0SMatthew G Knepley if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;} 2911289a08f5SMatthew Knepley } else { 2912289a08f5SMatthew Knepley x[i] = 0.0; if (idx) idx[i] = 0; 2913289a08f5SMatthew Knepley } 2914c87e5d42SMatthew Knepley for (j = 0; j < ncols; j++) { 2915c87e5d42SMatthew Knepley atmp = PetscAbsScalar(*aa); 2916289a08f5SMatthew Knepley if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 2917c87e5d42SMatthew Knepley aa++; aj++; 2918c87e5d42SMatthew Knepley } 2919c87e5d42SMatthew Knepley } 2920c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2921c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2922c87e5d42SMatthew Knepley } 2923c87e5d42SMatthew Knepley 2924985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 2925985db425SBarry Smith { 2926985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2927985db425SBarry Smith PetscErrorCode ierr; 2928d9ca1df4SBarry Smith PetscInt i,j,m = A->rmap->n,ncols,n; 2929d9ca1df4SBarry Smith const PetscInt *ai,*aj; 2930985db425SBarry Smith PetscScalar *x; 2931d9ca1df4SBarry Smith const MatScalar *aa; 2932985db425SBarry Smith 2933985db425SBarry Smith PetscFunctionBegin; 2934e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2935985db425SBarry Smith aa = a->a; 2936985db425SBarry Smith ai = a->i; 2937985db425SBarry Smith aj = a->j; 2938985db425SBarry Smith 2939985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 2940985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 2941985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 2942e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 2943985db425SBarry Smith for (i=0; i<m; i++) { 2944985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 2945d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 2946985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 2947985db425SBarry Smith } else { /* row is sparse so already KNOW minimum is 0.0 or lower */ 2948985db425SBarry Smith x[i] = 0.0; 2949985db425SBarry Smith if (idx) { /* find first implicit 0.0 in the row */ 2950985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 2951985db425SBarry Smith for (j=0; j<ncols; j++) { 2952985db425SBarry Smith if (aj[j] > j) { 2953985db425SBarry Smith idx[i] = j; 2954985db425SBarry Smith break; 2955985db425SBarry Smith } 2956985db425SBarry Smith } 2957985db425SBarry Smith } 2958985db425SBarry Smith } 2959985db425SBarry Smith for (j=0; j<ncols; j++) { 2960985db425SBarry Smith if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 2961985db425SBarry Smith aa++; aj++; 2962e34fafa9SBarry Smith } 2963e34fafa9SBarry Smith } 2964e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 2965e34fafa9SBarry Smith PetscFunctionReturn(0); 2966e34fafa9SBarry Smith } 2967bbead8a2SBarry Smith 2968bbead8a2SBarry Smith #include <petscblaslapack.h> 2969af0996ceSBarry Smith #include <petsc/private/kernels/blockinvert.h> 2970bbead8a2SBarry Smith 2971713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values) 2972bbead8a2SBarry Smith { 2973bbead8a2SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 2974bbead8a2SBarry Smith PetscErrorCode ierr; 297533d57670SJed Brown PetscInt i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j; 2976bbead8a2SBarry Smith MatScalar *diag,work[25],*v_work; 2977bbead8a2SBarry Smith PetscReal shift = 0.0; 29781a9391e3SHong Zhang PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE; 2979bbead8a2SBarry Smith 2980bbead8a2SBarry Smith PetscFunctionBegin; 2981a455e926SHong Zhang allowzeropivot = PetscNot(A->erroriffailure); 29824a0d0026SBarry Smith if (a->ibdiagvalid) { 29834a0d0026SBarry Smith if (values) *values = a->ibdiag; 29844a0d0026SBarry Smith PetscFunctionReturn(0); 29854a0d0026SBarry Smith } 2986bbead8a2SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 2987bbead8a2SBarry Smith if (!a->ibdiag) { 2988785e854fSJed Brown ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr); 29893bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr); 2990bbead8a2SBarry Smith } 2991bbead8a2SBarry Smith diag = a->ibdiag; 2992bbead8a2SBarry Smith if (values) *values = a->ibdiag; 2993bbead8a2SBarry Smith /* factor and invert each block */ 2994bbead8a2SBarry Smith switch (bs) { 2995bbead8a2SBarry Smith case 1: 2996bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 2997bbead8a2SBarry Smith ierr = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr); 2998ec1892c8SHong Zhang if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) { 2999ec1892c8SHong Zhang if (allowzeropivot) { 30007b6c816cSBarry Smith A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 30017b6c816cSBarry Smith A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]); 30027b6c816cSBarry Smith A->factorerror_zeropivot_row = i; 30037b6c816cSBarry Smith ierr = PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);CHKERRQ(ierr); 30047b6c816cSBarry Smith } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D pivot %g tolerance %g",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON); 3005ec1892c8SHong Zhang } 3006bbead8a2SBarry Smith diag[i] = (PetscScalar)1.0 / (diag[i] + shift); 3007bbead8a2SBarry Smith } 3008bbead8a2SBarry Smith break; 3009bbead8a2SBarry Smith case 2: 3010bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3011bbead8a2SBarry Smith ij[0] = 2*i; ij[1] = 2*i + 1; 3012bbead8a2SBarry Smith ierr = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr); 3013a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 30147b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 301596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr); 3016bbead8a2SBarry Smith diag += 4; 3017bbead8a2SBarry Smith } 3018bbead8a2SBarry Smith break; 3019bbead8a2SBarry Smith case 3: 3020bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3021bbead8a2SBarry Smith ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2; 3022bbead8a2SBarry Smith ierr = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr); 3023a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 30247b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 302596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr); 3026bbead8a2SBarry Smith diag += 9; 3027bbead8a2SBarry Smith } 3028bbead8a2SBarry Smith break; 3029bbead8a2SBarry Smith case 4: 3030bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3031bbead8a2SBarry Smith ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3; 3032bbead8a2SBarry Smith ierr = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr); 3033a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 30347b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 303596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr); 3036bbead8a2SBarry Smith diag += 16; 3037bbead8a2SBarry Smith } 3038bbead8a2SBarry Smith break; 3039bbead8a2SBarry Smith case 5: 3040bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3041bbead8a2SBarry 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; 3042bbead8a2SBarry Smith ierr = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr); 3043a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 30447b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 304596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr); 3046bbead8a2SBarry Smith diag += 25; 3047bbead8a2SBarry Smith } 3048bbead8a2SBarry Smith break; 3049bbead8a2SBarry Smith case 6: 3050bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3051bbead8a2SBarry 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; 3052bbead8a2SBarry Smith ierr = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr); 3053a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 30547b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 305596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr); 3056bbead8a2SBarry Smith diag += 36; 3057bbead8a2SBarry Smith } 3058bbead8a2SBarry Smith break; 3059bbead8a2SBarry Smith case 7: 3060bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3061bbead8a2SBarry 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; 3062bbead8a2SBarry Smith ierr = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr); 3063a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 30647b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 306596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr); 3066bbead8a2SBarry Smith diag += 49; 3067bbead8a2SBarry Smith } 3068bbead8a2SBarry Smith break; 3069bbead8a2SBarry Smith default: 3070dcca6d9dSJed Brown ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr); 3071bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3072bbead8a2SBarry Smith for (j=0; j<bs; j++) { 3073bbead8a2SBarry Smith IJ[j] = bs*i + j; 3074bbead8a2SBarry Smith } 3075bbead8a2SBarry Smith ierr = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr); 30765f8bbccaSHong Zhang ierr = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 30777b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 307896b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr); 3079bbead8a2SBarry Smith diag += bs2; 3080bbead8a2SBarry Smith } 3081bbead8a2SBarry Smith ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr); 3082bbead8a2SBarry Smith } 3083bbead8a2SBarry Smith a->ibdiagvalid = PETSC_TRUE; 3084bbead8a2SBarry Smith PetscFunctionReturn(0); 3085bbead8a2SBarry Smith } 3086bbead8a2SBarry Smith 308773a71a0fSBarry Smith static PetscErrorCode MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx) 308873a71a0fSBarry Smith { 308973a71a0fSBarry Smith PetscErrorCode ierr; 309073a71a0fSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data; 309173a71a0fSBarry Smith PetscScalar a; 309273a71a0fSBarry Smith PetscInt m,n,i,j,col; 309373a71a0fSBarry Smith 309473a71a0fSBarry Smith PetscFunctionBegin; 309573a71a0fSBarry Smith if (!x->assembled) { 309673a71a0fSBarry Smith ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr); 309773a71a0fSBarry Smith for (i=0; i<m; i++) { 309873a71a0fSBarry Smith for (j=0; j<aij->imax[i]; j++) { 309973a71a0fSBarry Smith ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr); 310073a71a0fSBarry Smith col = (PetscInt)(n*PetscRealPart(a)); 310173a71a0fSBarry Smith ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr); 310273a71a0fSBarry Smith } 310373a71a0fSBarry Smith } 310473a71a0fSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded"); 310573a71a0fSBarry Smith ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 310673a71a0fSBarry Smith ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 310773a71a0fSBarry Smith PetscFunctionReturn(0); 310873a71a0fSBarry Smith } 310973a71a0fSBarry Smith 31107d68702bSBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a) 31117d68702bSBarry Smith { 31127d68702bSBarry Smith PetscErrorCode ierr; 31137d68702bSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)Y->data; 31147d68702bSBarry Smith 31157d68702bSBarry Smith PetscFunctionBegin; 31166f33a894SBarry Smith if (!Y->preallocated || !aij->nz) { 31177d68702bSBarry Smith ierr = MatSeqAIJSetPreallocation(Y,1,NULL);CHKERRQ(ierr); 31187d68702bSBarry Smith } 31197d68702bSBarry Smith ierr = MatShift_Basic(Y,a);CHKERRQ(ierr); 31207d68702bSBarry Smith PetscFunctionReturn(0); 31217d68702bSBarry Smith } 31227d68702bSBarry Smith 3123682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 31240a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ, 3125cb5b572fSBarry Smith MatGetRow_SeqAIJ, 3126cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 3127cb5b572fSBarry Smith MatMult_SeqAIJ, 312897304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 31297c922b88SBarry Smith MatMultTranspose_SeqAIJ, 31307c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 3131db4efbfdSBarry Smith 0, 3132db4efbfdSBarry Smith 0, 3133db4efbfdSBarry Smith 0, 3134db4efbfdSBarry Smith /* 10*/ 0, 3135cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 3136cb5b572fSBarry Smith 0, 313741f059aeSBarry Smith MatSOR_SeqAIJ, 313817ab2063SBarry Smith MatTranspose_SeqAIJ, 313997304618SKris Buschelman /*1 5*/ MatGetInfo_SeqAIJ, 3140cb5b572fSBarry Smith MatEqual_SeqAIJ, 3141cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 3142cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 3143cb5b572fSBarry Smith MatNorm_SeqAIJ, 314497304618SKris Buschelman /* 20*/ 0, 3145cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 3146cb5b572fSBarry Smith MatSetOption_SeqAIJ, 3147cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 3148d519adbfSMatthew Knepley /* 24*/ MatZeroRows_SeqAIJ, 3149db4efbfdSBarry Smith 0, 3150db4efbfdSBarry Smith 0, 3151db4efbfdSBarry Smith 0, 3152db4efbfdSBarry Smith 0, 31534994cf47SJed Brown /* 29*/ MatSetUp_SeqAIJ, 3154db4efbfdSBarry Smith 0, 3155db4efbfdSBarry Smith 0, 31568c778c55SBarry Smith 0, 31578c778c55SBarry Smith 0, 3158d519adbfSMatthew Knepley /* 34*/ MatDuplicate_SeqAIJ, 3159cb5b572fSBarry Smith 0, 3160cb5b572fSBarry Smith 0, 3161cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 3162cb5b572fSBarry Smith 0, 3163d519adbfSMatthew Knepley /* 39*/ MatAXPY_SeqAIJ, 31647dae84e0SHong Zhang MatCreateSubMatrices_SeqAIJ, 3165cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 3166cb5b572fSBarry Smith MatGetValues_SeqAIJ, 3167cb5b572fSBarry Smith MatCopy_SeqAIJ, 3168d519adbfSMatthew Knepley /* 44*/ MatGetRowMax_SeqAIJ, 3169cb5b572fSBarry Smith MatScale_SeqAIJ, 31707d68702bSBarry Smith MatShift_SeqAIJ, 317179299369SBarry Smith MatDiagonalSet_SeqAIJ, 31726e169961SBarry Smith MatZeroRowsColumns_SeqAIJ, 317373a71a0fSBarry Smith /* 49*/ MatSetRandom_SeqAIJ, 31743b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 31753b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 31763b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 3177a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 317893dfae19SHong Zhang /* 54*/ MatFDColoringCreate_SeqXAIJ, 3179b9617806SBarry Smith 0, 31800513a670SBarry Smith 0, 3181cda55fadSBarry Smith MatPermute_SeqAIJ, 3182cda55fadSBarry Smith 0, 3183d519adbfSMatthew Knepley /* 59*/ 0, 3184b9b97703SBarry Smith MatDestroy_SeqAIJ, 3185b9b97703SBarry Smith MatView_SeqAIJ, 3186357abbc8SBarry Smith 0, 3187321b30b9SSatish Balay MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ, 3188321b30b9SSatish Balay /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ, 3189321b30b9SSatish Balay MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ, 3190ee4f033dSBarry Smith 0, 3191ee4f033dSBarry Smith 0, 3192ee4f033dSBarry Smith 0, 3193d519adbfSMatthew Knepley /* 69*/ MatGetRowMaxAbs_SeqAIJ, 3194c87e5d42SMatthew Knepley MatGetRowMinAbs_SeqAIJ, 3195ee4f033dSBarry Smith 0, 3196dcf5cc72SBarry Smith 0, 31972c93a97aSBarry Smith 0, 31982c93a97aSBarry Smith /* 74*/ 0, 31993acb8795SBarry Smith MatFDColoringApply_AIJ, 320097304618SKris Buschelman 0, 320197304618SKris Buschelman 0, 320297304618SKris Buschelman 0, 32036ce1633cSBarry Smith /* 79*/ MatFindZeroDiagonals_SeqAIJ, 320497304618SKris Buschelman 0, 320597304618SKris Buschelman 0, 320697304618SKris Buschelman 0, 3207bc011b1eSHong Zhang MatLoad_SeqAIJ, 3208d519adbfSMatthew Knepley /* 84*/ MatIsSymmetric_SeqAIJ, 32091cbb95d3SBarry Smith MatIsHermitian_SeqAIJ, 32106284ec50SHong Zhang 0, 32116284ec50SHong Zhang 0, 3212bc011b1eSHong Zhang 0, 3213d519adbfSMatthew Knepley /* 89*/ MatMatMult_SeqAIJ_SeqAIJ, 321426be0446SHong Zhang MatMatMultSymbolic_SeqAIJ_SeqAIJ, 321526be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 321665e8a0caSHong Zhang MatPtAP_SeqAIJ_SeqAIJ, 32178fa4b5a6SHong Zhang MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy, 32188fa4b5a6SHong Zhang /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy, 32196fc122caSHong Zhang MatMatTransposeMult_SeqAIJ_SeqAIJ, 32206fc122caSHong Zhang MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ, 32216fc122caSHong Zhang MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ, 32222121bac1SHong Zhang 0, 32232121bac1SHong Zhang /* 99*/ 0, 3224609c6c4dSKris Buschelman 0, 3225609c6c4dSKris Buschelman 0, 322687d4246cSBarry Smith MatConjugate_SeqAIJ, 322787d4246cSBarry Smith 0, 3228d519adbfSMatthew Knepley /*104*/ MatSetValuesRow_SeqAIJ, 322999cafbc1SBarry Smith MatRealPart_SeqAIJ, 3230f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 3231f5edf698SHong Zhang 0, 32322bebee5dSHong Zhang 0, 3233cbd44569SHong Zhang /*109*/ MatMatSolve_SeqAIJ, 3234985db425SBarry Smith 0, 32352af78befSBarry Smith MatGetRowMin_SeqAIJ, 32362af78befSBarry Smith 0, 3237599ef60dSHong Zhang MatMissingDiagonal_SeqAIJ, 3238d519adbfSMatthew Knepley /*114*/ 0, 3239599ef60dSHong Zhang 0, 32403c2a7987SHong Zhang 0, 3241fe97e370SBarry Smith 0, 3242fbdbba38SShri Abhyankar 0, 3243fbdbba38SShri Abhyankar /*119*/ 0, 3244fbdbba38SShri Abhyankar 0, 3245fbdbba38SShri Abhyankar 0, 324682d44351SHong Zhang 0, 3247b3a44c85SBarry Smith MatGetMultiProcBlock_SeqAIJ, 32480716a85fSBarry Smith /*124*/ MatFindNonzeroRows_SeqAIJ, 3249bbead8a2SBarry Smith MatGetColumnNorms_SeqAIJ, 325037868618SMatthew G Knepley MatInvertBlockDiagonal_SeqAIJ, 325137868618SMatthew G Knepley 0, 325237868618SMatthew G Knepley 0, 32535df89d91SHong Zhang /*129*/ 0, 325475648e8dSHong Zhang MatTransposeMatMult_SeqAIJ_SeqAIJ, 325575648e8dSHong Zhang MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ, 325675648e8dSHong Zhang MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ, 3257b9af6bddSHong Zhang MatTransposeColoringCreate_SeqAIJ, 3258b9af6bddSHong Zhang /*134*/ MatTransColoringApplySpToDen_SeqAIJ, 32592b8ad9a3SHong Zhang MatTransColoringApplyDenToSp_SeqAIJ, 32602b8ad9a3SHong Zhang MatRARt_SeqAIJ_SeqAIJ, 32612b8ad9a3SHong Zhang MatRARtSymbolic_SeqAIJ_SeqAIJ, 32623964eb88SJed Brown MatRARtNumeric_SeqAIJ_SeqAIJ, 32633964eb88SJed Brown /*139*/0, 3264f9426fe0SMark Adams 0, 32651919a2e2SJed Brown 0, 32663a062f41SBarry Smith MatFDColoringSetUp_SeqXAIJ, 32679c8f2541SHong Zhang MatFindOffBlockDiagonalEntries_SeqAIJ, 32682d033e1fSHong Zhang /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ, 32692d033e1fSHong Zhang MatDestroySubMatrices_SeqAIJ 32709e29f15eSvictorle }; 327117ab2063SBarry Smith 32727087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 3273bef8e0ddSBarry Smith { 3274bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 327597f1f81fSBarry Smith PetscInt i,nz,n; 3276bef8e0ddSBarry Smith 3277bef8e0ddSBarry Smith PetscFunctionBegin; 3278bef8e0ddSBarry Smith nz = aij->maxnz; 3279d0f46423SBarry Smith n = mat->rmap->n; 3280bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 3281bef8e0ddSBarry Smith aij->j[i] = indices[i]; 3282bef8e0ddSBarry Smith } 3283bef8e0ddSBarry Smith aij->nz = nz; 3284bef8e0ddSBarry Smith for (i=0; i<n; i++) { 3285bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 3286bef8e0ddSBarry Smith } 3287bef8e0ddSBarry Smith PetscFunctionReturn(0); 3288bef8e0ddSBarry Smith } 3289bef8e0ddSBarry Smith 3290bef8e0ddSBarry Smith /*@ 3291bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 3292bef8e0ddSBarry Smith in the matrix. 3293bef8e0ddSBarry Smith 3294bef8e0ddSBarry Smith Input Parameters: 3295bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 3296bef8e0ddSBarry Smith - indices - the column indices 3297bef8e0ddSBarry Smith 329815091d37SBarry Smith Level: advanced 329915091d37SBarry Smith 3300bef8e0ddSBarry Smith Notes: 3301bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 3302bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 3303bef8e0ddSBarry Smith of the MatSetValues() operation. 3304bef8e0ddSBarry Smith 3305bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 3306d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 3307bef8e0ddSBarry Smith 3308bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 3309bef8e0ddSBarry Smith 3310b9617806SBarry Smith The indices should start with zero, not one. 3311b9617806SBarry Smith 3312bef8e0ddSBarry Smith @*/ 33137087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 3314bef8e0ddSBarry Smith { 33154ac538c5SBarry Smith PetscErrorCode ierr; 3316bef8e0ddSBarry Smith 3317bef8e0ddSBarry Smith PetscFunctionBegin; 33180700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 33194482741eSBarry Smith PetscValidPointer(indices,2); 33204ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr); 3321bef8e0ddSBarry Smith PetscFunctionReturn(0); 3322bef8e0ddSBarry Smith } 3323bef8e0ddSBarry Smith 3324be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 3325be6bf707SBarry Smith 33267087cfbeSBarry Smith PetscErrorCode MatStoreValues_SeqAIJ(Mat mat) 3327be6bf707SBarry Smith { 3328be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 33296849ba73SBarry Smith PetscErrorCode ierr; 3330d0f46423SBarry Smith size_t nz = aij->i[mat->rmap->n]; 3331be6bf707SBarry Smith 3332be6bf707SBarry Smith PetscFunctionBegin; 3333169f6850SBarry Smith if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3334be6bf707SBarry Smith 3335be6bf707SBarry Smith /* allocate space for values if not already there */ 3336be6bf707SBarry Smith if (!aij->saved_values) { 3337854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr); 33383bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr); 3339be6bf707SBarry Smith } 3340be6bf707SBarry Smith 3341be6bf707SBarry Smith /* copy values over */ 334287828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 3343be6bf707SBarry Smith PetscFunctionReturn(0); 3344be6bf707SBarry Smith } 3345be6bf707SBarry Smith 3346be6bf707SBarry Smith /*@ 3347be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 3348be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3349be6bf707SBarry Smith nonlinear portion. 3350be6bf707SBarry Smith 3351be6bf707SBarry Smith Collect on Mat 3352be6bf707SBarry Smith 3353be6bf707SBarry Smith Input Parameters: 33540e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 3355be6bf707SBarry Smith 335615091d37SBarry Smith Level: advanced 335715091d37SBarry Smith 3358be6bf707SBarry Smith Common Usage, with SNESSolve(): 3359be6bf707SBarry Smith $ Create Jacobian matrix 3360be6bf707SBarry Smith $ Set linear terms into matrix 3361be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 3362be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 3363be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 3364512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3365be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3366be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 3367be6bf707SBarry Smith $ In your Jacobian routine 3368be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3369be6bf707SBarry Smith $ Set nonlinear terms in matrix 3370be6bf707SBarry Smith 3371be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 3372be6bf707SBarry Smith $ // build linear portion of Jacobian 3373512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3374be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3375be6bf707SBarry Smith $ loop over nonlinear iterations 3376be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3377be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 3378be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 3379be6bf707SBarry Smith $ Solve linear system with Jacobian 3380be6bf707SBarry Smith $ endloop 3381be6bf707SBarry Smith 3382be6bf707SBarry Smith Notes: 3383be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 3384512a5fc5SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before 3385be6bf707SBarry Smith calling this routine. 3386be6bf707SBarry Smith 33870c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 33880c468ba9SBarry Smith and does not allocated additional space. 33890c468ba9SBarry Smith 3390be6bf707SBarry Smith .seealso: MatRetrieveValues() 3391be6bf707SBarry Smith 3392be6bf707SBarry Smith @*/ 33937087cfbeSBarry Smith PetscErrorCode MatStoreValues(Mat mat) 3394be6bf707SBarry Smith { 33954ac538c5SBarry Smith PetscErrorCode ierr; 3396be6bf707SBarry Smith 3397be6bf707SBarry Smith PetscFunctionBegin; 33980700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3399e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3400e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 34014ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr); 3402be6bf707SBarry Smith PetscFunctionReturn(0); 3403be6bf707SBarry Smith } 3404be6bf707SBarry Smith 34057087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat) 3406be6bf707SBarry Smith { 3407be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 34086849ba73SBarry Smith PetscErrorCode ierr; 3409d0f46423SBarry Smith PetscInt nz = aij->i[mat->rmap->n]; 3410be6bf707SBarry Smith 3411be6bf707SBarry Smith PetscFunctionBegin; 3412169f6850SBarry Smith if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3413f23aa3ddSBarry Smith if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 3414be6bf707SBarry Smith /* copy values over */ 341587828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 3416be6bf707SBarry Smith PetscFunctionReturn(0); 3417be6bf707SBarry Smith } 3418be6bf707SBarry Smith 3419be6bf707SBarry Smith /*@ 3420be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 3421be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3422be6bf707SBarry Smith nonlinear portion. 3423be6bf707SBarry Smith 3424be6bf707SBarry Smith Collect on Mat 3425be6bf707SBarry Smith 3426be6bf707SBarry Smith Input Parameters: 3427386f7cf9SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 3428be6bf707SBarry Smith 342915091d37SBarry Smith Level: advanced 343015091d37SBarry Smith 3431be6bf707SBarry Smith .seealso: MatStoreValues() 3432be6bf707SBarry Smith 3433be6bf707SBarry Smith @*/ 34347087cfbeSBarry Smith PetscErrorCode MatRetrieveValues(Mat mat) 3435be6bf707SBarry Smith { 34364ac538c5SBarry Smith PetscErrorCode ierr; 3437be6bf707SBarry Smith 3438be6bf707SBarry Smith PetscFunctionBegin; 34390700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3440e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3441e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 34424ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr); 3443be6bf707SBarry Smith PetscFunctionReturn(0); 3444be6bf707SBarry Smith } 3445be6bf707SBarry Smith 3446f83d6046SBarry Smith 3447be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 344817ab2063SBarry Smith /*@C 3449682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 34500d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 34516e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 345251c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 34532bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 345417ab2063SBarry Smith 3455db81eaa0SLois Curfman McInnes Collective on MPI_Comm 3456db81eaa0SLois Curfman McInnes 345717ab2063SBarry Smith Input Parameters: 3458db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 345917ab2063SBarry Smith . m - number of rows 346017ab2063SBarry Smith . n - number of columns 346117ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 346251c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 34630298fd71SBarry Smith (possibly different for each row) or NULL 346417ab2063SBarry Smith 346517ab2063SBarry Smith Output Parameter: 3466416022c9SBarry Smith . A - the matrix 346717ab2063SBarry Smith 3468175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3469ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3470175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3471175b88e8SBarry Smith 3472b259b22eSLois Curfman McInnes Notes: 347349a6f317SBarry Smith If nnz is given then nz is ignored 347449a6f317SBarry Smith 347517ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 347617ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 34770002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 347844cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 347917ab2063SBarry Smith 348017ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 34810298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 34823d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 34836da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 348417ab2063SBarry Smith 3485682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 34864fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 3487682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 34886c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 34896c7ebb05SLois Curfman McInnes 34906c7ebb05SLois Curfman McInnes Options Database Keys: 3491698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 34929db58ca8SBarry Smith - -mat_inode_limit <limit> - Sets inode limit (max limit=5) 349317ab2063SBarry Smith 3494027ccd11SLois Curfman McInnes Level: intermediate 3495027ccd11SLois Curfman McInnes 349669b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 349736db0b34SBarry Smith 349817ab2063SBarry Smith @*/ 34997087cfbeSBarry Smith PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 350017ab2063SBarry Smith { 3501dfbe8321SBarry Smith PetscErrorCode ierr; 35026945ee14SBarry Smith 35033a40ed3dSBarry Smith PetscFunctionBegin; 3504f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 3505117016b1SBarry Smith ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 3506c4752a88SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 3507d28bb7d2SJed Brown ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 3508273d9f13SBarry Smith PetscFunctionReturn(0); 3509273d9f13SBarry Smith } 3510273d9f13SBarry Smith 3511273d9f13SBarry Smith /*@C 3512273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 3513273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 3514273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 3515273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 3516273d9f13SBarry Smith 3517273d9f13SBarry Smith Collective on MPI_Comm 3518273d9f13SBarry Smith 3519273d9f13SBarry Smith Input Parameters: 35201c4f3114SJed Brown + B - The matrix 3521273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 3522273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 35230298fd71SBarry Smith (possibly different for each row) or NULL 3524273d9f13SBarry Smith 3525273d9f13SBarry Smith Notes: 352649a6f317SBarry Smith If nnz is given then nz is ignored 352749a6f317SBarry Smith 3528273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3529273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 3530273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 3531273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 3532273d9f13SBarry Smith 3533273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 35340298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 3535273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 3536273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 3537273d9f13SBarry Smith 3538aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3539aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3540aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3541aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3542aa95bbe8SBarry Smith 3543a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 3544a96a251dSBarry Smith entries or columns indices 3545a96a251dSBarry Smith 3546273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 3547273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 3548273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 3549273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3550273d9f13SBarry Smith 3551273d9f13SBarry Smith Options Database Keys: 3552698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 355347b2e64bSBarry Smith - -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3554273d9f13SBarry Smith 3555273d9f13SBarry Smith Level: intermediate 3556273d9f13SBarry Smith 355769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo() 3558273d9f13SBarry Smith 3559273d9f13SBarry Smith @*/ 35607087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 3561273d9f13SBarry Smith { 35624ac538c5SBarry Smith PetscErrorCode ierr; 3563a23d5eceSKris Buschelman 3564a23d5eceSKris Buschelman PetscFunctionBegin; 35656ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 35666ba663aaSJed Brown PetscValidType(B,1); 35674ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr); 3568a23d5eceSKris Buschelman PetscFunctionReturn(0); 3569a23d5eceSKris Buschelman } 3570a23d5eceSKris Buschelman 35717087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz) 3572a23d5eceSKris Buschelman { 3573273d9f13SBarry Smith Mat_SeqAIJ *b; 35742576faa2SJed Brown PetscBool skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE; 35756849ba73SBarry Smith PetscErrorCode ierr; 357697f1f81fSBarry Smith PetscInt i; 3577273d9f13SBarry Smith 3578273d9f13SBarry Smith PetscFunctionBegin; 35792576faa2SJed Brown if (nz >= 0 || nnz) realalloc = PETSC_TRUE; 3580a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 3581c461c341SBarry Smith skipallocation = PETSC_TRUE; 3582c461c341SBarry Smith nz = 0; 3583c461c341SBarry Smith } 358426283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 358526283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3586899cda47SBarry Smith 3587435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 358860e0710aSBarry Smith if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz); 3589b73539f3SBarry Smith if (nnz) { 3590d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 359160e0710aSBarry 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]); 359260e0710aSBarry 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); 3593b73539f3SBarry Smith } 3594b73539f3SBarry Smith } 3595b73539f3SBarry Smith 3596273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 35972205254eSKarl Rupp 3598273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 3599273d9f13SBarry Smith 3600ab93d7beSBarry Smith if (!skipallocation) { 36012ee49352SLisandro Dalcin if (!b->imax) { 3602dcca6d9dSJed Brown ierr = PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);CHKERRQ(ierr); 36033bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 36042ee49352SLisandro Dalcin } 3605846b4da1SFande Kong if (!b->ipre) { 3606846b4da1SFande Kong ierr = PetscMalloc1(B->rmap->n,&b->ipre);CHKERRQ(ierr); 3607846b4da1SFande Kong ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3608846b4da1SFande Kong } 3609273d9f13SBarry Smith if (!nnz) { 3610435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 3611c62bd62aSJed Brown else if (nz < 0) nz = 1; 3612d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) b->imax[i] = nz; 3613d0f46423SBarry Smith nz = nz*B->rmap->n; 3614273d9f13SBarry Smith } else { 3615273d9f13SBarry Smith nz = 0; 3616d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 3617273d9f13SBarry Smith } 3618ab93d7beSBarry Smith /* b->ilen will count nonzeros in each row so far. */ 36192205254eSKarl Rupp for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0; 3620ab93d7beSBarry Smith 3621273d9f13SBarry Smith /* allocate the matrix space */ 362253dd7562SDmitry Karpeev /* FIXME: should B's old memory be unlogged? */ 36232ee49352SLisandro Dalcin ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr); 3624396832f4SHong Zhang if (B->structure_only) { 36255848002fSHong Zhang ierr = PetscMalloc1(nz,&b->j);CHKERRQ(ierr); 36265848002fSHong Zhang ierr = PetscMalloc1(B->rmap->n+1,&b->i);CHKERRQ(ierr); 3627396832f4SHong Zhang ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));CHKERRQ(ierr); 3628396832f4SHong Zhang } else { 3629dcca6d9dSJed Brown ierr = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr); 36303bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr); 3631396832f4SHong Zhang } 3632bfeeae90SHong Zhang b->i[0] = 0; 3633d0f46423SBarry Smith for (i=1; i<B->rmap->n+1; i++) { 36345da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 36355da197adSKris Buschelman } 3636396832f4SHong Zhang if (B->structure_only) { 3637396832f4SHong Zhang b->singlemalloc = PETSC_FALSE; 3638396832f4SHong Zhang b->free_a = PETSC_FALSE; 3639396832f4SHong Zhang } else { 3640273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 3641e6b907acSBarry Smith b->free_a = PETSC_TRUE; 3642396832f4SHong Zhang } 3643e6b907acSBarry Smith b->free_ij = PETSC_TRUE; 3644c461c341SBarry Smith } else { 3645e6b907acSBarry Smith b->free_a = PETSC_FALSE; 3646e6b907acSBarry Smith b->free_ij = PETSC_FALSE; 3647c461c341SBarry Smith } 3648273d9f13SBarry Smith 3649846b4da1SFande Kong if (b->ipre && nnz != b->ipre && b->imax) { 3650846b4da1SFande Kong /* reserve user-requested sparsity */ 3651846b4da1SFande Kong ierr = PetscMemcpy(b->ipre,b->imax,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3652846b4da1SFande Kong } 3653846b4da1SFande Kong 3654846b4da1SFande Kong 3655273d9f13SBarry Smith b->nz = 0; 3656273d9f13SBarry Smith b->maxnz = nz; 3657273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 36582205254eSKarl Rupp if (realalloc) { 36592205254eSKarl Rupp ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 36602205254eSKarl Rupp } 3661cb7b82ddSBarry Smith B->was_assembled = PETSC_FALSE; 3662cb7b82ddSBarry Smith B->assembled = PETSC_FALSE; 3663273d9f13SBarry Smith PetscFunctionReturn(0); 3664273d9f13SBarry Smith } 3665273d9f13SBarry Smith 3666846b4da1SFande Kong 3667846b4da1SFande Kong PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A) 3668846b4da1SFande Kong { 3669846b4da1SFande Kong Mat_SeqAIJ *a; 3670a5bbaf83SFande Kong PetscInt i; 3671846b4da1SFande Kong PetscErrorCode ierr; 3672846b4da1SFande Kong 3673846b4da1SFande Kong PetscFunctionBegin; 3674846b4da1SFande Kong PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3675846b4da1SFande Kong a = (Mat_SeqAIJ*)A->data; 36762c814fdeSFande Kong /* if no saved info, we error out */ 36772c814fdeSFande Kong if (!a->ipre) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_NULL,"No saved preallocation info \n"); 36782c814fdeSFande Kong 36792c814fdeSFande Kong if (!a->i || !a->j || !a->a || !a->imax || !a->ilen) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_NULL,"Memory info is incomplete, and can not reset preallocation \n"); 36802c814fdeSFande Kong 3681846b4da1SFande Kong ierr = PetscMemcpy(a->imax,a->ipre,A->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3682846b4da1SFande Kong ierr = PetscMemzero(a->ilen,A->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3683846b4da1SFande Kong a->i[0] = 0; 3684846b4da1SFande Kong for (i=1; i<A->rmap->n+1; i++) { 3685846b4da1SFande Kong a->i[i] = a->i[i-1] + a->imax[i-1]; 3686846b4da1SFande Kong } 3687846b4da1SFande Kong A->preallocated = PETSC_TRUE; 3688846b4da1SFande Kong a->nz = 0; 3689846b4da1SFande Kong a->maxnz = a->i[A->rmap->n]; 3690846b4da1SFande Kong A->info.nz_unneeded = (double)a->maxnz; 3691846b4da1SFande Kong A->was_assembled = PETSC_FALSE; 3692846b4da1SFande Kong A->assembled = PETSC_FALSE; 3693846b4da1SFande Kong PetscFunctionReturn(0); 3694846b4da1SFande Kong } 3695846b4da1SFande Kong 369658d36128SBarry Smith /*@ 3697a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 3698a1661176SMatthew Knepley 3699a1661176SMatthew Knepley Input Parameters: 3700a1661176SMatthew Knepley + B - the matrix 3701a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 3702a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 3703a1661176SMatthew Knepley - v - optional values in the matrix 3704a1661176SMatthew Knepley 3705a1661176SMatthew Knepley Level: developer 3706a1661176SMatthew Knepley 370758d36128SBarry Smith The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays() 370858d36128SBarry Smith 3709a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential 3710a1661176SMatthew Knepley 3711c1c1d628SHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ 3712a1661176SMatthew Knepley @*/ 3713a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 3714a1661176SMatthew Knepley { 3715a1661176SMatthew Knepley PetscErrorCode ierr; 3716a1661176SMatthew Knepley 3717a1661176SMatthew Knepley PetscFunctionBegin; 37180700a824SBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,1); 37196ba663aaSJed Brown PetscValidType(B,1); 37204ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3721a1661176SMatthew Knepley PetscFunctionReturn(0); 3722a1661176SMatthew Knepley } 3723a1661176SMatthew Knepley 37247087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3725a1661176SMatthew Knepley { 3726a1661176SMatthew Knepley PetscInt i; 3727a1661176SMatthew Knepley PetscInt m,n; 3728a1661176SMatthew Knepley PetscInt nz; 3729a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 3730a1661176SMatthew Knepley PetscScalar *values; 3731a1661176SMatthew Knepley PetscErrorCode ierr; 3732a1661176SMatthew Knepley 3733a1661176SMatthew Knepley PetscFunctionBegin; 373465e19b50SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]); 3735779a8d59SSatish Balay 3736779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 3737779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3738779a8d59SSatish Balay 3739779a8d59SSatish Balay ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 3740854ce69bSBarry Smith ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr); 3741a1661176SMatthew Knepley for (i = 0; i < m; i++) { 3742b7940d39SSatish Balay nz = Ii[i+1]- Ii[i]; 3743a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 374465e19b50SBarry Smith if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 3745a1661176SMatthew Knepley nnz[i] = nz; 3746a1661176SMatthew Knepley } 3747a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 3748a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 3749a1661176SMatthew Knepley 3750a1661176SMatthew Knepley if (v) { 3751a1661176SMatthew Knepley values = (PetscScalar*) v; 3752a1661176SMatthew Knepley } else { 37531795a4d1SJed Brown ierr = PetscCalloc1(nz_max, &values);CHKERRQ(ierr); 3754a1661176SMatthew Knepley } 3755a1661176SMatthew Knepley 3756a1661176SMatthew Knepley for (i = 0; i < m; i++) { 3757b7940d39SSatish Balay nz = Ii[i+1] - Ii[i]; 3758b7940d39SSatish Balay ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr); 3759a1661176SMatthew Knepley } 3760a1661176SMatthew Knepley 3761a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3762a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3763a1661176SMatthew Knepley 3764a1661176SMatthew Knepley if (!v) { 3765a1661176SMatthew Knepley ierr = PetscFree(values);CHKERRQ(ierr); 3766a1661176SMatthew Knepley } 37677827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 3768a1661176SMatthew Knepley PetscFunctionReturn(0); 3769a1661176SMatthew Knepley } 3770a1661176SMatthew Knepley 3771c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h> 3772af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h> 3773170fe5c8SBarry Smith 3774170fe5c8SBarry Smith /* 3775170fe5c8SBarry Smith Computes (B'*A')' since computing B*A directly is untenable 3776170fe5c8SBarry Smith 3777170fe5c8SBarry Smith n p p 3778170fe5c8SBarry Smith ( ) ( ) ( ) 3779170fe5c8SBarry Smith m ( A ) * n ( B ) = m ( C ) 3780170fe5c8SBarry Smith ( ) ( ) ( ) 3781170fe5c8SBarry Smith 3782170fe5c8SBarry Smith */ 3783170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C) 3784170fe5c8SBarry Smith { 3785170fe5c8SBarry Smith PetscErrorCode ierr; 3786170fe5c8SBarry Smith Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data; 3787170fe5c8SBarry Smith Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data; 3788170fe5c8SBarry Smith Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data; 37891de00fd4SBarry Smith PetscInt i,n,m,q,p; 3790170fe5c8SBarry Smith const PetscInt *ii,*idx; 3791170fe5c8SBarry Smith const PetscScalar *b,*a,*a_q; 3792170fe5c8SBarry Smith PetscScalar *c,*c_q; 3793170fe5c8SBarry Smith 3794170fe5c8SBarry Smith PetscFunctionBegin; 3795d0f46423SBarry Smith m = A->rmap->n; 3796d0f46423SBarry Smith n = A->cmap->n; 3797d0f46423SBarry Smith p = B->cmap->n; 3798170fe5c8SBarry Smith a = sub_a->v; 3799170fe5c8SBarry Smith b = sub_b->a; 3800170fe5c8SBarry Smith c = sub_c->v; 3801170fe5c8SBarry Smith ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr); 3802170fe5c8SBarry Smith 3803170fe5c8SBarry Smith ii = sub_b->i; 3804170fe5c8SBarry Smith idx = sub_b->j; 3805170fe5c8SBarry Smith for (i=0; i<n; i++) { 3806170fe5c8SBarry Smith q = ii[i+1] - ii[i]; 3807170fe5c8SBarry Smith while (q-->0) { 3808170fe5c8SBarry Smith c_q = c + m*(*idx); 3809170fe5c8SBarry Smith a_q = a + m*i; 3810854c7f52SBarry Smith PetscKernelAXPY(c_q,*b,a_q,m); 3811170fe5c8SBarry Smith idx++; 3812170fe5c8SBarry Smith b++; 3813170fe5c8SBarry Smith } 3814170fe5c8SBarry Smith } 3815170fe5c8SBarry Smith PetscFunctionReturn(0); 3816170fe5c8SBarry Smith } 3817170fe5c8SBarry Smith 3818170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 3819170fe5c8SBarry Smith { 3820170fe5c8SBarry Smith PetscErrorCode ierr; 3821d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 3822170fe5c8SBarry Smith Mat Cmat; 3823170fe5c8SBarry Smith 3824170fe5c8SBarry Smith PetscFunctionBegin; 382560e0710aSBarry 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); 3826ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr); 3827170fe5c8SBarry Smith ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr); 382833d57670SJed Brown ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr); 3829170fe5c8SBarry Smith ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr); 38300298fd71SBarry Smith ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr); 3831d73949e8SHong Zhang 3832d73949e8SHong Zhang Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ; 38332205254eSKarl Rupp 3834170fe5c8SBarry Smith *C = Cmat; 3835170fe5c8SBarry Smith PetscFunctionReturn(0); 3836170fe5c8SBarry Smith } 3837170fe5c8SBarry Smith 3838170fe5c8SBarry Smith /* ----------------------------------------------------------------*/ 3839150d2497SBarry Smith PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 3840170fe5c8SBarry Smith { 3841170fe5c8SBarry Smith PetscErrorCode ierr; 3842170fe5c8SBarry Smith 3843170fe5c8SBarry Smith PetscFunctionBegin; 3844170fe5c8SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 38453ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 3846170fe5c8SBarry Smith ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr); 38473ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 3848170fe5c8SBarry Smith } 38493ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 3850170fe5c8SBarry Smith ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr); 38513ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 3852170fe5c8SBarry Smith PetscFunctionReturn(0); 3853170fe5c8SBarry Smith } 3854170fe5c8SBarry Smith 3855170fe5c8SBarry Smith 38560bad9183SKris Buschelman /*MC 3857fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 38580bad9183SKris Buschelman based on compressed sparse row format. 38590bad9183SKris Buschelman 38600bad9183SKris Buschelman Options Database Keys: 38610bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 38620bad9183SKris Buschelman 38630bad9183SKris Buschelman Level: beginner 38640bad9183SKris Buschelman 3865f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 38660bad9183SKris Buschelman M*/ 38670bad9183SKris Buschelman 3868ccd284c7SBarry Smith /*MC 3869ccd284c7SBarry Smith MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 3870ccd284c7SBarry Smith 3871ccd284c7SBarry Smith This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 3872ccd284c7SBarry Smith and MATMPIAIJ otherwise. As a result, for single process communicators, 3873ccd284c7SBarry Smith MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported 3874ccd284c7SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 3875ccd284c7SBarry Smith the above preallocation routines for simplicity. 3876ccd284c7SBarry Smith 3877ccd284c7SBarry Smith Options Database Keys: 3878ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 3879ccd284c7SBarry Smith 388095452b02SPatrick Sanan Developer Notes: 388195452b02SPatrick Sanan Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when 3882ccd284c7SBarry Smith enough exist. 3883ccd284c7SBarry Smith 3884ccd284c7SBarry Smith Level: beginner 3885ccd284c7SBarry Smith 3886ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ 3887ccd284c7SBarry Smith M*/ 3888ccd284c7SBarry Smith 3889ccd284c7SBarry Smith /*MC 3890ccd284c7SBarry Smith MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices. 3891ccd284c7SBarry Smith 3892ccd284c7SBarry Smith This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator, 3893ccd284c7SBarry Smith and MATMPIAIJCRL otherwise. As a result, for single process communicators, 3894ccd284c7SBarry Smith MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported 3895ccd284c7SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 3896ccd284c7SBarry Smith the above preallocation routines for simplicity. 3897ccd284c7SBarry Smith 3898ccd284c7SBarry Smith Options Database Keys: 3899ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions() 3900ccd284c7SBarry Smith 3901ccd284c7SBarry Smith Level: beginner 3902ccd284c7SBarry Smith 3903ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL 3904ccd284c7SBarry Smith M*/ 3905ccd284c7SBarry Smith 39067906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*); 39077906f579SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 39087906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*); 39097906f579SHong Zhang #endif 39107906f579SHong Zhang #if defined(PETSC_HAVE_HYPRE) 39117906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*); 39127906f579SHong Zhang PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 39137906f579SHong Zhang #endif 39147906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*); 39157906f579SHong Zhang 39167906f579SHong Zhang #if defined(PETSC_HAVE_MATLAB_ENGINE) 39177906f579SHong Zhang PETSC_EXTERN PetscErrorCode MatlabEnginePut_SeqAIJ(PetscObject,void*); 39187906f579SHong Zhang PETSC_EXTERN PetscErrorCode MatlabEngineGet_SeqAIJ(PetscObject,void*); 39197906f579SHong Zhang #endif 39207906f579SHong Zhang 3921d4002b98SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*); 392275d48cdbSStefano Zampini PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*); 39237906f579SHong Zhang 39248c778c55SBarry Smith /*@C 39258397e458SBarry Smith MatSeqAIJGetArray - gives access to the array where the data for a MATSEQAIJ matrix is stored 39268c778c55SBarry Smith 39278c778c55SBarry Smith Not Collective 39288c778c55SBarry Smith 39298c778c55SBarry Smith Input Parameter: 3930579dbff0SBarry Smith . mat - a MATSEQAIJ matrix 39318c778c55SBarry Smith 39328c778c55SBarry Smith Output Parameter: 39338c778c55SBarry Smith . array - pointer to the data 39348c778c55SBarry Smith 39358c778c55SBarry Smith Level: intermediate 39368c778c55SBarry Smith 3937774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90() 39388c778c55SBarry Smith @*/ 39398c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray(Mat A,PetscScalar **array) 39408c778c55SBarry Smith { 39418c778c55SBarry Smith PetscErrorCode ierr; 39428c778c55SBarry Smith 39438c778c55SBarry Smith PetscFunctionBegin; 39448c778c55SBarry Smith ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr); 39458c778c55SBarry Smith PetscFunctionReturn(0); 39468c778c55SBarry Smith } 39478c778c55SBarry Smith 394821e72a00SBarry Smith /*@C 394921e72a00SBarry Smith MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row 395021e72a00SBarry Smith 395121e72a00SBarry Smith Not Collective 395221e72a00SBarry Smith 395321e72a00SBarry Smith Input Parameter: 3954579dbff0SBarry Smith . mat - a MATSEQAIJ matrix 395521e72a00SBarry Smith 395621e72a00SBarry Smith Output Parameter: 395721e72a00SBarry Smith . nz - the maximum number of nonzeros in any row 395821e72a00SBarry Smith 395921e72a00SBarry Smith Level: intermediate 396021e72a00SBarry Smith 396121e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90() 396221e72a00SBarry Smith @*/ 396321e72a00SBarry Smith PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz) 396421e72a00SBarry Smith { 396521e72a00SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data; 396621e72a00SBarry Smith 396721e72a00SBarry Smith PetscFunctionBegin; 396821e72a00SBarry Smith *nz = aij->rmax; 396921e72a00SBarry Smith PetscFunctionReturn(0); 397021e72a00SBarry Smith } 397121e72a00SBarry Smith 39728c778c55SBarry Smith /*@C 3973579dbff0SBarry Smith MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray() 39748c778c55SBarry Smith 39758c778c55SBarry Smith Not Collective 39768c778c55SBarry Smith 39778c778c55SBarry Smith Input Parameters: 3978579dbff0SBarry Smith . mat - a MATSEQAIJ matrix 39798c778c55SBarry Smith . array - pointer to the data 39808c778c55SBarry Smith 39818c778c55SBarry Smith Level: intermediate 39828c778c55SBarry Smith 3983774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90() 39848c778c55SBarry Smith @*/ 39858c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray(Mat A,PetscScalar **array) 39868c778c55SBarry Smith { 39878c778c55SBarry Smith PetscErrorCode ierr; 39888c778c55SBarry Smith 39898c778c55SBarry Smith PetscFunctionBegin; 39908c778c55SBarry Smith ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr); 39918c778c55SBarry Smith PetscFunctionReturn(0); 39928c778c55SBarry Smith } 39938c778c55SBarry Smith 39948cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B) 3995273d9f13SBarry Smith { 3996273d9f13SBarry Smith Mat_SeqAIJ *b; 3997dfbe8321SBarry Smith PetscErrorCode ierr; 399838baddfdSBarry Smith PetscMPIInt size; 3999273d9f13SBarry Smith 4000273d9f13SBarry Smith PetscFunctionBegin; 4001ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr); 4002e32f2f54SBarry Smith if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 4003273d9f13SBarry Smith 4004b00a9115SJed Brown ierr = PetscNewLog(B,&b);CHKERRQ(ierr); 40052205254eSKarl Rupp 4006b0a32e0cSBarry Smith B->data = (void*)b; 40072205254eSKarl Rupp 4008549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 40092205254eSKarl Rupp 4010416022c9SBarry Smith b->row = 0; 4011416022c9SBarry Smith b->col = 0; 401282bf6240SBarry Smith b->icol = 0; 4013b810aeb4SBarry Smith b->reallocs = 0; 401436db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 4015f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 4016416022c9SBarry Smith b->nonew = 0; 4017416022c9SBarry Smith b->diag = 0; 4018416022c9SBarry Smith b->solve_work = 0; 40192a1b7f2aSHong Zhang B->spptr = 0; 4020be6bf707SBarry Smith b->saved_values = 0; 4021d7f994e1SBarry Smith b->idiag = 0; 402271f1c65dSBarry Smith b->mdiag = 0; 402371f1c65dSBarry Smith b->ssor_work = 0; 402471f1c65dSBarry Smith b->omega = 1.0; 402571f1c65dSBarry Smith b->fshift = 0.0; 402671f1c65dSBarry Smith b->idiagvalid = PETSC_FALSE; 4027bbead8a2SBarry Smith b->ibdiagvalid = PETSC_FALSE; 4028a9817697SBarry Smith b->keepnonzeropattern = PETSC_FALSE; 402917ab2063SBarry Smith 403035d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 4031bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr); 4032bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr); 40338c778c55SBarry Smith 4034b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4035bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr); 4036bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr); 4037b3866ffcSBarry Smith #endif 403817f1a0eaSHong Zhang 4039bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 4040bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr); 4041bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 4042bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 4043bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 4044bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr); 40459779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE) 40464a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr); 4047191b95cbSRichard Tran Mills #endif 4048bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr); 4049af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 4050af8000cdSHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr); 4051af8000cdSHong Zhang #endif 405263c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE) 405363c07aadSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);CHKERRQ(ierr); 40543dad0653Sstefano_zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);CHKERRQ(ierr); 405563c07aadSStefano Zampini #endif 4056b49cda9fSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr); 4057d4002b98SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);CHKERRQ(ierr); 4058bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 4059bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 4060bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 4061846b4da1SFande Kong ierr = PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);CHKERRQ(ierr); 4062bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 4063bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 4064bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr); 4065bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 4066bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 406775d48cdbSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);CHKERRQ(ierr); 40684108e4d5SBarry Smith ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr); 406917667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 40704099cc6bSBarry Smith ierr = MatSeqAIJSetTypeFromOptions(B);CHKERRQ(ierr); /* this allows changing the matrix subtype to say MATSEQAIJPERM */ 40713a40ed3dSBarry Smith PetscFunctionReturn(0); 407217ab2063SBarry Smith } 407317ab2063SBarry Smith 4074b24902e0SBarry Smith /* 4075b24902e0SBarry Smith Given a matrix generated with MatGetFactor() duplicates all the information in A into B 4076b24902e0SBarry Smith */ 4077ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace) 407817ab2063SBarry Smith { 4079416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 40806849ba73SBarry Smith PetscErrorCode ierr; 4081d0f46423SBarry Smith PetscInt i,m = A->rmap->n; 408217ab2063SBarry Smith 40833a40ed3dSBarry Smith PetscFunctionBegin; 4084273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 4085273d9f13SBarry Smith 4086d5f3da31SBarry Smith C->factortype = A->factortype; 4087416022c9SBarry Smith c->row = 0; 4088416022c9SBarry Smith c->col = 0; 408982bf6240SBarry Smith c->icol = 0; 40906ad4291fSHong Zhang c->reallocs = 0; 409117ab2063SBarry Smith 40926ad4291fSHong Zhang C->assembled = PETSC_TRUE; 409317ab2063SBarry Smith 4094aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr); 4095aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr); 4096eec197d1SBarry Smith 4097dcca6d9dSJed Brown ierr = PetscMalloc2(m,&c->imax,m,&c->ilen);CHKERRQ(ierr); 40983bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr); 409917ab2063SBarry Smith for (i=0; i<m; i++) { 4100416022c9SBarry Smith c->imax[i] = a->imax[i]; 4101416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 410217ab2063SBarry Smith } 410317ab2063SBarry Smith 410417ab2063SBarry Smith /* allocate the matrix space */ 4105f77e22a1SHong Zhang if (mallocmatspace) { 4106dcca6d9dSJed Brown ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr); 41073bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 41082205254eSKarl Rupp 4109f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 41102205254eSKarl Rupp 411197f1f81fSBarry Smith ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 411217ab2063SBarry Smith if (m > 0) { 411397f1f81fSBarry Smith ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr); 4114be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 4115bfeeae90SHong Zhang ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 4116be6bf707SBarry Smith } else { 4117bfeeae90SHong Zhang ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr); 411817ab2063SBarry Smith } 411908480c60SBarry Smith } 4120f77e22a1SHong Zhang } 412117ab2063SBarry Smith 41226ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 4123416022c9SBarry Smith c->roworiented = a->roworiented; 4124416022c9SBarry Smith c->nonew = a->nonew; 4125416022c9SBarry Smith if (a->diag) { 4126854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr); 41273bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 412817ab2063SBarry Smith for (i=0; i<m; i++) { 4129416022c9SBarry Smith c->diag[i] = a->diag[i]; 413017ab2063SBarry Smith } 41313a40ed3dSBarry Smith } else c->diag = 0; 41322205254eSKarl Rupp 41336ad4291fSHong Zhang c->solve_work = 0; 41346ad4291fSHong Zhang c->saved_values = 0; 41356ad4291fSHong Zhang c->idiag = 0; 413671f1c65dSBarry Smith c->ssor_work = 0; 4137a9817697SBarry Smith c->keepnonzeropattern = a->keepnonzeropattern; 4138e6b907acSBarry Smith c->free_a = PETSC_TRUE; 4139e6b907acSBarry Smith c->free_ij = PETSC_TRUE; 41406ad4291fSHong Zhang 4141893ad86cSHong Zhang c->rmax = a->rmax; 4142416022c9SBarry Smith c->nz = a->nz; 41438ed568f8SMatthew G Knepley c->maxnz = a->nz; /* Since we allocate exactly the right amount */ 4144273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 4145754ec7b1SSatish Balay 41466ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 41476ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 4148cd6b891eSBarry Smith if (a->compressedrow.use) { 41496ad4291fSHong Zhang i = a->compressedrow.nrows; 4150dcca6d9dSJed Brown ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr); 41516ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr); 41526ad4291fSHong Zhang ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr); 415327ea64f8SHong Zhang } else { 415427ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 41550298fd71SBarry Smith c->compressedrow.i = NULL; 41560298fd71SBarry Smith c->compressedrow.rindex = NULL; 41576ad4291fSHong Zhang } 4158ea632784SBarry Smith c->nonzerorowcnt = a->nonzerorowcnt; 4159e56f5c9eSBarry Smith C->nonzerostate = A->nonzerostate; 41604846f1f5SKris Buschelman 41612205254eSKarl Rupp ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr); 4162140e18c1SBarry Smith ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr); 41633a40ed3dSBarry Smith PetscFunctionReturn(0); 416417ab2063SBarry Smith } 416517ab2063SBarry Smith 4166b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 4167b24902e0SBarry Smith { 4168b24902e0SBarry Smith PetscErrorCode ierr; 4169b24902e0SBarry Smith 4170b24902e0SBarry Smith PetscFunctionBegin; 4171ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 41724b6263acSBarry Smith ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr); 4173cfd3f464SBarry Smith if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) { 417433d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 4175cfd3f464SBarry Smith } 4176a54f2f98SBarry Smith ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 4177f77e22a1SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr); 4178b24902e0SBarry Smith PetscFunctionReturn(0); 4179b24902e0SBarry Smith } 4180b24902e0SBarry Smith 4181112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer) 4182fbdbba38SShri Abhyankar { 4183fbdbba38SShri Abhyankar Mat_SeqAIJ *a; 4184fbdbba38SShri Abhyankar PetscErrorCode ierr; 4185fbdbba38SShri Abhyankar PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols; 4186fbdbba38SShri Abhyankar int fd; 4187fbdbba38SShri Abhyankar PetscMPIInt size; 4188fbdbba38SShri Abhyankar MPI_Comm comm; 41893059b6faSBarry Smith PetscInt bs = newMat->rmap->bs; 4190fbdbba38SShri Abhyankar 4191fbdbba38SShri Abhyankar PetscFunctionBegin; 4192c98fd787SBarry Smith /* force binary viewer to load .info file if it has not yet done so */ 4193c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 4194fbdbba38SShri Abhyankar ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 4195fbdbba38SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4196fbdbba38SShri Abhyankar if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor"); 4197bbead8a2SBarry Smith 41980298fd71SBarry Smith ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr); 41990298fd71SBarry Smith ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr); 4200bbead8a2SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 42013059b6faSBarry Smith if (bs < 0) bs = 1; 42023059b6faSBarry Smith ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr); 4203bbead8a2SBarry Smith 4204fbdbba38SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 4205fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 4206fbdbba38SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 4207fbdbba38SShri Abhyankar M = header[1]; N = header[2]; nz = header[3]; 4208fbdbba38SShri Abhyankar 4209bbead8a2SBarry Smith if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 4210fbdbba38SShri Abhyankar 4211fbdbba38SShri Abhyankar /* read in row lengths */ 4212785e854fSJed Brown ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr); 4213fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 4214fbdbba38SShri Abhyankar 4215fbdbba38SShri Abhyankar /* check if sum of rowlengths is same as nz */ 4216fbdbba38SShri Abhyankar for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 421760e0710aSBarry Smith if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %dD, sum-row-lengths = %D\n",nz,sum); 4218fbdbba38SShri Abhyankar 4219fbdbba38SShri Abhyankar /* set global size if not set already*/ 4220f501eaabSShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) { 4221fbdbba38SShri Abhyankar ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 4222aabbc4fbSShri Abhyankar } else { 42239d36ed5fSBarry Smith /* if sizes and type are already set, check if the matrix global sizes are correct */ 4224fbdbba38SShri Abhyankar ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr); 42254c5b953cSHong Zhang if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */ 42264c5b953cSHong Zhang ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr); 42274c5b953cSHong Zhang } 422860e0710aSBarry Smith 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); 4229aabbc4fbSShri Abhyankar } 4230fbdbba38SShri Abhyankar ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr); 4231fbdbba38SShri Abhyankar a = (Mat_SeqAIJ*)newMat->data; 4232fbdbba38SShri Abhyankar 4233fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 4234fbdbba38SShri Abhyankar 4235fbdbba38SShri Abhyankar /* read in nonzero values */ 4236fbdbba38SShri Abhyankar ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 4237fbdbba38SShri Abhyankar 4238fbdbba38SShri Abhyankar /* set matrix "i" values */ 4239fbdbba38SShri Abhyankar a->i[0] = 0; 4240fbdbba38SShri Abhyankar for (i=1; i<= M; i++) { 4241fbdbba38SShri Abhyankar a->i[i] = a->i[i-1] + rowlengths[i-1]; 4242fbdbba38SShri Abhyankar a->ilen[i-1] = rowlengths[i-1]; 4243fbdbba38SShri Abhyankar } 4244fbdbba38SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 4245fbdbba38SShri Abhyankar 4246fbdbba38SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4247fbdbba38SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4248fbdbba38SShri Abhyankar PetscFunctionReturn(0); 4249fbdbba38SShri Abhyankar } 4250fbdbba38SShri Abhyankar 4251ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg) 42527264ac53SSatish Balay { 42537264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data; 4254dfbe8321SBarry Smith PetscErrorCode ierr; 4255eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 4256eeffb40dSHong Zhang PetscInt k; 4257eeffb40dSHong Zhang #endif 42587264ac53SSatish Balay 42593a40ed3dSBarry Smith PetscFunctionBegin; 4260bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 4261d0f46423SBarry Smith if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) { 4262ca44d042SBarry Smith *flg = PETSC_FALSE; 4263ca44d042SBarry Smith PetscFunctionReturn(0); 4264bcd2baecSBarry Smith } 42657264ac53SSatish Balay 42667264ac53SSatish Balay /* if the a->i are the same */ 4267d0f46423SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr); 4268abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 42697264ac53SSatish Balay 42707264ac53SSatish Balay /* if a->j are the same */ 427197f1f81fSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr); 4272abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 4273bcd2baecSBarry Smith 4274bcd2baecSBarry Smith /* if a->a are the same */ 4275eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 4276eeffb40dSHong Zhang for (k=0; k<a->nz; k++) { 4277eeffb40dSHong Zhang if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) { 4278eeffb40dSHong Zhang *flg = PETSC_FALSE; 42793a40ed3dSBarry Smith PetscFunctionReturn(0); 4280eeffb40dSHong Zhang } 4281eeffb40dSHong Zhang } 4282eeffb40dSHong Zhang #else 4283eeffb40dSHong Zhang ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 4284eeffb40dSHong Zhang #endif 4285eeffb40dSHong Zhang PetscFunctionReturn(0); 42867264ac53SSatish Balay } 428736db0b34SBarry Smith 428805869f15SSatish Balay /*@ 428936db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 429036db0b34SBarry Smith provided by the user. 429136db0b34SBarry Smith 4292c75a6043SHong Zhang Collective on MPI_Comm 429336db0b34SBarry Smith 429436db0b34SBarry Smith Input Parameters: 429536db0b34SBarry Smith + comm - must be an MPI communicator of size 1 429636db0b34SBarry Smith . m - number of rows 429736db0b34SBarry Smith . n - number of columns 429836db0b34SBarry Smith . i - row indices 429936db0b34SBarry Smith . j - column indices 430036db0b34SBarry Smith - a - matrix values 430136db0b34SBarry Smith 430236db0b34SBarry Smith Output Parameter: 430336db0b34SBarry Smith . mat - the matrix 430436db0b34SBarry Smith 430536db0b34SBarry Smith Level: intermediate 430636db0b34SBarry Smith 430736db0b34SBarry Smith Notes: 43080551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 4309292fb18eSBarry Smith once the matrix is destroyed and not before 431036db0b34SBarry Smith 431136db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 431236db0b34SBarry Smith 4313bfeeae90SHong Zhang The i and j indices are 0 based 431436db0b34SBarry Smith 4315a4552177SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 4316a4552177SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 43178eef79e4SBarry Smith as shown 4318a4552177SSatish Balay 43198eef79e4SBarry Smith $ 1 0 0 43208eef79e4SBarry Smith $ 2 0 3 43218eef79e4SBarry Smith $ 4 5 6 43228eef79e4SBarry Smith $ 43238eef79e4SBarry Smith $ i = {0,1,3,6} [size = nrow+1 = 3+1] 43248eef79e4SBarry Smith $ j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row 43258eef79e4SBarry Smith $ v = {1,2,3,4,5,6} [size = 6] 4326a4552177SSatish Balay 43279985e31cSBarry Smith 432869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 432936db0b34SBarry Smith 433036db0b34SBarry Smith @*/ 4331c3c607ccSBarry Smith PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat) 433236db0b34SBarry Smith { 4333dfbe8321SBarry Smith PetscErrorCode ierr; 4334cbcfb4deSHong Zhang PetscInt ii; 433536db0b34SBarry Smith Mat_SeqAIJ *aij; 4336cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG) 4337cbcfb4deSHong Zhang PetscInt jj; 4338cbcfb4deSHong Zhang #endif 433936db0b34SBarry Smith 434036db0b34SBarry Smith PetscFunctionBegin; 434141096f02SStefano Zampini if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4342f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4343f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 4344a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */ 4345ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 4346ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 4347ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 4348dcca6d9dSJed Brown ierr = PetscMalloc2(m,&aij->imax,m,&aij->ilen);CHKERRQ(ierr); 4349ab93d7beSBarry Smith 435036db0b34SBarry Smith aij->i = i; 435136db0b34SBarry Smith aij->j = j; 435236db0b34SBarry Smith aij->a = a; 435336db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 435436db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 4355e6b907acSBarry Smith aij->free_a = PETSC_FALSE; 4356e6b907acSBarry Smith aij->free_ij = PETSC_FALSE; 435736db0b34SBarry Smith 435836db0b34SBarry Smith for (ii=0; ii<m; ii++) { 435936db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 43602515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 436160e0710aSBarry 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]); 43629985e31cSBarry Smith for (jj=i[ii]+1; jj<i[ii+1]; jj++) { 4363e32f2f54SBarry 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); 4364e32f2f54SBarry 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); 43659985e31cSBarry Smith } 436636db0b34SBarry Smith #endif 436736db0b34SBarry Smith } 43682515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 436936db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 437060e0710aSBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]); 437160e0710aSBarry 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]); 437236db0b34SBarry Smith } 437336db0b34SBarry Smith #endif 437436db0b34SBarry Smith 4375b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4376b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 437736db0b34SBarry Smith PetscFunctionReturn(0); 437836db0b34SBarry Smith } 437980ef6e79SMatthew G Knepley /*@C 4380d021a1c5SVictor Minden MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format) 43818a0b0e6bSVictor Minden provided by the user. 43828a0b0e6bSVictor Minden 43838a0b0e6bSVictor Minden Collective on MPI_Comm 43848a0b0e6bSVictor Minden 43858a0b0e6bSVictor Minden Input Parameters: 43868a0b0e6bSVictor Minden + comm - must be an MPI communicator of size 1 43878a0b0e6bSVictor Minden . m - number of rows 43888a0b0e6bSVictor Minden . n - number of columns 43898a0b0e6bSVictor Minden . i - row indices 43908a0b0e6bSVictor Minden . j - column indices 43911230e6d1SVictor Minden . a - matrix values 43921230e6d1SVictor Minden . nz - number of nonzeros 43931230e6d1SVictor Minden - idx - 0 or 1 based 43948a0b0e6bSVictor Minden 43958a0b0e6bSVictor Minden Output Parameter: 43968a0b0e6bSVictor Minden . mat - the matrix 43978a0b0e6bSVictor Minden 43988a0b0e6bSVictor Minden Level: intermediate 43998a0b0e6bSVictor Minden 44008a0b0e6bSVictor Minden Notes: 44018a0b0e6bSVictor Minden The i and j indices are 0 based 44028a0b0e6bSVictor Minden 44038a0b0e6bSVictor Minden The format which is used for the sparse matrix input, is equivalent to a 44048a0b0e6bSVictor Minden row-major ordering.. i.e for the following matrix, the input data expected is 44058a0b0e6bSVictor Minden as shown: 44068a0b0e6bSVictor Minden 44078a0b0e6bSVictor Minden 1 0 0 44088a0b0e6bSVictor Minden 2 0 3 44098a0b0e6bSVictor Minden 4 5 6 44108a0b0e6bSVictor Minden 44118a0b0e6bSVictor Minden i = {0,1,1,2,2,2} 44128a0b0e6bSVictor Minden j = {0,0,2,0,1,2} 44138a0b0e6bSVictor Minden v = {1,2,3,4,5,6} 44148a0b0e6bSVictor Minden 44158a0b0e6bSVictor Minden 441669b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 44178a0b0e6bSVictor Minden 44188a0b0e6bSVictor Minden @*/ 4419c3c607ccSBarry Smith PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx) 44208a0b0e6bSVictor Minden { 44218a0b0e6bSVictor Minden PetscErrorCode ierr; 4422d021a1c5SVictor Minden PetscInt ii, *nnz, one = 1,row,col; 44238a0b0e6bSVictor Minden 44248a0b0e6bSVictor Minden 44258a0b0e6bSVictor Minden PetscFunctionBegin; 44261795a4d1SJed Brown ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr); 44271230e6d1SVictor Minden for (ii = 0; ii < nz; ii++) { 4428c8d679ebSHong Zhang nnz[i[ii] - !!idx] += 1; 44291230e6d1SVictor Minden } 44308a0b0e6bSVictor Minden ierr = MatCreate(comm,mat);CHKERRQ(ierr); 44318a0b0e6bSVictor Minden ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 44328a0b0e6bSVictor Minden ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 44331230e6d1SVictor Minden ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr); 44341230e6d1SVictor Minden for (ii = 0; ii < nz; ii++) { 44351230e6d1SVictor Minden if (idx) { 44361230e6d1SVictor Minden row = i[ii] - 1; 44371230e6d1SVictor Minden col = j[ii] - 1; 44381230e6d1SVictor Minden } else { 44391230e6d1SVictor Minden row = i[ii]; 44401230e6d1SVictor Minden col = j[ii]; 44418a0b0e6bSVictor Minden } 44421230e6d1SVictor Minden ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr); 44438a0b0e6bSVictor Minden } 44448a0b0e6bSVictor Minden ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 44458a0b0e6bSVictor Minden ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4446d021a1c5SVictor Minden ierr = PetscFree(nnz);CHKERRQ(ierr); 44478a0b0e6bSVictor Minden PetscFunctionReturn(0); 44488a0b0e6bSVictor Minden } 444936db0b34SBarry Smith 4450acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A) 4451acf2f550SJed Brown { 4452acf2f550SJed Brown Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 4453acf2f550SJed Brown PetscErrorCode ierr; 4454acf2f550SJed Brown 4455acf2f550SJed Brown PetscFunctionBegin; 4456acf2f550SJed Brown a->idiagvalid = PETSC_FALSE; 4457acf2f550SJed Brown a->ibdiagvalid = PETSC_FALSE; 44582205254eSKarl Rupp 4459acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr); 4460acf2f550SJed Brown PetscFunctionReturn(0); 4461acf2f550SJed Brown } 4462acf2f550SJed Brown 44639c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 44649c8f2541SHong Zhang { 44659c8f2541SHong Zhang PetscErrorCode ierr; 44668761c3d6SHong Zhang PetscMPIInt size; 44679c8f2541SHong Zhang 44689c8f2541SHong Zhang PetscFunctionBegin; 44698761c3d6SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 44707bbdc51dSHong Zhang if (size == 1) { 44717bbdc51dSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 44727bbdc51dSHong Zhang ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr); 44737bbdc51dSHong Zhang } else { 44748761c3d6SHong Zhang ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 44757bbdc51dSHong Zhang } 44768761c3d6SHong Zhang } else { 44779c8f2541SHong Zhang ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr); 44788761c3d6SHong Zhang } 44799c8f2541SHong Zhang PetscFunctionReturn(0); 44809c8f2541SHong Zhang } 44819c8f2541SHong Zhang 448281824310SBarry Smith /* 448353dd7562SDmitry Karpeev Permute A into C's *local* index space using rowemb,colemb. 448453dd7562SDmitry Karpeev The embedding are supposed to be injections and the above implies that the range of rowemb is a subset 448553dd7562SDmitry Karpeev of [0,m), colemb is in [0,n). 448653dd7562SDmitry Karpeev If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A. 448753dd7562SDmitry Karpeev */ 448853dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B) 448953dd7562SDmitry Karpeev { 449053dd7562SDmitry Karpeev /* If making this function public, change the error returned in this function away from _PLIB. */ 449153dd7562SDmitry Karpeev PetscErrorCode ierr; 449253dd7562SDmitry Karpeev Mat_SeqAIJ *Baij; 449353dd7562SDmitry Karpeev PetscBool seqaij; 449453dd7562SDmitry Karpeev PetscInt m,n,*nz,i,j,count; 449553dd7562SDmitry Karpeev PetscScalar v; 449653dd7562SDmitry Karpeev const PetscInt *rowindices,*colindices; 449753dd7562SDmitry Karpeev 449853dd7562SDmitry Karpeev PetscFunctionBegin; 449953dd7562SDmitry Karpeev if (!B) PetscFunctionReturn(0); 450053dd7562SDmitry Karpeev /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */ 45014099cc6bSBarry Smith ierr = PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr); 450253dd7562SDmitry Karpeev if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type"); 450353dd7562SDmitry Karpeev if (rowemb) { 450453dd7562SDmitry Karpeev ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr); 450553dd7562SDmitry Karpeev if (m != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Row IS of size %D is incompatible with matrix row size %D",m,B->rmap->n); 450653dd7562SDmitry Karpeev } else { 45076c4ed002SBarry Smith if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix"); 450853dd7562SDmitry Karpeev } 450953dd7562SDmitry Karpeev if (colemb) { 451053dd7562SDmitry Karpeev ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr); 451153dd7562SDmitry Karpeev if (n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Diag col IS of size %D is incompatible with input matrix col size %D",n,B->cmap->n); 451253dd7562SDmitry Karpeev } else { 451353dd7562SDmitry Karpeev if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix"); 451453dd7562SDmitry Karpeev } 451553dd7562SDmitry Karpeev 451653dd7562SDmitry Karpeev Baij = (Mat_SeqAIJ*)(B->data); 451753dd7562SDmitry Karpeev if (pattern == DIFFERENT_NONZERO_PATTERN) { 451853dd7562SDmitry Karpeev ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr); 451953dd7562SDmitry Karpeev for (i=0; i<B->rmap->n; i++) { 452053dd7562SDmitry Karpeev nz[i] = Baij->i[i+1] - Baij->i[i]; 452153dd7562SDmitry Karpeev } 452253dd7562SDmitry Karpeev ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr); 452353dd7562SDmitry Karpeev ierr = PetscFree(nz);CHKERRQ(ierr); 452453dd7562SDmitry Karpeev } 452553dd7562SDmitry Karpeev if (pattern == SUBSET_NONZERO_PATTERN) { 452653dd7562SDmitry Karpeev ierr = MatZeroEntries(C);CHKERRQ(ierr); 452753dd7562SDmitry Karpeev } 452853dd7562SDmitry Karpeev count = 0; 452953dd7562SDmitry Karpeev rowindices = NULL; 453053dd7562SDmitry Karpeev colindices = NULL; 453153dd7562SDmitry Karpeev if (rowemb) { 453253dd7562SDmitry Karpeev ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr); 453353dd7562SDmitry Karpeev } 453453dd7562SDmitry Karpeev if (colemb) { 453553dd7562SDmitry Karpeev ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr); 453653dd7562SDmitry Karpeev } 453753dd7562SDmitry Karpeev for (i=0; i<B->rmap->n; i++) { 453853dd7562SDmitry Karpeev PetscInt row; 453953dd7562SDmitry Karpeev row = i; 454053dd7562SDmitry Karpeev if (rowindices) row = rowindices[i]; 454153dd7562SDmitry Karpeev for (j=Baij->i[i]; j<Baij->i[i+1]; j++) { 454253dd7562SDmitry Karpeev PetscInt col; 454353dd7562SDmitry Karpeev col = Baij->j[count]; 454453dd7562SDmitry Karpeev if (colindices) col = colindices[col]; 454553dd7562SDmitry Karpeev v = Baij->a[count]; 454653dd7562SDmitry Karpeev ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr); 454753dd7562SDmitry Karpeev ++count; 454853dd7562SDmitry Karpeev } 454953dd7562SDmitry Karpeev } 455053dd7562SDmitry Karpeev /* FIXME: set C's nonzerostate correctly. */ 455153dd7562SDmitry Karpeev /* Assembly for C is necessary. */ 455253dd7562SDmitry Karpeev C->preallocated = PETSC_TRUE; 455353dd7562SDmitry Karpeev C->assembled = PETSC_TRUE; 455453dd7562SDmitry Karpeev C->was_assembled = PETSC_FALSE; 455553dd7562SDmitry Karpeev PetscFunctionReturn(0); 455653dd7562SDmitry Karpeev } 455753dd7562SDmitry Karpeev 45584099cc6bSBarry Smith PetscFunctionList MatSeqAIJList = NULL; 45594099cc6bSBarry Smith 45604099cc6bSBarry Smith /*@C 45614099cc6bSBarry Smith MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype 45624099cc6bSBarry Smith 45634099cc6bSBarry Smith Collective on Mat 45644099cc6bSBarry Smith 45654099cc6bSBarry Smith Input Parameters: 45664099cc6bSBarry Smith + mat - the matrix object 45674099cc6bSBarry Smith - matype - matrix type 45684099cc6bSBarry Smith 45694099cc6bSBarry Smith Options Database Key: 45704099cc6bSBarry Smith . -mat_seqai_type <method> - for example seqaijcrl 45714099cc6bSBarry Smith 45724099cc6bSBarry Smith 45734099cc6bSBarry Smith Level: intermediate 45744099cc6bSBarry Smith 45754099cc6bSBarry Smith .keywords: Mat, MatType, set, method 45764099cc6bSBarry Smith 45774099cc6bSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat 45784099cc6bSBarry Smith @*/ 45794099cc6bSBarry Smith PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype) 45804099cc6bSBarry Smith { 4581fd9d3c67SJed Brown PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*); 45824099cc6bSBarry Smith PetscBool sametype; 45834099cc6bSBarry Smith 45844099cc6bSBarry Smith PetscFunctionBegin; 45854099cc6bSBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 45864099cc6bSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr); 45874099cc6bSBarry Smith if (sametype) PetscFunctionReturn(0); 45884099cc6bSBarry Smith 45894099cc6bSBarry Smith ierr = PetscFunctionListFind(MatSeqAIJList,matype,&r);CHKERRQ(ierr); 45904099cc6bSBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype); 45914099cc6bSBarry Smith ierr = (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);CHKERRQ(ierr); 45924099cc6bSBarry Smith PetscFunctionReturn(0); 45934099cc6bSBarry Smith } 45944099cc6bSBarry Smith 45954099cc6bSBarry Smith 45964099cc6bSBarry Smith /*@C 45974099cc6bSBarry Smith MatSeqAIJRegister - - Adds a new sub-matrix type for sequential AIJ matrices 45984099cc6bSBarry Smith 45994099cc6bSBarry Smith Not Collective 46004099cc6bSBarry Smith 46014099cc6bSBarry Smith Input Parameters: 46024099cc6bSBarry Smith + name - name of a new user-defined matrix type, for example MATSEQAIJCRL 46034099cc6bSBarry Smith - function - routine to convert to subtype 46044099cc6bSBarry Smith 46054099cc6bSBarry Smith Notes: 46064099cc6bSBarry Smith MatSeqAIJRegister() may be called multiple times to add several user-defined solvers. 46074099cc6bSBarry Smith 46084099cc6bSBarry Smith 46094099cc6bSBarry Smith Then, your matrix can be chosen with the procedural interface at runtime via the option 46104099cc6bSBarry Smith $ -mat_seqaij_type my_mat 46114099cc6bSBarry Smith 46124099cc6bSBarry Smith Level: advanced 46134099cc6bSBarry Smith 46144099cc6bSBarry Smith .keywords: Mat, register 46154099cc6bSBarry Smith 46164099cc6bSBarry Smith .seealso: MatSeqAIJRegisterAll() 46174099cc6bSBarry Smith 46184099cc6bSBarry Smith 46194099cc6bSBarry Smith Level: advanced 46204099cc6bSBarry Smith @*/ 4621388d47a6SSatish Balay PetscErrorCode MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *)) 46224099cc6bSBarry Smith { 46234099cc6bSBarry Smith PetscErrorCode ierr; 46244099cc6bSBarry Smith 46254099cc6bSBarry Smith PetscFunctionBegin; 46264099cc6bSBarry Smith ierr = PetscFunctionListAdd(&MatSeqAIJList,sname,function);CHKERRQ(ierr); 46274099cc6bSBarry Smith PetscFunctionReturn(0); 46284099cc6bSBarry Smith } 46294099cc6bSBarry Smith 46304099cc6bSBarry Smith PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE; 46314099cc6bSBarry Smith 46324099cc6bSBarry Smith /*@C 46334099cc6bSBarry Smith MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ 46344099cc6bSBarry Smith 46354099cc6bSBarry Smith Not Collective 46364099cc6bSBarry Smith 46374099cc6bSBarry Smith Level: advanced 46384099cc6bSBarry Smith 46394099cc6bSBarry Smith Developers Note: CUSP and CUSPARSE do not yet support the MatConvert_SeqAIJ..() paradigm and thus cannot be registered here 46404099cc6bSBarry Smith 46414099cc6bSBarry Smith .keywords: KSP, register, all 46424099cc6bSBarry Smith 46434099cc6bSBarry Smith .seealso: MatRegisterAll(), MatSeqAIJRegister() 46444099cc6bSBarry Smith @*/ 46454099cc6bSBarry Smith PetscErrorCode MatSeqAIJRegisterAll(void) 46464099cc6bSBarry Smith { 46474099cc6bSBarry Smith PetscErrorCode ierr; 46484099cc6bSBarry Smith 46494099cc6bSBarry Smith PetscFunctionBegin; 46504099cc6bSBarry Smith if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(0); 46514099cc6bSBarry Smith MatSeqAIJRegisterAllCalled = PETSC_TRUE; 46524099cc6bSBarry Smith 46534099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr); 46544099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr); 46559779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE) 46566b62b571SRichard Tran Mills ierr = MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr); 4657485f9817SRichard Tran Mills #endif 46584099cc6bSBarry Smith #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA) 46594099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);CHKERRQ(ierr); 46604099cc6bSBarry Smith #endif 46614099cc6bSBarry Smith PetscFunctionReturn(0); 46624099cc6bSBarry Smith } 466353dd7562SDmitry Karpeev 466453dd7562SDmitry Karpeev /* 466581824310SBarry Smith Special version for direct calls from Fortran 466681824310SBarry Smith */ 4667af0996ceSBarry Smith #include <petsc/private/fortranimpl.h> 466881824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 466981824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 467081824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 467181824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 467281824310SBarry Smith #endif 467381824310SBarry Smith 467481824310SBarry Smith /* Change these macros so can be used in void function */ 467581824310SBarry Smith #undef CHKERRQ 4676ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr) 467781824310SBarry Smith #undef SETERRQ2 4678e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 46794994cf47SJed Brown #undef SETERRQ3 46804994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 468181824310SBarry Smith 46828cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr) 468381824310SBarry Smith { 468481824310SBarry Smith Mat A = *AA; 468581824310SBarry Smith PetscInt m = *mm, n = *nn; 468681824310SBarry Smith InsertMode is = *isis; 468781824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 468881824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 468981824310SBarry Smith PetscInt *imax,*ai,*ailen; 469081824310SBarry Smith PetscErrorCode ierr; 469181824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 469254f21887SBarry Smith MatScalar *ap,value,*aa; 4693ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 4694ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 469581824310SBarry Smith 469681824310SBarry Smith PetscFunctionBegin; 46974994cf47SJed Brown MatCheckPreallocated(A,1); 469881824310SBarry Smith imax = a->imax; 469981824310SBarry Smith ai = a->i; 470081824310SBarry Smith ailen = a->ilen; 470181824310SBarry Smith aj = a->j; 470281824310SBarry Smith aa = a->a; 470381824310SBarry Smith 470481824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 470581824310SBarry Smith row = im[k]; 470681824310SBarry Smith if (row < 0) continue; 470781824310SBarry Smith #if defined(PETSC_USE_DEBUG) 4708ce94432eSBarry Smith if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 470981824310SBarry Smith #endif 471081824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 471181824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 471281824310SBarry Smith low = 0; 471381824310SBarry Smith high = nrow; 471481824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 471581824310SBarry Smith if (in[l] < 0) continue; 471681824310SBarry Smith #if defined(PETSC_USE_DEBUG) 4717ce94432eSBarry Smith if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 471881824310SBarry Smith #endif 471981824310SBarry Smith col = in[l]; 47202205254eSKarl Rupp if (roworiented) value = v[l + k*n]; 47212205254eSKarl Rupp else value = v[k + l*m]; 47222205254eSKarl Rupp 472381824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 472481824310SBarry Smith 47252205254eSKarl Rupp if (col <= lastcol) low = 0; 47262205254eSKarl Rupp else high = nrow; 472781824310SBarry Smith lastcol = col; 472881824310SBarry Smith while (high-low > 5) { 472981824310SBarry Smith t = (low+high)/2; 473081824310SBarry Smith if (rp[t] > col) high = t; 473181824310SBarry Smith else low = t; 473281824310SBarry Smith } 473381824310SBarry Smith for (i=low; i<high; i++) { 473481824310SBarry Smith if (rp[i] > col) break; 473581824310SBarry Smith if (rp[i] == col) { 473681824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 473781824310SBarry Smith else ap[i] = value; 473881824310SBarry Smith goto noinsert; 473981824310SBarry Smith } 474081824310SBarry Smith } 474181824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 474281824310SBarry Smith if (nonew == 1) goto noinsert; 4743ce94432eSBarry Smith if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 4744fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 474581824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 474681824310SBarry Smith /* shift up all the later entries in this row */ 474781824310SBarry Smith for (ii=N; ii>=i; ii--) { 474881824310SBarry Smith rp[ii+1] = rp[ii]; 474981824310SBarry Smith ap[ii+1] = ap[ii]; 475081824310SBarry Smith } 475181824310SBarry Smith rp[i] = col; 475281824310SBarry Smith ap[i] = value; 4753e56f5c9eSBarry Smith A->nonzerostate++; 475481824310SBarry Smith noinsert:; 475581824310SBarry Smith low = i + 1; 475681824310SBarry Smith } 475781824310SBarry Smith ailen[row] = nrow; 475881824310SBarry Smith } 475981824310SBarry Smith PetscFunctionReturnVoid(); 476081824310SBarry Smith } 4761