1d5d45c9bSBarry Smith /* 23369ce9aSBarry Smith Defines the basic matrix operations for the AIJ (compressed row) 3d5d45c9bSBarry Smith matrix storage format. 4d5d45c9bSBarry Smith */ 53369ce9aSBarry Smith 67c4f633dSBarry Smith 7c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h> /*I "petscmat.h" I*/ 8c6db04a5SJed Brown #include <petscblaslapack.h> 9c6db04a5SJed Brown #include <petscbt.h> 10af0996ceSBarry Smith #include <petsc/private/kernels/blocktranspose.h> 110716a85fSBarry Smith 124099cc6bSBarry Smith PetscErrorCode MatSeqAIJSetTypeFromOptions(Mat A) 134099cc6bSBarry Smith { 144099cc6bSBarry Smith PetscErrorCode ierr; 154099cc6bSBarry Smith PetscBool flg; 164099cc6bSBarry Smith char type[256]; 174099cc6bSBarry Smith 184099cc6bSBarry Smith PetscFunctionBegin; 194099cc6bSBarry Smith ierr = PetscObjectOptionsBegin((PetscObject)A); 204099cc6bSBarry Smith ierr = PetscOptionsFList("-mat_seqaij_type","Matrix SeqAIJ type","MatSeqAIJSetType",MatSeqAIJList,"seqaij",type,256,&flg);CHKERRQ(ierr); 214099cc6bSBarry Smith if (flg) { 224099cc6bSBarry Smith ierr = MatSeqAIJSetType(A,type);CHKERRQ(ierr); 234099cc6bSBarry Smith } 244099cc6bSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 254099cc6bSBarry Smith PetscFunctionReturn(0); 264099cc6bSBarry Smith } 274099cc6bSBarry Smith 280716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms) 290716a85fSBarry Smith { 300716a85fSBarry Smith PetscErrorCode ierr; 310716a85fSBarry Smith PetscInt i,m,n; 320716a85fSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data; 330716a85fSBarry Smith 340716a85fSBarry Smith PetscFunctionBegin; 350716a85fSBarry Smith ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr); 36580bdb30SBarry Smith ierr = PetscArrayzero(norms,n);CHKERRQ(ierr); 370716a85fSBarry Smith if (type == NORM_2) { 380716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 390716a85fSBarry Smith norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]); 400716a85fSBarry Smith } 410716a85fSBarry Smith } else if (type == NORM_1) { 420716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 430716a85fSBarry Smith norms[aij->j[i]] += PetscAbsScalar(aij->a[i]); 440716a85fSBarry Smith } 450716a85fSBarry Smith } else if (type == NORM_INFINITY) { 460716a85fSBarry Smith for (i=0; i<aij->i[m]; i++) { 470716a85fSBarry Smith norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]); 480716a85fSBarry Smith } 490716a85fSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType"); 500716a85fSBarry Smith 510716a85fSBarry Smith if (type == NORM_2) { 528f1a2a5eSBarry Smith for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]); 530716a85fSBarry Smith } 540716a85fSBarry Smith PetscFunctionReturn(0); 550716a85fSBarry Smith } 560716a85fSBarry Smith 573a062f41SBarry Smith PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is) 583a062f41SBarry Smith { 593a062f41SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 603a062f41SBarry Smith PetscInt i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs; 613a062f41SBarry Smith const PetscInt *jj = a->j,*ii = a->i; 623a062f41SBarry Smith PetscInt *rows; 633a062f41SBarry Smith PetscErrorCode ierr; 643a062f41SBarry Smith 653a062f41SBarry Smith PetscFunctionBegin; 663a062f41SBarry Smith for (i=0; i<m; i++) { 673a062f41SBarry Smith if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) { 683a062f41SBarry Smith cnt++; 693a062f41SBarry Smith } 703a062f41SBarry Smith } 713a062f41SBarry Smith ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr); 723a062f41SBarry Smith cnt = 0; 733a062f41SBarry Smith for (i=0; i<m; i++) { 743a062f41SBarry Smith if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) { 753a062f41SBarry Smith rows[cnt] = i; 763a062f41SBarry Smith cnt++; 773a062f41SBarry Smith } 783a062f41SBarry Smith } 793a062f41SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);CHKERRQ(ierr); 803a062f41SBarry Smith PetscFunctionReturn(0); 813a062f41SBarry Smith } 823a062f41SBarry Smith 83f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows) 846ce1633cSBarry Smith { 856ce1633cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 866ce1633cSBarry Smith const MatScalar *aa = a->a; 876ce1633cSBarry Smith PetscInt i,m=A->rmap->n,cnt = 0; 88b2db7409Sstefano_zampini const PetscInt *ii = a->i,*jj = a->j,*diag; 896ce1633cSBarry Smith PetscInt *rows; 906ce1633cSBarry Smith PetscErrorCode ierr; 916ce1633cSBarry Smith 926ce1633cSBarry Smith PetscFunctionBegin; 936ce1633cSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 946ce1633cSBarry Smith diag = a->diag; 956ce1633cSBarry Smith for (i=0; i<m; i++) { 96b2db7409Sstefano_zampini if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) { 976ce1633cSBarry Smith cnt++; 986ce1633cSBarry Smith } 996ce1633cSBarry Smith } 100785e854fSJed Brown ierr = PetscMalloc1(cnt,&rows);CHKERRQ(ierr); 1016ce1633cSBarry Smith cnt = 0; 1026ce1633cSBarry Smith for (i=0; i<m; i++) { 103b2db7409Sstefano_zampini if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) { 1046ce1633cSBarry Smith rows[cnt++] = i; 1056ce1633cSBarry Smith } 1066ce1633cSBarry Smith } 107f1f41ecbSJed Brown *nrows = cnt; 108f1f41ecbSJed Brown *zrows = rows; 109f1f41ecbSJed Brown PetscFunctionReturn(0); 110f1f41ecbSJed Brown } 111f1f41ecbSJed Brown 112f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows) 113f1f41ecbSJed Brown { 114f1f41ecbSJed Brown PetscInt nrows,*rows; 115f1f41ecbSJed Brown PetscErrorCode ierr; 116f1f41ecbSJed Brown 117f1f41ecbSJed Brown PetscFunctionBegin; 1180298fd71SBarry Smith *zrows = NULL; 119f1f41ecbSJed Brown ierr = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr); 120ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr); 1216ce1633cSBarry Smith PetscFunctionReturn(0); 1226ce1633cSBarry Smith } 1236ce1633cSBarry Smith 124b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows) 125b3a44c85SBarry Smith { 126b3a44c85SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 127b3a44c85SBarry Smith const MatScalar *aa; 128b3a44c85SBarry Smith PetscInt m=A->rmap->n,cnt = 0; 129b3a44c85SBarry Smith const PetscInt *ii; 130b3a44c85SBarry Smith PetscInt n,i,j,*rows; 131b3a44c85SBarry Smith PetscErrorCode ierr; 132b3a44c85SBarry Smith 133b3a44c85SBarry Smith PetscFunctionBegin; 134b3a44c85SBarry Smith *keptrows = 0; 135b3a44c85SBarry Smith ii = a->i; 136b3a44c85SBarry Smith for (i=0; i<m; i++) { 137b3a44c85SBarry Smith n = ii[i+1] - ii[i]; 138b3a44c85SBarry Smith if (!n) { 139b3a44c85SBarry Smith cnt++; 140b3a44c85SBarry Smith goto ok1; 141b3a44c85SBarry Smith } 142b3a44c85SBarry Smith aa = a->a + ii[i]; 143b3a44c85SBarry Smith for (j=0; j<n; j++) { 144b3a44c85SBarry Smith if (aa[j] != 0.0) goto ok1; 145b3a44c85SBarry Smith } 146b3a44c85SBarry Smith cnt++; 147b3a44c85SBarry Smith ok1:; 148b3a44c85SBarry Smith } 149b3a44c85SBarry Smith if (!cnt) PetscFunctionReturn(0); 150854ce69bSBarry Smith ierr = PetscMalloc1(A->rmap->n-cnt,&rows);CHKERRQ(ierr); 151b3a44c85SBarry Smith cnt = 0; 152b3a44c85SBarry Smith for (i=0; i<m; i++) { 153b3a44c85SBarry Smith n = ii[i+1] - ii[i]; 154b3a44c85SBarry Smith if (!n) continue; 155b3a44c85SBarry Smith aa = a->a + ii[i]; 156b3a44c85SBarry Smith for (j=0; j<n; j++) { 157b3a44c85SBarry Smith if (aa[j] != 0.0) { 158b3a44c85SBarry Smith rows[cnt++] = i; 159b3a44c85SBarry Smith break; 160b3a44c85SBarry Smith } 161b3a44c85SBarry Smith } 162b3a44c85SBarry Smith } 163b3a44c85SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr); 164b3a44c85SBarry Smith PetscFunctionReturn(0); 165b3a44c85SBarry Smith } 166b3a44c85SBarry Smith 1677087cfbeSBarry Smith PetscErrorCode MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is) 16879299369SBarry Smith { 16979299369SBarry Smith PetscErrorCode ierr; 17079299369SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) Y->data; 17199e65526SBarry Smith PetscInt i,m = Y->rmap->n; 17299e65526SBarry Smith const PetscInt *diag; 17354f21887SBarry Smith MatScalar *aa = aij->a; 17499e65526SBarry Smith const PetscScalar *v; 175ace3abfcSBarry Smith PetscBool missing; 176837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 177837a59e1SRichard Tran Mills PetscBool inserted = PETSC_FALSE; 178837a59e1SRichard Tran Mills #endif 17979299369SBarry Smith 18079299369SBarry Smith PetscFunctionBegin; 18109f38230SBarry Smith if (Y->assembled) { 1820298fd71SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);CHKERRQ(ierr); 18309f38230SBarry Smith if (!missing) { 18479299369SBarry Smith diag = aij->diag; 18599e65526SBarry Smith ierr = VecGetArrayRead(D,&v);CHKERRQ(ierr); 18679299369SBarry Smith if (is == INSERT_VALUES) { 187837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 188837a59e1SRichard Tran Mills inserted = PETSC_TRUE; 189837a59e1SRichard Tran Mills #endif 19079299369SBarry Smith for (i=0; i<m; i++) { 19179299369SBarry Smith aa[diag[i]] = v[i]; 19279299369SBarry Smith } 19379299369SBarry Smith } else { 19479299369SBarry Smith for (i=0; i<m; i++) { 195837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 196837a59e1SRichard Tran Mills if (v[i] != 0.0) inserted = PETSC_TRUE; 197837a59e1SRichard Tran Mills #endif 19879299369SBarry Smith aa[diag[i]] += v[i]; 19979299369SBarry Smith } 20079299369SBarry Smith } 201837a59e1SRichard Tran Mills #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 202837a59e1SRichard Tran Mills if (inserted) Y->offloadmask = PETSC_OFFLOAD_CPU; 203837a59e1SRichard Tran Mills #endif 20499e65526SBarry Smith ierr = VecRestoreArrayRead(D,&v);CHKERRQ(ierr); 20579299369SBarry Smith PetscFunctionReturn(0); 20679299369SBarry Smith } 207acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr); 20809f38230SBarry Smith } 20909f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 21009f38230SBarry Smith PetscFunctionReturn(0); 21109f38230SBarry Smith } 21279299369SBarry Smith 2131a83f524SJed Brown PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 21417ab2063SBarry Smith { 215416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 216dfbe8321SBarry Smith PetscErrorCode ierr; 21797f1f81fSBarry Smith PetscInt i,ishift; 21817ab2063SBarry Smith 2193a40ed3dSBarry Smith PetscFunctionBegin; 220d0f46423SBarry Smith *m = A->rmap->n; 2213a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 222bfeeae90SHong Zhang ishift = 0; 22353e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 2242462f5fdSStefano Zampini ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr); 225bfeeae90SHong Zhang } else if (oshift == 1) { 2261a83f524SJed Brown PetscInt *tia; 227d0f46423SBarry Smith PetscInt nz = a->i[A->rmap->n]; 2283b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 229854ce69bSBarry Smith ierr = PetscMalloc1(A->rmap->n+1,&tia);CHKERRQ(ierr); 2301a83f524SJed Brown for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1; 2311a83f524SJed Brown *ia = tia; 232ecc77c7aSBarry Smith if (ja) { 2331a83f524SJed Brown PetscInt *tja; 234854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&tja);CHKERRQ(ierr); 2351a83f524SJed Brown for (i=0; i<nz; i++) tja[i] = a->j[i] + 1; 2361a83f524SJed Brown *ja = tja; 237ecc77c7aSBarry Smith } 2386945ee14SBarry Smith } else { 239ecc77c7aSBarry Smith *ia = a->i; 240ecc77c7aSBarry Smith if (ja) *ja = a->j; 241a2ce50c7SBarry Smith } 2423a40ed3dSBarry Smith PetscFunctionReturn(0); 243a2744918SBarry Smith } 244a2744918SBarry Smith 2451a83f524SJed Brown PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2466945ee14SBarry Smith { 247dfbe8321SBarry Smith PetscErrorCode ierr; 2486945ee14SBarry Smith 2493a40ed3dSBarry Smith PetscFunctionBegin; 2503a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 251bfeeae90SHong Zhang if ((symmetric && !A->structurally_symmetric) || oshift == 1) { 252606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 253ecc77c7aSBarry Smith if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);} 254bcd2baecSBarry Smith } 2553a40ed3dSBarry Smith PetscFunctionReturn(0); 25617ab2063SBarry Smith } 25717ab2063SBarry Smith 2581a83f524SJed Brown PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2593b2fbd54SBarry Smith { 2603b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 261dfbe8321SBarry Smith PetscErrorCode ierr; 262d0f46423SBarry Smith PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 26397f1f81fSBarry Smith PetscInt nz = a->i[m],row,*jj,mr,col; 2643b2fbd54SBarry Smith 2653a40ed3dSBarry Smith PetscFunctionBegin; 266899cda47SBarry Smith *nn = n; 2673a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 2683b2fbd54SBarry Smith if (symmetric) { 2692462f5fdSStefano Zampini ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);CHKERRQ(ierr); 2703b2fbd54SBarry Smith } else { 271b9e7e5c1SBarry Smith ierr = PetscCalloc1(n,&collengths);CHKERRQ(ierr); 272854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr); 273b9e7e5c1SBarry Smith ierr = PetscMalloc1(nz,&cja);CHKERRQ(ierr); 2743b2fbd54SBarry Smith jj = a->j; 2753b2fbd54SBarry Smith for (i=0; i<nz; i++) { 276bfeeae90SHong Zhang collengths[jj[i]]++; 2773b2fbd54SBarry Smith } 2783b2fbd54SBarry Smith cia[0] = oshift; 2793b2fbd54SBarry Smith for (i=0; i<n; i++) { 2803b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 2813b2fbd54SBarry Smith } 282580bdb30SBarry Smith ierr = PetscArrayzero(collengths,n);CHKERRQ(ierr); 2833b2fbd54SBarry Smith jj = a->j; 284a93ec695SBarry Smith for (row=0; row<m; row++) { 285a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 286a93ec695SBarry Smith for (i=0; i<mr; i++) { 287bfeeae90SHong Zhang col = *jj++; 2882205254eSKarl Rupp 2893b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 2903b2fbd54SBarry Smith } 2913b2fbd54SBarry Smith } 292606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 2933b2fbd54SBarry Smith *ia = cia; *ja = cja; 2943b2fbd54SBarry Smith } 2953a40ed3dSBarry Smith PetscFunctionReturn(0); 2963b2fbd54SBarry Smith } 2973b2fbd54SBarry Smith 2981a83f524SJed Brown PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done) 2993b2fbd54SBarry Smith { 300dfbe8321SBarry Smith PetscErrorCode ierr; 301606d414cSSatish Balay 3023a40ed3dSBarry Smith PetscFunctionBegin; 3033a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 3043b2fbd54SBarry Smith 305606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 306606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 3073a40ed3dSBarry Smith PetscFunctionReturn(0); 3083b2fbd54SBarry Smith } 3093b2fbd54SBarry Smith 3107cee066cSHong Zhang /* 3117cee066cSHong Zhang MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from 3127cee066cSHong Zhang MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output 313040ebd07SHong Zhang spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ() 3147cee066cSHong Zhang */ 3157cee066cSHong 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) 3167cee066cSHong Zhang { 3177cee066cSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3187cee066cSHong Zhang PetscErrorCode ierr; 3197cee066cSHong Zhang PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n; 320071fcb05SBarry Smith PetscInt nz = a->i[m],row,mr,col,tmp; 3217cee066cSHong Zhang PetscInt *cspidx; 322071fcb05SBarry Smith const PetscInt *jj; 3237cee066cSHong Zhang 3247cee066cSHong Zhang PetscFunctionBegin; 3257cee066cSHong Zhang *nn = n; 3267cee066cSHong Zhang if (!ia) PetscFunctionReturn(0); 327625f6d37SHong Zhang 328b9e7e5c1SBarry Smith ierr = PetscCalloc1(n,&collengths);CHKERRQ(ierr); 329854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&cia);CHKERRQ(ierr); 330b9e7e5c1SBarry Smith ierr = PetscMalloc1(nz,&cja);CHKERRQ(ierr); 331b9e7e5c1SBarry Smith ierr = PetscMalloc1(nz,&cspidx);CHKERRQ(ierr); 3327cee066cSHong Zhang jj = a->j; 3337cee066cSHong Zhang for (i=0; i<nz; i++) { 3347cee066cSHong Zhang collengths[jj[i]]++; 3357cee066cSHong Zhang } 3367cee066cSHong Zhang cia[0] = oshift; 3377cee066cSHong Zhang for (i=0; i<n; i++) { 3387cee066cSHong Zhang cia[i+1] = cia[i] + collengths[i]; 3397cee066cSHong Zhang } 340580bdb30SBarry Smith ierr = PetscArrayzero(collengths,n);CHKERRQ(ierr); 3417cee066cSHong Zhang jj = a->j; 3427cee066cSHong Zhang for (row=0; row<m; row++) { 3437cee066cSHong Zhang mr = a->i[row+1] - a->i[row]; 3447cee066cSHong Zhang for (i=0; i<mr; i++) { 3457cee066cSHong Zhang col = *jj++; 346071fcb05SBarry Smith tmp = cia[col] + collengths[col]++ - oshift; 347071fcb05SBarry Smith cspidx[tmp] = a->i[row] + i; /* index of a->j */ 348071fcb05SBarry Smith cja[tmp] = row + oshift; 3497cee066cSHong Zhang } 3507cee066cSHong Zhang } 3517cee066cSHong Zhang ierr = PetscFree(collengths);CHKERRQ(ierr); 352071fcb05SBarry Smith *ia = cia; 353071fcb05SBarry Smith *ja = cja; 3547cee066cSHong Zhang *spidx = cspidx; 3557cee066cSHong Zhang PetscFunctionReturn(0); 3567cee066cSHong Zhang } 3577cee066cSHong Zhang 3587cee066cSHong 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) 3597cee066cSHong Zhang { 3607cee066cSHong Zhang PetscErrorCode ierr; 3617cee066cSHong Zhang 3627cee066cSHong Zhang PetscFunctionBegin; 3635243ef75SHong Zhang ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);CHKERRQ(ierr); 3647cee066cSHong Zhang ierr = PetscFree(*spidx);CHKERRQ(ierr); 3657cee066cSHong Zhang PetscFunctionReturn(0); 3667cee066cSHong Zhang } 3677cee066cSHong Zhang 36887d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[]) 36987d4246cSBarry Smith { 37087d4246cSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 37187d4246cSBarry Smith PetscInt *ai = a->i; 37287d4246cSBarry Smith PetscErrorCode ierr; 37387d4246cSBarry Smith 37487d4246cSBarry Smith PetscFunctionBegin; 375580bdb30SBarry Smith ierr = PetscArraycpy(a->a+ai[row],v,ai[row+1]-ai[row]);CHKERRQ(ierr); 376e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 377c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && ai[row+1]-ai[row]) A->offloadmask = PETSC_OFFLOAD_CPU; 378e2cf4d64SStefano Zampini #endif 37987d4246cSBarry Smith PetscFunctionReturn(0); 38087d4246cSBarry Smith } 38187d4246cSBarry Smith 382bd04181cSBarry Smith /* 383bd04181cSBarry Smith MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions 384bd04181cSBarry Smith 385bd04181cSBarry Smith - a single row of values is set with each call 386bd04181cSBarry Smith - no row or column indices are negative or (in error) larger than the number of rows or columns 387bd04181cSBarry Smith - the values are always added to the matrix, not set 388bd04181cSBarry Smith - no new locations are introduced in the nonzero structure of the matrix 389bd04181cSBarry Smith 3901f763a69SBarry Smith This does NOT assume the global column indices are sorted 391bd04181cSBarry Smith 3921f763a69SBarry Smith */ 393bd04181cSBarry Smith 394af0996ceSBarry Smith #include <petsc/private/isimpl.h> 395189e4007SBarry Smith PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 396189e4007SBarry Smith { 397189e4007SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3981f763a69SBarry Smith PetscInt low,high,t,row,nrow,i,col,l; 3991f763a69SBarry Smith const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j; 4001f763a69SBarry Smith PetscInt lastcol = -1; 401189e4007SBarry Smith MatScalar *ap,value,*aa = a->a; 402189e4007SBarry Smith const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices; 403189e4007SBarry Smith 404f38dd0b8SBarry Smith row = ridx[im[0]]; 4051f763a69SBarry Smith rp = aj + ai[row]; 4061f763a69SBarry Smith ap = aa + ai[row]; 4071f763a69SBarry Smith nrow = ailen[row]; 408189e4007SBarry Smith low = 0; 409189e4007SBarry Smith high = nrow; 410189e4007SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 411189e4007SBarry Smith col = cidx[in[l]]; 412f38dd0b8SBarry Smith value = v[l]; 413189e4007SBarry Smith 414189e4007SBarry Smith if (col <= lastcol) low = 0; 415189e4007SBarry Smith else high = nrow; 416189e4007SBarry Smith lastcol = col; 417189e4007SBarry Smith while (high-low > 5) { 418189e4007SBarry Smith t = (low+high)/2; 419189e4007SBarry Smith if (rp[t] > col) high = t; 420189e4007SBarry Smith else low = t; 421189e4007SBarry Smith } 422189e4007SBarry Smith for (i=low; i<high; i++) { 423189e4007SBarry Smith if (rp[i] == col) { 4241f763a69SBarry Smith ap[i] += value; 425189e4007SBarry Smith low = i + 1; 4261f763a69SBarry Smith break; 427189e4007SBarry Smith } 428189e4007SBarry Smith } 429189e4007SBarry Smith } 430e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 431c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU; 432e2cf4d64SStefano Zampini #endif 433f38dd0b8SBarry Smith return 0; 434189e4007SBarry Smith } 435189e4007SBarry Smith 43697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 43717ab2063SBarry Smith { 438416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 439e2ee6c50SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 44097f1f81fSBarry Smith PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen; 4416849ba73SBarry Smith PetscErrorCode ierr; 442e2ee6c50SBarry Smith PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1; 443d8cdefa3SHong Zhang MatScalar *ap=NULL,value=0.0,*aa = a->a; 444ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 445ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 446e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 447e2cf4d64SStefano Zampini PetscBool inserted = PETSC_FALSE; 448e2cf4d64SStefano Zampini #endif 44917ab2063SBarry Smith 4503a40ed3dSBarry Smith PetscFunctionBegin; 45117ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 452416022c9SBarry Smith row = im[k]; 4535ef9f2a5SBarry Smith if (row < 0) continue; 4542515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 455e32f2f54SBarry 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); 4563b2fbd54SBarry Smith #endif 457720833daSHong Zhang rp = aj + ai[row]; 458876c6284SHong Zhang if (!A->structure_only) ap = aa + ai[row]; 45917ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 460416022c9SBarry Smith low = 0; 461c71e6ed7SBarry Smith high = nrow; 46217ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 4635ef9f2a5SBarry Smith if (in[l] < 0) continue; 4642515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 465e32f2f54SBarry 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); 4663b2fbd54SBarry Smith #endif 467bfeeae90SHong Zhang col = in[l]; 468071fcb05SBarry Smith if (v && !A->structure_only) value = roworiented ? v[l + k*n] : v[k + l*m]; 469071fcb05SBarry Smith if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue; 47036db0b34SBarry Smith 4712205254eSKarl Rupp if (col <= lastcol) low = 0; 4722205254eSKarl Rupp else high = nrow; 473e2ee6c50SBarry Smith lastcol = col; 474416022c9SBarry Smith while (high-low > 5) { 475416022c9SBarry Smith t = (low+high)/2; 476416022c9SBarry Smith if (rp[t] > col) high = t; 477416022c9SBarry Smith else low = t; 47817ab2063SBarry Smith } 479416022c9SBarry Smith for (i=low; i<high; i++) { 48017ab2063SBarry Smith if (rp[i] > col) break; 48117ab2063SBarry Smith if (rp[i] == col) { 482876c6284SHong Zhang if (!A->structure_only) { 4830c0d7e18SFande Kong if (is == ADD_VALUES) { 4840c0d7e18SFande Kong ap[i] += value; 4850c0d7e18SFande Kong (void)PetscLogFlops(1.0); 4860c0d7e18SFande Kong } 48717ab2063SBarry Smith else ap[i] = value; 488e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 489e2cf4d64SStefano Zampini inserted = PETSC_TRUE; 490e2cf4d64SStefano Zampini #endif 491720833daSHong Zhang } 492e44c0bd4SBarry Smith low = i + 1; 49317ab2063SBarry Smith goto noinsert; 49417ab2063SBarry Smith } 49517ab2063SBarry Smith } 496dcd36c23SBarry Smith if (value == 0.0 && ignorezeroentries && row != col) goto noinsert; 497c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 498e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col); 499720833daSHong Zhang if (A->structure_only) { 500876c6284SHong Zhang MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar); 501720833daSHong Zhang } else { 502fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 503720833daSHong Zhang } 504c03d1d03SSatish Balay N = nrow++ - 1; a->nz++; high++; 505416022c9SBarry Smith /* shift up all the later entries in this row */ 506580bdb30SBarry Smith ierr = PetscArraymove(rp+i+1,rp+i,N-i+1);CHKERRQ(ierr); 50717ab2063SBarry Smith rp[i] = col; 508580bdb30SBarry Smith if (!A->structure_only){ 509580bdb30SBarry Smith ierr = PetscArraymove(ap+i+1,ap+i,N-i+1);CHKERRQ(ierr); 510580bdb30SBarry Smith ap[i] = value; 511580bdb30SBarry Smith } 512416022c9SBarry Smith low = i + 1; 513e56f5c9eSBarry Smith A->nonzerostate++; 514e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 515e2cf4d64SStefano Zampini inserted = PETSC_TRUE; 516e2cf4d64SStefano Zampini #endif 517e44c0bd4SBarry Smith noinsert:; 51817ab2063SBarry Smith } 51917ab2063SBarry Smith ailen[row] = nrow; 52017ab2063SBarry Smith } 521e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 522c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU; 523e2cf4d64SStefano Zampini #endif 5243a40ed3dSBarry Smith PetscFunctionReturn(0); 52517ab2063SBarry Smith } 52617ab2063SBarry Smith 527071fcb05SBarry Smith PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is) 528071fcb05SBarry Smith { 529071fcb05SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 530071fcb05SBarry Smith PetscInt *rp,k,row; 531071fcb05SBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 532071fcb05SBarry Smith PetscErrorCode ierr; 533071fcb05SBarry Smith PetscInt *aj = a->j; 534071fcb05SBarry Smith MatScalar *aa = a->a,*ap; 535071fcb05SBarry Smith 536071fcb05SBarry Smith PetscFunctionBegin; 537071fcb05SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 538071fcb05SBarry Smith row = im[k]; 539071fcb05SBarry Smith rp = aj + ai[row]; 540071fcb05SBarry Smith ap = aa + ai[row]; 541071fcb05SBarry Smith if (!A->was_assembled) { 542071fcb05SBarry Smith ierr = PetscMemcpy(rp,in,n*sizeof(PetscInt));CHKERRQ(ierr); 543071fcb05SBarry Smith } 544071fcb05SBarry Smith if (!A->structure_only) { 545071fcb05SBarry Smith if (v) { 546071fcb05SBarry Smith ierr = PetscMemcpy(ap,v,n*sizeof(PetscScalar));CHKERRQ(ierr); 547071fcb05SBarry Smith v += n; 548071fcb05SBarry Smith } else { 549071fcb05SBarry Smith ierr = PetscMemzero(ap,n*sizeof(PetscScalar));CHKERRQ(ierr); 550071fcb05SBarry Smith } 551071fcb05SBarry Smith } 552071fcb05SBarry Smith ailen[row] = n; 553071fcb05SBarry Smith a->nz += n; 554071fcb05SBarry Smith } 555e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 556c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU; 557e2cf4d64SStefano Zampini #endif 558071fcb05SBarry Smith PetscFunctionReturn(0); 559071fcb05SBarry Smith } 560071fcb05SBarry Smith 56181824310SBarry Smith 562a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[]) 5637eb43aa7SLois Curfman McInnes { 5647eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 56597f1f81fSBarry Smith PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 56697f1f81fSBarry Smith PetscInt *ai = a->i,*ailen = a->ilen; 56754f21887SBarry Smith MatScalar *ap,*aa = a->a; 5687eb43aa7SLois Curfman McInnes 5693a40ed3dSBarry Smith PetscFunctionBegin; 5707eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 5717eb43aa7SLois Curfman McInnes row = im[k]; 572e32f2f54SBarry Smith if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */ 573e32f2f54SBarry 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); 574bfeeae90SHong Zhang rp = aj + ai[row]; ap = aa + ai[row]; 5757eb43aa7SLois Curfman McInnes nrow = ailen[row]; 5767eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 577e32f2f54SBarry Smith if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */ 578e32f2f54SBarry 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); 579bfeeae90SHong Zhang col = in[l]; 5807eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 5817eb43aa7SLois Curfman McInnes while (high-low > 5) { 5827eb43aa7SLois Curfman McInnes t = (low+high)/2; 5837eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 5847eb43aa7SLois Curfman McInnes else low = t; 5857eb43aa7SLois Curfman McInnes } 5867eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 5877eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 5887eb43aa7SLois Curfman McInnes if (rp[i] == col) { 589b49de8d1SLois Curfman McInnes *v++ = ap[i]; 5907eb43aa7SLois Curfman McInnes goto finished; 5917eb43aa7SLois Curfman McInnes } 5927eb43aa7SLois Curfman McInnes } 59397e567efSBarry Smith *v++ = 0.0; 5947eb43aa7SLois Curfman McInnes finished:; 5957eb43aa7SLois Curfman McInnes } 5967eb43aa7SLois Curfman McInnes } 5973a40ed3dSBarry Smith PetscFunctionReturn(0); 5987eb43aa7SLois Curfman McInnes } 5997eb43aa7SLois Curfman McInnes 6003ea6fe3dSLisandro Dalcin PetscErrorCode MatView_SeqAIJ_Binary(Mat mat,PetscViewer viewer) 60117ab2063SBarry Smith { 6023ea6fe3dSLisandro Dalcin Mat_SeqAIJ *A = (Mat_SeqAIJ*)mat->data; 6033ea6fe3dSLisandro Dalcin PetscInt header[4],M,N,m,nz,i; 6043ea6fe3dSLisandro Dalcin PetscInt *rowlens; 6056849ba73SBarry Smith PetscErrorCode ierr; 60617ab2063SBarry Smith 6073a40ed3dSBarry Smith PetscFunctionBegin; 6083ea6fe3dSLisandro Dalcin ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 6092205254eSKarl Rupp 6103ea6fe3dSLisandro Dalcin M = mat->rmap->N; 6113ea6fe3dSLisandro Dalcin N = mat->cmap->N; 6123ea6fe3dSLisandro Dalcin m = mat->rmap->n; 6133ea6fe3dSLisandro Dalcin nz = A->nz; 614416022c9SBarry Smith 6153ea6fe3dSLisandro Dalcin /* write matrix header */ 6163ea6fe3dSLisandro Dalcin header[0] = MAT_FILE_CLASSID; 6173ea6fe3dSLisandro Dalcin header[1] = M; header[2] = N; header[3] = nz; 6183ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);CHKERRQ(ierr); 619416022c9SBarry Smith 6203ea6fe3dSLisandro Dalcin /* fill in and store row lengths */ 6213ea6fe3dSLisandro Dalcin ierr = PetscMalloc1(m,&rowlens);CHKERRQ(ierr); 6223ea6fe3dSLisandro Dalcin for (i=0; i<m; i++) rowlens[i] = A->i[i+1] - A->i[i]; 6233ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,rowlens,m,PETSC_INT);CHKERRQ(ierr); 6243ea6fe3dSLisandro Dalcin ierr = PetscFree(rowlens);CHKERRQ(ierr); 6253ea6fe3dSLisandro Dalcin /* store column indices */ 6263ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,A->j,nz,PETSC_INT);CHKERRQ(ierr); 627416022c9SBarry Smith /* store nonzero values */ 6283ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,A->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 629b37d52dbSMark F. Adams 6303ea6fe3dSLisandro Dalcin /* write block size option to the viewer's .info file */ 6313ea6fe3dSLisandro Dalcin ierr = MatView_Binary_BlockSizes(mat,viewer);CHKERRQ(ierr); 6323a40ed3dSBarry Smith PetscFunctionReturn(0); 63317ab2063SBarry Smith } 634416022c9SBarry Smith 6357dc0baabSHong Zhang static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer) 6367dc0baabSHong Zhang { 6377dc0baabSHong Zhang PetscErrorCode ierr; 6387dc0baabSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 6397dc0baabSHong Zhang PetscInt i,k,m=A->rmap->N; 6407dc0baabSHong Zhang 6417dc0baabSHong Zhang PetscFunctionBegin; 6427dc0baabSHong Zhang ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 6437dc0baabSHong Zhang for (i=0; i<m; i++) { 6447dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 6457dc0baabSHong Zhang for (k=a->i[i]; k<a->i[i+1]; k++) { 6467dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);CHKERRQ(ierr); 6477dc0baabSHong Zhang } 6487dc0baabSHong Zhang ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 6497dc0baabSHong Zhang } 6507dc0baabSHong Zhang ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 6517dc0baabSHong Zhang PetscFunctionReturn(0); 6527dc0baabSHong Zhang } 6537dc0baabSHong Zhang 65409573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer); 655cd155464SBarry Smith 656dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 657416022c9SBarry Smith { 658416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 659dfbe8321SBarry Smith PetscErrorCode ierr; 66060e0710aSBarry Smith PetscInt i,j,m = A->rmap->n; 661e060cb09SBarry Smith const char *name; 662f3ef73ceSBarry Smith PetscViewerFormat format; 66317ab2063SBarry Smith 6643a40ed3dSBarry Smith PetscFunctionBegin; 6657dc0baabSHong Zhang if (A->structure_only) { 6667dc0baabSHong Zhang ierr = MatView_SeqAIJ_ASCII_structonly(A,viewer);CHKERRQ(ierr); 6677dc0baabSHong Zhang PetscFunctionReturn(0); 6687dc0baabSHong Zhang } 66943e49210SHong Zhang 670b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 67171c2f376SKris Buschelman if (format == PETSC_VIEWER_ASCII_MATLAB) { 67297f1f81fSBarry Smith PetscInt nofinalvalue = 0; 67360e0710aSBarry Smith if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) { 674c337ccceSJed Brown /* Need a dummy value to ensure the dimension of the matrix. */ 675d00d2cf4SBarry Smith nofinalvalue = 1; 676d00d2cf4SBarry Smith } 677d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 678d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr); 67977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr); 680fbfe6fa7SJed Brown #if defined(PETSC_USE_COMPLEX) 681fbfe6fa7SJed Brown ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 682fbfe6fa7SJed Brown #else 68377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 684fbfe6fa7SJed Brown #endif 685b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 68617ab2063SBarry Smith 68717ab2063SBarry Smith for (i=0; i<m; i++) { 68860e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 689aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 690a9bf72d8SJed 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); 69117ab2063SBarry Smith #else 69260e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);CHKERRQ(ierr); 69317ab2063SBarry Smith #endif 69417ab2063SBarry Smith } 69517ab2063SBarry Smith } 696d00d2cf4SBarry Smith if (nofinalvalue) { 697c337ccceSJed Brown #if defined(PETSC_USE_COMPLEX) 698c337ccceSJed Brown ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e %18.16e\n",m,A->cmap->n,0.,0.);CHKERRQ(ierr); 699c337ccceSJed Brown #else 700d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr); 701c337ccceSJed Brown #endif 702d00d2cf4SBarry Smith } 703317d6ea6SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 704fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 705d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 7062950ac48SStefano Zampini } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 707cd155464SBarry Smith PetscFunctionReturn(0); 708fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 709d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 71044cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 71177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 71260e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 713aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 71436db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 71560e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 71636db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 71760e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 71836db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 71960e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 7206831982aSBarry Smith } 72144cd7ae7SLois Curfman McInnes #else 72260e0710aSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr);} 72344cd7ae7SLois Curfman McInnes #endif 72444cd7ae7SLois Curfman McInnes } 725b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 72644cd7ae7SLois Curfman McInnes } 727d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 728fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 72997f1f81fSBarry Smith PetscInt nzd=0,fshift=1,*sptr; 730d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 731854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&sptr);CHKERRQ(ierr); 732496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 733496be53dSLois Curfman McInnes sptr[i] = nzd+1; 73460e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 735496be53dSLois Curfman McInnes if (a->j[j] >= i) { 736aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 73736db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 738496be53dSLois Curfman McInnes #else 739496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 740496be53dSLois Curfman McInnes #endif 741496be53dSLois Curfman McInnes } 742496be53dSLois Curfman McInnes } 743496be53dSLois Curfman McInnes } 7442e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 74577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr); 7462e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 7472205254eSKarl Rupp if (i+4<m) { 7482205254eSKarl 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); 7492205254eSKarl Rupp } else if (i+3<m) { 7502205254eSKarl 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); 7512205254eSKarl Rupp } else if (i+2<m) { 7522205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr); 7532205254eSKarl Rupp } else if (i+1<m) { 7542205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr); 7552205254eSKarl Rupp } else if (i<m) { 7562205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr); 7572205254eSKarl Rupp } else { 7582205254eSKarl Rupp ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr); 7592205254eSKarl Rupp } 760496be53dSLois Curfman McInnes } 761b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 762606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 763496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 76460e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 76577431f27SBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);} 766496be53dSLois Curfman McInnes } 767b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 768496be53dSLois Curfman McInnes } 769b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 770496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 77160e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 772496be53dSLois Curfman McInnes if (a->j[j] >= i) { 773aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 77436db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 77560e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 7766831982aSBarry Smith } 777496be53dSLois Curfman McInnes #else 77860e0710aSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);CHKERRQ(ierr);} 779496be53dSLois Curfman McInnes #endif 780496be53dSLois Curfman McInnes } 781496be53dSLois Curfman McInnes } 782b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 783496be53dSLois Curfman McInnes } 784d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 785fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 78697f1f81fSBarry Smith PetscInt cnt = 0,jcnt; 78787828ca2SBarry Smith PetscScalar value; 78868f1ed48SBarry Smith #if defined(PETSC_USE_COMPLEX) 78968f1ed48SBarry Smith PetscBool realonly = PETSC_TRUE; 79068f1ed48SBarry Smith 79168f1ed48SBarry Smith for (i=0; i<a->i[m]; i++) { 79268f1ed48SBarry Smith if (PetscImaginaryPart(a->a[i]) != 0.0) { 79368f1ed48SBarry Smith realonly = PETSC_FALSE; 79468f1ed48SBarry Smith break; 79568f1ed48SBarry Smith } 79668f1ed48SBarry Smith } 79768f1ed48SBarry Smith #endif 79802594712SBarry Smith 799d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 80002594712SBarry Smith for (i=0; i<m; i++) { 80102594712SBarry Smith jcnt = 0; 802d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 803e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 80402594712SBarry Smith value = a->a[cnt++]; 805e24b481bSBarry Smith jcnt++; 80602594712SBarry Smith } else { 80702594712SBarry Smith value = 0.0; 80802594712SBarry Smith } 809aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 81068f1ed48SBarry Smith if (realonly) { 81160e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));CHKERRQ(ierr); 81268f1ed48SBarry Smith } else { 81360e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));CHKERRQ(ierr); 81468f1ed48SBarry Smith } 81502594712SBarry Smith #else 81660e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);CHKERRQ(ierr); 81702594712SBarry Smith #endif 81802594712SBarry Smith } 819b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 82002594712SBarry Smith } 821d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 8223c215bfdSMatthew Knepley } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) { 823150b93efSMatthew G. Knepley PetscInt fshift=1; 824d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 8253c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 82619303e72SJonathan Guyer ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");CHKERRQ(ierr); 8273c215bfdSMatthew Knepley #else 82819303e72SJonathan Guyer ierr = PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");CHKERRQ(ierr); 8293c215bfdSMatthew Knepley #endif 830d0f46423SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr); 8313c215bfdSMatthew Knepley for (i=0; i<m; i++) { 83260e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 8333c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX) 834a9a0e077SKarl 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); 8353c215bfdSMatthew Knepley #else 836150b93efSMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);CHKERRQ(ierr); 8373c215bfdSMatthew Knepley #endif 8383c215bfdSMatthew Knepley } 8393c215bfdSMatthew Knepley } 840d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 8413a40ed3dSBarry Smith } else { 842d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 843d5f3da31SBarry Smith if (A->factortype) { 84416cd7e1dSShri Abhyankar for (i=0; i<m; i++) { 84516cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 84616cd7e1dSShri Abhyankar /* L part */ 84760e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 84816cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 84916cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 85060e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 85116cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 8526712e2f1SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr); 85316cd7e1dSShri Abhyankar } else { 85460e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 85516cd7e1dSShri Abhyankar } 85616cd7e1dSShri Abhyankar #else 85760e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 85816cd7e1dSShri Abhyankar #endif 85916cd7e1dSShri Abhyankar } 86016cd7e1dSShri Abhyankar /* diagonal */ 86116cd7e1dSShri Abhyankar j = a->diag[i]; 86216cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 86316cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 86460e0710aSBarry 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); 86516cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 8666712e2f1SBarry 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); 86716cd7e1dSShri Abhyankar } else { 86860e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr); 86916cd7e1dSShri Abhyankar } 87016cd7e1dSShri Abhyankar #else 87160e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));CHKERRQ(ierr); 87216cd7e1dSShri Abhyankar #endif 87316cd7e1dSShri Abhyankar 87416cd7e1dSShri Abhyankar /* U part */ 87560e0710aSBarry Smith for (j=a->diag[i+1]+1; j<a->diag[i]; j++) { 87616cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX) 87716cd7e1dSShri Abhyankar if (PetscImaginaryPart(a->a[j]) > 0.0) { 87860e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 87916cd7e1dSShri Abhyankar } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 88022ab088eSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));CHKERRQ(ierr); 88116cd7e1dSShri Abhyankar } else { 88260e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 88316cd7e1dSShri Abhyankar } 88416cd7e1dSShri Abhyankar #else 88560e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 88616cd7e1dSShri Abhyankar #endif 88716cd7e1dSShri Abhyankar } 88816cd7e1dSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 88916cd7e1dSShri Abhyankar } 89016cd7e1dSShri Abhyankar } else { 89117ab2063SBarry Smith for (i=0; i<m; i++) { 89277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr); 89360e0710aSBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 894aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 89536db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 89660e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 89736db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 89860e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 8993a40ed3dSBarry Smith } else { 90060e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));CHKERRQ(ierr); 90117ab2063SBarry Smith } 90217ab2063SBarry Smith #else 90360e0710aSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);CHKERRQ(ierr); 90417ab2063SBarry Smith #endif 90517ab2063SBarry Smith } 906b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 90717ab2063SBarry Smith } 90816cd7e1dSShri Abhyankar } 909d00279f6SBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 91017ab2063SBarry Smith } 911b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 9123a40ed3dSBarry Smith PetscFunctionReturn(0); 913416022c9SBarry Smith } 914416022c9SBarry Smith 9159804daf3SBarry Smith #include <petscdraw.h> 916dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 917416022c9SBarry Smith { 918480ef9eaSBarry Smith Mat A = (Mat) Aa; 919416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 920dfbe8321SBarry Smith PetscErrorCode ierr; 921383922c3SLisandro Dalcin PetscInt i,j,m = A->rmap->n; 922383922c3SLisandro Dalcin int color; 923b05fc000SLisandro Dalcin PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r; 924b0a32e0cSBarry Smith PetscViewer viewer; 925f3ef73ceSBarry Smith PetscViewerFormat format; 926cddf8d76SBarry Smith 9273a40ed3dSBarry Smith PetscFunctionBegin; 928480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 929b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 930b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 931383922c3SLisandro Dalcin 932416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 9330513a670SBarry Smith 934fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 935383922c3SLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 9360513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 937b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 938416022c9SBarry Smith for (i=0; i<m; i++) { 939cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 940bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 941bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 94236db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 943b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 944cddf8d76SBarry Smith } 945cddf8d76SBarry Smith } 946b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 947cddf8d76SBarry Smith for (i=0; i<m; i++) { 948cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 949bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 950bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 951cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 952b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 953cddf8d76SBarry Smith } 954cddf8d76SBarry Smith } 955b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 956cddf8d76SBarry Smith for (i=0; i<m; i++) { 957cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 958bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 959bfeeae90SHong Zhang x_l = a->j[j]; x_r = x_l + 1.0; 96036db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 961b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 962416022c9SBarry Smith } 963416022c9SBarry Smith } 964383922c3SLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 9650513a670SBarry Smith } else { 9660513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 9670513a670SBarry Smith /* first determine max of all nonzero values */ 968b05fc000SLisandro Dalcin PetscReal minv = 0.0, maxv = 0.0; 969383922c3SLisandro Dalcin PetscInt nz = a->nz, count = 0; 970b0a32e0cSBarry Smith PetscDraw popup; 9710513a670SBarry Smith 9720513a670SBarry Smith for (i=0; i<nz; i++) { 9730513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 9740513a670SBarry Smith } 975383922c3SLisandro Dalcin if (minv >= maxv) maxv = minv + PETSC_SMALL; 976b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 97745f3bb6eSLisandro Dalcin ierr = PetscDrawScalePopup(popup,minv,maxv);CHKERRQ(ierr); 978383922c3SLisandro Dalcin 979383922c3SLisandro Dalcin ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr); 9800513a670SBarry Smith for (i=0; i<m; i++) { 981383922c3SLisandro Dalcin y_l = m - i - 1.0; 982383922c3SLisandro Dalcin y_r = y_l + 1.0; 983bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 984383922c3SLisandro Dalcin x_l = a->j[j]; 985383922c3SLisandro Dalcin x_r = x_l + 1.0; 986b05fc000SLisandro Dalcin color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv); 987b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 9880513a670SBarry Smith count++; 9890513a670SBarry Smith } 9900513a670SBarry Smith } 991383922c3SLisandro Dalcin ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr); 9920513a670SBarry Smith } 993480ef9eaSBarry Smith PetscFunctionReturn(0); 994480ef9eaSBarry Smith } 995cddf8d76SBarry Smith 9969804daf3SBarry Smith #include <petscdraw.h> 997dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 998480ef9eaSBarry Smith { 999dfbe8321SBarry Smith PetscErrorCode ierr; 1000b0a32e0cSBarry Smith PetscDraw draw; 100136db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 1002ace3abfcSBarry Smith PetscBool isnull; 1003480ef9eaSBarry Smith 1004480ef9eaSBarry Smith PetscFunctionBegin; 1005b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1006b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 1007480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 1008480ef9eaSBarry Smith 1009d0f46423SBarry Smith xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0; 1010480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 1011b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 1012832b7cebSLisandro Dalcin ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 1013b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 10140298fd71SBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr); 1015832b7cebSLisandro Dalcin ierr = PetscDrawSave(draw);CHKERRQ(ierr); 10163a40ed3dSBarry Smith PetscFunctionReturn(0); 1017416022c9SBarry Smith } 1018416022c9SBarry Smith 1019dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer) 1020416022c9SBarry Smith { 1021dfbe8321SBarry Smith PetscErrorCode ierr; 1022ace3abfcSBarry Smith PetscBool iascii,isbinary,isdraw; 1023416022c9SBarry Smith 10243a40ed3dSBarry Smith PetscFunctionBegin; 1025251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1026251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1027251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1028c45a1595SBarry Smith if (iascii) { 10293a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 10300f5bd95cSBarry Smith } else if (isbinary) { 10313a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 10320f5bd95cSBarry Smith } else if (isdraw) { 10333a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 103411aeaf0aSBarry Smith } 10354108e4d5SBarry Smith ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr); 10363a40ed3dSBarry Smith PetscFunctionReturn(0); 103717ab2063SBarry Smith } 103819bcc07fSBarry Smith 1039dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 104017ab2063SBarry Smith { 1041416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 10426849ba73SBarry Smith PetscErrorCode ierr; 1043580bdb30SBarry Smith PetscInt fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax; 1044d0f46423SBarry Smith PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0; 104554f21887SBarry Smith MatScalar *aa = a->a,*ap; 10463447b6efSHong Zhang PetscReal ratio = 0.6; 104717ab2063SBarry Smith 10483a40ed3dSBarry Smith PetscFunctionBegin; 10493a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 1050071fcb05SBarry Smith ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1051071fcb05SBarry Smith if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) PetscFunctionReturn(0); 105217ab2063SBarry Smith 105343ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 105417ab2063SBarry Smith for (i=1; i<m; i++) { 1055416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 105617ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 105794a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 105817ab2063SBarry Smith if (fshift) { 1059bfeeae90SHong Zhang ip = aj + ai[i]; 1060bfeeae90SHong Zhang ap = aa + ai[i]; 106117ab2063SBarry Smith N = ailen[i]; 1062580bdb30SBarry Smith ierr = PetscArraymove(ip-fshift,ip,N);CHKERRQ(ierr); 1063580bdb30SBarry Smith if (!A->structure_only) { 1064580bdb30SBarry Smith ierr = PetscArraymove(ap-fshift,ap,N);CHKERRQ(ierr); 106517ab2063SBarry Smith } 106617ab2063SBarry Smith } 106717ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 106817ab2063SBarry Smith } 106917ab2063SBarry Smith if (m) { 107017ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 107117ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 107217ab2063SBarry Smith } 10737b083b7cSBarry Smith 107417ab2063SBarry Smith /* reset ilen and imax for each row */ 10757b083b7cSBarry Smith a->nonzerorowcnt = 0; 1076396832f4SHong Zhang if (A->structure_only) { 1077071fcb05SBarry Smith ierr = PetscFree(a->imax);CHKERRQ(ierr); 1078071fcb05SBarry Smith ierr = PetscFree(a->ilen);CHKERRQ(ierr); 1079396832f4SHong Zhang } else { /* !A->structure_only */ 108017ab2063SBarry Smith for (i=0; i<m; i++) { 108117ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 10827b083b7cSBarry Smith a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0); 108317ab2063SBarry Smith } 1084396832f4SHong Zhang } 1085bfeeae90SHong Zhang a->nz = ai[m]; 108665e19b50SBarry 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); 108717ab2063SBarry Smith 108809f38230SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1089d0f46423SBarry 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); 1090ae15b995SBarry Smith ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr); 1091ae15b995SBarry Smith ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr); 10922205254eSKarl Rupp 10938e58a170SBarry Smith A->info.mallocs += a->reallocs; 1094dd5f02e7SSatish Balay a->reallocs = 0; 10956712e2f1SBarry Smith A->info.nz_unneeded = (PetscReal)fshift; 109636db0b34SBarry Smith a->rmax = rmax; 10974e220ebcSLois Curfman McInnes 1098396832f4SHong Zhang if (!A->structure_only) { 109911e456e1SBarry Smith ierr = MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr); 1100396832f4SHong Zhang } 11014108e4d5SBarry Smith ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr); 11023a40ed3dSBarry Smith PetscFunctionReturn(0); 110317ab2063SBarry Smith } 110417ab2063SBarry Smith 110599cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A) 110699cafbc1SBarry Smith { 110799cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 110899cafbc1SBarry Smith PetscInt i,nz = a->nz; 110954f21887SBarry Smith MatScalar *aa = a->a; 1110acf2f550SJed Brown PetscErrorCode ierr; 111199cafbc1SBarry Smith 111299cafbc1SBarry Smith PetscFunctionBegin; 111399cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]); 1114acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1115e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1116c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 1117e2cf4d64SStefano Zampini #endif 111899cafbc1SBarry Smith PetscFunctionReturn(0); 111999cafbc1SBarry Smith } 112099cafbc1SBarry Smith 112199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A) 112299cafbc1SBarry Smith { 112399cafbc1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 112499cafbc1SBarry Smith PetscInt i,nz = a->nz; 112554f21887SBarry Smith MatScalar *aa = a->a; 1126acf2f550SJed Brown PetscErrorCode ierr; 112799cafbc1SBarry Smith 112899cafbc1SBarry Smith PetscFunctionBegin; 112999cafbc1SBarry Smith for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]); 1130acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1131e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1132c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 1133e2cf4d64SStefano Zampini #endif 113499cafbc1SBarry Smith PetscFunctionReturn(0); 113599cafbc1SBarry Smith } 113699cafbc1SBarry Smith 1137dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A) 113817ab2063SBarry Smith { 1139416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1140dfbe8321SBarry Smith PetscErrorCode ierr; 11413a40ed3dSBarry Smith 11423a40ed3dSBarry Smith PetscFunctionBegin; 1143580bdb30SBarry Smith ierr = PetscArrayzero(a->a,a->i[A->rmap->n]);CHKERRQ(ierr); 1144acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 1145e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 1146c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 1147e2cf4d64SStefano Zampini #endif 11483a40ed3dSBarry Smith PetscFunctionReturn(0); 114917ab2063SBarry Smith } 1150416022c9SBarry Smith 1151dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A) 115217ab2063SBarry Smith { 1153416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1154dfbe8321SBarry Smith PetscErrorCode ierr; 1155d5d45c9bSBarry Smith 11563a40ed3dSBarry Smith PetscFunctionBegin; 1157aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1158d0f46423SBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz); 115917ab2063SBarry Smith #endif 1160e6b907acSBarry Smith ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr); 11616bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 11626bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 116305b42c5fSBarry Smith ierr = PetscFree(a->diag);CHKERRQ(ierr); 1164d48dcb14SBarry Smith ierr = PetscFree(a->ibdiag);CHKERRQ(ierr); 1165071fcb05SBarry Smith ierr = PetscFree(a->imax);CHKERRQ(ierr); 1166071fcb05SBarry Smith ierr = PetscFree(a->ilen);CHKERRQ(ierr); 1167846b4da1SFande Kong ierr = PetscFree(a->ipre);CHKERRQ(ierr); 116871f1c65dSBarry Smith ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr); 116905b42c5fSBarry Smith ierr = PetscFree(a->solve_work);CHKERRQ(ierr); 11706bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 117105b42c5fSBarry Smith ierr = PetscFree(a->saved_values);CHKERRQ(ierr); 11726bf464f9SBarry Smith ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr); 1173cd6b891eSBarry Smith ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr); 11740b7e3e3dSHong Zhang ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr); 1175a30b2313SHong Zhang 11764108e4d5SBarry Smith ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr); 1177bf0cc555SLisandro Dalcin ierr = PetscFree(A->data);CHKERRQ(ierr); 1178901853e0SKris Buschelman 1179dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr); 1180bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);CHKERRQ(ierr); 1181bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr); 1182bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr); 1183bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);CHKERRQ(ierr); 1184bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);CHKERRQ(ierr); 1185bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);CHKERRQ(ierr); 11864222ddf1SHong Zhang 11874222ddf1SHong Zhang #if defined(PETSC_HAVE_CUDA) 11884222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcusparse_C",NULL);CHKERRQ(ierr); 11894222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatSetFromOptions_seqaijcusparse_seqaij_C",NULL);CHKERRQ(ierr); 11904222ddf1SHong Zhang #endif 11914222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcrl_C",NULL);CHKERRQ(ierr); 1192af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 1193af8000cdSHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);CHKERRQ(ierr); 1194af8000cdSHong Zhang #endif 119563c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE) 119663c07aadSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);CHKERRQ(ierr); 11974222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",NULL);CHKERRQ(ierr); 119863c07aadSStefano Zampini #endif 1199b49cda9fSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);CHKERRQ(ierr); 1200c9225affSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);CHKERRQ(ierr); 1201c9225affSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);CHKERRQ(ierr); 1202bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);CHKERRQ(ierr); 1203bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);CHKERRQ(ierr); 1204846b4da1SFande Kong ierr = PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);CHKERRQ(ierr); 1205bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr); 1206bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);CHKERRQ(ierr); 12074222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_is_seqaij_C",NULL);CHKERRQ(ierr); 12084222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqdense_seqaij_C",NULL);CHKERRQ(ierr); 12094222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaij_seqaij_C",NULL);CHKERRQ(ierr); 12103a40ed3dSBarry Smith PetscFunctionReturn(0); 121117ab2063SBarry Smith } 121217ab2063SBarry Smith 1213ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg) 121417ab2063SBarry Smith { 1215416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12164846f1f5SKris Buschelman PetscErrorCode ierr; 12173a40ed3dSBarry Smith 12183a40ed3dSBarry Smith PetscFunctionBegin; 1219a65d3064SKris Buschelman switch (op) { 1220a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 12214e0d8c25SBarry Smith a->roworiented = flg; 1222a65d3064SKris Buschelman break; 1223a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 1224a9817697SBarry Smith a->keepnonzeropattern = flg; 1225a65d3064SKris Buschelman break; 1226512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 1227512a5fc5SBarry Smith a->nonew = (flg ? 0 : 1); 1228a65d3064SKris Buschelman break; 1229a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 12304e0d8c25SBarry Smith a->nonew = (flg ? -1 : 0); 1231a65d3064SKris Buschelman break; 1232a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 12334e0d8c25SBarry Smith a->nonew = (flg ? -2 : 0); 1234a65d3064SKris Buschelman break; 123528b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 123628b2fa4aSMatthew Knepley a->nounused = (flg ? -1 : 0); 123728b2fa4aSMatthew Knepley break; 1238a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 12394e0d8c25SBarry Smith a->ignorezeroentries = flg; 12400df259c2SBarry Smith break; 12413d472b54SHong Zhang case MAT_SPD: 1242b1646e73SJed Brown case MAT_SYMMETRIC: 1243b1646e73SJed Brown case MAT_STRUCTURALLY_SYMMETRIC: 1244b1646e73SJed Brown case MAT_HERMITIAN: 1245b1646e73SJed Brown case MAT_SYMMETRY_ETERNAL: 1246957cac9fSHong Zhang case MAT_STRUCTURE_ONLY: 12475021d80fSJed Brown /* These options are handled directly by MatSetOption() */ 12485021d80fSJed Brown break; 12494e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1250a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 1251a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 1252290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 1253a65d3064SKris Buschelman break; 1254b87ac2d8SJed Brown case MAT_USE_INODES: 1255b87ac2d8SJed Brown /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */ 1256b87ac2d8SJed Brown break; 1257c10200c1SHong Zhang case MAT_SUBMAT_SINGLEIS: 1258c10200c1SHong Zhang A->submat_singleis = flg; 1259c10200c1SHong Zhang break; 1260071fcb05SBarry Smith case MAT_SORTED_FULL: 1261071fcb05SBarry Smith if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull; 1262071fcb05SBarry Smith else A->ops->setvalues = MatSetValues_SeqAIJ; 1263071fcb05SBarry Smith break; 1264a65d3064SKris Buschelman default: 1265e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 1266a65d3064SKris Buschelman } 12674108e4d5SBarry Smith ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr); 12683a40ed3dSBarry Smith PetscFunctionReturn(0); 126917ab2063SBarry Smith } 127017ab2063SBarry Smith 1271dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v) 127217ab2063SBarry Smith { 1273416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12746849ba73SBarry Smith PetscErrorCode ierr; 1275fdc842d1SBarry Smith PetscInt i,j,n,*ai=a->i,*aj=a->j; 1276fdc842d1SBarry Smith PetscScalar *aa=a->a,*x; 127717ab2063SBarry Smith 12783a40ed3dSBarry Smith PetscFunctionBegin; 1279d3e70bfaSHong Zhang ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 1280e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 128135e7444dSHong Zhang 1282d5f3da31SBarry Smith if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) { 1283d3e70bfaSHong Zhang PetscInt *diag=a->diag; 1284fdc842d1SBarry Smith ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr); 12852c990fa1SHong Zhang for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]]; 1286fdc842d1SBarry Smith ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr); 128735e7444dSHong Zhang PetscFunctionReturn(0); 128835e7444dSHong Zhang } 128935e7444dSHong Zhang 1290fdc842d1SBarry Smith ierr = VecGetArrayWrite(v,&x);CHKERRQ(ierr); 129135e7444dSHong Zhang for (i=0; i<n; i++) { 1292fdc842d1SBarry Smith x[i] = 0.0; 129335e7444dSHong Zhang for (j=ai[i]; j<ai[i+1]; j++) { 129435e7444dSHong Zhang if (aj[j] == i) { 129535e7444dSHong Zhang x[i] = aa[j]; 129617ab2063SBarry Smith break; 129717ab2063SBarry Smith } 129817ab2063SBarry Smith } 129917ab2063SBarry Smith } 1300fdc842d1SBarry Smith ierr = VecRestoreArrayWrite(v,&x);CHKERRQ(ierr); 13013a40ed3dSBarry Smith PetscFunctionReturn(0); 130217ab2063SBarry Smith } 130317ab2063SBarry Smith 1304c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 1305dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 130617ab2063SBarry Smith { 1307416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1308d9ca1df4SBarry Smith PetscScalar *y; 1309d9ca1df4SBarry Smith const PetscScalar *x; 1310dfbe8321SBarry Smith PetscErrorCode ierr; 1311d0f46423SBarry Smith PetscInt m = A->rmap->n; 13125c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1313d9ca1df4SBarry Smith const MatScalar *v; 1314a77337e4SBarry Smith PetscScalar alpha; 1315d9ca1df4SBarry Smith PetscInt n,i,j; 1316d9ca1df4SBarry Smith const PetscInt *idx,*ii,*ridx=NULL; 13173447b6efSHong Zhang Mat_CompressedRow cprow = a->compressedrow; 1318ace3abfcSBarry Smith PetscBool usecprow = cprow.use; 13195c897100SBarry Smith #endif 132017ab2063SBarry Smith 13213a40ed3dSBarry Smith PetscFunctionBegin; 13222e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 1323d9ca1df4SBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 13241ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 13255c897100SBarry Smith 13265c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 1327bfeeae90SHong Zhang fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y); 13285c897100SBarry Smith #else 13293447b6efSHong Zhang if (usecprow) { 13303447b6efSHong Zhang m = cprow.nrows; 13313447b6efSHong Zhang ii = cprow.i; 13327b2bb3b9SHong Zhang ridx = cprow.rindex; 13333447b6efSHong Zhang } else { 13343447b6efSHong Zhang ii = a->i; 13353447b6efSHong Zhang } 133617ab2063SBarry Smith for (i=0; i<m; i++) { 13373447b6efSHong Zhang idx = a->j + ii[i]; 13383447b6efSHong Zhang v = a->a + ii[i]; 13393447b6efSHong Zhang n = ii[i+1] - ii[i]; 13403447b6efSHong Zhang if (usecprow) { 13417b2bb3b9SHong Zhang alpha = x[ridx[i]]; 13423447b6efSHong Zhang } else { 134317ab2063SBarry Smith alpha = x[i]; 13443447b6efSHong Zhang } 134504fbf559SBarry Smith for (j=0; j<n; j++) y[idx[j]] += alpha*v[j]; 134617ab2063SBarry Smith } 13475c897100SBarry Smith #endif 1348dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1349d9ca1df4SBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 13501ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 13513a40ed3dSBarry Smith PetscFunctionReturn(0); 135217ab2063SBarry Smith } 135317ab2063SBarry Smith 1354dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 13555c897100SBarry Smith { 1356dfbe8321SBarry Smith PetscErrorCode ierr; 13575c897100SBarry Smith 13585c897100SBarry Smith PetscFunctionBegin; 1359170fe5c8SBarry Smith ierr = VecSet(yy,0.0);CHKERRQ(ierr); 13605c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 13615c897100SBarry Smith PetscFunctionReturn(0); 13625c897100SBarry Smith } 13635c897100SBarry Smith 1364c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h> 136578b84d54SShri Abhyankar 1366dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 136717ab2063SBarry Smith { 1368416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1369d9fead3dSBarry Smith PetscScalar *y; 137054f21887SBarry Smith const PetscScalar *x; 137154f21887SBarry Smith const MatScalar *aa; 1372dfbe8321SBarry Smith PetscErrorCode ierr; 1373003131ecSBarry Smith PetscInt m=A->rmap->n; 13740298fd71SBarry Smith const PetscInt *aj,*ii,*ridx=NULL; 13757b083b7cSBarry Smith PetscInt n,i; 1376362ced78SSatish Balay PetscScalar sum; 1377ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 137817ab2063SBarry Smith 1379b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 138097952fefSHong Zhang #pragma disjoint(*x,*y,*aa) 1381fee21e36SBarry Smith #endif 1382fee21e36SBarry Smith 13833a40ed3dSBarry Smith PetscFunctionBegin; 13843649974fSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 13851ebc52fbSHong Zhang ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 1386416022c9SBarry Smith ii = a->i; 13874eb6d288SHong Zhang if (usecprow) { /* use compressed row format */ 1388580bdb30SBarry Smith ierr = PetscArrayzero(y,m);CHKERRQ(ierr); 138997952fefSHong Zhang m = a->compressedrow.nrows; 139097952fefSHong Zhang ii = a->compressedrow.i; 139197952fefSHong Zhang ridx = a->compressedrow.rindex; 139297952fefSHong Zhang for (i=0; i<m; i++) { 139397952fefSHong Zhang n = ii[i+1] - ii[i]; 139497952fefSHong Zhang aj = a->j + ii[i]; 139597952fefSHong Zhang aa = a->a + ii[i]; 139697952fefSHong Zhang sum = 0.0; 1397003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 1398003131ecSBarry Smith /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 139997952fefSHong Zhang y[*ridx++] = sum; 140097952fefSHong Zhang } 140197952fefSHong Zhang } else { /* do not use compressed row format */ 1402b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 14033d3eaba7SBarry Smith aj = a->j; 14043d3eaba7SBarry Smith aa = a->a; 1405b05257ddSBarry Smith fortranmultaij_(&m,x,ii,aj,aa,y); 1406b05257ddSBarry Smith #else 140717ab2063SBarry Smith for (i=0; i<m; i++) { 1408003131ecSBarry Smith n = ii[i+1] - ii[i]; 1409003131ecSBarry Smith aj = a->j + ii[i]; 1410003131ecSBarry Smith aa = a->a + ii[i]; 141117ab2063SBarry Smith sum = 0.0; 1412003131ecSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 141317ab2063SBarry Smith y[i] = sum; 141417ab2063SBarry Smith } 14158d195f9aSBarry Smith #endif 1416b05257ddSBarry Smith } 14177b083b7cSBarry Smith ierr = PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);CHKERRQ(ierr); 14183649974fSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 14191ebc52fbSHong Zhang ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 14203a40ed3dSBarry Smith PetscFunctionReturn(0); 142117ab2063SBarry Smith } 142217ab2063SBarry Smith 1423b434eb95SMatthew G. Knepley PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy) 1424b434eb95SMatthew G. Knepley { 1425b434eb95SMatthew G. Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1426b434eb95SMatthew G. Knepley PetscScalar *y; 1427b434eb95SMatthew G. Knepley const PetscScalar *x; 1428b434eb95SMatthew G. Knepley const MatScalar *aa; 1429b434eb95SMatthew G. Knepley PetscErrorCode ierr; 1430b434eb95SMatthew G. Knepley PetscInt m=A->rmap->n; 1431b434eb95SMatthew G. Knepley const PetscInt *aj,*ii,*ridx=NULL; 1432b434eb95SMatthew G. Knepley PetscInt n,i,nonzerorow=0; 1433b434eb95SMatthew G. Knepley PetscScalar sum; 1434b434eb95SMatthew G. Knepley PetscBool usecprow=a->compressedrow.use; 1435b434eb95SMatthew G. Knepley 1436b434eb95SMatthew G. Knepley #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 1437b434eb95SMatthew G. Knepley #pragma disjoint(*x,*y,*aa) 1438b434eb95SMatthew G. Knepley #endif 1439b434eb95SMatthew G. Knepley 1440b434eb95SMatthew G. Knepley PetscFunctionBegin; 1441b434eb95SMatthew G. Knepley ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1442b434eb95SMatthew G. Knepley ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 1443b434eb95SMatthew G. Knepley if (usecprow) { /* use compressed row format */ 1444b434eb95SMatthew G. Knepley m = a->compressedrow.nrows; 1445b434eb95SMatthew G. Knepley ii = a->compressedrow.i; 1446b434eb95SMatthew G. Knepley ridx = a->compressedrow.rindex; 1447b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1448b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1449b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1450b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1451b434eb95SMatthew G. Knepley sum = 0.0; 1452b434eb95SMatthew G. Knepley nonzerorow += (n>0); 1453b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1454b434eb95SMatthew G. Knepley /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */ 1455b434eb95SMatthew G. Knepley y[*ridx++] = sum; 1456b434eb95SMatthew G. Knepley } 1457b434eb95SMatthew G. Knepley } else { /* do not use compressed row format */ 14583d3eaba7SBarry Smith ii = a->i; 1459b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1460b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1461b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1462b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1463b434eb95SMatthew G. Knepley sum = 0.0; 1464b434eb95SMatthew G. Knepley nonzerorow += (n>0); 1465b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1466b434eb95SMatthew G. Knepley y[i] = sum; 1467b434eb95SMatthew G. Knepley } 1468b434eb95SMatthew G. Knepley } 1469b434eb95SMatthew G. Knepley ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr); 1470b434eb95SMatthew G. Knepley ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1471b434eb95SMatthew G. Knepley ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 1472b434eb95SMatthew G. Knepley PetscFunctionReturn(0); 1473b434eb95SMatthew G. Knepley } 1474b434eb95SMatthew G. Knepley 1475b434eb95SMatthew G. Knepley PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1476b434eb95SMatthew G. Knepley { 1477b434eb95SMatthew G. Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1478b434eb95SMatthew G. Knepley PetscScalar *y,*z; 1479b434eb95SMatthew G. Knepley const PetscScalar *x; 1480b434eb95SMatthew G. Knepley const MatScalar *aa; 1481b434eb95SMatthew G. Knepley PetscErrorCode ierr; 1482b434eb95SMatthew G. Knepley PetscInt m = A->rmap->n,*aj,*ii; 1483b434eb95SMatthew G. Knepley PetscInt n,i,*ridx=NULL; 1484b434eb95SMatthew G. Knepley PetscScalar sum; 1485b434eb95SMatthew G. Knepley PetscBool usecprow=a->compressedrow.use; 1486b434eb95SMatthew G. Knepley 1487b434eb95SMatthew G. Knepley PetscFunctionBegin; 1488b434eb95SMatthew G. Knepley ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1489d9ca1df4SBarry Smith ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 1490b434eb95SMatthew G. Knepley if (usecprow) { /* use compressed row format */ 1491b434eb95SMatthew G. Knepley if (zz != yy) { 1492580bdb30SBarry Smith ierr = PetscArraycpy(z,y,m);CHKERRQ(ierr); 1493b434eb95SMatthew G. Knepley } 1494b434eb95SMatthew G. Knepley m = a->compressedrow.nrows; 1495b434eb95SMatthew G. Knepley ii = a->compressedrow.i; 1496b434eb95SMatthew G. Knepley ridx = a->compressedrow.rindex; 1497b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1498b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1499b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1500b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1501b434eb95SMatthew G. Knepley sum = y[*ridx]; 1502b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1503b434eb95SMatthew G. Knepley z[*ridx++] = sum; 1504b434eb95SMatthew G. Knepley } 1505b434eb95SMatthew G. Knepley } else { /* do not use compressed row format */ 15063d3eaba7SBarry Smith ii = a->i; 1507b434eb95SMatthew G. Knepley for (i=0; i<m; i++) { 1508b434eb95SMatthew G. Knepley n = ii[i+1] - ii[i]; 1509b434eb95SMatthew G. Knepley aj = a->j + ii[i]; 1510b434eb95SMatthew G. Knepley aa = a->a + ii[i]; 1511b434eb95SMatthew G. Knepley sum = y[i]; 1512b434eb95SMatthew G. Knepley PetscSparseDenseMaxDot(sum,x,aa,aj,n); 1513b434eb95SMatthew G. Knepley z[i] = sum; 1514b434eb95SMatthew G. Knepley } 1515b434eb95SMatthew G. Knepley } 1516b434eb95SMatthew G. Knepley ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1517b434eb95SMatthew G. Knepley ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1518d9ca1df4SBarry Smith ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 1519b434eb95SMatthew G. Knepley PetscFunctionReturn(0); 1520b434eb95SMatthew G. Knepley } 1521b434eb95SMatthew G. Knepley 1522c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h> 1523dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 152417ab2063SBarry Smith { 1525416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1526f15663dcSBarry Smith PetscScalar *y,*z; 1527f15663dcSBarry Smith const PetscScalar *x; 152854f21887SBarry Smith const MatScalar *aa; 1529dfbe8321SBarry Smith PetscErrorCode ierr; 1530d9ca1df4SBarry Smith const PetscInt *aj,*ii,*ridx=NULL; 1531d9ca1df4SBarry Smith PetscInt m = A->rmap->n,n,i; 1532362ced78SSatish Balay PetscScalar sum; 1533ace3abfcSBarry Smith PetscBool usecprow=a->compressedrow.use; 15349ea0dfa2SSatish Balay 15353a40ed3dSBarry Smith PetscFunctionBegin; 1536f15663dcSBarry Smith ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr); 1537d9ca1df4SBarry Smith ierr = VecGetArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 15384eb6d288SHong Zhang if (usecprow) { /* use compressed row format */ 15394eb6d288SHong Zhang if (zz != yy) { 1540580bdb30SBarry Smith ierr = PetscArraycpy(z,y,m);CHKERRQ(ierr); 15414eb6d288SHong Zhang } 154297952fefSHong Zhang m = a->compressedrow.nrows; 154397952fefSHong Zhang ii = a->compressedrow.i; 154497952fefSHong Zhang ridx = a->compressedrow.rindex; 154597952fefSHong Zhang for (i=0; i<m; i++) { 154697952fefSHong Zhang n = ii[i+1] - ii[i]; 154797952fefSHong Zhang aj = a->j + ii[i]; 154897952fefSHong Zhang aa = a->a + ii[i]; 154997952fefSHong Zhang sum = y[*ridx]; 1550f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 155197952fefSHong Zhang z[*ridx++] = sum; 155297952fefSHong Zhang } 155397952fefSHong Zhang } else { /* do not use compressed row format */ 15543d3eaba7SBarry Smith ii = a->i; 1555f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 15563d3eaba7SBarry Smith aj = a->j; 15573d3eaba7SBarry Smith aa = a->a; 1558f15663dcSBarry Smith fortranmultaddaij_(&m,x,ii,aj,aa,y,z); 1559f15663dcSBarry Smith #else 156017ab2063SBarry Smith for (i=0; i<m; i++) { 1561f15663dcSBarry Smith n = ii[i+1] - ii[i]; 1562f15663dcSBarry Smith aj = a->j + ii[i]; 1563f15663dcSBarry Smith aa = a->a + ii[i]; 156417ab2063SBarry Smith sum = y[i]; 1565f15663dcSBarry Smith PetscSparseDensePlusDot(sum,x,aa,aj,n); 156617ab2063SBarry Smith z[i] = sum; 156717ab2063SBarry Smith } 156802ab625aSSatish Balay #endif 1569f15663dcSBarry Smith } 1570dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1571f15663dcSBarry Smith ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr); 1572d9ca1df4SBarry Smith ierr = VecRestoreArrayPair(yy,zz,&y,&z);CHKERRQ(ierr); 15733a40ed3dSBarry Smith PetscFunctionReturn(0); 157417ab2063SBarry Smith } 157517ab2063SBarry Smith 157617ab2063SBarry Smith /* 157717ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 157817ab2063SBarry Smith */ 1579dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A) 158017ab2063SBarry Smith { 1581416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 15826849ba73SBarry Smith PetscErrorCode ierr; 1583d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n; 158417ab2063SBarry Smith 15853a40ed3dSBarry Smith PetscFunctionBegin; 158609f38230SBarry Smith if (!a->diag) { 1587785e854fSJed Brown ierr = PetscMalloc1(m,&a->diag);CHKERRQ(ierr); 15883bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));CHKERRQ(ierr); 158909f38230SBarry Smith } 1590d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 159109f38230SBarry Smith a->diag[i] = a->i[i+1]; 1592bfeeae90SHong Zhang for (j=a->i[i]; j<a->i[i+1]; j++) { 1593bfeeae90SHong Zhang if (a->j[j] == i) { 159409f38230SBarry Smith a->diag[i] = j; 159517ab2063SBarry Smith break; 159617ab2063SBarry Smith } 159717ab2063SBarry Smith } 159817ab2063SBarry Smith } 15993a40ed3dSBarry Smith PetscFunctionReturn(0); 160017ab2063SBarry Smith } 160117ab2063SBarry Smith 160261ecd0c6SBarry Smith PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v) 160361ecd0c6SBarry Smith { 160461ecd0c6SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 160561ecd0c6SBarry Smith const PetscInt *diag = (const PetscInt*)a->diag; 160661ecd0c6SBarry Smith const PetscInt *ii = (const PetscInt*) a->i; 160761ecd0c6SBarry Smith PetscInt i,*mdiag = NULL; 160861ecd0c6SBarry Smith PetscErrorCode ierr; 160961ecd0c6SBarry Smith PetscInt cnt = 0; /* how many diagonals are missing */ 161061ecd0c6SBarry Smith 161161ecd0c6SBarry Smith PetscFunctionBegin; 161261ecd0c6SBarry Smith if (!A->preallocated || !a->nz) { 161361ecd0c6SBarry Smith ierr = MatSeqAIJSetPreallocation(A,1,NULL);CHKERRQ(ierr); 161461ecd0c6SBarry Smith ierr = MatShift_Basic(A,v);CHKERRQ(ierr); 161561ecd0c6SBarry Smith PetscFunctionReturn(0); 161661ecd0c6SBarry Smith } 161761ecd0c6SBarry Smith 161861ecd0c6SBarry Smith if (a->diagonaldense) { 161961ecd0c6SBarry Smith cnt = 0; 162061ecd0c6SBarry Smith } else { 162161ecd0c6SBarry Smith ierr = PetscCalloc1(A->rmap->n,&mdiag);CHKERRQ(ierr); 162261ecd0c6SBarry Smith for (i=0; i<A->rmap->n; i++) { 162361ecd0c6SBarry Smith if (diag[i] >= ii[i+1]) { 162461ecd0c6SBarry Smith cnt++; 162561ecd0c6SBarry Smith mdiag[i] = 1; 162661ecd0c6SBarry Smith } 162761ecd0c6SBarry Smith } 162861ecd0c6SBarry Smith } 162961ecd0c6SBarry Smith if (!cnt) { 163061ecd0c6SBarry Smith ierr = MatShift_Basic(A,v);CHKERRQ(ierr); 163161ecd0c6SBarry Smith } else { 1632b6f2aa54SBarry Smith PetscScalar *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */ 1633b6f2aa54SBarry Smith PetscInt *oldj = a->j, *oldi = a->i; 163461ecd0c6SBarry Smith PetscBool singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij; 163561ecd0c6SBarry Smith 163661ecd0c6SBarry Smith a->a = NULL; 163761ecd0c6SBarry Smith a->j = NULL; 163861ecd0c6SBarry Smith a->i = NULL; 163961ecd0c6SBarry Smith /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */ 164061ecd0c6SBarry Smith for (i=0; i<A->rmap->n; i++) { 164161ecd0c6SBarry Smith a->imax[i] += mdiag[i]; 1642447d62f5SStefano Zampini a->imax[i] = PetscMin(a->imax[i],A->cmap->n); 164361ecd0c6SBarry Smith } 164461ecd0c6SBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);CHKERRQ(ierr); 164561ecd0c6SBarry Smith 164661ecd0c6SBarry Smith /* copy old values into new matrix data structure */ 164761ecd0c6SBarry Smith for (i=0; i<A->rmap->n; i++) { 164861ecd0c6SBarry Smith ierr = MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);CHKERRQ(ierr); 1649447d62f5SStefano Zampini if (i < A->cmap->n) { 165061ecd0c6SBarry Smith ierr = MatSetValue(A,i,i,v,ADD_VALUES);CHKERRQ(ierr); 165161ecd0c6SBarry Smith } 1652447d62f5SStefano Zampini } 165361ecd0c6SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 165461ecd0c6SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 165561ecd0c6SBarry Smith if (singlemalloc) { 165661ecd0c6SBarry Smith ierr = PetscFree3(olda,oldj,oldi);CHKERRQ(ierr); 165761ecd0c6SBarry Smith } else { 165861ecd0c6SBarry Smith if (free_a) {ierr = PetscFree(olda);CHKERRQ(ierr);} 165961ecd0c6SBarry Smith if (free_ij) {ierr = PetscFree(oldj);CHKERRQ(ierr);} 166061ecd0c6SBarry Smith if (free_ij) {ierr = PetscFree(oldi);CHKERRQ(ierr);} 166161ecd0c6SBarry Smith } 166261ecd0c6SBarry Smith } 166361ecd0c6SBarry Smith ierr = PetscFree(mdiag);CHKERRQ(ierr); 166461ecd0c6SBarry Smith a->diagonaldense = PETSC_TRUE; 166561ecd0c6SBarry Smith PetscFunctionReturn(0); 166661ecd0c6SBarry Smith } 166761ecd0c6SBarry Smith 1668be5855fcSBarry Smith /* 1669be5855fcSBarry Smith Checks for missing diagonals 1670be5855fcSBarry Smith */ 1671ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool *missing,PetscInt *d) 1672be5855fcSBarry Smith { 1673be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 16747734d3b5SMatthew G. Knepley PetscInt *diag,*ii = a->i,i; 1675994fe344SLisandro Dalcin PetscErrorCode ierr; 1676be5855fcSBarry Smith 1677be5855fcSBarry Smith PetscFunctionBegin; 167809f38230SBarry Smith *missing = PETSC_FALSE; 16797734d3b5SMatthew G. Knepley if (A->rmap->n > 0 && !ii) { 168009f38230SBarry Smith *missing = PETSC_TRUE; 168109f38230SBarry Smith if (d) *d = 0; 1682994fe344SLisandro Dalcin ierr = PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");CHKERRQ(ierr); 168309f38230SBarry Smith } else { 168401445905SHong Zhang PetscInt n; 168501445905SHong Zhang n = PetscMin(A->rmap->n, A->cmap->n); 1686f1e2ffcdSBarry Smith diag = a->diag; 168701445905SHong Zhang for (i=0; i<n; i++) { 16887734d3b5SMatthew G. Knepley if (diag[i] >= ii[i+1]) { 168909f38230SBarry Smith *missing = PETSC_TRUE; 169009f38230SBarry Smith if (d) *d = i; 1691994fe344SLisandro Dalcin ierr = PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);CHKERRQ(ierr); 1692358d2f5dSShri Abhyankar break; 169309f38230SBarry Smith } 1694be5855fcSBarry Smith } 1695be5855fcSBarry Smith } 1696be5855fcSBarry Smith PetscFunctionReturn(0); 1697be5855fcSBarry Smith } 1698be5855fcSBarry Smith 16990da83c2eSBarry Smith #include <petscblaslapack.h> 17000da83c2eSBarry Smith #include <petsc/private/kernels/blockinvert.h> 17010da83c2eSBarry Smith 17020da83c2eSBarry Smith /* 17030da83c2eSBarry Smith Note that values is allocated externally by the PC and then passed into this routine 17040da83c2eSBarry Smith */ 17050da83c2eSBarry Smith PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag) 17060da83c2eSBarry Smith { 17070da83c2eSBarry Smith PetscErrorCode ierr; 17080da83c2eSBarry Smith PetscInt n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots; 17090da83c2eSBarry Smith PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE; 17100da83c2eSBarry Smith const PetscReal shift = 0.0; 17110da83c2eSBarry Smith PetscInt ipvt[5]; 17120da83c2eSBarry Smith PetscScalar work[25],*v_work; 17130da83c2eSBarry Smith 17140da83c2eSBarry Smith PetscFunctionBegin; 17150da83c2eSBarry Smith allowzeropivot = PetscNot(A->erroriffailure); 17160da83c2eSBarry Smith for (i=0; i<nblocks; i++) ncnt += bsizes[i]; 17170da83c2eSBarry Smith if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n); 17180da83c2eSBarry Smith for (i=0; i<nblocks; i++) { 17190da83c2eSBarry Smith bsizemax = PetscMax(bsizemax,bsizes[i]); 17200da83c2eSBarry Smith } 17210da83c2eSBarry Smith ierr = PetscMalloc1(bsizemax,&indx);CHKERRQ(ierr); 17220da83c2eSBarry Smith if (bsizemax > 7) { 17230da83c2eSBarry Smith ierr = PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);CHKERRQ(ierr); 17240da83c2eSBarry Smith } 17250da83c2eSBarry Smith ncnt = 0; 17260da83c2eSBarry Smith for (i=0; i<nblocks; i++) { 17270da83c2eSBarry Smith for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j; 17280da83c2eSBarry Smith ierr = MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);CHKERRQ(ierr); 17290da83c2eSBarry Smith switch (bsizes[i]) { 17300da83c2eSBarry Smith case 1: 17310da83c2eSBarry Smith *diag = 1.0/(*diag); 17320da83c2eSBarry Smith break; 17330da83c2eSBarry Smith case 2: 17340da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17350da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17360da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr); 17370da83c2eSBarry Smith break; 17380da83c2eSBarry Smith case 3: 17390da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17400da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17410da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr); 17420da83c2eSBarry Smith break; 17430da83c2eSBarry Smith case 4: 17440da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17450da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17460da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr); 17470da83c2eSBarry Smith break; 17480da83c2eSBarry Smith case 5: 17490da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17500da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17510da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr); 17520da83c2eSBarry Smith break; 17530da83c2eSBarry Smith case 6: 17540da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17550da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17560da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr); 17570da83c2eSBarry Smith break; 17580da83c2eSBarry Smith case 7: 17590da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17600da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17610da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr); 17620da83c2eSBarry Smith break; 17630da83c2eSBarry Smith default: 17640da83c2eSBarry Smith ierr = PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 17650da83c2eSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 17660da83c2eSBarry Smith ierr = PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);CHKERRQ(ierr); 17670da83c2eSBarry Smith } 17680da83c2eSBarry Smith ncnt += bsizes[i]; 17690da83c2eSBarry Smith diag += bsizes[i]*bsizes[i]; 17700da83c2eSBarry Smith } 17710da83c2eSBarry Smith if (bsizemax > 7) { 17720da83c2eSBarry Smith ierr = PetscFree2(v_work,v_pivots);CHKERRQ(ierr); 17730da83c2eSBarry Smith } 17740da83c2eSBarry Smith ierr = PetscFree(indx);CHKERRQ(ierr); 17750da83c2eSBarry Smith PetscFunctionReturn(0); 17760da83c2eSBarry Smith } 17770da83c2eSBarry Smith 1778422a814eSBarry Smith /* 1779422a814eSBarry Smith Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways 1780422a814eSBarry Smith */ 17817087cfbeSBarry Smith PetscErrorCode MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift) 178271f1c65dSBarry Smith { 178371f1c65dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 178471f1c65dSBarry Smith PetscErrorCode ierr; 1785d0f46423SBarry Smith PetscInt i,*diag,m = A->rmap->n; 178654f21887SBarry Smith MatScalar *v = a->a; 178754f21887SBarry Smith PetscScalar *idiag,*mdiag; 178871f1c65dSBarry Smith 178971f1c65dSBarry Smith PetscFunctionBegin; 179071f1c65dSBarry Smith if (a->idiagvalid) PetscFunctionReturn(0); 179171f1c65dSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 179271f1c65dSBarry Smith diag = a->diag; 179371f1c65dSBarry Smith if (!a->idiag) { 1794dcca6d9dSJed Brown ierr = PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);CHKERRQ(ierr); 17953bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr); 179671f1c65dSBarry Smith v = a->a; 179771f1c65dSBarry Smith } 179871f1c65dSBarry Smith mdiag = a->mdiag; 179971f1c65dSBarry Smith idiag = a->idiag; 180071f1c65dSBarry Smith 1801422a814eSBarry Smith if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) { 180271f1c65dSBarry Smith for (i=0; i<m; i++) { 180371f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 1804899639b0SHong Zhang if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */ 1805899639b0SHong Zhang if (PetscRealPart(fshift)) { 1806899639b0SHong Zhang ierr = PetscInfo1(A,"Zero diagonal on row %D\n",i);CHKERRQ(ierr); 18077b6c816cSBarry Smith A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 18087b6c816cSBarry Smith A->factorerror_zeropivot_value = 0.0; 18097b6c816cSBarry Smith A->factorerror_zeropivot_row = i; 1810a6fa060aSHong Zhang } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i); 1811899639b0SHong Zhang } 181271f1c65dSBarry Smith idiag[i] = 1.0/v[diag[i]]; 181371f1c65dSBarry Smith } 181471f1c65dSBarry Smith ierr = PetscLogFlops(m);CHKERRQ(ierr); 181571f1c65dSBarry Smith } else { 181671f1c65dSBarry Smith for (i=0; i<m; i++) { 181771f1c65dSBarry Smith mdiag[i] = v[diag[i]]; 181871f1c65dSBarry Smith idiag[i] = omega/(fshift + v[diag[i]]); 181971f1c65dSBarry Smith } 1820dc0b31edSSatish Balay ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr); 182171f1c65dSBarry Smith } 182271f1c65dSBarry Smith a->idiagvalid = PETSC_TRUE; 182371f1c65dSBarry Smith PetscFunctionReturn(0); 182471f1c65dSBarry Smith } 182571f1c65dSBarry Smith 1826c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h> 182741f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 182817ab2063SBarry Smith { 1829416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1830e6d1f457SBarry Smith PetscScalar *x,d,sum,*t,scale; 18313d3eaba7SBarry Smith const MatScalar *v,*idiag=0,*mdiag; 183254f21887SBarry Smith const PetscScalar *b, *bs,*xb, *ts; 1833dfbe8321SBarry Smith PetscErrorCode ierr; 18343d3eaba7SBarry Smith PetscInt n,m = A->rmap->n,i; 183597f1f81fSBarry Smith const PetscInt *idx,*diag; 183617ab2063SBarry Smith 18373a40ed3dSBarry Smith PetscFunctionBegin; 1838b965ef7fSBarry Smith its = its*lits; 183991723122SBarry Smith 184071f1c65dSBarry Smith if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */ 184171f1c65dSBarry Smith if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);} 184271f1c65dSBarry Smith a->fshift = fshift; 184371f1c65dSBarry Smith a->omega = omega; 1844ed480e8bSBarry Smith 184571f1c65dSBarry Smith diag = a->diag; 184671f1c65dSBarry Smith t = a->ssor_work; 1847ed480e8bSBarry Smith idiag = a->idiag; 184871f1c65dSBarry Smith mdiag = a->mdiag; 1849ed480e8bSBarry Smith 18501ebc52fbSHong Zhang ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 18513649974fSBarry Smith ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr); 1852ed480e8bSBarry Smith /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */ 185317ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 185417ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 1855ed480e8bSBarry Smith bs = b; 185617ab2063SBarry Smith for (i=0; i<m; i++) { 185771f1c65dSBarry Smith d = fshift + mdiag[i]; 1858416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1859ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1860ed480e8bSBarry Smith v = a->a + diag[i] + 1; 186117ab2063SBarry Smith sum = b[i]*d/omega; 1862003131ecSBarry Smith PetscSparseDensePlusDot(sum,bs,v,idx,n); 186317ab2063SBarry Smith x[i] = sum; 186417ab2063SBarry Smith } 18651ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 18663649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1867efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 18683a40ed3dSBarry Smith PetscFunctionReturn(0); 186917ab2063SBarry Smith } 1870c783ea89SBarry Smith 18712205254eSKarl Rupp if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 18722205254eSKarl Rupp else if (flag & SOR_EISENSTAT) { 18734c500f23SPierre Jolivet /* Let A = L + U + D; where L is lower triangular, 1874887ee2caSBarry Smith U is upper triangular, E = D/omega; This routine applies 187517ab2063SBarry Smith 187617ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 187717ab2063SBarry Smith 1878887ee2caSBarry Smith to a vector efficiently using Eisenstat's trick. 187917ab2063SBarry Smith */ 188017ab2063SBarry Smith scale = (2.0/omega) - 1.0; 188117ab2063SBarry Smith 188217ab2063SBarry Smith /* x = (E + U)^{-1} b */ 188317ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1884416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1885ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1886ed480e8bSBarry Smith v = a->a + diag[i] + 1; 188717ab2063SBarry Smith sum = b[i]; 1888e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1889ed480e8bSBarry Smith x[i] = sum*idiag[i]; 189017ab2063SBarry Smith } 189117ab2063SBarry Smith 189217ab2063SBarry Smith /* t = b - (2*E - D)x */ 1893416022c9SBarry Smith v = a->a; 18942205254eSKarl Rupp for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i]; 189517ab2063SBarry Smith 189617ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1897ed480e8bSBarry Smith ts = t; 1898416022c9SBarry Smith diag = a->diag; 189917ab2063SBarry Smith for (i=0; i<m; i++) { 1900416022c9SBarry Smith n = diag[i] - a->i[i]; 1901ed480e8bSBarry Smith idx = a->j + a->i[i]; 1902ed480e8bSBarry Smith v = a->a + a->i[i]; 190317ab2063SBarry Smith sum = t[i]; 1904003131ecSBarry Smith PetscSparseDenseMinusDot(sum,ts,v,idx,n); 1905ed480e8bSBarry Smith t[i] = sum*idiag[i]; 1906733d66baSBarry Smith /* x = x + t */ 1907733d66baSBarry Smith x[i] += t[i]; 190817ab2063SBarry Smith } 190917ab2063SBarry Smith 1910dc0b31edSSatish Balay ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr); 19111ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 19123649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 19133a40ed3dSBarry Smith PetscFunctionReturn(0); 191417ab2063SBarry Smith } 191517ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 191617ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) { 191717ab2063SBarry Smith for (i=0; i<m; i++) { 1918416022c9SBarry Smith n = diag[i] - a->i[i]; 1919ed480e8bSBarry Smith idx = a->j + a->i[i]; 1920ed480e8bSBarry Smith v = a->a + a->i[i]; 192117ab2063SBarry Smith sum = b[i]; 1922e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 19235c99c7daSBarry Smith t[i] = sum; 1924ed480e8bSBarry Smith x[i] = sum*idiag[i]; 192517ab2063SBarry Smith } 19265c99c7daSBarry Smith xb = t; 1927efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 19283a40ed3dSBarry Smith } else xb = b; 192917ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) { 193017ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1931416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1932ed480e8bSBarry Smith idx = a->j + diag[i] + 1; 1933ed480e8bSBarry Smith v = a->a + diag[i] + 1; 193417ab2063SBarry Smith sum = xb[i]; 1935e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 19365c99c7daSBarry Smith if (xb == b) { 1937ed480e8bSBarry Smith x[i] = sum*idiag[i]; 19385c99c7daSBarry Smith } else { 1939b19a5dc2SMark Adams x[i] = (1-omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 194017ab2063SBarry Smith } 19415c99c7daSBarry Smith } 1942b19a5dc2SMark Adams ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */ 194317ab2063SBarry Smith } 194417ab2063SBarry Smith its--; 194517ab2063SBarry Smith } 194617ab2063SBarry Smith while (its--) { 194717ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) { 194817ab2063SBarry Smith for (i=0; i<m; i++) { 1949b19a5dc2SMark Adams /* lower */ 1950b19a5dc2SMark Adams n = diag[i] - a->i[i]; 1951ed480e8bSBarry Smith idx = a->j + a->i[i]; 1952ed480e8bSBarry Smith v = a->a + a->i[i]; 195317ab2063SBarry Smith sum = b[i]; 1954e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1955b19a5dc2SMark Adams t[i] = sum; /* save application of the lower-triangular part */ 1956b19a5dc2SMark Adams /* upper */ 1957b19a5dc2SMark Adams n = a->i[i+1] - diag[i] - 1; 1958b19a5dc2SMark Adams idx = a->j + diag[i] + 1; 1959b19a5dc2SMark Adams v = a->a + diag[i] + 1; 1960b19a5dc2SMark Adams PetscSparseDenseMinusDot(sum,x,v,idx,n); 1961b19a5dc2SMark Adams x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 196217ab2063SBarry Smith } 1963b19a5dc2SMark Adams xb = t; 19649f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1965b19a5dc2SMark Adams } else xb = b; 196617ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) { 196717ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1968b19a5dc2SMark Adams sum = xb[i]; 1969b19a5dc2SMark Adams if (xb == b) { 1970b19a5dc2SMark Adams /* whole matrix (no checkpointing available) */ 1971416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1972ed480e8bSBarry Smith idx = a->j + a->i[i]; 1973ed480e8bSBarry Smith v = a->a + a->i[i]; 1974e6d1f457SBarry Smith PetscSparseDenseMinusDot(sum,x,v,idx,n); 1975ed480e8bSBarry Smith x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i]; 1976b19a5dc2SMark Adams } else { /* lower-triangular part has been saved, so only apply upper-triangular */ 1977b19a5dc2SMark Adams n = a->i[i+1] - diag[i] - 1; 1978b19a5dc2SMark Adams idx = a->j + diag[i] + 1; 1979b19a5dc2SMark Adams v = a->a + diag[i] + 1; 1980b19a5dc2SMark Adams PetscSparseDenseMinusDot(sum,x,v,idx,n); 1981b19a5dc2SMark Adams x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */ 198217ab2063SBarry Smith } 1983b19a5dc2SMark Adams } 1984b19a5dc2SMark Adams if (xb == b) { 19859f863219SBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 1986b19a5dc2SMark Adams } else { 1987b19a5dc2SMark Adams ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); /* assumes 1/2 in upper */ 1988b19a5dc2SMark Adams } 198917ab2063SBarry Smith } 199017ab2063SBarry Smith } 19911ebc52fbSHong Zhang ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 19923649974fSBarry Smith ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); 1993365a8a9eSBarry Smith PetscFunctionReturn(0); 199417ab2063SBarry Smith } 199517ab2063SBarry Smith 19962af78befSBarry Smith 1997dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 199817ab2063SBarry Smith { 1999416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 20004e220ebcSLois Curfman McInnes 20013a40ed3dSBarry Smith PetscFunctionBegin; 20024e220ebcSLois Curfman McInnes info->block_size = 1.0; 20033966268fSBarry Smith info->nz_allocated = a->maxnz; 20043966268fSBarry Smith info->nz_used = a->nz; 20053966268fSBarry Smith info->nz_unneeded = (a->maxnz - a->nz); 20063966268fSBarry Smith info->assemblies = A->num_ass; 20073966268fSBarry Smith info->mallocs = A->info.mallocs; 20087adad957SLisandro Dalcin info->memory = ((PetscObject)A)->mem; 2009d5f3da31SBarry Smith if (A->factortype) { 20104e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 20114e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 20124e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 20134e220ebcSLois Curfman McInnes } else { 20144e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 20154e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 20164e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 20174e220ebcSLois Curfman McInnes } 20183a40ed3dSBarry Smith PetscFunctionReturn(0); 201917ab2063SBarry Smith } 202017ab2063SBarry Smith 20212b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 202217ab2063SBarry Smith { 2023416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2024c7da8527SEric Chamberland PetscInt i,m = A->rmap->n - 1; 20256849ba73SBarry Smith PetscErrorCode ierr; 202697b48c8fSBarry Smith const PetscScalar *xx; 202797b48c8fSBarry Smith PetscScalar *bb; 2028c7da8527SEric Chamberland PetscInt d = 0; 202917ab2063SBarry Smith 20303a40ed3dSBarry Smith PetscFunctionBegin; 203197b48c8fSBarry Smith if (x && b) { 203297b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 203397b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 203497b48c8fSBarry Smith for (i=0; i<N; i++) { 203597b48c8fSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2036447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) continue; 203797b48c8fSBarry Smith bb[rows[i]] = diag*xx[rows[i]]; 203897b48c8fSBarry Smith } 203997b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 204097b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 204197b48c8fSBarry Smith } 204297b48c8fSBarry Smith 2043a9817697SBarry Smith if (a->keepnonzeropattern) { 2044f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 2045e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2046580bdb30SBarry Smith ierr = PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);CHKERRQ(ierr); 2047f1e2ffcdSBarry Smith } 2048f4df32b1SMatthew Knepley if (diag != 0.0) { 2049c7da8527SEric Chamberland for (i=0; i<N; i++) { 2050c7da8527SEric Chamberland d = rows[i]; 2051447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) continue; 2052c7da8527SEric 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); 2053c7da8527SEric Chamberland } 2054f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 2055447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) continue; 2056f4df32b1SMatthew Knepley a->a[a->diag[rows[i]]] = diag; 2057f1e2ffcdSBarry Smith } 2058f1e2ffcdSBarry Smith } 2059f1e2ffcdSBarry Smith } else { 2060f4df32b1SMatthew Knepley if (diag != 0.0) { 206117ab2063SBarry Smith for (i=0; i<N; i++) { 2062e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 20637ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 2064447d62f5SStefano Zampini if (rows[i] >= A->cmap->n) { 2065447d62f5SStefano Zampini a->ilen[rows[i]] = 0; 2066447d62f5SStefano Zampini } else { 2067416022c9SBarry Smith a->ilen[rows[i]] = 1; 2068f4df32b1SMatthew Knepley a->a[a->i[rows[i]]] = diag; 2069bfeeae90SHong Zhang a->j[a->i[rows[i]]] = rows[i]; 2070447d62f5SStefano Zampini } 2071447d62f5SStefano Zampini } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */ 2072f4df32b1SMatthew Knepley ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 207317ab2063SBarry Smith } 207417ab2063SBarry Smith } 20753a40ed3dSBarry Smith } else { 207617ab2063SBarry Smith for (i=0; i<N; i++) { 2077e32f2f54SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2078416022c9SBarry Smith a->ilen[rows[i]] = 0; 207917ab2063SBarry Smith } 208017ab2063SBarry Smith } 2081e56f5c9eSBarry Smith A->nonzerostate++; 2082f1e2ffcdSBarry Smith } 2083e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2084c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 2085e2cf4d64SStefano Zampini #endif 20864099cc6bSBarry Smith ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20873a40ed3dSBarry Smith PetscFunctionReturn(0); 208817ab2063SBarry Smith } 208917ab2063SBarry Smith 20906e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 20916e169961SBarry Smith { 20926e169961SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 20936e169961SBarry Smith PetscInt i,j,m = A->rmap->n - 1,d = 0; 20946e169961SBarry Smith PetscErrorCode ierr; 20952b40b63fSBarry Smith PetscBool missing,*zeroed,vecs = PETSC_FALSE; 20966e169961SBarry Smith const PetscScalar *xx; 20976e169961SBarry Smith PetscScalar *bb; 20986e169961SBarry Smith 20996e169961SBarry Smith PetscFunctionBegin; 21006e169961SBarry Smith if (x && b) { 21016e169961SBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 21026e169961SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 21032b40b63fSBarry Smith vecs = PETSC_TRUE; 21046e169961SBarry Smith } 21051795a4d1SJed Brown ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr); 21066e169961SBarry Smith for (i=0; i<N; i++) { 21076e169961SBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]); 2108580bdb30SBarry Smith ierr = PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);CHKERRQ(ierr); 21092205254eSKarl Rupp 21106e169961SBarry Smith zeroed[rows[i]] = PETSC_TRUE; 21116e169961SBarry Smith } 21126e169961SBarry Smith for (i=0; i<A->rmap->n; i++) { 21136e169961SBarry Smith if (!zeroed[i]) { 21146e169961SBarry Smith for (j=a->i[i]; j<a->i[i+1]; j++) { 21154cf107fdSStefano Zampini if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) { 21162b40b63fSBarry Smith if (vecs) bb[i] -= a->a[j]*xx[a->j[j]]; 21176e169961SBarry Smith a->a[j] = 0.0; 21186e169961SBarry Smith } 21196e169961SBarry Smith } 21204cf107fdSStefano Zampini } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i]; 21216e169961SBarry Smith } 21226e169961SBarry Smith if (x && b) { 21236e169961SBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 21246e169961SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 21256e169961SBarry Smith } 21266e169961SBarry Smith ierr = PetscFree(zeroed);CHKERRQ(ierr); 21276e169961SBarry Smith if (diag != 0.0) { 21286e169961SBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr); 21291d5a398dSstefano_zampini if (missing) { 21301d5a398dSstefano_zampini for (i=0; i<N; i++) { 21314cf107fdSStefano Zampini if (rows[i] >= A->cmap->N) continue; 21324cf107fdSStefano 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]); 21331d5a398dSstefano_zampini ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr); 21341d5a398dSstefano_zampini } 21351d5a398dSstefano_zampini } else { 21366e169961SBarry Smith for (i=0; i<N; i++) { 21376e169961SBarry Smith a->a[a->diag[rows[i]]] = diag; 21386e169961SBarry Smith } 21396e169961SBarry Smith } 21401d5a398dSstefano_zampini } 2141e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2142c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 2143e2cf4d64SStefano Zampini #endif 21444099cc6bSBarry Smith ierr = (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21456e169961SBarry Smith PetscFunctionReturn(0); 21466e169961SBarry Smith } 21476e169961SBarry Smith 2148a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 214917ab2063SBarry Smith { 2150416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 215197f1f81fSBarry Smith PetscInt *itmp; 215217ab2063SBarry Smith 21533a40ed3dSBarry Smith PetscFunctionBegin; 2154e32f2f54SBarry Smith if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row); 215517ab2063SBarry Smith 2156416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 2157bfeeae90SHong Zhang if (v) *v = a->a + a->i[row]; 215817ab2063SBarry Smith if (idx) { 2159bfeeae90SHong Zhang itmp = a->j + a->i[row]; 216026fbe8dcSKarl Rupp if (*nz) *idx = itmp; 216117ab2063SBarry Smith else *idx = 0; 216217ab2063SBarry Smith } 21633a40ed3dSBarry Smith PetscFunctionReturn(0); 216417ab2063SBarry Smith } 216517ab2063SBarry Smith 2166bfeeae90SHong Zhang /* remove this function? */ 2167a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 216817ab2063SBarry Smith { 21693a40ed3dSBarry Smith PetscFunctionBegin; 21703a40ed3dSBarry Smith PetscFunctionReturn(0); 217117ab2063SBarry Smith } 217217ab2063SBarry Smith 2173dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 217417ab2063SBarry Smith { 2175416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 217654f21887SBarry Smith MatScalar *v = a->a; 217736db0b34SBarry Smith PetscReal sum = 0.0; 21786849ba73SBarry Smith PetscErrorCode ierr; 217997f1f81fSBarry Smith PetscInt i,j; 218017ab2063SBarry Smith 21813a40ed3dSBarry Smith PetscFunctionBegin; 218217ab2063SBarry Smith if (type == NORM_FROBENIUS) { 2183570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FP16) 2184570b7f6dSBarry Smith PetscBLASInt one = 1,nz = a->nz; 2185570b7f6dSBarry Smith *nrm = BLASnrm2_(&nz,v,&one); 2186570b7f6dSBarry Smith #else 2187416022c9SBarry Smith for (i=0; i<a->nz; i++) { 218836db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 218917ab2063SBarry Smith } 21908f1a2a5eSBarry Smith *nrm = PetscSqrtReal(sum); 2191570b7f6dSBarry Smith #endif 2192*ca0c957dSBarry Smith ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr); 21933a40ed3dSBarry Smith } else if (type == NORM_1) { 219436db0b34SBarry Smith PetscReal *tmp; 219597f1f81fSBarry Smith PetscInt *jj = a->j; 21961795a4d1SJed Brown ierr = PetscCalloc1(A->cmap->n+1,&tmp);CHKERRQ(ierr); 2197064f8208SBarry Smith *nrm = 0.0; 2198416022c9SBarry Smith for (j=0; j<a->nz; j++) { 2199bfeeae90SHong Zhang tmp[*jj++] += PetscAbsScalar(*v); v++; 220017ab2063SBarry Smith } 2201d0f46423SBarry Smith for (j=0; j<A->cmap->n; j++) { 2202064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 220317ab2063SBarry Smith } 2204606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 220551f70360SJed Brown ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr); 22063a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 2207064f8208SBarry Smith *nrm = 0.0; 2208d0f46423SBarry Smith for (j=0; j<A->rmap->n; j++) { 2209bfeeae90SHong Zhang v = a->a + a->i[j]; 221017ab2063SBarry Smith sum = 0.0; 2211416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 2212cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 221317ab2063SBarry Smith } 2214064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 221517ab2063SBarry Smith } 221651f70360SJed Brown ierr = PetscLogFlops(PetscMax(a->nz-1,0));CHKERRQ(ierr); 2217f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm"); 22183a40ed3dSBarry Smith PetscFunctionReturn(0); 221917ab2063SBarry Smith } 222017ab2063SBarry Smith 22214e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */ 22224e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B) 22234e938277SHong Zhang { 22244e938277SHong Zhang PetscErrorCode ierr; 22254e938277SHong Zhang PetscInt i,j,anzj; 22264e938277SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b; 22274e938277SHong Zhang PetscInt an=A->cmap->N,am=A->rmap->N; 22284e938277SHong Zhang PetscInt *ati,*atj,*atfill,*ai=a->i,*aj=a->j; 22294e938277SHong Zhang 22304e938277SHong Zhang PetscFunctionBegin; 22314e938277SHong Zhang /* Allocate space for symbolic transpose info and work array */ 2232854ce69bSBarry Smith ierr = PetscCalloc1(an+1,&ati);CHKERRQ(ierr); 2233785e854fSJed Brown ierr = PetscMalloc1(ai[am],&atj);CHKERRQ(ierr); 2234785e854fSJed Brown ierr = PetscMalloc1(an,&atfill);CHKERRQ(ierr); 22354e938277SHong Zhang 22364e938277SHong Zhang /* Walk through aj and count ## of non-zeros in each row of A^T. */ 22374e938277SHong Zhang /* Note: offset by 1 for fast conversion into csr format. */ 223826fbe8dcSKarl Rupp for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1; 22394e938277SHong Zhang /* Form ati for csr format of A^T. */ 224026fbe8dcSKarl Rupp for (i=0;i<an;i++) ati[i+1] += ati[i]; 22414e938277SHong Zhang 22424e938277SHong Zhang /* Copy ati into atfill so we have locations of the next free space in atj */ 2243580bdb30SBarry Smith ierr = PetscArraycpy(atfill,ati,an);CHKERRQ(ierr); 22444e938277SHong Zhang 22454e938277SHong Zhang /* Walk through A row-wise and mark nonzero entries of A^T. */ 22464e938277SHong Zhang for (i=0;i<am;i++) { 22474e938277SHong Zhang anzj = ai[i+1] - ai[i]; 22484e938277SHong Zhang for (j=0;j<anzj;j++) { 22494e938277SHong Zhang atj[atfill[*aj]] = i; 22504e938277SHong Zhang atfill[*aj++] += 1; 22514e938277SHong Zhang } 22524e938277SHong Zhang } 22534e938277SHong Zhang 22544e938277SHong Zhang /* Clean up temporary space and complete requests. */ 22554e938277SHong Zhang ierr = PetscFree(atfill);CHKERRQ(ierr); 2256ce94432eSBarry Smith ierr = MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);CHKERRQ(ierr); 225733d57670SJed Brown ierr = MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr); 2258b5bb3eecSMark Adams ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2259a2f3521dSMark F. Adams 22604e938277SHong Zhang b = (Mat_SeqAIJ*)((*B)->data); 22614e938277SHong Zhang b->free_a = PETSC_FALSE; 22624e938277SHong Zhang b->free_ij = PETSC_TRUE; 22634e938277SHong Zhang b->nonew = 0; 22644e938277SHong Zhang PetscFunctionReturn(0); 22654e938277SHong Zhang } 22664e938277SHong Zhang 22677087cfbeSBarry Smith PetscErrorCode MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 2268cd0d46ebSvictorle { 22693d3eaba7SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data; 227054f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 227154f21887SBarry Smith MatScalar *va,*vb; 22726849ba73SBarry Smith PetscErrorCode ierr; 227397f1f81fSBarry Smith PetscInt ma,na,mb,nb, i; 2274cd0d46ebSvictorle 2275cd0d46ebSvictorle PetscFunctionBegin; 2276cd0d46ebSvictorle ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 2277cd0d46ebSvictorle ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 22785485867bSBarry Smith if (ma!=nb || na!=mb) { 22795485867bSBarry Smith *f = PETSC_FALSE; 22805485867bSBarry Smith PetscFunctionReturn(0); 22815485867bSBarry Smith } 2282cd0d46ebSvictorle aii = aij->i; bii = bij->i; 2283cd0d46ebSvictorle adx = aij->j; bdx = bij->j; 2284cd0d46ebSvictorle va = aij->a; vb = bij->a; 2285785e854fSJed Brown ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr); 2286785e854fSJed Brown ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr); 2287cd0d46ebSvictorle for (i=0; i<ma; i++) aptr[i] = aii[i]; 2288cd0d46ebSvictorle for (i=0; i<mb; i++) bptr[i] = bii[i]; 2289cd0d46ebSvictorle 2290cd0d46ebSvictorle *f = PETSC_TRUE; 2291cd0d46ebSvictorle for (i=0; i<ma; i++) { 2292cd0d46ebSvictorle while (aptr[i]<aii[i+1]) { 229397f1f81fSBarry Smith PetscInt idc,idr; 22945485867bSBarry Smith PetscScalar vc,vr; 2295cd0d46ebSvictorle /* column/row index/value */ 22965485867bSBarry Smith idc = adx[aptr[i]]; 22975485867bSBarry Smith idr = bdx[bptr[idc]]; 22985485867bSBarry Smith vc = va[aptr[i]]; 22995485867bSBarry Smith vr = vb[bptr[idc]]; 23005485867bSBarry Smith if (i!=idr || PetscAbsScalar(vc-vr) > tol) { 23015485867bSBarry Smith *f = PETSC_FALSE; 23025485867bSBarry Smith goto done; 2303cd0d46ebSvictorle } else { 23045485867bSBarry Smith aptr[i]++; 23055485867bSBarry Smith if (B || i!=idc) bptr[idc]++; 2306cd0d46ebSvictorle } 2307cd0d46ebSvictorle } 2308cd0d46ebSvictorle } 2309cd0d46ebSvictorle done: 2310cd0d46ebSvictorle ierr = PetscFree(aptr);CHKERRQ(ierr); 23113aeef889SHong Zhang ierr = PetscFree(bptr);CHKERRQ(ierr); 2312cd0d46ebSvictorle PetscFunctionReturn(0); 2313cd0d46ebSvictorle } 2314cd0d46ebSvictorle 23157087cfbeSBarry Smith PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f) 23161cbb95d3SBarry Smith { 23173d3eaba7SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data; 231854f21887SBarry Smith PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr; 231954f21887SBarry Smith MatScalar *va,*vb; 23201cbb95d3SBarry Smith PetscErrorCode ierr; 23211cbb95d3SBarry Smith PetscInt ma,na,mb,nb, i; 23221cbb95d3SBarry Smith 23231cbb95d3SBarry Smith PetscFunctionBegin; 23241cbb95d3SBarry Smith ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr); 23251cbb95d3SBarry Smith ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr); 23261cbb95d3SBarry Smith if (ma!=nb || na!=mb) { 23271cbb95d3SBarry Smith *f = PETSC_FALSE; 23281cbb95d3SBarry Smith PetscFunctionReturn(0); 23291cbb95d3SBarry Smith } 23301cbb95d3SBarry Smith aii = aij->i; bii = bij->i; 23311cbb95d3SBarry Smith adx = aij->j; bdx = bij->j; 23321cbb95d3SBarry Smith va = aij->a; vb = bij->a; 2333785e854fSJed Brown ierr = PetscMalloc1(ma,&aptr);CHKERRQ(ierr); 2334785e854fSJed Brown ierr = PetscMalloc1(mb,&bptr);CHKERRQ(ierr); 23351cbb95d3SBarry Smith for (i=0; i<ma; i++) aptr[i] = aii[i]; 23361cbb95d3SBarry Smith for (i=0; i<mb; i++) bptr[i] = bii[i]; 23371cbb95d3SBarry Smith 23381cbb95d3SBarry Smith *f = PETSC_TRUE; 23391cbb95d3SBarry Smith for (i=0; i<ma; i++) { 23401cbb95d3SBarry Smith while (aptr[i]<aii[i+1]) { 23411cbb95d3SBarry Smith PetscInt idc,idr; 23421cbb95d3SBarry Smith PetscScalar vc,vr; 23431cbb95d3SBarry Smith /* column/row index/value */ 23441cbb95d3SBarry Smith idc = adx[aptr[i]]; 23451cbb95d3SBarry Smith idr = bdx[bptr[idc]]; 23461cbb95d3SBarry Smith vc = va[aptr[i]]; 23471cbb95d3SBarry Smith vr = vb[bptr[idc]]; 23481cbb95d3SBarry Smith if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) { 23491cbb95d3SBarry Smith *f = PETSC_FALSE; 23501cbb95d3SBarry Smith goto done; 23511cbb95d3SBarry Smith } else { 23521cbb95d3SBarry Smith aptr[i]++; 23531cbb95d3SBarry Smith if (B || i!=idc) bptr[idc]++; 23541cbb95d3SBarry Smith } 23551cbb95d3SBarry Smith } 23561cbb95d3SBarry Smith } 23571cbb95d3SBarry Smith done: 23581cbb95d3SBarry Smith ierr = PetscFree(aptr);CHKERRQ(ierr); 23591cbb95d3SBarry Smith ierr = PetscFree(bptr);CHKERRQ(ierr); 23601cbb95d3SBarry Smith PetscFunctionReturn(0); 23611cbb95d3SBarry Smith } 23621cbb95d3SBarry Smith 2363ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 23649e29f15eSvictorle { 2365dfbe8321SBarry Smith PetscErrorCode ierr; 23666e111a19SKarl Rupp 23679e29f15eSvictorle PetscFunctionBegin; 23685485867bSBarry Smith ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 23699e29f15eSvictorle PetscFunctionReturn(0); 23709e29f15eSvictorle } 23719e29f15eSvictorle 2372ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool *f) 23731cbb95d3SBarry Smith { 23741cbb95d3SBarry Smith PetscErrorCode ierr; 23756e111a19SKarl Rupp 23761cbb95d3SBarry Smith PetscFunctionBegin; 23771cbb95d3SBarry Smith ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr); 23781cbb95d3SBarry Smith PetscFunctionReturn(0); 23791cbb95d3SBarry Smith } 23801cbb95d3SBarry Smith 2381dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 238217ab2063SBarry Smith { 2383416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2384fff8e43fSBarry Smith const PetscScalar *l,*r; 2385fff8e43fSBarry Smith PetscScalar x; 238654f21887SBarry Smith MatScalar *v; 2387dfbe8321SBarry Smith PetscErrorCode ierr; 2388fff8e43fSBarry Smith PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz; 2389fff8e43fSBarry Smith const PetscInt *jj; 239017ab2063SBarry Smith 23913a40ed3dSBarry Smith PetscFunctionBegin; 239217ab2063SBarry Smith if (ll) { 23933ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 23943ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 2395e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 2396e32f2f54SBarry Smith if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 2397fff8e43fSBarry Smith ierr = VecGetArrayRead(ll,&l);CHKERRQ(ierr); 2398416022c9SBarry Smith v = a->a; 239917ab2063SBarry Smith for (i=0; i<m; i++) { 240017ab2063SBarry Smith x = l[i]; 2401416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 24022205254eSKarl Rupp for (j=0; j<M; j++) (*v++) *= x; 240317ab2063SBarry Smith } 2404fff8e43fSBarry Smith ierr = VecRestoreArrayRead(ll,&l);CHKERRQ(ierr); 2405efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 240617ab2063SBarry Smith } 240717ab2063SBarry Smith if (rr) { 2408e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 2409e32f2f54SBarry Smith if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 2410fff8e43fSBarry Smith ierr = VecGetArrayRead(rr,&r);CHKERRQ(ierr); 2411416022c9SBarry Smith v = a->a; jj = a->j; 24122205254eSKarl Rupp for (i=0; i<nz; i++) (*v++) *= r[*jj++]; 2413fff8e43fSBarry Smith ierr = VecRestoreArrayRead(rr,&r);CHKERRQ(ierr); 2414efee365bSSatish Balay ierr = PetscLogFlops(nz);CHKERRQ(ierr); 241517ab2063SBarry Smith } 2416acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(A);CHKERRQ(ierr); 2417e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2418c70f7ee4SJunchao Zhang if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU; 2419e2cf4d64SStefano Zampini #endif 24203a40ed3dSBarry Smith PetscFunctionReturn(0); 242117ab2063SBarry Smith } 242217ab2063SBarry Smith 24237dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B) 242417ab2063SBarry Smith { 2425db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 24266849ba73SBarry Smith PetscErrorCode ierr; 2427d0f46423SBarry Smith PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens; 242897f1f81fSBarry Smith PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 24295d0c19d7SBarry Smith const PetscInt *irow,*icol; 24305d0c19d7SBarry Smith PetscInt nrows,ncols; 243197f1f81fSBarry Smith PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 243254f21887SBarry Smith MatScalar *a_new,*mat_a; 2433416022c9SBarry Smith Mat C; 2434cdc6f3adSToby Isaac PetscBool stride; 243517ab2063SBarry Smith 24363a40ed3dSBarry Smith PetscFunctionBegin; 243799141d43SSatish Balay 243817ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 2439b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 2440b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 244117ab2063SBarry Smith 2442251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr); 2443ff718158SBarry Smith if (stride) { 2444ff718158SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 2445ff718158SBarry Smith } else { 2446ff718158SBarry Smith first = 0; 2447ff718158SBarry Smith step = 0; 2448ff718158SBarry Smith } 2449fee21e36SBarry Smith if (stride && step == 1) { 245002834360SBarry Smith /* special case of contiguous rows */ 2451dcca6d9dSJed Brown ierr = PetscMalloc2(nrows,&lens,nrows,&starts);CHKERRQ(ierr); 245202834360SBarry Smith /* loop over new rows determining lens and starting points */ 245302834360SBarry Smith for (i=0; i<nrows; i++) { 2454bfeeae90SHong Zhang kstart = ai[irow[i]]; 2455a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 2456a91a9bebSLisandro Dalcin starts[i] = kstart; 245702834360SBarry Smith for (k=kstart; k<kend; k++) { 2458bfeeae90SHong Zhang if (aj[k] >= first) { 245902834360SBarry Smith starts[i] = k; 246002834360SBarry Smith break; 246102834360SBarry Smith } 246202834360SBarry Smith } 2463a2744918SBarry Smith sum = 0; 246402834360SBarry Smith while (k < kend) { 2465bfeeae90SHong Zhang if (aj[k++] >= first+ncols) break; 2466a2744918SBarry Smith sum++; 246702834360SBarry Smith } 2468a2744918SBarry Smith lens[i] = sum; 246902834360SBarry Smith } 247002834360SBarry Smith /* create submatrix */ 2471cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 247297f1f81fSBarry Smith PetscInt n_cols,n_rows; 247308480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 2474e32f2f54SBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 2475d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 247608480c60SBarry Smith C = *B; 24773a40ed3dSBarry Smith } else { 24783bef6203SJed Brown PetscInt rbs,cbs; 2479ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2480f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 24813bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 24823bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 24833bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 24847adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2485ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 248608480c60SBarry Smith } 2487db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 2488db02288aSLois Curfman McInnes 248902834360SBarry Smith /* loop over rows inserting into submatrix */ 2490db02288aSLois Curfman McInnes a_new = c->a; 2491db02288aSLois Curfman McInnes j_new = c->j; 2492db02288aSLois Curfman McInnes i_new = c->i; 2493bfeeae90SHong Zhang 249402834360SBarry Smith for (i=0; i<nrows; i++) { 2495a2744918SBarry Smith ii = starts[i]; 2496a2744918SBarry Smith lensi = lens[i]; 2497a2744918SBarry Smith for (k=0; k<lensi; k++) { 2498a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 249902834360SBarry Smith } 2500580bdb30SBarry Smith ierr = PetscArraycpy(a_new,a->a + starts[i],lensi);CHKERRQ(ierr); 2501a2744918SBarry Smith a_new += lensi; 2502a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 2503a2744918SBarry Smith c->ilen[i] = lensi; 250402834360SBarry Smith } 25050e83c824SBarry Smith ierr = PetscFree2(lens,starts);CHKERRQ(ierr); 25063a40ed3dSBarry Smith } else { 250702834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 25081795a4d1SJed Brown ierr = PetscCalloc1(oldcols,&smap);CHKERRQ(ierr); 2509854ce69bSBarry Smith ierr = PetscMalloc1(1+nrows,&lens);CHKERRQ(ierr); 25104dcab191SBarry Smith for (i=0; i<ncols; i++) { 25114dcab191SBarry Smith #if defined(PETSC_USE_DEBUG) 251204160ab8SPatrick Sanan 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); 25134dcab191SBarry Smith #endif 25144dcab191SBarry Smith smap[icol[i]] = i+1; 25154dcab191SBarry Smith } 25164dcab191SBarry Smith 251702834360SBarry Smith /* determine lens of each row */ 251802834360SBarry Smith for (i=0; i<nrows; i++) { 2519bfeeae90SHong Zhang kstart = ai[irow[i]]; 252002834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 252102834360SBarry Smith lens[i] = 0; 252202834360SBarry Smith for (k=kstart; k<kend; k++) { 2523bfeeae90SHong Zhang if (smap[aj[k]]) { 252402834360SBarry Smith lens[i]++; 252502834360SBarry Smith } 252602834360SBarry Smith } 252702834360SBarry Smith } 252817ab2063SBarry Smith /* Create and fill new matrix */ 2529a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 2530ace3abfcSBarry Smith PetscBool equal; 25310f5bd95cSBarry Smith 253299141d43SSatish Balay c = (Mat_SeqAIJ*)((*B)->data); 2533e32f2f54SBarry Smith if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 2534580bdb30SBarry Smith ierr = PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);CHKERRQ(ierr); 2535f23aa3ddSBarry Smith if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 2536580bdb30SBarry Smith ierr = PetscArrayzero(c->ilen,(*B)->rmap->n);CHKERRQ(ierr); 253708480c60SBarry Smith C = *B; 25383a40ed3dSBarry Smith } else { 25393bef6203SJed Brown PetscInt rbs,cbs; 2540ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2541f69a0ea3SMatthew Knepley ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 25423bef6203SJed Brown ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr); 25433bef6203SJed Brown ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 25443bef6203SJed Brown ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr); 25457adad957SLisandro Dalcin ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2546ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr); 254708480c60SBarry Smith } 254899141d43SSatish Balay c = (Mat_SeqAIJ*)(C->data); 254917ab2063SBarry Smith for (i=0; i<nrows; i++) { 255099141d43SSatish Balay row = irow[i]; 2551bfeeae90SHong Zhang kstart = ai[row]; 255299141d43SSatish Balay kend = kstart + a->ilen[row]; 2553bfeeae90SHong Zhang mat_i = c->i[i]; 255499141d43SSatish Balay mat_j = c->j + mat_i; 255599141d43SSatish Balay mat_a = c->a + mat_i; 255699141d43SSatish Balay mat_ilen = c->ilen + i; 255717ab2063SBarry Smith for (k=kstart; k<kend; k++) { 2558bfeeae90SHong Zhang if ((tcol=smap[a->j[k]])) { 2559ed480e8bSBarry Smith *mat_j++ = tcol - 1; 256099141d43SSatish Balay *mat_a++ = a->a[k]; 256199141d43SSatish Balay (*mat_ilen)++; 256299141d43SSatish Balay 256317ab2063SBarry Smith } 256417ab2063SBarry Smith } 256517ab2063SBarry Smith } 256602834360SBarry Smith /* Free work space */ 256702834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 2568606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 2569606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 2570cdc6f3adSToby Isaac /* sort */ 2571cdc6f3adSToby Isaac for (i = 0; i < nrows; i++) { 2572cdc6f3adSToby Isaac PetscInt ilen; 2573cdc6f3adSToby Isaac 2574cdc6f3adSToby Isaac mat_i = c->i[i]; 2575cdc6f3adSToby Isaac mat_j = c->j + mat_i; 2576cdc6f3adSToby Isaac mat_a = c->a + mat_i; 2577cdc6f3adSToby Isaac ilen = c->ilen[i]; 2578390e1bf2SBarry Smith ierr = PetscSortIntWithScalarArray(ilen,mat_j,mat_a);CHKERRQ(ierr); 2579cdc6f3adSToby Isaac } 258002834360SBarry Smith } 2581305c6ccfSStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2582b470e4b4SRichard Tran Mills ierr = MatBindToCPU(C,A->boundtocpu);CHKERRQ(ierr); 2583305c6ccfSStefano Zampini #endif 25846d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 25856d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 258617ab2063SBarry Smith 258717ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 2588416022c9SBarry Smith *B = C; 25893a40ed3dSBarry Smith PetscFunctionReturn(0); 259017ab2063SBarry Smith } 259117ab2063SBarry Smith 2592fc08c53fSHong Zhang PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat) 259382d44351SHong Zhang { 259482d44351SHong Zhang PetscErrorCode ierr; 259582d44351SHong Zhang Mat B; 259682d44351SHong Zhang 259782d44351SHong Zhang PetscFunctionBegin; 2598c2d650bdSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 259982d44351SHong Zhang ierr = MatCreate(subComm,&B);CHKERRQ(ierr); 260082d44351SHong Zhang ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr); 260133d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,mat,mat);CHKERRQ(ierr); 260282d44351SHong Zhang ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 260382d44351SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr); 260482d44351SHong Zhang *subMat = B; 2605c2d650bdSHong Zhang } else { 2606c2d650bdSHong Zhang ierr = MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 2607c2d650bdSHong Zhang } 260882d44351SHong Zhang PetscFunctionReturn(0); 260982d44351SHong Zhang } 261082d44351SHong Zhang 26119a625307SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info) 2612a871dcd8SBarry Smith { 261363b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2614dfbe8321SBarry Smith PetscErrorCode ierr; 261563b91edcSBarry Smith Mat outA; 2616ace3abfcSBarry Smith PetscBool row_identity,col_identity; 261763b91edcSBarry Smith 26183a40ed3dSBarry Smith PetscFunctionBegin; 2619e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 26201df811f5SHong Zhang 2621b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 2622b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 2623a871dcd8SBarry Smith 262463b91edcSBarry Smith outA = inA; 2625d5f3da31SBarry Smith outA->factortype = MAT_FACTOR_LU; 2626f6224b95SHong Zhang ierr = PetscFree(inA->solvertype);CHKERRQ(ierr); 2627f6224b95SHong Zhang ierr = PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);CHKERRQ(ierr); 26282205254eSKarl Rupp 2629c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 26306bf464f9SBarry Smith ierr = ISDestroy(&a->row);CHKERRQ(ierr); 26312205254eSKarl Rupp 2632c3122656SLisandro Dalcin a->row = row; 26332205254eSKarl Rupp 2634c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 26356bf464f9SBarry Smith ierr = ISDestroy(&a->col);CHKERRQ(ierr); 26362205254eSKarl Rupp 2637c3122656SLisandro Dalcin a->col = col; 263863b91edcSBarry Smith 263936db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 26406bf464f9SBarry Smith ierr = ISDestroy(&a->icol);CHKERRQ(ierr); 26414c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 26423bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr); 2643f0ec6fceSSatish Balay 264494a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 2645854ce69bSBarry Smith ierr = PetscMalloc1(inA->rmap->n+1,&a->solve_work);CHKERRQ(ierr); 26463bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr); 264794a9d846SBarry Smith } 264863b91edcSBarry Smith 2649f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 2650137fb511SHong Zhang if (row_identity && col_identity) { 2651ad04f41aSHong Zhang ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr); 2652137fb511SHong Zhang } else { 2653719d5645SBarry Smith ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr); 2654137fb511SHong Zhang } 26553a40ed3dSBarry Smith PetscFunctionReturn(0); 2656a871dcd8SBarry Smith } 2657a871dcd8SBarry Smith 2658f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha) 2659f0b747eeSBarry Smith { 2660f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 2661f4df32b1SMatthew Knepley PetscScalar oalpha = alpha; 2662efee365bSSatish Balay PetscErrorCode ierr; 2663c5df96a5SBarry Smith PetscBLASInt one = 1,bnz; 26643a40ed3dSBarry Smith 26653a40ed3dSBarry Smith PetscFunctionBegin; 2666c5df96a5SBarry Smith ierr = PetscBLASIntCast(a->nz,&bnz);CHKERRQ(ierr); 26678b83055fSJed Brown PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one)); 2668efee365bSSatish Balay ierr = PetscLogFlops(a->nz);CHKERRQ(ierr); 2669acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(inA);CHKERRQ(ierr); 2670e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2671c70f7ee4SJunchao Zhang if (inA->offloadmask != PETSC_OFFLOAD_UNALLOCATED) inA->offloadmask = PETSC_OFFLOAD_CPU; 2672e2cf4d64SStefano Zampini #endif 26733a40ed3dSBarry Smith PetscFunctionReturn(0); 2674f0b747eeSBarry Smith } 2675f0b747eeSBarry Smith 2676f68bb481SHong Zhang PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj) 267716b64355SHong Zhang { 267816b64355SHong Zhang PetscErrorCode ierr; 267916b64355SHong Zhang PetscInt i; 268016b64355SHong Zhang 268116b64355SHong Zhang PetscFunctionBegin; 268216b64355SHong Zhang if (!submatj->id) { /* delete data that are linked only to submats[id=0] */ 268316b64355SHong Zhang ierr = PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);CHKERRQ(ierr); 268416b64355SHong Zhang 268516b64355SHong Zhang for (i=0; i<submatj->nrqr; ++i) { 268616b64355SHong Zhang ierr = PetscFree(submatj->sbuf2[i]);CHKERRQ(ierr); 268716b64355SHong Zhang } 268816b64355SHong Zhang ierr = PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);CHKERRQ(ierr); 268916b64355SHong Zhang 269016b64355SHong Zhang if (submatj->rbuf1) { 269116b64355SHong Zhang ierr = PetscFree(submatj->rbuf1[0]);CHKERRQ(ierr); 269216b64355SHong Zhang ierr = PetscFree(submatj->rbuf1);CHKERRQ(ierr); 269316b64355SHong Zhang } 269416b64355SHong Zhang 269516b64355SHong Zhang for (i=0; i<submatj->nrqs; ++i) { 269616b64355SHong Zhang ierr = PetscFree(submatj->rbuf3[i]);CHKERRQ(ierr); 269716b64355SHong Zhang } 269816b64355SHong Zhang ierr = PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);CHKERRQ(ierr); 269916b64355SHong Zhang ierr = PetscFree(submatj->pa);CHKERRQ(ierr); 270016b64355SHong Zhang } 270116b64355SHong Zhang 270216b64355SHong Zhang #if defined(PETSC_USE_CTABLE) 270316b64355SHong Zhang ierr = PetscTableDestroy((PetscTable*)&submatj->rmap);CHKERRQ(ierr); 270416b64355SHong Zhang if (submatj->cmap_loc) {ierr = PetscFree(submatj->cmap_loc);CHKERRQ(ierr);} 270516b64355SHong Zhang ierr = PetscFree(submatj->rmap_loc);CHKERRQ(ierr); 270616b64355SHong Zhang #else 270716b64355SHong Zhang ierr = PetscFree(submatj->rmap);CHKERRQ(ierr); 270816b64355SHong Zhang #endif 270916b64355SHong Zhang 271016b64355SHong Zhang if (!submatj->allcolumns) { 271116b64355SHong Zhang #if defined(PETSC_USE_CTABLE) 271216b64355SHong Zhang ierr = PetscTableDestroy((PetscTable*)&submatj->cmap);CHKERRQ(ierr); 271316b64355SHong Zhang #else 271416b64355SHong Zhang ierr = PetscFree(submatj->cmap);CHKERRQ(ierr); 271516b64355SHong Zhang #endif 271616b64355SHong Zhang } 271716b64355SHong Zhang ierr = PetscFree(submatj->row2proc);CHKERRQ(ierr); 271816b64355SHong Zhang 271916b64355SHong Zhang ierr = PetscFree(submatj);CHKERRQ(ierr); 272016b64355SHong Zhang PetscFunctionReturn(0); 272116b64355SHong Zhang } 272216b64355SHong Zhang 27230fb991dcSHong Zhang PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C) 272416b64355SHong Zhang { 272516b64355SHong Zhang PetscErrorCode ierr; 272616b64355SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data; 27275c39f6d9SHong Zhang Mat_SubSppt *submatj = c->submatis1; 272816b64355SHong Zhang 272916b64355SHong Zhang PetscFunctionBegin; 273034136279SStefano Zampini ierr = (*submatj->destroy)(C);CHKERRQ(ierr); 2731f68bb481SHong Zhang ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr); 273216b64355SHong Zhang PetscFunctionReturn(0); 273316b64355SHong Zhang } 273416b64355SHong Zhang 27352d033e1fSHong Zhang PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[]) 27362d033e1fSHong Zhang { 27372d033e1fSHong Zhang PetscErrorCode ierr; 27382d033e1fSHong Zhang PetscInt i; 27390fb991dcSHong Zhang Mat C; 27400fb991dcSHong Zhang Mat_SeqAIJ *c; 27410fb991dcSHong Zhang Mat_SubSppt *submatj; 27422d033e1fSHong Zhang 27432d033e1fSHong Zhang PetscFunctionBegin; 27442d033e1fSHong Zhang for (i=0; i<n; i++) { 27450fb991dcSHong Zhang C = (*mat)[i]; 27460fb991dcSHong Zhang c = (Mat_SeqAIJ*)C->data; 27470fb991dcSHong Zhang submatj = c->submatis1; 27482d033e1fSHong Zhang if (submatj) { 2749682e4c99SStefano Zampini if (--((PetscObject)C)->refct <= 0) { 275034136279SStefano Zampini ierr = (*submatj->destroy)(C);CHKERRQ(ierr); 2751f68bb481SHong Zhang ierr = MatDestroySubMatrix_Private(submatj);CHKERRQ(ierr); 275234136279SStefano Zampini ierr = PetscFree(C->defaultvectype);CHKERRQ(ierr); 27532d033e1fSHong Zhang ierr = PetscLayoutDestroy(&C->rmap);CHKERRQ(ierr); 27542d033e1fSHong Zhang ierr = PetscLayoutDestroy(&C->cmap);CHKERRQ(ierr); 27552d033e1fSHong Zhang ierr = PetscHeaderDestroy(&C);CHKERRQ(ierr); 2756682e4c99SStefano Zampini } 27572d033e1fSHong Zhang } else { 27582d033e1fSHong Zhang ierr = MatDestroy(&C);CHKERRQ(ierr); 27592d033e1fSHong Zhang } 27602d033e1fSHong Zhang } 276186e85357SHong Zhang 276263a75b2aSHong Zhang /* Destroy Dummy submatrices created for reuse */ 276363a75b2aSHong Zhang ierr = MatDestroySubMatrices_Dummy(n,mat);CHKERRQ(ierr); 276463a75b2aSHong Zhang 27652d033e1fSHong Zhang ierr = PetscFree(*mat);CHKERRQ(ierr); 27662d033e1fSHong Zhang PetscFunctionReturn(0); 27672d033e1fSHong Zhang } 27682d033e1fSHong Zhang 27697dae84e0SHong Zhang PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[]) 2770cddf8d76SBarry Smith { 2771dfbe8321SBarry Smith PetscErrorCode ierr; 277297f1f81fSBarry Smith PetscInt i; 2773cddf8d76SBarry Smith 27743a40ed3dSBarry Smith PetscFunctionBegin; 2775cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 2776df750dc8SHong Zhang ierr = PetscCalloc1(n+1,B);CHKERRQ(ierr); 2777cddf8d76SBarry Smith } 2778cddf8d76SBarry Smith 2779cddf8d76SBarry Smith for (i=0; i<n; i++) { 27807dae84e0SHong Zhang ierr = MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 2781cddf8d76SBarry Smith } 27823a40ed3dSBarry Smith PetscFunctionReturn(0); 2783cddf8d76SBarry Smith } 2784cddf8d76SBarry Smith 278597f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov) 27864dcbc457SBarry Smith { 2787e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 27886849ba73SBarry Smith PetscErrorCode ierr; 27895d0c19d7SBarry Smith PetscInt row,i,j,k,l,m,n,*nidx,isz,val; 27905d0c19d7SBarry Smith const PetscInt *idx; 279197f1f81fSBarry Smith PetscInt start,end,*ai,*aj; 2792f1af5d2fSBarry Smith PetscBT table; 2793bbd702dbSSatish Balay 27943a40ed3dSBarry Smith PetscFunctionBegin; 2795d0f46423SBarry Smith m = A->rmap->n; 2796e4d965acSSatish Balay ai = a->i; 2797bfeeae90SHong Zhang aj = a->j; 27988a047759SSatish Balay 2799e32f2f54SBarry Smith if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used"); 280006763907SSatish Balay 2801854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&nidx);CHKERRQ(ierr); 280253b8de81SBarry Smith ierr = PetscBTCreate(m,&table);CHKERRQ(ierr); 280306763907SSatish Balay 2804e4d965acSSatish Balay for (i=0; i<is_max; i++) { 2805b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 2806e4d965acSSatish Balay isz = 0; 28076831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 2808e4d965acSSatish Balay 2809e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 28104dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 2811b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 2812e4d965acSSatish Balay 2813dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 2814e4d965acSSatish Balay for (j=0; j<n; ++j) { 28152205254eSKarl Rupp if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j]; 28164dcbc457SBarry Smith } 281706763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 28186bf464f9SBarry Smith ierr = ISDestroy(&is[i]);CHKERRQ(ierr); 2819e4d965acSSatish Balay 282004a348a9SBarry Smith k = 0; 282104a348a9SBarry Smith for (j=0; j<ov; j++) { /* for each overlap */ 282204a348a9SBarry Smith n = isz; 282306763907SSatish Balay for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */ 2824e4d965acSSatish Balay row = nidx[k]; 2825e4d965acSSatish Balay start = ai[row]; 2826e4d965acSSatish Balay end = ai[row+1]; 282704a348a9SBarry Smith for (l = start; l<end; l++) { 2828efb16452SHong Zhang val = aj[l]; 28292205254eSKarl Rupp if (!PetscBTLookupSet(table,val)) nidx[isz++] = val; 2830e4d965acSSatish Balay } 2831e4d965acSSatish Balay } 2832e4d965acSSatish Balay } 283370b3c8c7SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr); 2834e4d965acSSatish Balay } 283594bacf5dSBarry Smith ierr = PetscBTDestroy(&table);CHKERRQ(ierr); 2836606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 28373a40ed3dSBarry Smith PetscFunctionReturn(0); 28384dcbc457SBarry Smith } 283917ab2063SBarry Smith 28400513a670SBarry Smith /* -------------------------------------------------------------- */ 2841dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 28420513a670SBarry Smith { 28430513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 28446849ba73SBarry Smith PetscErrorCode ierr; 28453b98c0a2SBarry Smith PetscInt i,nz = 0,m = A->rmap->n,n = A->cmap->n; 28465d0c19d7SBarry Smith const PetscInt *row,*col; 28475d0c19d7SBarry Smith PetscInt *cnew,j,*lens; 284856cd22aeSBarry Smith IS icolp,irowp; 28490298fd71SBarry Smith PetscInt *cwork = NULL; 28500298fd71SBarry Smith PetscScalar *vwork = NULL; 28510513a670SBarry Smith 28523a40ed3dSBarry Smith PetscFunctionBegin; 28534c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 285456cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 28554c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 285656cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 28570513a670SBarry Smith 28580513a670SBarry Smith /* determine lengths of permuted rows */ 2859854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&lens);CHKERRQ(ierr); 28602205254eSKarl Rupp for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i]; 2861ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 2862f69a0ea3SMatthew Knepley ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr); 286333d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 28647adad957SLisandro Dalcin ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2865ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr); 2866606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 28670513a670SBarry Smith 2868785e854fSJed Brown ierr = PetscMalloc1(n,&cnew);CHKERRQ(ierr); 28690513a670SBarry Smith for (i=0; i<m; i++) { 287032ec9ce4SBarry Smith ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 28712205254eSKarl Rupp for (j=0; j<nz; j++) cnew[j] = col[cwork[j]]; 2872cdc0ba36SBarry Smith ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 287332ec9ce4SBarry Smith ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 28740513a670SBarry Smith } 2875606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 28762205254eSKarl Rupp 28773c7d62e4SBarry Smith (*B)->assembled = PETSC_FALSE; 28782205254eSKarl Rupp 28799fe5e383SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2880b470e4b4SRichard Tran Mills ierr = MatBindToCPU(*B,A->boundtocpu);CHKERRQ(ierr); 28819fe5e383SStefano Zampini #endif 28820513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28830513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 288456cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 288556cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 28866bf464f9SBarry Smith ierr = ISDestroy(&irowp);CHKERRQ(ierr); 28876bf464f9SBarry Smith ierr = ISDestroy(&icolp);CHKERRQ(ierr); 28886768869dSprj- if (rowp == colp) { 28896768869dSprj- if (A->symmetric) { 28906768869dSprj- ierr = MatSetOption(*B,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); 28916768869dSprj- } 28926768869dSprj- if (A->hermitian) { 28936768869dSprj- ierr = MatSetOption(*B,MAT_HERMITIAN,PETSC_TRUE);CHKERRQ(ierr); 28946768869dSprj- } 28956768869dSprj- } 28963a40ed3dSBarry Smith PetscFunctionReturn(0); 28970513a670SBarry Smith } 28980513a670SBarry Smith 2899dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 2900cb5b572fSBarry Smith { 2901dfbe8321SBarry Smith PetscErrorCode ierr; 2902cb5b572fSBarry Smith 2903cb5b572fSBarry Smith PetscFunctionBegin; 290433f4a19fSKris Buschelman /* If the two matrices have the same copy implementation, use fast copy. */ 290533f4a19fSKris Buschelman if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) { 2906be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2907be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 2908be6bf707SBarry Smith 29094d805d7cSStefano Zampini if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different %D != %D",a->i[A->rmap->n],b->i[B->rmap->n]); 2910580bdb30SBarry Smith ierr = PetscArraycpy(b->a,a->a,a->i[A->rmap->n]);CHKERRQ(ierr); 2911cdc753b6SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr); 2912cb5b572fSBarry Smith } else { 2913cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2914cb5b572fSBarry Smith } 2915cb5b572fSBarry Smith PetscFunctionReturn(0); 2916cb5b572fSBarry Smith } 2917cb5b572fSBarry Smith 29184994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A) 2919273d9f13SBarry Smith { 2920dfbe8321SBarry Smith PetscErrorCode ierr; 2921273d9f13SBarry Smith 2922273d9f13SBarry Smith PetscFunctionBegin; 2923ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 2924273d9f13SBarry Smith PetscFunctionReturn(0); 2925273d9f13SBarry Smith } 2926273d9f13SBarry Smith 2927f38c1e66SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[]) 29286c0721eeSBarry Smith { 29296c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 29306e111a19SKarl Rupp 29316c0721eeSBarry Smith PetscFunctionBegin; 29326c0721eeSBarry Smith *array = a->a; 29336c0721eeSBarry Smith PetscFunctionReturn(0); 29346c0721eeSBarry Smith } 29356c0721eeSBarry Smith 2936f38c1e66SStefano Zampini PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[]) 29376c0721eeSBarry Smith { 29386c0721eeSBarry Smith PetscFunctionBegin; 2939f38c1e66SStefano Zampini *array = NULL; 29406c0721eeSBarry Smith PetscFunctionReturn(0); 29416c0721eeSBarry Smith } 2942273d9f13SBarry Smith 29438229c054SShri Abhyankar /* 29448229c054SShri Abhyankar Computes the number of nonzeros per row needed for preallocation when X and Y 29458229c054SShri Abhyankar have different nonzero structure. 29468229c054SShri Abhyankar */ 2947b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz) 2948ec7775f6SShri Abhyankar { 2949b264fe52SHong Zhang PetscInt i,j,k,nzx,nzy; 2950ec7775f6SShri Abhyankar 2951ec7775f6SShri Abhyankar PetscFunctionBegin; 2952ec7775f6SShri Abhyankar /* Set the number of nonzeros in the new matrix */ 2953ec7775f6SShri Abhyankar for (i=0; i<m; i++) { 2954b264fe52SHong Zhang const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i]; 2955b264fe52SHong Zhang nzx = xi[i+1] - xi[i]; 2956b264fe52SHong Zhang nzy = yi[i+1] - yi[i]; 29578af7cee1SJed Brown nnz[i] = 0; 29588af7cee1SJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 2959b264fe52SHong Zhang for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */ 2960b264fe52SHong Zhang if (k<nzy && yjj[k]==xjj[j]) k++; /* Skip duplicate */ 29618af7cee1SJed Brown nnz[i]++; 29628af7cee1SJed Brown } 29638af7cee1SJed Brown for (; k<nzy; k++) nnz[i]++; 2964ec7775f6SShri Abhyankar } 2965ec7775f6SShri Abhyankar PetscFunctionReturn(0); 2966ec7775f6SShri Abhyankar } 2967ec7775f6SShri Abhyankar 2968b264fe52SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz) 2969b264fe52SHong Zhang { 2970b264fe52SHong Zhang PetscInt m = Y->rmap->N; 2971b264fe52SHong Zhang Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 2972b264fe52SHong Zhang Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 2973b264fe52SHong Zhang PetscErrorCode ierr; 2974b264fe52SHong Zhang 2975b264fe52SHong Zhang PetscFunctionBegin; 2976b264fe52SHong Zhang /* Set the number of nonzeros in the new matrix */ 2977b264fe52SHong Zhang ierr = MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr); 2978b264fe52SHong Zhang PetscFunctionReturn(0); 2979b264fe52SHong Zhang } 2980b264fe52SHong Zhang 2981f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2982ac90fabeSBarry Smith { 2983dfbe8321SBarry Smith PetscErrorCode ierr; 2984ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data; 2985c5df96a5SBarry Smith PetscBLASInt one=1,bnz; 2986ac90fabeSBarry Smith 2987ac90fabeSBarry Smith PetscFunctionBegin; 2988c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 2989ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2990f4df32b1SMatthew Knepley PetscScalar alpha = a; 29918b83055fSJed Brown PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2992acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal(Y);CHKERRQ(ierr); 2993a3fa217bSJose E. Roman ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); 2994e2cf4d64SStefano Zampini /* the MatAXPY_Basic* subroutines calls MatAssembly, so the matrix on the GPU 2995e2cf4d64SStefano Zampini will be updated */ 2996e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 2997c70f7ee4SJunchao Zhang if (Y->offloadmask != PETSC_OFFLOAD_UNALLOCATED) { 2998c70f7ee4SJunchao Zhang Y->offloadmask = PETSC_OFFLOAD_CPU; 2999e2cf4d64SStefano Zampini } 3000e2cf4d64SStefano Zampini #endif 3001ab784542SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */ 3002ab784542SHong Zhang ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 3003ac90fabeSBarry Smith } else { 30048229c054SShri Abhyankar Mat B; 30058229c054SShri Abhyankar PetscInt *nnz; 3006785e854fSJed Brown ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr); 3007ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr); 3008bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 30094aa94f47SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 301033d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr); 3011176df525SBarry Smith ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr); 30128229c054SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr); 3013ecd8bba6SJed Brown ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr); 3014ec7775f6SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 301528be2f97SBarry Smith ierr = MatHeaderReplace(Y,&B);CHKERRQ(ierr); 30168229c054SShri Abhyankar ierr = PetscFree(nnz);CHKERRQ(ierr); 3017ac90fabeSBarry Smith } 3018ac90fabeSBarry Smith PetscFunctionReturn(0); 3019ac90fabeSBarry Smith } 3020ac90fabeSBarry Smith 30217087cfbeSBarry Smith PetscErrorCode MatConjugate_SeqAIJ(Mat mat) 3022354c94deSBarry Smith { 3023354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 3024354c94deSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 3025354c94deSBarry Smith PetscInt i,nz; 3026354c94deSBarry Smith PetscScalar *a; 3027354c94deSBarry Smith 3028354c94deSBarry Smith PetscFunctionBegin; 3029354c94deSBarry Smith nz = aij->nz; 3030354c94deSBarry Smith a = aij->a; 30312205254eSKarl Rupp for (i=0; i<nz; i++) a[i] = PetscConj(a[i]); 3032e2cf4d64SStefano Zampini #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA) 3033c70f7ee4SJunchao Zhang if (mat->offloadmask != PETSC_OFFLOAD_UNALLOCATED) mat->offloadmask = PETSC_OFFLOAD_CPU; 3034e2cf4d64SStefano Zampini #endif 3035354c94deSBarry Smith #else 3036354c94deSBarry Smith PetscFunctionBegin; 3037354c94deSBarry Smith #endif 3038354c94deSBarry Smith PetscFunctionReturn(0); 3039354c94deSBarry Smith } 3040354c94deSBarry Smith 3041985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3042e34fafa9SBarry Smith { 3043e34fafa9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3044e34fafa9SBarry Smith PetscErrorCode ierr; 3045d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 3046e34fafa9SBarry Smith PetscReal atmp; 3047985db425SBarry Smith PetscScalar *x; 3048e34fafa9SBarry Smith MatScalar *aa; 3049e34fafa9SBarry Smith 3050e34fafa9SBarry Smith PetscFunctionBegin; 3051e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3052e34fafa9SBarry Smith aa = a->a; 3053e34fafa9SBarry Smith ai = a->i; 3054e34fafa9SBarry Smith aj = a->j; 3055e34fafa9SBarry Smith 3056985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 3057e34fafa9SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3058e34fafa9SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 3059e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 3060e34fafa9SBarry Smith for (i=0; i<m; i++) { 3061e34fafa9SBarry Smith ncols = ai[1] - ai[0]; ai++; 30629189402eSHong Zhang x[i] = 0.0; 3063e34fafa9SBarry Smith for (j=0; j<ncols; j++) { 3064985db425SBarry Smith atmp = PetscAbsScalar(*aa); 3065985db425SBarry Smith if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 3066985db425SBarry Smith aa++; aj++; 3067985db425SBarry Smith } 3068985db425SBarry Smith } 3069985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3070985db425SBarry Smith PetscFunctionReturn(0); 3071985db425SBarry Smith } 3072985db425SBarry Smith 3073985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3074985db425SBarry Smith { 3075985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3076985db425SBarry Smith PetscErrorCode ierr; 3077d0f46423SBarry Smith PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 3078985db425SBarry Smith PetscScalar *x; 3079985db425SBarry Smith MatScalar *aa; 3080985db425SBarry Smith 3081985db425SBarry Smith PetscFunctionBegin; 3082e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3083985db425SBarry Smith aa = a->a; 3084985db425SBarry Smith ai = a->i; 3085985db425SBarry Smith aj = a->j; 3086985db425SBarry Smith 3087985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 3088985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3089985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 3090e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 3091985db425SBarry Smith for (i=0; i<m; i++) { 3092985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 3093d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 3094985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 3095985db425SBarry Smith } else { /* row is sparse so already KNOW maximum is 0.0 or higher */ 3096985db425SBarry Smith x[i] = 0.0; 3097985db425SBarry Smith if (idx) { 3098985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 3099985db425SBarry Smith for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */ 3100985db425SBarry Smith if (aj[j] > j) { 3101985db425SBarry Smith idx[i] = j; 3102985db425SBarry Smith break; 3103985db425SBarry Smith } 3104985db425SBarry Smith } 3105985db425SBarry Smith } 3106985db425SBarry Smith } 3107985db425SBarry Smith for (j=0; j<ncols; j++) { 3108985db425SBarry Smith if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 3109985db425SBarry Smith aa++; aj++; 3110985db425SBarry Smith } 3111985db425SBarry Smith } 3112985db425SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3113985db425SBarry Smith PetscFunctionReturn(0); 3114985db425SBarry Smith } 3115985db425SBarry Smith 3116c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3117c87e5d42SMatthew Knepley { 3118c87e5d42SMatthew Knepley Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3119c87e5d42SMatthew Knepley PetscErrorCode ierr; 3120c87e5d42SMatthew Knepley PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n; 3121c87e5d42SMatthew Knepley PetscReal atmp; 3122c87e5d42SMatthew Knepley PetscScalar *x; 3123c87e5d42SMatthew Knepley MatScalar *aa; 3124c87e5d42SMatthew Knepley 3125c87e5d42SMatthew Knepley PetscFunctionBegin; 3126e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3127c87e5d42SMatthew Knepley aa = a->a; 3128c87e5d42SMatthew Knepley ai = a->i; 3129c87e5d42SMatthew Knepley aj = a->j; 3130c87e5d42SMatthew Knepley 3131c87e5d42SMatthew Knepley ierr = VecSet(v,0.0);CHKERRQ(ierr); 3132c87e5d42SMatthew Knepley ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3133c87e5d42SMatthew Knepley ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 313460e0710aSBarry 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); 3135c87e5d42SMatthew Knepley for (i=0; i<m; i++) { 3136c87e5d42SMatthew Knepley ncols = ai[1] - ai[0]; ai++; 3137289a08f5SMatthew Knepley if (ncols) { 3138289a08f5SMatthew Knepley /* Get first nonzero */ 3139289a08f5SMatthew Knepley for (j = 0; j < ncols; j++) { 3140289a08f5SMatthew Knepley atmp = PetscAbsScalar(aa[j]); 31412205254eSKarl Rupp if (atmp > 1.0e-12) { 31422205254eSKarl Rupp x[i] = atmp; 31432205254eSKarl Rupp if (idx) idx[i] = aj[j]; 31442205254eSKarl Rupp break; 31452205254eSKarl Rupp } 3146289a08f5SMatthew Knepley } 314712431cb0SMatthew G Knepley if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;} 3148289a08f5SMatthew Knepley } else { 3149289a08f5SMatthew Knepley x[i] = 0.0; if (idx) idx[i] = 0; 3150289a08f5SMatthew Knepley } 3151c87e5d42SMatthew Knepley for (j = 0; j < ncols; j++) { 3152c87e5d42SMatthew Knepley atmp = PetscAbsScalar(*aa); 3153289a08f5SMatthew Knepley if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;} 3154c87e5d42SMatthew Knepley aa++; aj++; 3155c87e5d42SMatthew Knepley } 3156c87e5d42SMatthew Knepley } 3157c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3158c87e5d42SMatthew Knepley PetscFunctionReturn(0); 3159c87e5d42SMatthew Knepley } 3160c87e5d42SMatthew Knepley 3161985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[]) 3162985db425SBarry Smith { 3163985db425SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3164985db425SBarry Smith PetscErrorCode ierr; 3165d9ca1df4SBarry Smith PetscInt i,j,m = A->rmap->n,ncols,n; 3166d9ca1df4SBarry Smith const PetscInt *ai,*aj; 3167985db425SBarry Smith PetscScalar *x; 3168d9ca1df4SBarry Smith const MatScalar *aa; 3169985db425SBarry Smith 3170985db425SBarry Smith PetscFunctionBegin; 3171e32f2f54SBarry Smith if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3172985db425SBarry Smith aa = a->a; 3173985db425SBarry Smith ai = a->i; 3174985db425SBarry Smith aj = a->j; 3175985db425SBarry Smith 3176985db425SBarry Smith ierr = VecSet(v,0.0);CHKERRQ(ierr); 3177985db425SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 3178985db425SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 3179e32f2f54SBarry Smith if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 3180985db425SBarry Smith for (i=0; i<m; i++) { 3181985db425SBarry Smith ncols = ai[1] - ai[0]; ai++; 3182d0f46423SBarry Smith if (ncols == A->cmap->n) { /* row is dense */ 3183985db425SBarry Smith x[i] = *aa; if (idx) idx[i] = 0; 3184985db425SBarry Smith } else { /* row is sparse so already KNOW minimum is 0.0 or lower */ 3185985db425SBarry Smith x[i] = 0.0; 3186985db425SBarry Smith if (idx) { /* find first implicit 0.0 in the row */ 3187985db425SBarry Smith idx[i] = 0; /* in case ncols is zero */ 3188985db425SBarry Smith for (j=0; j<ncols; j++) { 3189985db425SBarry Smith if (aj[j] > j) { 3190985db425SBarry Smith idx[i] = j; 3191985db425SBarry Smith break; 3192985db425SBarry Smith } 3193985db425SBarry Smith } 3194985db425SBarry Smith } 3195985db425SBarry Smith } 3196985db425SBarry Smith for (j=0; j<ncols; j++) { 3197985db425SBarry Smith if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;} 3198985db425SBarry Smith aa++; aj++; 3199e34fafa9SBarry Smith } 3200e34fafa9SBarry Smith } 3201e34fafa9SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 3202e34fafa9SBarry Smith PetscFunctionReturn(0); 3203e34fafa9SBarry Smith } 3204bbead8a2SBarry Smith 3205713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values) 3206bbead8a2SBarry Smith { 3207bbead8a2SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data; 3208bbead8a2SBarry Smith PetscErrorCode ierr; 320933d57670SJed Brown PetscInt i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j; 3210bbead8a2SBarry Smith MatScalar *diag,work[25],*v_work; 32110da83c2eSBarry Smith const PetscReal shift = 0.0; 32121a9391e3SHong Zhang PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE; 3213bbead8a2SBarry Smith 3214bbead8a2SBarry Smith PetscFunctionBegin; 3215a455e926SHong Zhang allowzeropivot = PetscNot(A->erroriffailure); 32164a0d0026SBarry Smith if (a->ibdiagvalid) { 32174a0d0026SBarry Smith if (values) *values = a->ibdiag; 32184a0d0026SBarry Smith PetscFunctionReturn(0); 32194a0d0026SBarry Smith } 3220bbead8a2SBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 3221bbead8a2SBarry Smith if (!a->ibdiag) { 3222785e854fSJed Brown ierr = PetscMalloc1(bs2*mbs,&a->ibdiag);CHKERRQ(ierr); 32233bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr); 3224bbead8a2SBarry Smith } 3225bbead8a2SBarry Smith diag = a->ibdiag; 3226bbead8a2SBarry Smith if (values) *values = a->ibdiag; 3227bbead8a2SBarry Smith /* factor and invert each block */ 3228bbead8a2SBarry Smith switch (bs) { 3229bbead8a2SBarry Smith case 1: 3230bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3231bbead8a2SBarry Smith ierr = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr); 3232ec1892c8SHong Zhang if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) { 3233ec1892c8SHong Zhang if (allowzeropivot) { 32347b6c816cSBarry Smith A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 32357b6c816cSBarry Smith A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]); 32367b6c816cSBarry Smith A->factorerror_zeropivot_row = i; 32377b6c816cSBarry Smith ierr = PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);CHKERRQ(ierr); 32387b6c816cSBarry 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); 3239ec1892c8SHong Zhang } 3240bbead8a2SBarry Smith diag[i] = (PetscScalar)1.0 / (diag[i] + shift); 3241bbead8a2SBarry Smith } 3242bbead8a2SBarry Smith break; 3243bbead8a2SBarry Smith case 2: 3244bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3245bbead8a2SBarry Smith ij[0] = 2*i; ij[1] = 2*i + 1; 3246bbead8a2SBarry Smith ierr = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr); 3247a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32487b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 324996b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr); 3250bbead8a2SBarry Smith diag += 4; 3251bbead8a2SBarry Smith } 3252bbead8a2SBarry Smith break; 3253bbead8a2SBarry Smith case 3: 3254bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3255bbead8a2SBarry Smith ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2; 3256bbead8a2SBarry Smith ierr = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr); 3257a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32587b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 325996b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr); 3260bbead8a2SBarry Smith diag += 9; 3261bbead8a2SBarry Smith } 3262bbead8a2SBarry Smith break; 3263bbead8a2SBarry Smith case 4: 3264bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3265bbead8a2SBarry Smith ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3; 3266bbead8a2SBarry Smith ierr = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr); 3267a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32687b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 326996b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr); 3270bbead8a2SBarry Smith diag += 16; 3271bbead8a2SBarry Smith } 3272bbead8a2SBarry Smith break; 3273bbead8a2SBarry Smith case 5: 3274bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3275bbead8a2SBarry 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; 3276bbead8a2SBarry Smith ierr = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr); 3277a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32787b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 327996b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr); 3280bbead8a2SBarry Smith diag += 25; 3281bbead8a2SBarry Smith } 3282bbead8a2SBarry Smith break; 3283bbead8a2SBarry Smith case 6: 3284bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3285bbead8a2SBarry 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; 3286bbead8a2SBarry Smith ierr = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr); 3287a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32887b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 328996b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr); 3290bbead8a2SBarry Smith diag += 36; 3291bbead8a2SBarry Smith } 3292bbead8a2SBarry Smith break; 3293bbead8a2SBarry Smith case 7: 3294bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3295bbead8a2SBarry 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; 3296bbead8a2SBarry Smith ierr = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr); 3297a455e926SHong Zhang ierr = PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 32987b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 329996b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr); 3300bbead8a2SBarry Smith diag += 49; 3301bbead8a2SBarry Smith } 3302bbead8a2SBarry Smith break; 3303bbead8a2SBarry Smith default: 3304dcca6d9dSJed Brown ierr = PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);CHKERRQ(ierr); 3305bbead8a2SBarry Smith for (i=0; i<mbs; i++) { 3306bbead8a2SBarry Smith for (j=0; j<bs; j++) { 3307bbead8a2SBarry Smith IJ[j] = bs*i + j; 3308bbead8a2SBarry Smith } 3309bbead8a2SBarry Smith ierr = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr); 33105f8bbccaSHong Zhang ierr = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);CHKERRQ(ierr); 33117b6c816cSBarry Smith if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT; 331296b95a6bSBarry Smith ierr = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr); 3313bbead8a2SBarry Smith diag += bs2; 3314bbead8a2SBarry Smith } 3315bbead8a2SBarry Smith ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr); 3316bbead8a2SBarry Smith } 3317bbead8a2SBarry Smith a->ibdiagvalid = PETSC_TRUE; 3318bbead8a2SBarry Smith PetscFunctionReturn(0); 3319bbead8a2SBarry Smith } 3320bbead8a2SBarry Smith 332173a71a0fSBarry Smith static PetscErrorCode MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx) 332273a71a0fSBarry Smith { 332373a71a0fSBarry Smith PetscErrorCode ierr; 332473a71a0fSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data; 332573a71a0fSBarry Smith PetscScalar a; 332673a71a0fSBarry Smith PetscInt m,n,i,j,col; 332773a71a0fSBarry Smith 332873a71a0fSBarry Smith PetscFunctionBegin; 332973a71a0fSBarry Smith if (!x->assembled) { 333073a71a0fSBarry Smith ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr); 333173a71a0fSBarry Smith for (i=0; i<m; i++) { 333273a71a0fSBarry Smith for (j=0; j<aij->imax[i]; j++) { 333373a71a0fSBarry Smith ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr); 333473a71a0fSBarry Smith col = (PetscInt)(n*PetscRealPart(a)); 333573a71a0fSBarry Smith ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr); 333673a71a0fSBarry Smith } 333773a71a0fSBarry Smith } 3338e2ce353bSJunchao Zhang } else { 3339e2ce353bSJunchao Zhang for (i=0; i<aij->nz; i++) {ierr = PetscRandomGetValue(rctx,aij->a+i);CHKERRQ(ierr);} 3340e2ce353bSJunchao Zhang } 334173a71a0fSBarry Smith ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 334273a71a0fSBarry Smith ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 334373a71a0fSBarry Smith PetscFunctionReturn(0); 334473a71a0fSBarry Smith } 334573a71a0fSBarry Smith 3346679944adSJunchao Zhang /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */ 3347679944adSJunchao Zhang PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx) 3348679944adSJunchao Zhang { 3349679944adSJunchao Zhang PetscErrorCode ierr; 3350679944adSJunchao Zhang Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data; 3351679944adSJunchao Zhang PetscScalar a; 3352679944adSJunchao Zhang PetscInt m,n,i,j,col,nskip; 3353679944adSJunchao Zhang 3354679944adSJunchao Zhang PetscFunctionBegin; 3355679944adSJunchao Zhang nskip = high - low; 3356679944adSJunchao Zhang ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr); 3357679944adSJunchao Zhang n -= nskip; /* shrink number of columns where nonzeros can be set */ 3358679944adSJunchao Zhang for (i=0; i<m; i++) { 3359679944adSJunchao Zhang for (j=0; j<aij->imax[i]; j++) { 3360679944adSJunchao Zhang ierr = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr); 3361679944adSJunchao Zhang col = (PetscInt)(n*PetscRealPart(a)); 3362679944adSJunchao Zhang if (col >= low) col += nskip; /* shift col rightward to skip the hole */ 3363679944adSJunchao Zhang ierr = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr); 3364679944adSJunchao Zhang } 3365e2ce353bSJunchao Zhang } 3366679944adSJunchao Zhang ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3367679944adSJunchao Zhang ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3368679944adSJunchao Zhang PetscFunctionReturn(0); 3369679944adSJunchao Zhang } 3370679944adSJunchao Zhang 3371679944adSJunchao Zhang 3372682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 33730a6ffc59SBarry Smith static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ, 3374cb5b572fSBarry Smith MatGetRow_SeqAIJ, 3375cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 3376cb5b572fSBarry Smith MatMult_SeqAIJ, 337797304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ, 33787c922b88SBarry Smith MatMultTranspose_SeqAIJ, 33797c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 3380db4efbfdSBarry Smith 0, 3381db4efbfdSBarry Smith 0, 3382db4efbfdSBarry Smith 0, 3383db4efbfdSBarry Smith /* 10*/ 0, 3384cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 3385cb5b572fSBarry Smith 0, 338641f059aeSBarry Smith MatSOR_SeqAIJ, 338791e9d3e2SHong Zhang MatTranspose_SeqAIJ, 338897304618SKris Buschelman /*1 5*/ MatGetInfo_SeqAIJ, 3389cb5b572fSBarry Smith MatEqual_SeqAIJ, 3390cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 3391cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 3392cb5b572fSBarry Smith MatNorm_SeqAIJ, 339397304618SKris Buschelman /* 20*/ 0, 3394cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 3395cb5b572fSBarry Smith MatSetOption_SeqAIJ, 3396cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 3397d519adbfSMatthew Knepley /* 24*/ MatZeroRows_SeqAIJ, 3398db4efbfdSBarry Smith 0, 3399db4efbfdSBarry Smith 0, 3400db4efbfdSBarry Smith 0, 3401db4efbfdSBarry Smith 0, 34024994cf47SJed Brown /* 29*/ MatSetUp_SeqAIJ, 3403db4efbfdSBarry Smith 0, 3404db4efbfdSBarry Smith 0, 34058c778c55SBarry Smith 0, 34068c778c55SBarry Smith 0, 3407d519adbfSMatthew Knepley /* 34*/ MatDuplicate_SeqAIJ, 3408cb5b572fSBarry Smith 0, 3409cb5b572fSBarry Smith 0, 3410cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 3411cb5b572fSBarry Smith 0, 3412d519adbfSMatthew Knepley /* 39*/ MatAXPY_SeqAIJ, 34137dae84e0SHong Zhang MatCreateSubMatrices_SeqAIJ, 3414cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 3415cb5b572fSBarry Smith MatGetValues_SeqAIJ, 3416cb5b572fSBarry Smith MatCopy_SeqAIJ, 3417d519adbfSMatthew Knepley /* 44*/ MatGetRowMax_SeqAIJ, 3418cb5b572fSBarry Smith MatScale_SeqAIJ, 34197d68702bSBarry Smith MatShift_SeqAIJ, 342079299369SBarry Smith MatDiagonalSet_SeqAIJ, 34216e169961SBarry Smith MatZeroRowsColumns_SeqAIJ, 342273a71a0fSBarry Smith /* 49*/ MatSetRandom_SeqAIJ, 34233b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 34243b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 34253b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 3426a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 342793dfae19SHong Zhang /* 54*/ MatFDColoringCreate_SeqXAIJ, 3428b9617806SBarry Smith 0, 34290513a670SBarry Smith 0, 3430cda55fadSBarry Smith MatPermute_SeqAIJ, 3431cda55fadSBarry Smith 0, 3432d519adbfSMatthew Knepley /* 59*/ 0, 3433b9b97703SBarry Smith MatDestroy_SeqAIJ, 3434b9b97703SBarry Smith MatView_SeqAIJ, 3435357abbc8SBarry Smith 0, 34364222ddf1SHong Zhang 0, 34374222ddf1SHong Zhang /* 64*/ 0, 3438321b30b9SSatish Balay MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ, 3439ee4f033dSBarry Smith 0, 3440ee4f033dSBarry Smith 0, 3441ee4f033dSBarry Smith 0, 3442d519adbfSMatthew Knepley /* 69*/ MatGetRowMaxAbs_SeqAIJ, 3443c87e5d42SMatthew Knepley MatGetRowMinAbs_SeqAIJ, 3444ee4f033dSBarry Smith 0, 3445dcf5cc72SBarry Smith 0, 34462c93a97aSBarry Smith 0, 34472c93a97aSBarry Smith /* 74*/ 0, 34483acb8795SBarry Smith MatFDColoringApply_AIJ, 344997304618SKris Buschelman 0, 345097304618SKris Buschelman 0, 345197304618SKris Buschelman 0, 34526ce1633cSBarry Smith /* 79*/ MatFindZeroDiagonals_SeqAIJ, 345397304618SKris Buschelman 0, 345497304618SKris Buschelman 0, 345597304618SKris Buschelman 0, 3456bc011b1eSHong Zhang MatLoad_SeqAIJ, 3457d519adbfSMatthew Knepley /* 84*/ MatIsSymmetric_SeqAIJ, 34581cbb95d3SBarry Smith MatIsHermitian_SeqAIJ, 34596284ec50SHong Zhang 0, 34606284ec50SHong Zhang 0, 3461bc011b1eSHong Zhang 0, 34624222ddf1SHong Zhang /* 89*/ 0, 34634222ddf1SHong Zhang 0, 346426be0446SHong Zhang MatMatMultNumeric_SeqAIJ_SeqAIJ, 34654222ddf1SHong Zhang 0, 34664222ddf1SHong Zhang 0, 34678fa4b5a6SHong Zhang /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy, 34684222ddf1SHong Zhang 0, 34694222ddf1SHong Zhang 0, 34706fc122caSHong Zhang MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ, 34712121bac1SHong Zhang 0, 34724222ddf1SHong Zhang /* 99*/ MatProductSetFromOptions_SeqAIJ, 3473609c6c4dSKris Buschelman 0, 3474609c6c4dSKris Buschelman 0, 347587d4246cSBarry Smith MatConjugate_SeqAIJ, 347687d4246cSBarry Smith 0, 3477d519adbfSMatthew Knepley /*104*/ MatSetValuesRow_SeqAIJ, 347899cafbc1SBarry Smith MatRealPart_SeqAIJ, 3479f5edf698SHong Zhang MatImaginaryPart_SeqAIJ, 3480f5edf698SHong Zhang 0, 34812bebee5dSHong Zhang 0, 3482cbd44569SHong Zhang /*109*/ MatMatSolve_SeqAIJ, 3483985db425SBarry Smith 0, 34842af78befSBarry Smith MatGetRowMin_SeqAIJ, 34852af78befSBarry Smith 0, 3486599ef60dSHong Zhang MatMissingDiagonal_SeqAIJ, 3487d519adbfSMatthew Knepley /*114*/ 0, 3488599ef60dSHong Zhang 0, 34893c2a7987SHong Zhang 0, 3490fe97e370SBarry Smith 0, 3491fbdbba38SShri Abhyankar 0, 3492fbdbba38SShri Abhyankar /*119*/ 0, 3493fbdbba38SShri Abhyankar 0, 3494fbdbba38SShri Abhyankar 0, 349582d44351SHong Zhang 0, 3496b3a44c85SBarry Smith MatGetMultiProcBlock_SeqAIJ, 34970716a85fSBarry Smith /*124*/ MatFindNonzeroRows_SeqAIJ, 3498bbead8a2SBarry Smith MatGetColumnNorms_SeqAIJ, 349937868618SMatthew G Knepley MatInvertBlockDiagonal_SeqAIJ, 35000da83c2eSBarry Smith MatInvertVariableBlockDiagonal_SeqAIJ, 350137868618SMatthew G Knepley 0, 35025df89d91SHong Zhang /*129*/ 0, 35034222ddf1SHong Zhang 0, 35044222ddf1SHong Zhang 0, 350575648e8dSHong Zhang MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ, 3506b9af6bddSHong Zhang MatTransposeColoringCreate_SeqAIJ, 3507b9af6bddSHong Zhang /*134*/ MatTransColoringApplySpToDen_SeqAIJ, 35082b8ad9a3SHong Zhang MatTransColoringApplyDenToSp_SeqAIJ, 35094222ddf1SHong Zhang 0, 35104222ddf1SHong Zhang 0, 35113964eb88SJed Brown MatRARtNumeric_SeqAIJ_SeqAIJ, 35123964eb88SJed Brown /*139*/0, 3513f9426fe0SMark Adams 0, 35141919a2e2SJed Brown 0, 35153a062f41SBarry Smith MatFDColoringSetUp_SeqXAIJ, 35169c8f2541SHong Zhang MatFindOffBlockDiagonalEntries_SeqAIJ, 35174222ddf1SHong Zhang MatCreateMPIMatConcatenateSeqMat_SeqAIJ, 35184222ddf1SHong Zhang /*145*/MatDestroySubMatrices_SeqAIJ, 35194222ddf1SHong Zhang 0, 35204222ddf1SHong Zhang 0 35219e29f15eSvictorle }; 352217ab2063SBarry Smith 35237087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices) 3524bef8e0ddSBarry Smith { 3525bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 352697f1f81fSBarry Smith PetscInt i,nz,n; 3527bef8e0ddSBarry Smith 3528bef8e0ddSBarry Smith PetscFunctionBegin; 3529bef8e0ddSBarry Smith nz = aij->maxnz; 3530d0f46423SBarry Smith n = mat->rmap->n; 3531bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 3532bef8e0ddSBarry Smith aij->j[i] = indices[i]; 3533bef8e0ddSBarry Smith } 3534bef8e0ddSBarry Smith aij->nz = nz; 3535bef8e0ddSBarry Smith for (i=0; i<n; i++) { 3536bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 3537bef8e0ddSBarry Smith } 3538bef8e0ddSBarry Smith PetscFunctionReturn(0); 3539bef8e0ddSBarry Smith } 3540bef8e0ddSBarry Smith 3541a3bb6f32SFande Kong /* 3542e8b528d9SFande Kong * When a sparse matrix has many zero columns, we should compact them out to save the space 3543a3bb6f32SFande Kong * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable() 3544a3bb6f32SFande Kong * */ 3545a3bb6f32SFande Kong PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping) 3546a3bb6f32SFande Kong { 3547a3bb6f32SFande Kong Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 3548a3bb6f32SFande Kong PetscTable gid1_lid1; 3549a3bb6f32SFande Kong PetscTablePosition tpos; 3550a3bb6f32SFande Kong PetscInt gid,lid,i,j,ncols,ec; 3551a3bb6f32SFande Kong PetscInt *garray; 3552a3bb6f32SFande Kong PetscErrorCode ierr; 3553a3bb6f32SFande Kong 3554a3bb6f32SFande Kong PetscFunctionBegin; 3555a3bb6f32SFande Kong PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3556a3bb6f32SFande Kong PetscValidPointer(mapping,2); 3557a3bb6f32SFande Kong /* use a table */ 3558a3bb6f32SFande Kong ierr = PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);CHKERRQ(ierr); 3559a3bb6f32SFande Kong ec = 0; 3560a3bb6f32SFande Kong for (i=0; i<mat->rmap->n; i++) { 3561a3bb6f32SFande Kong ncols = aij->i[i+1] - aij->i[i]; 3562a3bb6f32SFande Kong for (j=0; j<ncols; j++) { 3563a3bb6f32SFande Kong PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1; 3564a3bb6f32SFande Kong ierr = PetscTableFind(gid1_lid1,gid1,&data);CHKERRQ(ierr); 3565a3bb6f32SFande Kong if (!data) { 3566a3bb6f32SFande Kong /* one based table */ 3567a3bb6f32SFande Kong ierr = PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);CHKERRQ(ierr); 3568a3bb6f32SFande Kong } 3569a3bb6f32SFande Kong } 3570a3bb6f32SFande Kong } 3571a3bb6f32SFande Kong /* form array of columns we need */ 3572a3bb6f32SFande Kong ierr = PetscMalloc1(ec+1,&garray);CHKERRQ(ierr); 3573a3bb6f32SFande Kong ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr); 3574a3bb6f32SFande Kong while (tpos) { 3575a3bb6f32SFande Kong ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);CHKERRQ(ierr); 3576a3bb6f32SFande Kong gid--; 3577a3bb6f32SFande Kong lid--; 3578a3bb6f32SFande Kong garray[lid] = gid; 3579a3bb6f32SFande Kong } 3580a3bb6f32SFande Kong ierr = PetscSortInt(ec,garray);CHKERRQ(ierr); /* sort, and rebuild */ 3581a3bb6f32SFande Kong ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr); 3582a3bb6f32SFande Kong for (i=0; i<ec; i++) { 3583a3bb6f32SFande Kong ierr = PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr); 3584a3bb6f32SFande Kong } 3585a3bb6f32SFande Kong /* compact out the extra columns in B */ 3586a3bb6f32SFande Kong for (i=0; i<mat->rmap->n; i++) { 3587a3bb6f32SFande Kong ncols = aij->i[i+1] - aij->i[i]; 3588a3bb6f32SFande Kong for (j=0; j<ncols; j++) { 3589a3bb6f32SFande Kong PetscInt gid1 = aij->j[aij->i[i] + j] + 1; 3590a3bb6f32SFande Kong ierr = PetscTableFind(gid1_lid1,gid1,&lid);CHKERRQ(ierr); 3591a3bb6f32SFande Kong lid--; 3592a3bb6f32SFande Kong aij->j[aij->i[i] + j] = lid; 3593a3bb6f32SFande Kong } 3594a3bb6f32SFande Kong } 3595ca5434daSLawrence Mitchell ierr = PetscLayoutDestroy(&mat->cmap);CHKERRQ(ierr); 3596ca5434daSLawrence Mitchell ierr = PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat),ec,ec,1,&mat->cmap);CHKERRQ(ierr); 3597a3bb6f32SFande Kong ierr = PetscTableDestroy(&gid1_lid1);CHKERRQ(ierr); 3598a3bb6f32SFande Kong ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr); 3599a3bb6f32SFande Kong ierr = ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr); 3600a3bb6f32SFande Kong PetscFunctionReturn(0); 3601a3bb6f32SFande Kong } 3602a3bb6f32SFande Kong 3603bef8e0ddSBarry Smith /*@ 3604bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 3605bef8e0ddSBarry Smith in the matrix. 3606bef8e0ddSBarry Smith 3607bef8e0ddSBarry Smith Input Parameters: 3608bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 3609bef8e0ddSBarry Smith - indices - the column indices 3610bef8e0ddSBarry Smith 361115091d37SBarry Smith Level: advanced 361215091d37SBarry Smith 3613bef8e0ddSBarry Smith Notes: 3614bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 3615bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 3616bef8e0ddSBarry Smith of the MatSetValues() operation. 3617bef8e0ddSBarry Smith 3618bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 3619d1be2dadSMatthew Knepley MatCreateSeqAIJ(), and the columns indices MUST be sorted. 3620bef8e0ddSBarry Smith 3621bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 3622bef8e0ddSBarry Smith 3623b9617806SBarry Smith The indices should start with zero, not one. 3624b9617806SBarry Smith 3625bef8e0ddSBarry Smith @*/ 36267087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices) 3627bef8e0ddSBarry Smith { 36284ac538c5SBarry Smith PetscErrorCode ierr; 3629bef8e0ddSBarry Smith 3630bef8e0ddSBarry Smith PetscFunctionBegin; 36310700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 36324482741eSBarry Smith PetscValidPointer(indices,2); 36334ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr); 3634bef8e0ddSBarry Smith PetscFunctionReturn(0); 3635bef8e0ddSBarry Smith } 3636bef8e0ddSBarry Smith 3637be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 3638be6bf707SBarry Smith 36397087cfbeSBarry Smith PetscErrorCode MatStoreValues_SeqAIJ(Mat mat) 3640be6bf707SBarry Smith { 3641be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 36426849ba73SBarry Smith PetscErrorCode ierr; 3643d0f46423SBarry Smith size_t nz = aij->i[mat->rmap->n]; 3644be6bf707SBarry Smith 3645be6bf707SBarry Smith PetscFunctionBegin; 3646169f6850SBarry Smith if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3647be6bf707SBarry Smith 3648be6bf707SBarry Smith /* allocate space for values if not already there */ 3649be6bf707SBarry Smith if (!aij->saved_values) { 3650854ce69bSBarry Smith ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr); 36513bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr); 3652be6bf707SBarry Smith } 3653be6bf707SBarry Smith 3654be6bf707SBarry Smith /* copy values over */ 3655580bdb30SBarry Smith ierr = PetscArraycpy(aij->saved_values,aij->a,nz);CHKERRQ(ierr); 3656be6bf707SBarry Smith PetscFunctionReturn(0); 3657be6bf707SBarry Smith } 3658be6bf707SBarry Smith 3659be6bf707SBarry Smith /*@ 3660be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 3661be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3662be6bf707SBarry Smith nonlinear portion. 3663be6bf707SBarry Smith 3664be6bf707SBarry Smith Collect on Mat 3665be6bf707SBarry Smith 3666be6bf707SBarry Smith Input Parameters: 36670e609b76SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 3668be6bf707SBarry Smith 366915091d37SBarry Smith Level: advanced 367015091d37SBarry Smith 3671be6bf707SBarry Smith Common Usage, with SNESSolve(): 3672be6bf707SBarry Smith $ Create Jacobian matrix 3673be6bf707SBarry Smith $ Set linear terms into matrix 3674be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 3675be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 3676be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 3677512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3678be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3679be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 3680be6bf707SBarry Smith $ In your Jacobian routine 3681be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3682be6bf707SBarry Smith $ Set nonlinear terms in matrix 3683be6bf707SBarry Smith 3684be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 3685be6bf707SBarry Smith $ // build linear portion of Jacobian 3686512a5fc5SBarry Smith $ ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); 3687be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 3688be6bf707SBarry Smith $ loop over nonlinear iterations 3689be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 3690be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 3691be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 3692be6bf707SBarry Smith $ Solve linear system with Jacobian 3693be6bf707SBarry Smith $ endloop 3694be6bf707SBarry Smith 3695be6bf707SBarry Smith Notes: 3696be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 3697512a5fc5SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before 3698be6bf707SBarry Smith calling this routine. 3699be6bf707SBarry Smith 37000c468ba9SBarry Smith When this is called multiple times it overwrites the previous set of stored values 37010c468ba9SBarry Smith and does not allocated additional space. 37020c468ba9SBarry Smith 3703be6bf707SBarry Smith .seealso: MatRetrieveValues() 3704be6bf707SBarry Smith 3705be6bf707SBarry Smith @*/ 37067087cfbeSBarry Smith PetscErrorCode MatStoreValues(Mat mat) 3707be6bf707SBarry Smith { 37084ac538c5SBarry Smith PetscErrorCode ierr; 3709be6bf707SBarry Smith 3710be6bf707SBarry Smith PetscFunctionBegin; 37110700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3712e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3713e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 37144ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr); 3715be6bf707SBarry Smith PetscFunctionReturn(0); 3716be6bf707SBarry Smith } 3717be6bf707SBarry Smith 37187087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat) 3719be6bf707SBarry Smith { 3720be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 37216849ba73SBarry Smith PetscErrorCode ierr; 3722d0f46423SBarry Smith PetscInt nz = aij->i[mat->rmap->n]; 3723be6bf707SBarry Smith 3724be6bf707SBarry Smith PetscFunctionBegin; 3725169f6850SBarry Smith if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first"); 3726f23aa3ddSBarry Smith if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first"); 3727be6bf707SBarry Smith /* copy values over */ 3728580bdb30SBarry Smith ierr = PetscArraycpy(aij->a,aij->saved_values,nz);CHKERRQ(ierr); 3729be6bf707SBarry Smith PetscFunctionReturn(0); 3730be6bf707SBarry Smith } 3731be6bf707SBarry Smith 3732be6bf707SBarry Smith /*@ 3733be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 3734be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 3735be6bf707SBarry Smith nonlinear portion. 3736be6bf707SBarry Smith 3737be6bf707SBarry Smith Collect on Mat 3738be6bf707SBarry Smith 3739be6bf707SBarry Smith Input Parameters: 3740386f7cf9SBarry Smith . mat - the matrix (currently only AIJ matrices support this option) 3741be6bf707SBarry Smith 374215091d37SBarry Smith Level: advanced 374315091d37SBarry Smith 3744be6bf707SBarry Smith .seealso: MatStoreValues() 3745be6bf707SBarry Smith 3746be6bf707SBarry Smith @*/ 37477087cfbeSBarry Smith PetscErrorCode MatRetrieveValues(Mat mat) 3748be6bf707SBarry Smith { 37494ac538c5SBarry Smith PetscErrorCode ierr; 3750be6bf707SBarry Smith 3751be6bf707SBarry Smith PetscFunctionBegin; 37520700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 3753e32f2f54SBarry Smith if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3754e32f2f54SBarry Smith if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 37554ac538c5SBarry Smith ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr); 3756be6bf707SBarry Smith PetscFunctionReturn(0); 3757be6bf707SBarry Smith } 3758be6bf707SBarry Smith 3759f83d6046SBarry Smith 3760be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 376117ab2063SBarry Smith /*@C 3762682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 37630d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 37646e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 376551c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 37662bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 376717ab2063SBarry Smith 3768d083f849SBarry Smith Collective 3769db81eaa0SLois Curfman McInnes 377017ab2063SBarry Smith Input Parameters: 3771db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 377217ab2063SBarry Smith . m - number of rows 377317ab2063SBarry Smith . n - number of columns 377417ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 377551c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 37760298fd71SBarry Smith (possibly different for each row) or NULL 377717ab2063SBarry Smith 377817ab2063SBarry Smith Output Parameter: 3779416022c9SBarry Smith . A - the matrix 378017ab2063SBarry Smith 3781175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3782f6f02116SRichard Tran Mills MatXXXXSetPreallocation() paradigm instead of this routine directly. 3783175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3784175b88e8SBarry Smith 3785b259b22eSLois Curfman McInnes Notes: 378649a6f317SBarry Smith If nnz is given then nz is ignored 378749a6f317SBarry Smith 378817ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 378917ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 37900002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 379144cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 379217ab2063SBarry Smith 379317ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 37940298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 37953d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 37966da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 379717ab2063SBarry Smith 3798682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 37994fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 3800682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 38016c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 38026c7ebb05SLois Curfman McInnes 38036c7ebb05SLois Curfman McInnes Options Database Keys: 3804698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 38059db58ca8SBarry Smith - -mat_inode_limit <limit> - Sets inode limit (max limit=5) 380617ab2063SBarry Smith 3807027ccd11SLois Curfman McInnes Level: intermediate 3808027ccd11SLois Curfman McInnes 380969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 381036db0b34SBarry Smith 381117ab2063SBarry Smith @*/ 38127087cfbeSBarry Smith PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A) 381317ab2063SBarry Smith { 3814dfbe8321SBarry Smith PetscErrorCode ierr; 38156945ee14SBarry Smith 38163a40ed3dSBarry Smith PetscFunctionBegin; 3817f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 3818117016b1SBarry Smith ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr); 3819c4752a88SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 3820d28bb7d2SJed Brown ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr); 3821273d9f13SBarry Smith PetscFunctionReturn(0); 3822273d9f13SBarry Smith } 3823273d9f13SBarry Smith 3824273d9f13SBarry Smith /*@C 3825273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 3826273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 3827273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 3828273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 3829273d9f13SBarry Smith 3830d083f849SBarry Smith Collective 3831273d9f13SBarry Smith 3832273d9f13SBarry Smith Input Parameters: 38331c4f3114SJed Brown + B - The matrix 3834273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 3835273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 38360298fd71SBarry Smith (possibly different for each row) or NULL 3837273d9f13SBarry Smith 3838273d9f13SBarry Smith Notes: 383949a6f317SBarry Smith If nnz is given then nz is ignored 384049a6f317SBarry Smith 3841273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3842273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 3843273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 3844273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 3845273d9f13SBarry Smith 3846273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 38470298fd71SBarry Smith Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory 3848273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 3849273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 3850273d9f13SBarry Smith 3851aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3852aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3853aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3854aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3855aa95bbe8SBarry Smith 3856a96a251dSBarry Smith Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix 3857a96a251dSBarry Smith entries or columns indices 3858a96a251dSBarry Smith 3859273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 3860273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 3861273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 3862273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3863273d9f13SBarry Smith 3864273d9f13SBarry Smith Options Database Keys: 3865698d4c6aSKris Buschelman + -mat_no_inode - Do not use inodes 386647b2e64bSBarry Smith - -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3867273d9f13SBarry Smith 3868273d9f13SBarry Smith Level: intermediate 3869273d9f13SBarry Smith 387069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo() 3871273d9f13SBarry Smith 3872273d9f13SBarry Smith @*/ 38737087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[]) 3874273d9f13SBarry Smith { 38754ac538c5SBarry Smith PetscErrorCode ierr; 3876a23d5eceSKris Buschelman 3877a23d5eceSKris Buschelman PetscFunctionBegin; 38786ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 38796ba663aaSJed Brown PetscValidType(B,1); 38804ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr); 3881a23d5eceSKris Buschelman PetscFunctionReturn(0); 3882a23d5eceSKris Buschelman } 3883a23d5eceSKris Buschelman 38847087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz) 3885a23d5eceSKris Buschelman { 3886273d9f13SBarry Smith Mat_SeqAIJ *b; 38872576faa2SJed Brown PetscBool skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE; 38886849ba73SBarry Smith PetscErrorCode ierr; 388997f1f81fSBarry Smith PetscInt i; 3890273d9f13SBarry Smith 3891273d9f13SBarry Smith PetscFunctionBegin; 38922576faa2SJed Brown if (nz >= 0 || nnz) realalloc = PETSC_TRUE; 3893a96a251dSBarry Smith if (nz == MAT_SKIP_ALLOCATION) { 3894c461c341SBarry Smith skipallocation = PETSC_TRUE; 3895c461c341SBarry Smith nz = 0; 3896c461c341SBarry Smith } 389726283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 389826283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3899899cda47SBarry Smith 3900435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 390160e0710aSBarry Smith if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz); 3902071fcb05SBarry Smith #if defined(PETSC_USE_DEBUG) 3903b73539f3SBarry Smith if (nnz) { 3904d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 390560e0710aSBarry 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]); 390660e0710aSBarry 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); 3907b73539f3SBarry Smith } 3908b73539f3SBarry Smith } 3909071fcb05SBarry Smith #endif 3910b73539f3SBarry Smith 3911273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 39122205254eSKarl Rupp 3913273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 3914273d9f13SBarry Smith 3915ab93d7beSBarry Smith if (!skipallocation) { 39162ee49352SLisandro Dalcin if (!b->imax) { 3917071fcb05SBarry Smith ierr = PetscMalloc1(B->rmap->n,&b->imax);CHKERRQ(ierr); 3918071fcb05SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3919071fcb05SBarry Smith } 3920071fcb05SBarry Smith if (!b->ilen) { 3921071fcb05SBarry Smith /* b->ilen will count nonzeros in each row so far. */ 3922071fcb05SBarry Smith ierr = PetscCalloc1(B->rmap->n,&b->ilen);CHKERRQ(ierr); 3923071fcb05SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3924071fcb05SBarry Smith } else { 3925071fcb05SBarry Smith ierr = PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 39262ee49352SLisandro Dalcin } 3927846b4da1SFande Kong if (!b->ipre) { 3928846b4da1SFande Kong ierr = PetscMalloc1(B->rmap->n,&b->ipre);CHKERRQ(ierr); 3929846b4da1SFande Kong ierr = PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr); 3930846b4da1SFande Kong } 3931273d9f13SBarry Smith if (!nnz) { 3932435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 3933c62bd62aSJed Brown else if (nz < 0) nz = 1; 39345d2a9ed1SStefano Zampini nz = PetscMin(nz,B->cmap->n); 3935d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) b->imax[i] = nz; 3936d0f46423SBarry Smith nz = nz*B->rmap->n; 3937273d9f13SBarry Smith } else { 3938c73702f5SBarry Smith PetscInt64 nz64 = 0; 3939c73702f5SBarry Smith for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];} 3940c73702f5SBarry Smith ierr = PetscIntCast(nz64,&nz);CHKERRQ(ierr); 3941273d9f13SBarry Smith } 3942ab93d7beSBarry Smith 3943273d9f13SBarry Smith /* allocate the matrix space */ 394453dd7562SDmitry Karpeev /* FIXME: should B's old memory be unlogged? */ 39452ee49352SLisandro Dalcin ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr); 3946396832f4SHong Zhang if (B->structure_only) { 39475848002fSHong Zhang ierr = PetscMalloc1(nz,&b->j);CHKERRQ(ierr); 39485848002fSHong Zhang ierr = PetscMalloc1(B->rmap->n+1,&b->i);CHKERRQ(ierr); 3949396832f4SHong Zhang ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));CHKERRQ(ierr); 3950396832f4SHong Zhang } else { 3951dcca6d9dSJed Brown ierr = PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);CHKERRQ(ierr); 39523bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr); 3953396832f4SHong Zhang } 3954bfeeae90SHong Zhang b->i[0] = 0; 3955d0f46423SBarry Smith for (i=1; i<B->rmap->n+1; i++) { 39565da197adSKris Buschelman b->i[i] = b->i[i-1] + b->imax[i-1]; 39575da197adSKris Buschelman } 3958396832f4SHong Zhang if (B->structure_only) { 3959396832f4SHong Zhang b->singlemalloc = PETSC_FALSE; 3960396832f4SHong Zhang b->free_a = PETSC_FALSE; 3961396832f4SHong Zhang } else { 3962273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 3963e6b907acSBarry Smith b->free_a = PETSC_TRUE; 3964396832f4SHong Zhang } 3965e6b907acSBarry Smith b->free_ij = PETSC_TRUE; 3966c461c341SBarry Smith } else { 3967e6b907acSBarry Smith b->free_a = PETSC_FALSE; 3968e6b907acSBarry Smith b->free_ij = PETSC_FALSE; 3969c461c341SBarry Smith } 3970273d9f13SBarry Smith 3971846b4da1SFande Kong if (b->ipre && nnz != b->ipre && b->imax) { 3972846b4da1SFande Kong /* reserve user-requested sparsity */ 3973580bdb30SBarry Smith ierr = PetscArraycpy(b->ipre,b->imax,B->rmap->n);CHKERRQ(ierr); 3974846b4da1SFande Kong } 3975846b4da1SFande Kong 3976846b4da1SFande Kong 3977273d9f13SBarry Smith b->nz = 0; 3978273d9f13SBarry Smith b->maxnz = nz; 3979273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 39802205254eSKarl Rupp if (realalloc) { 39812205254eSKarl Rupp ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 39822205254eSKarl Rupp } 3983cb7b82ddSBarry Smith B->was_assembled = PETSC_FALSE; 3984cb7b82ddSBarry Smith B->assembled = PETSC_FALSE; 3985273d9f13SBarry Smith PetscFunctionReturn(0); 3986273d9f13SBarry Smith } 3987273d9f13SBarry Smith 3988846b4da1SFande Kong 3989846b4da1SFande Kong PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A) 3990846b4da1SFande Kong { 3991846b4da1SFande Kong Mat_SeqAIJ *a; 3992a5bbaf83SFande Kong PetscInt i; 3993846b4da1SFande Kong PetscErrorCode ierr; 3994846b4da1SFande Kong 3995846b4da1SFande Kong PetscFunctionBegin; 3996846b4da1SFande Kong PetscValidHeaderSpecific(A,MAT_CLASSID,1); 399714d0e64fSAlex Lindsay 399814d0e64fSAlex Lindsay /* Check local size. If zero, then return */ 399914d0e64fSAlex Lindsay if (!A->rmap->n) PetscFunctionReturn(0); 400014d0e64fSAlex Lindsay 4001846b4da1SFande Kong a = (Mat_SeqAIJ*)A->data; 40022c814fdeSFande Kong /* if no saved info, we error out */ 4003fb4dc15dSAlex Lindsay if (!a->ipre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"No saved preallocation info \n"); 40042c814fdeSFande Kong 4005fb4dc15dSAlex 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"); 40062c814fdeSFande Kong 4007580bdb30SBarry Smith ierr = PetscArraycpy(a->imax,a->ipre,A->rmap->n);CHKERRQ(ierr); 4008580bdb30SBarry Smith ierr = PetscArrayzero(a->ilen,A->rmap->n);CHKERRQ(ierr); 4009846b4da1SFande Kong a->i[0] = 0; 4010846b4da1SFande Kong for (i=1; i<A->rmap->n+1; i++) { 4011846b4da1SFande Kong a->i[i] = a->i[i-1] + a->imax[i-1]; 4012846b4da1SFande Kong } 4013846b4da1SFande Kong A->preallocated = PETSC_TRUE; 4014846b4da1SFande Kong a->nz = 0; 4015846b4da1SFande Kong a->maxnz = a->i[A->rmap->n]; 4016846b4da1SFande Kong A->info.nz_unneeded = (double)a->maxnz; 4017846b4da1SFande Kong A->was_assembled = PETSC_FALSE; 4018846b4da1SFande Kong A->assembled = PETSC_FALSE; 4019846b4da1SFande Kong PetscFunctionReturn(0); 4020846b4da1SFande Kong } 4021846b4da1SFande Kong 402258d36128SBarry Smith /*@ 4023a1661176SMatthew Knepley MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format. 4024a1661176SMatthew Knepley 4025a1661176SMatthew Knepley Input Parameters: 4026a1661176SMatthew Knepley + B - the matrix 4027a1661176SMatthew Knepley . i - the indices into j for the start of each row (starts with zero) 4028a1661176SMatthew Knepley . j - the column indices for each row (starts with zero) these must be sorted for each row 4029a1661176SMatthew Knepley - v - optional values in the matrix 4030a1661176SMatthew Knepley 4031a1661176SMatthew Knepley Level: developer 4032a1661176SMatthew Knepley 403358d36128SBarry Smith The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays() 403458d36128SBarry Smith 4035c1c1d628SHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ 4036a1661176SMatthew Knepley @*/ 4037a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[]) 4038a1661176SMatthew Knepley { 4039a1661176SMatthew Knepley PetscErrorCode ierr; 4040a1661176SMatthew Knepley 4041a1661176SMatthew Knepley PetscFunctionBegin; 40420700a824SBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,1); 40436ba663aaSJed Brown PetscValidType(B,1); 40444ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 4045a1661176SMatthew Knepley PetscFunctionReturn(0); 4046a1661176SMatthew Knepley } 4047a1661176SMatthew Knepley 40487087cfbeSBarry Smith PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 4049a1661176SMatthew Knepley { 4050a1661176SMatthew Knepley PetscInt i; 4051a1661176SMatthew Knepley PetscInt m,n; 4052a1661176SMatthew Knepley PetscInt nz; 4053a1661176SMatthew Knepley PetscInt *nnz, nz_max = 0; 4054a1661176SMatthew Knepley PetscErrorCode ierr; 4055a1661176SMatthew Knepley 4056a1661176SMatthew Knepley PetscFunctionBegin; 405765e19b50SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]); 4058779a8d59SSatish Balay 4059779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 4060779a8d59SSatish Balay ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 4061779a8d59SSatish Balay 4062779a8d59SSatish Balay ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr); 4063854ce69bSBarry Smith ierr = PetscMalloc1(m+1, &nnz);CHKERRQ(ierr); 4064a1661176SMatthew Knepley for (i = 0; i < m; i++) { 4065b7940d39SSatish Balay nz = Ii[i+1]- Ii[i]; 4066a1661176SMatthew Knepley nz_max = PetscMax(nz_max, nz); 406765e19b50SBarry Smith if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz); 4068a1661176SMatthew Knepley nnz[i] = nz; 4069a1661176SMatthew Knepley } 4070a1661176SMatthew Knepley ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr); 4071a1661176SMatthew Knepley ierr = PetscFree(nnz);CHKERRQ(ierr); 4072a1661176SMatthew Knepley 4073a1661176SMatthew Knepley for (i = 0; i < m; i++) { 4074071fcb05SBarry Smith ierr = MatSetValues_SeqAIJ(B, 1, &i, Ii[i+1] - Ii[i], J+Ii[i], v ? v + Ii[i] : NULL, INSERT_VALUES);CHKERRQ(ierr); 4075a1661176SMatthew Knepley } 4076a1661176SMatthew Knepley 4077a1661176SMatthew Knepley ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4078a1661176SMatthew Knepley ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4079a1661176SMatthew Knepley 40807827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 4081a1661176SMatthew Knepley PetscFunctionReturn(0); 4082a1661176SMatthew Knepley } 4083a1661176SMatthew Knepley 4084c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h> 4085af0996ceSBarry Smith #include <petsc/private/kernels/petscaxpy.h> 4086170fe5c8SBarry Smith 4087170fe5c8SBarry Smith /* 4088170fe5c8SBarry Smith Computes (B'*A')' since computing B*A directly is untenable 4089170fe5c8SBarry Smith 4090170fe5c8SBarry Smith n p p 4091170fe5c8SBarry Smith ( ) ( ) ( ) 4092170fe5c8SBarry Smith m ( A ) * n ( B ) = m ( C ) 4093170fe5c8SBarry Smith ( ) ( ) ( ) 4094170fe5c8SBarry Smith 4095170fe5c8SBarry Smith */ 4096170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C) 4097170fe5c8SBarry Smith { 4098170fe5c8SBarry Smith PetscErrorCode ierr; 4099170fe5c8SBarry Smith Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data; 4100170fe5c8SBarry Smith Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data; 4101170fe5c8SBarry Smith Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data; 41021de00fd4SBarry Smith PetscInt i,n,m,q,p; 4103170fe5c8SBarry Smith const PetscInt *ii,*idx; 4104170fe5c8SBarry Smith const PetscScalar *b,*a,*a_q; 4105170fe5c8SBarry Smith PetscScalar *c,*c_q; 4106170fe5c8SBarry Smith 4107170fe5c8SBarry Smith PetscFunctionBegin; 4108d0f46423SBarry Smith m = A->rmap->n; 4109d0f46423SBarry Smith n = A->cmap->n; 4110d0f46423SBarry Smith p = B->cmap->n; 4111170fe5c8SBarry Smith a = sub_a->v; 4112170fe5c8SBarry Smith b = sub_b->a; 4113170fe5c8SBarry Smith c = sub_c->v; 4114580bdb30SBarry Smith ierr = PetscArrayzero(c,m*p);CHKERRQ(ierr); 4115170fe5c8SBarry Smith 4116170fe5c8SBarry Smith ii = sub_b->i; 4117170fe5c8SBarry Smith idx = sub_b->j; 4118170fe5c8SBarry Smith for (i=0; i<n; i++) { 4119170fe5c8SBarry Smith q = ii[i+1] - ii[i]; 4120170fe5c8SBarry Smith while (q-->0) { 4121170fe5c8SBarry Smith c_q = c + m*(*idx); 4122170fe5c8SBarry Smith a_q = a + m*i; 4123854c7f52SBarry Smith PetscKernelAXPY(c_q,*b,a_q,m); 4124170fe5c8SBarry Smith idx++; 4125170fe5c8SBarry Smith b++; 4126170fe5c8SBarry Smith } 4127170fe5c8SBarry Smith } 4128170fe5c8SBarry Smith PetscFunctionReturn(0); 4129170fe5c8SBarry Smith } 4130170fe5c8SBarry Smith 41314222ddf1SHong Zhang PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat C) 4132170fe5c8SBarry Smith { 4133170fe5c8SBarry Smith PetscErrorCode ierr; 4134d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 4135170fe5c8SBarry Smith 4136170fe5c8SBarry Smith PetscFunctionBegin; 413760e0710aSBarry 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); 41384222ddf1SHong Zhang ierr = MatSetSizes(C,m,n,m,n);CHKERRQ(ierr); 41394222ddf1SHong Zhang ierr = MatSetBlockSizesFromMats(C,A,B);CHKERRQ(ierr); 41404222ddf1SHong Zhang ierr = MatSetType(C,MATSEQDENSE);CHKERRQ(ierr); 41414222ddf1SHong Zhang ierr = MatSeqDenseSetPreallocation(C,NULL);CHKERRQ(ierr); 4142d73949e8SHong Zhang 41434222ddf1SHong Zhang C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ; 4144170fe5c8SBarry Smith PetscFunctionReturn(0); 4145170fe5c8SBarry Smith } 4146170fe5c8SBarry Smith 4147170fe5c8SBarry Smith /* ----------------------------------------------------------------*/ 41480bad9183SKris Buschelman /*MC 4149fafad747SKris Buschelman MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices, 41500bad9183SKris Buschelman based on compressed sparse row format. 41510bad9183SKris Buschelman 41520bad9183SKris Buschelman Options Database Keys: 41530bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions() 41540bad9183SKris Buschelman 41550bad9183SKris Buschelman Level: beginner 41560bad9183SKris Buschelman 41570cd7f59aSBarry Smith Notes: 41580cd7f59aSBarry Smith MatSetValues() may be called for this matrix type with a NULL argument for the numerical values, 41590cd7f59aSBarry Smith in this case the values associated with the rows and columns one passes in are set to zero 41600cd7f59aSBarry Smith in the matrix 41610cd7f59aSBarry Smith 41620cd7f59aSBarry Smith MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no 41630cd7f59aSBarry Smith space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored 41640cd7f59aSBarry Smith 41650cd7f59aSBarry Smith Developer Notes: 41660cd7f59aSBarry Smith It would be nice if all matrix formats supported passing NULL in for the numerical values 41670cd7f59aSBarry Smith 4168f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType 41690bad9183SKris Buschelman M*/ 41700bad9183SKris Buschelman 4171ccd284c7SBarry Smith /*MC 4172ccd284c7SBarry Smith MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 4173ccd284c7SBarry Smith 4174ccd284c7SBarry Smith This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 4175ccd284c7SBarry Smith and MATMPIAIJ otherwise. As a result, for single process communicators, 41760cd7f59aSBarry Smith MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported 4177ccd284c7SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 4178ccd284c7SBarry Smith the above preallocation routines for simplicity. 4179ccd284c7SBarry Smith 4180ccd284c7SBarry Smith Options Database Keys: 4181ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 4182ccd284c7SBarry Smith 418395452b02SPatrick Sanan Developer Notes: 4184ca9cdca7SRichard Tran Mills Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when 4185ccd284c7SBarry Smith enough exist. 4186ccd284c7SBarry Smith 4187ccd284c7SBarry Smith Level: beginner 4188ccd284c7SBarry Smith 4189ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ 4190ccd284c7SBarry Smith M*/ 4191ccd284c7SBarry Smith 4192ccd284c7SBarry Smith /*MC 4193ccd284c7SBarry Smith MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices. 4194ccd284c7SBarry Smith 4195ccd284c7SBarry Smith This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator, 4196ccd284c7SBarry Smith and MATMPIAIJCRL otherwise. As a result, for single process communicators, 4197ccd284c7SBarry Smith MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported 4198ccd284c7SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 4199ccd284c7SBarry Smith the above preallocation routines for simplicity. 4200ccd284c7SBarry Smith 4201ccd284c7SBarry Smith Options Database Keys: 4202ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions() 4203ccd284c7SBarry Smith 4204ccd284c7SBarry Smith Level: beginner 4205ccd284c7SBarry Smith 4206ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL 4207ccd284c7SBarry Smith M*/ 4208ccd284c7SBarry Smith 42097906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*); 42107906f579SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 42117906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*); 42127906f579SHong Zhang #endif 42137906f579SHong Zhang #if defined(PETSC_HAVE_HYPRE) 42147906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*); 42157906f579SHong Zhang #endif 42167906f579SHong Zhang PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*); 42177906f579SHong Zhang 4218d4002b98SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*); 4219c9225affSStefano Zampini PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*); 42204222ddf1SHong Zhang PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat); 42217906f579SHong Zhang 42228c778c55SBarry Smith /*@C 42238f1ea47aSStefano Zampini MatSeqAIJGetArray - gives read/write access to the array where the data for a MATSEQAIJ matrix is stored 42248c778c55SBarry Smith 42258c778c55SBarry Smith Not Collective 42268c778c55SBarry Smith 42278c778c55SBarry Smith Input Parameter: 4228579dbff0SBarry Smith . mat - a MATSEQAIJ matrix 42298c778c55SBarry Smith 42308c778c55SBarry Smith Output Parameter: 42318c778c55SBarry Smith . array - pointer to the data 42328c778c55SBarry Smith 42338c778c55SBarry Smith Level: intermediate 42348c778c55SBarry Smith 4235774cf152SJed Brown .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90() 42368c778c55SBarry Smith @*/ 42378c778c55SBarry Smith PetscErrorCode MatSeqAIJGetArray(Mat A,PetscScalar **array) 42388c778c55SBarry Smith { 42398c778c55SBarry Smith PetscErrorCode ierr; 42408c778c55SBarry Smith 42418c778c55SBarry Smith PetscFunctionBegin; 42428c778c55SBarry Smith ierr = PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr); 42438c778c55SBarry Smith PetscFunctionReturn(0); 42448c778c55SBarry Smith } 42458c778c55SBarry Smith 424621e72a00SBarry Smith /*@C 42478f1ea47aSStefano Zampini MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a MATSEQAIJ matrix is stored 42488f1ea47aSStefano Zampini 42498f1ea47aSStefano Zampini Not Collective 42508f1ea47aSStefano Zampini 42518f1ea47aSStefano Zampini Input Parameter: 42528f1ea47aSStefano Zampini . mat - a MATSEQAIJ matrix 42538f1ea47aSStefano Zampini 42548f1ea47aSStefano Zampini Output Parameter: 42558f1ea47aSStefano Zampini . array - pointer to the data 42568f1ea47aSStefano Zampini 42578f1ea47aSStefano Zampini Level: intermediate 42588f1ea47aSStefano Zampini 42598f1ea47aSStefano Zampini .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayRead() 42608f1ea47aSStefano Zampini @*/ 42618f1ea47aSStefano Zampini PetscErrorCode MatSeqAIJGetArrayRead(Mat A,const PetscScalar **array) 42628f1ea47aSStefano Zampini { 42638f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4264c70f7ee4SJunchao Zhang PetscOffloadMask oval; 42658f1ea47aSStefano Zampini #endif 42668f1ea47aSStefano Zampini PetscErrorCode ierr; 42678f1ea47aSStefano Zampini 42688f1ea47aSStefano Zampini PetscFunctionBegin; 42698f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4270c70f7ee4SJunchao Zhang oval = A->offloadmask; 42718f1ea47aSStefano Zampini #endif 42728f1ea47aSStefano Zampini ierr = MatSeqAIJGetArray(A,(PetscScalar**)array);CHKERRQ(ierr); 42738f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4274c70f7ee4SJunchao Zhang if (oval == PETSC_OFFLOAD_GPU || oval == PETSC_OFFLOAD_BOTH) A->offloadmask = PETSC_OFFLOAD_BOTH; 42758f1ea47aSStefano Zampini #endif 42768f1ea47aSStefano Zampini PetscFunctionReturn(0); 42778f1ea47aSStefano Zampini } 42788f1ea47aSStefano Zampini 42798f1ea47aSStefano Zampini /*@C 42808f1ea47aSStefano Zampini MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from MatSeqAIJGetArrayRead 42818f1ea47aSStefano Zampini 42828f1ea47aSStefano Zampini Not Collective 42838f1ea47aSStefano Zampini 42848f1ea47aSStefano Zampini Input Parameter: 42858f1ea47aSStefano Zampini . mat - a MATSEQAIJ matrix 42868f1ea47aSStefano Zampini 42878f1ea47aSStefano Zampini Output Parameter: 42888f1ea47aSStefano Zampini . array - pointer to the data 42898f1ea47aSStefano Zampini 42908f1ea47aSStefano Zampini Level: intermediate 42918f1ea47aSStefano Zampini 42928f1ea47aSStefano Zampini .seealso: MatSeqAIJGetArray(), MatSeqAIJGetArrayRead() 42938f1ea47aSStefano Zampini @*/ 42948f1ea47aSStefano Zampini PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A,const PetscScalar **array) 42958f1ea47aSStefano Zampini { 42968f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4297c70f7ee4SJunchao Zhang PetscOffloadMask oval; 42988f1ea47aSStefano Zampini #endif 42998f1ea47aSStefano Zampini PetscErrorCode ierr; 43008f1ea47aSStefano Zampini 43018f1ea47aSStefano Zampini PetscFunctionBegin; 43028f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4303c70f7ee4SJunchao Zhang oval = A->offloadmask; 43048f1ea47aSStefano Zampini #endif 43058f1ea47aSStefano Zampini ierr = MatSeqAIJRestoreArray(A,(PetscScalar**)array);CHKERRQ(ierr); 43068f1ea47aSStefano Zampini #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL) 4307c70f7ee4SJunchao Zhang A->offloadmask = oval; 43088f1ea47aSStefano Zampini #endif 43098f1ea47aSStefano Zampini PetscFunctionReturn(0); 43108f1ea47aSStefano Zampini } 43118f1ea47aSStefano Zampini 43128f1ea47aSStefano Zampini /*@C 431321e72a00SBarry Smith MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row 431421e72a00SBarry Smith 431521e72a00SBarry Smith Not Collective 431621e72a00SBarry Smith 431721e72a00SBarry Smith Input Parameter: 4318579dbff0SBarry Smith . mat - a MATSEQAIJ matrix 431921e72a00SBarry Smith 432021e72a00SBarry Smith Output Parameter: 432121e72a00SBarry Smith . nz - the maximum number of nonzeros in any row 432221e72a00SBarry Smith 432321e72a00SBarry Smith Level: intermediate 432421e72a00SBarry Smith 432521e72a00SBarry Smith .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90() 432621e72a00SBarry Smith @*/ 432721e72a00SBarry Smith PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz) 432821e72a00SBarry Smith { 432921e72a00SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data; 433021e72a00SBarry Smith 433121e72a00SBarry Smith PetscFunctionBegin; 433221e72a00SBarry Smith *nz = aij->rmax; 433321e72a00SBarry Smith PetscFunctionReturn(0); 433421e72a00SBarry Smith } 433521e72a00SBarry Smith 43368c778c55SBarry Smith /*@C 4337579dbff0SBarry Smith MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray() 43388c778c55SBarry Smith 43398c778c55SBarry Smith Not Collective 43408c778c55SBarry Smith 43418c778c55SBarry Smith Input Parameters: 4342a2b725a8SWilliam Gropp + mat - a MATSEQAIJ matrix 4343a2b725a8SWilliam Gropp - array - pointer to the data 43448c778c55SBarry Smith 43458c778c55SBarry Smith Level: intermediate 43468c778c55SBarry Smith 4347774cf152SJed Brown .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90() 43488c778c55SBarry Smith @*/ 43498c778c55SBarry Smith PetscErrorCode MatSeqAIJRestoreArray(Mat A,PetscScalar **array) 43508c778c55SBarry Smith { 43518c778c55SBarry Smith PetscErrorCode ierr; 43528c778c55SBarry Smith 43538c778c55SBarry Smith PetscFunctionBegin; 43548c778c55SBarry Smith ierr = PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));CHKERRQ(ierr); 43558c778c55SBarry Smith PetscFunctionReturn(0); 43568c778c55SBarry Smith } 43578c778c55SBarry Smith 435834b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA) 435902fe1965SBarry Smith PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat); 436002fe1965SBarry Smith #endif 436102fe1965SBarry Smith 43628cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B) 4363273d9f13SBarry Smith { 4364273d9f13SBarry Smith Mat_SeqAIJ *b; 4365dfbe8321SBarry Smith PetscErrorCode ierr; 436638baddfdSBarry Smith PetscMPIInt size; 4367273d9f13SBarry Smith 4368273d9f13SBarry Smith PetscFunctionBegin; 4369ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr); 4370e32f2f54SBarry Smith if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 4371273d9f13SBarry Smith 4372b00a9115SJed Brown ierr = PetscNewLog(B,&b);CHKERRQ(ierr); 43732205254eSKarl Rupp 4374b0a32e0cSBarry Smith B->data = (void*)b; 43752205254eSKarl Rupp 4376549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 4377071fcb05SBarry Smith if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull; 43782205254eSKarl Rupp 4379416022c9SBarry Smith b->row = 0; 4380416022c9SBarry Smith b->col = 0; 438182bf6240SBarry Smith b->icol = 0; 4382b810aeb4SBarry Smith b->reallocs = 0; 438336db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 4384f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 4385416022c9SBarry Smith b->nonew = 0; 4386416022c9SBarry Smith b->diag = 0; 4387416022c9SBarry Smith b->solve_work = 0; 43882a1b7f2aSHong Zhang B->spptr = 0; 4389be6bf707SBarry Smith b->saved_values = 0; 4390d7f994e1SBarry Smith b->idiag = 0; 439171f1c65dSBarry Smith b->mdiag = 0; 439271f1c65dSBarry Smith b->ssor_work = 0; 439371f1c65dSBarry Smith b->omega = 1.0; 439471f1c65dSBarry Smith b->fshift = 0.0; 439571f1c65dSBarry Smith b->idiagvalid = PETSC_FALSE; 4396bbead8a2SBarry Smith b->ibdiagvalid = PETSC_FALSE; 4397a9817697SBarry Smith b->keepnonzeropattern = PETSC_FALSE; 439817ab2063SBarry Smith 439935d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 4400bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);CHKERRQ(ierr); 4401bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);CHKERRQ(ierr); 44028c778c55SBarry Smith 4403b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4404bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr); 4405bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr); 4406b3866ffcSBarry Smith #endif 440717f1a0eaSHong Zhang 4408bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 4409bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);CHKERRQ(ierr); 4410bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 4411bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr); 4412bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr); 4413bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr); 44144dfdc2d9SRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr); 44159779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE) 44164a2a386eSRichard Tran Mills ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr); 4417191b95cbSRichard Tran Mills #endif 441834b5b067SBarry Smith #if defined(PETSC_HAVE_CUDA) 441902fe1965SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);CHKERRQ(ierr); 44204222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijcusparse_seqaij_C",MatProductSetFromOptions_SeqAIJ);CHKERRQ(ierr); 442102fe1965SBarry Smith #endif 4422bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr); 4423af8000cdSHong Zhang #if defined(PETSC_HAVE_ELEMENTAL) 4424af8000cdSHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);CHKERRQ(ierr); 4425af8000cdSHong Zhang #endif 442663c07aadSStefano Zampini #if defined(PETSC_HAVE_HYPRE) 442763c07aadSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);CHKERRQ(ierr); 44284222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",MatProductSetFromOptions_Transpose_AIJ_AIJ);CHKERRQ(ierr); 442963c07aadSStefano Zampini #endif 4430b49cda9fSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);CHKERRQ(ierr); 4431d4002b98SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);CHKERRQ(ierr); 4432c9225affSStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);CHKERRQ(ierr); 4433bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 4434bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);CHKERRQ(ierr); 4435bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr); 4436846b4da1SFande Kong ierr = PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);CHKERRQ(ierr); 4437bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr); 4438bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr); 4439bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr); 4440bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr); 44414222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_is_seqaij_C",MatProductSetFromOptions_IS_XAIJ);CHKERRQ(ierr); 44424222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqdense_seqaij_C",MatProductSetFromOptions_SeqDense_SeqAIJ);CHKERRQ(ierr); 44434222ddf1SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaij_seqaij_C",MatProductSetFromOptions_SeqAIJ);CHKERRQ(ierr); 44444108e4d5SBarry Smith ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr); 444517667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 44464099cc6bSBarry Smith ierr = MatSeqAIJSetTypeFromOptions(B);CHKERRQ(ierr); /* this allows changing the matrix subtype to say MATSEQAIJPERM */ 44473a40ed3dSBarry Smith PetscFunctionReturn(0); 444817ab2063SBarry Smith } 444917ab2063SBarry Smith 4450b24902e0SBarry Smith /* 4451b24902e0SBarry Smith Given a matrix generated with MatGetFactor() duplicates all the information in A into B 4452b24902e0SBarry Smith */ 4453ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace) 445417ab2063SBarry Smith { 4455416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 44566849ba73SBarry Smith PetscErrorCode ierr; 4457071fcb05SBarry Smith PetscInt m = A->rmap->n,i; 445817ab2063SBarry Smith 44593a40ed3dSBarry Smith PetscFunctionBegin; 4460273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 4461273d9f13SBarry Smith 4462d5f3da31SBarry Smith C->factortype = A->factortype; 4463416022c9SBarry Smith c->row = 0; 4464416022c9SBarry Smith c->col = 0; 446582bf6240SBarry Smith c->icol = 0; 44666ad4291fSHong Zhang c->reallocs = 0; 446717ab2063SBarry Smith 44686ad4291fSHong Zhang C->assembled = PETSC_TRUE; 446917ab2063SBarry Smith 4470aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr); 4471aa5ea44dSBarry Smith ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr); 4472eec197d1SBarry Smith 4473071fcb05SBarry Smith ierr = PetscMalloc1(m,&c->imax);CHKERRQ(ierr); 4474071fcb05SBarry Smith ierr = PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));CHKERRQ(ierr); 4475071fcb05SBarry Smith ierr = PetscMalloc1(m,&c->ilen);CHKERRQ(ierr); 4476071fcb05SBarry Smith ierr = PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));CHKERRQ(ierr); 44773bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));CHKERRQ(ierr); 447817ab2063SBarry Smith 447917ab2063SBarry Smith /* allocate the matrix space */ 4480f77e22a1SHong Zhang if (mallocmatspace) { 4481dcca6d9dSJed Brown ierr = PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);CHKERRQ(ierr); 44823bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 44832205254eSKarl Rupp 4484f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 44852205254eSKarl Rupp 4486580bdb30SBarry Smith ierr = PetscArraycpy(c->i,a->i,m+1);CHKERRQ(ierr); 448717ab2063SBarry Smith if (m > 0) { 4488580bdb30SBarry Smith ierr = PetscArraycpy(c->j,a->j,a->i[m]);CHKERRQ(ierr); 4489be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 4490580bdb30SBarry Smith ierr = PetscArraycpy(c->a,a->a,a->i[m]);CHKERRQ(ierr); 4491be6bf707SBarry Smith } else { 4492580bdb30SBarry Smith ierr = PetscArrayzero(c->a,a->i[m]);CHKERRQ(ierr); 449317ab2063SBarry Smith } 449408480c60SBarry Smith } 4495f77e22a1SHong Zhang } 449617ab2063SBarry Smith 44976ad4291fSHong Zhang c->ignorezeroentries = a->ignorezeroentries; 4498416022c9SBarry Smith c->roworiented = a->roworiented; 4499416022c9SBarry Smith c->nonew = a->nonew; 4500416022c9SBarry Smith if (a->diag) { 4501854ce69bSBarry Smith ierr = PetscMalloc1(m+1,&c->diag);CHKERRQ(ierr); 4502071fcb05SBarry Smith ierr = PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));CHKERRQ(ierr); 45033bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr); 4504071fcb05SBarry Smith } else c->diag = NULL; 45052205254eSKarl Rupp 45066ad4291fSHong Zhang c->solve_work = 0; 45076ad4291fSHong Zhang c->saved_values = 0; 45086ad4291fSHong Zhang c->idiag = 0; 450971f1c65dSBarry Smith c->ssor_work = 0; 4510a9817697SBarry Smith c->keepnonzeropattern = a->keepnonzeropattern; 4511e6b907acSBarry Smith c->free_a = PETSC_TRUE; 4512e6b907acSBarry Smith c->free_ij = PETSC_TRUE; 45136ad4291fSHong Zhang 4514893ad86cSHong Zhang c->rmax = a->rmax; 4515416022c9SBarry Smith c->nz = a->nz; 45168ed568f8SMatthew G Knepley c->maxnz = a->nz; /* Since we allocate exactly the right amount */ 4517273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 4518754ec7b1SSatish Balay 45196ad4291fSHong Zhang c->compressedrow.use = a->compressedrow.use; 45206ad4291fSHong Zhang c->compressedrow.nrows = a->compressedrow.nrows; 4521cd6b891eSBarry Smith if (a->compressedrow.use) { 45226ad4291fSHong Zhang i = a->compressedrow.nrows; 4523dcca6d9dSJed Brown ierr = PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);CHKERRQ(ierr); 4524580bdb30SBarry Smith ierr = PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);CHKERRQ(ierr); 4525580bdb30SBarry Smith ierr = PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);CHKERRQ(ierr); 452627ea64f8SHong Zhang } else { 452727ea64f8SHong Zhang c->compressedrow.use = PETSC_FALSE; 45280298fd71SBarry Smith c->compressedrow.i = NULL; 45290298fd71SBarry Smith c->compressedrow.rindex = NULL; 45306ad4291fSHong Zhang } 4531ea632784SBarry Smith c->nonzerorowcnt = a->nonzerorowcnt; 4532e56f5c9eSBarry Smith C->nonzerostate = A->nonzerostate; 45334846f1f5SKris Buschelman 45342205254eSKarl Rupp ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr); 4535140e18c1SBarry Smith ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr); 45363a40ed3dSBarry Smith PetscFunctionReturn(0); 453717ab2063SBarry Smith } 453817ab2063SBarry Smith 4539b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 4540b24902e0SBarry Smith { 4541b24902e0SBarry Smith PetscErrorCode ierr; 4542b24902e0SBarry Smith 4543b24902e0SBarry Smith PetscFunctionBegin; 4544ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr); 45454b6263acSBarry Smith ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr); 4546cfd3f464SBarry Smith if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) { 454733d57670SJed Brown ierr = MatSetBlockSizesFromMats(*B,A,A);CHKERRQ(ierr); 4548cfd3f464SBarry Smith } 4549a54f2f98SBarry Smith ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr); 4550f77e22a1SHong Zhang ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr); 4551b24902e0SBarry Smith PetscFunctionReturn(0); 4552b24902e0SBarry Smith } 4553b24902e0SBarry Smith 4554112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer) 4555fbdbba38SShri Abhyankar { 455652f91c60SVaclav Hapla PetscBool isbinary, ishdf5; 455752f91c60SVaclav Hapla PetscErrorCode ierr; 455852f91c60SVaclav Hapla 455952f91c60SVaclav Hapla PetscFunctionBegin; 456052f91c60SVaclav Hapla PetscValidHeaderSpecific(newMat,MAT_CLASSID,1); 456152f91c60SVaclav Hapla PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 4562c27b3999SVaclav Hapla /* force binary viewer to load .info file if it has not yet done so */ 4563c27b3999SVaclav Hapla ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 456452f91c60SVaclav Hapla ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 456552f91c60SVaclav Hapla ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr); 456652f91c60SVaclav Hapla if (isbinary) { 456752f91c60SVaclav Hapla ierr = MatLoad_SeqAIJ_Binary(newMat,viewer);CHKERRQ(ierr); 456852f91c60SVaclav Hapla } else if (ishdf5) { 456952f91c60SVaclav Hapla #if defined(PETSC_HAVE_HDF5) 457052f91c60SVaclav Hapla ierr = MatLoad_AIJ_HDF5(newMat,viewer);CHKERRQ(ierr); 457152f91c60SVaclav Hapla #else 457252f91c60SVaclav Hapla SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5"); 457352f91c60SVaclav Hapla #endif 457452f91c60SVaclav Hapla } else { 457552f91c60SVaclav 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); 457652f91c60SVaclav Hapla } 457752f91c60SVaclav Hapla PetscFunctionReturn(0); 457852f91c60SVaclav Hapla } 457952f91c60SVaclav Hapla 45803ea6fe3dSLisandro Dalcin PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer) 458152f91c60SVaclav Hapla { 45823ea6fe3dSLisandro Dalcin Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->data; 4583fbdbba38SShri Abhyankar PetscErrorCode ierr; 45843ea6fe3dSLisandro Dalcin PetscInt header[4],*rowlens,M,N,nz,sum,rows,cols,i; 4585fbdbba38SShri Abhyankar 4586fbdbba38SShri Abhyankar PetscFunctionBegin; 45873ea6fe3dSLisandro Dalcin ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 4588bbead8a2SBarry Smith 45893ea6fe3dSLisandro Dalcin /* read in matrix header */ 45903ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);CHKERRQ(ierr); 45913ea6fe3dSLisandro Dalcin if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file"); 4592fbdbba38SShri Abhyankar M = header[1]; N = header[2]; nz = header[3]; 45933ea6fe3dSLisandro Dalcin if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M); 45943ea6fe3dSLisandro Dalcin if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N); 4595bbead8a2SBarry Smith if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as SeqAIJ"); 4596fbdbba38SShri Abhyankar 45973ea6fe3dSLisandro Dalcin /* set block sizes from the viewer's .info file */ 45983ea6fe3dSLisandro Dalcin ierr = MatLoad_Binary_BlockSizes(mat,viewer);CHKERRQ(ierr); 45993ea6fe3dSLisandro Dalcin /* set local and global sizes if not set already */ 46003ea6fe3dSLisandro Dalcin if (mat->rmap->n < 0) mat->rmap->n = M; 46013ea6fe3dSLisandro Dalcin if (mat->cmap->n < 0) mat->cmap->n = N; 46023ea6fe3dSLisandro Dalcin if (mat->rmap->N < 0) mat->rmap->N = M; 46033ea6fe3dSLisandro Dalcin if (mat->cmap->N < 0) mat->cmap->N = N; 46043ea6fe3dSLisandro Dalcin ierr = PetscLayoutSetUp(mat->rmap);CHKERRQ(ierr); 46053ea6fe3dSLisandro Dalcin ierr = PetscLayoutSetUp(mat->cmap);CHKERRQ(ierr); 46063ea6fe3dSLisandro Dalcin 46073ea6fe3dSLisandro Dalcin /* check if the matrix sizes are correct */ 46083ea6fe3dSLisandro Dalcin ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 46093ea6fe3dSLisandro Dalcin if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols); 46103ea6fe3dSLisandro Dalcin 4611fbdbba38SShri Abhyankar /* read in row lengths */ 46123ea6fe3dSLisandro Dalcin ierr = PetscMalloc1(M,&rowlens);CHKERRQ(ierr); 46133ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryRead(viewer,rowlens,M,NULL,PETSC_INT);CHKERRQ(ierr); 46143ea6fe3dSLisandro Dalcin /* check if sum(rowlens) is same as nz */ 46153ea6fe3dSLisandro Dalcin sum = 0; for (i=0; i<M; i++) sum += rowlens[i]; 46163ea6fe3dSLisandro Dalcin if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent matrix data in file: nonzeros = %D, sum-row-lengths = %D\n",nz,sum); 46173ea6fe3dSLisandro Dalcin /* preallocate and check sizes */ 46183ea6fe3dSLisandro Dalcin ierr = MatSeqAIJSetPreallocation_SeqAIJ(mat,0,rowlens);CHKERRQ(ierr); 46193ea6fe3dSLisandro Dalcin ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 462060e0710aSBarry 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); 46213ea6fe3dSLisandro Dalcin /* store row lengths */ 46223ea6fe3dSLisandro Dalcin ierr = PetscArraycpy(a->ilen,rowlens,M);CHKERRQ(ierr); 46233ea6fe3dSLisandro Dalcin ierr = PetscFree(rowlens);CHKERRQ(ierr); 4624fbdbba38SShri Abhyankar 46253ea6fe3dSLisandro Dalcin /* fill in "i" row pointers */ 46263ea6fe3dSLisandro Dalcin a->i[0] = 0; for (i=0; i<M; i++) a->i[i+1] = a->i[i] + a->ilen[i]; 46273ea6fe3dSLisandro Dalcin /* read in "j" column indices */ 46283ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryRead(viewer,a->j,nz,NULL,PETSC_INT);CHKERRQ(ierr); 46293ea6fe3dSLisandro Dalcin /* read in "a" nonzero values */ 46303ea6fe3dSLisandro Dalcin ierr = PetscViewerBinaryRead(viewer,a->a,nz,NULL,PETSC_SCALAR);CHKERRQ(ierr); 4631fbdbba38SShri Abhyankar 46323ea6fe3dSLisandro Dalcin ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 46333ea6fe3dSLisandro Dalcin ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4634fbdbba38SShri Abhyankar PetscFunctionReturn(0); 4635fbdbba38SShri Abhyankar } 4636fbdbba38SShri Abhyankar 4637ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg) 46387264ac53SSatish Balay { 46397264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data; 4640dfbe8321SBarry Smith PetscErrorCode ierr; 4641eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 4642eeffb40dSHong Zhang PetscInt k; 4643eeffb40dSHong Zhang #endif 46447264ac53SSatish Balay 46453a40ed3dSBarry Smith PetscFunctionBegin; 4646bfeeae90SHong Zhang /* If the matrix dimensions are not equal,or no of nonzeros */ 4647d0f46423SBarry Smith if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) { 4648ca44d042SBarry Smith *flg = PETSC_FALSE; 4649ca44d042SBarry Smith PetscFunctionReturn(0); 4650bcd2baecSBarry Smith } 46517264ac53SSatish Balay 46527264ac53SSatish Balay /* if the a->i are the same */ 4653580bdb30SBarry Smith ierr = PetscArraycmp(a->i,b->i,A->rmap->n+1,flg);CHKERRQ(ierr); 4654abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 46557264ac53SSatish Balay 46567264ac53SSatish Balay /* if a->j are the same */ 4657580bdb30SBarry Smith ierr = PetscArraycmp(a->j,b->j,a->nz,flg);CHKERRQ(ierr); 4658abc0a331SBarry Smith if (!*flg) PetscFunctionReturn(0); 4659bcd2baecSBarry Smith 4660bcd2baecSBarry Smith /* if a->a are the same */ 4661eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX) 4662eeffb40dSHong Zhang for (k=0; k<a->nz; k++) { 4663eeffb40dSHong Zhang if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) { 4664eeffb40dSHong Zhang *flg = PETSC_FALSE; 46653a40ed3dSBarry Smith PetscFunctionReturn(0); 4666eeffb40dSHong Zhang } 4667eeffb40dSHong Zhang } 4668eeffb40dSHong Zhang #else 4669580bdb30SBarry Smith ierr = PetscArraycmp(a->a,b->a,a->nz,flg);CHKERRQ(ierr); 4670eeffb40dSHong Zhang #endif 4671eeffb40dSHong Zhang PetscFunctionReturn(0); 46727264ac53SSatish Balay } 467336db0b34SBarry Smith 467405869f15SSatish Balay /*@ 467536db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 467636db0b34SBarry Smith provided by the user. 467736db0b34SBarry Smith 4678d083f849SBarry Smith Collective 467936db0b34SBarry Smith 468036db0b34SBarry Smith Input Parameters: 468136db0b34SBarry Smith + comm - must be an MPI communicator of size 1 468236db0b34SBarry Smith . m - number of rows 468336db0b34SBarry Smith . n - number of columns 4684483a2f95SBarry Smith . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix 468536db0b34SBarry Smith . j - column indices 468636db0b34SBarry Smith - a - matrix values 468736db0b34SBarry Smith 468836db0b34SBarry Smith Output Parameter: 468936db0b34SBarry Smith . mat - the matrix 469036db0b34SBarry Smith 469136db0b34SBarry Smith Level: intermediate 469236db0b34SBarry Smith 469336db0b34SBarry Smith Notes: 46940551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 4695292fb18eSBarry Smith once the matrix is destroyed and not before 469636db0b34SBarry Smith 469736db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 469836db0b34SBarry Smith 4699bfeeae90SHong Zhang The i and j indices are 0 based 470036db0b34SBarry Smith 4701a4552177SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 4702a4552177SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 47038eef79e4SBarry Smith as shown 4704a4552177SSatish Balay 47058eef79e4SBarry Smith $ 1 0 0 47068eef79e4SBarry Smith $ 2 0 3 47078eef79e4SBarry Smith $ 4 5 6 47088eef79e4SBarry Smith $ 47098eef79e4SBarry Smith $ i = {0,1,3,6} [size = nrow+1 = 3+1] 47108eef79e4SBarry Smith $ j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row 47118eef79e4SBarry Smith $ v = {1,2,3,4,5,6} [size = 6] 4712a4552177SSatish Balay 47139985e31cSBarry Smith 471469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 471536db0b34SBarry Smith 471636db0b34SBarry Smith @*/ 4717c3c607ccSBarry Smith PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat) 471836db0b34SBarry Smith { 4719dfbe8321SBarry Smith PetscErrorCode ierr; 4720cbcfb4deSHong Zhang PetscInt ii; 472136db0b34SBarry Smith Mat_SeqAIJ *aij; 4722cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG) 4723cbcfb4deSHong Zhang PetscInt jj; 4724cbcfb4deSHong Zhang #endif 472536db0b34SBarry Smith 472636db0b34SBarry Smith PetscFunctionBegin; 472741096f02SStefano Zampini if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4728f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4729f69a0ea3SMatthew Knepley ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 4730a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */ 4731ab93d7beSBarry Smith ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 4732ab93d7beSBarry Smith ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr); 4733ab93d7beSBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 4734071fcb05SBarry Smith ierr = PetscMalloc1(m,&aij->imax);CHKERRQ(ierr); 4735071fcb05SBarry Smith ierr = PetscMalloc1(m,&aij->ilen);CHKERRQ(ierr); 4736ab93d7beSBarry Smith 473736db0b34SBarry Smith aij->i = i; 473836db0b34SBarry Smith aij->j = j; 473936db0b34SBarry Smith aij->a = a; 474036db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 474136db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 4742e6b907acSBarry Smith aij->free_a = PETSC_FALSE; 4743e6b907acSBarry Smith aij->free_ij = PETSC_FALSE; 474436db0b34SBarry Smith 474536db0b34SBarry Smith for (ii=0; ii<m; ii++) { 474636db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 47472515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 474860e0710aSBarry 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]); 47499985e31cSBarry Smith for (jj=i[ii]+1; jj<i[ii+1]; jj++) { 4750a061629eSStefano 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); 4751a061629eSStefano 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); 47529985e31cSBarry Smith } 475336db0b34SBarry Smith #endif 475436db0b34SBarry Smith } 47552515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 475636db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 475760e0710aSBarry Smith if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]); 475860e0710aSBarry 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]); 475936db0b34SBarry Smith } 476036db0b34SBarry Smith #endif 476136db0b34SBarry Smith 4762b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4763b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 476436db0b34SBarry Smith PetscFunctionReturn(0); 476536db0b34SBarry Smith } 476680ef6e79SMatthew G Knepley /*@C 4767d021a1c5SVictor Minden MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format) 47688a0b0e6bSVictor Minden provided by the user. 47698a0b0e6bSVictor Minden 4770d083f849SBarry Smith Collective 47718a0b0e6bSVictor Minden 47728a0b0e6bSVictor Minden Input Parameters: 47738a0b0e6bSVictor Minden + comm - must be an MPI communicator of size 1 47748a0b0e6bSVictor Minden . m - number of rows 47758a0b0e6bSVictor Minden . n - number of columns 47768a0b0e6bSVictor Minden . i - row indices 47778a0b0e6bSVictor Minden . j - column indices 47781230e6d1SVictor Minden . a - matrix values 47791230e6d1SVictor Minden . nz - number of nonzeros 47801230e6d1SVictor Minden - idx - 0 or 1 based 47818a0b0e6bSVictor Minden 47828a0b0e6bSVictor Minden Output Parameter: 47838a0b0e6bSVictor Minden . mat - the matrix 47848a0b0e6bSVictor Minden 47858a0b0e6bSVictor Minden Level: intermediate 47868a0b0e6bSVictor Minden 47878a0b0e6bSVictor Minden Notes: 47888a0b0e6bSVictor Minden The i and j indices are 0 based 47898a0b0e6bSVictor Minden 47908a0b0e6bSVictor Minden The format which is used for the sparse matrix input, is equivalent to a 47918a0b0e6bSVictor Minden row-major ordering.. i.e for the following matrix, the input data expected is 47928a0b0e6bSVictor Minden as shown: 47938a0b0e6bSVictor Minden 47948a0b0e6bSVictor Minden 1 0 0 47958a0b0e6bSVictor Minden 2 0 3 47968a0b0e6bSVictor Minden 4 5 6 47978a0b0e6bSVictor Minden 47988a0b0e6bSVictor Minden i = {0,1,1,2,2,2} 47998a0b0e6bSVictor Minden j = {0,0,2,0,1,2} 48008a0b0e6bSVictor Minden v = {1,2,3,4,5,6} 48018a0b0e6bSVictor Minden 48028a0b0e6bSVictor Minden 480369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR() 48048a0b0e6bSVictor Minden 48058a0b0e6bSVictor Minden @*/ 4806c3c607ccSBarry Smith PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx) 48078a0b0e6bSVictor Minden { 48088a0b0e6bSVictor Minden PetscErrorCode ierr; 4809d021a1c5SVictor Minden PetscInt ii, *nnz, one = 1,row,col; 48108a0b0e6bSVictor Minden 48118a0b0e6bSVictor Minden 48128a0b0e6bSVictor Minden PetscFunctionBegin; 48131795a4d1SJed Brown ierr = PetscCalloc1(m,&nnz);CHKERRQ(ierr); 48141230e6d1SVictor Minden for (ii = 0; ii < nz; ii++) { 4815c8d679ebSHong Zhang nnz[i[ii] - !!idx] += 1; 48161230e6d1SVictor Minden } 48178a0b0e6bSVictor Minden ierr = MatCreate(comm,mat);CHKERRQ(ierr); 48188a0b0e6bSVictor Minden ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr); 48198a0b0e6bSVictor Minden ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr); 48201230e6d1SVictor Minden ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr); 48211230e6d1SVictor Minden for (ii = 0; ii < nz; ii++) { 48221230e6d1SVictor Minden if (idx) { 48231230e6d1SVictor Minden row = i[ii] - 1; 48241230e6d1SVictor Minden col = j[ii] - 1; 48251230e6d1SVictor Minden } else { 48261230e6d1SVictor Minden row = i[ii]; 48271230e6d1SVictor Minden col = j[ii]; 48288a0b0e6bSVictor Minden } 48291230e6d1SVictor Minden ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr); 48308a0b0e6bSVictor Minden } 48318a0b0e6bSVictor Minden ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 48328a0b0e6bSVictor Minden ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4833d021a1c5SVictor Minden ierr = PetscFree(nnz);CHKERRQ(ierr); 48348a0b0e6bSVictor Minden PetscFunctionReturn(0); 48358a0b0e6bSVictor Minden } 483636db0b34SBarry Smith 4837acf2f550SJed Brown PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A) 4838acf2f550SJed Brown { 4839acf2f550SJed Brown Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data; 4840acf2f550SJed Brown PetscErrorCode ierr; 4841acf2f550SJed Brown 4842acf2f550SJed Brown PetscFunctionBegin; 4843acf2f550SJed Brown a->idiagvalid = PETSC_FALSE; 4844acf2f550SJed Brown a->ibdiagvalid = PETSC_FALSE; 48452205254eSKarl Rupp 4846acf2f550SJed Brown ierr = MatSeqAIJInvalidateDiagonal_Inode(A);CHKERRQ(ierr); 4847acf2f550SJed Brown PetscFunctionReturn(0); 4848acf2f550SJed Brown } 4849acf2f550SJed Brown 48509c8f2541SHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 48519c8f2541SHong Zhang { 48529c8f2541SHong Zhang PetscErrorCode ierr; 48538761c3d6SHong Zhang PetscMPIInt size; 48549c8f2541SHong Zhang 48559c8f2541SHong Zhang PetscFunctionBegin; 48568761c3d6SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 48577bbdc51dSHong Zhang if (size == 1) { 48587bbdc51dSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 48597bbdc51dSHong Zhang ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr); 48607bbdc51dSHong Zhang } else { 48618761c3d6SHong Zhang ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 48627bbdc51dSHong Zhang } 48638761c3d6SHong Zhang } else { 48649c8f2541SHong Zhang ierr = MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr); 48658761c3d6SHong Zhang } 48669c8f2541SHong Zhang PetscFunctionReturn(0); 48679c8f2541SHong Zhang } 48689c8f2541SHong Zhang 486981824310SBarry Smith /* 487053dd7562SDmitry Karpeev Permute A into C's *local* index space using rowemb,colemb. 487153dd7562SDmitry Karpeev The embedding are supposed to be injections and the above implies that the range of rowemb is a subset 487253dd7562SDmitry Karpeev of [0,m), colemb is in [0,n). 487353dd7562SDmitry Karpeev If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A. 487453dd7562SDmitry Karpeev */ 487553dd7562SDmitry Karpeev PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B) 487653dd7562SDmitry Karpeev { 487753dd7562SDmitry Karpeev /* If making this function public, change the error returned in this function away from _PLIB. */ 487853dd7562SDmitry Karpeev PetscErrorCode ierr; 487953dd7562SDmitry Karpeev Mat_SeqAIJ *Baij; 488053dd7562SDmitry Karpeev PetscBool seqaij; 488153dd7562SDmitry Karpeev PetscInt m,n,*nz,i,j,count; 488253dd7562SDmitry Karpeev PetscScalar v; 488353dd7562SDmitry Karpeev const PetscInt *rowindices,*colindices; 488453dd7562SDmitry Karpeev 488553dd7562SDmitry Karpeev PetscFunctionBegin; 488653dd7562SDmitry Karpeev if (!B) PetscFunctionReturn(0); 488753dd7562SDmitry Karpeev /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */ 48884099cc6bSBarry Smith ierr = PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);CHKERRQ(ierr); 488953dd7562SDmitry Karpeev if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type"); 489053dd7562SDmitry Karpeev if (rowemb) { 489153dd7562SDmitry Karpeev ierr = ISGetLocalSize(rowemb,&m);CHKERRQ(ierr); 489253dd7562SDmitry 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); 489353dd7562SDmitry Karpeev } else { 48946c4ed002SBarry Smith if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix"); 489553dd7562SDmitry Karpeev } 489653dd7562SDmitry Karpeev if (colemb) { 489753dd7562SDmitry Karpeev ierr = ISGetLocalSize(colemb,&n);CHKERRQ(ierr); 489853dd7562SDmitry 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); 489953dd7562SDmitry Karpeev } else { 490053dd7562SDmitry Karpeev if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix"); 490153dd7562SDmitry Karpeev } 490253dd7562SDmitry Karpeev 490353dd7562SDmitry Karpeev Baij = (Mat_SeqAIJ*)(B->data); 490453dd7562SDmitry Karpeev if (pattern == DIFFERENT_NONZERO_PATTERN) { 490553dd7562SDmitry Karpeev ierr = PetscMalloc1(B->rmap->n,&nz);CHKERRQ(ierr); 490653dd7562SDmitry Karpeev for (i=0; i<B->rmap->n; i++) { 490753dd7562SDmitry Karpeev nz[i] = Baij->i[i+1] - Baij->i[i]; 490853dd7562SDmitry Karpeev } 490953dd7562SDmitry Karpeev ierr = MatSeqAIJSetPreallocation(C,0,nz);CHKERRQ(ierr); 491053dd7562SDmitry Karpeev ierr = PetscFree(nz);CHKERRQ(ierr); 491153dd7562SDmitry Karpeev } 491253dd7562SDmitry Karpeev if (pattern == SUBSET_NONZERO_PATTERN) { 491353dd7562SDmitry Karpeev ierr = MatZeroEntries(C);CHKERRQ(ierr); 491453dd7562SDmitry Karpeev } 491553dd7562SDmitry Karpeev count = 0; 491653dd7562SDmitry Karpeev rowindices = NULL; 491753dd7562SDmitry Karpeev colindices = NULL; 491853dd7562SDmitry Karpeev if (rowemb) { 491953dd7562SDmitry Karpeev ierr = ISGetIndices(rowemb,&rowindices);CHKERRQ(ierr); 492053dd7562SDmitry Karpeev } 492153dd7562SDmitry Karpeev if (colemb) { 492253dd7562SDmitry Karpeev ierr = ISGetIndices(colemb,&colindices);CHKERRQ(ierr); 492353dd7562SDmitry Karpeev } 492453dd7562SDmitry Karpeev for (i=0; i<B->rmap->n; i++) { 492553dd7562SDmitry Karpeev PetscInt row; 492653dd7562SDmitry Karpeev row = i; 492753dd7562SDmitry Karpeev if (rowindices) row = rowindices[i]; 492853dd7562SDmitry Karpeev for (j=Baij->i[i]; j<Baij->i[i+1]; j++) { 492953dd7562SDmitry Karpeev PetscInt col; 493053dd7562SDmitry Karpeev col = Baij->j[count]; 493153dd7562SDmitry Karpeev if (colindices) col = colindices[col]; 493253dd7562SDmitry Karpeev v = Baij->a[count]; 493353dd7562SDmitry Karpeev ierr = MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);CHKERRQ(ierr); 493453dd7562SDmitry Karpeev ++count; 493553dd7562SDmitry Karpeev } 493653dd7562SDmitry Karpeev } 493753dd7562SDmitry Karpeev /* FIXME: set C's nonzerostate correctly. */ 493853dd7562SDmitry Karpeev /* Assembly for C is necessary. */ 493953dd7562SDmitry Karpeev C->preallocated = PETSC_TRUE; 494053dd7562SDmitry Karpeev C->assembled = PETSC_TRUE; 494153dd7562SDmitry Karpeev C->was_assembled = PETSC_FALSE; 494253dd7562SDmitry Karpeev PetscFunctionReturn(0); 494353dd7562SDmitry Karpeev } 494453dd7562SDmitry Karpeev 49454099cc6bSBarry Smith PetscFunctionList MatSeqAIJList = NULL; 49464099cc6bSBarry Smith 49474099cc6bSBarry Smith /*@C 49484099cc6bSBarry Smith MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype 49494099cc6bSBarry Smith 49504099cc6bSBarry Smith Collective on Mat 49514099cc6bSBarry Smith 49524099cc6bSBarry Smith Input Parameters: 49534099cc6bSBarry Smith + mat - the matrix object 49544099cc6bSBarry Smith - matype - matrix type 49554099cc6bSBarry Smith 49564099cc6bSBarry Smith Options Database Key: 49574099cc6bSBarry Smith . -mat_seqai_type <method> - for example seqaijcrl 49584099cc6bSBarry Smith 49594099cc6bSBarry Smith 49604099cc6bSBarry Smith Level: intermediate 49614099cc6bSBarry Smith 49624099cc6bSBarry Smith .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat 49634099cc6bSBarry Smith @*/ 49644099cc6bSBarry Smith PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype) 49654099cc6bSBarry Smith { 4966fd9d3c67SJed Brown PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*); 49674099cc6bSBarry Smith PetscBool sametype; 49684099cc6bSBarry Smith 49694099cc6bSBarry Smith PetscFunctionBegin; 49704099cc6bSBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 49714099cc6bSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr); 49724099cc6bSBarry Smith if (sametype) PetscFunctionReturn(0); 49734099cc6bSBarry Smith 49744099cc6bSBarry Smith ierr = PetscFunctionListFind(MatSeqAIJList,matype,&r);CHKERRQ(ierr); 49754099cc6bSBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype); 49764099cc6bSBarry Smith ierr = (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);CHKERRQ(ierr); 49774099cc6bSBarry Smith PetscFunctionReturn(0); 49784099cc6bSBarry Smith } 49794099cc6bSBarry Smith 49804099cc6bSBarry Smith 49814099cc6bSBarry Smith /*@C 49824099cc6bSBarry Smith MatSeqAIJRegister - - Adds a new sub-matrix type for sequential AIJ matrices 49834099cc6bSBarry Smith 49844099cc6bSBarry Smith Not Collective 49854099cc6bSBarry Smith 49864099cc6bSBarry Smith Input Parameters: 49874099cc6bSBarry Smith + name - name of a new user-defined matrix type, for example MATSEQAIJCRL 49884099cc6bSBarry Smith - function - routine to convert to subtype 49894099cc6bSBarry Smith 49904099cc6bSBarry Smith Notes: 49914099cc6bSBarry Smith MatSeqAIJRegister() may be called multiple times to add several user-defined solvers. 49924099cc6bSBarry Smith 49934099cc6bSBarry Smith 49944099cc6bSBarry Smith Then, your matrix can be chosen with the procedural interface at runtime via the option 49954099cc6bSBarry Smith $ -mat_seqaij_type my_mat 49964099cc6bSBarry Smith 49974099cc6bSBarry Smith Level: advanced 49984099cc6bSBarry Smith 49994099cc6bSBarry Smith .seealso: MatSeqAIJRegisterAll() 50004099cc6bSBarry Smith 50014099cc6bSBarry Smith 50024099cc6bSBarry Smith Level: advanced 50034099cc6bSBarry Smith @*/ 5004388d47a6SSatish Balay PetscErrorCode MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *)) 50054099cc6bSBarry Smith { 50064099cc6bSBarry Smith PetscErrorCode ierr; 50074099cc6bSBarry Smith 50084099cc6bSBarry Smith PetscFunctionBegin; 50099cc31a68SJed Brown ierr = MatInitializePackage();CHKERRQ(ierr); 50104099cc6bSBarry Smith ierr = PetscFunctionListAdd(&MatSeqAIJList,sname,function);CHKERRQ(ierr); 50114099cc6bSBarry Smith PetscFunctionReturn(0); 50124099cc6bSBarry Smith } 50134099cc6bSBarry Smith 50144099cc6bSBarry Smith PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE; 50154099cc6bSBarry Smith 50164099cc6bSBarry Smith /*@C 50174099cc6bSBarry Smith MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ 50184099cc6bSBarry Smith 50194099cc6bSBarry Smith Not Collective 50204099cc6bSBarry Smith 50214099cc6bSBarry Smith Level: advanced 50224099cc6bSBarry Smith 50234099cc6bSBarry Smith Developers Note: CUSP and CUSPARSE do not yet support the MatConvert_SeqAIJ..() paradigm and thus cannot be registered here 50244099cc6bSBarry Smith 50254099cc6bSBarry Smith .seealso: MatRegisterAll(), MatSeqAIJRegister() 50264099cc6bSBarry Smith @*/ 50274099cc6bSBarry Smith PetscErrorCode MatSeqAIJRegisterAll(void) 50284099cc6bSBarry Smith { 50294099cc6bSBarry Smith PetscErrorCode ierr; 50304099cc6bSBarry Smith 50314099cc6bSBarry Smith PetscFunctionBegin; 50324099cc6bSBarry Smith if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(0); 50334099cc6bSBarry Smith MatSeqAIJRegisterAllCalled = PETSC_TRUE; 50344099cc6bSBarry Smith 50354099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr); 50364099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr); 50374dfdc2d9SRichard Tran Mills ierr = MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL);CHKERRQ(ierr); 50389779e05dSSatish Balay #if defined(PETSC_HAVE_MKL_SPARSE) 50396b62b571SRichard Tran Mills ierr = MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL);CHKERRQ(ierr); 5040485f9817SRichard Tran Mills #endif 50414099cc6bSBarry Smith #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA) 50424099cc6bSBarry Smith ierr = MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);CHKERRQ(ierr); 50434099cc6bSBarry Smith #endif 50444099cc6bSBarry Smith PetscFunctionReturn(0); 50454099cc6bSBarry Smith } 504653dd7562SDmitry Karpeev 504753dd7562SDmitry Karpeev /* 504881824310SBarry Smith Special version for direct calls from Fortran 504981824310SBarry Smith */ 5050af0996ceSBarry Smith #include <petsc/private/fortranimpl.h> 505181824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 505281824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ 505381824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 505481824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij 505581824310SBarry Smith #endif 505681824310SBarry Smith 505781824310SBarry Smith /* Change these macros so can be used in void function */ 505881824310SBarry Smith #undef CHKERRQ 5059ce94432eSBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr) 506081824310SBarry Smith #undef SETERRQ2 5061e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 50624994cf47SJed Brown #undef SETERRQ3 50634994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 506481824310SBarry Smith 506519caf8f3SSatish Balay PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr) 506681824310SBarry Smith { 506781824310SBarry Smith Mat A = *AA; 506881824310SBarry Smith PetscInt m = *mm, n = *nn; 506981824310SBarry Smith InsertMode is = *isis; 507081824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 507181824310SBarry Smith PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N; 507281824310SBarry Smith PetscInt *imax,*ai,*ailen; 507381824310SBarry Smith PetscErrorCode ierr; 507481824310SBarry Smith PetscInt *aj,nonew = a->nonew,lastcol = -1; 507554f21887SBarry Smith MatScalar *ap,value,*aa; 5076ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 5077ace3abfcSBarry Smith PetscBool roworiented = a->roworiented; 507881824310SBarry Smith 507981824310SBarry Smith PetscFunctionBegin; 50804994cf47SJed Brown MatCheckPreallocated(A,1); 508181824310SBarry Smith imax = a->imax; 508281824310SBarry Smith ai = a->i; 508381824310SBarry Smith ailen = a->ilen; 508481824310SBarry Smith aj = a->j; 508581824310SBarry Smith aa = a->a; 508681824310SBarry Smith 508781824310SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 508881824310SBarry Smith row = im[k]; 508981824310SBarry Smith if (row < 0) continue; 509081824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5091ce94432eSBarry Smith if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large"); 509281824310SBarry Smith #endif 509381824310SBarry Smith rp = aj + ai[row]; ap = aa + ai[row]; 509481824310SBarry Smith rmax = imax[row]; nrow = ailen[row]; 509581824310SBarry Smith low = 0; 509681824310SBarry Smith high = nrow; 509781824310SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 509881824310SBarry Smith if (in[l] < 0) continue; 509981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5100ce94432eSBarry Smith if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large"); 510181824310SBarry Smith #endif 510281824310SBarry Smith col = in[l]; 51032205254eSKarl Rupp if (roworiented) value = v[l + k*n]; 51042205254eSKarl Rupp else value = v[k + l*m]; 51052205254eSKarl Rupp 510681824310SBarry Smith if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue; 510781824310SBarry Smith 51082205254eSKarl Rupp if (col <= lastcol) low = 0; 51092205254eSKarl Rupp else high = nrow; 511081824310SBarry Smith lastcol = col; 511181824310SBarry Smith while (high-low > 5) { 511281824310SBarry Smith t = (low+high)/2; 511381824310SBarry Smith if (rp[t] > col) high = t; 511481824310SBarry Smith else low = t; 511581824310SBarry Smith } 511681824310SBarry Smith for (i=low; i<high; i++) { 511781824310SBarry Smith if (rp[i] > col) break; 511881824310SBarry Smith if (rp[i] == col) { 511981824310SBarry Smith if (is == ADD_VALUES) ap[i] += value; 512081824310SBarry Smith else ap[i] = value; 512181824310SBarry Smith goto noinsert; 512281824310SBarry Smith } 512381824310SBarry Smith } 512481824310SBarry Smith if (value == 0.0 && ignorezeroentries) goto noinsert; 512581824310SBarry Smith if (nonew == 1) goto noinsert; 5126ce94432eSBarry Smith if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix"); 5127fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar); 512881824310SBarry Smith N = nrow++ - 1; a->nz++; high++; 512981824310SBarry Smith /* shift up all the later entries in this row */ 513081824310SBarry Smith for (ii=N; ii>=i; ii--) { 513181824310SBarry Smith rp[ii+1] = rp[ii]; 513281824310SBarry Smith ap[ii+1] = ap[ii]; 513381824310SBarry Smith } 513481824310SBarry Smith rp[i] = col; 513581824310SBarry Smith ap[i] = value; 5136e56f5c9eSBarry Smith A->nonzerostate++; 513781824310SBarry Smith noinsert:; 513881824310SBarry Smith low = i + 1; 513981824310SBarry Smith } 514081824310SBarry Smith ailen[row] = nrow; 514181824310SBarry Smith } 514281824310SBarry Smith PetscFunctionReturnVoid(); 514381824310SBarry Smith } 5144