16f79c3a4SBarry Smith 2b45d2f2cSJed Brown #include <petsc-private/matimpl.h> /*I "petscmat.h" I*/ 36f79c3a4SBarry Smith 44a2ae208SSatish Balay #undef __FUNCT__ 54a2ae208SSatish Balay #define __FUNCT__ "MatAXPY" 606be10caSBarry Smith /*@ 721c89e3eSBarry Smith MatAXPY - Computes Y = a*X + Y. 86f79c3a4SBarry Smith 93f9fe445SBarry Smith Logically Collective on Mat 10fee21e36SBarry Smith 1198a79cdbSBarry Smith Input Parameters: 12607cd303SBarry Smith + a - the scalar multiplier 13607cd303SBarry Smith . X - the first matrix 14607cd303SBarry Smith . Y - the second matrix 15407f6b05SHong Zhang - str - either SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN 16407f6b05SHong Zhang or SUBSET_NONZERO_PATTERN (nonzeros of X is a subset of Y's) 1798a79cdbSBarry Smith 182860a424SLois Curfman McInnes Level: intermediate 192860a424SLois Curfman McInnes 209cf4f1e8SLois Curfman McInnes .keywords: matrix, add 21d4bb536fSBarry Smith 222860a424SLois Curfman McInnes .seealso: MatAYPX() 2306be10caSBarry Smith @*/ 247087cfbeSBarry Smith PetscErrorCode MatAXPY(Mat Y,PetscScalar a,Mat X,MatStructure str) 256f79c3a4SBarry Smith { 266849ba73SBarry Smith PetscErrorCode ierr; 27c1ac3661SBarry Smith PetscInt m1,m2,n1,n2; 286f79c3a4SBarry Smith 293a40ed3dSBarry Smith PetscFunctionBegin; 300700a824SBarry Smith PetscValidHeaderSpecific(X,MAT_CLASSID,3); 310700a824SBarry Smith PetscValidHeaderSpecific(Y,MAT_CLASSID,1); 32c5eb9154SBarry Smith PetscValidLogicalCollectiveScalar(Y,a,2); 33273d9f13SBarry Smith ierr = MatGetSize(X,&m1,&n1);CHKERRQ(ierr); 34273d9f13SBarry Smith ierr = MatGetSize(Y,&m2,&n2);CHKERRQ(ierr); 35e32f2f54SBarry Smith if (m1 != m2 || n1 != n2) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Non conforming matrix add: %D %D %D %D",m1,m2,n1,n2); 361987afe7SBarry Smith 37e8136da8SHong Zhang ierr = PetscLogEventBegin(MAT_AXPY,Y,0,0,0);CHKERRQ(ierr); 38f4df32b1SMatthew Knepley if (Y->ops->axpy) { 39f4df32b1SMatthew Knepley ierr = (*Y->ops->axpy)(Y,a,X,str);CHKERRQ(ierr); 40d4bb536fSBarry Smith } else { 41f4df32b1SMatthew Knepley ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 42607cd303SBarry Smith } 43e8136da8SHong Zhang ierr = PetscLogEventEnd(MAT_AXPY,Y,0,0,0);CHKERRQ(ierr); 442eea6e16SBarry Smith #if defined(PETSC_HAVE_CUSP) 452eea6e16SBarry Smith if (Y->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) { 462eea6e16SBarry Smith Y->valid_GPU_matrix = PETSC_CUSP_CPU; 472eea6e16SBarry Smith } 482eea6e16SBarry Smith #endif 49d67ff14aSKarl Rupp #if defined(PETSC_HAVE_VIENNACL) 50d67ff14aSKarl Rupp if (Y->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) { 51d67ff14aSKarl Rupp Y->valid_GPU_matrix = PETSC_VIENNACL_CPU; 52d67ff14aSKarl Rupp } 53d67ff14aSKarl Rupp #endif 54607cd303SBarry Smith PetscFunctionReturn(0); 55607cd303SBarry Smith } 56607cd303SBarry Smith 57607cd303SBarry Smith #undef __FUNCT__ 58607cd303SBarry Smith #define __FUNCT__ "MatAXPY_Basic" 59f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_Basic(Mat Y,PetscScalar a,Mat X,MatStructure str) 60607cd303SBarry Smith { 6138baddfdSBarry Smith PetscInt i,start,end,j,ncols,m,n; 626849ba73SBarry Smith PetscErrorCode ierr; 6338baddfdSBarry Smith const PetscInt *row; 64b3cc6726SBarry Smith PetscScalar *val; 65b3cc6726SBarry Smith const PetscScalar *vals; 66607cd303SBarry Smith 67607cd303SBarry Smith PetscFunctionBegin; 688dadbd76SSatish Balay ierr = MatGetSize(X,&m,&n);CHKERRQ(ierr); 6990f02eecSBarry Smith ierr = MatGetOwnershipRange(X,&start,&end);CHKERRQ(ierr); 70f4df32b1SMatthew Knepley if (a == 1.0) { 71d4bb536fSBarry Smith for (i = start; i < end; i++) { 72d4bb536fSBarry Smith ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 73d4bb536fSBarry Smith ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr); 74d4bb536fSBarry Smith ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 75d4bb536fSBarry Smith } 76d4bb536fSBarry Smith } else { 77785e854fSJed Brown ierr = PetscMalloc1((n+1),&val);CHKERRQ(ierr); 7806be10caSBarry Smith for (i=start; i<end; i++) { 79b3cc6726SBarry Smith ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 8006be10caSBarry Smith for (j=0; j<ncols; j++) { 81f4df32b1SMatthew Knepley val[j] = a*vals[j]; 826f79c3a4SBarry Smith } 83b3cc6726SBarry Smith ierr = MatSetValues(Y,1,&i,ncols,row,val,ADD_VALUES);CHKERRQ(ierr); 84b3cc6726SBarry Smith ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 856f79c3a4SBarry Smith } 86b3cc6726SBarry Smith ierr = PetscFree(val);CHKERRQ(ierr); 87d4bb536fSBarry Smith } 886d4a8577SBarry Smith ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 896d4a8577SBarry Smith ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 903a40ed3dSBarry Smith PetscFunctionReturn(0); 916f79c3a4SBarry Smith } 92052efed2SBarry Smith 934a2ae208SSatish Balay #undef __FUNCT__ 94ec7775f6SShri Abhyankar #define __FUNCT__ "MatAXPY_BasicWithPreallocation" 95ec7775f6SShri Abhyankar PetscErrorCode MatAXPY_BasicWithPreallocation(Mat B,Mat Y,PetscScalar a,Mat X,MatStructure str) 96ec7775f6SShri Abhyankar { 97ec7775f6SShri Abhyankar PetscInt i,start,end,j,ncols,m,n; 98ec7775f6SShri Abhyankar PetscErrorCode ierr; 99ec7775f6SShri Abhyankar const PetscInt *row; 100ec7775f6SShri Abhyankar PetscScalar *val; 101ec7775f6SShri Abhyankar const PetscScalar *vals; 102ec7775f6SShri Abhyankar 103ec7775f6SShri Abhyankar PetscFunctionBegin; 104ec7775f6SShri Abhyankar ierr = MatGetSize(X,&m,&n);CHKERRQ(ierr); 105ec7775f6SShri Abhyankar ierr = MatGetOwnershipRange(X,&start,&end);CHKERRQ(ierr); 106ec7775f6SShri Abhyankar if (a == 1.0) { 107ec7775f6SShri Abhyankar for (i = start; i < end; i++) { 108ec7775f6SShri Abhyankar ierr = MatGetRow(Y,i,&ncols,&row,&vals);CHKERRQ(ierr); 109ec7775f6SShri Abhyankar ierr = MatSetValues(B,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr); 110ec7775f6SShri Abhyankar ierr = MatRestoreRow(Y,i,&ncols,&row,&vals);CHKERRQ(ierr); 111ec7775f6SShri Abhyankar 112ec7775f6SShri Abhyankar ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 113ec7775f6SShri Abhyankar ierr = MatSetValues(B,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr); 114ec7775f6SShri Abhyankar ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 115ec7775f6SShri Abhyankar } 116ec7775f6SShri Abhyankar } else { 117785e854fSJed Brown ierr = PetscMalloc1((n+1),&val);CHKERRQ(ierr); 118ec7775f6SShri Abhyankar for (i=start; i<end; i++) { 119ec7775f6SShri Abhyankar ierr = MatGetRow(Y,i,&ncols,&row,&vals);CHKERRQ(ierr); 120ec7775f6SShri Abhyankar ierr = MatSetValues(B,1,&i,ncols,row,vals,ADD_VALUES);CHKERRQ(ierr); 121ec7775f6SShri Abhyankar ierr = MatRestoreRow(Y,i,&ncols,&row,&vals);CHKERRQ(ierr); 122ec7775f6SShri Abhyankar 123ec7775f6SShri Abhyankar ierr = MatGetRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 124ec7775f6SShri Abhyankar for (j=0; j<ncols; j++) { 125ec7775f6SShri Abhyankar val[j] = a*vals[j]; 126ec7775f6SShri Abhyankar } 127ec7775f6SShri Abhyankar ierr = MatSetValues(B,1,&i,ncols,row,val,ADD_VALUES);CHKERRQ(ierr); 128ec7775f6SShri Abhyankar ierr = MatRestoreRow(X,i,&ncols,&row,&vals);CHKERRQ(ierr); 129ec7775f6SShri Abhyankar } 130ec7775f6SShri Abhyankar ierr = PetscFree(val);CHKERRQ(ierr); 131ec7775f6SShri Abhyankar } 132ec7775f6SShri Abhyankar ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 133ec7775f6SShri Abhyankar ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 134ec7775f6SShri Abhyankar PetscFunctionReturn(0); 135ec7775f6SShri Abhyankar } 136ec7775f6SShri Abhyankar 137ec7775f6SShri Abhyankar #undef __FUNCT__ 1384a2ae208SSatish Balay #define __FUNCT__ "MatShift" 139052efed2SBarry Smith /*@ 14087828ca2SBarry Smith MatShift - Computes Y = Y + a I, where a is a PetscScalar and I is the identity matrix. 141052efed2SBarry Smith 1423f9fe445SBarry Smith Neighbor-wise Collective on Mat 143fee21e36SBarry Smith 14498a79cdbSBarry Smith Input Parameters: 14598a79cdbSBarry Smith + Y - the matrices 14687828ca2SBarry Smith - a - the PetscScalar 14798a79cdbSBarry Smith 1482860a424SLois Curfman McInnes Level: intermediate 1492860a424SLois Curfman McInnes 150052efed2SBarry Smith .keywords: matrix, add, shift 1516b9ee512SLois Curfman McInnes 152f56f2b3fSBarry Smith .seealso: MatDiagonalSet() 153052efed2SBarry Smith @*/ 1547087cfbeSBarry Smith PetscErrorCode MatShift(Mat Y,PetscScalar a) 155052efed2SBarry Smith { 1566849ba73SBarry Smith PetscErrorCode ierr; 15738baddfdSBarry Smith PetscInt i,start,end; 158052efed2SBarry Smith 1593a40ed3dSBarry Smith PetscFunctionBegin; 1600700a824SBarry Smith PetscValidHeaderSpecific(Y,MAT_CLASSID,1); 161ce94432eSBarry Smith if (!Y->assembled) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 162ce94432eSBarry Smith if (Y->factortype) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1634994cf47SJed Brown MatCheckPreallocated(Y,1); 164b50b34bdSBarry Smith 165f830108cSBarry Smith if (Y->ops->shift) { 166f4df32b1SMatthew Knepley ierr = (*Y->ops->shift)(Y,a);CHKERRQ(ierr); 16762d58ce1SBarry Smith } else { 168f4df32b1SMatthew Knepley PetscScalar alpha = a; 169d4bb536fSBarry Smith ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 170052efed2SBarry Smith for (i=start; i<end; i++) { 171f4df32b1SMatthew Knepley ierr = MatSetValues(Y,1,&i,1,&i,&alpha,ADD_VALUES);CHKERRQ(ierr); 172052efed2SBarry Smith } 1736d4a8577SBarry Smith ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1746d4a8577SBarry Smith ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 175052efed2SBarry Smith } 1762eea6e16SBarry Smith #if defined(PETSC_HAVE_CUSP) 1772eea6e16SBarry Smith if (Y->valid_GPU_matrix != PETSC_CUSP_UNALLOCATED) { 1782eea6e16SBarry Smith Y->valid_GPU_matrix = PETSC_CUSP_CPU; 1792eea6e16SBarry Smith } 1802eea6e16SBarry Smith #endif 181d67ff14aSKarl Rupp #if defined(PETSC_HAVE_VIENNACL) 182d67ff14aSKarl Rupp if (Y->valid_GPU_matrix != PETSC_VIENNACL_UNALLOCATED) { 183d67ff14aSKarl Rupp Y->valid_GPU_matrix = PETSC_VIENNACL_CPU; 184d67ff14aSKarl Rupp } 185d67ff14aSKarl Rupp #endif 1863a40ed3dSBarry Smith PetscFunctionReturn(0); 187052efed2SBarry Smith } 1886d84be18SBarry Smith 1894a2ae208SSatish Balay #undef __FUNCT__ 19009f38230SBarry Smith #define __FUNCT__ "MatDiagonalSet_Default" 1917087cfbeSBarry Smith PetscErrorCode MatDiagonalSet_Default(Mat Y,Vec D,InsertMode is) 19209f38230SBarry Smith { 19309f38230SBarry Smith PetscErrorCode ierr; 194*67576b8bSBarry Smith PetscInt i,start,end; 19509f38230SBarry Smith PetscScalar *v; 19609f38230SBarry Smith 19709f38230SBarry Smith PetscFunctionBegin; 19809f38230SBarry Smith ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 19909f38230SBarry Smith ierr = VecGetArray(D,&v);CHKERRQ(ierr); 20009f38230SBarry Smith for (i=start; i<end; i++) { 20109f38230SBarry Smith ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,is);CHKERRQ(ierr); 20209f38230SBarry Smith } 20309f38230SBarry Smith ierr = VecRestoreArray(D,&v);CHKERRQ(ierr); 20409f38230SBarry Smith ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20509f38230SBarry Smith ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20609f38230SBarry Smith PetscFunctionReturn(0); 20709f38230SBarry Smith } 20809f38230SBarry Smith 20909f38230SBarry Smith #undef __FUNCT__ 2104a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalSet" 2116d84be18SBarry Smith /*@ 212f56f2b3fSBarry Smith MatDiagonalSet - Computes Y = Y + D, where D is a diagonal matrix 213f56f2b3fSBarry Smith that is represented as a vector. Or Y[i,i] = D[i] if InsertMode is 214f56f2b3fSBarry Smith INSERT_VALUES. 2156d84be18SBarry Smith 2166d84be18SBarry Smith Input Parameters: 21798a79cdbSBarry Smith + Y - the input matrix 218f56f2b3fSBarry Smith . D - the diagonal matrix, represented as a vector 219f56f2b3fSBarry Smith - i - INSERT_VALUES or ADD_VALUES 2206d84be18SBarry Smith 2213f9fe445SBarry Smith Neighbor-wise Collective on Mat and Vec 222fee21e36SBarry Smith 2232860a424SLois Curfman McInnes Level: intermediate 2242860a424SLois Curfman McInnes 2256b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal 2266b9ee512SLois Curfman McInnes 2276b9ee512SLois Curfman McInnes .seealso: MatShift() 2286d84be18SBarry Smith @*/ 2297087cfbeSBarry Smith PetscErrorCode MatDiagonalSet(Mat Y,Vec D,InsertMode is) 2306d84be18SBarry Smith { 2316849ba73SBarry Smith PetscErrorCode ierr; 232*67576b8bSBarry Smith PetscInt matlocal,veclocal; 2336d84be18SBarry Smith 2343a40ed3dSBarry Smith PetscFunctionBegin; 2350700a824SBarry Smith PetscValidHeaderSpecific(Y,MAT_CLASSID,1); 2360700a824SBarry Smith PetscValidHeaderSpecific(D,VEC_CLASSID,2); 237*67576b8bSBarry Smith ierr = MatGetLocalSize(Y,&matlocal,NULL);CHKERRQ(ierr); 238*67576b8bSBarry Smith ierr = VecGetLocalSize(D,&veclocal);CHKERRQ(ierr); 239*67576b8bSBarry Smith if (matlocal != veclocal) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number local rows of matrix %D does not match that of vector for diagonal %D",matlocal,veclocal); 240f56f2b3fSBarry Smith if (Y->ops->diagonalset) { 241f56f2b3fSBarry Smith ierr = (*Y->ops->diagonalset)(Y,D,is);CHKERRQ(ierr); 24294d884c6SBarry Smith } else { 24309f38230SBarry Smith ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr); 2446d84be18SBarry Smith } 2453a40ed3dSBarry Smith PetscFunctionReturn(0); 2466d84be18SBarry Smith } 247d4bb536fSBarry Smith 2484a2ae208SSatish Balay #undef __FUNCT__ 2494a2ae208SSatish Balay #define __FUNCT__ "MatAYPX" 250d4bb536fSBarry Smith /*@ 25104aac2b0SHong Zhang MatAYPX - Computes Y = a*Y + X. 252d4bb536fSBarry Smith 2533f9fe445SBarry Smith Logically on Mat 254fee21e36SBarry Smith 25598a79cdbSBarry Smith Input Parameters: 25604aac2b0SHong Zhang + a - the PetscScalar multiplier 25704aac2b0SHong Zhang . Y - the first matrix 25804aac2b0SHong Zhang . X - the second matrix 25904aac2b0SHong Zhang - str - either SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN or SUBSET_NONZERO_PATTERN 260d4bb536fSBarry Smith 2612860a424SLois Curfman McInnes Level: intermediate 2622860a424SLois Curfman McInnes 263d4bb536fSBarry Smith .keywords: matrix, add 264d4bb536fSBarry Smith 2652860a424SLois Curfman McInnes .seealso: MatAXPY() 266d4bb536fSBarry Smith @*/ 2677087cfbeSBarry Smith PetscErrorCode MatAYPX(Mat Y,PetscScalar a,Mat X,MatStructure str) 268d4bb536fSBarry Smith { 26987828ca2SBarry Smith PetscScalar one = 1.0; 2706849ba73SBarry Smith PetscErrorCode ierr; 27138baddfdSBarry Smith PetscInt mX,mY,nX,nY; 272d4bb536fSBarry Smith 2733a40ed3dSBarry Smith PetscFunctionBegin; 274c5eb9154SBarry Smith PetscValidHeaderSpecific(X,MAT_CLASSID,3); 2750700a824SBarry Smith PetscValidHeaderSpecific(Y,MAT_CLASSID,1); 276c5eb9154SBarry Smith PetscValidLogicalCollectiveScalar(Y,a,2); 277329f5518SBarry Smith ierr = MatGetSize(X,&mX,&nX);CHKERRQ(ierr); 278329f5518SBarry Smith ierr = MatGetSize(X,&mY,&nY);CHKERRQ(ierr); 279e32f2f54SBarry Smith if (mX != mY || nX != nY) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Non conforming matrices: %D %D first %D %D second",mX,mY,nX,nY); 280d4bb536fSBarry Smith 281f4df32b1SMatthew Knepley ierr = MatScale(Y,a);CHKERRQ(ierr); 282cb9801acSJed Brown ierr = MatAXPY(Y,one,X,str);CHKERRQ(ierr); 2833a40ed3dSBarry Smith PetscFunctionReturn(0); 284d4bb536fSBarry Smith } 285b0a32e0cSBarry Smith 2864a2ae208SSatish Balay #undef __FUNCT__ 2874a2ae208SSatish Balay #define __FUNCT__ "MatComputeExplicitOperator" 288b0a32e0cSBarry Smith /*@ 289b0a32e0cSBarry Smith MatComputeExplicitOperator - Computes the explicit matrix 290b0a32e0cSBarry Smith 291b0a32e0cSBarry Smith Collective on Mat 292b0a32e0cSBarry Smith 293b0a32e0cSBarry Smith Input Parameter: 294b0a32e0cSBarry Smith . inmat - the matrix 295b0a32e0cSBarry Smith 296b0a32e0cSBarry Smith Output Parameter: 297b0a32e0cSBarry Smith . mat - the explict preconditioned operator 298b0a32e0cSBarry Smith 299b0a32e0cSBarry Smith Notes: 300b0a32e0cSBarry Smith This computation is done by applying the operators to columns of the 301b0a32e0cSBarry Smith identity matrix. 302b0a32e0cSBarry Smith 303b0a32e0cSBarry Smith Currently, this routine uses a dense matrix format when 1 processor 304b0a32e0cSBarry Smith is used and a sparse format otherwise. This routine is costly in general, 305b0a32e0cSBarry Smith and is recommended for use only with relatively small systems. 306b0a32e0cSBarry Smith 307b0a32e0cSBarry Smith Level: advanced 308b0a32e0cSBarry Smith 309b0a32e0cSBarry Smith .keywords: Mat, compute, explicit, operator 310b0a32e0cSBarry Smith @*/ 3117087cfbeSBarry Smith PetscErrorCode MatComputeExplicitOperator(Mat inmat,Mat *mat) 312b0a32e0cSBarry Smith { 313b0a32e0cSBarry Smith Vec in,out; 314dfbe8321SBarry Smith PetscErrorCode ierr; 3150b12b109SJed Brown PetscInt i,m,n,M,N,*rows,start,end; 316b0a32e0cSBarry Smith MPI_Comm comm; 31787828ca2SBarry Smith PetscScalar *array,zero = 0.0,one = 1.0; 31838baddfdSBarry Smith PetscMPIInt size; 319b0a32e0cSBarry Smith 320b0a32e0cSBarry Smith PetscFunctionBegin; 3210700a824SBarry Smith PetscValidHeaderSpecific(inmat,MAT_CLASSID,1); 3224482741eSBarry Smith PetscValidPointer(mat,2); 323b0a32e0cSBarry Smith 324ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)inmat,&comm);CHKERRQ(ierr); 325b0a32e0cSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 326b0a32e0cSBarry Smith 3270b12b109SJed Brown ierr = MatGetLocalSize(inmat,&m,&n);CHKERRQ(ierr); 3280b12b109SJed Brown ierr = MatGetSize(inmat,&M,&N);CHKERRQ(ierr); 3292a7a6963SBarry Smith ierr = MatCreateVecs(inmat,&in,&out);CHKERRQ(ierr); 3300b12b109SJed Brown ierr = VecSetOption(in,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr); 3310b12b109SJed Brown ierr = VecGetOwnershipRange(out,&start,&end);CHKERRQ(ierr); 332785e854fSJed Brown ierr = PetscMalloc1(m,&rows);CHKERRQ(ierr); 3338865f1eaSKarl Rupp for (i=0; i<m; i++) rows[i] = start + i; 334b0a32e0cSBarry Smith 335f69a0ea3SMatthew Knepley ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3360b12b109SJed Brown ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 337b0a32e0cSBarry Smith if (size == 1) { 338be5d1d56SKris Buschelman ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr); 3390298fd71SBarry Smith ierr = MatSeqDenseSetPreallocation(*mat,NULL);CHKERRQ(ierr); 340b0a32e0cSBarry Smith } else { 341be5d1d56SKris Buschelman ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 3420298fd71SBarry Smith ierr = MatMPIAIJSetPreallocation(*mat,n,NULL,N-n,NULL);CHKERRQ(ierr); 343b0a32e0cSBarry Smith } 344b0a32e0cSBarry Smith 3450b12b109SJed Brown for (i=0; i<N; i++) { 346b0a32e0cSBarry Smith 3472dcb1b2aSMatthew Knepley ierr = VecSet(in,zero);CHKERRQ(ierr); 348b0a32e0cSBarry Smith ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr); 349b0a32e0cSBarry Smith ierr = VecAssemblyBegin(in);CHKERRQ(ierr); 350b0a32e0cSBarry Smith ierr = VecAssemblyEnd(in);CHKERRQ(ierr); 351b0a32e0cSBarry Smith 352b0a32e0cSBarry Smith ierr = MatMult(inmat,in,out);CHKERRQ(ierr); 353b0a32e0cSBarry Smith 354b0a32e0cSBarry Smith ierr = VecGetArray(out,&array);CHKERRQ(ierr); 355b0a32e0cSBarry Smith ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 356b0a32e0cSBarry Smith ierr = VecRestoreArray(out,&array);CHKERRQ(ierr); 357b0a32e0cSBarry Smith 358b0a32e0cSBarry Smith } 359b0a32e0cSBarry Smith ierr = PetscFree(rows);CHKERRQ(ierr); 3606bf464f9SBarry Smith ierr = VecDestroy(&out);CHKERRQ(ierr); 3616bf464f9SBarry Smith ierr = VecDestroy(&in);CHKERRQ(ierr); 362b0a32e0cSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 363b0a32e0cSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 364b0a32e0cSBarry Smith PetscFunctionReturn(0); 365b0a32e0cSBarry Smith } 366b0a32e0cSBarry Smith 36724f910e3SHong Zhang /* Get the map xtoy which is used by MatAXPY() in the case of SUBSET_NONZERO_PATTERN */ 36824f910e3SHong Zhang #undef __FUNCT__ 36924f910e3SHong Zhang #define __FUNCT__ "MatAXPYGetxtoy_Private" 37038baddfdSBarry Smith PetscErrorCode MatAXPYGetxtoy_Private(PetscInt m,PetscInt *xi,PetscInt *xj,PetscInt *xgarray, PetscInt *yi,PetscInt *yj,PetscInt *ygarray, PetscInt **xtoy) 37124f910e3SHong Zhang { 3726849ba73SBarry Smith PetscErrorCode ierr; 37338baddfdSBarry Smith PetscInt row,i,nz,xcol,ycol,jx,jy,*x2y; 37424f910e3SHong Zhang 37524f910e3SHong Zhang PetscFunctionBegin; 376785e854fSJed Brown ierr = PetscMalloc1(xi[m],&x2y);CHKERRQ(ierr); 37724f910e3SHong Zhang i = 0; 37824f910e3SHong Zhang for (row=0; row<m; row++) { 37924f910e3SHong Zhang nz = xi[1] - xi[0]; 38024f910e3SHong Zhang jy = 0; 38124f910e3SHong Zhang for (jx=0; jx<nz; jx++,jy++) { 38224f910e3SHong Zhang if (xgarray && ygarray) { 38324f910e3SHong Zhang xcol = xgarray[xj[*xi + jx]]; 38424f910e3SHong Zhang ycol = ygarray[yj[*yi + jy]]; 38524f910e3SHong Zhang } else { 38624f910e3SHong Zhang xcol = xj[*xi + jx]; 38724f910e3SHong Zhang ycol = yj[*yi + jy]; /* col index for y */ 38824f910e3SHong Zhang } 38924f910e3SHong Zhang while (ycol < xcol) { 39024f910e3SHong Zhang jy++; 39124f910e3SHong Zhang if (ygarray) { 39224f910e3SHong Zhang ycol = ygarray[yj[*yi + jy]]; 39324f910e3SHong Zhang } else { 39424f910e3SHong Zhang ycol = yj[*yi + jy]; 39524f910e3SHong Zhang } 39624f910e3SHong Zhang } 397e32f2f54SBarry Smith if (xcol != ycol) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"X matrix entry (%D,%D) is not in Y matrix",row,ycol); 39824f910e3SHong Zhang x2y[i++] = *yi + jy; 39924f910e3SHong Zhang } 40024f910e3SHong Zhang xi++; yi++; 40124f910e3SHong Zhang } 40224f910e3SHong Zhang *xtoy = x2y; 40324f910e3SHong Zhang PetscFunctionReturn(0); 40424f910e3SHong Zhang } 4054325cce7SMatthew G Knepley 4064325cce7SMatthew G Knepley #undef __FUNCT__ 4074325cce7SMatthew G Knepley #define __FUNCT__ "MatChop" 4084325cce7SMatthew G Knepley /*@ 4094325cce7SMatthew G Knepley MatChop - Set all values in the matrix less than the tolerance to zero 4104325cce7SMatthew G Knepley 4114325cce7SMatthew G Knepley Input Parameters: 4124325cce7SMatthew G Knepley + A - The matrix 4134325cce7SMatthew G Knepley - tol - The zero tolerance 4144325cce7SMatthew G Knepley 4154325cce7SMatthew G Knepley Output Parameters: 4164325cce7SMatthew G Knepley . A - The chopped matrix 4174325cce7SMatthew G Knepley 4184325cce7SMatthew G Knepley Level: intermediate 4194325cce7SMatthew G Knepley 4204325cce7SMatthew G Knepley .seealso: MatCreate(), MatZeroEntries() 4213fc99919SSatish Balay @*/ 4224325cce7SMatthew G Knepley PetscErrorCode MatChop(Mat A, PetscReal tol) 4234325cce7SMatthew G Knepley { 4244325cce7SMatthew G Knepley PetscScalar *newVals; 4254325cce7SMatthew G Knepley PetscInt *newCols; 4264325cce7SMatthew G Knepley PetscInt rStart, rEnd, numRows, maxRows, r, colMax = 0; 4274325cce7SMatthew G Knepley PetscErrorCode ierr; 4284325cce7SMatthew G Knepley 4294325cce7SMatthew G Knepley PetscFunctionBegin; 4304325cce7SMatthew G Knepley ierr = MatGetOwnershipRange(A, &rStart, &rEnd);CHKERRQ(ierr); 4314325cce7SMatthew G Knepley for (r = rStart; r < rEnd; ++r) { 4324325cce7SMatthew G Knepley PetscInt ncols; 4334325cce7SMatthew G Knepley 4340298fd71SBarry Smith ierr = MatGetRow(A, r, &ncols, NULL, NULL);CHKERRQ(ierr); 4354325cce7SMatthew G Knepley colMax = PetscMax(colMax, ncols);CHKERRQ(ierr); 4360298fd71SBarry Smith ierr = MatRestoreRow(A, r, &ncols, NULL, NULL);CHKERRQ(ierr); 4374325cce7SMatthew G Knepley } 4384325cce7SMatthew G Knepley numRows = rEnd - rStart; 4395a4c1302SJed Brown ierr = MPI_Allreduce(&numRows, &maxRows, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 440dcca6d9dSJed Brown ierr = PetscMalloc2(colMax,&newCols,colMax,&newVals);CHKERRQ(ierr); 4414325cce7SMatthew G Knepley for (r = rStart; r < rStart+maxRows; ++r) { 4424325cce7SMatthew G Knepley const PetscScalar *vals; 4434325cce7SMatthew G Knepley const PetscInt *cols; 444fad45679SMatthew G. Knepley PetscInt ncols, newcols, c; 4454325cce7SMatthew G Knepley 4464325cce7SMatthew G Knepley if (r < rEnd) { 4474325cce7SMatthew G Knepley ierr = MatGetRow(A, r, &ncols, &cols, &vals);CHKERRQ(ierr); 4484325cce7SMatthew G Knepley for (c = 0; c < ncols; ++c) { 4494325cce7SMatthew G Knepley newCols[c] = cols[c]; 4504325cce7SMatthew G Knepley newVals[c] = PetscAbsScalar(vals[c]) < tol ? 0.0 : vals[c]; 4514325cce7SMatthew G Knepley } 452fad45679SMatthew G. Knepley newcols = ncols; 4534325cce7SMatthew G Knepley ierr = MatRestoreRow(A, r, &ncols, &cols, &vals);CHKERRQ(ierr); 454fad45679SMatthew G. Knepley ierr = MatSetValues(A, 1, &r, newcols, newCols, newVals, INSERT_VALUES);CHKERRQ(ierr); 4554325cce7SMatthew G Knepley } 4564325cce7SMatthew G Knepley ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4574325cce7SMatthew G Knepley ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4584325cce7SMatthew G Knepley } 4594325cce7SMatthew G Knepley ierr = PetscFree2(newCols,newVals);CHKERRQ(ierr); 4604325cce7SMatthew G Knepley PetscFunctionReturn(0); 4614325cce7SMatthew G Knepley } 462