Actual source code: pbjacobi.c

petsc-3.3-p7 2013-05-11
  2: /* 
  3:    Include files needed for the PBJacobi preconditioner:
  4:      pcimpl.h - private include file intended for use by all preconditioners 
  5: */

  7: #include <petsc-private/matimpl.h>
  8: #include <petsc-private/pcimpl.h>   /*I "petscpc.h" I*/

 10: /* 
 11:    Private context (data structure) for the PBJacobi preconditioner.  
 12: */
 13: typedef struct {
 14:   const MatScalar *diag;
 15:   PetscInt    bs,mbs;
 16: } PC_PBJacobi;


 21: static PetscErrorCode PCApply_PBJacobi_1(PC pc,Vec x,Vec y)
 22: {
 23:   PC_PBJacobi       *jac = (PC_PBJacobi*)pc->data;
 24:   PetscErrorCode    ierr;
 25:   PetscInt          i,m = jac->mbs;
 26:   const MatScalar   *diag = jac->diag;
 27:   const PetscScalar *xx;
 28:   PetscScalar       *yy;

 31:   VecGetArrayRead(x,&xx);
 32:   VecGetArray(y,&yy);
 33:   for (i=0; i<m; i++) {
 34:     yy[i] = diag[i]*xx[i];
 35:   }
 36:   VecRestoreArrayRead(x,&xx);
 37:   VecRestoreArray(y,&yy);
 38:   PetscLogFlops(2.0*m);
 39:   return(0);
 40: }

 44: static PetscErrorCode PCApply_PBJacobi_2(PC pc,Vec x,Vec y)
 45: {
 46:   PC_PBJacobi     *jac = (PC_PBJacobi*)pc->data;
 47:   PetscErrorCode  ierr;
 48:   PetscInt        i,m = jac->mbs;
 49:   const MatScalar *diag = jac->diag;
 50:   PetscScalar     x0,x1,*xx,*yy;
 51: 
 53:   VecGetArray(x,&xx);
 54:   VecGetArray(y,&yy);
 55:   for (i=0; i<m; i++) {
 56:     x0 = xx[2*i]; x1 = xx[2*i+1];
 57:     yy[2*i]   = diag[0]*x0 + diag[2]*x1;
 58:     yy[2*i+1] = diag[1]*x0 + diag[3]*x1;
 59:     diag     += 4;
 60:   }
 61:   VecRestoreArray(x,&xx);
 62:   VecRestoreArray(y,&yy);
 63:   PetscLogFlops(6.0*m);
 64:   return(0);
 65: }
 68: static PetscErrorCode PCApply_PBJacobi_3(PC pc,Vec x,Vec y)
 69: {
 70:   PC_PBJacobi     *jac = (PC_PBJacobi*)pc->data;
 71:   PetscErrorCode  ierr;
 72:   PetscInt        i,m = jac->mbs;
 73:   const MatScalar *diag = jac->diag;
 74:   PetscScalar     x0,x1,x2,*xx,*yy;
 75: 
 77:   VecGetArray(x,&xx);
 78:   VecGetArray(y,&yy);
 79:   for (i=0; i<m; i++) {
 80:     x0 = xx[3*i]; x1 = xx[3*i+1]; x2 = xx[3*i+2];
 81:     yy[3*i]   = diag[0]*x0 + diag[3]*x1 + diag[6]*x2;
 82:     yy[3*i+1] = diag[1]*x0 + diag[4]*x1 + diag[7]*x2;
 83:     yy[3*i+2] = diag[2]*x0 + diag[5]*x1 + diag[8]*x2;
 84:     diag     += 9;
 85:   }
 86:   VecRestoreArray(x,&xx);
 87:   VecRestoreArray(y,&yy);
 88:   PetscLogFlops(15.0*m);
 89:   return(0);
 90: }
 93: static PetscErrorCode PCApply_PBJacobi_4(PC pc,Vec x,Vec y)
 94: {
 95:   PC_PBJacobi      *jac = (PC_PBJacobi*)pc->data;
 96:   PetscErrorCode   ierr;
 97:   PetscInt         i,m = jac->mbs;
 98:   const MatScalar  *diag = jac->diag;
 99:   PetscScalar      x0,x1,x2,x3,*xx,*yy;
100: 
102:   VecGetArray(x,&xx);
103:   VecGetArray(y,&yy);
104:   for (i=0; i<m; i++) {
105:     x0 = xx[4*i]; x1 = xx[4*i+1]; x2 = xx[4*i+2]; x3 = xx[4*i+3];
106:     yy[4*i]   = diag[0]*x0 + diag[4]*x1 + diag[8]*x2  + diag[12]*x3;
107:     yy[4*i+1] = diag[1]*x0 + diag[5]*x1 + diag[9]*x2  + diag[13]*x3;
108:     yy[4*i+2] = diag[2]*x0 + diag[6]*x1 + diag[10]*x2 + diag[14]*x3;
109:     yy[4*i+3] = diag[3]*x0 + diag[7]*x1 + diag[11]*x2 + diag[15]*x3;
110:     diag     += 16;
111:   }
112:   VecRestoreArray(x,&xx);
113:   VecRestoreArray(y,&yy);
114:   PetscLogFlops(28.0*m);
115:   return(0);
116: }
119: static PetscErrorCode PCApply_PBJacobi_5(PC pc,Vec x,Vec y)
120: {
121:   PC_PBJacobi     *jac = (PC_PBJacobi*)pc->data;
122:   PetscErrorCode  ierr;
123:   PetscInt        i,m = jac->mbs;
124:   const MatScalar *diag = jac->diag;
125:   PetscScalar     x0,x1,x2,x3,x4,*xx,*yy;
126: 
128:   VecGetArray(x,&xx);
129:   VecGetArray(y,&yy);
130:   for (i=0; i<m; i++) {
131:     x0 = xx[5*i]; x1 = xx[5*i+1]; x2 = xx[5*i+2]; x3 = xx[5*i+3]; x4 = xx[5*i+4];
132:     yy[5*i]   = diag[0]*x0 + diag[5]*x1 + diag[10]*x2  + diag[15]*x3 + diag[20]*x4;
133:     yy[5*i+1] = diag[1]*x0 + diag[6]*x1 + diag[11]*x2  + diag[16]*x3 + diag[21]*x4;
134:     yy[5*i+2] = diag[2]*x0 + diag[7]*x1 + diag[12]*x2 + diag[17]*x3 + diag[22]*x4;
135:     yy[5*i+3] = diag[3]*x0 + diag[8]*x1 + diag[13]*x2 + diag[18]*x3 + diag[23]*x4;
136:     yy[5*i+4] = diag[4]*x0 + diag[9]*x1 + diag[14]*x2 + diag[19]*x3 + diag[24]*x4;
137:     diag     += 25;
138:   }
139:   VecRestoreArray(x,&xx);
140:   VecRestoreArray(y,&yy);
141:   PetscLogFlops(45.0*m);
142:   return(0);
143: }
146: static PetscErrorCode PCApply_PBJacobi_6(PC pc,Vec x,Vec y)
147: {
148:   PC_PBJacobi     *jac = (PC_PBJacobi*)pc->data;
149:   PetscErrorCode  ierr;
150:   PetscInt        i,m = jac->mbs;
151:   const MatScalar *diag = jac->diag;
152:   PetscScalar     x0,x1,x2,x3,x4,x5,*xx,*yy;
153: 
155:   VecGetArray(x,&xx);
156:   VecGetArray(y,&yy);
157:   for (i=0; i<m; i++) {
158:     x0 = xx[6*i]; x1 = xx[6*i+1]; x2 = xx[6*i+2]; x3 = xx[6*i+3]; x4 = xx[6*i+4]; x5 = xx[6*i+5];
159:     yy[6*i]   = diag[0]*x0 + diag[6]*x1  + diag[12]*x2  + diag[18]*x3 + diag[24]*x4 + diag[30]*x5;
160:     yy[6*i+1] = diag[1]*x0 + diag[7]*x1  + diag[13]*x2  + diag[19]*x3 + diag[25]*x4 + diag[31]*x5;
161:     yy[6*i+2] = diag[2]*x0 + diag[8]*x1  + diag[14]*x2  + diag[20]*x3 + diag[26]*x4 + diag[32]*x5;
162:     yy[6*i+3] = diag[3]*x0 + diag[9]*x1  + diag[15]*x2  + diag[21]*x3 + diag[27]*x4 + diag[33]*x5;
163:     yy[6*i+4] = diag[4]*x0 + diag[10]*x1 + diag[16]*x2  + diag[22]*x3 + diag[28]*x4 + diag[34]*x5;
164:     yy[6*i+5] = diag[5]*x0 + diag[11]*x1 + diag[17]*x2  + diag[23]*x3 + diag[29]*x4 + diag[35]*x5;
165:     diag     += 36;
166:   }
167:   VecRestoreArray(x,&xx);
168:   VecRestoreArray(y,&yy);
169:   PetscLogFlops(66.0*m);
170:   return(0);
171: }
174: static PetscErrorCode PCApply_PBJacobi_7(PC pc,Vec x,Vec y)
175: {
176:   PC_PBJacobi     *jac = (PC_PBJacobi*)pc->data;
177:   PetscErrorCode  ierr;
178:   PetscInt        i,m = jac->mbs;
179:   const MatScalar *diag = jac->diag;
180:   PetscScalar     x0,x1,x2,x3,x4,x5,x6,*xx,*yy;

183:   VecGetArray(x,&xx);
184:   VecGetArray(y,&yy);
185:   for (i=0; i<m; i++) {
186:     x0 = xx[7*i]; x1 = xx[7*i+1]; x2 = xx[7*i+2]; x3 = xx[7*i+3]; x4 = xx[7*i+4]; x5 = xx[7*i+5]; x6 = xx[7*i+6];
187:     yy[7*i]   = diag[0]*x0 + diag[7]*x1  + diag[14]*x2  + diag[21]*x3 + diag[28]*x4 + diag[35]*x5 + diag[42]*x6;
188:     yy[7*i+1] = diag[1]*x0 + diag[8]*x1  + diag[15]*x2  + diag[22]*x3 + diag[29]*x4 + diag[36]*x5 + diag[43]*x6;
189:     yy[7*i+2] = diag[2]*x0 + diag[9]*x1  + diag[16]*x2  + diag[23]*x3 + diag[30]*x4 + diag[37]*x5 + diag[44]*x6;
190:     yy[7*i+3] = diag[3]*x0 + diag[10]*x1 + diag[17]*x2  + diag[24]*x3 + diag[31]*x4 + diag[38]*x5 + diag[45]*x6;
191:     yy[7*i+4] = diag[4]*x0 + diag[11]*x1 + diag[18]*x2  + diag[25]*x3 + diag[32]*x4 + diag[39]*x5 + diag[46]*x6;
192:     yy[7*i+5] = diag[5]*x0 + diag[12]*x1 + diag[19]*x2  + diag[26]*x3 + diag[33]*x4 + diag[40]*x5 + diag[47]*x6;
193:     yy[7*i+6] = diag[6]*x0 + diag[13]*x1 + diag[20]*x2  + diag[27]*x3 + diag[34]*x4 + diag[41]*x5 + diag[48]*x6;
194:     diag     += 49;
195:   }
196:   VecRestoreArray(x,&xx);
197:   VecRestoreArray(y,&yy);
198:   PetscLogFlops(80.0*m);
199:   return(0);
200: }
201: /* -------------------------------------------------------------------------- */
204: static PetscErrorCode PCSetUp_PBJacobi(PC pc)
205: {
206:   PC_PBJacobi    *jac = (PC_PBJacobi*)pc->data;
208:   Mat            A = pc->pmat;

211:   if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Supported only for square matrices and square storage");

213:   MatInvertBlockDiagonal(A,&jac->diag);
214:   jac->bs     = A->rmap->bs;
215:   jac->mbs    = A->rmap->n/A->rmap->bs;
216:   switch (jac->bs){
217:     case 1:
218:       pc->ops->apply = PCApply_PBJacobi_1;
219:       break;
220:     case 2:
221:       pc->ops->apply = PCApply_PBJacobi_2;
222:       break;
223:     case 3:
224:       pc->ops->apply = PCApply_PBJacobi_3;
225:       break;
226:     case 4:
227:       pc->ops->apply = PCApply_PBJacobi_4;
228:       break;
229:     case 5:
230:       pc->ops->apply = PCApply_PBJacobi_5;
231:       break;
232:     case 6:
233:       pc->ops->apply = PCApply_PBJacobi_6;
234:       break;
235:     case 7:
236:       pc->ops->apply = PCApply_PBJacobi_7;
237:       break;
238:     default:
239:       SETERRQ1(((PetscObject)pc)->comm,PETSC_ERR_SUP,"not supported for block size %D",jac->bs);
240:   }

242:   return(0);
243: }
244: /* -------------------------------------------------------------------------- */
247: static PetscErrorCode PCDestroy_PBJacobi(PC pc)
248: {

252:   /*
253:       Free the private data structure that was hanging off the PC
254:   */
255:   PetscFree(pc->data);
256:   return(0);
257: }

261: static PetscErrorCode PCView_PBJacobi(PC pc,PetscViewer viewer)
262: {
264:   PC_PBJacobi    *jac = (PC_PBJacobi*)pc->data;
265:   PetscBool      iascii;

268:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
269:   if (iascii) {
270:     PetscViewerASCIIPrintf(viewer,"  point-block Jacobi: block size %D\n",jac->bs);
271:   } else SETERRQ1(((PetscObject)pc)->comm,PETSC_ERR_SUP,"Viewer type %s not supported for point-block Jacobi",((PetscObject)viewer)->type_name);
272:   return(0);
273: }

275: /* -------------------------------------------------------------------------- */
276: /*MC
277:      PCPBJACOBI - Point block Jacobi

279:    Level: beginner

281:   Concepts: point block Jacobi


284: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC

286: M*/

288: EXTERN_C_BEGIN
291: PetscErrorCode  PCCreate_PBJacobi(PC pc)
292: {
293:   PC_PBJacobi    *jac;


298:   /*
299:      Creates the private data structure for this preconditioner and
300:      attach it to the PC object.
301:   */
302:   PetscNewLog(pc,PC_PBJacobi,&jac);
303:   pc->data  = (void*)jac;

305:   /*
306:      Initialize the pointers to vectors to ZERO; these will be used to store
307:      diagonal entries of the matrix for fast preconditioner application.
308:   */
309:   jac->diag          = 0;

311:   /*
312:       Set the pointers for the functions that are provided above.
313:       Now when the user-level routines (such as PCApply(), PCDestroy(), etc.)
314:       are called, they will automatically call these functions.  Note we
315:       choose not to provide a couple of these functions since they are
316:       not needed.
317:   */
318:   pc->ops->apply               = 0; /*set depending on the block size */
319:   pc->ops->applytranspose      = 0;
320:   pc->ops->setup               = PCSetUp_PBJacobi;
321:   pc->ops->destroy             = PCDestroy_PBJacobi;
322:   pc->ops->setfromoptions      = 0;
323:   pc->ops->view                = PCView_PBJacobi;
324:   pc->ops->applyrichardson     = 0;
325:   pc->ops->applysymmetricleft  = 0;
326:   pc->ops->applysymmetricright = 0;
327:   return(0);
328: }
329: EXTERN_C_END