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); 37580bdb30SBarry Smith ierr = PetscArrayzero(norms,n);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; 177*837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 178*837a59e1SRichard Tran Mills PetscBool inserted = PETSC_FALSE; 179*837a59e1SRichard Tran Mills #endif 18079299369SBarry Smith 18179299369SBarry Smith PetscFunctionBegin; 18209f38230SBarry Smith if (Y->assembled) { 1830298fd71SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr); 18409f38230SBarry Smith if (!missing) { 18579299369SBarry Smith diag = aij->diag; 18699e65526SBarry Smith ierr = VecGetArrayRead(D,&v);CHKERRQ(ierr); 18779299369SBarry Smith if (is == INSERT_VALUES) { 188*837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 189*837a59e1SRichard Tran Mills inserted = PETSC_TRUE; 190*837a59e1SRichard Tran Mills #endif 19179299369SBarry Smith for (i=0; i<m; i++) { 19279299369SBarry Smith aa[diag[i]] = v[i]; 19379299369SBarry Smith } 19479299369SBarry Smith } else { 19579299369SBarry Smith for (i=0; i<m; i++) { 196*837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 197*837a59e1SRichard Tran Mills if (v[i] != 0.0) inserted = PETSC_TRUE; 198*837a59e1SRichard Tran Mills #endif 19979299369SBarry Smith aa[diag[i]] += v[i]; 20079299369SBarry Smith } 20179299369SBarry Smith } 202*837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 203*837a59e1SRichard Tran Mills if (inserted) Y->offloadmask = PETSC_OFFLOAD_CPU; 204*837a59e1SRichard Tran Mills #endif 20599e65526SBarry Smith ierr = VecRestoreArrayRead(D,&v);CHKERRQ(ierr); 20679299369SBarry Smith PetscFunctionReturn(0); 20779299369SBarry Smith } 208acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr); 20909f38230SBarry Smith } 21009f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 21109f38230SBarry Smith PetscFunctionReturn(0); 21209f38230SBarry Smith } 21379299369SBarry Smith 2141a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 21517ab2063SBarry Smith { 216416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 217dfbe8321SBarry Smith PetscErrorCode ierr; 21897f1f81fSBarry Smith PetscInt i,ishift; 21917ab2063SBarry Smith 2203a40ed3dSBarry Smith PetscFunctionBegin; 221d0f46423SBarry Smith *m = A->rmap->n; 2223a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 223bfeeae90SHong Zhang ishift = 0; 22453e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 2252462f5fdSStefano Zampini ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr); 226bfeeae90SHong Zhang } else if (oshift == 1) { 2271a83f524SJed Brown PetscInt *tia; 228d0f46423SBarry Smith PetscInt nz = a->i[A->rmap->n]; 2293b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 230854ce69bSBarry Smith ierr = PetscMalloc1(A->rmap->n+1,&tia);CHKERRQ(ierr); 2311a83f524SJed Brown for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1; 2321a83f524SJed Brown *ia = tia; 233ecc77c7aSBarry Smith if (ja) { 2341a83f524SJed Brown PetscInt *tja; 235854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&tja);CHKERRQ(ierr); 2361a83f524SJed Brown for (i=0; i<nz; i++) tja[i] = a->j[i] + 1; 2371a83f524SJed Brown *ja = tja; 238ecc77c7aSBarry Smith } 2396945ee14SBarry Smith } else { 240ecc77c7aSBarry Smith *ia = a->i; 241ecc77c7aSBarry Smith if (ja) *ja = a->j; 242a2ce50c7SBarry Smith } 2433a40ed3dSBarry Smith PetscFunctionReturn(0); 244a2744918SBarry Smith } 245a2744918SBarry Smith 2461a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2476945ee14SBarry Smith { 248dfbe8321SBarry Smith PetscErrorCode ierr; 2496945ee14SBarry Smith 2503a40ed3dSBarry Smith PetscFunctionBegin; 2513a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 252bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 253606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 254ecc77c7aSBarry Smith if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);} 255bcd2baecSBarry Smith } 2563a40ed3dSBarry Smith PetscFunctionReturn(0); 25717ab2063SBarry Smith } 25817ab2063SBarry Smith 2591a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2603b2fbd54SBarry Smith { 2613b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 262dfbe8321SBarry Smith PetscErrorCode ierr; 263d0f46423SBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 26497f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 2653b2fbd54SBarry Smith 2663a40ed3dSBarry Smith PetscFunctionBegin; 267899cda47SBarry Smith *nn = n; 2683a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 2693b2fbd54SBarry Smith if (symmetric) { 2702462f5fdSStefano Zampini ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr); 2713b2fbd54SBarry Smith } else { 272b9e7e5c1SBarry Smith ierr = PetscCalloc1(n,&collengths);CHKERRQ(ierr); 273854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr); 274b9e7e5c1SBarry Smith ierr = PetscMalloc1(nz,&cja);CHKERRQ(ierr); 2753b2fbd54SBarry Smith jj = a->j; 2763b2fbd54SBarry Smith for (i=0; i<nz; i++) { 277bfeeae90SHong Zhang collengths[jj[i]]++; 2783b2fbd54SBarry Smith } 2793b2fbd54SBarry Smith cia[0] = oshift; 2803b2fbd54SBarry Smith for (i=0; i<n; i++) { 2813b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 2823b2fbd54SBarry Smith } 283580bdb30SBarry Smith ierr = PetscArrayzero(collengths,n);CHKERRQ(ierr); 2843b2fbd54SBarry Smith jj = a->j; 285a93ec695SBarry Smith for (row=0; row<m; row++) { 286a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 287a93ec695SBarry Smith for (i=0; i<mr; i++) { 288bfeeae90SHong Zhang col = *jj++; 2892205254eSKarl Rupp 2903b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 2913b2fbd54SBarry Smith } 2923b2fbd54SBarry Smith } 293606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 2943b2fbd54SBarry Smith *ia = cia; *ja = cja; 2953b2fbd54SBarry Smith } 2963a40ed3dSBarry Smith PetscFunctionReturn(0); 2973b2fbd54SBarry Smith } 2983b2fbd54SBarry Smith 2991a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 3003b2fbd54SBarry Smith { 301dfbe8321SBarry Smith PetscErrorCode ierr; 302606d414cSSatish Balay 3033a40ed3dSBarry Smith PetscFunctionBegin; 3043a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 3053b2fbd54SBarry Smith 306606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 307606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 3083a40ed3dSBarry Smith PetscFunctionReturn(0); 3093b2fbd54SBarry Smith } 3103b2fbd54SBarry Smith 3117cee066cSHong Zhang /* 3127cee066cSHong Zhang MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from 3137cee066cSHong Zhang MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output 314040ebd07SHong Zhang spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ() 3157cee066cSHong Zhang */ 3167cee066cSHong 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) 3177cee066cSHong Zhang { 3187cee066cSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3197cee066cSHong Zhang PetscErrorCode ierr; 3207cee066cSHong Zhang PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 321071fcb05SBarry Smith PetscInt nz = a->i[m],row,mr,col,tmp; 3227cee066cSHong Zhang PetscInt *cspidx; 323071fcb05SBarry Smith const PetscInt *jj; 3247cee066cSHong Zhang 3257cee066cSHong Zhang PetscFunctionBegin; 3267cee066cSHong Zhang *nn = n; 3277cee066cSHong Zhang if (!ia) PetscFunctionReturn(0); 328625f6d37SHong Zhang 329b9e7e5c1SBarry Smith ierr = PetscCalloc1(n,&collengths);CHKERRQ(ierr); 330854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr); 331b9e7e5c1SBarry Smith ierr = PetscMalloc1(nz,&cja);CHKERRQ(ierr); 332b9e7e5c1SBarry Smith ierr = PetscMalloc1(nz,&cspidx);CHKERRQ(ierr); 3337cee066cSHong Zhang jj = a->j; 3347cee066cSHong Zhang for (i=0; i<nz; i++) { 3357cee066cSHong Zhang collengths[jj[i]]++; 3367cee066cSHong Zhang } 3377cee066cSHong Zhang cia[0] = oshift; 3387cee066cSHong Zhang for (i=0; i<n; i++) { 3397cee066cSHong Zhang cia[i+1] = cia[i] + collengths[i]; 3407cee066cSHong Zhang } 341580bdb30SBarry Smith ierr = PetscArrayzero(collengths,n);CHKERRQ(ierr); 3427cee066cSHong Zhang jj = a->j; 3437cee066cSHong Zhang for (row=0; row<m; row++) { 3447cee066cSHong Zhang mr = a->i[row+1] - a->i[row]; 3457cee066cSHong Zhang for (i=0; i<mr; i++) { 3467cee066cSHong Zhang col = *jj++; 347071fcb05SBarry Smith tmp = cia[col] + collengths[col]++ - oshift; 348071fcb05SBarry Smith cspidx[tmp] = a->i[row] + i; /* index of a->j */ 349071fcb05SBarry Smith cja[tmp] = row + oshift; 3507cee066cSHong Zhang } 3517cee066cSHong Zhang } 3527cee066cSHong Zhang ierr = PetscFree(collengths);CHKERRQ(ierr); 353071fcb05SBarry Smith *ia = cia; 354071fcb05SBarry Smith *ja = cja; 3557cee066cSHong Zhang *spidx = cspidx; 3567cee066cSHong Zhang PetscFunctionReturn(0); 3577cee066cSHong Zhang } 3587cee066cSHong Zhang 3597cee066cSHong 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) 3607cee066cSHong Zhang { 3617cee066cSHong Zhang PetscErrorCode ierr; 3627cee066cSHong Zhang 3637cee066cSHong Zhang PetscFunctionBegin; 3645243ef75SHong Zhang ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 3657cee066cSHong Zhang ierr = PetscFree(*spidx);CHKERRQ(ierr); 3667cee066cSHong Zhang PetscFunctionReturn(0); 3677cee066cSHong Zhang } 3687cee066cSHong Zhang 36987d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 37087d4246cSBarry Smith { 37187d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 37287d4246cSBarry Smith PetscInt *ai = a->i; 37387d4246cSBarry Smith PetscErrorCode ierr; 37487d4246cSBarry Smith 37587d4246cSBarry Smith PetscFunctionBegin; 376580bdb30SBarry Smith ierr = PetscArraycpy(a->a+ai[row],v,ai[row+1]-ai[row]);CHKERRQ(ierr); 377e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 378c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && ai[row+1]-ai[row]) A->offloadmask = PETSC_OFFLOAD_CPU; 379e2cf4d64SStefano Zampini #endif 38087d4246cSBarry Smith PetscFunctionReturn(0); 38187d4246cSBarry Smith } 38287d4246cSBarry Smith 383bd04181cSBarry Smith /* 384bd04181cSBarry Smith MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions 385bd04181cSBarry Smith 386bd04181cSBarry Smith - a single row of values is set with each call 387bd04181cSBarry Smith - no row or column indices are negative or (in error) larger than the number of rows or columns 388bd04181cSBarry Smith - the values are always added to the matrix, not set 389bd04181cSBarry Smith - no new locations are introduced in the nonzero structure of the matrix 390bd04181cSBarry Smith 3911f763a69SBarry Smith This does NOT assume the global column indices are sorted 392bd04181cSBarry Smith 3931f763a69SBarry Smith */ 394bd04181cSBarry Smith 395af0996ceSBarry Smith #include <petsc/private/isimpl.h> 396189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 397189e4007SBarry Smith { 398189e4007SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3991f763a69SBarry Smith PetscInt low,high,t,row,nrow,i,col,l; 4001f763a69SBarry Smith const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j; 4011f763a69SBarry Smith PetscInt lastcol = -1; 402189e4007SBarry Smith MatScalar *ap,value,*aa = a->a; 403189e4007SBarry Smith const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices; 404189e4007SBarry Smith 405f38dd0b8SBarry Smith row = ridx[im[0]]; 4061f763a69SBarry Smith rp = aj + ai[row]; 4071f763a69SBarry Smith ap = aa + ai[row]; 4081f763a69SBarry Smith nrow = ailen[row]; 409189e4007SBarry Smith low = 0; 410189e4007SBarry Smith high = nrow; 411189e4007SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 412189e4007SBarry Smith col = cidx[in[l]]; 413f38dd0b8SBarry Smith value = v[l]; 414189e4007SBarry Smith 415189e4007SBarry Smith if (col <= lastcol) low = 0; 416189e4007SBarry Smith else high = nrow; 417189e4007SBarry Smith lastcol = col; 418189e4007SBarry Smith while (high-low > 5) { 419189e4007SBarry Smith t = (low+high)/2; 420189e4007SBarry Smith if (rp[t] > col) high = t; 421189e4007SBarry Smith else low = t; 422189e4007SBarry Smith } 423189e4007SBarry Smith for (i=low; i<high; i++) { 424189e4007SBarry Smith if (rp[i] == col) { 4251f763a69SBarry Smith ap[i] += value; 426189e4007SBarry Smith low = i + 1; 4271f763a69SBarry Smith break; 428189e4007SBarry Smith } 429189e4007SBarry Smith } 430189e4007SBarry Smith } 431e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 432c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU; 433e2cf4d64SStefano Zampini #endif 434f38dd0b8SBarry Smith return 0; 435189e4007SBarry Smith } 436189e4007SBarry Smith 43797f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 43817ab2063SBarry Smith { 439416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 440e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 44197f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 4426849ba73SBarry Smith PetscErrorCode ierr; 443e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 444d8cdefa3SHong Zhang MatScalar *ap=NULL,value=0.0,*aa = a->a; 445ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 446ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 447e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 448e2cf4d64SStefano Zampini PetscBool inserted = PETSC_FALSE; 449e2cf4d64SStefano Zampini #endif 45017ab2063SBarry Smith 4513a40ed3dSBarry Smith PetscFunctionBegin; 45217ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 453416022c9SBarry Smith row = im[k]; 4545ef9f2a5SBarry Smith if (row < 0) continue; 4552515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 456e32f2f54SBarry 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); 4573b2fbd54SBarry Smith #endif 458720833daSHong Zhang rp = aj + ai[row]; 459876c6284SHong Zhang if (!A->structure_only) ap = aa + ai[row]; 46017ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 461416022c9SBarry Smith low = 0; 462c71e6ed7SBarry Smith high = nrow; 46317ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 4645ef9f2a5SBarry Smith if (in[l] < 0) continue; 4652515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 466e32f2f54SBarry 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); 4673b2fbd54SBarry Smith #endif 468bfeeae90SHong Zhang col = in[l]; 469071fcb05SBarry Smith if (v && !A->structure_only) value = roworiented ? v[l + k*n] : v[k + l*m]; 470071fcb05SBarry Smith if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue; 47136db0b34SBarry Smith 4722205254eSKarl Rupp if (col <= lastcol) low = 0; 4732205254eSKarl Rupp else high = nrow; 474e2ee6c50SBarry Smith lastcol = col; 475416022c9SBarry Smith while (high-low > 5) { 476416022c9SBarry Smith t = (low+high)/2; 477416022c9SBarry Smith if (rp[t] > col) high = t; 478416022c9SBarry Smith else low = t; 47917ab2063SBarry Smith } 480416022c9SBarry Smith for (i=low; i<high; i++) { 48117ab2063SBarry Smith if (rp[i] > col) break; 48217ab2063SBarry Smith if (rp[i] == col) { 483876c6284SHong Zhang if (!A->structure_only) { 4840c0d7e18SFande Kong if (is == ADD_VALUES) { 4850c0d7e18SFande Kong ap[i] += value; 4860c0d7e18SFande Kong (void)PetscLogFlops(1.0); 4870c0d7e18SFande Kong } 48817ab2063SBarry Smith else ap[i] = value; 489e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 490e2cf4d64SStefano Zampini inserted = PETSC_TRUE; 491e2cf4d64SStefano Zampini #endif 492720833daSHong Zhang } 493e44c0bd4SBarry Smith low = i + 1; 49417ab2063SBarry Smith goto noinsert; 49517ab2063SBarry Smith } 49617ab2063SBarry Smith } 497dcd36c23SBarry Smith if (value == 0.0 && ignorezeroentries && row != col) goto noinsert; 498c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 499e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 500720833daSHong Zhang if (A->structure_only) { 501876c6284SHong Zhang MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar); 502720833daSHong Zhang } else { 503fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 504720833daSHong Zhang } 505c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 506416022c9SBarry Smith /* shift up all the later entries in this row */ 507580bdb30SBarry Smith ierr = PetscArraymove(rp+i+1,rp+i,N-i+1);CHKERRQ(ierr); 50817ab2063SBarry Smith rp[i] = col; 509580bdb30SBarry Smith if (!A->structure_only){ 510580bdb30SBarry Smith ierr = PetscArraymove(ap+i+1,ap+i,N-i+1);CHKERRQ(ierr); 511580bdb30SBarry Smith ap[i] = value; 512580bdb30SBarry Smith } 513416022c9SBarry Smith low = i + 1; 514e56f5c9eSBarry Smith A->nonzerostate++; 515e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 516e2cf4d64SStefano Zampini inserted = PETSC_TRUE; 517e2cf4d64SStefano Zampini #endif 518e44c0bd4SBarry Smith noinsert:; 51917ab2063SBarry Smith } 52017ab2063SBarry Smith ailen[row] = nrow; 52117ab2063SBarry Smith } 522e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 523c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU; 524e2cf4d64SStefano Zampini #endif 5253a40ed3dSBarry Smith PetscFunctionReturn(0); 52617ab2063SBarry Smith } 52717ab2063SBarry Smith 528071fcb05SBarry Smith PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 529071fcb05SBarry Smith { 530071fcb05SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 531071fcb05SBarry Smith PetscInt *rp,k,row; 532071fcb05SBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 533071fcb05SBarry Smith PetscErrorCode ierr; 534071fcb05SBarry Smith PetscInt *aj = a->j; 535071fcb05SBarry Smith MatScalar *aa = a->a,*ap; 536071fcb05SBarry Smith 537071fcb05SBarry Smith PetscFunctionBegin; 538071fcb05SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 539071fcb05SBarry Smith row = im[k]; 540071fcb05SBarry Smith rp = aj + ai[row]; 541071fcb05SBarry Smith ap = aa + ai[row]; 542071fcb05SBarry Smith if (!A->was_assembled) { 543071fcb05SBarry Smith ierr = PetscMemcpy(rp,in,n*sizeof(PetscInt));CHKERRQ(ierr); 544071fcb05SBarry Smith } 545071fcb05SBarry Smith if (!A->structure_only) { 546071fcb05SBarry Smith if (v) { 547071fcb05SBarry Smith ierr = PetscMemcpy(ap,v,n*sizeof(PetscScalar));CHKERRQ(ierr); 548071fcb05SBarry Smith v += n; 549071fcb05SBarry Smith } else { 550071fcb05SBarry Smith ierr = PetscMemzero(ap,n*sizeof(PetscScalar));CHKERRQ(ierr); 551071fcb05SBarry Smith } 552071fcb05SBarry Smith } 553071fcb05SBarry Smith ailen[row] = n; 554071fcb05SBarry Smith a->nz += n; 555071fcb05SBarry Smith } 556e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 557c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU; 558e2cf4d64SStefano Zampini #endif 559071fcb05SBarry Smith PetscFunctionReturn(0); 560071fcb05SBarry Smith } 561071fcb05SBarry Smith 56281824310SBarry Smith 563a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 5647eb43aa7SLois Curfman McInnes { 5657eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 56697f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 56797f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 56854f21887SBarry Smith MatScalar *ap,*aa = a->a; 5697eb43aa7SLois Curfman McInnes 5703a40ed3dSBarry Smith PetscFunctionBegin; 5717eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 5727eb43aa7SLois Curfman McInnes row = im[k]; 573e32f2f54SBarry Smith if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */ 574e32f2f54SBarry 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); 575bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 5767eb43aa7SLois Curfman McInnes nrow = ailen[row]; 5777eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 578e32f2f54SBarry Smith if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */ 579e32f2f54SBarry 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); 580bfeeae90SHong Zhang col = in[l]; 5817eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 5827eb43aa7SLois Curfman McInnes while (high-low > 5) { 5837eb43aa7SLois Curfman McInnes t = (low+high)/2; 5847eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 5857eb43aa7SLois Curfman McInnes else low = t; 5867eb43aa7SLois Curfman McInnes } 5877eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 5887eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 5897eb43aa7SLois Curfman McInnes if (rp[i] == col) { 590b49de8d1SLois Curfman McInnes *v++ = ap[i]; 5917eb43aa7SLois Curfman McInnes goto finished; 5927eb43aa7SLois Curfman McInnes } 5937eb43aa7SLois Curfman McInnes } 59497e567efSBarry Smith *v++ = 0.0; 5957eb43aa7SLois Curfman McInnes finished:; 5967eb43aa7SLois Curfman McInnes } 5977eb43aa7SLois Curfman McInnes } 5983a40ed3dSBarry Smith PetscFunctionReturn(0); 5997eb43aa7SLois Curfman McInnes } 6007eb43aa7SLois Curfman McInnes 60117ab2063SBarry Smith 602dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 60317ab2063SBarry Smith { 604416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 6056849ba73SBarry Smith PetscErrorCode ierr; 6066f69ff64SBarry Smith PetscInt i,*col_lens; 6076f69ff64SBarry Smith int fd; 608b37d52dbSMark F. Adams FILE *file; 60917ab2063SBarry Smith 6103a40ed3dSBarry Smith PetscFunctionBegin; 611b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 612854ce69bSBarry Smith ierr = PetscMalloc1(4+A->rmap->n,&col_lens);CHKERRQ(ierr); 6132205254eSKarl Rupp 6140700a824SBarry Smith col_lens[0] = MAT_FILE_CLASSID; 615d0f46423SBarry Smith col_lens[1] = A->rmap->n; 616d0f46423SBarry Smith col_lens[2] = A->cmap->n; 617416022c9SBarry Smith col_lens[3] = a->nz; 618416022c9SBarry Smith 619416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 620d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 621416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 62217ab2063SBarry Smith } 623d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 624606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 625416022c9SBarry Smith 626416022c9SBarry Smith /* store column indices (zero start index) */ 6276f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 628416022c9SBarry Smith 629416022c9SBarry Smith /* store nonzero values */ 6306f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr); 631b37d52dbSMark F. Adams 632b37d52dbSMark F. Adams ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 633b37d52dbSMark F. Adams if (file) { 63433d57670SJed Brown fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs)); 635b37d52dbSMark F. Adams } 6363a40ed3dSBarry Smith PetscFunctionReturn(0); 63717ab2063SBarry Smith } 638416022c9SBarry Smith 6397dc0baabSHong Zhang static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer) 6407dc0baabSHong Zhang { 6417dc0baabSHong Zhang PetscErrorCode ierr; 6427dc0baabSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 6437dc0baabSHong Zhang PetscInt i,k,m=A->rmap->N; 6447dc0baabSHong Zhang 6457dc0baabSHong Zhang PetscFunctionBegin; 6467dc0baabSHong Zhang ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 6477dc0baabSHong Zhang for (i=0; i<m; i++) { 6487dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 6497dc0baabSHong Zhang for (k=a->i[i]; k<a->i[i+1]; k++) { 6507dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);CHKERRQ(ierr); 6517dc0baabSHong Zhang } 6527dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 6537dc0baabSHong Zhang } 6547dc0baabSHong Zhang ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 6557dc0baabSHong Zhang PetscFunctionReturn(0); 6567dc0baabSHong Zhang } 6577dc0baabSHong Zhang 65809573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 659cd155464SBarry Smith 660dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 661416022c9SBarry Smith { 662416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 663dfbe8321SBarry Smith PetscErrorCode ierr; 66460e0710aSBarry Smith PetscInt i,j,m = A->rmap->n; 665e060cb09SBarry Smith const char *name; 666f3ef73ceSBarry Smith PetscViewerFormat format; 66717ab2063SBarry Smith 6683a40ed3dSBarry Smith PetscFunctionBegin; 6697dc0baabSHong Zhang if (A->structure_only) { 6707dc0baabSHong Zhang ierr = MatView_SeqAIJ_ASCII_structonly(A,viewer);CHKERRQ(ierr); 6717dc0baabSHong Zhang PetscFunctionReturn(0); 6727dc0baabSHong Zhang } 67343e49210SHong Zhang 674b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 67571c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 67697f1f81fSBarry Smith PetscInt nofinalvalue = 0; 67760e0710aSBarry Smith if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) { 678c337ccceSJed Brown /* Need a dummy value to ensure the dimension of the matrix. */ 679d00d2cf4SBarry Smith nofinalvalue = 1; 680d00d2cf4SBarry Smith } 681d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 682d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr); 68377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 684fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX) 685fbfe6fa7SJed Brown ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 686fbfe6fa7SJed Brown #else 68777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 688fbfe6fa7SJed Brown #endif 689b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 69017ab2063SBarry Smith 69117ab2063SBarry Smith for (i=0; i<m; i++) { 69260e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 693aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 694a9bf72d8SJed 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); 69517ab2063SBarry Smith #else 69660e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr); 69717ab2063SBarry Smith #endif 69817ab2063SBarry Smith } 69917ab2063SBarry Smith } 700d00d2cf4SBarry Smith if (nofinalvalue) { 701c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX) 702c337ccceSJed Brown ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr); 703c337ccceSJed Brown #else 704d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr); 705c337ccceSJed Brown #endif 706d00d2cf4SBarry Smith } 707317d6ea6SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 708fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 709d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 7102950ac48SStefano Zampini } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 711cd155464SBarry Smith PetscFunctionReturn(0); 712fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 713d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 71444cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 71577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 71660e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 717aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 71836db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 71960e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 72036db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 72160e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 72236db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 72360e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 7246831982aSBarry Smith } 72544cd7ae7SLois Curfman McInnes #else 72660e0710aSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);} 72744cd7ae7SLois Curfman McInnes #endif 72844cd7ae7SLois Curfman McInnes } 729b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 73044cd7ae7SLois Curfman McInnes } 731d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 732fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 73397f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 734d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 735854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr); 736496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 737496be53dSLois Curfman McInnes sptr[i] = nzd+1; 73860e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 739496be53dSLois Curfman McInnes if (a->j[j] >= i) { 740aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 74136db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 742496be53dSLois Curfman McInnes #else 743496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 744496be53dSLois Curfman McInnes #endif 745496be53dSLois Curfman McInnes } 746496be53dSLois Curfman McInnes } 747496be53dSLois Curfman McInnes } 7482e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 74977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 7502e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 7512205254eSKarl Rupp if (i+4<m) { 7522205254eSKarl 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); 7532205254eSKarl Rupp } else if (i+3<m) { 7542205254eSKarl 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); 7552205254eSKarl Rupp } else if (i+2<m) { 7562205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr); 7572205254eSKarl Rupp } else if (i+1<m) { 7582205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr); 7592205254eSKarl Rupp } else if (i<m) { 7602205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr); 7612205254eSKarl Rupp } else { 7622205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr); 7632205254eSKarl Rupp } 764496be53dSLois Curfman McInnes } 765b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 766606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 767496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 76860e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 76977431f27SBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);} 770496be53dSLois Curfman McInnes } 771b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 772496be53dSLois Curfman McInnes } 773b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 774496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 77560e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 776496be53dSLois Curfman McInnes if (a->j[j] >= i) { 777aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 77836db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 77960e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 7806831982aSBarry Smith } 781496be53dSLois Curfman McInnes #else 78260e0710aSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);} 783496be53dSLois Curfman McInnes #endif 784496be53dSLois Curfman McInnes } 785496be53dSLois Curfman McInnes } 786b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 787496be53dSLois Curfman McInnes } 788d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 789fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 79097f1f81fSBarry Smith PetscInt cnt = 0,jcnt; 79187828ca2SBarry Smith PetscScalar value; 79268f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX) 79368f1ed48SBarry Smith PetscBool realonly = PETSC_TRUE; 79468f1ed48SBarry Smith 79568f1ed48SBarry Smith for (i=0; i<a->i[m]; i++) { 79668f1ed48SBarry Smith if (PetscImaginaryPart(a->a[i]) != 0.0) { 79768f1ed48SBarry Smith realonly = PETSC_FALSE; 79868f1ed48SBarry Smith break; 79968f1ed48SBarry Smith } 80068f1ed48SBarry Smith } 80168f1ed48SBarry Smith #endif 80202594712SBarry Smith 803d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 80402594712SBarry Smith for (i=0; i<m; i++) { 80502594712SBarry Smith jcnt = 0; 806d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 807e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 80802594712SBarry Smith value = a->a[cnt++]; 809e24b481bSBarry Smith jcnt++; 81002594712SBarry Smith } else { 81102594712SBarry Smith value = 0.0; 81202594712SBarry Smith } 813aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 81468f1ed48SBarry Smith if (realonly) { 81560e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr); 81668f1ed48SBarry Smith } else { 81760e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr); 81868f1ed48SBarry Smith } 81902594712SBarry Smith #else 82060e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr); 82102594712SBarry Smith #endif 82202594712SBarry Smith } 823b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 82402594712SBarry Smith } 825d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 8263c215bfdSMatthew Knepley } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) { 827150b93efSMatthew G. Knepley PetscInt fshift=1; 828d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 8293c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 83019303e72SJonathan Guyer ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr); 8313c215bfdSMatthew Knepley #else 83219303e72SJonathan Guyer ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr); 8333c215bfdSMatthew Knepley #endif 834d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr); 8353c215bfdSMatthew Knepley for (i=0; i<m; i++) { 83660e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 8373c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 838a9a0e077SKarl 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); 8393c215bfdSMatthew Knepley #else 840150b93efSMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr); 8413c215bfdSMatthew Knepley #endif 8423c215bfdSMatthew Knepley } 8433c215bfdSMatthew Knepley } 844d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 8453a40ed3dSBarry Smith } else { 846d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 847d5f3da31SBarry Smith if (A->factortype) { 84816cd7e1dSShri Abhyankar for (i=0; i<m; i++) { 84916cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 85016cd7e1dSShri Abhyankar /* L part */ 85160e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 85216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 85316cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 85460e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 85516cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 8566712e2f1SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr); 85716cd7e1dSShri Abhyankar } else { 85860e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 85916cd7e1dSShri Abhyankar } 86016cd7e1dSShri Abhyankar #else 86160e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 86216cd7e1dSShri Abhyankar #endif 86316cd7e1dSShri Abhyankar } 86416cd7e1dSShri Abhyankar /* diagonal */ 86516cd7e1dSShri Abhyankar j = a->diag[i]; 86616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 86716cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 86860e0710aSBarry 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); 86916cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 8706712e2f1SBarry 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); 87116cd7e1dSShri Abhyankar } else { 87260e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr); 87316cd7e1dSShri Abhyankar } 87416cd7e1dSShri Abhyankar #else 87560e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr); 87616cd7e1dSShri Abhyankar #endif 87716cd7e1dSShri Abhyankar 87816cd7e1dSShri Abhyankar /* U part */ 87960e0710aSBarry Smith for (j=a->diag[i+1]+1; j<a->diag[i]; j++) { 88016cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 88116cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 88260e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 88316cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 88422ab088eSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr); 88516cd7e1dSShri Abhyankar } else { 88660e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 88716cd7e1dSShri Abhyankar } 88816cd7e1dSShri Abhyankar #else 88960e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 89016cd7e1dSShri Abhyankar #endif 89116cd7e1dSShri Abhyankar } 89216cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 89316cd7e1dSShri Abhyankar } 89416cd7e1dSShri Abhyankar } else { 89517ab2063SBarry Smith for (i=0; i<m; i++) { 89677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 89760e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 898aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 89936db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 90060e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 90136db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 90260e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 9033a40ed3dSBarry Smith } else { 90460e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 90517ab2063SBarry Smith } 90617ab2063SBarry Smith #else 90760e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 90817ab2063SBarry Smith #endif 90917ab2063SBarry Smith } 910b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 91117ab2063SBarry Smith } 91216cd7e1dSShri Abhyankar } 913d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 91417ab2063SBarry Smith } 915b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 9163a40ed3dSBarry Smith PetscFunctionReturn(0); 917416022c9SBarry Smith } 918416022c9SBarry Smith 9199804daf3SBarry Smith #include <petscdraw.h> 920dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 921416022c9SBarry Smith { 922480ef9eaSBarry Smith Mat A = (Mat) Aa; 923416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 924dfbe8321SBarry Smith PetscErrorCode ierr; 925383922c3SLisandro Dalcin PetscInt i,j,m = A->rmap->n; 926383922c3SLisandro Dalcin int color; 927b05fc000SLisandro Dalcin PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r; 928b0a32e0cSBarry Smith PetscViewer viewer; 929f3ef73ceSBarry Smith PetscViewerFormat format; 930cddf8d76SBarry Smith 9313a40ed3dSBarry Smith PetscFunctionBegin; 932480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 933b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 934b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 935383922c3SLisandro Dalcin 936416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 9370513a670SBarry Smith 938fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 939383922c3SLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 9400513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 941b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 942416022c9SBarry Smith for (i=0; i<m; i++) { 943cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 944bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 945bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 94636db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 947b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 948cddf8d76SBarry Smith } 949cddf8d76SBarry Smith } 950b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 951cddf8d76SBarry Smith for (i=0; i<m; i++) { 952cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 953bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 954bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 955cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 956b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 957cddf8d76SBarry Smith } 958cddf8d76SBarry Smith } 959b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 960cddf8d76SBarry Smith for (i=0; i<m; i++) { 961cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 962bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 963bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 96436db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 965b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 966416022c9SBarry Smith } 967416022c9SBarry Smith } 968383922c3SLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 9690513a670SBarry Smith } else { 9700513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 9710513a670SBarry Smith /* first determine max of all nonzero values */ 972b05fc000SLisandro Dalcin PetscReal minv = 0.0, maxv = 0.0; 973383922c3SLisandro Dalcin PetscInt nz = a->nz, count = 0; 974b0a32e0cSBarry Smith PetscDraw popup; 9750513a670SBarry Smith 9760513a670SBarry Smith for (i=0; i<nz; i++) { 9770513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 9780513a670SBarry Smith } 979383922c3SLisandro Dalcin if (minv >= maxv) maxv = minv + PETSC_SMALL; 980b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 98145f3bb6eSLisandro Dalcin ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr); 982383922c3SLisandro Dalcin 983383922c3SLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 9840513a670SBarry Smith for (i=0; i<m; i++) { 985383922c3SLisandro Dalcin y_l = m - i - 1.0; 986383922c3SLisandro Dalcin y_r = y_l + 1.0; 987bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 988383922c3SLisandro Dalcin x_l = a->j[j]; 989383922c3SLisandro Dalcin x_r = x_l + 1.0; 990b05fc000SLisandro Dalcin color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv); 991b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 9920513a670SBarry Smith count++; 9930513a670SBarry Smith } 9940513a670SBarry Smith } 995383922c3SLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 9960513a670SBarry Smith } 997480ef9eaSBarry Smith PetscFunctionReturn(0); 998480ef9eaSBarry Smith } 999cddf8d76SBarry Smith 10009804daf3SBarry Smith #include <petscdraw.h> 1001dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 1002480ef9eaSBarry Smith { 1003dfbe8321SBarry Smith PetscErrorCode ierr; 1004b0a32e0cSBarry Smith PetscDraw draw; 100536db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 1006ace3abfcSBarry Smith PetscBool isnull; 1007480ef9eaSBarry Smith 1008480ef9eaSBarry Smith PetscFunctionBegin; 1009b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1010b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 1011480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 1012480ef9eaSBarry Smith 1013d0f46423SBarry Smith xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0; 1014480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 1015b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 1016832b7cebSLisandro Dalcin ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 1017b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 10180298fd71SBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr); 1019832b7cebSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 10203a40ed3dSBarry Smith PetscFunctionReturn(0); 1021416022c9SBarry Smith } 1022416022c9SBarry Smith 1023dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 1024416022c9SBarry Smith { 1025dfbe8321SBarry Smith PetscErrorCode ierr; 1026ace3abfcSBarry Smith PetscBool iascii,isbinary,isdraw; 1027416022c9SBarry Smith 10283a40ed3dSBarry Smith PetscFunctionBegin; 1029251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1030251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1031251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1032c45a1595SBarry Smith if (iascii) { 10333a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 10340f5bd95cSBarry Smith } else if (isbinary) { 10353a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 10360f5bd95cSBarry Smith } else if (isdraw) { 10373a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 103811aeaf0aSBarry Smith } 10394108e4d5SBarry Smith ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr); 10403a40ed3dSBarry Smith PetscFunctionReturn(0); 104117ab2063SBarry Smith } 104219bcc07fSBarry Smith 1043dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 104417ab2063SBarry Smith { 1045416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 10466849ba73SBarry Smith PetscErrorCode ierr; 1047580bdb30SBarry Smith PetscInt fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax; 1048d0f46423SBarry Smith PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0; 104954f21887SBarry Smith MatScalar *aa = a->a,*ap; 10503447b6efSHong Zhang PetscReal ratio = 0.6; 105117ab2063SBarry Smith 10523a40ed3dSBarry Smith PetscFunctionBegin; 10533a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 1054071fcb05SBarry Smith ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1055071fcb05SBarry Smith if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) PetscFunctionReturn(0); 105617ab2063SBarry Smith 105743ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 105817ab2063SBarry Smith for (i=1; i<m; i++) { 1059416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 106017ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 106194a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 106217ab2063SBarry Smith if (fshift) { 1063bfeeae90SHong Zhang ip = aj + ai[i]; 1064bfeeae90SHong Zhang ap = aa + ai[i]; 106517ab2063SBarry Smith N = ailen[i]; 1066580bdb30SBarry Smith ierr = PetscArraymove(ip-fshift,ip,N);CHKERRQ(ierr); 1067580bdb30SBarry Smith if (!A->structure_only) { 1068580bdb30SBarry Smith ierr = PetscArraymove(ap-fshift,ap,N);CHKERRQ(ierr); 106917ab2063SBarry Smith } 107017ab2063SBarry Smith } 107117ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 107217ab2063SBarry Smith } 107317ab2063SBarry Smith if (m) { 107417ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 107517ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 107617ab2063SBarry Smith } 10777b083b7cSBarry Smith 107817ab2063SBarry Smith /* reset ilen and imax for each row */ 10797b083b7cSBarry Smith a->nonzerorowcnt = 0; 1080396832f4SHong Zhang if (A->structure_only) { 1081071fcb05SBarry Smith ierr = PetscFree(a->imax);CHKERRQ(ierr); 1082071fcb05SBarry Smith ierr = PetscFree(a->ilen);CHKERRQ(ierr); 1083396832f4SHong Zhang } else { /* !A->structure_only */ 108417ab2063SBarry Smith for (i=0; i<m; i++) { 108517ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 10867b083b7cSBarry Smith a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0); 108717ab2063SBarry Smith } 1088396832f4SHong Zhang } 1089bfeeae90SHong Zhang a->nz = ai[m]; 109065e19b50SBarry 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); 109117ab2063SBarry Smith 109209f38230SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1093d0f46423SBarry 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); 1094ae15b995SBarry Smith ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr); 1095ae15b995SBarry Smith ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr); 10962205254eSKarl Rupp 10978e58a170SBarry Smith A->info.mallocs += a->reallocs; 1098dd5f02e7SSatish Balay a->reallocs = 0; 10996712e2f1SBarry Smith A->info.nz_unneeded = (PetscReal)fshift; 110036db0b34SBarry Smith a->rmax = rmax; 11014e220ebcSLois Curfman McInnes 1102396832f4SHong Zhang if (!A->structure_only) { 110311e456e1SBarry Smith ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 1104396832f4SHong Zhang } 11054108e4d5SBarry Smith ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr); 11063a40ed3dSBarry Smith PetscFunctionReturn(0); 110717ab2063SBarry Smith } 110817ab2063SBarry Smith 110999cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 111099cafbc1SBarry Smith { 111199cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 111299cafbc1SBarry Smith PetscInt i,nz = a->nz; 111354f21887SBarry Smith MatScalar *aa = a->a; 1114acf2f550SJed Brown PetscErrorCode ierr; 111599cafbc1SBarry Smith 111699cafbc1SBarry Smith PetscFunctionBegin; 111799cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 1118acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1119e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1120c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 1121e2cf4d64SStefano Zampini #endif 112299cafbc1SBarry Smith PetscFunctionReturn(0); 112399cafbc1SBarry Smith } 112499cafbc1SBarry Smith 112599cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 112699cafbc1SBarry Smith { 112799cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 112899cafbc1SBarry Smith PetscInt i,nz = a->nz; 112954f21887SBarry Smith MatScalar *aa = a->a; 1130acf2f550SJed Brown PetscErrorCode ierr; 113199cafbc1SBarry Smith 113299cafbc1SBarry Smith PetscFunctionBegin; 113399cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 1134acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1135e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1136c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 1137e2cf4d64SStefano Zampini #endif 113899cafbc1SBarry Smith PetscFunctionReturn(0); 113999cafbc1SBarry Smith } 114099cafbc1SBarry Smith 1141dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 114217ab2063SBarry Smith { 1143416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1144dfbe8321SBarry Smith PetscErrorCode ierr; 11453a40ed3dSBarry Smith 11463a40ed3dSBarry Smith PetscFunctionBegin; 1147580bdb30SBarry Smith ierr = PetscArrayzero(a->a,a->i[A->rmap->n]);CHKERRQ(ierr); 1148acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1149e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1150c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 1151e2cf4d64SStefano Zampini #endif 11523a40ed3dSBarry Smith PetscFunctionReturn(0); 115317ab2063SBarry Smith } 1154416022c9SBarry Smith 1155dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 115617ab2063SBarry Smith { 1157416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1158dfbe8321SBarry Smith PetscErrorCode ierr; 1159d5d45c9bSBarry Smith 11603a40ed3dSBarry Smith PetscFunctionBegin; 1161aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1162d0f46423SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz); 116317ab2063SBarry Smith #endif 1164e6b907acSBarry Smith ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr); 11656bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 11666bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 116705b42c5fSBarry Smith ierr = PetscFree(a->diag);CHKERRQ(ierr); 1168d48dcb14SBarry Smith ierr = PetscFree(a->ibdiag);CHKERRQ(ierr); 1169071fcb05SBarry Smith ierr = PetscFree(a->imax);CHKERRQ(ierr); 1170071fcb05SBarry Smith ierr = PetscFree(a->ilen);CHKERRQ(ierr); 1171846b4da1SFande Kong ierr = PetscFree(a->ipre);CHKERRQ(ierr); 117271f1c65dSBarry Smith ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr); 117305b42c5fSBarry Smith ierr = PetscFree(a->solve_work);CHKERRQ(ierr); 11746bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 117505b42c5fSBarry Smith ierr = PetscFree(a->saved_values);CHKERRQ(ierr); 11766bf464f9SBarry Smith ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr); 1177cd6b891eSBarry Smith ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr); 11780b7e3e3dSHong Zhang ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr); 1179a30b2313SHong Zhang 11804108e4d5SBarry Smith ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr); 1181bf0cc555SLisandro Dalcin ierr = PetscFree(A->data);CHKERRQ(ierr); 1182901853e0SKris Buschelman 1183dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr); 1184bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr); 1185bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr); 1186bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr); 1187bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr); 1188bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr); 1189bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr); 1190af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 1191af8000cdSHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr); 1192af8000cdSHong Zhang #endif 119363c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE) 119463c07aadSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);CHKERRQ(ierr); 11953dad0653Sstefano_zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);CHKERRQ(ierr); 119663c07aadSStefano Zampini #endif 1197b49cda9fSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr); 1198c9225affSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);CHKERRQ(ierr); 1199c9225affSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);CHKERRQ(ierr); 1200bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr); 1201bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr); 1202846b4da1SFande Kong ierr = PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);CHKERRQ(ierr); 1203bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr); 1204bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr); 120575d48cdbSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);CHKERRQ(ierr); 12063a40ed3dSBarry Smith PetscFunctionReturn(0); 120717ab2063SBarry Smith } 120817ab2063SBarry Smith 1209ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg) 121017ab2063SBarry Smith { 1211416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12124846f1f5SKris Buschelman PetscErrorCode ierr; 12133a40ed3dSBarry Smith 12143a40ed3dSBarry Smith PetscFunctionBegin; 1215a65d3064SKris Buschelman switch (op) { 1216a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 12174e0d8c25SBarry Smith a->roworiented = flg; 1218a65d3064SKris Buschelman break; 1219a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 1220a9817697SBarry Smith a->keepnonzeropattern = flg; 1221a65d3064SKris Buschelman break; 1222512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 1223512a5fc5SBarry Smith a->nonew = (flg ? 0 : 1); 1224a65d3064SKris Buschelman break; 1225a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 12264e0d8c25SBarry Smith a->nonew = (flg ? -1 : 0); 1227a65d3064SKris Buschelman break; 1228a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 12294e0d8c25SBarry Smith a->nonew = (flg ? -2 : 0); 1230a65d3064SKris Buschelman break; 123128b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 123228b2fa4aSMatthew Knepley a->nounused = (flg ? -1 : 0); 123328b2fa4aSMatthew Knepley break; 1234a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 12354e0d8c25SBarry Smith a->ignorezeroentries = flg; 12360df259c2SBarry Smith break; 12373d472b54SHong Zhang case MAT_SPD: 1238b1646e73SJed Brown case MAT_SYMMETRIC: 1239b1646e73SJed Brown case MAT_STRUCTURALLY_SYMMETRIC: 1240b1646e73SJed Brown case MAT_HERMITIAN: 1241b1646e73SJed Brown case MAT_SYMMETRY_ETERNAL: 1242957cac9fSHong Zhang case MAT_STRUCTURE_ONLY: 12435021d80fSJed Brown /* These options are handled directly by MatSetOption() */ 12445021d80fSJed Brown break; 12454e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1246a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 1247a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 1248290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 1249a65d3064SKris Buschelman break; 1250b87ac2d8SJed Brown case MAT_USE_INODES: 1251b87ac2d8SJed Brown /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */ 1252b87ac2d8SJed Brown break; 1253c10200c1SHong Zhang case MAT_SUBMAT_SINGLEIS: 1254c10200c1SHong Zhang A->submat_singleis = flg; 1255c10200c1SHong Zhang break; 1256071fcb05SBarry Smith case MAT_SORTED_FULL: 1257071fcb05SBarry Smith if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull; 1258071fcb05SBarry Smith else A->ops->setvalues = MatSetValues_SeqAIJ; 1259071fcb05SBarry Smith break; 1260a65d3064SKris Buschelman default: 1261e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 1262a65d3064SKris Buschelman } 12634108e4d5SBarry Smith ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr); 12643a40ed3dSBarry Smith PetscFunctionReturn(0); 126517ab2063SBarry Smith } 126617ab2063SBarry Smith 1267dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 126817ab2063SBarry Smith { 1269416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12706849ba73SBarry Smith PetscErrorCode ierr; 1271fdc842d1SBarry Smith PetscInt i,j,n,*ai=a->i,*aj=a->j; 1272fdc842d1SBarry Smith PetscScalar *aa=a->a,*x; 127317ab2063SBarry Smith 12743a40ed3dSBarry Smith PetscFunctionBegin; 1275d3e70bfaSHong Zhang ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 1276e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 127735e7444dSHong Zhang 1278d5f3da31SBarry Smith if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) { 1279d3e70bfaSHong Zhang PetscInt *diag=a->diag; 1280fdc842d1SBarry Smith ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr); 12812c990fa1SHong Zhang for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]]; 1282fdc842d1SBarry Smith ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr); 128335e7444dSHong Zhang PetscFunctionReturn(0); 128435e7444dSHong Zhang } 128535e7444dSHong Zhang 1286fdc842d1SBarry Smith ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr); 128735e7444dSHong Zhang for (i=0; i<n; i++) { 1288fdc842d1SBarry Smith x[i] = 0.0; 128935e7444dSHong Zhang for (j=ai[i]; j<ai[i+1]; j++) { 129035e7444dSHong Zhang if (aj[j] == i) { 129135e7444dSHong Zhang x[i] = aa[j]; 129217ab2063SBarry Smith break; 129317ab2063SBarry Smith } 129417ab2063SBarry Smith } 129517ab2063SBarry Smith } 1296fdc842d1SBarry Smith ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr); 12973a40ed3dSBarry Smith PetscFunctionReturn(0); 129817ab2063SBarry Smith } 129917ab2063SBarry Smith 1300c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 1301dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 130217ab2063SBarry Smith { 1303416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1304d9ca1df4SBarry Smith PetscScalar *y; 1305d9ca1df4SBarry Smith const PetscScalar *x; 1306dfbe8321SBarry Smith PetscErrorCode ierr; 1307d0f46423SBarry Smith PetscInt m = A->rmap->n; 13085c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1309d9ca1df4SBarry Smith const MatScalar *v; 1310a77337e4SBarry Smith PetscScalar alpha; 1311d9ca1df4SBarry Smith PetscInt n,i,j; 1312d9ca1df4SBarry Smith const PetscInt *idx,*ii,*ridx=NULL; 13133447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 1314ace3abfcSBarry Smith PetscBool usecprow = cprow.use; 13155c897100SBarry Smith #endif 131617ab2063SBarry Smith 13173a40ed3dSBarry Smith PetscFunctionBegin; 13182e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 1319d9ca1df4SBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 13201ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 13215c897100SBarry Smith 13225c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1323bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 13245c897100SBarry Smith #else 13253447b6efSHong Zhang if (usecprow) { 13263447b6efSHong Zhang m = cprow.nrows; 13273447b6efSHong Zhang ii = cprow.i; 13287b2bb3b9SHong Zhang ridx = cprow.rindex; 13293447b6efSHong Zhang } else { 13303447b6efSHong Zhang ii = a->i; 13313447b6efSHong Zhang } 133217ab2063SBarry Smith for (i=0; i<m; i++) { 13333447b6efSHong Zhang idx = a->j + ii[i]; 13343447b6efSHong Zhang v = a->a + ii[i]; 13353447b6efSHong Zhang n = ii[i+1] - ii[i]; 13363447b6efSHong Zhang if (usecprow) { 13377b2bb3b9SHong Zhang alpha = x[ridx[i]]; 13383447b6efSHong Zhang } else { 133917ab2063SBarry Smith alpha = x[i]; 13403447b6efSHong Zhang } 134104fbf559SBarry Smith for (j=0; j<n; j++) y[idx[j]] += alpha*v[j]; 134217ab2063SBarry Smith } 13435c897100SBarry Smith #endif 1344dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1345d9ca1df4SBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 13461ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 13473a40ed3dSBarry Smith PetscFunctionReturn(0); 134817ab2063SBarry Smith } 134917ab2063SBarry Smith 1350dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 13515c897100SBarry Smith { 1352dfbe8321SBarry Smith PetscErrorCode ierr; 13535c897100SBarry Smith 13545c897100SBarry Smith PetscFunctionBegin; 1355170fe5c8SBarry Smith ierr = VecSet(yy,0.0);CHKERRQ(ierr); 13565c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 13575c897100SBarry Smith PetscFunctionReturn(0); 13585c897100SBarry Smith } 13595c897100SBarry Smith 1360c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 136178b84d54SShri Abhyankar 1362dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 136317ab2063SBarry Smith { 1364416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1365d9fead3dSBarry Smith PetscScalar *y; 136654f21887SBarry Smith const PetscScalar *x; 136754f21887SBarry Smith const MatScalar *aa; 1368dfbe8321SBarry Smith PetscErrorCode ierr; 1369003131ecSBarry Smith PetscInt m=A->rmap->n; 13700298fd71SBarry Smith const PetscInt *aj,*ii,*ridx=NULL; 13717b083b7cSBarry Smith PetscInt n,i; 1372362ced78SSatish Balay PetscScalar sum; 1373ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 137417ab2063SBarry Smith 1375b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 137697952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 1377fee21e36SBarry Smith #endif 1378fee21e36SBarry Smith 13793a40ed3dSBarry Smith PetscFunctionBegin; 13803649974fSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 13811ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 1382416022c9SBarry Smith ii = a->i; 13834eb6d288SHong Zhang if (usecprow) { /* use compressed row format */ 1384580bdb30SBarry Smith ierr = PetscArrayzero(y,m);CHKERRQ(ierr); 138597952fefSHong Zhang m = a->compressedrow.nrows; 138697952fefSHong Zhang ii = a->compressedrow.i; 138797952fefSHong Zhang ridx = a->compressedrow.rindex; 138897952fefSHong Zhang for (i=0; i<m; i++) { 138997952fefSHong Zhang n = ii[i+1] - ii[i]; 139097952fefSHong Zhang aj = a->j + ii[i]; 139197952fefSHong Zhang aa = a->a + ii[i]; 139297952fefSHong Zhang sum = 0.0; 1393003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 1394003131ecSBarry Smith /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 139597952fefSHong Zhang y[*ridx++] = sum; 139697952fefSHong Zhang } 139797952fefSHong Zhang } else { /* do not use compressed row format */ 1398b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 13993d3eaba7SBarry Smith aj = a->j; 14003d3eaba7SBarry Smith aa = a->a; 1401b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 1402b05257ddSBarry Smith #else 140317ab2063SBarry Smith for (i=0; i<m; i++) { 1404003131ecSBarry Smith n = ii[i+1] - ii[i]; 1405003131ecSBarry Smith aj = a->j + ii[i]; 1406003131ecSBarry Smith aa = a->a + ii[i]; 140717ab2063SBarry Smith sum = 0.0; 1408003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 140917ab2063SBarry Smith y[i] = sum; 141017ab2063SBarry Smith } 14118d195f9aSBarry Smith #endif 1412b05257ddSBarry Smith } 14137b083b7cSBarry Smith ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 14143649974fSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 14151ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 14163a40ed3dSBarry Smith PetscFunctionReturn(0); 141717ab2063SBarry Smith } 141817ab2063SBarry Smith 1419b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy) 1420b434eb95SMatthew G. Knepley { 1421b434eb95SMatthew G. Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1422b434eb95SMatthew G. Knepley PetscScalar *y; 1423b434eb95SMatthew G. Knepley const PetscScalar *x; 1424b434eb95SMatthew G. Knepley const MatScalar *aa; 1425b434eb95SMatthew G. Knepley PetscErrorCode ierr; 1426b434eb95SMatthew G. Knepley PetscInt m=A->rmap->n; 1427b434eb95SMatthew G. Knepley const PetscInt *aj,*ii,*ridx=NULL; 1428b434eb95SMatthew G. Knepley PetscInt n,i,nonzerorow=0; 1429b434eb95SMatthew G. Knepley PetscScalar sum; 1430b434eb95SMatthew G. Knepley PetscBool usecprow=a->compressedrow.use; 1431b434eb95SMatthew G. Knepley 1432b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 1433b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa) 1434b434eb95SMatthew G. Knepley #endif 1435b434eb95SMatthew G. Knepley 1436b434eb95SMatthew G. Knepley PetscFunctionBegin; 1437b434eb95SMatthew G. Knepley ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1438b434eb95SMatthew G. Knepley ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 1439b434eb95SMatthew G. Knepley if (usecprow) { /* use compressed row format */ 1440b434eb95SMatthew G. Knepley m = a->compressedrow.nrows; 1441b434eb95SMatthew G. Knepley ii = a->compressedrow.i; 1442b434eb95SMatthew G. Knepley ridx = a->compressedrow.rindex; 1443b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1444b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1445b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1446b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1447b434eb95SMatthew G. Knepley sum = 0.0; 1448b434eb95SMatthew G. Knepley nonzerorow += (n>0); 1449b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1450b434eb95SMatthew G. Knepley /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 1451b434eb95SMatthew G. Knepley y[*ridx++] = sum; 1452b434eb95SMatthew G. Knepley } 1453b434eb95SMatthew G. Knepley } else { /* do not use compressed row format */ 14543d3eaba7SBarry Smith ii = a->i; 1455b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1456b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1457b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1458b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1459b434eb95SMatthew G. Knepley sum = 0.0; 1460b434eb95SMatthew G. Knepley nonzerorow += (n>0); 1461b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1462b434eb95SMatthew G. Knepley y[i] = sum; 1463b434eb95SMatthew G. Knepley } 1464b434eb95SMatthew G. Knepley } 1465b434eb95SMatthew G. Knepley ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr); 1466b434eb95SMatthew G. Knepley ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1467b434eb95SMatthew G. Knepley ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 1468b434eb95SMatthew G. Knepley PetscFunctionReturn(0); 1469b434eb95SMatthew G. Knepley } 1470b434eb95SMatthew G. Knepley 1471b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1472b434eb95SMatthew G. Knepley { 1473b434eb95SMatthew G. Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1474b434eb95SMatthew G. Knepley PetscScalar *y,*z; 1475b434eb95SMatthew G. Knepley const PetscScalar *x; 1476b434eb95SMatthew G. Knepley const MatScalar *aa; 1477b434eb95SMatthew G. Knepley PetscErrorCode ierr; 1478b434eb95SMatthew G. Knepley PetscInt m = A->rmap->n,*aj,*ii; 1479b434eb95SMatthew G. Knepley PetscInt n,i,*ridx=NULL; 1480b434eb95SMatthew G. Knepley PetscScalar sum; 1481b434eb95SMatthew G. Knepley PetscBool usecprow=a->compressedrow.use; 1482b434eb95SMatthew G. Knepley 1483b434eb95SMatthew G. Knepley PetscFunctionBegin; 1484b434eb95SMatthew G. Knepley ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1485d9ca1df4SBarry Smith ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 1486b434eb95SMatthew G. Knepley if (usecprow) { /* use compressed row format */ 1487b434eb95SMatthew G. Knepley if (zz != yy) { 1488580bdb30SBarry Smith ierr = PetscArraycpy(z,y,m);CHKERRQ(ierr); 1489b434eb95SMatthew G. Knepley } 1490b434eb95SMatthew G. Knepley m = a->compressedrow.nrows; 1491b434eb95SMatthew G. Knepley ii = a->compressedrow.i; 1492b434eb95SMatthew G. Knepley ridx = a->compressedrow.rindex; 1493b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1494b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1495b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1496b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1497b434eb95SMatthew G. Knepley sum = y[*ridx]; 1498b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1499b434eb95SMatthew G. Knepley z[*ridx++] = sum; 1500b434eb95SMatthew G. Knepley } 1501b434eb95SMatthew G. Knepley } else { /* do not use compressed row format */ 15023d3eaba7SBarry Smith ii = a->i; 1503b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1504b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1505b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1506b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1507b434eb95SMatthew G. Knepley sum = y[i]; 1508b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1509b434eb95SMatthew G. Knepley z[i] = sum; 1510b434eb95SMatthew G. Knepley } 1511b434eb95SMatthew G. Knepley } 1512b434eb95SMatthew G. Knepley ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1513b434eb95SMatthew G. Knepley ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1514d9ca1df4SBarry Smith ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 1515b434eb95SMatthew G. Knepley PetscFunctionReturn(0); 1516b434eb95SMatthew G. Knepley } 1517b434eb95SMatthew G. Knepley 1518c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h> 1519dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 152017ab2063SBarry Smith { 1521416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1522f15663dcSBarry Smith PetscScalar *y,*z; 1523f15663dcSBarry Smith const PetscScalar *x; 152454f21887SBarry Smith const MatScalar *aa; 1525dfbe8321SBarry Smith PetscErrorCode ierr; 1526d9ca1df4SBarry Smith const PetscInt *aj,*ii,*ridx=NULL; 1527d9ca1df4SBarry Smith PetscInt m = A->rmap->n,n,i; 1528362ced78SSatish Balay PetscScalar sum; 1529ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 15309ea0dfa2SSatish Balay 15313a40ed3dSBarry Smith PetscFunctionBegin; 1532f15663dcSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1533d9ca1df4SBarry Smith ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 15344eb6d288SHong Zhang if (usecprow) { /* use compressed row format */ 15354eb6d288SHong Zhang if (zz != yy) { 1536580bdb30SBarry Smith ierr = PetscArraycpy(z,y,m);CHKERRQ(ierr); 15374eb6d288SHong Zhang } 153897952fefSHong Zhang m = a->compressedrow.nrows; 153997952fefSHong Zhang ii = a->compressedrow.i; 154097952fefSHong Zhang ridx = a->compressedrow.rindex; 154197952fefSHong Zhang for (i=0; i<m; i++) { 154297952fefSHong Zhang n = ii[i+1] - ii[i]; 154397952fefSHong Zhang aj = a->j + ii[i]; 154497952fefSHong Zhang aa = a->a + ii[i]; 154597952fefSHong Zhang sum = y[*ridx]; 1546f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 154797952fefSHong Zhang z[*ridx++] = sum; 154897952fefSHong Zhang } 154997952fefSHong Zhang } else { /* do not use compressed row format */ 15503d3eaba7SBarry Smith ii = a->i; 1551f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 15523d3eaba7SBarry Smith aj = a->j; 15533d3eaba7SBarry Smith aa = a->a; 1554f15663dcSBarry Smith fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 1555f15663dcSBarry Smith #else 155617ab2063SBarry Smith for (i=0; i<m; i++) { 1557f15663dcSBarry Smith n = ii[i+1] - ii[i]; 1558f15663dcSBarry Smith aj = a->j + ii[i]; 1559f15663dcSBarry Smith aa = a->a + ii[i]; 156017ab2063SBarry Smith sum = y[i]; 1561f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 156217ab2063SBarry Smith z[i] = sum; 156317ab2063SBarry Smith } 156402ab625aSSatish Balay #endif 1565f15663dcSBarry Smith } 1566dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1567f15663dcSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1568d9ca1df4SBarry Smith ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 15693a40ed3dSBarry Smith PetscFunctionReturn(0); 157017ab2063SBarry Smith } 157117ab2063SBarry Smith 157217ab2063SBarry Smith /* 157317ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 157417ab2063SBarry Smith */ 1575dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 157617ab2063SBarry Smith { 1577416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15786849ba73SBarry Smith PetscErrorCode ierr; 1579d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n; 158017ab2063SBarry Smith 15813a40ed3dSBarry Smith PetscFunctionBegin; 158209f38230SBarry Smith if (!a->diag) { 1583785e854fSJed Brown ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr); 15843bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr); 158509f38230SBarry Smith } 1586d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 158709f38230SBarry Smith a->diag[i] = a->i[i+1]; 1588bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1589bfeeae90SHong Zhang if (a->j[j] == i) { 159009f38230SBarry Smith a->diag[i] = j; 159117ab2063SBarry Smith break; 159217ab2063SBarry Smith } 159317ab2063SBarry Smith } 159417ab2063SBarry Smith } 15953a40ed3dSBarry Smith PetscFunctionReturn(0); 159617ab2063SBarry Smith } 159717ab2063SBarry Smith 159861ecd0c6SBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v) 159961ecd0c6SBarry Smith { 160061ecd0c6SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 160161ecd0c6SBarry Smith const PetscInt *diag = (const PetscInt*)a->diag; 160261ecd0c6SBarry Smith const PetscInt *ii = (const PetscInt*) a->i; 160361ecd0c6SBarry Smith PetscInt i,*mdiag = NULL; 160461ecd0c6SBarry Smith PetscErrorCode ierr; 160561ecd0c6SBarry Smith PetscInt cnt = 0; /* how many diagonals are missing */ 160661ecd0c6SBarry Smith 160761ecd0c6SBarry Smith PetscFunctionBegin; 160861ecd0c6SBarry Smith if (!A->preallocated || !a->nz) { 160961ecd0c6SBarry Smith ierr = MatSeqAIJSetPreallocation(A,1,NULL);CHKERRQ(ierr); 161061ecd0c6SBarry Smith ierr = MatShift_Basic(A,v);CHKERRQ(ierr); 161161ecd0c6SBarry Smith PetscFunctionReturn(0); 161261ecd0c6SBarry Smith } 161361ecd0c6SBarry Smith 161461ecd0c6SBarry Smith if (a->diagonaldense) { 161561ecd0c6SBarry Smith cnt = 0; 161661ecd0c6SBarry Smith } else { 161761ecd0c6SBarry Smith ierr = PetscCalloc1(A->rmap->n,&mdiag);CHKERRQ(ierr); 161861ecd0c6SBarry Smith for (i=0; i<A->rmap->n; i++) { 161961ecd0c6SBarry Smith if (diag[i] >= ii[i+1]) { 162061ecd0c6SBarry Smith cnt++; 162161ecd0c6SBarry Smith mdiag[i] = 1; 162261ecd0c6SBarry Smith } 162361ecd0c6SBarry Smith } 162461ecd0c6SBarry Smith } 162561ecd0c6SBarry Smith if (!cnt) { 162661ecd0c6SBarry Smith ierr = MatShift_Basic(A,v);CHKERRQ(ierr); 162761ecd0c6SBarry Smith } else { 1628b6f2aa54SBarry Smith PetscScalar *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */ 1629b6f2aa54SBarry Smith PetscInt *oldj = a->j, *oldi = a->i; 163061ecd0c6SBarry Smith PetscBool singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij; 163161ecd0c6SBarry Smith 163261ecd0c6SBarry Smith a->a = NULL; 163361ecd0c6SBarry Smith a->j = NULL; 163461ecd0c6SBarry Smith a->i = NULL; 163561ecd0c6SBarry Smith /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */ 163661ecd0c6SBarry Smith for (i=0; i<A->rmap->n; i++) { 163761ecd0c6SBarry Smith a->imax[i] += mdiag[i]; 1638447d62f5SStefano Zampini a->imax[i] = PetscMin(a->imax[i],A->cmap->n); 163961ecd0c6SBarry Smith } 164061ecd0c6SBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);CHKERRQ(ierr); 164161ecd0c6SBarry Smith 164261ecd0c6SBarry Smith /* copy old values into new matrix data structure */ 164361ecd0c6SBarry Smith for (i=0; i<A->rmap->n; i++) { 164461ecd0c6SBarry Smith ierr = MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);CHKERRQ(ierr); 1645447d62f5SStefano Zampini if (i < A->cmap->n) { 164661ecd0c6SBarry Smith ierr = MatSetValue(A,i,i,v,ADD_VALUES);CHKERRQ(ierr); 164761ecd0c6SBarry Smith } 1648447d62f5SStefano Zampini } 164961ecd0c6SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 165061ecd0c6SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 165161ecd0c6SBarry Smith if (singlemalloc) { 165261ecd0c6SBarry Smith ierr = PetscFree3(olda,oldj,oldi);CHKERRQ(ierr); 165361ecd0c6SBarry Smith } else { 165461ecd0c6SBarry Smith if (free_a) {ierr = PetscFree(olda);CHKERRQ(ierr);} 165561ecd0c6SBarry Smith if (free_ij) {ierr = PetscFree(oldj);CHKERRQ(ierr);} 165661ecd0c6SBarry Smith if (free_ij) {ierr = PetscFree(oldi);CHKERRQ(ierr);} 165761ecd0c6SBarry Smith } 165861ecd0c6SBarry Smith } 165961ecd0c6SBarry Smith ierr = PetscFree(mdiag);CHKERRQ(ierr); 166061ecd0c6SBarry Smith a->diagonaldense = PETSC_TRUE; 166161ecd0c6SBarry Smith PetscFunctionReturn(0); 166261ecd0c6SBarry Smith } 166361ecd0c6SBarry Smith 1664be5855fcSBarry Smith /* 1665be5855fcSBarry Smith Checks for missing diagonals 1666be5855fcSBarry Smith */ 1667ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool *missing,PetscInt *d) 1668be5855fcSBarry Smith { 1669be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 16707734d3b5SMatthew G. Knepley PetscInt *diag,*ii = a->i,i; 1671994fe344SLisandro Dalcin PetscErrorCode ierr; 1672be5855fcSBarry Smith 1673be5855fcSBarry Smith PetscFunctionBegin; 167409f38230SBarry Smith *missing = PETSC_FALSE; 16757734d3b5SMatthew G. Knepley if (A->rmap->n > 0 && !ii) { 167609f38230SBarry Smith *missing = PETSC_TRUE; 167709f38230SBarry Smith if (d) *d = 0; 1678994fe344SLisandro Dalcin ierr = PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");CHKERRQ(ierr); 167909f38230SBarry Smith } else { 168001445905SHong Zhang PetscInt n; 168101445905SHong Zhang n = PetscMin(A->rmap->n, A->cmap->n); 1682f1e2ffcdSBarry Smith diag = a->diag; 168301445905SHong Zhang for (i=0; i<n; i++) { 16847734d3b5SMatthew G. Knepley if (diag[i] >= ii[i+1]) { 168509f38230SBarry Smith *missing = PETSC_TRUE; 168609f38230SBarry Smith if (d) *d = i; 1687994fe344SLisandro Dalcin ierr = PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);CHKERRQ(ierr); 1688358d2f5dSShri Abhyankar break; 168909f38230SBarry Smith } 1690be5855fcSBarry Smith } 1691be5855fcSBarry Smith } 1692be5855fcSBarry Smith PetscFunctionReturn(0); 1693be5855fcSBarry Smith } 1694be5855fcSBarry Smith 16950da83c2eSBarry Smith #include <petscblaslapack.h> 16960da83c2eSBarry Smith #include <petsc/private/kernels/blockinvert.h> 16970da83c2eSBarry Smith 16980da83c2eSBarry Smith /* 16990da83c2eSBarry Smith Note that values is allocated externally by the PC and then passed into this routine 17000da83c2eSBarry Smith */ 17010da83c2eSBarry Smith PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag) 17020da83c2eSBarry Smith { 17030da83c2eSBarry Smith PetscErrorCode ierr; 17040da83c2eSBarry Smith PetscInt n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots; 17050da83c2eSBarry Smith PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE; 17060da83c2eSBarry Smith const PetscReal shift = 0.0; 17070da83c2eSBarry Smith PetscInt ipvt[5]; 17080da83c2eSBarry Smith PetscScalar work[25],*v_work; 17090da83c2eSBarry Smith 17100da83c2eSBarry Smith PetscFunctionBegin; 17110da83c2eSBarry Smith allowzeropivot = PetscNot(A->erroriffailure); 17120da83c2eSBarry Smith for (i=0; i<nblocks; i++) ncnt += bsizes[i]; 17130da83c2eSBarry Smith if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n); 17140da83c2eSBarry Smith for (i=0; i<nblocks; i++) { 17150da83c2eSBarry Smith bsizemax = PetscMax(bsizemax,bsizes[i]); 17160da83c2eSBarry Smith } 17170da83c2eSBarry Smith ierr = PetscMalloc1(bsizemax,&indx);CHKERRQ(ierr); 17180da83c2eSBarry Smith if (bsizemax > 7) { 17190da83c2eSBarry Smith ierr = PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);CHKERRQ(ierr); 17200da83c2eSBarry Smith } 17210da83c2eSBarry Smith ncnt = 0; 17220da83c2eSBarry Smith for (i=0; i<nblocks; i++) { 17230da83c2eSBarry Smith for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j; 17240da83c2eSBarry Smith ierr = MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);CHKERRQ(ierr); 17250da83c2eSBarry Smith switch (bsizes[i]) { 17260da83c2eSBarry Smith case 1: 17270da83c2eSBarry Smith *diag = 1.0/(*diag); 17280da83c2eSBarry Smith break; 17290da83c2eSBarry Smith case 2: 17300da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17310da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17320da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr); 17330da83c2eSBarry Smith break; 17340da83c2eSBarry Smith case 3: 17350da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17360da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17370da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr); 17380da83c2eSBarry Smith break; 17390da83c2eSBarry Smith case 4: 17400da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17410da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17420da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr); 17430da83c2eSBarry Smith break; 17440da83c2eSBarry Smith case 5: 17450da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17460da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17470da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr); 17480da83c2eSBarry Smith break; 17490da83c2eSBarry Smith case 6: 17500da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17510da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17520da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr); 17530da83c2eSBarry Smith break; 17540da83c2eSBarry Smith case 7: 17550da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17560da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17570da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr); 17580da83c2eSBarry Smith break; 17590da83c2eSBarry Smith default: 17600da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17610da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17620da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);CHKERRQ(ierr); 17630da83c2eSBarry Smith } 17640da83c2eSBarry Smith ncnt += bsizes[i]; 17650da83c2eSBarry Smith diag += bsizes[i]*bsizes[i]; 17660da83c2eSBarry Smith } 17670da83c2eSBarry Smith if (bsizemax > 7) { 17680da83c2eSBarry Smith ierr = PetscFree2(v_work,v_pivots);CHKERRQ(ierr); 17690da83c2eSBarry Smith } 17700da83c2eSBarry Smith ierr = PetscFree(indx);CHKERRQ(ierr); 17710da83c2eSBarry Smith PetscFunctionReturn(0); 17720da83c2eSBarry Smith } 17730da83c2eSBarry Smith 1774422a814eSBarry Smith /* 1775422a814eSBarry Smith Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways 1776422a814eSBarry Smith */ 17777087cfbeSBarry Smith PetscErrorCode MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift) 177871f1c65dSBarry Smith { 177971f1c65dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 178071f1c65dSBarry Smith PetscErrorCode ierr; 1781d0f46423SBarry Smith PetscInt i,*diag,m = A->rmap->n; 178254f21887SBarry Smith MatScalar *v = a->a; 178354f21887SBarry Smith PetscScalar *idiag,*mdiag; 178471f1c65dSBarry Smith 178571f1c65dSBarry Smith PetscFunctionBegin; 178671f1c65dSBarry Smith if (a->idiagvalid) PetscFunctionReturn(0); 178771f1c65dSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 178871f1c65dSBarry Smith diag = a->diag; 178971f1c65dSBarry Smith if (!a->idiag) { 1790dcca6d9dSJed Brown ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr); 17913bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr); 179271f1c65dSBarry Smith v = a->a; 179371f1c65dSBarry Smith } 179471f1c65dSBarry Smith mdiag = a->mdiag; 179571f1c65dSBarry Smith idiag = a->idiag; 179671f1c65dSBarry Smith 1797422a814eSBarry Smith if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) { 179871f1c65dSBarry Smith for (i=0; i<m; i++) { 179971f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 1800899639b0SHong Zhang if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */ 1801899639b0SHong Zhang if (PetscRealPart(fshift)) { 1802899639b0SHong Zhang ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr); 18037b6c816cSBarry Smith A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 18047b6c816cSBarry Smith A->factorerror_zeropivot_value = 0.0; 18057b6c816cSBarry Smith A->factorerror_zeropivot_row = i; 1806a6fa060aSHong Zhang } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i); 1807899639b0SHong Zhang } 180871f1c65dSBarry Smith idiag[i] = 1.0/v[diag[i]]; 180971f1c65dSBarry Smith } 181071f1c65dSBarry Smith ierr = PetscLogFlops(m);CHKERRQ(ierr); 181171f1c65dSBarry Smith } else { 181271f1c65dSBarry Smith for (i=0; i<m; i++) { 181371f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 181471f1c65dSBarry Smith idiag[i] = omega/(fshift + v[diag[i]]); 181571f1c65dSBarry Smith } 1816dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr); 181771f1c65dSBarry Smith } 181871f1c65dSBarry Smith a->idiagvalid = PETSC_TRUE; 181971f1c65dSBarry Smith PetscFunctionReturn(0); 182071f1c65dSBarry Smith } 182171f1c65dSBarry Smith 1822c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h> 182341f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 182417ab2063SBarry Smith { 1825416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1826e6d1f457SBarry Smith PetscScalar *x,d,sum,*t,scale; 18273d3eaba7SBarry Smith const MatScalar *v,*idiag=0,*mdiag; 182854f21887SBarry Smith const PetscScalar *b, *bs,*xb, *ts; 1829dfbe8321SBarry Smith PetscErrorCode ierr; 18303d3eaba7SBarry Smith PetscInt n,m = A->rmap->n,i; 183197f1f81fSBarry Smith const PetscInt *idx,*diag; 183217ab2063SBarry Smith 18333a40ed3dSBarry Smith PetscFunctionBegin; 1834b965ef7fSBarry Smith its = its*lits; 183591723122SBarry Smith 183671f1c65dSBarry Smith if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */ 183771f1c65dSBarry Smith if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);} 183871f1c65dSBarry Smith a->fshift = fshift; 183971f1c65dSBarry Smith a->omega = omega; 1840ed480e8bSBarry Smith 184171f1c65dSBarry Smith diag = a->diag; 184271f1c65dSBarry Smith t = a->ssor_work; 1843ed480e8bSBarry Smith idiag = a->idiag; 184471f1c65dSBarry Smith mdiag = a->mdiag; 1845ed480e8bSBarry Smith 18461ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 18473649974fSBarry Smith ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr); 1848ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 184917ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 185017ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1851ed480e8bSBarry Smith bs = b; 185217ab2063SBarry Smith for (i=0; i<m; i++) { 185371f1c65dSBarry Smith d = fshift + mdiag[i]; 1854416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1855ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1856ed480e8bSBarry Smith v = a->a + diag[i] + 1; 185717ab2063SBarry Smith sum = b[i]*d/omega; 1858003131ecSBarry Smith PetscSparseDensePlusDot(sum,bs,v,idx,n); 185917ab2063SBarry Smith x[i] = sum; 186017ab2063SBarry Smith } 18611ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 18623649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1863efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 18643a40ed3dSBarry Smith PetscFunctionReturn(0); 186517ab2063SBarry Smith } 1866c783ea89SBarry Smith 18672205254eSKarl Rupp if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 18682205254eSKarl Rupp else if (flag & SOR_EISENSTAT) { 18694c500f23SPierre Jolivet /* Let A = L + U + D; where L is lower triangular, 1870887ee2caSBarry Smith U is upper triangular, E = D/omega; This routine applies 187117ab2063SBarry Smith 187217ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 187317ab2063SBarry Smith 1874887ee2caSBarry Smith to a vector efficiently using Eisenstat's trick. 187517ab2063SBarry Smith */ 187617ab2063SBarry Smith scale = (2.0/omega) - 1.0; 187717ab2063SBarry Smith 187817ab2063SBarry Smith /* x = (E + U)^{-1} b */ 187917ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1880416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1881ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1882ed480e8bSBarry Smith v = a->a + diag[i] + 1; 188317ab2063SBarry Smith sum = b[i]; 1884e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1885ed480e8bSBarry Smith x[i] = sum*idiag[i]; 188617ab2063SBarry Smith } 188717ab2063SBarry Smith 188817ab2063SBarry Smith /* t = b - (2*E - D)x */ 1889416022c9SBarry Smith v = a->a; 18902205254eSKarl Rupp for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i]; 189117ab2063SBarry Smith 189217ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1893ed480e8bSBarry Smith ts = t; 1894416022c9SBarry Smith diag = a->diag; 189517ab2063SBarry Smith for (i=0; i<m; i++) { 1896416022c9SBarry Smith n = diag[i] - a->i[i]; 1897ed480e8bSBarry Smith idx = a->j + a->i[i]; 1898ed480e8bSBarry Smith v = a->a + a->i[i]; 189917ab2063SBarry Smith sum = t[i]; 1900003131ecSBarry Smith PetscSparseDenseMinusDot(sum,ts,v,idx,n); 1901ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1902733d66baSBarry Smith /* x = x + t */ 1903733d66baSBarry Smith x[i] += t[i]; 190417ab2063SBarry Smith } 190517ab2063SBarry Smith 1906dc0b31edSSatish Balay ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr); 19071ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 19083649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 19093a40ed3dSBarry Smith PetscFunctionReturn(0); 191017ab2063SBarry Smith } 191117ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 191217ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) { 191317ab2063SBarry Smith for (i=0; i<m; i++) { 1914416022c9SBarry Smith n = diag[i] - a->i[i]; 1915ed480e8bSBarry Smith idx = a->j + a->i[i]; 1916ed480e8bSBarry Smith v = a->a + a->i[i]; 191717ab2063SBarry Smith sum = b[i]; 1918e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 19195c99c7daSBarry Smith t[i] = sum; 1920ed480e8bSBarry Smith x[i] = sum*idiag[i]; 192117ab2063SBarry Smith } 19225c99c7daSBarry Smith xb = t; 1923efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 19243a40ed3dSBarry Smith } else xb = b; 192517ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) { 192617ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1927416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1928ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1929ed480e8bSBarry Smith v = a->a + diag[i] + 1; 193017ab2063SBarry Smith sum = xb[i]; 1931e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 19325c99c7daSBarry Smith if (xb == b) { 1933ed480e8bSBarry Smith x[i] = sum*idiag[i]; 19345c99c7daSBarry Smith } else { 1935b19a5dc2SMark Adams x[i] = (1-omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 193617ab2063SBarry Smith } 19375c99c7daSBarry Smith } 1938b19a5dc2SMark Adams ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */ 193917ab2063SBarry Smith } 194017ab2063SBarry Smith its--; 194117ab2063SBarry Smith } 194217ab2063SBarry Smith while (its--) { 194317ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) { 194417ab2063SBarry Smith for (i=0; i<m; i++) { 1945b19a5dc2SMark Adams /* lower */ 1946b19a5dc2SMark Adams n = diag[i] - a->i[i]; 1947ed480e8bSBarry Smith idx = a->j + a->i[i]; 1948ed480e8bSBarry Smith v = a->a + a->i[i]; 194917ab2063SBarry Smith sum = b[i]; 1950e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1951b19a5dc2SMark Adams t[i] = sum; /* save application of the lower-triangular part */ 1952b19a5dc2SMark Adams /* upper */ 1953b19a5dc2SMark Adams n = a->i[i+1] - diag[i] - 1; 1954b19a5dc2SMark Adams idx = a->j + diag[i] + 1; 1955b19a5dc2SMark Adams v = a->a + diag[i] + 1; 1956b19a5dc2SMark Adams PetscSparseDenseMinusDot(sum,x,v,idx,n); 1957b19a5dc2SMark Adams x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 195817ab2063SBarry Smith } 1959b19a5dc2SMark Adams xb = t; 19609f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1961b19a5dc2SMark Adams } else xb = b; 196217ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) { 196317ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1964b19a5dc2SMark Adams sum = xb[i]; 1965b19a5dc2SMark Adams if (xb == b) { 1966b19a5dc2SMark Adams /* whole matrix (no checkpointing available) */ 1967416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1968ed480e8bSBarry Smith idx = a->j + a->i[i]; 1969ed480e8bSBarry Smith v = a->a + a->i[i]; 1970e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1971ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 1972b19a5dc2SMark Adams } else { /* lower-triangular part has been saved, so only apply upper-triangular */ 1973b19a5dc2SMark Adams n = a->i[i+1] - diag[i] - 1; 1974b19a5dc2SMark Adams idx = a->j + diag[i] + 1; 1975b19a5dc2SMark Adams v = a->a + diag[i] + 1; 1976b19a5dc2SMark Adams PetscSparseDenseMinusDot(sum,x,v,idx,n); 1977b19a5dc2SMark Adams x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 197817ab2063SBarry Smith } 1979b19a5dc2SMark Adams } 1980b19a5dc2SMark Adams if (xb == b) { 19819f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1982b19a5dc2SMark Adams } else { 1983b19a5dc2SMark Adams ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */ 1984b19a5dc2SMark Adams } 198517ab2063SBarry Smith } 198617ab2063SBarry Smith } 19871ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 19883649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1989365a8a9eSBarry Smith PetscFunctionReturn(0); 199017ab2063SBarry Smith } 199117ab2063SBarry Smith 19922af78befSBarry Smith 1993dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 199417ab2063SBarry Smith { 1995416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 19964e220ebcSLois Curfman McInnes 19973a40ed3dSBarry Smith PetscFunctionBegin; 19984e220ebcSLois Curfman McInnes info->block_size = 1.0; 19993966268fSBarry Smith info->nz_allocated = a->maxnz; 20003966268fSBarry Smith info->nz_used = a->nz; 20013966268fSBarry Smith info->nz_unneeded = (a->maxnz - a->nz); 20023966268fSBarry Smith info->assemblies = A->num_ass; 20033966268fSBarry Smith info->mallocs = A->info.mallocs; 20047adad957SLisandro Dalcin info->memory = ((PetscObject)A)->mem; 2005d5f3da31SBarry Smith if (A->factortype) { 20064e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 20074e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 20084e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 20094e220ebcSLois Curfman McInnes } else { 20104e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 20114e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 20124e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 20134e220ebcSLois Curfman McInnes } 20143a40ed3dSBarry Smith PetscFunctionReturn(0); 201517ab2063SBarry Smith } 201617ab2063SBarry Smith 20172b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 201817ab2063SBarry Smith { 2019416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2020c7da8527SEric Chamberland PetscInt i,m = A->rmap->n - 1; 20216849ba73SBarry Smith PetscErrorCode ierr; 202297b48c8fSBarry Smith const PetscScalar *xx; 202397b48c8fSBarry Smith PetscScalar *bb; 2024c7da8527SEric Chamberland PetscInt d = 0; 202517ab2063SBarry Smith 20263a40ed3dSBarry Smith PetscFunctionBegin; 202797b48c8fSBarry Smith if (x && b) { 202897b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 202997b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 203097b48c8fSBarry Smith for (i=0; i<N; i++) { 203197b48c8fSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2032447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) continue; 203397b48c8fSBarry Smith bb[rows[i]] = diag*xx[rows[i]]; 203497b48c8fSBarry Smith } 203597b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 203697b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 203797b48c8fSBarry Smith } 203897b48c8fSBarry Smith 2039a9817697SBarry Smith if (a->keepnonzeropattern) { 2040f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 2041e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2042580bdb30SBarry Smith ierr = PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);CHKERRQ(ierr); 2043f1e2ffcdSBarry Smith } 2044f4df32b1SMatthew Knepley if (diag != 0.0) { 2045c7da8527SEric Chamberland for (i=0; i<N; i++) { 2046c7da8527SEric Chamberland d = rows[i]; 2047447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) continue; 2048c7da8527SEric 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); 2049c7da8527SEric Chamberland } 2050f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 2051447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) continue; 2052f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 2053f1e2ffcdSBarry Smith } 2054f1e2ffcdSBarry Smith } 2055f1e2ffcdSBarry Smith } else { 2056f4df32b1SMatthew Knepley if (diag != 0.0) { 205717ab2063SBarry Smith for (i=0; i<N; i++) { 2058e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 20597ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 2060447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) { 2061447d62f5SStefano Zampini a->ilen[rows[i]] = 0; 2062447d62f5SStefano Zampini } else { 2063416022c9SBarry Smith a->ilen[rows[i]] = 1; 2064f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 2065bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 2066447d62f5SStefano Zampini } 2067447d62f5SStefano Zampini } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */ 2068f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 206917ab2063SBarry Smith } 207017ab2063SBarry Smith } 20713a40ed3dSBarry Smith } else { 207217ab2063SBarry Smith for (i=0; i<N; i++) { 2073e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2074416022c9SBarry Smith a->ilen[rows[i]] = 0; 207517ab2063SBarry Smith } 207617ab2063SBarry Smith } 2077e56f5c9eSBarry Smith A->nonzerostate++; 2078f1e2ffcdSBarry Smith } 2079e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2080c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 2081e2cf4d64SStefano Zampini #endif 20824099cc6bSBarry Smith ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20833a40ed3dSBarry Smith PetscFunctionReturn(0); 208417ab2063SBarry Smith } 208517ab2063SBarry Smith 20866e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 20876e169961SBarry Smith { 20886e169961SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 20896e169961SBarry Smith PetscInt i,j,m = A->rmap->n - 1,d = 0; 20906e169961SBarry Smith PetscErrorCode ierr; 20912b40b63fSBarry Smith PetscBool missing,*zeroed,vecs = PETSC_FALSE; 20926e169961SBarry Smith const PetscScalar *xx; 20936e169961SBarry Smith PetscScalar *bb; 20946e169961SBarry Smith 20956e169961SBarry Smith PetscFunctionBegin; 20966e169961SBarry Smith if (x && b) { 20976e169961SBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 20986e169961SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 20992b40b63fSBarry Smith vecs = PETSC_TRUE; 21006e169961SBarry Smith } 21011795a4d1SJed Brown ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr); 21026e169961SBarry Smith for (i=0; i<N; i++) { 21036e169961SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2104580bdb30SBarry Smith ierr = PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);CHKERRQ(ierr); 21052205254eSKarl Rupp 21066e169961SBarry Smith zeroed[rows[i]] = PETSC_TRUE; 21076e169961SBarry Smith } 21086e169961SBarry Smith for (i=0; i<A->rmap->n; i++) { 21096e169961SBarry Smith if (!zeroed[i]) { 21106e169961SBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 21114cf107fdSStefano Zampini if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) { 21122b40b63fSBarry Smith if (vecs) bb[i] -= a->a[j]*xx[a->j[j]]; 21136e169961SBarry Smith a->a[j] = 0.0; 21146e169961SBarry Smith } 21156e169961SBarry Smith } 21164cf107fdSStefano Zampini } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i]; 21176e169961SBarry Smith } 21186e169961SBarry Smith if (x && b) { 21196e169961SBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 21206e169961SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 21216e169961SBarry Smith } 21226e169961SBarry Smith ierr = PetscFree(zeroed);CHKERRQ(ierr); 21236e169961SBarry Smith if (diag != 0.0) { 21246e169961SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 21251d5a398dSstefano_zampini if (missing) { 21261d5a398dSstefano_zampini for (i=0; i<N; i++) { 21274cf107fdSStefano Zampini if (rows[i] >= A->cmap->N) continue; 21284cf107fdSStefano Zampini if (a->nonew && rows[i] >= d) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D (%D)",d,rows[i]); 21291d5a398dSstefano_zampini ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 21301d5a398dSstefano_zampini } 21311d5a398dSstefano_zampini } else { 21326e169961SBarry Smith for (i=0; i<N; i++) { 21336e169961SBarry Smith a->a[a->diag[rows[i]]] = diag; 21346e169961SBarry Smith } 21356e169961SBarry Smith } 21361d5a398dSstefano_zampini } 2137e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2138c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 2139e2cf4d64SStefano Zampini #endif 21404099cc6bSBarry Smith ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21416e169961SBarry Smith PetscFunctionReturn(0); 21426e169961SBarry Smith } 21436e169961SBarry Smith 2144a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 214517ab2063SBarry Smith { 2146416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 214797f1f81fSBarry Smith PetscInt *itmp; 214817ab2063SBarry Smith 21493a40ed3dSBarry Smith PetscFunctionBegin; 2150e32f2f54SBarry Smith if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 215117ab2063SBarry Smith 2152416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 2153bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 215417ab2063SBarry Smith if (idx) { 2155bfeeae90SHong Zhang itmp = a->j + a->i[row]; 215626fbe8dcSKarl Rupp if (*nz) *idx = itmp; 215717ab2063SBarry Smith else *idx = 0; 215817ab2063SBarry Smith } 21593a40ed3dSBarry Smith PetscFunctionReturn(0); 216017ab2063SBarry Smith } 216117ab2063SBarry Smith 2162bfeeae90SHong Zhang /* remove this function? */ 2163a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 216417ab2063SBarry Smith { 21653a40ed3dSBarry Smith PetscFunctionBegin; 21663a40ed3dSBarry Smith PetscFunctionReturn(0); 216717ab2063SBarry Smith } 216817ab2063SBarry Smith 2169dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 217017ab2063SBarry Smith { 2171416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 217254f21887SBarry Smith MatScalar *v = a->a; 217336db0b34SBarry Smith PetscReal sum = 0.0; 21746849ba73SBarry Smith PetscErrorCode ierr; 217597f1f81fSBarry Smith PetscInt i,j; 217617ab2063SBarry Smith 21773a40ed3dSBarry Smith PetscFunctionBegin; 217817ab2063SBarry Smith if (type == NORM_FROBENIUS) { 2179570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FP16) 2180570b7f6dSBarry Smith PetscBLASInt one = 1,nz = a->nz; 2181570b7f6dSBarry Smith *nrm = BLASnrm2_(&nz,v,&one); 2182570b7f6dSBarry Smith #else 2183416022c9SBarry Smith for (i=0; i<a->nz; i++) { 218436db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 218517ab2063SBarry Smith } 21868f1a2a5eSBarry Smith *nrm = PetscSqrtReal(sum); 2187570b7f6dSBarry Smith #endif 218851f70360SJed Brown ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr); 21893a40ed3dSBarry Smith } else if (type == NORM_1) { 219036db0b34SBarry Smith PetscReal *tmp; 219197f1f81fSBarry Smith PetscInt *jj = a->j; 21921795a4d1SJed Brown ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr); 2193064f8208SBarry Smith *nrm = 0.0; 2194416022c9SBarry Smith for (j=0; j<a->nz; j++) { 2195bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 219617ab2063SBarry Smith } 2197d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 2198064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 219917ab2063SBarry Smith } 2200606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 220151f70360SJed Brown ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr); 22023a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 2203064f8208SBarry Smith *nrm = 0.0; 2204d0f46423SBarry Smith for (j=0; j<A->rmap->n; j++) { 2205bfeeae90SHong Zhang v = a->a + a->i[j]; 220617ab2063SBarry Smith sum = 0.0; 2207416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 2208cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 220917ab2063SBarry Smith } 2210064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 221117ab2063SBarry Smith } 221251f70360SJed Brown ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr); 2213f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm"); 22143a40ed3dSBarry Smith PetscFunctionReturn(0); 221517ab2063SBarry Smith } 221617ab2063SBarry Smith 22174e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */ 22184e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B) 22194e938277SHong Zhang { 22204e938277SHong Zhang PetscErrorCode ierr; 22214e938277SHong Zhang PetscInt i,j,anzj; 22224e938277SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b; 22234e938277SHong Zhang PetscInt an=A->cmap->N,am=A->rmap->N; 22244e938277SHong Zhang PetscInt *ati,*atj,*atfill,*ai=a->i,*aj=a->j; 22254e938277SHong Zhang 22264e938277SHong Zhang PetscFunctionBegin; 22274e938277SHong Zhang /* Allocate space for symbolic transpose info and work array */ 2228854ce69bSBarry Smith ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr); 2229785e854fSJed Brown ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr); 2230785e854fSJed Brown ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr); 22314e938277SHong Zhang 22324e938277SHong Zhang /* Walk through aj and count ## of non-zeros in each row of A^T. */ 22334e938277SHong Zhang /* Note: offset by 1 for fast conversion into csr format. */ 223426fbe8dcSKarl Rupp for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1; 22354e938277SHong Zhang /* Form ati for csr format of A^T. */ 223626fbe8dcSKarl Rupp for (i=0;i<an;i++) ati[i+1] += ati[i]; 22374e938277SHong Zhang 22384e938277SHong Zhang /* Copy ati into atfill so we have locations of the next free space in atj */ 2239580bdb30SBarry Smith ierr = PetscArraycpy(atfill,ati,an);CHKERRQ(ierr); 22404e938277SHong Zhang 22414e938277SHong Zhang /* Walk through A row-wise and mark nonzero entries of A^T. */ 22424e938277SHong Zhang for (i=0;i<am;i++) { 22434e938277SHong Zhang anzj = ai[i+1] - ai[i]; 22444e938277SHong Zhang for (j=0;j<anzj;j++) { 22454e938277SHong Zhang atj[atfill[*aj]] = i; 22464e938277SHong Zhang atfill[*aj++] += 1; 22474e938277SHong Zhang } 22484e938277SHong Zhang } 22494e938277SHong Zhang 22504e938277SHong Zhang /* Clean up temporary space and complete requests. */ 22514e938277SHong Zhang ierr = PetscFree(atfill);CHKERRQ(ierr); 2252ce94432eSBarry Smith ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr); 225333d57670SJed Brown ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr); 2254b5bb3eecSMark Adams ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2255a2f3521dSMark F. Adams 22564e938277SHong Zhang b = (Mat_SeqAIJ*)((*B)->data); 22574e938277SHong Zhang b->free_a = PETSC_FALSE; 22584e938277SHong Zhang b->free_ij = PETSC_TRUE; 22594e938277SHong Zhang b->nonew = 0; 22604e938277SHong Zhang PetscFunctionReturn(0); 22614e938277SHong Zhang } 22624e938277SHong Zhang 22637087cfbeSBarry Smith PetscErrorCode MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 2264cd0d46ebSvictorle { 22653d3eaba7SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data; 226654f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 226754f21887SBarry Smith MatScalar *va,*vb; 22686849ba73SBarry Smith PetscErrorCode ierr; 226997f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 2270cd0d46ebSvictorle 2271cd0d46ebSvictorle PetscFunctionBegin; 2272cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 2273cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 22745485867bSBarry Smith if (ma!=nb || na!=mb) { 22755485867bSBarry Smith *f = PETSC_FALSE; 22765485867bSBarry Smith PetscFunctionReturn(0); 22775485867bSBarry Smith } 2278cd0d46ebSvictorle aii = aij->i; bii = bij->i; 2279cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 2280cd0d46ebSvictorle va = aij->a; vb = bij->a; 2281785e854fSJed Brown ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr); 2282785e854fSJed Brown ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr); 2283cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 2284cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 2285cd0d46ebSvictorle 2286cd0d46ebSvictorle *f = PETSC_TRUE; 2287cd0d46ebSvictorle for (i=0; i<ma; i++) { 2288cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 228997f1f81fSBarry Smith PetscInt idc,idr; 22905485867bSBarry Smith PetscScalar vc,vr; 2291cd0d46ebSvictorle /* column/row index/value */ 22925485867bSBarry Smith idc = adx[aptr[i]]; 22935485867bSBarry Smith idr = bdx[bptr[idc]]; 22945485867bSBarry Smith vc = va[aptr[i]]; 22955485867bSBarry Smith vr = vb[bptr[idc]]; 22965485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 22975485867bSBarry Smith *f = PETSC_FALSE; 22985485867bSBarry Smith goto done; 2299cd0d46ebSvictorle } else { 23005485867bSBarry Smith aptr[i]++; 23015485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 2302cd0d46ebSvictorle } 2303cd0d46ebSvictorle } 2304cd0d46ebSvictorle } 2305cd0d46ebSvictorle done: 2306cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 23073aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 2308cd0d46ebSvictorle PetscFunctionReturn(0); 2309cd0d46ebSvictorle } 2310cd0d46ebSvictorle 23117087cfbeSBarry Smith PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 23121cbb95d3SBarry Smith { 23133d3eaba7SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data; 231454f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 231554f21887SBarry Smith MatScalar *va,*vb; 23161cbb95d3SBarry Smith PetscErrorCode ierr; 23171cbb95d3SBarry Smith PetscInt ma,na,mb,nb, i; 23181cbb95d3SBarry Smith 23191cbb95d3SBarry Smith PetscFunctionBegin; 23201cbb95d3SBarry Smith ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 23211cbb95d3SBarry Smith ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 23221cbb95d3SBarry Smith if (ma!=nb || na!=mb) { 23231cbb95d3SBarry Smith *f = PETSC_FALSE; 23241cbb95d3SBarry Smith PetscFunctionReturn(0); 23251cbb95d3SBarry Smith } 23261cbb95d3SBarry Smith aii = aij->i; bii = bij->i; 23271cbb95d3SBarry Smith adx = aij->j; bdx = bij->j; 23281cbb95d3SBarry Smith va = aij->a; vb = bij->a; 2329785e854fSJed Brown ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr); 2330785e854fSJed Brown ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr); 23311cbb95d3SBarry Smith for (i=0; i<ma; i++) aptr[i] = aii[i]; 23321cbb95d3SBarry Smith for (i=0; i<mb; i++) bptr[i] = bii[i]; 23331cbb95d3SBarry Smith 23341cbb95d3SBarry Smith *f = PETSC_TRUE; 23351cbb95d3SBarry Smith for (i=0; i<ma; i++) { 23361cbb95d3SBarry Smith while (aptr[i]<aii[i+1]) { 23371cbb95d3SBarry Smith PetscInt idc,idr; 23381cbb95d3SBarry Smith PetscScalar vc,vr; 23391cbb95d3SBarry Smith /* column/row index/value */ 23401cbb95d3SBarry Smith idc = adx[aptr[i]]; 23411cbb95d3SBarry Smith idr = bdx[bptr[idc]]; 23421cbb95d3SBarry Smith vc = va[aptr[i]]; 23431cbb95d3SBarry Smith vr = vb[bptr[idc]]; 23441cbb95d3SBarry Smith if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) { 23451cbb95d3SBarry Smith *f = PETSC_FALSE; 23461cbb95d3SBarry Smith goto done; 23471cbb95d3SBarry Smith } else { 23481cbb95d3SBarry Smith aptr[i]++; 23491cbb95d3SBarry Smith if (B || i!=idc) bptr[idc]++; 23501cbb95d3SBarry Smith } 23511cbb95d3SBarry Smith } 23521cbb95d3SBarry Smith } 23531cbb95d3SBarry Smith done: 23541cbb95d3SBarry Smith ierr = PetscFree(aptr);CHKERRQ(ierr); 23551cbb95d3SBarry Smith ierr = PetscFree(bptr);CHKERRQ(ierr); 23561cbb95d3SBarry Smith PetscFunctionReturn(0); 23571cbb95d3SBarry Smith } 23581cbb95d3SBarry Smith 2359ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 23609e29f15eSvictorle { 2361dfbe8321SBarry Smith PetscErrorCode ierr; 23626e111a19SKarl Rupp 23639e29f15eSvictorle PetscFunctionBegin; 23645485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 23659e29f15eSvictorle PetscFunctionReturn(0); 23669e29f15eSvictorle } 23679e29f15eSvictorle 2368ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 23691cbb95d3SBarry Smith { 23701cbb95d3SBarry Smith PetscErrorCode ierr; 23716e111a19SKarl Rupp 23721cbb95d3SBarry Smith PetscFunctionBegin; 23731cbb95d3SBarry Smith ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 23741cbb95d3SBarry Smith PetscFunctionReturn(0); 23751cbb95d3SBarry Smith } 23761cbb95d3SBarry Smith 2377dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 237817ab2063SBarry Smith { 2379416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2380fff8e43fSBarry Smith const PetscScalar *l,*r; 2381fff8e43fSBarry Smith PetscScalar x; 238254f21887SBarry Smith MatScalar *v; 2383dfbe8321SBarry Smith PetscErrorCode ierr; 2384fff8e43fSBarry Smith PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz; 2385fff8e43fSBarry Smith const PetscInt *jj; 238617ab2063SBarry Smith 23873a40ed3dSBarry Smith PetscFunctionBegin; 238817ab2063SBarry Smith if (ll) { 23893ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 23903ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 2391e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 2392e32f2f54SBarry Smith if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 2393fff8e43fSBarry Smith ierr = VecGetArrayRead(ll,&l);CHKERRQ(ierr); 2394416022c9SBarry Smith v = a->a; 239517ab2063SBarry Smith for (i=0; i<m; i++) { 239617ab2063SBarry Smith x = l[i]; 2397416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 23982205254eSKarl Rupp for (j=0; j<M; j++) (*v++) *= x; 239917ab2063SBarry Smith } 2400fff8e43fSBarry Smith ierr = VecRestoreArrayRead(ll,&l);CHKERRQ(ierr); 2401efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 240217ab2063SBarry Smith } 240317ab2063SBarry Smith if (rr) { 2404e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 2405e32f2f54SBarry Smith if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 2406fff8e43fSBarry Smith ierr = VecGetArrayRead(rr,&r);CHKERRQ(ierr); 2407416022c9SBarry Smith v = a->a; jj = a->j; 24082205254eSKarl Rupp for (i=0; i<nz; i++) (*v++) *= r[*jj++]; 2409fff8e43fSBarry Smith ierr = VecRestoreArrayRead(rr,&r);CHKERRQ(ierr); 2410efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 241117ab2063SBarry Smith } 2412acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 2413e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2414c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 2415e2cf4d64SStefano Zampini #endif 24163a40ed3dSBarry Smith PetscFunctionReturn(0); 241717ab2063SBarry Smith } 241817ab2063SBarry Smith 24197dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 242017ab2063SBarry Smith { 2421db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 24226849ba73SBarry Smith PetscErrorCode ierr; 2423d0f46423SBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens; 242497f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 24255d0c19d7SBarry Smith const PetscInt *irow,*icol; 24265d0c19d7SBarry Smith PetscInt nrows,ncols; 242797f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 242854f21887SBarry Smith MatScalar *a_new,*mat_a; 2429416022c9SBarry Smith Mat C; 2430cdc6f3adSToby Isaac PetscBool stride; 243117ab2063SBarry Smith 24323a40ed3dSBarry Smith PetscFunctionBegin; 243399141d43SSatish Balay 243417ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 2435b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 2436b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 243717ab2063SBarry Smith 2438251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr); 2439ff718158SBarry Smith if (stride) { 2440ff718158SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 2441ff718158SBarry Smith } else { 2442ff718158SBarry Smith first = 0; 2443ff718158SBarry Smith step = 0; 2444ff718158SBarry Smith } 2445fee21e36SBarry Smith if (stride && step == 1) { 244602834360SBarry Smith /* special case of contiguous rows */ 2447dcca6d9dSJed Brown ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr); 244802834360SBarry Smith /* loop over new rows determining lens and starting points */ 244902834360SBarry Smith for (i=0; i<nrows; i++) { 2450bfeeae90SHong Zhang kstart = ai[irow[i]]; 2451a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 2452a91a9bebSLisandro Dalcin starts[i] = kstart; 245302834360SBarry Smith for (k=kstart; k<kend; k++) { 2454bfeeae90SHong Zhang if (aj[k] >= first) { 245502834360SBarry Smith starts[i] = k; 245602834360SBarry Smith break; 245702834360SBarry Smith } 245802834360SBarry Smith } 2459a2744918SBarry Smith sum = 0; 246002834360SBarry Smith while (k < kend) { 2461bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 2462a2744918SBarry Smith sum++; 246302834360SBarry Smith } 2464a2744918SBarry Smith lens[i] = sum; 246502834360SBarry Smith } 246602834360SBarry Smith /* create submatrix */ 2467cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 246897f1f81fSBarry Smith PetscInt n_cols,n_rows; 246908480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 2470e32f2f54SBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 2471d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 247208480c60SBarry Smith C = *B; 24733a40ed3dSBarry Smith } else { 24743bef6203SJed Brown PetscInt rbs,cbs; 2475ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2476f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 24773bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 24783bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 24793bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 24807adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2481ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 248208480c60SBarry Smith } 2483db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 2484db02288aSLois Curfman McInnes 248502834360SBarry Smith /* loop over rows inserting into submatrix */ 2486db02288aSLois Curfman McInnes a_new = c->a; 2487db02288aSLois Curfman McInnes j_new = c->j; 2488db02288aSLois Curfman McInnes i_new = c->i; 2489bfeeae90SHong Zhang 249002834360SBarry Smith for (i=0; i<nrows; i++) { 2491a2744918SBarry Smith ii = starts[i]; 2492a2744918SBarry Smith lensi = lens[i]; 2493a2744918SBarry Smith for (k=0; k<lensi; k++) { 2494a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 249502834360SBarry Smith } 2496580bdb30SBarry Smith ierr = PetscArraycpy(a_new,a->a + starts[i],lensi);CHKERRQ(ierr); 2497a2744918SBarry Smith a_new += lensi; 2498a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 2499a2744918SBarry Smith c->ilen[i] = lensi; 250002834360SBarry Smith } 25010e83c824SBarry Smith ierr = PetscFree2(lens,starts);CHKERRQ(ierr); 25023a40ed3dSBarry Smith } else { 250302834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 25041795a4d1SJed Brown ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr); 2505854ce69bSBarry Smith ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr); 25064dcab191SBarry Smith for (i=0; i<ncols; i++) { 25074dcab191SBarry Smith #if defined(PETSC_USE_DEBUG) 25084dcab191SBarry 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); 25094dcab191SBarry Smith #endif 25104dcab191SBarry Smith smap[icol[i]] = i+1; 25114dcab191SBarry Smith } 25124dcab191SBarry Smith 251302834360SBarry Smith /* determine lens of each row */ 251402834360SBarry Smith for (i=0; i<nrows; i++) { 2515bfeeae90SHong Zhang kstart = ai[irow[i]]; 251602834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 251702834360SBarry Smith lens[i] = 0; 251802834360SBarry Smith for (k=kstart; k<kend; k++) { 2519bfeeae90SHong Zhang if (smap[aj[k]]) { 252002834360SBarry Smith lens[i]++; 252102834360SBarry Smith } 252202834360SBarry Smith } 252302834360SBarry Smith } 252417ab2063SBarry Smith /* Create and fill new matrix */ 2525a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 2526ace3abfcSBarry Smith PetscBool equal; 25270f5bd95cSBarry Smith 252899141d43SSatish Balay c = (Mat_SeqAIJ*)((*B)->data); 2529e32f2f54SBarry Smith if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 2530580bdb30SBarry Smith ierr = PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);CHKERRQ(ierr); 2531f23aa3ddSBarry Smith if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 2532580bdb30SBarry Smith ierr = PetscArrayzero(c->ilen,(*B)->rmap->n);CHKERRQ(ierr); 253308480c60SBarry Smith C = *B; 25343a40ed3dSBarry Smith } else { 25353bef6203SJed Brown PetscInt rbs,cbs; 2536ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2537f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 25383bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 25393bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 25403bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 25417adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2542ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 254308480c60SBarry Smith } 254499141d43SSatish Balay c = (Mat_SeqAIJ*)(C->data); 254517ab2063SBarry Smith for (i=0; i<nrows; i++) { 254699141d43SSatish Balay row = irow[i]; 2547bfeeae90SHong Zhang kstart = ai[row]; 254899141d43SSatish Balay kend = kstart + a->ilen[row]; 2549bfeeae90SHong Zhang mat_i = c->i[i]; 255099141d43SSatish Balay mat_j = c->j + mat_i; 255199141d43SSatish Balay mat_a = c->a + mat_i; 255299141d43SSatish Balay mat_ilen = c->ilen + i; 255317ab2063SBarry Smith for (k=kstart; k<kend; k++) { 2554bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 2555ed480e8bSBarry Smith *mat_j++ = tcol - 1; 255699141d43SSatish Balay *mat_a++ = a->a[k]; 255799141d43SSatish Balay (*mat_ilen)++; 255899141d43SSatish Balay 255917ab2063SBarry Smith } 256017ab2063SBarry Smith } 256117ab2063SBarry Smith } 256202834360SBarry Smith /* Free work space */ 256302834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 2564606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 2565606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 2566cdc6f3adSToby Isaac /* sort */ 2567cdc6f3adSToby Isaac for (i = 0; i < nrows; i++) { 2568cdc6f3adSToby Isaac PetscInt ilen; 2569cdc6f3adSToby Isaac 2570cdc6f3adSToby Isaac mat_i = c->i[i]; 2571cdc6f3adSToby Isaac mat_j = c->j + mat_i; 2572cdc6f3adSToby Isaac mat_a = c->a + mat_i; 2573cdc6f3adSToby Isaac ilen = c->ilen[i]; 2574390e1bf2SBarry Smith ierr = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr); 2575cdc6f3adSToby Isaac } 257602834360SBarry Smith } 2577305c6ccfSStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2578305c6ccfSStefano Zampini ierr = MatPinToCPU(C,A->pinnedtocpu);CHKERRQ(ierr); 2579305c6ccfSStefano Zampini #endif 25806d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 25816d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 258217ab2063SBarry Smith 258317ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 2584416022c9SBarry Smith *B = C; 25853a40ed3dSBarry Smith PetscFunctionReturn(0); 258617ab2063SBarry Smith } 258717ab2063SBarry Smith 2588fc08c53fSHong Zhang PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat) 258982d44351SHong Zhang { 259082d44351SHong Zhang PetscErrorCode ierr; 259182d44351SHong Zhang Mat B; 259282d44351SHong Zhang 259382d44351SHong Zhang PetscFunctionBegin; 2594c2d650bdSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 259582d44351SHong Zhang ierr = MatCreate(subComm,&B);CHKERRQ(ierr); 259682d44351SHong Zhang ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr); 259733d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr); 259882d44351SHong Zhang ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 259982d44351SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr); 260082d44351SHong Zhang *subMat = B; 2601c2d650bdSHong Zhang } else { 2602c2d650bdSHong Zhang ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 2603c2d650bdSHong Zhang } 260482d44351SHong Zhang PetscFunctionReturn(0); 260582d44351SHong Zhang } 260682d44351SHong Zhang 26079a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info) 2608a871dcd8SBarry Smith { 260963b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2610dfbe8321SBarry Smith PetscErrorCode ierr; 261163b91edcSBarry Smith Mat outA; 2612ace3abfcSBarry Smith PetscBool row_identity,col_identity; 261363b91edcSBarry Smith 26143a40ed3dSBarry Smith PetscFunctionBegin; 2615e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 26161df811f5SHong Zhang 2617b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 2618b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 2619a871dcd8SBarry Smith 262063b91edcSBarry Smith outA = inA; 2621d5f3da31SBarry Smith outA->factortype = MAT_FACTOR_LU; 2622f6224b95SHong Zhang ierr = PetscFree(inA->solvertype);CHKERRQ(ierr); 2623f6224b95SHong Zhang ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr); 26242205254eSKarl Rupp 2625c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 26266bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 26272205254eSKarl Rupp 2628c3122656SLisandro Dalcin a->row = row; 26292205254eSKarl Rupp 2630c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 26316bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 26322205254eSKarl Rupp 2633c3122656SLisandro Dalcin a->col = col; 263463b91edcSBarry Smith 263536db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 26366bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 26374c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 26383bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr); 2639f0ec6fceSSatish Balay 264094a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 2641854ce69bSBarry Smith ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr); 26423bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 264394a9d846SBarry Smith } 264463b91edcSBarry Smith 2645f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 2646137fb511SHong Zhang if (row_identity && col_identity) { 2647ad04f41aSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr); 2648137fb511SHong Zhang } else { 2649719d5645SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr); 2650137fb511SHong Zhang } 26513a40ed3dSBarry Smith PetscFunctionReturn(0); 2652a871dcd8SBarry Smith } 2653a871dcd8SBarry Smith 2654f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 2655f0b747eeSBarry Smith { 2656f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2657f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 2658efee365bSSatish Balay PetscErrorCode ierr; 2659c5df96a5SBarry Smith PetscBLASInt one = 1,bnz; 26603a40ed3dSBarry Smith 26613a40ed3dSBarry Smith PetscFunctionBegin; 2662c5df96a5SBarry Smith ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr); 26638b83055fSJed Brown PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one)); 2664efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 2665acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr); 2666e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2667c70f7ee4SJunchao Zhang if (inA->offloadmask != PETSC_OFFLOAD_UNALLOCATED) inA->offloadmask = PETSC_OFFLOAD_CPU; 2668e2cf4d64SStefano Zampini #endif 26693a40ed3dSBarry Smith PetscFunctionReturn(0); 2670f0b747eeSBarry Smith } 2671f0b747eeSBarry Smith 2672f68bb481SHong Zhang PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj) 267316b64355SHong Zhang { 267416b64355SHong Zhang PetscErrorCode ierr; 267516b64355SHong Zhang PetscInt i; 267616b64355SHong Zhang 267716b64355SHong Zhang PetscFunctionBegin; 267816b64355SHong Zhang if (!submatj->id) { /* delete data that are linked only to submats[id=0] */ 267916b64355SHong Zhang ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr); 268016b64355SHong Zhang 268116b64355SHong Zhang for (i=0; i<submatj->nrqr; ++i) { 268216b64355SHong Zhang ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr); 268316b64355SHong Zhang } 268416b64355SHong Zhang ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr); 268516b64355SHong Zhang 268616b64355SHong Zhang if (submatj->rbuf1) { 268716b64355SHong Zhang ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr); 268816b64355SHong Zhang ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr); 268916b64355SHong Zhang } 269016b64355SHong Zhang 269116b64355SHong Zhang for (i=0; i<submatj->nrqs; ++i) { 269216b64355SHong Zhang ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr); 269316b64355SHong Zhang } 269416b64355SHong Zhang ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr); 269516b64355SHong Zhang ierr = PetscFree(submatj->pa);CHKERRQ(ierr); 269616b64355SHong Zhang } 269716b64355SHong Zhang 269816b64355SHong Zhang #if defined(PETSC_USE_CTABLE) 269916b64355SHong Zhang ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr); 270016b64355SHong Zhang if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);} 270116b64355SHong Zhang ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr); 270216b64355SHong Zhang #else 270316b64355SHong Zhang ierr = PetscFree(submatj->rmap);CHKERRQ(ierr); 270416b64355SHong Zhang #endif 270516b64355SHong Zhang 270616b64355SHong Zhang if (!submatj->allcolumns) { 270716b64355SHong Zhang #if defined(PETSC_USE_CTABLE) 270816b64355SHong Zhang ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr); 270916b64355SHong Zhang #else 271016b64355SHong Zhang ierr = PetscFree(submatj->cmap);CHKERRQ(ierr); 271116b64355SHong Zhang #endif 271216b64355SHong Zhang } 271316b64355SHong Zhang ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr); 271416b64355SHong Zhang 271516b64355SHong Zhang ierr = PetscFree(submatj);CHKERRQ(ierr); 271616b64355SHong Zhang PetscFunctionReturn(0); 271716b64355SHong Zhang } 271816b64355SHong Zhang 27190fb991dcSHong Zhang PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C) 272016b64355SHong Zhang { 272116b64355SHong Zhang PetscErrorCode ierr; 272216b64355SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data; 27235c39f6d9SHong Zhang Mat_SubSppt *submatj = c->submatis1; 272416b64355SHong Zhang 272516b64355SHong Zhang PetscFunctionBegin; 272634136279SStefano Zampini ierr = (*submatj->destroy)(C);CHKERRQ(ierr); 2727f68bb481SHong Zhang ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr); 272816b64355SHong Zhang PetscFunctionReturn(0); 272916b64355SHong Zhang } 273016b64355SHong Zhang 27312d033e1fSHong Zhang PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[]) 27322d033e1fSHong Zhang { 27332d033e1fSHong Zhang PetscErrorCode ierr; 27342d033e1fSHong Zhang PetscInt i; 27350fb991dcSHong Zhang Mat C; 27360fb991dcSHong Zhang Mat_SeqAIJ *c; 27370fb991dcSHong Zhang Mat_SubSppt *submatj; 27382d033e1fSHong Zhang 27392d033e1fSHong Zhang PetscFunctionBegin; 27402d033e1fSHong Zhang for (i=0; i<n; i++) { 27410fb991dcSHong Zhang C = (*mat)[i]; 27420fb991dcSHong Zhang c = (Mat_SeqAIJ*)C->data; 27430fb991dcSHong Zhang submatj = c->submatis1; 27442d033e1fSHong Zhang if (submatj) { 2745682e4c99SStefano Zampini if (--((PetscObject)C)->refct <= 0) { 274634136279SStefano Zampini ierr = (*submatj->destroy)(C);CHKERRQ(ierr); 2747f68bb481SHong Zhang ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr); 274834136279SStefano Zampini ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr); 27492d033e1fSHong Zhang ierr = PetscLayoutDestroy(&C->rmap);CHKERRQ(ierr); 27502d033e1fSHong Zhang ierr = PetscLayoutDestroy(&C->cmap);CHKERRQ(ierr); 27512d033e1fSHong Zhang ierr = PetscHeaderDestroy(&C);CHKERRQ(ierr); 2752682e4c99SStefano Zampini } 27532d033e1fSHong Zhang } else { 27542d033e1fSHong Zhang ierr = MatDestroy(&C);CHKERRQ(ierr); 27552d033e1fSHong Zhang } 27562d033e1fSHong Zhang } 275786e85357SHong Zhang 275863a75b2aSHong Zhang /* Destroy Dummy submatrices created for reuse */ 275963a75b2aSHong Zhang ierr = MatDestroySubMatrices_Dummy(n,mat);CHKERRQ(ierr); 276063a75b2aSHong Zhang 27612d033e1fSHong Zhang ierr = PetscFree(*mat);CHKERRQ(ierr); 27622d033e1fSHong Zhang PetscFunctionReturn(0); 27632d033e1fSHong Zhang } 27642d033e1fSHong Zhang 27657dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 2766cddf8d76SBarry Smith { 2767dfbe8321SBarry Smith PetscErrorCode ierr; 276897f1f81fSBarry Smith PetscInt i; 2769cddf8d76SBarry Smith 27703a40ed3dSBarry Smith PetscFunctionBegin; 2771cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 2772df750dc8SHong Zhang ierr = PetscCalloc1(n+1,B);CHKERRQ(ierr); 2773cddf8d76SBarry Smith } 2774cddf8d76SBarry Smith 2775cddf8d76SBarry Smith for (i=0; i<n; i++) { 27767dae84e0SHong Zhang ierr = MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 2777cddf8d76SBarry Smith } 27783a40ed3dSBarry Smith PetscFunctionReturn(0); 2779cddf8d76SBarry Smith } 2780cddf8d76SBarry Smith 278197f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 27824dcbc457SBarry Smith { 2783e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 27846849ba73SBarry Smith PetscErrorCode ierr; 27855d0c19d7SBarry Smith PetscInt row,i,j,k,l,m,n,*nidx,isz,val; 27865d0c19d7SBarry Smith const PetscInt *idx; 278797f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 2788f1af5d2fSBarry Smith PetscBT table; 2789bbd702dbSSatish Balay 27903a40ed3dSBarry Smith PetscFunctionBegin; 2791d0f46423SBarry Smith m = A->rmap->n; 2792e4d965acSSatish Balay ai = a->i; 2793bfeeae90SHong Zhang aj = a->j; 27948a047759SSatish Balay 2795e32f2f54SBarry Smith if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 279606763907SSatish Balay 2797854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr); 279853b8de81SBarry Smith ierr = PetscBTCreate(m,&table);CHKERRQ(ierr); 279906763907SSatish Balay 2800e4d965acSSatish Balay for (i=0; i<is_max; i++) { 2801b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 2802e4d965acSSatish Balay isz = 0; 28036831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 2804e4d965acSSatish Balay 2805e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 28064dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 2807b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 2808e4d965acSSatish Balay 2809dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 2810e4d965acSSatish Balay for (j=0; j<n; ++j) { 28112205254eSKarl Rupp if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j]; 28124dcbc457SBarry Smith } 281306763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 28146bf464f9SBarry Smith ierr = ISDestroy(&is[i]);CHKERRQ(ierr); 2815e4d965acSSatish Balay 281604a348a9SBarry Smith k = 0; 281704a348a9SBarry Smith for (j=0; j<ov; j++) { /* for each overlap */ 281804a348a9SBarry Smith n = isz; 281906763907SSatish Balay for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */ 2820e4d965acSSatish Balay row = nidx[k]; 2821e4d965acSSatish Balay start = ai[row]; 2822e4d965acSSatish Balay end = ai[row+1]; 282304a348a9SBarry Smith for (l = start; l<end; l++) { 2824efb16452SHong Zhang val = aj[l]; 28252205254eSKarl Rupp if (!PetscBTLookupSet(table,val)) nidx[isz++] = val; 2826e4d965acSSatish Balay } 2827e4d965acSSatish Balay } 2828e4d965acSSatish Balay } 282970b3c8c7SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr); 2830e4d965acSSatish Balay } 283194bacf5dSBarry Smith ierr = PetscBTDestroy(&table);CHKERRQ(ierr); 2832606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 28333a40ed3dSBarry Smith PetscFunctionReturn(0); 28344dcbc457SBarry Smith } 283517ab2063SBarry Smith 28360513a670SBarry Smith /* -------------------------------------------------------------- */ 2837dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 28380513a670SBarry Smith { 28390513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 28406849ba73SBarry Smith PetscErrorCode ierr; 28413b98c0a2SBarry Smith PetscInt i,nz = 0,m = A->rmap->n,n = A->cmap->n; 28425d0c19d7SBarry Smith const PetscInt *row,*col; 28435d0c19d7SBarry Smith PetscInt *cnew,j,*lens; 284456cd22aeSBarry Smith IS icolp,irowp; 28450298fd71SBarry Smith PetscInt *cwork = NULL; 28460298fd71SBarry Smith PetscScalar *vwork = NULL; 28470513a670SBarry Smith 28483a40ed3dSBarry Smith PetscFunctionBegin; 28494c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 285056cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 28514c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 285256cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 28530513a670SBarry Smith 28540513a670SBarry Smith /* determine lengths of permuted rows */ 2855854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr); 28562205254eSKarl Rupp for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i]; 2857ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 2858f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 285933d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 28607adad957SLisandro Dalcin ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2861ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 2862606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 28630513a670SBarry Smith 2864785e854fSJed Brown ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr); 28650513a670SBarry Smith for (i=0; i<m; i++) { 286632ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 28672205254eSKarl Rupp for (j=0; j<nz; j++) cnew[j] = col[cwork[j]]; 2868cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 286932ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 28700513a670SBarry Smith } 2871606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 28722205254eSKarl Rupp 28733c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 28742205254eSKarl Rupp 28759fe5e383SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 28769fe5e383SStefano Zampini ierr = MatPinToCPU(*B,A->pinnedtocpu);CHKERRQ(ierr); 28779fe5e383SStefano Zampini #endif 28780513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28790513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 288056cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 288156cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 28826bf464f9SBarry Smith ierr = ISDestroy(&irowp);CHKERRQ(ierr); 28836bf464f9SBarry Smith ierr = ISDestroy(&icolp);CHKERRQ(ierr); 28846768869dSprj- if (rowp == colp) { 28856768869dSprj- if (A->symmetric) { 28866768869dSprj- ierr = MatSetOption(*B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 28876768869dSprj- } 28886768869dSprj- if (A->hermitian) { 28896768869dSprj- ierr = MatSetOption(*B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 28906768869dSprj- } 28916768869dSprj- } 28923a40ed3dSBarry Smith PetscFunctionReturn(0); 28930513a670SBarry Smith } 28940513a670SBarry Smith 2895dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 2896cb5b572fSBarry Smith { 2897dfbe8321SBarry Smith PetscErrorCode ierr; 2898cb5b572fSBarry Smith 2899cb5b572fSBarry Smith PetscFunctionBegin; 290033f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 290133f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 2902be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2903be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 2904be6bf707SBarry Smith 2905700c5bfcSBarry 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"); 2906580bdb30SBarry Smith ierr = PetscArraycpy(b->a,a->a,a->i[A->rmap->n]);CHKERRQ(ierr); 2907cdc753b6SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 2908cb5b572fSBarry Smith } else { 2909cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2910cb5b572fSBarry Smith } 2911cb5b572fSBarry Smith PetscFunctionReturn(0); 2912cb5b572fSBarry Smith } 2913cb5b572fSBarry Smith 29144994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A) 2915273d9f13SBarry Smith { 2916dfbe8321SBarry Smith PetscErrorCode ierr; 2917273d9f13SBarry Smith 2918273d9f13SBarry Smith PetscFunctionBegin; 2919ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 2920273d9f13SBarry Smith PetscFunctionReturn(0); 2921273d9f13SBarry Smith } 2922273d9f13SBarry Smith 2923f38c1e66SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 29246c0721eeSBarry Smith { 29256c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 29266e111a19SKarl Rupp 29276c0721eeSBarry Smith PetscFunctionBegin; 29286c0721eeSBarry Smith *array = a->a; 29296c0721eeSBarry Smith PetscFunctionReturn(0); 29306c0721eeSBarry Smith } 29316c0721eeSBarry Smith 2932f38c1e66SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 29336c0721eeSBarry Smith { 29346c0721eeSBarry Smith PetscFunctionBegin; 2935f38c1e66SStefano Zampini *array = NULL; 29366c0721eeSBarry Smith PetscFunctionReturn(0); 29376c0721eeSBarry Smith } 2938273d9f13SBarry Smith 29398229c054SShri Abhyankar /* 29408229c054SShri Abhyankar Computes the number of nonzeros per row needed for preallocation when X and Y 29418229c054SShri Abhyankar have different nonzero structure. 29428229c054SShri Abhyankar */ 2943b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz) 2944ec7775f6SShri Abhyankar { 2945b264fe52SHong Zhang PetscInt i,j,k,nzx,nzy; 2946ec7775f6SShri Abhyankar 2947ec7775f6SShri Abhyankar PetscFunctionBegin; 2948ec7775f6SShri Abhyankar /* Set the number of nonzeros in the new matrix */ 2949ec7775f6SShri Abhyankar for (i=0; i<m; i++) { 2950b264fe52SHong Zhang const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i]; 2951b264fe52SHong Zhang nzx = xi[i+1] - xi[i]; 2952b264fe52SHong Zhang nzy = yi[i+1] - yi[i]; 29538af7cee1SJed Brown nnz[i] = 0; 29548af7cee1SJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 2955b264fe52SHong Zhang for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */ 2956b264fe52SHong Zhang if (k<nzy && yjj[k]==xjj[j]) k++; /* Skip duplicate */ 29578af7cee1SJed Brown nnz[i]++; 29588af7cee1SJed Brown } 29598af7cee1SJed Brown for (; k<nzy; k++) nnz[i]++; 2960ec7775f6SShri Abhyankar } 2961ec7775f6SShri Abhyankar PetscFunctionReturn(0); 2962ec7775f6SShri Abhyankar } 2963ec7775f6SShri Abhyankar 2964b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz) 2965b264fe52SHong Zhang { 2966b264fe52SHong Zhang PetscInt m = Y->rmap->N; 2967b264fe52SHong Zhang Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 2968b264fe52SHong Zhang Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 2969b264fe52SHong Zhang PetscErrorCode ierr; 2970b264fe52SHong Zhang 2971b264fe52SHong Zhang PetscFunctionBegin; 2972b264fe52SHong Zhang /* Set the number of nonzeros in the new matrix */ 2973b264fe52SHong Zhang ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr); 2974b264fe52SHong Zhang PetscFunctionReturn(0); 2975b264fe52SHong Zhang } 2976b264fe52SHong Zhang 2977f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2978ac90fabeSBarry Smith { 2979dfbe8321SBarry Smith PetscErrorCode ierr; 2980ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data; 2981c5df96a5SBarry Smith PetscBLASInt one=1,bnz; 2982ac90fabeSBarry Smith 2983ac90fabeSBarry Smith PetscFunctionBegin; 2984c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 2985ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2986f4df32b1SMatthew Knepley PetscScalar alpha = a; 29878b83055fSJed Brown PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2988acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr); 2989a3fa217bSJose E. Roman ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); 2990e2cf4d64SStefano Zampini /* the MatAXPY_Basic* subroutines calls MatAssembly, so the matrix on the GPU 2991e2cf4d64SStefano Zampini will be updated */ 2992e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2993c70f7ee4SJunchao Zhang if (Y->offloadmask != PETSC_OFFLOAD_UNALLOCATED) { 2994c70f7ee4SJunchao Zhang Y->offloadmask = PETSC_OFFLOAD_CPU; 2995e2cf4d64SStefano Zampini } 2996e2cf4d64SStefano Zampini #endif 2997ab784542SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 2998ab784542SHong Zhang ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 2999ac90fabeSBarry Smith } else { 30008229c054SShri Abhyankar Mat B; 30018229c054SShri Abhyankar PetscInt *nnz; 3002785e854fSJed Brown ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr); 3003ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr); 3004bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 30054aa94f47SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 300633d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr); 3007176df525SBarry Smith ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr); 30088229c054SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr); 3009ecd8bba6SJed Brown ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr); 3010ec7775f6SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 301128be2f97SBarry Smith ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr); 30128229c054SShri Abhyankar ierr = PetscFree(nnz);CHKERRQ(ierr); 3013ac90fabeSBarry Smith } 3014ac90fabeSBarry Smith PetscFunctionReturn(0); 3015ac90fabeSBarry Smith } 3016ac90fabeSBarry Smith 30177087cfbeSBarry Smith PetscErrorCode MatConjugate_SeqAIJ(Mat mat) 3018354c94deSBarry Smith { 3019354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 3020354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 3021354c94deSBarry Smith PetscInt i,nz; 3022354c94deSBarry Smith PetscScalar *a; 3023354c94deSBarry Smith 3024354c94deSBarry Smith PetscFunctionBegin; 3025354c94deSBarry Smith nz = aij->nz; 3026354c94deSBarry Smith a = aij->a; 30272205254eSKarl Rupp for (i=0; i<nz; i++) a[i] = PetscConj(a[i]); 3028e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 3029c70f7ee4SJunchao Zhang if (mat->offloadmask != PETSC_OFFLOAD_UNALLOCATED) mat->offloadmask = PETSC_OFFLOAD_CPU; 3030e2cf4d64SStefano Zampini #endif 3031354c94deSBarry Smith #else 3032354c94deSBarry Smith PetscFunctionBegin; 3033354c94deSBarry Smith #endif 3034354c94deSBarry Smith PetscFunctionReturn(0); 3035354c94deSBarry Smith } 3036354c94deSBarry Smith 3037985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3038e34fafa9SBarry Smith { 3039e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3040e34fafa9SBarry Smith PetscErrorCode ierr; 3041d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 3042e34fafa9SBarry Smith PetscReal atmp; 3043985db425SBarry Smith PetscScalar *x; 3044e34fafa9SBarry Smith MatScalar *aa; 3045e34fafa9SBarry Smith 3046e34fafa9SBarry Smith PetscFunctionBegin; 3047e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3048e34fafa9SBarry Smith aa = a->a; 3049e34fafa9SBarry Smith ai = a->i; 3050e34fafa9SBarry Smith aj = a->j; 3051e34fafa9SBarry Smith 3052985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 3053e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3054e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 3055e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 3056e34fafa9SBarry Smith for (i=0; i<m; i++) { 3057e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 30589189402eSHong Zhang x[i] = 0.0; 3059e34fafa9SBarry Smith for (j=0; j<ncols; j++) { 3060985db425SBarry Smith atmp = PetscAbsScalar(*aa); 3061985db425SBarry Smith if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 3062985db425SBarry Smith aa++; aj++; 3063985db425SBarry Smith } 3064985db425SBarry Smith } 3065985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3066985db425SBarry Smith PetscFunctionReturn(0); 3067985db425SBarry Smith } 3068985db425SBarry Smith 3069985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3070985db425SBarry Smith { 3071985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3072985db425SBarry Smith PetscErrorCode ierr; 3073d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 3074985db425SBarry Smith PetscScalar *x; 3075985db425SBarry Smith MatScalar *aa; 3076985db425SBarry Smith 3077985db425SBarry Smith PetscFunctionBegin; 3078e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3079985db425SBarry Smith aa = a->a; 3080985db425SBarry Smith ai = a->i; 3081985db425SBarry Smith aj = a->j; 3082985db425SBarry Smith 3083985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 3084985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3085985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 3086e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 3087985db425SBarry Smith for (i=0; i<m; i++) { 3088985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 3089d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 3090985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 3091985db425SBarry Smith } else { /* row is sparse so already KNOW maximum is 0.0 or higher */ 3092985db425SBarry Smith x[i] = 0.0; 3093985db425SBarry Smith if (idx) { 3094985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 3095985db425SBarry Smith for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */ 3096985db425SBarry Smith if (aj[j] > j) { 3097985db425SBarry Smith idx[i] = j; 3098985db425SBarry Smith break; 3099985db425SBarry Smith } 3100985db425SBarry Smith } 3101985db425SBarry Smith } 3102985db425SBarry Smith } 3103985db425SBarry Smith for (j=0; j<ncols; j++) { 3104985db425SBarry Smith if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 3105985db425SBarry Smith aa++; aj++; 3106985db425SBarry Smith } 3107985db425SBarry Smith } 3108985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3109985db425SBarry Smith PetscFunctionReturn(0); 3110985db425SBarry Smith } 3111985db425SBarry Smith 3112c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3113c87e5d42SMatthew Knepley { 3114c87e5d42SMatthew Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3115c87e5d42SMatthew Knepley PetscErrorCode ierr; 3116c87e5d42SMatthew Knepley PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 3117c87e5d42SMatthew Knepley PetscReal atmp; 3118c87e5d42SMatthew Knepley PetscScalar *x; 3119c87e5d42SMatthew Knepley MatScalar *aa; 3120c87e5d42SMatthew Knepley 3121c87e5d42SMatthew Knepley PetscFunctionBegin; 3122e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3123c87e5d42SMatthew Knepley aa = a->a; 3124c87e5d42SMatthew Knepley ai = a->i; 3125c87e5d42SMatthew Knepley aj = a->j; 3126c87e5d42SMatthew Knepley 3127c87e5d42SMatthew Knepley ierr = VecSet(v,0.0);CHKERRQ(ierr); 3128c87e5d42SMatthew Knepley ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3129c87e5d42SMatthew Knepley ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 313060e0710aSBarry 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); 3131c87e5d42SMatthew Knepley for (i=0; i<m; i++) { 3132c87e5d42SMatthew Knepley ncols = ai[1] - ai[0]; ai++; 3133289a08f5SMatthew Knepley if (ncols) { 3134289a08f5SMatthew Knepley /* Get first nonzero */ 3135289a08f5SMatthew Knepley for (j = 0; j < ncols; j++) { 3136289a08f5SMatthew Knepley atmp = PetscAbsScalar(aa[j]); 31372205254eSKarl Rupp if (atmp > 1.0e-12) { 31382205254eSKarl Rupp x[i] = atmp; 31392205254eSKarl Rupp if (idx) idx[i] = aj[j]; 31402205254eSKarl Rupp break; 31412205254eSKarl Rupp } 3142289a08f5SMatthew Knepley } 314312431cb0SMatthew G Knepley if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;} 3144289a08f5SMatthew Knepley } else { 3145289a08f5SMatthew Knepley x[i] = 0.0; if (idx) idx[i] = 0; 3146289a08f5SMatthew Knepley } 3147c87e5d42SMatthew Knepley for (j = 0; j < ncols; j++) { 3148c87e5d42SMatthew Knepley atmp = PetscAbsScalar(*aa); 3149289a08f5SMatthew Knepley if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 3150c87e5d42SMatthew Knepley aa++; aj++; 3151c87e5d42SMatthew Knepley } 3152c87e5d42SMatthew Knepley } 3153c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3154c87e5d42SMatthew Knepley PetscFunctionReturn(0); 3155c87e5d42SMatthew Knepley } 3156c87e5d42SMatthew Knepley 3157985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3158985db425SBarry Smith { 3159985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3160985db425SBarry Smith PetscErrorCode ierr; 3161d9ca1df4SBarry Smith PetscInt i,j,m = A->rmap->n,ncols,n; 3162d9ca1df4SBarry Smith const PetscInt *ai,*aj; 3163985db425SBarry Smith PetscScalar *x; 3164d9ca1df4SBarry Smith const MatScalar *aa; 3165985db425SBarry Smith 3166985db425SBarry Smith PetscFunctionBegin; 3167e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3168985db425SBarry Smith aa = a->a; 3169985db425SBarry Smith ai = a->i; 3170985db425SBarry Smith aj = a->j; 3171985db425SBarry Smith 3172985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 3173985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3174985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 3175e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 3176985db425SBarry Smith for (i=0; i<m; i++) { 3177985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 3178d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 3179985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 3180985db425SBarry Smith } else { /* row is sparse so already KNOW minimum is 0.0 or lower */ 3181985db425SBarry Smith x[i] = 0.0; 3182985db425SBarry Smith if (idx) { /* find first implicit 0.0 in the row */ 3183985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 3184985db425SBarry Smith for (j=0; j<ncols; j++) { 3185985db425SBarry Smith if (aj[j] > j) { 3186985db425SBarry Smith idx[i] = j; 3187985db425SBarry Smith break; 3188985db425SBarry Smith } 3189985db425SBarry Smith } 3190985db425SBarry Smith } 3191985db425SBarry Smith } 3192985db425SBarry Smith for (j=0; j<ncols; j++) { 3193985db425SBarry Smith if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 3194985db425SBarry Smith aa++; aj++; 3195e34fafa9SBarry Smith } 3196e34fafa9SBarry Smith } 3197e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3198e34fafa9SBarry Smith PetscFunctionReturn(0); 3199e34fafa9SBarry Smith } 3200bbead8a2SBarry Smith 3201713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values) 3202bbead8a2SBarry Smith { 3203bbead8a2SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 3204bbead8a2SBarry Smith PetscErrorCode ierr; 320533d57670SJed Brown PetscInt i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j; 3206bbead8a2SBarry Smith MatScalar *diag,work[25],*v_work; 32070da83c2eSBarry Smith const PetscReal shift = 0.0; 32081a9391e3SHong Zhang PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE; 3209bbead8a2SBarry Smith 3210bbead8a2SBarry Smith PetscFunctionBegin; 3211a455e926SHong Zhang allowzeropivot = PetscNot(A->erroriffailure); 32124a0d0026SBarry Smith if (a->ibdiagvalid) { 32134a0d0026SBarry Smith if (values) *values = a->ibdiag; 32144a0d0026SBarry Smith PetscFunctionReturn(0); 32154a0d0026SBarry Smith } 3216bbead8a2SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 3217bbead8a2SBarry Smith if (!a->ibdiag) { 3218785e854fSJed Brown ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr); 32193bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr); 3220bbead8a2SBarry Smith } 3221bbead8a2SBarry Smith diag = a->ibdiag; 3222bbead8a2SBarry Smith if (values) *values = a->ibdiag; 3223bbead8a2SBarry Smith /* factor and invert each block */ 3224bbead8a2SBarry Smith switch (bs) { 3225bbead8a2SBarry Smith case 1: 3226bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3227bbead8a2SBarry Smith ierr = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr); 3228ec1892c8SHong Zhang if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) { 3229ec1892c8SHong Zhang if (allowzeropivot) { 32307b6c816cSBarry Smith A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 32317b6c816cSBarry Smith A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]); 32327b6c816cSBarry Smith A->factorerror_zeropivot_row = i; 32337b6c816cSBarry Smith ierr = PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);CHKERRQ(ierr); 32347b6c816cSBarry 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); 3235ec1892c8SHong Zhang } 3236bbead8a2SBarry Smith diag[i] = (PetscScalar)1.0 / (diag[i] + shift); 3237bbead8a2SBarry Smith } 3238bbead8a2SBarry Smith break; 3239bbead8a2SBarry Smith case 2: 3240bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3241bbead8a2SBarry Smith ij[0] = 2*i; ij[1] = 2*i + 1; 3242bbead8a2SBarry Smith ierr = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr); 3243a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32447b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 324596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr); 3246bbead8a2SBarry Smith diag += 4; 3247bbead8a2SBarry Smith } 3248bbead8a2SBarry Smith break; 3249bbead8a2SBarry Smith case 3: 3250bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3251bbead8a2SBarry Smith ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2; 3252bbead8a2SBarry Smith ierr = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr); 3253a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32547b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 325596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr); 3256bbead8a2SBarry Smith diag += 9; 3257bbead8a2SBarry Smith } 3258bbead8a2SBarry Smith break; 3259bbead8a2SBarry Smith case 4: 3260bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3261bbead8a2SBarry Smith ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3; 3262bbead8a2SBarry Smith ierr = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr); 3263a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32647b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 326596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr); 3266bbead8a2SBarry Smith diag += 16; 3267bbead8a2SBarry Smith } 3268bbead8a2SBarry Smith break; 3269bbead8a2SBarry Smith case 5: 3270bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3271bbead8a2SBarry 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; 3272bbead8a2SBarry Smith ierr = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr); 3273a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32747b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 327596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr); 3276bbead8a2SBarry Smith diag += 25; 3277bbead8a2SBarry Smith } 3278bbead8a2SBarry Smith break; 3279bbead8a2SBarry Smith case 6: 3280bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3281bbead8a2SBarry 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; 3282bbead8a2SBarry Smith ierr = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr); 3283a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32847b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 328596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr); 3286bbead8a2SBarry Smith diag += 36; 3287bbead8a2SBarry Smith } 3288bbead8a2SBarry Smith break; 3289bbead8a2SBarry Smith case 7: 3290bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3291bbead8a2SBarry 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; 3292bbead8a2SBarry Smith ierr = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr); 3293a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32947b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 329596b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr); 3296bbead8a2SBarry Smith diag += 49; 3297bbead8a2SBarry Smith } 3298bbead8a2SBarry Smith break; 3299bbead8a2SBarry Smith default: 3300dcca6d9dSJed Brown ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr); 3301bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3302bbead8a2SBarry Smith for (j=0; j<bs; j++) { 3303bbead8a2SBarry Smith IJ[j] = bs*i + j; 3304bbead8a2SBarry Smith } 3305bbead8a2SBarry Smith ierr = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr); 33065f8bbccaSHong Zhang ierr = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 33077b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 330896b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr); 3309bbead8a2SBarry Smith diag += bs2; 3310bbead8a2SBarry Smith } 3311bbead8a2SBarry Smith ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr); 3312bbead8a2SBarry Smith } 3313bbead8a2SBarry Smith a->ibdiagvalid = PETSC_TRUE; 3314bbead8a2SBarry Smith PetscFunctionReturn(0); 3315bbead8a2SBarry Smith } 3316bbead8a2SBarry Smith 331773a71a0fSBarry Smith static PetscErrorCode MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx) 331873a71a0fSBarry Smith { 331973a71a0fSBarry Smith PetscErrorCode ierr; 332073a71a0fSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data; 332173a71a0fSBarry Smith PetscScalar a; 332273a71a0fSBarry Smith PetscInt m,n,i,j,col; 332373a71a0fSBarry Smith 332473a71a0fSBarry Smith PetscFunctionBegin; 332573a71a0fSBarry Smith if (!x->assembled) { 332673a71a0fSBarry Smith ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr); 332773a71a0fSBarry Smith for (i=0; i<m; i++) { 332873a71a0fSBarry Smith for (j=0; j<aij->imax[i]; j++) { 332973a71a0fSBarry Smith ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr); 333073a71a0fSBarry Smith col = (PetscInt)(n*PetscRealPart(a)); 333173a71a0fSBarry Smith ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr); 333273a71a0fSBarry Smith } 333373a71a0fSBarry Smith } 3334e2ce353bSJunchao Zhang } else { 3335e2ce353bSJunchao Zhang for (i=0; i<aij->nz; i++) {ierr = PetscRandomGetValue(rctx,aij->a+i);CHKERRQ(ierr);} 3336e2ce353bSJunchao Zhang } 333773a71a0fSBarry Smith ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 333873a71a0fSBarry Smith ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 333973a71a0fSBarry Smith PetscFunctionReturn(0); 334073a71a0fSBarry Smith } 334173a71a0fSBarry Smith 3342679944adSJunchao Zhang /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */ 3343679944adSJunchao Zhang PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx) 3344679944adSJunchao Zhang { 3345679944adSJunchao Zhang PetscErrorCode ierr; 3346679944adSJunchao Zhang Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data; 3347679944adSJunchao Zhang PetscScalar a; 3348679944adSJunchao Zhang PetscInt m,n,i,j,col,nskip; 3349679944adSJunchao Zhang 3350679944adSJunchao Zhang PetscFunctionBegin; 3351679944adSJunchao Zhang nskip = high - low; 3352679944adSJunchao Zhang ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr); 3353679944adSJunchao Zhang n -= nskip; /* shrink number of columns where nonzeros can be set */ 3354679944adSJunchao Zhang for (i=0; i<m; i++) { 3355679944adSJunchao Zhang for (j=0; j<aij->imax[i]; j++) { 3356679944adSJunchao Zhang ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr); 3357679944adSJunchao Zhang col = (PetscInt)(n*PetscRealPart(a)); 3358679944adSJunchao Zhang if (col >= low) col += nskip; /* shift col rightward to skip the hole */ 3359679944adSJunchao Zhang ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr); 3360679944adSJunchao Zhang } 3361e2ce353bSJunchao Zhang } 3362679944adSJunchao Zhang ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3363679944adSJunchao Zhang ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3364679944adSJunchao Zhang PetscFunctionReturn(0); 3365679944adSJunchao Zhang } 3366679944adSJunchao Zhang 3367679944adSJunchao Zhang 3368682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 33690a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ, 3370cb5b572fSBarry Smith MatGetRow_SeqAIJ, 3371cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 3372cb5b572fSBarry Smith MatMult_SeqAIJ, 337397304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 33747c922b88SBarry Smith MatMultTranspose_SeqAIJ, 33757c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 3376db4efbfdSBarry Smith 0, 3377db4efbfdSBarry Smith 0, 3378db4efbfdSBarry Smith 0, 3379db4efbfdSBarry Smith /* 10*/ 0, 3380cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 3381cb5b572fSBarry Smith 0, 338241f059aeSBarry Smith MatSOR_SeqAIJ, 338391e9d3e2SHong Zhang MatTranspose_SeqAIJ, 338497304618SKris Buschelman /*1 5*/ MatGetInfo_SeqAIJ, 3385cb5b572fSBarry Smith MatEqual_SeqAIJ, 3386cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 3387cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 3388cb5b572fSBarry Smith MatNorm_SeqAIJ, 338997304618SKris Buschelman /* 20*/ 0, 3390cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 3391cb5b572fSBarry Smith MatSetOption_SeqAIJ, 3392cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 3393d519adbfSMatthew Knepley /* 24*/ MatZeroRows_SeqAIJ, 3394db4efbfdSBarry Smith 0, 3395db4efbfdSBarry Smith 0, 3396db4efbfdSBarry Smith 0, 3397db4efbfdSBarry Smith 0, 33984994cf47SJed Brown /* 29*/ MatSetUp_SeqAIJ, 3399db4efbfdSBarry Smith 0, 3400db4efbfdSBarry Smith 0, 34018c778c55SBarry Smith 0, 34028c778c55SBarry Smith 0, 3403d519adbfSMatthew Knepley /* 34*/ MatDuplicate_SeqAIJ, 3404cb5b572fSBarry Smith 0, 3405cb5b572fSBarry Smith 0, 3406cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 3407cb5b572fSBarry Smith 0, 3408d519adbfSMatthew Knepley /* 39*/ MatAXPY_SeqAIJ, 34097dae84e0SHong Zhang MatCreateSubMatrices_SeqAIJ, 3410cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 3411cb5b572fSBarry Smith MatGetValues_SeqAIJ, 3412cb5b572fSBarry Smith MatCopy_SeqAIJ, 3413d519adbfSMatthew Knepley /* 44*/ MatGetRowMax_SeqAIJ, 3414cb5b572fSBarry Smith MatScale_SeqAIJ, 34157d68702bSBarry Smith MatShift_SeqAIJ, 341679299369SBarry Smith MatDiagonalSet_SeqAIJ, 34176e169961SBarry Smith MatZeroRowsColumns_SeqAIJ, 341873a71a0fSBarry Smith /* 49*/ MatSetRandom_SeqAIJ, 34193b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 34203b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 34213b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 3422a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 342393dfae19SHong Zhang /* 54*/ MatFDColoringCreate_SeqXAIJ, 3424b9617806SBarry Smith 0, 34250513a670SBarry Smith 0, 3426cda55fadSBarry Smith MatPermute_SeqAIJ, 3427cda55fadSBarry Smith 0, 3428d519adbfSMatthew Knepley /* 59*/ 0, 3429b9b97703SBarry Smith MatDestroy_SeqAIJ, 3430b9b97703SBarry Smith MatView_SeqAIJ, 3431357abbc8SBarry Smith 0, 3432321b30b9SSatish Balay MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ, 3433321b30b9SSatish Balay /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ, 3434321b30b9SSatish Balay MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ, 3435ee4f033dSBarry Smith 0, 3436ee4f033dSBarry Smith 0, 3437ee4f033dSBarry Smith 0, 3438d519adbfSMatthew Knepley /* 69*/ MatGetRowMaxAbs_SeqAIJ, 3439c87e5d42SMatthew Knepley MatGetRowMinAbs_SeqAIJ, 3440ee4f033dSBarry Smith 0, 3441dcf5cc72SBarry Smith 0, 34422c93a97aSBarry Smith 0, 34432c93a97aSBarry Smith /* 74*/ 0, 34443acb8795SBarry Smith MatFDColoringApply_AIJ, 344597304618SKris Buschelman 0, 344697304618SKris Buschelman 0, 344797304618SKris Buschelman 0, 34486ce1633cSBarry Smith /* 79*/ MatFindZeroDiagonals_SeqAIJ, 344997304618SKris Buschelman 0, 345097304618SKris Buschelman 0, 345197304618SKris Buschelman 0, 3452bc011b1eSHong Zhang MatLoad_SeqAIJ, 3453d519adbfSMatthew Knepley /* 84*/ MatIsSymmetric_SeqAIJ, 34541cbb95d3SBarry Smith MatIsHermitian_SeqAIJ, 34556284ec50SHong Zhang 0, 34566284ec50SHong Zhang 0, 3457bc011b1eSHong Zhang 0, 3458d519adbfSMatthew Knepley /* 89*/ MatMatMult_SeqAIJ_SeqAIJ, 345926be0446SHong Zhang MatMatMultSymbolic_SeqAIJ_SeqAIJ, 346026be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 346165e8a0caSHong Zhang MatPtAP_SeqAIJ_SeqAIJ, 34628fa4b5a6SHong Zhang MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy, 34638fa4b5a6SHong Zhang /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy, 34646fc122caSHong Zhang MatMatTransposeMult_SeqAIJ_SeqAIJ, 34656fc122caSHong Zhang MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ, 34666fc122caSHong Zhang MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ, 34672121bac1SHong Zhang 0, 34682121bac1SHong Zhang /* 99*/ 0, 3469609c6c4dSKris Buschelman 0, 3470609c6c4dSKris Buschelman 0, 347187d4246cSBarry Smith MatConjugate_SeqAIJ, 347287d4246cSBarry Smith 0, 3473d519adbfSMatthew Knepley /*104*/ MatSetValuesRow_SeqAIJ, 347499cafbc1SBarry Smith MatRealPart_SeqAIJ, 3475f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 3476f5edf698SHong Zhang 0, 34772bebee5dSHong Zhang 0, 3478cbd44569SHong Zhang /*109*/ MatMatSolve_SeqAIJ, 3479985db425SBarry Smith 0, 34802af78befSBarry Smith MatGetRowMin_SeqAIJ, 34812af78befSBarry Smith 0, 3482599ef60dSHong Zhang MatMissingDiagonal_SeqAIJ, 3483d519adbfSMatthew Knepley /*114*/ 0, 3484599ef60dSHong Zhang 0, 34853c2a7987SHong Zhang 0, 3486fe97e370SBarry Smith 0, 3487fbdbba38SShri Abhyankar 0, 3488fbdbba38SShri Abhyankar /*119*/ 0, 3489fbdbba38SShri Abhyankar 0, 3490fbdbba38SShri Abhyankar 0, 349182d44351SHong Zhang 0, 3492b3a44c85SBarry Smith MatGetMultiProcBlock_SeqAIJ, 34930716a85fSBarry Smith /*124*/ MatFindNonzeroRows_SeqAIJ, 3494bbead8a2SBarry Smith MatGetColumnNorms_SeqAIJ, 349537868618SMatthew G Knepley MatInvertBlockDiagonal_SeqAIJ, 34960da83c2eSBarry Smith MatInvertVariableBlockDiagonal_SeqAIJ, 349737868618SMatthew G Knepley 0, 34985df89d91SHong Zhang /*129*/ 0, 349975648e8dSHong Zhang MatTransposeMatMult_SeqAIJ_SeqAIJ, 350075648e8dSHong Zhang MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ, 350175648e8dSHong Zhang MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ, 3502b9af6bddSHong Zhang MatTransposeColoringCreate_SeqAIJ, 3503b9af6bddSHong Zhang /*134*/ MatTransColoringApplySpToDen_SeqAIJ, 35042b8ad9a3SHong Zhang MatTransColoringApplyDenToSp_SeqAIJ, 35052b8ad9a3SHong Zhang MatRARt_SeqAIJ_SeqAIJ, 35062b8ad9a3SHong Zhang MatRARtSymbolic_SeqAIJ_SeqAIJ, 35073964eb88SJed Brown MatRARtNumeric_SeqAIJ_SeqAIJ, 35083964eb88SJed Brown /*139*/0, 3509f9426fe0SMark Adams 0, 35101919a2e2SJed Brown 0, 35113a062f41SBarry Smith MatFDColoringSetUp_SeqXAIJ, 35129c8f2541SHong Zhang MatFindOffBlockDiagonalEntries_SeqAIJ, 35132d033e1fSHong Zhang /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ, 35142d033e1fSHong Zhang MatDestroySubMatrices_SeqAIJ 35159e29f15eSvictorle }; 351617ab2063SBarry Smith 35177087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 3518bef8e0ddSBarry Smith { 3519bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 352097f1f81fSBarry Smith PetscInt i,nz,n; 3521bef8e0ddSBarry Smith 3522bef8e0ddSBarry Smith PetscFunctionBegin; 3523bef8e0ddSBarry Smith nz = aij->maxnz; 3524d0f46423SBarry Smith n = mat->rmap->n; 3525bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 3526bef8e0ddSBarry Smith aij->j[i] = indices[i]; 3527bef8e0ddSBarry Smith } 3528bef8e0ddSBarry Smith aij->nz = nz; 3529bef8e0ddSBarry Smith for (i=0; i<n; i++) { 3530bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 3531bef8e0ddSBarry Smith } 3532bef8e0ddSBarry Smith PetscFunctionReturn(0); 3533bef8e0ddSBarry Smith } 3534bef8e0ddSBarry Smith 3535a3bb6f32SFande Kong /* 3536e8b528d9SFande Kong * When a sparse matrix has many zero columns, we should compact them out to save the space 3537a3bb6f32SFande Kong * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable() 3538a3bb6f32SFande Kong * */ 3539a3bb6f32SFande Kong PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping) 3540a3bb6f32SFande Kong { 3541a3bb6f32SFande Kong Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 3542a3bb6f32SFande Kong PetscTable gid1_lid1; 3543a3bb6f32SFande Kong PetscTablePosition tpos; 3544a3bb6f32SFande Kong PetscInt gid,lid,i,j,ncols,ec; 3545a3bb6f32SFande Kong PetscInt *garray; 3546a3bb6f32SFande Kong PetscErrorCode ierr; 3547a3bb6f32SFande Kong 3548a3bb6f32SFande Kong PetscFunctionBegin; 3549a3bb6f32SFande Kong PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3550a3bb6f32SFande Kong PetscValidPointer(mapping,2); 3551a3bb6f32SFande Kong /* use a table */ 3552a3bb6f32SFande Kong ierr = PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);CHKERRQ(ierr); 3553a3bb6f32SFande Kong ec = 0; 3554a3bb6f32SFande Kong for (i=0; i<mat->rmap->n; i++) { 3555a3bb6f32SFande Kong ncols = aij->i[i+1] - aij->i[i]; 3556a3bb6f32SFande Kong for (j=0; j<ncols; j++) { 3557a3bb6f32SFande Kong PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1; 3558a3bb6f32SFande Kong ierr = PetscTableFind(gid1_lid1,gid1,&data);CHKERRQ(ierr); 3559a3bb6f32SFande Kong if (!data) { 3560a3bb6f32SFande Kong /* one based table */ 3561a3bb6f32SFande Kong ierr = PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);CHKERRQ(ierr); 3562a3bb6f32SFande Kong } 3563a3bb6f32SFande Kong } 3564a3bb6f32SFande Kong } 3565a3bb6f32SFande Kong /* form array of columns we need */ 3566a3bb6f32SFande Kong ierr = PetscMalloc1(ec+1,&garray);CHKERRQ(ierr); 3567a3bb6f32SFande Kong ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr); 3568a3bb6f32SFande Kong while (tpos) { 3569a3bb6f32SFande Kong ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);CHKERRQ(ierr); 3570a3bb6f32SFande Kong gid--; 3571a3bb6f32SFande Kong lid--; 3572a3bb6f32SFande Kong garray[lid] = gid; 3573a3bb6f32SFande Kong } 3574a3bb6f32SFande Kong ierr = PetscSortInt(ec,garray);CHKERRQ(ierr); /* sort, and rebuild */ 3575a3bb6f32SFande Kong ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr); 3576a3bb6f32SFande Kong for (i=0; i<ec; i++) { 3577a3bb6f32SFande Kong ierr = PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr); 3578a3bb6f32SFande Kong } 3579a3bb6f32SFande Kong /* compact out the extra columns in B */ 3580a3bb6f32SFande Kong for (i=0; i<mat->rmap->n; i++) { 3581a3bb6f32SFande Kong ncols = aij->i[i+1] - aij->i[i]; 3582a3bb6f32SFande Kong for (j=0; j<ncols; j++) { 3583a3bb6f32SFande Kong PetscInt gid1 = aij->j[aij->i[i] + j] + 1; 3584a3bb6f32SFande Kong ierr = PetscTableFind(gid1_lid1,gid1,&lid);CHKERRQ(ierr); 3585a3bb6f32SFande Kong lid--; 3586a3bb6f32SFande Kong aij->j[aij->i[i] + j] = lid; 3587a3bb6f32SFande Kong } 3588a3bb6f32SFande Kong } 3589ca5434daSLawrence Mitchell ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr); 3590ca5434daSLawrence Mitchell ierr = PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat),ec,ec,1,&mat->cmap);CHKERRQ(ierr); 3591a3bb6f32SFande Kong ierr = PetscTableDestroy(&gid1_lid1);CHKERRQ(ierr); 3592a3bb6f32SFande Kong ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr); 3593a3bb6f32SFande Kong ierr = ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr); 3594a3bb6f32SFande Kong PetscFunctionReturn(0); 3595a3bb6f32SFande Kong } 3596a3bb6f32SFande Kong 3597bef8e0ddSBarry Smith /*@ 3598bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 3599bef8e0ddSBarry Smith in the matrix. 3600bef8e0ddSBarry Smith 3601bef8e0ddSBarry Smith Input Parameters: 3602bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 3603bef8e0ddSBarry Smith - indices - the column indices 3604bef8e0ddSBarry Smith 360515091d37SBarry Smith Level: advanced 360615091d37SBarry Smith 3607bef8e0ddSBarry Smith Notes: 3608bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 3609bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 3610bef8e0ddSBarry Smith of the MatSetValues() operation. 3611bef8e0ddSBarry Smith 3612bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 3613d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 3614bef8e0ddSBarry Smith 3615bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 3616bef8e0ddSBarry Smith 3617b9617806SBarry Smith The indices should start with zero, not one. 3618b9617806SBarry Smith 3619bef8e0ddSBarry Smith @*/ 36207087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 3621bef8e0ddSBarry Smith { 36224ac538c5SBarry Smith PetscErrorCode ierr; 3623bef8e0ddSBarry Smith 3624bef8e0ddSBarry Smith PetscFunctionBegin; 36250700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 36264482741eSBarry Smith PetscValidPointer(indices,2); 36274ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr); 3628bef8e0ddSBarry Smith PetscFunctionReturn(0); 3629bef8e0ddSBarry Smith } 3630bef8e0ddSBarry Smith 3631be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 3632be6bf707SBarry Smith 36337087cfbeSBarry Smith PetscErrorCode MatStoreValues_SeqAIJ(Mat mat) 3634be6bf707SBarry Smith { 3635be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 36366849ba73SBarry Smith PetscErrorCode ierr; 3637d0f46423SBarry Smith size_t nz = aij->i[mat->rmap->n]; 3638be6bf707SBarry Smith 3639be6bf707SBarry Smith PetscFunctionBegin; 3640169f6850SBarry Smith if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3641be6bf707SBarry Smith 3642be6bf707SBarry Smith /* allocate space for values if not already there */ 3643be6bf707SBarry Smith if (!aij->saved_values) { 3644854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr); 36453bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr); 3646be6bf707SBarry Smith } 3647be6bf707SBarry Smith 3648be6bf707SBarry Smith /* copy values over */ 3649580bdb30SBarry Smith ierr = PetscArraycpy(aij->saved_values,aij->a,nz);CHKERRQ(ierr); 3650be6bf707SBarry Smith PetscFunctionReturn(0); 3651be6bf707SBarry Smith } 3652be6bf707SBarry Smith 3653be6bf707SBarry Smith /*@ 3654be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 3655be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3656be6bf707SBarry Smith nonlinear portion. 3657be6bf707SBarry Smith 3658be6bf707SBarry Smith Collect on Mat 3659be6bf707SBarry Smith 3660be6bf707SBarry Smith Input Parameters: 36610e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 3662be6bf707SBarry Smith 366315091d37SBarry Smith Level: advanced 366415091d37SBarry Smith 3665be6bf707SBarry Smith Common Usage, with SNESSolve(): 3666be6bf707SBarry Smith $ Create Jacobian matrix 3667be6bf707SBarry Smith $ Set linear terms into matrix 3668be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 3669be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 3670be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 3671512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3672be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3673be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 3674be6bf707SBarry Smith $ In your Jacobian routine 3675be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3676be6bf707SBarry Smith $ Set nonlinear terms in matrix 3677be6bf707SBarry Smith 3678be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 3679be6bf707SBarry Smith $ // build linear portion of Jacobian 3680512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3681be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3682be6bf707SBarry Smith $ loop over nonlinear iterations 3683be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3684be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 3685be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 3686be6bf707SBarry Smith $ Solve linear system with Jacobian 3687be6bf707SBarry Smith $ endloop 3688be6bf707SBarry Smith 3689be6bf707SBarry Smith Notes: 3690be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 3691512a5fc5SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before 3692be6bf707SBarry Smith calling this routine. 3693be6bf707SBarry Smith 36940c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 36950c468ba9SBarry Smith and does not allocated additional space. 36960c468ba9SBarry Smith 3697be6bf707SBarry Smith .seealso: MatRetrieveValues() 3698be6bf707SBarry Smith 3699be6bf707SBarry Smith @*/ 37007087cfbeSBarry Smith PetscErrorCode MatStoreValues(Mat mat) 3701be6bf707SBarry Smith { 37024ac538c5SBarry Smith PetscErrorCode ierr; 3703be6bf707SBarry Smith 3704be6bf707SBarry Smith PetscFunctionBegin; 37050700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3706e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3707e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 37084ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr); 3709be6bf707SBarry Smith PetscFunctionReturn(0); 3710be6bf707SBarry Smith } 3711be6bf707SBarry Smith 37127087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat) 3713be6bf707SBarry Smith { 3714be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 37156849ba73SBarry Smith PetscErrorCode ierr; 3716d0f46423SBarry Smith PetscInt nz = aij->i[mat->rmap->n]; 3717be6bf707SBarry Smith 3718be6bf707SBarry Smith PetscFunctionBegin; 3719169f6850SBarry Smith if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3720f23aa3ddSBarry Smith if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 3721be6bf707SBarry Smith /* copy values over */ 3722580bdb30SBarry Smith ierr = PetscArraycpy(aij->a,aij->saved_values,nz);CHKERRQ(ierr); 3723be6bf707SBarry Smith PetscFunctionReturn(0); 3724be6bf707SBarry Smith } 3725be6bf707SBarry Smith 3726be6bf707SBarry Smith /*@ 3727be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 3728be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3729be6bf707SBarry Smith nonlinear portion. 3730be6bf707SBarry Smith 3731be6bf707SBarry Smith Collect on Mat 3732be6bf707SBarry Smith 3733be6bf707SBarry Smith Input Parameters: 3734386f7cf9SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 3735be6bf707SBarry Smith 373615091d37SBarry Smith Level: advanced 373715091d37SBarry Smith 3738be6bf707SBarry Smith .seealso: MatStoreValues() 3739be6bf707SBarry Smith 3740be6bf707SBarry Smith @*/ 37417087cfbeSBarry Smith PetscErrorCode MatRetrieveValues(Mat mat) 3742be6bf707SBarry Smith { 37434ac538c5SBarry Smith PetscErrorCode ierr; 3744be6bf707SBarry Smith 3745be6bf707SBarry Smith PetscFunctionBegin; 37460700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3747e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3748e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 37494ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr); 3750be6bf707SBarry Smith PetscFunctionReturn(0); 3751be6bf707SBarry Smith } 3752be6bf707SBarry Smith 3753f83d6046SBarry Smith 3754be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 375517ab2063SBarry Smith /*@C 3756682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 37570d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 37586e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 375951c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 37602bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 376117ab2063SBarry Smith 3762d083f849SBarry Smith Collective 3763db81eaa0SLois Curfman McInnes 376417ab2063SBarry Smith Input Parameters: 3765db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 376617ab2063SBarry Smith . m - number of rows 376717ab2063SBarry Smith . n - number of columns 376817ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 376951c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 37700298fd71SBarry Smith (possibly different for each row) or NULL 377117ab2063SBarry Smith 377217ab2063SBarry Smith Output Parameter: 3773416022c9SBarry Smith . A - the matrix 377417ab2063SBarry Smith 3775175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3776f6f02116SRichard Tran Mills MatXXXXSetPreallocation() paradigm instead of this routine directly. 3777175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3778175b88e8SBarry Smith 3779b259b22eSLois Curfman McInnes Notes: 378049a6f317SBarry Smith If nnz is given then nz is ignored 378149a6f317SBarry Smith 378217ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 378317ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 37840002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 378544cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 378617ab2063SBarry Smith 378717ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 37880298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 37893d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 37906da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 379117ab2063SBarry Smith 3792682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 37934fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 3794682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 37956c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 37966c7ebb05SLois Curfman McInnes 37976c7ebb05SLois Curfman McInnes Options Database Keys: 3798698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 37999db58ca8SBarry Smith - -mat_inode_limit <limit> - Sets inode limit (max limit=5) 380017ab2063SBarry Smith 3801027ccd11SLois Curfman McInnes Level: intermediate 3802027ccd11SLois Curfman McInnes 380369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 380436db0b34SBarry Smith 380517ab2063SBarry Smith @*/ 38067087cfbeSBarry Smith PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 380717ab2063SBarry Smith { 3808dfbe8321SBarry Smith PetscErrorCode ierr; 38096945ee14SBarry Smith 38103a40ed3dSBarry Smith PetscFunctionBegin; 3811f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 3812117016b1SBarry Smith ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 3813c4752a88SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 3814d28bb7d2SJed Brown ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 3815273d9f13SBarry Smith PetscFunctionReturn(0); 3816273d9f13SBarry Smith } 3817273d9f13SBarry Smith 3818273d9f13SBarry Smith /*@C 3819273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 3820273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 3821273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 3822273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 3823273d9f13SBarry Smith 3824d083f849SBarry Smith Collective 3825273d9f13SBarry Smith 3826273d9f13SBarry Smith Input Parameters: 38271c4f3114SJed Brown + B - The matrix 3828273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 3829273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 38300298fd71SBarry Smith (possibly different for each row) or NULL 3831273d9f13SBarry Smith 3832273d9f13SBarry Smith Notes: 383349a6f317SBarry Smith If nnz is given then nz is ignored 383449a6f317SBarry Smith 3835273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3836273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 3837273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 3838273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 3839273d9f13SBarry Smith 3840273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 38410298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 3842273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 3843273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 3844273d9f13SBarry Smith 3845aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3846aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3847aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3848aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3849aa95bbe8SBarry Smith 3850a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 3851a96a251dSBarry Smith entries or columns indices 3852a96a251dSBarry Smith 3853273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 3854273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 3855273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 3856273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3857273d9f13SBarry Smith 3858273d9f13SBarry Smith Options Database Keys: 3859698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 386047b2e64bSBarry Smith - -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3861273d9f13SBarry Smith 3862273d9f13SBarry Smith Level: intermediate 3863273d9f13SBarry Smith 386469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo() 3865273d9f13SBarry Smith 3866273d9f13SBarry Smith @*/ 38677087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 3868273d9f13SBarry Smith { 38694ac538c5SBarry Smith PetscErrorCode ierr; 3870a23d5eceSKris Buschelman 3871a23d5eceSKris Buschelman PetscFunctionBegin; 38726ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 38736ba663aaSJed Brown PetscValidType(B,1); 38744ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr); 3875a23d5eceSKris Buschelman PetscFunctionReturn(0); 3876a23d5eceSKris Buschelman } 3877a23d5eceSKris Buschelman 38787087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz) 3879a23d5eceSKris Buschelman { 3880273d9f13SBarry Smith Mat_SeqAIJ *b; 38812576faa2SJed Brown PetscBool skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE; 38826849ba73SBarry Smith PetscErrorCode ierr; 388397f1f81fSBarry Smith PetscInt i; 3884273d9f13SBarry Smith 3885273d9f13SBarry Smith PetscFunctionBegin; 38862576faa2SJed Brown if (nz >= 0 || nnz) realalloc = PETSC_TRUE; 3887a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 3888c461c341SBarry Smith skipallocation = PETSC_TRUE; 3889c461c341SBarry Smith nz = 0; 3890c461c341SBarry Smith } 389126283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 389226283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3893899cda47SBarry Smith 3894435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 389560e0710aSBarry Smith if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz); 3896071fcb05SBarry Smith #if defined(PETSC_USE_DEBUG) 3897b73539f3SBarry Smith if (nnz) { 3898d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 389960e0710aSBarry 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]); 390060e0710aSBarry 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); 3901b73539f3SBarry Smith } 3902b73539f3SBarry Smith } 3903071fcb05SBarry Smith #endif 3904b73539f3SBarry Smith 3905273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 39062205254eSKarl Rupp 3907273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 3908273d9f13SBarry Smith 3909ab93d7beSBarry Smith if (!skipallocation) { 39102ee49352SLisandro Dalcin if (!b->imax) { 3911071fcb05SBarry Smith ierr = PetscMalloc1(B->rmap->n,&b->imax);CHKERRQ(ierr); 3912071fcb05SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3913071fcb05SBarry Smith } 3914071fcb05SBarry Smith if (!b->ilen) { 3915071fcb05SBarry Smith /* b->ilen will count nonzeros in each row so far. */ 3916071fcb05SBarry Smith ierr = PetscCalloc1(B->rmap->n,&b->ilen);CHKERRQ(ierr); 3917071fcb05SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3918071fcb05SBarry Smith } else { 3919071fcb05SBarry Smith ierr = PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 39202ee49352SLisandro Dalcin } 3921846b4da1SFande Kong if (!b->ipre) { 3922846b4da1SFande Kong ierr = PetscMalloc1(B->rmap->n,&b->ipre);CHKERRQ(ierr); 3923846b4da1SFande Kong ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3924846b4da1SFande Kong } 3925273d9f13SBarry Smith if (!nnz) { 3926435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 3927c62bd62aSJed Brown else if (nz < 0) nz = 1; 39285d2a9ed1SStefano Zampini nz = PetscMin(nz,B->cmap->n); 3929d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) b->imax[i] = nz; 3930d0f46423SBarry Smith nz = nz*B->rmap->n; 3931273d9f13SBarry Smith } else { 3932c73702f5SBarry Smith PetscInt64 nz64 = 0; 3933c73702f5SBarry Smith for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];} 3934c73702f5SBarry Smith ierr = PetscIntCast(nz64,&nz);CHKERRQ(ierr); 3935273d9f13SBarry Smith } 3936ab93d7beSBarry Smith 3937273d9f13SBarry Smith /* allocate the matrix space */ 393853dd7562SDmitry Karpeev /* FIXME: should B's old memory be unlogged? */ 39392ee49352SLisandro Dalcin ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr); 3940396832f4SHong Zhang if (B->structure_only) { 39415848002fSHong Zhang ierr = PetscMalloc1(nz,&b->j);CHKERRQ(ierr); 39425848002fSHong Zhang ierr = PetscMalloc1(B->rmap->n+1,&b->i);CHKERRQ(ierr); 3943396832f4SHong Zhang ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));CHKERRQ(ierr); 3944396832f4SHong Zhang } else { 3945dcca6d9dSJed Brown ierr = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr); 39463bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr); 3947396832f4SHong Zhang } 3948bfeeae90SHong Zhang b->i[0] = 0; 3949d0f46423SBarry Smith for (i=1; i<B->rmap->n+1; i++) { 39505da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 39515da197adSKris Buschelman } 3952396832f4SHong Zhang if (B->structure_only) { 3953396832f4SHong Zhang b->singlemalloc = PETSC_FALSE; 3954396832f4SHong Zhang b->free_a = PETSC_FALSE; 3955396832f4SHong Zhang } else { 3956273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 3957e6b907acSBarry Smith b->free_a = PETSC_TRUE; 3958396832f4SHong Zhang } 3959e6b907acSBarry Smith b->free_ij = PETSC_TRUE; 3960c461c341SBarry Smith } else { 3961e6b907acSBarry Smith b->free_a = PETSC_FALSE; 3962e6b907acSBarry Smith b->free_ij = PETSC_FALSE; 3963c461c341SBarry Smith } 3964273d9f13SBarry Smith 3965846b4da1SFande Kong if (b->ipre && nnz != b->ipre && b->imax) { 3966846b4da1SFande Kong /* reserve user-requested sparsity */ 3967580bdb30SBarry Smith ierr = PetscArraycpy(b->ipre,b->imax,B->rmap->n);CHKERRQ(ierr); 3968846b4da1SFande Kong } 3969846b4da1SFande Kong 3970846b4da1SFande Kong 3971273d9f13SBarry Smith b->nz = 0; 3972273d9f13SBarry Smith b->maxnz = nz; 3973273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 39742205254eSKarl Rupp if (realalloc) { 39752205254eSKarl Rupp ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 39762205254eSKarl Rupp } 3977cb7b82ddSBarry Smith B->was_assembled = PETSC_FALSE; 3978cb7b82ddSBarry Smith B->assembled = PETSC_FALSE; 3979273d9f13SBarry Smith PetscFunctionReturn(0); 3980273d9f13SBarry Smith } 3981273d9f13SBarry Smith 3982846b4da1SFande Kong 3983846b4da1SFande Kong PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A) 3984846b4da1SFande Kong { 3985846b4da1SFande Kong Mat_SeqAIJ *a; 3986a5bbaf83SFande Kong PetscInt i; 3987846b4da1SFande Kong PetscErrorCode ierr; 3988846b4da1SFande Kong 3989846b4da1SFande Kong PetscFunctionBegin; 3990846b4da1SFande Kong PetscValidHeaderSpecific(A,MAT_CLASSID,1); 399114d0e64fSAlex Lindsay 399214d0e64fSAlex Lindsay /* Check local size. If zero, then return */ 399314d0e64fSAlex Lindsay if (!A->rmap->n) PetscFunctionReturn(0); 399414d0e64fSAlex Lindsay 3995846b4da1SFande Kong a = (Mat_SeqAIJ*)A->data; 39962c814fdeSFande Kong /* if no saved info, we error out */ 3997fb4dc15dSAlex Lindsay if (!a->ipre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"No saved preallocation info \n"); 39982c814fdeSFande Kong 3999fb4dc15dSAlex Lindsay if (!a->i || !a->j || !a->a || !a->imax || !a->ilen) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Memory info is incomplete, and can not reset preallocation \n"); 40002c814fdeSFande Kong 4001580bdb30SBarry Smith ierr = PetscArraycpy(a->imax,a->ipre,A->rmap->n);CHKERRQ(ierr); 4002580bdb30SBarry Smith ierr = PetscArrayzero(a->ilen,A->rmap->n);CHKERRQ(ierr); 4003846b4da1SFande Kong a->i[0] = 0; 4004846b4da1SFande Kong for (i=1; i<A->rmap->n+1; i++) { 4005846b4da1SFande Kong a->i[i] = a->i[i-1] + a->imax[i-1]; 4006846b4da1SFande Kong } 4007846b4da1SFande Kong A->preallocated = PETSC_TRUE; 4008846b4da1SFande Kong a->nz = 0; 4009846b4da1SFande Kong a->maxnz = a->i[A->rmap->n]; 4010846b4da1SFande Kong A->info.nz_unneeded = (double)a->maxnz; 4011846b4da1SFande Kong A->was_assembled = PETSC_FALSE; 4012846b4da1SFande Kong A->assembled = PETSC_FALSE; 4013846b4da1SFande Kong PetscFunctionReturn(0); 4014846b4da1SFande Kong } 4015846b4da1SFande Kong 401658d36128SBarry Smith /*@ 4017a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 4018a1661176SMatthew Knepley 4019a1661176SMatthew Knepley Input Parameters: 4020a1661176SMatthew Knepley + B - the matrix 4021a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 4022a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 4023a1661176SMatthew Knepley - v - optional values in the matrix 4024a1661176SMatthew Knepley 4025a1661176SMatthew Knepley Level: developer 4026a1661176SMatthew Knepley 402758d36128SBarry Smith The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays() 402858d36128SBarry Smith 4029c1c1d628SHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ 4030a1661176SMatthew Knepley @*/ 4031a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 4032a1661176SMatthew Knepley { 4033a1661176SMatthew Knepley PetscErrorCode ierr; 4034a1661176SMatthew Knepley 4035a1661176SMatthew Knepley PetscFunctionBegin; 40360700a824SBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,1); 40376ba663aaSJed Brown PetscValidType(B,1); 40384ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 4039a1661176SMatthew Knepley PetscFunctionReturn(0); 4040a1661176SMatthew Knepley } 4041a1661176SMatthew Knepley 40427087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 4043a1661176SMatthew Knepley { 4044a1661176SMatthew Knepley PetscInt i; 4045a1661176SMatthew Knepley PetscInt m,n; 4046a1661176SMatthew Knepley PetscInt nz; 4047a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 4048a1661176SMatthew Knepley PetscErrorCode ierr; 4049a1661176SMatthew Knepley 4050a1661176SMatthew Knepley PetscFunctionBegin; 405165e19b50SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]); 4052779a8d59SSatish Balay 4053779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 4054779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 4055779a8d59SSatish Balay 4056779a8d59SSatish Balay ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 4057854ce69bSBarry Smith ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr); 4058a1661176SMatthew Knepley for (i = 0; i < m; i++) { 4059b7940d39SSatish Balay nz = Ii[i+1]- Ii[i]; 4060a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 406165e19b50SBarry Smith if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 4062a1661176SMatthew Knepley nnz[i] = nz; 4063a1661176SMatthew Knepley } 4064a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 4065a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 4066a1661176SMatthew Knepley 4067a1661176SMatthew Knepley for (i = 0; i < m; i++) { 4068071fcb05SBarry Smith ierr = MatSetValues_SeqAIJ(B, 1, &i, Ii[i+1] - Ii[i], J+Ii[i], v ? v + Ii[i] : NULL, INSERT_VALUES);CHKERRQ(ierr); 4069a1661176SMatthew Knepley } 4070a1661176SMatthew Knepley 4071a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4072a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4073a1661176SMatthew Knepley 40747827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 4075a1661176SMatthew Knepley PetscFunctionReturn(0); 4076a1661176SMatthew Knepley } 4077a1661176SMatthew Knepley 4078c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h> 4079af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h> 4080170fe5c8SBarry Smith 4081170fe5c8SBarry Smith /* 4082170fe5c8SBarry Smith Computes (B'*A')' since computing B*A directly is untenable 4083170fe5c8SBarry Smith 4084170fe5c8SBarry Smith n p p 4085170fe5c8SBarry Smith ( ) ( ) ( ) 4086170fe5c8SBarry Smith m ( A ) * n ( B ) = m ( C ) 4087170fe5c8SBarry Smith ( ) ( ) ( ) 4088170fe5c8SBarry Smith 4089170fe5c8SBarry Smith */ 4090170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C) 4091170fe5c8SBarry Smith { 4092170fe5c8SBarry Smith PetscErrorCode ierr; 4093170fe5c8SBarry Smith Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data; 4094170fe5c8SBarry Smith Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data; 4095170fe5c8SBarry Smith Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data; 40961de00fd4SBarry Smith PetscInt i,n,m,q,p; 4097170fe5c8SBarry Smith const PetscInt *ii,*idx; 4098170fe5c8SBarry Smith const PetscScalar *b,*a,*a_q; 4099170fe5c8SBarry Smith PetscScalar *c,*c_q; 4100170fe5c8SBarry Smith 4101170fe5c8SBarry Smith PetscFunctionBegin; 4102d0f46423SBarry Smith m = A->rmap->n; 4103d0f46423SBarry Smith n = A->cmap->n; 4104d0f46423SBarry Smith p = B->cmap->n; 4105170fe5c8SBarry Smith a = sub_a->v; 4106170fe5c8SBarry Smith b = sub_b->a; 4107170fe5c8SBarry Smith c = sub_c->v; 4108580bdb30SBarry Smith ierr = PetscArrayzero(c,m*p);CHKERRQ(ierr); 4109170fe5c8SBarry Smith 4110170fe5c8SBarry Smith ii = sub_b->i; 4111170fe5c8SBarry Smith idx = sub_b->j; 4112170fe5c8SBarry Smith for (i=0; i<n; i++) { 4113170fe5c8SBarry Smith q = ii[i+1] - ii[i]; 4114170fe5c8SBarry Smith while (q-->0) { 4115170fe5c8SBarry Smith c_q = c + m*(*idx); 4116170fe5c8SBarry Smith a_q = a + m*i; 4117854c7f52SBarry Smith PetscKernelAXPY(c_q,*b,a_q,m); 4118170fe5c8SBarry Smith idx++; 4119170fe5c8SBarry Smith b++; 4120170fe5c8SBarry Smith } 4121170fe5c8SBarry Smith } 4122170fe5c8SBarry Smith PetscFunctionReturn(0); 4123170fe5c8SBarry Smith } 4124170fe5c8SBarry Smith 4125170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 4126170fe5c8SBarry Smith { 4127170fe5c8SBarry Smith PetscErrorCode ierr; 4128d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 4129170fe5c8SBarry Smith Mat Cmat; 4130170fe5c8SBarry Smith 4131170fe5c8SBarry Smith PetscFunctionBegin; 413260e0710aSBarry 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); 4133ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr); 4134170fe5c8SBarry Smith ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr); 413533d57670SJed Brown ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr); 4136170fe5c8SBarry Smith ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr); 41370298fd71SBarry Smith ierr = MatSeqDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr); 4138d73949e8SHong Zhang 4139d73949e8SHong Zhang Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ; 41402205254eSKarl Rupp 4141170fe5c8SBarry Smith *C = Cmat; 4142170fe5c8SBarry Smith PetscFunctionReturn(0); 4143170fe5c8SBarry Smith } 4144170fe5c8SBarry Smith 4145170fe5c8SBarry Smith /* ----------------------------------------------------------------*/ 4146150d2497SBarry Smith PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 4147170fe5c8SBarry Smith { 4148170fe5c8SBarry Smith PetscErrorCode ierr; 4149170fe5c8SBarry Smith 4150170fe5c8SBarry Smith PetscFunctionBegin; 4151170fe5c8SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 41523ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 4153170fe5c8SBarry Smith ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr); 41543ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 4155170fe5c8SBarry Smith } 41563ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 4157170fe5c8SBarry Smith ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr); 41583ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 4159170fe5c8SBarry Smith PetscFunctionReturn(0); 4160170fe5c8SBarry Smith } 4161170fe5c8SBarry Smith 4162170fe5c8SBarry Smith 41630bad9183SKris Buschelman /*MC 4164fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 41650bad9183SKris Buschelman based on compressed sparse row format. 41660bad9183SKris Buschelman 41670bad9183SKris Buschelman Options Database Keys: 41680bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 41690bad9183SKris Buschelman 41700bad9183SKris Buschelman Level: beginner 41710bad9183SKris Buschelman 41720cd7f59aSBarry Smith Notes: 41730cd7f59aSBarry Smith MatSetValues() may be called for this matrix type with a NULL argument for the numerical values, 41740cd7f59aSBarry Smith in this case the values associated with the rows and columns one passes in are set to zero 41750cd7f59aSBarry Smith in the matrix 41760cd7f59aSBarry Smith 41770cd7f59aSBarry Smith MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no 41780cd7f59aSBarry Smith space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored 41790cd7f59aSBarry Smith 41800cd7f59aSBarry Smith Developer Notes: 41810cd7f59aSBarry Smith It would be nice if all matrix formats supported passing NULL in for the numerical values 41820cd7f59aSBarry Smith 4183f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 41840bad9183SKris Buschelman M*/ 41850bad9183SKris Buschelman 4186ccd284c7SBarry Smith /*MC 4187ccd284c7SBarry Smith MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 4188ccd284c7SBarry Smith 4189ccd284c7SBarry Smith This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 4190ccd284c7SBarry Smith and MATMPIAIJ otherwise. As a result, for single process communicators, 41910cd7f59aSBarry Smith MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported 4192ccd284c7SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 4193ccd284c7SBarry Smith the above preallocation routines for simplicity. 4194ccd284c7SBarry Smith 4195ccd284c7SBarry Smith Options Database Keys: 4196ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 4197ccd284c7SBarry Smith 419895452b02SPatrick Sanan Developer Notes: 4199ca9cdca7SRichard Tran Mills Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when 4200ccd284c7SBarry Smith enough exist. 4201ccd284c7SBarry Smith 4202ccd284c7SBarry Smith Level: beginner 4203ccd284c7SBarry Smith 4204ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ 4205ccd284c7SBarry Smith M*/ 4206ccd284c7SBarry Smith 4207ccd284c7SBarry Smith /*MC 4208ccd284c7SBarry Smith MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices. 4209ccd284c7SBarry Smith 4210ccd284c7SBarry Smith This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator, 4211ccd284c7SBarry Smith and MATMPIAIJCRL otherwise. As a result, for single process communicators, 4212ccd284c7SBarry Smith MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported 4213ccd284c7SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 4214ccd284c7SBarry Smith the above preallocation routines for simplicity. 4215ccd284c7SBarry Smith 4216ccd284c7SBarry Smith Options Database Keys: 4217ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions() 4218ccd284c7SBarry Smith 4219ccd284c7SBarry Smith Level: beginner 4220ccd284c7SBarry Smith 4221ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL 4222ccd284c7SBarry Smith M*/ 4223ccd284c7SBarry Smith 42247906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*); 42257906f579SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 42267906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*); 42277906f579SHong Zhang #endif 42287906f579SHong Zhang #if defined(PETSC_HAVE_HYPRE) 42297906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*); 42307906f579SHong Zhang PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*); 42317906f579SHong Zhang #endif 42327906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*); 42337906f579SHong Zhang 4234d4002b98SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*); 4235c9225affSStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*); 423675d48cdbSStefano Zampini PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*); 42377906f579SHong Zhang 42388c778c55SBarry Smith /*@C 42398f1ea47aSStefano Zampini MatSeqAIJGetArray - gives read/write access to the array where the data for a MATSEQAIJ matrix is stored 42408c778c55SBarry Smith 42418c778c55SBarry Smith Not Collective 42428c778c55SBarry Smith 42438c778c55SBarry Smith Input Parameter: 4244579dbff0SBarry Smith . mat - a MATSEQAIJ matrix 42458c778c55SBarry Smith 42468c778c55SBarry Smith Output Parameter: 42478c778c55SBarry Smith . array - pointer to the data 42488c778c55SBarry Smith 42498c778c55SBarry Smith Level: intermediate 42508c778c55SBarry Smith 4251774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90() 42528c778c55SBarry Smith @*/ 42538c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray(Mat A,PetscScalar **array) 42548c778c55SBarry Smith { 42558c778c55SBarry Smith PetscErrorCode ierr; 42568c778c55SBarry Smith 42578c778c55SBarry Smith PetscFunctionBegin; 42588c778c55SBarry Smith ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr); 42598c778c55SBarry Smith PetscFunctionReturn(0); 42608c778c55SBarry Smith } 42618c778c55SBarry Smith 426221e72a00SBarry Smith /*@C 42638f1ea47aSStefano Zampini MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a MATSEQAIJ matrix is stored 42648f1ea47aSStefano Zampini 42658f1ea47aSStefano Zampini Not Collective 42668f1ea47aSStefano Zampini 42678f1ea47aSStefano Zampini Input Parameter: 42688f1ea47aSStefano Zampini . mat - a MATSEQAIJ matrix 42698f1ea47aSStefano Zampini 42708f1ea47aSStefano Zampini Output Parameter: 42718f1ea47aSStefano Zampini . array - pointer to the data 42728f1ea47aSStefano Zampini 42738f1ea47aSStefano Zampini Level: intermediate 42748f1ea47aSStefano Zampini 42758f1ea47aSStefano Zampini .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayRead() 42768f1ea47aSStefano Zampini @*/ 42778f1ea47aSStefano Zampini PetscErrorCode MatSeqAIJGetArrayRead(Mat A,const PetscScalar **array) 42788f1ea47aSStefano Zampini { 42798f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4280c70f7ee4SJunchao Zhang PetscOffloadMask oval; 42818f1ea47aSStefano Zampini #endif 42828f1ea47aSStefano Zampini PetscErrorCode ierr; 42838f1ea47aSStefano Zampini 42848f1ea47aSStefano Zampini PetscFunctionBegin; 42858f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4286c70f7ee4SJunchao Zhang oval = A->offloadmask; 42878f1ea47aSStefano Zampini #endif 42888f1ea47aSStefano Zampini ierr = MatSeqAIJGetArray(A,(PetscScalar**)array);CHKERRQ(ierr); 42898f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4290c70f7ee4SJunchao Zhang if (oval == PETSC_OFFLOAD_GPU || oval == PETSC_OFFLOAD_BOTH) A->offloadmask = PETSC_OFFLOAD_BOTH; 42918f1ea47aSStefano Zampini #endif 42928f1ea47aSStefano Zampini PetscFunctionReturn(0); 42938f1ea47aSStefano Zampini } 42948f1ea47aSStefano Zampini 42958f1ea47aSStefano Zampini /*@C 42968f1ea47aSStefano Zampini MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from MatSeqAIJGetArrayRead 42978f1ea47aSStefano Zampini 42988f1ea47aSStefano Zampini Not Collective 42998f1ea47aSStefano Zampini 43008f1ea47aSStefano Zampini Input Parameter: 43018f1ea47aSStefano Zampini . mat - a MATSEQAIJ matrix 43028f1ea47aSStefano Zampini 43038f1ea47aSStefano Zampini Output Parameter: 43048f1ea47aSStefano Zampini . array - pointer to the data 43058f1ea47aSStefano Zampini 43068f1ea47aSStefano Zampini Level: intermediate 43078f1ea47aSStefano Zampini 43088f1ea47aSStefano Zampini .seealso: MatSeqAIJGetArray(), MatSeqAIJGetArrayRead() 43098f1ea47aSStefano Zampini @*/ 43108f1ea47aSStefano Zampini PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A,const PetscScalar **array) 43118f1ea47aSStefano Zampini { 43128f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4313c70f7ee4SJunchao Zhang PetscOffloadMask oval; 43148f1ea47aSStefano Zampini #endif 43158f1ea47aSStefano Zampini PetscErrorCode ierr; 43168f1ea47aSStefano Zampini 43178f1ea47aSStefano Zampini PetscFunctionBegin; 43188f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4319c70f7ee4SJunchao Zhang oval = A->offloadmask; 43208f1ea47aSStefano Zampini #endif 43218f1ea47aSStefano Zampini ierr = MatSeqAIJRestoreArray(A,(PetscScalar**)array);CHKERRQ(ierr); 43228f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4323c70f7ee4SJunchao Zhang A->offloadmask = oval; 43248f1ea47aSStefano Zampini #endif 43258f1ea47aSStefano Zampini PetscFunctionReturn(0); 43268f1ea47aSStefano Zampini } 43278f1ea47aSStefano Zampini 43288f1ea47aSStefano Zampini /*@C 432921e72a00SBarry Smith MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row 433021e72a00SBarry Smith 433121e72a00SBarry Smith Not Collective 433221e72a00SBarry Smith 433321e72a00SBarry Smith Input Parameter: 4334579dbff0SBarry Smith . mat - a MATSEQAIJ matrix 433521e72a00SBarry Smith 433621e72a00SBarry Smith Output Parameter: 433721e72a00SBarry Smith . nz - the maximum number of nonzeros in any row 433821e72a00SBarry Smith 433921e72a00SBarry Smith Level: intermediate 434021e72a00SBarry Smith 434121e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90() 434221e72a00SBarry Smith @*/ 434321e72a00SBarry Smith PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz) 434421e72a00SBarry Smith { 434521e72a00SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data; 434621e72a00SBarry Smith 434721e72a00SBarry Smith PetscFunctionBegin; 434821e72a00SBarry Smith *nz = aij->rmax; 434921e72a00SBarry Smith PetscFunctionReturn(0); 435021e72a00SBarry Smith } 435121e72a00SBarry Smith 43528c778c55SBarry Smith /*@C 4353579dbff0SBarry Smith MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray() 43548c778c55SBarry Smith 43558c778c55SBarry Smith Not Collective 43568c778c55SBarry Smith 43578c778c55SBarry Smith Input Parameters: 4358a2b725a8SWilliam Gropp + mat - a MATSEQAIJ matrix 4359a2b725a8SWilliam Gropp - array - pointer to the data 43608c778c55SBarry Smith 43618c778c55SBarry Smith Level: intermediate 43628c778c55SBarry Smith 4363774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90() 43648c778c55SBarry Smith @*/ 43658c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray(Mat A,PetscScalar **array) 43668c778c55SBarry Smith { 43678c778c55SBarry Smith PetscErrorCode ierr; 43688c778c55SBarry Smith 43698c778c55SBarry Smith PetscFunctionBegin; 43708c778c55SBarry Smith ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr); 43718c778c55SBarry Smith PetscFunctionReturn(0); 43728c778c55SBarry Smith } 43738c778c55SBarry Smith 437434b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA) 437502fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat); 437602fe1965SBarry Smith #endif 437702fe1965SBarry Smith 43788cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B) 4379273d9f13SBarry Smith { 4380273d9f13SBarry Smith Mat_SeqAIJ *b; 4381dfbe8321SBarry Smith PetscErrorCode ierr; 438238baddfdSBarry Smith PetscMPIInt size; 4383273d9f13SBarry Smith 4384273d9f13SBarry Smith PetscFunctionBegin; 4385ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr); 4386e32f2f54SBarry Smith if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 4387273d9f13SBarry Smith 4388b00a9115SJed Brown ierr = PetscNewLog(B,&b);CHKERRQ(ierr); 43892205254eSKarl Rupp 4390b0a32e0cSBarry Smith B->data = (void*)b; 43912205254eSKarl Rupp 4392549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 4393071fcb05SBarry Smith if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull; 43942205254eSKarl Rupp 4395416022c9SBarry Smith b->row = 0; 4396416022c9SBarry Smith b->col = 0; 439782bf6240SBarry Smith b->icol = 0; 4398b810aeb4SBarry Smith b->reallocs = 0; 439936db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 4400f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 4401416022c9SBarry Smith b->nonew = 0; 4402416022c9SBarry Smith b->diag = 0; 4403416022c9SBarry Smith b->solve_work = 0; 44042a1b7f2aSHong Zhang B->spptr = 0; 4405be6bf707SBarry Smith b->saved_values = 0; 4406d7f994e1SBarry Smith b->idiag = 0; 440771f1c65dSBarry Smith b->mdiag = 0; 440871f1c65dSBarry Smith b->ssor_work = 0; 440971f1c65dSBarry Smith b->omega = 1.0; 441071f1c65dSBarry Smith b->fshift = 0.0; 441171f1c65dSBarry Smith b->idiagvalid = PETSC_FALSE; 4412bbead8a2SBarry Smith b->ibdiagvalid = PETSC_FALSE; 4413a9817697SBarry Smith b->keepnonzeropattern = PETSC_FALSE; 441417ab2063SBarry Smith 441535d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 4416bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr); 4417bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr); 44188c778c55SBarry Smith 4419b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4420bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr); 4421bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr); 4422b3866ffcSBarry Smith #endif 442317f1a0eaSHong Zhang 4424bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 4425bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr); 4426bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 4427bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 4428bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 4429bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr); 44304dfdc2d9SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr); 44319779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE) 44324a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr); 4433191b95cbSRichard Tran Mills #endif 443434b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA) 443502fe1965SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);CHKERRQ(ierr); 443602fe1965SBarry Smith #endif 4437bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr); 4438af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 4439af8000cdSHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr); 4440af8000cdSHong Zhang #endif 444163c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE) 444263c07aadSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);CHKERRQ(ierr); 44433dad0653Sstefano_zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);CHKERRQ(ierr); 444463c07aadSStefano Zampini #endif 4445b49cda9fSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr); 4446d4002b98SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);CHKERRQ(ierr); 4447c9225affSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);CHKERRQ(ierr); 4448bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 4449bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 4450bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 4451846b4da1SFande Kong ierr = PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);CHKERRQ(ierr); 4452bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 4453bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 4454bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr); 4455bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 4456bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 445775d48cdbSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);CHKERRQ(ierr); 44584108e4d5SBarry Smith ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr); 445917667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 44604099cc6bSBarry Smith ierr = MatSeqAIJSetTypeFromOptions(B);CHKERRQ(ierr); /* this allows changing the matrix subtype to say MATSEQAIJPERM */ 44613a40ed3dSBarry Smith PetscFunctionReturn(0); 446217ab2063SBarry Smith } 446317ab2063SBarry Smith 4464b24902e0SBarry Smith /* 4465b24902e0SBarry Smith Given a matrix generated with MatGetFactor() duplicates all the information in A into B 4466b24902e0SBarry Smith */ 4467ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace) 446817ab2063SBarry Smith { 4469416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 44706849ba73SBarry Smith PetscErrorCode ierr; 4471071fcb05SBarry Smith PetscInt m = A->rmap->n,i; 447217ab2063SBarry Smith 44733a40ed3dSBarry Smith PetscFunctionBegin; 4474273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 4475273d9f13SBarry Smith 4476d5f3da31SBarry Smith C->factortype = A->factortype; 4477416022c9SBarry Smith c->row = 0; 4478416022c9SBarry Smith c->col = 0; 447982bf6240SBarry Smith c->icol = 0; 44806ad4291fSHong Zhang c->reallocs = 0; 448117ab2063SBarry Smith 44826ad4291fSHong Zhang C->assembled = PETSC_TRUE; 448317ab2063SBarry Smith 4484aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr); 4485aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr); 4486eec197d1SBarry Smith 4487071fcb05SBarry Smith ierr = PetscMalloc1(m,&c->imax);CHKERRQ(ierr); 4488071fcb05SBarry Smith ierr = PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));CHKERRQ(ierr); 4489071fcb05SBarry Smith ierr = PetscMalloc1(m,&c->ilen);CHKERRQ(ierr); 4490071fcb05SBarry Smith ierr = PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));CHKERRQ(ierr); 44913bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr); 449217ab2063SBarry Smith 449317ab2063SBarry Smith /* allocate the matrix space */ 4494f77e22a1SHong Zhang if (mallocmatspace) { 4495dcca6d9dSJed Brown ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr); 44963bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 44972205254eSKarl Rupp 4498f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 44992205254eSKarl Rupp 4500580bdb30SBarry Smith ierr = PetscArraycpy(c->i,a->i,m+1);CHKERRQ(ierr); 450117ab2063SBarry Smith if (m > 0) { 4502580bdb30SBarry Smith ierr = PetscArraycpy(c->j,a->j,a->i[m]);CHKERRQ(ierr); 4503be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 4504580bdb30SBarry Smith ierr = PetscArraycpy(c->a,a->a,a->i[m]);CHKERRQ(ierr); 4505be6bf707SBarry Smith } else { 4506580bdb30SBarry Smith ierr = PetscArrayzero(c->a,a->i[m]);CHKERRQ(ierr); 450717ab2063SBarry Smith } 450808480c60SBarry Smith } 4509f77e22a1SHong Zhang } 451017ab2063SBarry Smith 45116ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 4512416022c9SBarry Smith c->roworiented = a->roworiented; 4513416022c9SBarry Smith c->nonew = a->nonew; 4514416022c9SBarry Smith if (a->diag) { 4515854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr); 4516071fcb05SBarry Smith ierr = PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));CHKERRQ(ierr); 45173bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 4518071fcb05SBarry Smith } else c->diag = NULL; 45192205254eSKarl Rupp 45206ad4291fSHong Zhang c->solve_work = 0; 45216ad4291fSHong Zhang c->saved_values = 0; 45226ad4291fSHong Zhang c->idiag = 0; 452371f1c65dSBarry Smith c->ssor_work = 0; 4524a9817697SBarry Smith c->keepnonzeropattern = a->keepnonzeropattern; 4525e6b907acSBarry Smith c->free_a = PETSC_TRUE; 4526e6b907acSBarry Smith c->free_ij = PETSC_TRUE; 45276ad4291fSHong Zhang 4528893ad86cSHong Zhang c->rmax = a->rmax; 4529416022c9SBarry Smith c->nz = a->nz; 45308ed568f8SMatthew G Knepley c->maxnz = a->nz; /* Since we allocate exactly the right amount */ 4531273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 4532754ec7b1SSatish Balay 45336ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 45346ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 4535cd6b891eSBarry Smith if (a->compressedrow.use) { 45366ad4291fSHong Zhang i = a->compressedrow.nrows; 4537dcca6d9dSJed Brown ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr); 4538580bdb30SBarry Smith ierr = PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);CHKERRQ(ierr); 4539580bdb30SBarry Smith ierr = PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);CHKERRQ(ierr); 454027ea64f8SHong Zhang } else { 454127ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 45420298fd71SBarry Smith c->compressedrow.i = NULL; 45430298fd71SBarry Smith c->compressedrow.rindex = NULL; 45446ad4291fSHong Zhang } 4545ea632784SBarry Smith c->nonzerorowcnt = a->nonzerorowcnt; 4546e56f5c9eSBarry Smith C->nonzerostate = A->nonzerostate; 45474846f1f5SKris Buschelman 45482205254eSKarl Rupp ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr); 4549140e18c1SBarry Smith ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr); 45503a40ed3dSBarry Smith PetscFunctionReturn(0); 455117ab2063SBarry Smith } 455217ab2063SBarry Smith 4553b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 4554b24902e0SBarry Smith { 4555b24902e0SBarry Smith PetscErrorCode ierr; 4556b24902e0SBarry Smith 4557b24902e0SBarry Smith PetscFunctionBegin; 4558ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 45594b6263acSBarry Smith ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr); 4560cfd3f464SBarry Smith if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) { 456133d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 4562cfd3f464SBarry Smith } 4563a54f2f98SBarry Smith ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 4564f77e22a1SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr); 4565b24902e0SBarry Smith PetscFunctionReturn(0); 4566b24902e0SBarry Smith } 4567b24902e0SBarry Smith 4568112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer) 4569fbdbba38SShri Abhyankar { 457052f91c60SVaclav Hapla PetscBool isbinary, ishdf5; 457152f91c60SVaclav Hapla PetscErrorCode ierr; 457252f91c60SVaclav Hapla 457352f91c60SVaclav Hapla PetscFunctionBegin; 457452f91c60SVaclav Hapla PetscValidHeaderSpecific(newMat,MAT_CLASSID,1); 457552f91c60SVaclav Hapla PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 4576c27b3999SVaclav Hapla /* force binary viewer to load .info file if it has not yet done so */ 4577c27b3999SVaclav Hapla ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 457852f91c60SVaclav Hapla ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 457952f91c60SVaclav Hapla ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr); 458052f91c60SVaclav Hapla if (isbinary) { 458152f91c60SVaclav Hapla ierr = MatLoad_SeqAIJ_Binary(newMat,viewer);CHKERRQ(ierr); 458252f91c60SVaclav Hapla } else if (ishdf5) { 458352f91c60SVaclav Hapla #if defined(PETSC_HAVE_HDF5) 458452f91c60SVaclav Hapla ierr = MatLoad_AIJ_HDF5(newMat,viewer);CHKERRQ(ierr); 458552f91c60SVaclav Hapla #else 458652f91c60SVaclav Hapla SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5"); 458752f91c60SVaclav Hapla #endif 458852f91c60SVaclav Hapla } else { 458952f91c60SVaclav Hapla SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name); 459052f91c60SVaclav Hapla } 459152f91c60SVaclav Hapla PetscFunctionReturn(0); 459252f91c60SVaclav Hapla } 459352f91c60SVaclav Hapla 459452f91c60SVaclav Hapla PetscErrorCode MatLoad_SeqAIJ_Binary(Mat newMat, PetscViewer viewer) 459552f91c60SVaclav Hapla { 4596fbdbba38SShri Abhyankar Mat_SeqAIJ *a; 4597fbdbba38SShri Abhyankar PetscErrorCode ierr; 4598fbdbba38SShri Abhyankar PetscInt i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols; 4599fbdbba38SShri Abhyankar int fd; 4600fbdbba38SShri Abhyankar PetscMPIInt size; 4601fbdbba38SShri Abhyankar MPI_Comm comm; 46023059b6faSBarry Smith PetscInt bs = newMat->rmap->bs; 4603fbdbba38SShri Abhyankar 4604fbdbba38SShri Abhyankar PetscFunctionBegin; 4605fbdbba38SShri Abhyankar ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 4606fbdbba38SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4607fbdbba38SShri Abhyankar if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor"); 4608bbead8a2SBarry Smith 46090298fd71SBarry Smith ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr); 46100298fd71SBarry Smith ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr); 4611bbead8a2SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 46123059b6faSBarry Smith if (bs < 0) bs = 1; 46133059b6faSBarry Smith ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr); 4614bbead8a2SBarry Smith 4615fbdbba38SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 46169860990eSLisandro Dalcin ierr = PetscBinaryRead(fd,header,4,NULL,PETSC_INT);CHKERRQ(ierr); 4617fbdbba38SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 4618fbdbba38SShri Abhyankar M = header[1]; N = header[2]; nz = header[3]; 4619fbdbba38SShri Abhyankar 4620bbead8a2SBarry Smith if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 4621fbdbba38SShri Abhyankar 4622fbdbba38SShri Abhyankar /* read in row lengths */ 4623785e854fSJed Brown ierr = PetscMalloc1(M,&rowlengths);CHKERRQ(ierr); 46249860990eSLisandro Dalcin ierr = PetscBinaryRead(fd,rowlengths,M,NULL,PETSC_INT);CHKERRQ(ierr); 4625fbdbba38SShri Abhyankar 4626fbdbba38SShri Abhyankar /* check if sum of rowlengths is same as nz */ 4627fbdbba38SShri Abhyankar for (i=0,sum=0; i< M; i++) sum +=rowlengths[i]; 462860e0710aSBarry 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); 4629fbdbba38SShri Abhyankar 4630fbdbba38SShri Abhyankar /* set global size if not set already*/ 4631f501eaabSShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) { 4632fbdbba38SShri Abhyankar ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); 4633aabbc4fbSShri Abhyankar } else { 46349d36ed5fSBarry Smith /* if sizes and type are already set, check if the matrix global sizes are correct */ 4635fbdbba38SShri Abhyankar ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr); 46364c5b953cSHong Zhang if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */ 46374c5b953cSHong Zhang ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr); 46384c5b953cSHong Zhang } 463960e0710aSBarry 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); 4640aabbc4fbSShri Abhyankar } 4641fbdbba38SShri Abhyankar ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr); 4642fbdbba38SShri Abhyankar a = (Mat_SeqAIJ*)newMat->data; 4643fbdbba38SShri Abhyankar 46449860990eSLisandro Dalcin ierr = PetscBinaryRead(fd,a->j,nz,NULL,PETSC_INT);CHKERRQ(ierr); 4645fbdbba38SShri Abhyankar 4646fbdbba38SShri Abhyankar /* read in nonzero values */ 46479860990eSLisandro Dalcin ierr = PetscBinaryRead(fd,a->a,nz,NULL,PETSC_SCALAR);CHKERRQ(ierr); 4648fbdbba38SShri Abhyankar 4649fbdbba38SShri Abhyankar /* set matrix "i" values */ 4650fbdbba38SShri Abhyankar a->i[0] = 0; 4651fbdbba38SShri Abhyankar for (i=1; i<= M; i++) { 4652fbdbba38SShri Abhyankar a->i[i] = a->i[i-1] + rowlengths[i-1]; 4653fbdbba38SShri Abhyankar a->ilen[i-1] = rowlengths[i-1]; 4654fbdbba38SShri Abhyankar } 4655fbdbba38SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 4656fbdbba38SShri Abhyankar 4657fbdbba38SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4658fbdbba38SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4659fbdbba38SShri Abhyankar PetscFunctionReturn(0); 4660fbdbba38SShri Abhyankar } 4661fbdbba38SShri Abhyankar 4662ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg) 46637264ac53SSatish Balay { 46647264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data; 4665dfbe8321SBarry Smith PetscErrorCode ierr; 4666eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 4667eeffb40dSHong Zhang PetscInt k; 4668eeffb40dSHong Zhang #endif 46697264ac53SSatish Balay 46703a40ed3dSBarry Smith PetscFunctionBegin; 4671bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 4672d0f46423SBarry Smith if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) { 4673ca44d042SBarry Smith *flg = PETSC_FALSE; 4674ca44d042SBarry Smith PetscFunctionReturn(0); 4675bcd2baecSBarry Smith } 46767264ac53SSatish Balay 46777264ac53SSatish Balay /* if the a->i are the same */ 4678580bdb30SBarry Smith ierr = PetscArraycmp(a->i,b->i,A->rmap->n+1,flg);CHKERRQ(ierr); 4679abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 46807264ac53SSatish Balay 46817264ac53SSatish Balay /* if a->j are the same */ 4682580bdb30SBarry Smith ierr = PetscArraycmp(a->j,b->j,a->nz,flg);CHKERRQ(ierr); 4683abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 4684bcd2baecSBarry Smith 4685bcd2baecSBarry Smith /* if a->a are the same */ 4686eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 4687eeffb40dSHong Zhang for (k=0; k<a->nz; k++) { 4688eeffb40dSHong Zhang if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) { 4689eeffb40dSHong Zhang *flg = PETSC_FALSE; 46903a40ed3dSBarry Smith PetscFunctionReturn(0); 4691eeffb40dSHong Zhang } 4692eeffb40dSHong Zhang } 4693eeffb40dSHong Zhang #else 4694580bdb30SBarry Smith ierr = PetscArraycmp(a->a,b->a,a->nz,flg);CHKERRQ(ierr); 4695eeffb40dSHong Zhang #endif 4696eeffb40dSHong Zhang PetscFunctionReturn(0); 46977264ac53SSatish Balay } 469836db0b34SBarry Smith 469905869f15SSatish Balay /*@ 470036db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 470136db0b34SBarry Smith provided by the user. 470236db0b34SBarry Smith 4703d083f849SBarry Smith Collective 470436db0b34SBarry Smith 470536db0b34SBarry Smith Input Parameters: 470636db0b34SBarry Smith + comm - must be an MPI communicator of size 1 470736db0b34SBarry Smith . m - number of rows 470836db0b34SBarry Smith . n - number of columns 4709483a2f95SBarry Smith . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix 471036db0b34SBarry Smith . j - column indices 471136db0b34SBarry Smith - a - matrix values 471236db0b34SBarry Smith 471336db0b34SBarry Smith Output Parameter: 471436db0b34SBarry Smith . mat - the matrix 471536db0b34SBarry Smith 471636db0b34SBarry Smith Level: intermediate 471736db0b34SBarry Smith 471836db0b34SBarry Smith Notes: 47190551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 4720292fb18eSBarry Smith once the matrix is destroyed and not before 472136db0b34SBarry Smith 472236db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 472336db0b34SBarry Smith 4724bfeeae90SHong Zhang The i and j indices are 0 based 472536db0b34SBarry Smith 4726a4552177SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 4727a4552177SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 47288eef79e4SBarry Smith as shown 4729a4552177SSatish Balay 47308eef79e4SBarry Smith $ 1 0 0 47318eef79e4SBarry Smith $ 2 0 3 47328eef79e4SBarry Smith $ 4 5 6 47338eef79e4SBarry Smith $ 47348eef79e4SBarry Smith $ i = {0,1,3,6} [size = nrow+1 = 3+1] 47358eef79e4SBarry Smith $ j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row 47368eef79e4SBarry Smith $ v = {1,2,3,4,5,6} [size = 6] 4737a4552177SSatish Balay 47389985e31cSBarry Smith 473969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 474036db0b34SBarry Smith 474136db0b34SBarry Smith @*/ 4742c3c607ccSBarry Smith PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat) 474336db0b34SBarry Smith { 4744dfbe8321SBarry Smith PetscErrorCode ierr; 4745cbcfb4deSHong Zhang PetscInt ii; 474636db0b34SBarry Smith Mat_SeqAIJ *aij; 4747cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG) 4748cbcfb4deSHong Zhang PetscInt jj; 4749cbcfb4deSHong Zhang #endif 475036db0b34SBarry Smith 475136db0b34SBarry Smith PetscFunctionBegin; 475241096f02SStefano Zampini if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4753f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4754f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 4755a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */ 4756ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 4757ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 4758ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 4759071fcb05SBarry Smith ierr = PetscMalloc1(m,&aij->imax);CHKERRQ(ierr); 4760071fcb05SBarry Smith ierr = PetscMalloc1(m,&aij->ilen);CHKERRQ(ierr); 4761ab93d7beSBarry Smith 476236db0b34SBarry Smith aij->i = i; 476336db0b34SBarry Smith aij->j = j; 476436db0b34SBarry Smith aij->a = a; 476536db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 476636db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 4767e6b907acSBarry Smith aij->free_a = PETSC_FALSE; 4768e6b907acSBarry Smith aij->free_ij = PETSC_FALSE; 476936db0b34SBarry Smith 477036db0b34SBarry Smith for (ii=0; ii<m; ii++) { 477136db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 47722515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 477360e0710aSBarry 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]); 47749985e31cSBarry Smith for (jj=i[ii]+1; jj<i[ii+1]; jj++) { 4775a061629eSStefano Zampini if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is not sorted",jj-i[ii],j[jj],ii); 4776a061629eSStefano Zampini if (j[jj] == j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii); 47779985e31cSBarry Smith } 477836db0b34SBarry Smith #endif 477936db0b34SBarry Smith } 47802515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 478136db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 478260e0710aSBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]); 478360e0710aSBarry 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]); 478436db0b34SBarry Smith } 478536db0b34SBarry Smith #endif 478636db0b34SBarry Smith 4787b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4788b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 478936db0b34SBarry Smith PetscFunctionReturn(0); 479036db0b34SBarry Smith } 479180ef6e79SMatthew G Knepley /*@C 4792d021a1c5SVictor Minden MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format) 47938a0b0e6bSVictor Minden provided by the user. 47948a0b0e6bSVictor Minden 4795d083f849SBarry Smith Collective 47968a0b0e6bSVictor Minden 47978a0b0e6bSVictor Minden Input Parameters: 47988a0b0e6bSVictor Minden + comm - must be an MPI communicator of size 1 47998a0b0e6bSVictor Minden . m - number of rows 48008a0b0e6bSVictor Minden . n - number of columns 48018a0b0e6bSVictor Minden . i - row indices 48028a0b0e6bSVictor Minden . j - column indices 48031230e6d1SVictor Minden . a - matrix values 48041230e6d1SVictor Minden . nz - number of nonzeros 48051230e6d1SVictor Minden - idx - 0 or 1 based 48068a0b0e6bSVictor Minden 48078a0b0e6bSVictor Minden Output Parameter: 48088a0b0e6bSVictor Minden . mat - the matrix 48098a0b0e6bSVictor Minden 48108a0b0e6bSVictor Minden Level: intermediate 48118a0b0e6bSVictor Minden 48128a0b0e6bSVictor Minden Notes: 48138a0b0e6bSVictor Minden The i and j indices are 0 based 48148a0b0e6bSVictor Minden 48158a0b0e6bSVictor Minden The format which is used for the sparse matrix input, is equivalent to a 48168a0b0e6bSVictor Minden row-major ordering.. i.e for the following matrix, the input data expected is 48178a0b0e6bSVictor Minden as shown: 48188a0b0e6bSVictor Minden 48198a0b0e6bSVictor Minden 1 0 0 48208a0b0e6bSVictor Minden 2 0 3 48218a0b0e6bSVictor Minden 4 5 6 48228a0b0e6bSVictor Minden 48238a0b0e6bSVictor Minden i = {0,1,1,2,2,2} 48248a0b0e6bSVictor Minden j = {0,0,2,0,1,2} 48258a0b0e6bSVictor Minden v = {1,2,3,4,5,6} 48268a0b0e6bSVictor Minden 48278a0b0e6bSVictor Minden 482869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 48298a0b0e6bSVictor Minden 48308a0b0e6bSVictor Minden @*/ 4831c3c607ccSBarry Smith PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx) 48328a0b0e6bSVictor Minden { 48338a0b0e6bSVictor Minden PetscErrorCode ierr; 4834d021a1c5SVictor Minden PetscInt ii, *nnz, one = 1,row,col; 48358a0b0e6bSVictor Minden 48368a0b0e6bSVictor Minden 48378a0b0e6bSVictor Minden PetscFunctionBegin; 48381795a4d1SJed Brown ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr); 48391230e6d1SVictor Minden for (ii = 0; ii < nz; ii++) { 4840c8d679ebSHong Zhang nnz[i[ii] - !!idx] += 1; 48411230e6d1SVictor Minden } 48428a0b0e6bSVictor Minden ierr = MatCreate(comm,mat);CHKERRQ(ierr); 48438a0b0e6bSVictor Minden ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 48448a0b0e6bSVictor Minden ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 48451230e6d1SVictor Minden ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr); 48461230e6d1SVictor Minden for (ii = 0; ii < nz; ii++) { 48471230e6d1SVictor Minden if (idx) { 48481230e6d1SVictor Minden row = i[ii] - 1; 48491230e6d1SVictor Minden col = j[ii] - 1; 48501230e6d1SVictor Minden } else { 48511230e6d1SVictor Minden row = i[ii]; 48521230e6d1SVictor Minden col = j[ii]; 48538a0b0e6bSVictor Minden } 48541230e6d1SVictor Minden ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr); 48558a0b0e6bSVictor Minden } 48568a0b0e6bSVictor Minden ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 48578a0b0e6bSVictor Minden ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4858d021a1c5SVictor Minden ierr = PetscFree(nnz);CHKERRQ(ierr); 48598a0b0e6bSVictor Minden PetscFunctionReturn(0); 48608a0b0e6bSVictor Minden } 486136db0b34SBarry Smith 4862acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A) 4863acf2f550SJed Brown { 4864acf2f550SJed Brown Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 4865acf2f550SJed Brown PetscErrorCode ierr; 4866acf2f550SJed Brown 4867acf2f550SJed Brown PetscFunctionBegin; 4868acf2f550SJed Brown a->idiagvalid = PETSC_FALSE; 4869acf2f550SJed Brown a->ibdiagvalid = PETSC_FALSE; 48702205254eSKarl Rupp 4871acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr); 4872acf2f550SJed Brown PetscFunctionReturn(0); 4873acf2f550SJed Brown } 4874acf2f550SJed Brown 48759c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 48769c8f2541SHong Zhang { 48779c8f2541SHong Zhang PetscErrorCode ierr; 48788761c3d6SHong Zhang PetscMPIInt size; 48799c8f2541SHong Zhang 48809c8f2541SHong Zhang PetscFunctionBegin; 48818761c3d6SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 48827bbdc51dSHong Zhang if (size == 1) { 48837bbdc51dSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 48847bbdc51dSHong Zhang ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr); 48857bbdc51dSHong Zhang } else { 48868761c3d6SHong Zhang ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 48877bbdc51dSHong Zhang } 48888761c3d6SHong Zhang } else { 48899c8f2541SHong Zhang ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr); 48908761c3d6SHong Zhang } 48919c8f2541SHong Zhang PetscFunctionReturn(0); 48929c8f2541SHong Zhang } 48939c8f2541SHong Zhang 489481824310SBarry Smith /* 489553dd7562SDmitry Karpeev Permute A into C's *local* index space using rowemb,colemb. 489653dd7562SDmitry Karpeev The embedding are supposed to be injections and the above implies that the range of rowemb is a subset 489753dd7562SDmitry Karpeev of [0,m), colemb is in [0,n). 489853dd7562SDmitry Karpeev If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A. 489953dd7562SDmitry Karpeev */ 490053dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B) 490153dd7562SDmitry Karpeev { 490253dd7562SDmitry Karpeev /* If making this function public, change the error returned in this function away from _PLIB. */ 490353dd7562SDmitry Karpeev PetscErrorCode ierr; 490453dd7562SDmitry Karpeev Mat_SeqAIJ *Baij; 490553dd7562SDmitry Karpeev PetscBool seqaij; 490653dd7562SDmitry Karpeev PetscInt m,n,*nz,i,j,count; 490753dd7562SDmitry Karpeev PetscScalar v; 490853dd7562SDmitry Karpeev const PetscInt *rowindices,*colindices; 490953dd7562SDmitry Karpeev 491053dd7562SDmitry Karpeev PetscFunctionBegin; 491153dd7562SDmitry Karpeev if (!B) PetscFunctionReturn(0); 491253dd7562SDmitry Karpeev /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */ 49134099cc6bSBarry Smith ierr = PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr); 491453dd7562SDmitry Karpeev if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type"); 491553dd7562SDmitry Karpeev if (rowemb) { 491653dd7562SDmitry Karpeev ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr); 491753dd7562SDmitry 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); 491853dd7562SDmitry Karpeev } else { 49196c4ed002SBarry Smith if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix"); 492053dd7562SDmitry Karpeev } 492153dd7562SDmitry Karpeev if (colemb) { 492253dd7562SDmitry Karpeev ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr); 492353dd7562SDmitry 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); 492453dd7562SDmitry Karpeev } else { 492553dd7562SDmitry Karpeev if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix"); 492653dd7562SDmitry Karpeev } 492753dd7562SDmitry Karpeev 492853dd7562SDmitry Karpeev Baij = (Mat_SeqAIJ*)(B->data); 492953dd7562SDmitry Karpeev if (pattern == DIFFERENT_NONZERO_PATTERN) { 493053dd7562SDmitry Karpeev ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr); 493153dd7562SDmitry Karpeev for (i=0; i<B->rmap->n; i++) { 493253dd7562SDmitry Karpeev nz[i] = Baij->i[i+1] - Baij->i[i]; 493353dd7562SDmitry Karpeev } 493453dd7562SDmitry Karpeev ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr); 493553dd7562SDmitry Karpeev ierr = PetscFree(nz);CHKERRQ(ierr); 493653dd7562SDmitry Karpeev } 493753dd7562SDmitry Karpeev if (pattern == SUBSET_NONZERO_PATTERN) { 493853dd7562SDmitry Karpeev ierr = MatZeroEntries(C);CHKERRQ(ierr); 493953dd7562SDmitry Karpeev } 494053dd7562SDmitry Karpeev count = 0; 494153dd7562SDmitry Karpeev rowindices = NULL; 494253dd7562SDmitry Karpeev colindices = NULL; 494353dd7562SDmitry Karpeev if (rowemb) { 494453dd7562SDmitry Karpeev ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr); 494553dd7562SDmitry Karpeev } 494653dd7562SDmitry Karpeev if (colemb) { 494753dd7562SDmitry Karpeev ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr); 494853dd7562SDmitry Karpeev } 494953dd7562SDmitry Karpeev for (i=0; i<B->rmap->n; i++) { 495053dd7562SDmitry Karpeev PetscInt row; 495153dd7562SDmitry Karpeev row = i; 495253dd7562SDmitry Karpeev if (rowindices) row = rowindices[i]; 495353dd7562SDmitry Karpeev for (j=Baij->i[i]; j<Baij->i[i+1]; j++) { 495453dd7562SDmitry Karpeev PetscInt col; 495553dd7562SDmitry Karpeev col = Baij->j[count]; 495653dd7562SDmitry Karpeev if (colindices) col = colindices[col]; 495753dd7562SDmitry Karpeev v = Baij->a[count]; 495853dd7562SDmitry Karpeev ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr); 495953dd7562SDmitry Karpeev ++count; 496053dd7562SDmitry Karpeev } 496153dd7562SDmitry Karpeev } 496253dd7562SDmitry Karpeev /* FIXME: set C's nonzerostate correctly. */ 496353dd7562SDmitry Karpeev /* Assembly for C is necessary. */ 496453dd7562SDmitry Karpeev C->preallocated = PETSC_TRUE; 496553dd7562SDmitry Karpeev C->assembled = PETSC_TRUE; 496653dd7562SDmitry Karpeev C->was_assembled = PETSC_FALSE; 496753dd7562SDmitry Karpeev PetscFunctionReturn(0); 496853dd7562SDmitry Karpeev } 496953dd7562SDmitry Karpeev 49704099cc6bSBarry Smith PetscFunctionList MatSeqAIJList = NULL; 49714099cc6bSBarry Smith 49724099cc6bSBarry Smith /*@C 49734099cc6bSBarry Smith MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype 49744099cc6bSBarry Smith 49754099cc6bSBarry Smith Collective on Mat 49764099cc6bSBarry Smith 49774099cc6bSBarry Smith Input Parameters: 49784099cc6bSBarry Smith + mat - the matrix object 49794099cc6bSBarry Smith - matype - matrix type 49804099cc6bSBarry Smith 49814099cc6bSBarry Smith Options Database Key: 49824099cc6bSBarry Smith . -mat_seqai_type <method> - for example seqaijcrl 49834099cc6bSBarry Smith 49844099cc6bSBarry Smith 49854099cc6bSBarry Smith Level: intermediate 49864099cc6bSBarry Smith 49874099cc6bSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat 49884099cc6bSBarry Smith @*/ 49894099cc6bSBarry Smith PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype) 49904099cc6bSBarry Smith { 4991fd9d3c67SJed Brown PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*); 49924099cc6bSBarry Smith PetscBool sametype; 49934099cc6bSBarry Smith 49944099cc6bSBarry Smith PetscFunctionBegin; 49954099cc6bSBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 49964099cc6bSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr); 49974099cc6bSBarry Smith if (sametype) PetscFunctionReturn(0); 49984099cc6bSBarry Smith 49994099cc6bSBarry Smith ierr = PetscFunctionListFind(MatSeqAIJList,matype,&r);CHKERRQ(ierr); 50004099cc6bSBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype); 50014099cc6bSBarry Smith ierr = (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);CHKERRQ(ierr); 50024099cc6bSBarry Smith PetscFunctionReturn(0); 50034099cc6bSBarry Smith } 50044099cc6bSBarry Smith 50054099cc6bSBarry Smith 50064099cc6bSBarry Smith /*@C 50074099cc6bSBarry Smith MatSeqAIJRegister - - Adds a new sub-matrix type for sequential AIJ matrices 50084099cc6bSBarry Smith 50094099cc6bSBarry Smith Not Collective 50104099cc6bSBarry Smith 50114099cc6bSBarry Smith Input Parameters: 50124099cc6bSBarry Smith + name - name of a new user-defined matrix type, for example MATSEQAIJCRL 50134099cc6bSBarry Smith - function - routine to convert to subtype 50144099cc6bSBarry Smith 50154099cc6bSBarry Smith Notes: 50164099cc6bSBarry Smith MatSeqAIJRegister() may be called multiple times to add several user-defined solvers. 50174099cc6bSBarry Smith 50184099cc6bSBarry Smith 50194099cc6bSBarry Smith Then, your matrix can be chosen with the procedural interface at runtime via the option 50204099cc6bSBarry Smith $ -mat_seqaij_type my_mat 50214099cc6bSBarry Smith 50224099cc6bSBarry Smith Level: advanced 50234099cc6bSBarry Smith 50244099cc6bSBarry Smith .seealso: MatSeqAIJRegisterAll() 50254099cc6bSBarry Smith 50264099cc6bSBarry Smith 50274099cc6bSBarry Smith Level: advanced 50284099cc6bSBarry Smith @*/ 5029388d47a6SSatish Balay PetscErrorCode MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *)) 50304099cc6bSBarry Smith { 50314099cc6bSBarry Smith PetscErrorCode ierr; 50324099cc6bSBarry Smith 50334099cc6bSBarry Smith PetscFunctionBegin; 50349cc31a68SJed Brown ierr = MatInitializePackage();CHKERRQ(ierr); 50354099cc6bSBarry Smith ierr = PetscFunctionListAdd(&MatSeqAIJList,sname,function);CHKERRQ(ierr); 50364099cc6bSBarry Smith PetscFunctionReturn(0); 50374099cc6bSBarry Smith } 50384099cc6bSBarry Smith 50394099cc6bSBarry Smith PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE; 50404099cc6bSBarry Smith 50414099cc6bSBarry Smith /*@C 50424099cc6bSBarry Smith MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ 50434099cc6bSBarry Smith 50444099cc6bSBarry Smith Not Collective 50454099cc6bSBarry Smith 50464099cc6bSBarry Smith Level: advanced 50474099cc6bSBarry Smith 50484099cc6bSBarry Smith Developers Note: CUSP and CUSPARSE do not yet support the MatConvert_SeqAIJ..() paradigm and thus cannot be registered here 50494099cc6bSBarry Smith 50504099cc6bSBarry Smith .seealso: MatRegisterAll(), MatSeqAIJRegister() 50514099cc6bSBarry Smith @*/ 50524099cc6bSBarry Smith PetscErrorCode MatSeqAIJRegisterAll(void) 50534099cc6bSBarry Smith { 50544099cc6bSBarry Smith PetscErrorCode ierr; 50554099cc6bSBarry Smith 50564099cc6bSBarry Smith PetscFunctionBegin; 50574099cc6bSBarry Smith if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(0); 50584099cc6bSBarry Smith MatSeqAIJRegisterAllCalled = PETSC_TRUE; 50594099cc6bSBarry Smith 50604099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr); 50614099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr); 50624dfdc2d9SRichard Tran Mills ierr = MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr); 50639779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE) 50646b62b571SRichard Tran Mills ierr = MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr); 5065485f9817SRichard Tran Mills #endif 50664099cc6bSBarry Smith #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA) 50674099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);CHKERRQ(ierr); 50684099cc6bSBarry Smith #endif 50694099cc6bSBarry Smith PetscFunctionReturn(0); 50704099cc6bSBarry Smith } 507153dd7562SDmitry Karpeev 507253dd7562SDmitry Karpeev /* 507381824310SBarry Smith Special version for direct calls from Fortran 507481824310SBarry Smith */ 5075af0996ceSBarry Smith #include <petsc/private/fortranimpl.h> 507681824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 507781824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 507881824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 507981824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 508081824310SBarry Smith #endif 508181824310SBarry Smith 508281824310SBarry Smith /* Change these macros so can be used in void function */ 508381824310SBarry Smith #undef CHKERRQ 5084ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr) 508581824310SBarry Smith #undef SETERRQ2 5086e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 50874994cf47SJed Brown #undef SETERRQ3 50884994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 508981824310SBarry Smith 50908cc058d9SJed 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) 509181824310SBarry Smith { 509281824310SBarry Smith Mat A = *AA; 509381824310SBarry Smith PetscInt m = *mm, n = *nn; 509481824310SBarry Smith InsertMode is = *isis; 509581824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 509681824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 509781824310SBarry Smith PetscInt *imax,*ai,*ailen; 509881824310SBarry Smith PetscErrorCode ierr; 509981824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 510054f21887SBarry Smith MatScalar *ap,value,*aa; 5101ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 5102ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 510381824310SBarry Smith 510481824310SBarry Smith PetscFunctionBegin; 51054994cf47SJed Brown MatCheckPreallocated(A,1); 510681824310SBarry Smith imax = a->imax; 510781824310SBarry Smith ai = a->i; 510881824310SBarry Smith ailen = a->ilen; 510981824310SBarry Smith aj = a->j; 511081824310SBarry Smith aa = a->a; 511181824310SBarry Smith 511281824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 511381824310SBarry Smith row = im[k]; 511481824310SBarry Smith if (row < 0) continue; 511581824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5116ce94432eSBarry Smith if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 511781824310SBarry Smith #endif 511881824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 511981824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 512081824310SBarry Smith low = 0; 512181824310SBarry Smith high = nrow; 512281824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 512381824310SBarry Smith if (in[l] < 0) continue; 512481824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5125ce94432eSBarry Smith if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 512681824310SBarry Smith #endif 512781824310SBarry Smith col = in[l]; 51282205254eSKarl Rupp if (roworiented) value = v[l + k*n]; 51292205254eSKarl Rupp else value = v[k + l*m]; 51302205254eSKarl Rupp 513181824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 513281824310SBarry Smith 51332205254eSKarl Rupp if (col <= lastcol) low = 0; 51342205254eSKarl Rupp else high = nrow; 513581824310SBarry Smith lastcol = col; 513681824310SBarry Smith while (high-low > 5) { 513781824310SBarry Smith t = (low+high)/2; 513881824310SBarry Smith if (rp[t] > col) high = t; 513981824310SBarry Smith else low = t; 514081824310SBarry Smith } 514181824310SBarry Smith for (i=low; i<high; i++) { 514281824310SBarry Smith if (rp[i] > col) break; 514381824310SBarry Smith if (rp[i] == col) { 514481824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 514581824310SBarry Smith else ap[i] = value; 514681824310SBarry Smith goto noinsert; 514781824310SBarry Smith } 514881824310SBarry Smith } 514981824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 515081824310SBarry Smith if (nonew == 1) goto noinsert; 5151ce94432eSBarry Smith if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 5152fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 515381824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 515481824310SBarry Smith /* shift up all the later entries in this row */ 515581824310SBarry Smith for (ii=N; ii>=i; ii--) { 515681824310SBarry Smith rp[ii+1] = rp[ii]; 515781824310SBarry Smith ap[ii+1] = ap[ii]; 515881824310SBarry Smith } 515981824310SBarry Smith rp[i] = col; 516081824310SBarry Smith ap[i] = value; 5161e56f5c9eSBarry Smith A->nonzerostate++; 516281824310SBarry Smith noinsert:; 516381824310SBarry Smith low = i + 1; 516481824310SBarry Smith } 516581824310SBarry Smith ailen[row] = nrow; 516681824310SBarry Smith } 516781824310SBarry Smith PetscFunctionReturnVoid(); 516881824310SBarry Smith } 5169