1 #ifdef PETSC_RCS_HEADER 2 static char vcid[] = "$Id: mpiaijpc.c,v 1.35 1998/03/16 18:55:15 bsmith Exp bsmith $"; 3 #endif 4 /* 5 Defines a block Jacobi preconditioner for the SeqAIJ/MPIAIJ format. 6 Handles special case of single block per processor. 7 This file knows about storage formats for SeqMPI/MPIAIJ matrices. 8 The general case is handled in aijpc.c 9 */ 10 #include "src/mat/impls/aij/mpi/mpiaij.h" 11 #include "src/pc/pcimpl.h" 12 #include "src/vec/vecimpl.h" 13 #include "src/pc/impls/bjacobi/bjacobi.h" 14 #include "sles.h" 15 16 typedef struct { 17 Vec x, y; 18 } PC_BJacobi_MPIAIJ; 19 20 #undef __FUNC__ 21 #define __FUNC__ "PCDestroy_BJacobi_MPIAIJ" 22 int PCDestroy_BJacobi_MPIAIJ(PC pc) 23 { 24 PC_BJacobi *jac = (PC_BJacobi *) pc->data; 25 PC_BJacobi_MPIAIJ *bjac = (PC_BJacobi_MPIAIJ *) jac->data; 26 int ierr; 27 28 PetscFunctionBegin; 29 ierr = SLESDestroy(jac->sles[0]); CHKERRQ(ierr); 30 PetscFree(jac->sles); 31 ierr = VecDestroy(bjac->x); CHKERRQ(ierr); 32 ierr = VecDestroy(bjac->y); CHKERRQ(ierr); 33 if (jac->l_lens) PetscFree(jac->l_lens); 34 if (jac->g_lens) PetscFree(jac->g_lens); 35 PetscFree(bjac); PetscFree(jac); 36 PetscFunctionReturn(0); 37 } 38 39 40 #undef __FUNC__ 41 #define __FUNC__ "PCSetUpOnBlocks_BJacobi_MPIAIJ" 42 int PCSetUpOnBlocks_BJacobi_MPIAIJ(PC pc) 43 { 44 int ierr; 45 PC_BJacobi *jac = (PC_BJacobi *) pc->data; 46 PC_BJacobi_MPIAIJ *bjac = (PC_BJacobi_MPIAIJ *) jac->data; 47 48 PetscFunctionBegin; 49 ierr = SLESSetUp(jac->sles[0],bjac->x,bjac->y); CHKERRQ(ierr); 50 PetscFunctionReturn(0); 51 } 52 53 #undef __FUNC__ 54 #define __FUNC__ "PCApply_BJacobi_MPIAIJ" 55 int PCApply_BJacobi_MPIAIJ(PC pc,Vec x, Vec y) 56 { 57 int ierr,its; 58 PC_BJacobi *jac = (PC_BJacobi *) pc->data; 59 PC_BJacobi_MPIAIJ *bjac = (PC_BJacobi_MPIAIJ *) jac->data; 60 Scalar *x_array,*x_true_array, *y_array,*y_true_array; 61 62 PetscFunctionBegin; 63 /* 64 The VecPlaceArray() is to avoid having to copy the 65 y vector into the bjac->x vector. The reason for 66 the bjac->x vector is that we need a sequential vector 67 for the sequential solve. 68 */ 69 ierr = VecGetArray(x,&x_array); CHKERRQ(ierr); 70 ierr = VecGetArray(y,&y_array); CHKERRQ(ierr); 71 ierr = VecGetArray(bjac->x,&x_true_array); CHKERRQ(ierr); 72 ierr = VecGetArray(bjac->y,&y_true_array); CHKERRQ(ierr); 73 ierr = VecPlaceArray(bjac->x,x_array); CHKERRQ(ierr); 74 ierr = VecPlaceArray(bjac->y,y_array); CHKERRQ(ierr); 75 ierr = SLESSolve(jac->sles[0],bjac->x,bjac->y,&its); CHKERRQ(ierr); 76 ierr = VecPlaceArray(bjac->x,x_true_array); CHKERRQ(ierr); 77 ierr = VecPlaceArray(bjac->y,y_true_array);CHKERRQ(ierr); 78 PetscFunctionReturn(0); 79 } 80 81 #undef __FUNC__ 82 #define __FUNC__ "PCSetUp_BJacobi_MPIAIJ" 83 int PCSetUp_BJacobi_MPIAIJ(PC pc) 84 { 85 PC_BJacobi *jac = (PC_BJacobi *) pc->data; 86 Mat mat = pc->mat, pmat = pc->pmat; 87 Mat_MPIAIJ *pmatin = (Mat_MPIAIJ *) pmat->data; 88 Mat_MPIAIJ *matin = 0; 89 int ierr, m; 90 SLES sles; 91 Vec x,y; 92 PC_BJacobi_MPIAIJ *bjac; 93 KSP subksp; 94 PC subpc; 95 MatType type; 96 97 PetscFunctionBegin; 98 if (jac->use_true_local) { 99 ierr = MatGetType(pc->mat,&type,PETSC_NULL);CHKERRQ(ierr); 100 if (type != MATMPIAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Incompatible matrix type."); 101 matin = (Mat_MPIAIJ *) mat->data; 102 } 103 104 /* set default direct solver with no Krylov method */ 105 if (!pc->setupcalled) { 106 char *prefix; 107 ierr = SLESCreate(PETSC_COMM_SELF,&sles); CHKERRQ(ierr); 108 PLogObjectParent(pc,sles); 109 ierr = SLESGetKSP(sles,&subksp); CHKERRQ(ierr); 110 ierr = KSPSetType(subksp,KSPPREONLY); CHKERRQ(ierr); 111 ierr = SLESGetPC(sles,&subpc); CHKERRQ(ierr); 112 ierr = PCSetType(subpc,PCILU); CHKERRQ(ierr); 113 ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); 114 ierr = SLESSetOptionsPrefix(sles,prefix); CHKERRQ(ierr); 115 ierr = SLESAppendOptionsPrefix(sles,"sub_"); CHKERRQ(ierr); 116 ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 117 /* 118 This is not so good. The only reason we need to generate this vector 119 is so KSP may generate seq vectors for the local solves 120 */ 121 ierr = MatGetSize(pmatin->A,&m,&m); CHKERRQ(ierr); 122 ierr = VecCreateSeq(PETSC_COMM_SELF,m,&x); CHKERRQ(ierr); 123 ierr = VecCreateSeq(PETSC_COMM_SELF,m,&y); CHKERRQ(ierr); 124 PLogObjectParent(pc,x); 125 PLogObjectParent(pc,y); 126 127 pc->destroy = PCDestroy_BJacobi_MPIAIJ; 128 pc->apply = PCApply_BJacobi_MPIAIJ; 129 pc->setuponblocks = PCSetUpOnBlocks_BJacobi_MPIAIJ; 130 131 bjac = (PC_BJacobi_MPIAIJ *) PetscMalloc(sizeof(PC_BJacobi_MPIAIJ));CHKPTRQ(bjac); 132 PLogObjectMemory(pc,sizeof(PC_BJacobi_MPIAIJ)); 133 bjac->x = x; 134 bjac->y = y; 135 136 jac->sles = (SLES*) PetscMalloc( sizeof(SLES) ); CHKPTRQ(jac->sles); 137 jac->sles[0] = sles; 138 jac->data = (void *) bjac; 139 } else { 140 sles = jac->sles[0]; 141 bjac = (PC_BJacobi_MPIAIJ *)jac->data; 142 } 143 if (jac->use_true_local) { 144 ierr = SLESSetOperators(sles,matin->A,pmatin->A,pc->flag); CHKERRQ(ierr); 145 } else { 146 ierr = SLESSetOperators(sles,pmatin->A,pmatin->A,pc->flag); CHKERRQ(ierr); 147 } 148 PetscFunctionReturn(0); 149 } 150 151 #undef __FUNC__ 152 #define __FUNC__ "PCSetUp_BJacobi_SeqAIJ" 153 int PCSetUp_BJacobi_SeqAIJ(PC pc) 154 { 155 PC_BJacobi *jac = (PC_BJacobi *) pc->data; 156 Mat mat = pc->mat, pmat = pc->pmat; 157 int ierr, m; 158 SLES sles; 159 Vec x,y; 160 PC_BJacobi_MPIAIJ *bjac; 161 KSP subksp; 162 PC subpc; 163 MatType type; 164 165 PetscFunctionBegin; 166 if (jac->use_true_local) { 167 MatGetType(mat,&type,PETSC_NULL); 168 if (type != MATSEQAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Incompatible matrix type."); 169 } 170 171 /* set default direct solver with no Krylov method */ 172 if (!pc->setupcalled) { 173 char *prefix; 174 ierr = SLESCreate(PETSC_COMM_SELF,&sles); CHKERRQ(ierr); 175 PLogObjectParent(pc,sles); 176 ierr = SLESGetKSP(sles,&subksp); CHKERRQ(ierr); 177 ierr = KSPSetType(subksp,KSPPREONLY); CHKERRQ(ierr); 178 ierr = SLESGetPC(sles,&subpc); CHKERRQ(ierr); 179 ierr = PCSetType(subpc,PCILU); CHKERRQ(ierr); 180 ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); 181 ierr = SLESSetOptionsPrefix(sles,prefix); CHKERRQ(ierr); 182 ierr = SLESAppendOptionsPrefix(sles,"sub_"); CHKERRQ(ierr); 183 ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 184 /* 185 This is not so good. The only reason we need to generate this vector 186 is so KSP may generate seq vectors for the local solves 187 */ 188 ierr = MatGetSize(pmat,&m,&m); CHKERRQ(ierr); 189 ierr = VecCreateSeq(PETSC_COMM_SELF,m,&x); CHKERRQ(ierr); 190 ierr = VecCreateSeq(PETSC_COMM_SELF,m,&y); CHKERRQ(ierr); 191 PLogObjectParent(pc,x); 192 PLogObjectParent(pc,y); 193 194 pc->destroy = PCDestroy_BJacobi_MPIAIJ; 195 pc->apply = PCApply_BJacobi_MPIAIJ; 196 pc->setuponblocks = PCSetUpOnBlocks_BJacobi_MPIAIJ; 197 198 bjac = (PC_BJacobi_MPIAIJ *) PetscMalloc(sizeof(PC_BJacobi_MPIAIJ));CHKPTRQ(bjac); 199 PLogObjectMemory(pc,sizeof(PC_BJacobi_MPIAIJ)); 200 bjac->x = x; 201 bjac->y = y; 202 203 jac->sles = (SLES*) PetscMalloc( sizeof(SLES) ); CHKPTRQ(jac->sles); 204 jac->sles[0] = sles; 205 jac->data = (void *) bjac; 206 } else { 207 sles = jac->sles[0]; 208 bjac = (PC_BJacobi_MPIAIJ *)jac->data; 209 } 210 if (jac->use_true_local) { 211 ierr = SLESSetOperators(sles,mat,pmat,pc->flag); CHKERRQ(ierr); 212 } else { 213 ierr = SLESSetOperators(sles,pmat,pmat,pc->flag); CHKERRQ(ierr); 214 } 215 PetscFunctionReturn(0); 216 } 217 218