14b9ad928SBarry Smith /*$Id: pbjacobi.c,v 1.4 2001/08/07 03:03:42 balay Exp $*/ 24b9ad928SBarry Smith 34b9ad928SBarry Smith /* 44b9ad928SBarry Smith Include files needed for the PBJacobi preconditioner: 54b9ad928SBarry Smith pcimpl.h - private include file intended for use by all preconditioners 64b9ad928SBarry Smith */ 74b9ad928SBarry Smith 84b9ad928SBarry Smith #include "src/ksp/pc/pcimpl.h" /*I "petscpc.h" I*/ 94b9ad928SBarry Smith 104b9ad928SBarry Smith /* 114b9ad928SBarry Smith Private context (data structure) for the PBJacobi preconditioner. 124b9ad928SBarry Smith */ 134b9ad928SBarry Smith typedef struct { 144b9ad928SBarry Smith PetscScalar *diag; 154b9ad928SBarry Smith int bs,mbs; 164b9ad928SBarry Smith } PC_PBJacobi; 174b9ad928SBarry Smith 184b9ad928SBarry Smith /* 194b9ad928SBarry Smith Currently only implemented for baij matrices and directly access baij 204b9ad928SBarry Smith data structures. 214b9ad928SBarry Smith */ 224b9ad928SBarry Smith #include "src/mat/impls/baij/mpi/mpibaij.h" 234b9ad928SBarry Smith #include "src/inline/ilu.h" 244b9ad928SBarry Smith 254b9ad928SBarry Smith #undef __FUNCT__ 264b9ad928SBarry Smith #define __FUNCT__ "PCApply_PBJacobi_2" 274b9ad928SBarry Smith static int PCApply_PBJacobi_2(PC pc,Vec x,Vec y) 284b9ad928SBarry Smith { 294b9ad928SBarry Smith PC_PBJacobi *jac = (PC_PBJacobi*)pc->data; 304b9ad928SBarry Smith int ierr,i,m = jac->mbs; 314b9ad928SBarry Smith PetscScalar *diag = jac->diag,x0,x1,*xx,*yy; 324b9ad928SBarry Smith 334b9ad928SBarry Smith PetscFunctionBegin; 344b9ad928SBarry Smith ierr = VecGetArray(x,&xx);CHKERRQ(ierr); 354b9ad928SBarry Smith ierr = VecGetArray(y,&yy);CHKERRQ(ierr); 364b9ad928SBarry Smith for (i=0; i<m; i++) { 374b9ad928SBarry Smith x0 = xx[2*i]; x1 = xx[2*i+1]; 384b9ad928SBarry Smith yy[2*i] = diag[0]*x0 + diag[2]*x1; 394b9ad928SBarry Smith yy[2*i+1] = diag[1]*x0 + diag[3]*x1; 404b9ad928SBarry Smith diag += 4; 414b9ad928SBarry Smith } 424b9ad928SBarry Smith ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr); 434b9ad928SBarry Smith ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr); 444b9ad928SBarry Smith PetscLogFlops(6*m); 454b9ad928SBarry Smith PetscFunctionReturn(0); 464b9ad928SBarry Smith } 474b9ad928SBarry Smith #undef __FUNCT__ 484b9ad928SBarry Smith #define __FUNCT__ "PCApply_PBJacobi_3" 494b9ad928SBarry Smith static int PCApply_PBJacobi_3(PC pc,Vec x,Vec y) 504b9ad928SBarry Smith { 514b9ad928SBarry Smith PC_PBJacobi *jac = (PC_PBJacobi*)pc->data; 524b9ad928SBarry Smith int ierr,i,m = jac->mbs; 534b9ad928SBarry Smith PetscScalar *diag = jac->diag,x0,x1,x2,*xx,*yy; 544b9ad928SBarry Smith 554b9ad928SBarry Smith PetscFunctionBegin; 564b9ad928SBarry Smith ierr = VecGetArray(x,&xx);CHKERRQ(ierr); 574b9ad928SBarry Smith ierr = VecGetArray(y,&yy);CHKERRQ(ierr); 584b9ad928SBarry Smith for (i=0; i<m; i++) { 594b9ad928SBarry Smith x0 = xx[3*i]; x1 = xx[3*i+1]; x2 = xx[3*i+2]; 604b9ad928SBarry Smith yy[3*i] = diag[0]*x0 + diag[3]*x1 + diag[6]*x2; 614b9ad928SBarry Smith yy[3*i+1] = diag[1]*x0 + diag[4]*x1 + diag[7]*x2; 624b9ad928SBarry Smith yy[3*i+2] = diag[2]*x0 + diag[5]*x1 + diag[8]*x2; 634b9ad928SBarry Smith diag += 9; 644b9ad928SBarry Smith } 654b9ad928SBarry Smith ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr); 664b9ad928SBarry Smith ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr); 674b9ad928SBarry Smith PetscLogFlops(15*m); 684b9ad928SBarry Smith PetscFunctionReturn(0); 694b9ad928SBarry Smith } 704b9ad928SBarry Smith #undef __FUNCT__ 714b9ad928SBarry Smith #define __FUNCT__ "PCApply_PBJacobi_4" 724b9ad928SBarry Smith static int PCApply_PBJacobi_4(PC pc,Vec x,Vec y) 734b9ad928SBarry Smith { 744b9ad928SBarry Smith PC_PBJacobi *jac = (PC_PBJacobi*)pc->data; 754b9ad928SBarry Smith int ierr,i,m = jac->mbs; 764b9ad928SBarry Smith PetscScalar *diag = jac->diag,x0,x1,x2,x3,*xx,*yy; 774b9ad928SBarry Smith 784b9ad928SBarry Smith PetscFunctionBegin; 794b9ad928SBarry Smith ierr = VecGetArray(x,&xx);CHKERRQ(ierr); 804b9ad928SBarry Smith ierr = VecGetArray(y,&yy);CHKERRQ(ierr); 814b9ad928SBarry Smith for (i=0; i<m; i++) { 824b9ad928SBarry Smith x0 = xx[4*i]; x1 = xx[4*i+1]; x2 = xx[4*i+2]; x3 = xx[4*i+3]; 834b9ad928SBarry Smith yy[4*i] = diag[0]*x0 + diag[4]*x1 + diag[8]*x2 + diag[12]*x3; 844b9ad928SBarry Smith yy[4*i+1] = diag[1]*x0 + diag[5]*x1 + diag[9]*x2 + diag[13]*x3; 854b9ad928SBarry Smith yy[4*i+2] = diag[2]*x0 + diag[6]*x1 + diag[10]*x2 + diag[14]*x3; 864b9ad928SBarry Smith yy[4*i+3] = diag[3]*x0 + diag[7]*x1 + diag[11]*x2 + diag[15]*x3; 874b9ad928SBarry Smith diag += 16; 884b9ad928SBarry Smith } 894b9ad928SBarry Smith ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr); 904b9ad928SBarry Smith ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr); 914b9ad928SBarry Smith PetscLogFlops(28*m); 924b9ad928SBarry Smith PetscFunctionReturn(0); 934b9ad928SBarry Smith } 944b9ad928SBarry Smith #undef __FUNCT__ 954b9ad928SBarry Smith #define __FUNCT__ "PCApply_PBJacobi_5" 964b9ad928SBarry Smith static int PCApply_PBJacobi_5(PC pc,Vec x,Vec y) 974b9ad928SBarry Smith { 984b9ad928SBarry Smith PC_PBJacobi *jac = (PC_PBJacobi*)pc->data; 994b9ad928SBarry Smith int ierr,i,m = jac->mbs; 1004b9ad928SBarry Smith PetscScalar *diag = jac->diag,x0,x1,x2,x3,x4,*xx,*yy; 1014b9ad928SBarry Smith 1024b9ad928SBarry Smith PetscFunctionBegin; 1034b9ad928SBarry Smith ierr = VecGetArray(x,&xx);CHKERRQ(ierr); 1044b9ad928SBarry Smith ierr = VecGetArray(y,&yy);CHKERRQ(ierr); 1054b9ad928SBarry Smith for (i=0; i<m; i++) { 1064b9ad928SBarry Smith x0 = xx[5*i]; x1 = xx[5*i+1]; x2 = xx[5*i+2]; x3 = xx[5*i+3]; x4 = xx[5*i+4]; 1074b9ad928SBarry Smith yy[5*i] = diag[0]*x0 + diag[5]*x1 + diag[10]*x2 + diag[15]*x3 + diag[20]*x4; 1084b9ad928SBarry Smith yy[5*i+1] = diag[1]*x0 + diag[6]*x1 + diag[11]*x2 + diag[16]*x3 + diag[21]*x4; 1094b9ad928SBarry Smith yy[5*i+2] = diag[2]*x0 + diag[7]*x1 + diag[12]*x2 + diag[17]*x3 + diag[22]*x4; 1104b9ad928SBarry Smith yy[5*i+3] = diag[3]*x0 + diag[8]*x1 + diag[13]*x2 + diag[18]*x3 + diag[23]*x4; 1114b9ad928SBarry Smith yy[5*i+4] = diag[4]*x0 + diag[9]*x1 + diag[14]*x2 + diag[19]*x3 + diag[24]*x4; 1124b9ad928SBarry Smith diag += 25; 1134b9ad928SBarry Smith } 1144b9ad928SBarry Smith ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr); 1154b9ad928SBarry Smith ierr = VecRestoreArray(y,&yy);CHKERRQ(ierr); 1164b9ad928SBarry Smith PetscLogFlops(45*m); 1174b9ad928SBarry Smith PetscFunctionReturn(0); 1184b9ad928SBarry Smith } 1194b9ad928SBarry Smith /* -------------------------------------------------------------------------- */ 120*a1d92eedSBarry Smith extern int MatInvertBlockDiagonal_SeqBAIJ(Mat); 1214b9ad928SBarry Smith #undef __FUNCT__ 1224b9ad928SBarry Smith #define __FUNCT__ "PCSetUp_PBJacobi" 1234b9ad928SBarry Smith static int PCSetUp_PBJacobi(PC pc) 1244b9ad928SBarry Smith { 1254b9ad928SBarry Smith PC_PBJacobi *jac = (PC_PBJacobi*)pc->data; 126*a1d92eedSBarry Smith int ierr,size; 12721360622SBarry Smith PetscTruth seqbaij,mpibaij,baij; 1284b9ad928SBarry Smith Mat A = pc->pmat; 1294b9ad928SBarry Smith Mat_SeqBAIJ *a; 1304b9ad928SBarry Smith 1314b9ad928SBarry Smith PetscFunctionBegin; 1324b9ad928SBarry Smith ierr = PetscTypeCompare((PetscObject)pc->pmat,MATSEQBAIJ,&seqbaij);CHKERRQ(ierr); 1334b9ad928SBarry Smith ierr = PetscTypeCompare((PetscObject)pc->pmat,MATMPIBAIJ,&mpibaij);CHKERRQ(ierr); 13421360622SBarry Smith ierr = PetscTypeCompare((PetscObject)pc->pmat,MATBAIJ,&baij);CHKERRQ(ierr); 13521360622SBarry Smith if (!seqbaij && !mpibaij && !baij) { 1364b9ad928SBarry Smith SETERRQ(1,"Currently only supports BAIJ matrices"); 1374b9ad928SBarry Smith } 13821360622SBarry Smith ierr = MPI_Comm_size(pc->comm,&size);CHKERRQ(ierr); 13921360622SBarry Smith if (mpibaij || (baij && (size > 1))) A = ((Mat_MPIBAIJ*)A->data)->A; 1404b9ad928SBarry Smith if (A->m != A->n) SETERRQ(1,"Supported only for square matrices and square storage"); 141*a1d92eedSBarry Smith 142*a1d92eedSBarry Smith ierr = MatInvertBlockDiagonal_SeqBAIJ(A);CHKERRQ(ierr); 1434b9ad928SBarry Smith a = (Mat_SeqBAIJ*)A->data; 144*a1d92eedSBarry Smith jac->diag = a->idiag; 1454b9ad928SBarry Smith jac->bs = a->bs; 1464b9ad928SBarry Smith jac->mbs = a->mbs; 1474b9ad928SBarry Smith switch (a->bs){ 1484b9ad928SBarry Smith case 2: 1494b9ad928SBarry Smith pc->ops->apply = PCApply_PBJacobi_2; 1504b9ad928SBarry Smith break; 1514b9ad928SBarry Smith case 3: 1524b9ad928SBarry Smith pc->ops->apply = PCApply_PBJacobi_3; 1534b9ad928SBarry Smith break; 1544b9ad928SBarry Smith case 4: 1554b9ad928SBarry Smith pc->ops->apply = PCApply_PBJacobi_4; 1564b9ad928SBarry Smith break; 1574b9ad928SBarry Smith case 5: 1584b9ad928SBarry Smith pc->ops->apply = PCApply_PBJacobi_5; 1594b9ad928SBarry Smith break; 1604b9ad928SBarry Smith default: 1614b9ad928SBarry Smith SETERRQ1(1,"not supported for block size %d",a->bs); 1624b9ad928SBarry Smith } 1634b9ad928SBarry Smith 1644b9ad928SBarry Smith PetscFunctionReturn(0); 1654b9ad928SBarry Smith } 1664b9ad928SBarry Smith /* -------------------------------------------------------------------------- */ 1674b9ad928SBarry Smith #undef __FUNCT__ 1684b9ad928SBarry Smith #define __FUNCT__ "PCDestroy_PBJacobi" 1694b9ad928SBarry Smith static int PCDestroy_PBJacobi(PC pc) 1704b9ad928SBarry Smith { 1714b9ad928SBarry Smith PC_PBJacobi *jac = (PC_PBJacobi*)pc->data; 1724b9ad928SBarry Smith int ierr; 1734b9ad928SBarry Smith 1744b9ad928SBarry Smith PetscFunctionBegin; 1754b9ad928SBarry Smith /* 1764b9ad928SBarry Smith Free the private data structure that was hanging off the PC 1774b9ad928SBarry Smith */ 1784b9ad928SBarry Smith ierr = PetscFree(jac);CHKERRQ(ierr); 1794b9ad928SBarry Smith PetscFunctionReturn(0); 1804b9ad928SBarry Smith } 1814b9ad928SBarry Smith /* -------------------------------------------------------------------------- */ 1824b9ad928SBarry Smith EXTERN_C_BEGIN 1834b9ad928SBarry Smith #undef __FUNCT__ 1844b9ad928SBarry Smith #define __FUNCT__ "PCCreate_PBJacobi" 1854b9ad928SBarry Smith int PCCreate_PBJacobi(PC pc) 1864b9ad928SBarry Smith { 1874b9ad928SBarry Smith PC_PBJacobi *jac; 1884b9ad928SBarry Smith int ierr; 1894b9ad928SBarry Smith 1904b9ad928SBarry Smith PetscFunctionBegin; 1914b9ad928SBarry Smith 1924b9ad928SBarry Smith /* 1934b9ad928SBarry Smith Creates the private data structure for this preconditioner and 1944b9ad928SBarry Smith attach it to the PC object. 1954b9ad928SBarry Smith */ 1964b9ad928SBarry Smith ierr = PetscNew(PC_PBJacobi,&jac);CHKERRQ(ierr); 1974b9ad928SBarry Smith pc->data = (void*)jac; 1984b9ad928SBarry Smith 1994b9ad928SBarry Smith /* 2004b9ad928SBarry Smith Logs the memory usage; this is not needed but allows PETSc to 2014b9ad928SBarry Smith monitor how much memory is being used for various purposes. 2024b9ad928SBarry Smith */ 2034b9ad928SBarry Smith PetscLogObjectMemory(pc,sizeof(PC_PBJacobi)); 2044b9ad928SBarry Smith 2054b9ad928SBarry Smith /* 2064b9ad928SBarry Smith Initialize the pointers to vectors to ZERO; these will be used to store 2074b9ad928SBarry Smith diagonal entries of the matrix for fast preconditioner application. 2084b9ad928SBarry Smith */ 2094b9ad928SBarry Smith jac->diag = 0; 2104b9ad928SBarry Smith 2114b9ad928SBarry Smith /* 2124b9ad928SBarry Smith Set the pointers for the functions that are provided above. 2134b9ad928SBarry Smith Now when the user-level routines (such as PCApply(), PCDestroy(), etc.) 2144b9ad928SBarry Smith are called, they will automatically call these functions. Note we 2154b9ad928SBarry Smith choose not to provide a couple of these functions since they are 2164b9ad928SBarry Smith not needed. 2174b9ad928SBarry Smith */ 2184b9ad928SBarry Smith pc->ops->apply = 0; /*set depending on the block size */ 2194b9ad928SBarry Smith pc->ops->applytranspose = 0; 2204b9ad928SBarry Smith pc->ops->setup = PCSetUp_PBJacobi; 2214b9ad928SBarry Smith pc->ops->destroy = PCDestroy_PBJacobi; 2224b9ad928SBarry Smith pc->ops->setfromoptions = 0; 2234b9ad928SBarry Smith pc->ops->view = 0; 2244b9ad928SBarry Smith pc->ops->applyrichardson = 0; 2254b9ad928SBarry Smith pc->ops->applysymmetricleft = 0; 2264b9ad928SBarry Smith pc->ops->applysymmetricright = 0; 2274b9ad928SBarry Smith PetscFunctionReturn(0); 2284b9ad928SBarry Smith } 2294b9ad928SBarry Smith EXTERN_C_END 2304b9ad928SBarry Smith 2314b9ad928SBarry Smith 232