Actual source code: bddcfetidp.c

  1: #include <../src/ksp/pc/impls/bddc/bddc.h>
  2: #include <../src/ksp/pc/impls/bddc/bddcprivate.h>
  3: #include <petscblaslapack.h>

  5: static PetscErrorCode MatMult_BDdelta_deluxe_nonred(Mat A, Vec x, Vec y)
  6: {
  7:   BDdelta_DN     ctx;

 11:   MatShellGetContext(A,&ctx);
 12:   MatMultTranspose(ctx->BD,x,ctx->work);
 13:   KSPSolveTranspose(ctx->kBD,ctx->work,y);
 14:   /* No PC so cannot propagate up failure in KSPSolveTranspose() */
 15:   return(0);
 16: }

 18: static PetscErrorCode MatMultTranspose_BDdelta_deluxe_nonred(Mat A, Vec x, Vec y)
 19: {
 20:   BDdelta_DN     ctx;

 24:   MatShellGetContext(A,&ctx);
 25:   KSPSolve(ctx->kBD,x,ctx->work);
 26:   /* No PC so cannot propagate up failure in KSPSolve() */
 27:   MatMult(ctx->BD,ctx->work,y);
 28:   return(0);
 29: }

 31: static PetscErrorCode MatDestroy_BDdelta_deluxe_nonred(Mat A)
 32: {
 33:   BDdelta_DN     ctx;

 37:   MatShellGetContext(A,&ctx);
 38:   MatDestroy(&ctx->BD);
 39:   KSPDestroy(&ctx->kBD);
 40:   VecDestroy(&ctx->work);
 41:   PetscFree(ctx);
 42:   return(0);
 43: }

 45: PetscErrorCode PCBDDCCreateFETIDPMatContext(PC pc, FETIDPMat_ctx *fetidpmat_ctx)
 46: {
 47:   FETIDPMat_ctx  newctx;

 51:   PetscNew(&newctx);
 52:   /* increase the reference count for BDDC preconditioner */
 53:   PetscObjectReference((PetscObject)pc);
 54:   newctx->pc              = pc;
 55:   *fetidpmat_ctx          = newctx;
 56:   return(0);
 57: }

 59: PetscErrorCode PCBDDCCreateFETIDPPCContext(PC pc, FETIDPPC_ctx *fetidppc_ctx)
 60: {
 61:   FETIDPPC_ctx   newctx;

 65:   PetscNew(&newctx);
 66:   /* increase the reference count for BDDC preconditioner */
 67:   PetscObjectReference((PetscObject)pc);
 68:   newctx->pc              = pc;
 69:   *fetidppc_ctx           = newctx;
 70:   return(0);
 71: }

 73: PetscErrorCode PCBDDCDestroyFETIDPMat(Mat A)
 74: {
 75:   FETIDPMat_ctx  mat_ctx;

 79:   MatShellGetContext(A,&mat_ctx);
 80:   VecDestroy(&mat_ctx->lambda_local);
 81:   VecDestroy(&mat_ctx->temp_solution_D);
 82:   VecDestroy(&mat_ctx->temp_solution_B);
 83:   MatDestroy(&mat_ctx->B_delta);
 84:   MatDestroy(&mat_ctx->B_Ddelta);
 85:   MatDestroy(&mat_ctx->B_BB);
 86:   MatDestroy(&mat_ctx->B_BI);
 87:   MatDestroy(&mat_ctx->Bt_BB);
 88:   MatDestroy(&mat_ctx->Bt_BI);
 89:   MatDestroy(&mat_ctx->C);
 90:   VecDestroy(&mat_ctx->rhs_flip);
 91:   VecDestroy(&mat_ctx->vP);
 92:   VecDestroy(&mat_ctx->xPg);
 93:   VecDestroy(&mat_ctx->yPg);
 94:   VecScatterDestroy(&mat_ctx->l2g_lambda);
 95:   VecScatterDestroy(&mat_ctx->l2g_lambda_only);
 96:   VecScatterDestroy(&mat_ctx->l2g_p);
 97:   VecScatterDestroy(&mat_ctx->g2g_p);
 98:   PCDestroy(&mat_ctx->pc); /* decrease PCBDDC reference count */
 99:   ISDestroy(&mat_ctx->pressure);
100:   ISDestroy(&mat_ctx->lagrange);
101:   PetscFree(mat_ctx);
102:   return(0);
103: }

105: PetscErrorCode PCBDDCDestroyFETIDPPC(PC pc)
106: {
107:   FETIDPPC_ctx   pc_ctx;

111:   PCShellGetContext(pc,&pc_ctx);
112:   VecDestroy(&pc_ctx->lambda_local);
113:   MatDestroy(&pc_ctx->B_Ddelta);
114:   VecScatterDestroy(&pc_ctx->l2g_lambda);
115:   MatDestroy(&pc_ctx->S_j);
116:   PCDestroy(&pc_ctx->pc); /* decrease PCBDDC reference count */
117:   VecDestroy(&pc_ctx->xPg);
118:   VecDestroy(&pc_ctx->yPg);
119:   PetscFree(pc_ctx);
120:   return(0);
121: }

123: PetscErrorCode PCBDDCSetupFETIDPMatContext(FETIDPMat_ctx fetidpmat_ctx)
124: {
126:   PC_IS          *pcis=(PC_IS*)fetidpmat_ctx->pc->data;
127:   PC_BDDC        *pcbddc=(PC_BDDC*)fetidpmat_ctx->pc->data;
128:   PCBDDCGraph    mat_graph=pcbddc->mat_graph;
129:   Mat_IS         *matis  = (Mat_IS*)fetidpmat_ctx->pc->pmat->data;
130:   MPI_Comm       comm;
131:   Mat            ScalingMat,BD1,BD2;
132:   Vec            fetidp_global;
133:   IS             IS_l2g_lambda;
134:   IS             subset,subset_mult,subset_n,isvert;
135:   PetscBool      skip_node,fully_redundant;
136:   PetscInt       i,j,k,s,n_boundary_dofs,n_global_lambda,n_vertices,partial_sum;
137:   PetscInt       cum,n_local_lambda,n_lambda_for_dof,dual_size,n_neg_values,n_pos_values;
138:   PetscMPIInt    rank,size,buf_size,neigh;
139:   PetscScalar    scalar_value;
140:   const PetscInt *vertex_indices;
141:   PetscInt       *dual_dofs_boundary_indices,*aux_local_numbering_1;
142:   const PetscInt *aux_global_numbering;
143:   PetscInt       *aux_sums,*cols_B_delta,*l2g_indices;
144:   PetscScalar    *array,*scaling_factors,*vals_B_delta;
145:   PetscScalar    **all_factors;
146:   PetscInt       *aux_local_numbering_2;
147:   PetscLayout    llay;

149:   /* saddlepoint */
150:   ISLocalToGlobalMapping l2gmap_p;
151:   PetscLayout            play;
152:   IS                     gP,pP;
153:   PetscInt               nPl,nPg,nPgl;

156:   PetscObjectGetComm((PetscObject)(fetidpmat_ctx->pc),&comm);
157:   MPI_Comm_rank(comm,&rank);
158:   MPI_Comm_size(comm,&size);

160:   /* saddlepoint */
161:   nPl      = 0;
162:   nPg      = 0;
163:   nPgl     = 0;
164:   gP       = NULL;
165:   pP       = NULL;
166:   l2gmap_p = NULL;
167:   play     = NULL;
168:   PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_pP",(PetscObject*)&pP);
169:   if (pP) { /* saddle point */
170:     /* subdomain pressures in global numbering */
171:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_gP",(PetscObject*)&gP);
172:     if (!gP) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"gP not present");
173:     ISGetLocalSize(gP,&nPl);
174:     VecCreate(PETSC_COMM_SELF,&fetidpmat_ctx->vP);
175:     VecSetSizes(fetidpmat_ctx->vP,nPl,nPl);
176:     VecSetType(fetidpmat_ctx->vP,VECSTANDARD);
177:     VecSetUp(fetidpmat_ctx->vP);

179:     /* pressure matrix */
180:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_C",(PetscObject*)&fetidpmat_ctx->C);
181:     if (!fetidpmat_ctx->C) { /* null pressure block, compute layout and global numbering for pressures */
182:       IS Pg;

184:       ISRenumber(gP,NULL,&nPg,&Pg);
185:       ISLocalToGlobalMappingCreateIS(Pg,&l2gmap_p);
186:       ISDestroy(&Pg);
187:       PetscLayoutCreate(comm,&play);
188:       PetscLayoutSetBlockSize(play,1);
189:       PetscLayoutSetSize(play,nPg);
190:       ISGetLocalSize(pP,&nPgl);
191:       PetscLayoutSetLocalSize(play,nPgl);
192:       PetscLayoutSetUp(play);
193:     } else {
194:       PetscObjectReference((PetscObject)fetidpmat_ctx->C);
195:       MatGetLocalToGlobalMapping(fetidpmat_ctx->C,&l2gmap_p,NULL);
196:       PetscObjectReference((PetscObject)l2gmap_p);
197:       MatGetSize(fetidpmat_ctx->C,&nPg,NULL);
198:       MatGetLocalSize(fetidpmat_ctx->C,NULL,&nPgl);
199:       MatGetLayouts(fetidpmat_ctx->C,NULL,&llay);
200:       PetscLayoutReference(llay,&play);
201:     }
202:     VecCreateMPIWithArray(comm,1,nPgl,nPg,NULL,&fetidpmat_ctx->xPg);
203:     VecCreateMPIWithArray(comm,1,nPgl,nPg,NULL,&fetidpmat_ctx->yPg);

205:     /* import matrices for pressures coupling */
206:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_B_BI",(PetscObject*)&fetidpmat_ctx->B_BI);
207:     if (!fetidpmat_ctx->B_BI) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"B_BI not present");
208:     PetscObjectReference((PetscObject)fetidpmat_ctx->B_BI);

210:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_B_BB",(PetscObject*)&fetidpmat_ctx->B_BB);
211:     if (!fetidpmat_ctx->B_BB) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"B_BB not present");
212:     PetscObjectReference((PetscObject)fetidpmat_ctx->B_BB);

214:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_Bt_BI",(PetscObject*)&fetidpmat_ctx->Bt_BI);
215:     if (!fetidpmat_ctx->Bt_BI) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Bt_BI not present");
216:     PetscObjectReference((PetscObject)fetidpmat_ctx->Bt_BI);

218:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_Bt_BB",(PetscObject*)&fetidpmat_ctx->Bt_BB);
219:     if (!fetidpmat_ctx->Bt_BB) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Bt_BB not present");
220:     PetscObjectReference((PetscObject)fetidpmat_ctx->Bt_BB);

222:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_flip" ,(PetscObject*)&fetidpmat_ctx->rhs_flip);
223:     if (fetidpmat_ctx->rhs_flip) {
224:       PetscObjectReference((PetscObject)fetidpmat_ctx->rhs_flip);
225:     }
226:   }

228:   /* Default type of lagrange multipliers is non-redundant */
229:   fully_redundant = fetidpmat_ctx->fully_redundant;

231:   /* Evaluate local and global number of lagrange multipliers */
232:   VecSet(pcis->vec1_N,0.0);
233:   n_local_lambda = 0;
234:   partial_sum = 0;
235:   n_boundary_dofs = 0;
236:   s = 0;

238:   /* Get Vertices used to define the BDDC */
239:   PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&isvert);
240:   ISGetLocalSize(isvert,&n_vertices);
241:   ISGetIndices(isvert,&vertex_indices);

243:   dual_size = pcis->n_B-n_vertices;
244:   PetscMalloc1(dual_size,&dual_dofs_boundary_indices);
245:   PetscMalloc1(dual_size,&aux_local_numbering_1);
246:   PetscMalloc1(dual_size,&aux_local_numbering_2);

248:   VecGetArray(pcis->vec1_N,&array);
249:   for (i=0;i<pcis->n;i++) {
250:     j = mat_graph->count[i]; /* RECALL: mat_graph->count[i] does not count myself */
251:     if (j > 0) n_boundary_dofs++;
252:     skip_node = PETSC_FALSE;
253:     if (s < n_vertices && vertex_indices[s] == i) { /* it works for a sorted set of vertices */
254:       skip_node = PETSC_TRUE;
255:       s++;
256:     }
257:     if (j < 1) skip_node = PETSC_TRUE;
258:     if (mat_graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) skip_node = PETSC_TRUE;
259:     if (!skip_node) {
260:       if (fully_redundant) {
261:         /* fully redundant set of lagrange multipliers */
262:         n_lambda_for_dof = (j*(j+1))/2;
263:       } else {
264:         n_lambda_for_dof = j;
265:       }
266:       n_local_lambda += j;
267:       /* needed to evaluate global number of lagrange multipliers */
268:       array[i]=(1.0*n_lambda_for_dof)/(j+1.0); /* already scaled for the next global sum */
269:       /* store some data needed */
270:       dual_dofs_boundary_indices[partial_sum] = n_boundary_dofs-1;
271:       aux_local_numbering_1[partial_sum] = i;
272:       aux_local_numbering_2[partial_sum] = n_lambda_for_dof;
273:       partial_sum++;
274:     }
275:   }
276:   VecRestoreArray(pcis->vec1_N,&array);
277:   ISRestoreIndices(isvert,&vertex_indices);
278:   PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&isvert);
279:   dual_size = partial_sum;

281:   /* compute global ordering of lagrange multipliers and associate l2g map */
282:   ISCreateGeneral(comm,partial_sum,aux_local_numbering_1,PETSC_COPY_VALUES,&subset_n);
283:   ISLocalToGlobalMappingApplyIS(pcis->mapping,subset_n,&subset);
284:   ISDestroy(&subset_n);
285:   ISCreateGeneral(comm,partial_sum,aux_local_numbering_2,PETSC_OWN_POINTER,&subset_mult);
286:   ISRenumber(subset,subset_mult,&fetidpmat_ctx->n_lambda,&subset_n);
287:   ISDestroy(&subset);

289:   if (PetscDefined(USE_DEBUG)) {
290:     VecSet(pcis->vec1_global,0.0);
291:     VecScatterBegin(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
292:     VecScatterEnd(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
293:     VecSum(pcis->vec1_global,&scalar_value);
294:     i = (PetscInt)PetscRealPart(scalar_value);
295:     if (i != fetidpmat_ctx->n_lambda) SETERRQ2(PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Global number of multipliers mismatch! (%D != %D)",fetidpmat_ctx->n_lambda,i);
296:   }

298:   /* init data for scaling factors exchange */
299:   if (!pcbddc->use_deluxe_scaling) {
300:     PetscInt    *ptrs_buffer,neigh_position;
301:     PetscScalar *send_buffer,*recv_buffer;
302:     MPI_Request *send_reqs,*recv_reqs;

304:     partial_sum = 0;
305:     PetscMalloc1(pcis->n_neigh,&ptrs_buffer);
306:     PetscMalloc1(PetscMax(pcis->n_neigh-1,0),&send_reqs);
307:     PetscMalloc1(PetscMax(pcis->n_neigh-1,0),&recv_reqs);
308:     PetscMalloc1(pcis->n+1,&all_factors);
309:     if (pcis->n_neigh > 0) ptrs_buffer[0]=0;
310:     for (i=1;i<pcis->n_neigh;i++) {
311:       partial_sum += pcis->n_shared[i];
312:       ptrs_buffer[i] = ptrs_buffer[i-1]+pcis->n_shared[i];
313:     }
314:     PetscMalloc1(partial_sum,&send_buffer);
315:     PetscMalloc1(partial_sum,&recv_buffer);
316:     PetscMalloc1(partial_sum,&all_factors[0]);
317:     for (i=0;i<pcis->n-1;i++) {
318:       j = mat_graph->count[i];
319:       all_factors[i+1]=all_factors[i]+j;
320:     }

322:     /* scatter B scaling to N vec */
323:     VecScatterBegin(pcis->N_to_B,pcis->D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
324:     VecScatterEnd(pcis->N_to_B,pcis->D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
325:     /* communications */
326:     VecGetArrayRead(pcis->vec1_N,(const PetscScalar**)&array);
327:     for (i=1;i<pcis->n_neigh;i++) {
328:       for (j=0;j<pcis->n_shared[i];j++) {
329:         send_buffer[ptrs_buffer[i-1]+j]=array[pcis->shared[i][j]];
330:       }
331:       PetscMPIIntCast(ptrs_buffer[i]-ptrs_buffer[i-1],&buf_size);
332:       PetscMPIIntCast(pcis->neigh[i],&neigh);
333:       MPI_Isend(&send_buffer[ptrs_buffer[i-1]],buf_size,MPIU_SCALAR,neigh,0,comm,&send_reqs[i-1]);
334:       MPI_Irecv(&recv_buffer[ptrs_buffer[i-1]],buf_size,MPIU_SCALAR,neigh,0,comm,&recv_reqs[i-1]);
335:     }
336:     VecRestoreArrayRead(pcis->vec1_N,(const PetscScalar**)&array);
337:     if (pcis->n_neigh > 0) {
338:       MPI_Waitall(pcis->n_neigh-1,recv_reqs,MPI_STATUSES_IGNORE);
339:     }
340:     /* put values in correct places */
341:     for (i=1;i<pcis->n_neigh;i++) {
342:       for (j=0;j<pcis->n_shared[i];j++) {
343:         k = pcis->shared[i][j];
344:         neigh_position = 0;
345:         while (mat_graph->neighbours_set[k][neigh_position] != pcis->neigh[i]) {neigh_position++;}
346:         all_factors[k][neigh_position]=recv_buffer[ptrs_buffer[i-1]+j];
347:       }
348:     }
349:     if (pcis->n_neigh > 0) {
350:       MPI_Waitall(pcis->n_neigh-1,send_reqs,MPI_STATUSES_IGNORE);
351:     }
352:     PetscFree(send_reqs);
353:     PetscFree(recv_reqs);
354:     PetscFree(send_buffer);
355:     PetscFree(recv_buffer);
356:     PetscFree(ptrs_buffer);
357:   }

359:   /* Compute B and B_delta (local actions) */
360:   PetscMalloc1(pcis->n_neigh,&aux_sums);
361:   PetscMalloc1(n_local_lambda,&l2g_indices);
362:   PetscMalloc1(n_local_lambda,&vals_B_delta);
363:   PetscMalloc1(n_local_lambda,&cols_B_delta);
364:   if (!pcbddc->use_deluxe_scaling) {
365:     PetscMalloc1(n_local_lambda,&scaling_factors);
366:   } else {
367:     scaling_factors = NULL;
368:     all_factors     = NULL;
369:   }
370:   ISGetIndices(subset_n,&aux_global_numbering);
371:   partial_sum=0;
372:   cum = 0;
373:   for (i=0;i<dual_size;i++) {
374:     n_global_lambda = aux_global_numbering[cum];
375:     j = mat_graph->count[aux_local_numbering_1[i]];
376:     aux_sums[0]=0;
377:     for (s=1;s<j;s++) {
378:       aux_sums[s]=aux_sums[s-1]+j-s+1;
379:     }
380:     if (all_factors) array = all_factors[aux_local_numbering_1[i]];
381:     n_neg_values = 0;
382:     while (n_neg_values < j && mat_graph->neighbours_set[aux_local_numbering_1[i]][n_neg_values] < rank) {n_neg_values++;}
383:     n_pos_values = j - n_neg_values;
384:     if (fully_redundant) {
385:       for (s=0;s<n_neg_values;s++) {
386:         l2g_indices    [partial_sum+s]=aux_sums[s]+n_neg_values-s-1+n_global_lambda;
387:         cols_B_delta   [partial_sum+s]=dual_dofs_boundary_indices[i];
388:         vals_B_delta   [partial_sum+s]=-1.0;
389:         if (!pcbddc->use_deluxe_scaling) scaling_factors[partial_sum+s]=array[s];
390:       }
391:       for (s=0;s<n_pos_values;s++) {
392:         l2g_indices    [partial_sum+s+n_neg_values]=aux_sums[n_neg_values]+s+n_global_lambda;
393:         cols_B_delta   [partial_sum+s+n_neg_values]=dual_dofs_boundary_indices[i];
394:         vals_B_delta   [partial_sum+s+n_neg_values]=1.0;
395:         if (!pcbddc->use_deluxe_scaling) scaling_factors[partial_sum+s+n_neg_values]=array[s+n_neg_values];
396:       }
397:       partial_sum += j;
398:     } else {
399:       /* l2g_indices and default cols and vals of B_delta */
400:       for (s=0;s<j;s++) {
401:         l2g_indices    [partial_sum+s]=n_global_lambda+s;
402:         cols_B_delta   [partial_sum+s]=dual_dofs_boundary_indices[i];
403:         vals_B_delta   [partial_sum+s]=0.0;
404:       }
405:       /* B_delta */
406:       if (n_neg_values > 0) { /* there's a rank next to me to the left */
407:         vals_B_delta   [partial_sum+n_neg_values-1]=-1.0;
408:       }
409:       if (n_neg_values < j) { /* there's a rank next to me to the right */
410:         vals_B_delta   [partial_sum+n_neg_values]=1.0;
411:       }
412:       /* scaling as in Klawonn-Widlund 1999 */
413:       if (!pcbddc->use_deluxe_scaling) {
414:         for (s=0;s<n_neg_values;s++) {
415:           scalar_value = 0.0;
416:           for (k=0;k<s+1;k++) scalar_value += array[k];
417:           scaling_factors[partial_sum+s] = -scalar_value;
418:         }
419:         for (s=0;s<n_pos_values;s++) {
420:           scalar_value = 0.0;
421:           for (k=s+n_neg_values;k<j;k++) scalar_value += array[k];
422:           scaling_factors[partial_sum+s+n_neg_values] = scalar_value;
423:         }
424:       }
425:       partial_sum += j;
426:     }
427:     cum += aux_local_numbering_2[i];
428:   }
429:   ISRestoreIndices(subset_n,&aux_global_numbering);
430:   ISDestroy(&subset_mult);
431:   ISDestroy(&subset_n);
432:   PetscFree(aux_sums);
433:   PetscFree(aux_local_numbering_1);
434:   PetscFree(dual_dofs_boundary_indices);
435:   if (all_factors) {
436:     PetscFree(all_factors[0]);
437:     PetscFree(all_factors);
438:   }

440:   /* Create local part of B_delta */
441:   MatCreate(PETSC_COMM_SELF,&fetidpmat_ctx->B_delta);
442:   MatSetSizes(fetidpmat_ctx->B_delta,n_local_lambda,pcis->n_B,n_local_lambda,pcis->n_B);
443:   MatSetType(fetidpmat_ctx->B_delta,MATSEQAIJ);
444:   MatSeqAIJSetPreallocation(fetidpmat_ctx->B_delta,1,NULL);
445:   MatSetOption(fetidpmat_ctx->B_delta,MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);
446:   for (i=0;i<n_local_lambda;i++) {
447:     MatSetValue(fetidpmat_ctx->B_delta,i,cols_B_delta[i],vals_B_delta[i],INSERT_VALUES);
448:   }
449:   PetscFree(vals_B_delta);
450:   MatAssemblyBegin(fetidpmat_ctx->B_delta,MAT_FINAL_ASSEMBLY);
451:   MatAssemblyEnd(fetidpmat_ctx->B_delta,MAT_FINAL_ASSEMBLY);

453:   BD1 = NULL;
454:   BD2 = NULL;
455:   if (fully_redundant) {
456:     if (pcbddc->use_deluxe_scaling) SETERRQ(comm,PETSC_ERR_SUP,"Deluxe FETIDP with fully-redundant multipliers to be implemented");
457:     MatCreate(PETSC_COMM_SELF,&ScalingMat);
458:     MatSetSizes(ScalingMat,n_local_lambda,n_local_lambda,n_local_lambda,n_local_lambda);
459:     MatSetType(ScalingMat,MATSEQAIJ);
460:     MatSeqAIJSetPreallocation(ScalingMat,1,NULL);
461:     for (i=0;i<n_local_lambda;i++) {
462:       MatSetValue(ScalingMat,i,i,scaling_factors[i],INSERT_VALUES);
463:     }
464:     MatAssemblyBegin(ScalingMat,MAT_FINAL_ASSEMBLY);
465:     MatAssemblyEnd(ScalingMat,MAT_FINAL_ASSEMBLY);
466:     MatMatMult(ScalingMat,fetidpmat_ctx->B_delta,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&fetidpmat_ctx->B_Ddelta);
467:     MatDestroy(&ScalingMat);
468:   } else {
469:     MatCreate(PETSC_COMM_SELF,&fetidpmat_ctx->B_Ddelta);
470:     MatSetSizes(fetidpmat_ctx->B_Ddelta,n_local_lambda,pcis->n_B,n_local_lambda,pcis->n_B);
471:     if (!pcbddc->use_deluxe_scaling || !pcbddc->sub_schurs) {
472:       MatSetType(fetidpmat_ctx->B_Ddelta,MATSEQAIJ);
473:       MatSeqAIJSetPreallocation(fetidpmat_ctx->B_Ddelta,1,NULL);
474:       for (i=0;i<n_local_lambda;i++) {
475:         MatSetValue(fetidpmat_ctx->B_Ddelta,i,cols_B_delta[i],scaling_factors[i],INSERT_VALUES);
476:       }
477:       MatAssemblyBegin(fetidpmat_ctx->B_Ddelta,MAT_FINAL_ASSEMBLY);
478:       MatAssemblyEnd(fetidpmat_ctx->B_Ddelta,MAT_FINAL_ASSEMBLY);
479:     } else {
480:       /* scaling as in Klawonn-Widlund 1999 */
481:       PCBDDCDeluxeScaling deluxe_ctx = pcbddc->deluxe_ctx;
482:       PCBDDCSubSchurs     sub_schurs = pcbddc->sub_schurs;
483:       Mat                 T;
484:       PetscScalar         *W,lwork,*Bwork;
485:       const PetscInt      *idxs = NULL;
486:       PetscInt            cum,mss,*nnz;
487:       PetscBLASInt        *pivots,B_lwork,B_N,B_ierr;

489:       if (!pcbddc->deluxe_singlemat) SETERRQ(comm,PETSC_ERR_USER,"Cannot compute B_Ddelta! rerun with -pc_bddc_deluxe_singlemat");
490:       mss  = 0;
491:       PetscCalloc1(pcis->n_B,&nnz);
492:       if (sub_schurs->is_Ej_all) {
493:         ISGetIndices(sub_schurs->is_Ej_all,&idxs);
494:         for (i=0,cum=0;i<sub_schurs->n_subs;i++) {
495:           PetscInt subset_size;

497:           ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
498:           for (j=0;j<subset_size;j++) nnz[idxs[j+cum]] = subset_size;
499:           mss  = PetscMax(mss,subset_size);
500:           cum += subset_size;
501:         }
502:       }
503:       MatCreate(PETSC_COMM_SELF,&T);
504:       MatSetSizes(T,pcis->n_B,pcis->n_B,pcis->n_B,pcis->n_B);
505:       MatSetType(T,MATSEQAIJ);
506:       MatSeqAIJSetPreallocation(T,0,nnz);
507:       PetscFree(nnz);

509:       /* workspace allocation */
510:       B_lwork = 0;
511:       if (mss) {
512:         PetscScalar dummy = 1;

514:         B_lwork = -1;
515:         PetscBLASIntCast(mss,&B_N);
516:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
517:         PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&B_N,&dummy,&B_N,&B_N,&lwork,&B_lwork,&B_ierr));
518:         PetscFPTrapPop();
519:         if (B_ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in query to GETRI Lapack routine %d",(int)B_ierr);
520:         PetscBLASIntCast((PetscInt)PetscRealPart(lwork),&B_lwork);
521:       }
522:       PetscMalloc3(mss*mss,&W,mss,&pivots,B_lwork,&Bwork);

524:       for (i=0,cum=0;i<sub_schurs->n_subs;i++) {
525:         const PetscScalar *M;
526:         PetscInt          subset_size;

528:         ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
529:         PetscBLASIntCast(subset_size,&B_N);
530:         MatDenseGetArrayRead(deluxe_ctx->seq_mat[i],&M);
531:         PetscArraycpy(W,M,subset_size*subset_size);
532:         MatDenseRestoreArrayRead(deluxe_ctx->seq_mat[i],&M);
533:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
534:         PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&B_N,&B_N,W,&B_N,pivots,&B_ierr));
535:         if (B_ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in GETRF Lapack routine %d",(int)B_ierr);
536:         PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&B_N,W,&B_N,pivots,Bwork,&B_lwork,&B_ierr));
537:         if (B_ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in GETRI Lapack routine %d",(int)B_ierr);
538:         PetscFPTrapPop();
539:         /* silent static analyzer */
540:         if (!idxs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"IDXS not present");
541:         MatSetValues(T,subset_size,idxs+cum,subset_size,idxs+cum,W,INSERT_VALUES);
542:         cum += subset_size;
543:       }
544:       MatAssemblyBegin(T,MAT_FINAL_ASSEMBLY);
545:       MatAssemblyEnd(T,MAT_FINAL_ASSEMBLY);
546:       MatMatTransposeMult(T,fetidpmat_ctx->B_delta,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&BD1);
547:       MatMatMult(fetidpmat_ctx->B_delta,BD1,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&BD2);
548:       MatDestroy(&T);
549:       PetscFree3(W,pivots,Bwork);
550:       if (sub_schurs->is_Ej_all) {
551:         ISRestoreIndices(sub_schurs->is_Ej_all,&idxs);
552:       }
553:     }
554:   }
555:   PetscFree(scaling_factors);
556:   PetscFree(cols_B_delta);

558:   /* Layout of multipliers */
559:   PetscLayoutCreate(comm,&llay);
560:   PetscLayoutSetBlockSize(llay,1);
561:   PetscLayoutSetSize(llay,fetidpmat_ctx->n_lambda);
562:   PetscLayoutSetUp(llay);
563:   PetscLayoutGetLocalSize(llay,&fetidpmat_ctx->n);

565:   /* Local work vector of multipliers */
566:   VecCreate(PETSC_COMM_SELF,&fetidpmat_ctx->lambda_local);
567:   VecSetSizes(fetidpmat_ctx->lambda_local,n_local_lambda,n_local_lambda);
568:   VecSetType(fetidpmat_ctx->lambda_local,VECSEQ);

570:   if (BD2) {
571:     ISLocalToGlobalMapping l2g;
572:     Mat                    T,TA,*pT;
573:     IS                     is;
574:     PetscInt               nl,N;
575:     BDdelta_DN             ctx;

577:     PetscLayoutGetLocalSize(llay,&nl);
578:     PetscLayoutGetSize(llay,&N);
579:     MatCreate(comm,&T);
580:     MatSetSizes(T,nl,nl,N,N);
581:     MatSetType(T,MATIS);
582:     ISLocalToGlobalMappingCreate(comm,1,n_local_lambda,l2g_indices,PETSC_COPY_VALUES,&l2g);
583:     MatSetLocalToGlobalMapping(T,l2g,l2g);
584:     ISLocalToGlobalMappingDestroy(&l2g);
585:     MatISSetLocalMat(T,BD2);
586:     MatAssemblyBegin(T,MAT_FINAL_ASSEMBLY);
587:     MatAssemblyEnd(T,MAT_FINAL_ASSEMBLY);
588:     MatDestroy(&BD2);
589:     MatConvert(T,MATAIJ,MAT_INITIAL_MATRIX,&TA);
590:     MatDestroy(&T);
591:     ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_USE_POINTER,&is);
592:     MatCreateSubMatrices(TA,1,&is,&is,MAT_INITIAL_MATRIX,&pT);
593:     MatDestroy(&TA);
594:     ISDestroy(&is);
595:     BD2  = pT[0];
596:     PetscFree(pT);

598:     /* B_Ddelta for non-redundant multipliers with deluxe scaling */
599:     PetscNew(&ctx);
600:     MatSetType(fetidpmat_ctx->B_Ddelta,MATSHELL);
601:     MatShellSetContext(fetidpmat_ctx->B_Ddelta,ctx);
602:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_MULT,(void (*)(void))MatMult_BDdelta_deluxe_nonred);
603:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_MULT_TRANSPOSE,(void (*)(void))MatMultTranspose_BDdelta_deluxe_nonred);
604:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_DESTROY,(void (*)(void))MatDestroy_BDdelta_deluxe_nonred);
605:     MatSetUp(fetidpmat_ctx->B_Ddelta);

607:     PetscObjectReference((PetscObject)BD1);
608:     ctx->BD = BD1;
609:     KSPCreate(PETSC_COMM_SELF,&ctx->kBD);
610:     KSPSetOperators(ctx->kBD,BD2,BD2);
611:     VecDuplicate(fetidpmat_ctx->lambda_local,&ctx->work);
612:     fetidpmat_ctx->deluxe_nonred = PETSC_TRUE;
613:   }
614:   MatDestroy(&BD1);
615:   MatDestroy(&BD2);

617:   /* fetidpmat sizes */
618:   fetidpmat_ctx->n += nPgl;
619:   fetidpmat_ctx->N  = fetidpmat_ctx->n_lambda+nPg;

621:   /* Global vector for FETI-DP linear system */
622:   VecCreate(comm,&fetidp_global);
623:   VecSetSizes(fetidp_global,fetidpmat_ctx->n,fetidpmat_ctx->N);
624:   VecSetType(fetidp_global,VECMPI);
625:   VecSetUp(fetidp_global);

627:   /* Decide layout for fetidp dofs: if it is a saddle point problem
628:      pressure is ordered first in the local part of the global vector
629:      of the FETI-DP linear system */
630:   if (nPg) {
631:     Vec            v;
632:     IS             IS_l2g_p,ais;
633:     PetscLayout    alay;
634:     const PetscInt *idxs,*pranges,*aranges,*lranges;
635:     PetscInt       *l2g_indices_p,rst;
636:     PetscMPIInt    rank;

638:     PetscMalloc1(nPl,&l2g_indices_p);
639:     VecGetLayout(fetidp_global,&alay);
640:     PetscLayoutGetRanges(alay,&aranges);
641:     PetscLayoutGetRanges(play,&pranges);
642:     PetscLayoutGetRanges(llay,&lranges);

644:     MPI_Comm_rank(PetscObjectComm((PetscObject)fetidp_global),&rank);
645:     ISCreateStride(PetscObjectComm((PetscObject)fetidp_global),pranges[rank+1]-pranges[rank],aranges[rank],1,&fetidpmat_ctx->pressure);
646:     PetscObjectSetName((PetscObject)fetidpmat_ctx->pressure,"F_P");
647:     ISCreateStride(PetscObjectComm((PetscObject)fetidp_global),lranges[rank+1]-lranges[rank],aranges[rank]+pranges[rank+1]-pranges[rank],1,&fetidpmat_ctx->lagrange);
648:     PetscObjectSetName((PetscObject)fetidpmat_ctx->lagrange,"F_L");
649:     ISLocalToGlobalMappingGetIndices(l2gmap_p,&idxs);
650:     /* shift local to global indices for pressure */
651:     for (i=0;i<nPl;i++) {
652:       PetscMPIInt owner;

654:       PetscLayoutFindOwner(play,idxs[i],&owner);
655:       l2g_indices_p[i] = idxs[i]-pranges[owner]+aranges[owner];
656:     }
657:     ISLocalToGlobalMappingRestoreIndices(l2gmap_p,&idxs);
658:     ISCreateGeneral(comm,nPl,l2g_indices_p,PETSC_OWN_POINTER,&IS_l2g_p);

660:     /* local to global scatter for pressure */
661:     VecScatterCreate(fetidpmat_ctx->vP,NULL,fetidp_global,IS_l2g_p,&fetidpmat_ctx->l2g_p);
662:     ISDestroy(&IS_l2g_p);

664:     /* scatter for lagrange multipliers only */
665:     VecCreate(comm,&v);
666:     VecSetType(v,VECSTANDARD);
667:     VecSetLayout(v,llay);
668:     VecSetUp(v);
669:     ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_COPY_VALUES,&ais);
670:     VecScatterCreate(fetidpmat_ctx->lambda_local,NULL,v,ais,&fetidpmat_ctx->l2g_lambda_only);
671:     ISDestroy(&ais);
672:     VecDestroy(&v);

674:     /* shift local to global indices for multipliers */
675:     for (i=0;i<n_local_lambda;i++) {
676:       PetscInt    ps;
677:       PetscMPIInt owner;

679:       PetscLayoutFindOwner(llay,l2g_indices[i],&owner);
680:       ps = pranges[owner+1]-pranges[owner];
681:       l2g_indices[i] = l2g_indices[i]-lranges[owner]+aranges[owner]+ps;
682:     }

684:     /* scatter from alldofs to pressures global fetidp vector */
685:     PetscLayoutGetRange(alay,&rst,NULL);
686:     ISCreateStride(comm,nPgl,rst,1,&ais);
687:     VecScatterCreate(pcis->vec1_global,pP,fetidp_global,ais,&fetidpmat_ctx->g2g_p);
688:     ISDestroy(&ais);
689:   }
690:   PetscLayoutDestroy(&llay);
691:   PetscLayoutDestroy(&play);
692:   ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_OWN_POINTER,&IS_l2g_lambda);

694:   /* scatter from local to global multipliers */
695:   VecScatterCreate(fetidpmat_ctx->lambda_local,NULL,fetidp_global,IS_l2g_lambda,&fetidpmat_ctx->l2g_lambda);
696:   ISDestroy(&IS_l2g_lambda);
697:   ISLocalToGlobalMappingDestroy(&l2gmap_p);
698:   VecDestroy(&fetidp_global);

700:   /* Create some work vectors needed by fetidp */
701:   VecDuplicate(pcis->vec1_B,&fetidpmat_ctx->temp_solution_B);
702:   VecDuplicate(pcis->vec1_D,&fetidpmat_ctx->temp_solution_D);
703:   return(0);
704: }

706: PetscErrorCode PCBDDCSetupFETIDPPCContext(Mat fetimat, FETIDPPC_ctx fetidppc_ctx)
707: {
708:   FETIDPMat_ctx  mat_ctx;
709:   PC_BDDC        *pcbddc = (PC_BDDC*)fetidppc_ctx->pc->data;
710:   PC_IS          *pcis = (PC_IS*)fetidppc_ctx->pc->data;
711:   PetscBool      lumped = PETSC_FALSE;

715:   MatShellGetContext(fetimat,&mat_ctx);
716:   /* get references from objects created when setting up feti mat context */
717:   PetscObjectReference((PetscObject)mat_ctx->lambda_local);
718:   fetidppc_ctx->lambda_local = mat_ctx->lambda_local;
719:   PetscObjectReference((PetscObject)mat_ctx->B_Ddelta);
720:   fetidppc_ctx->B_Ddelta = mat_ctx->B_Ddelta;
721:   if (mat_ctx->deluxe_nonred) {
722:     PC               pc,mpc;
723:     BDdelta_DN       ctx;
724:     MatSolverType    solver;
725:     const char       *prefix;

727:     MatShellGetContext(mat_ctx->B_Ddelta,&ctx);
728:     KSPSetType(ctx->kBD,KSPPREONLY);
729:     KSPGetPC(ctx->kBD,&mpc);
730:     KSPGetPC(pcbddc->ksp_D,&pc);
731:     PCSetType(mpc,PCLU);
732:     PCFactorGetMatSolverType(pc,(MatSolverType*)&solver);
733:     if (solver) {
734:       PCFactorSetMatSolverType(mpc,solver);
735:     }
736:     MatGetOptionsPrefix(fetimat,&prefix);
737:     KSPSetOptionsPrefix(ctx->kBD,prefix);
738:     KSPAppendOptionsPrefix(ctx->kBD,"bddelta_");
739:     KSPSetFromOptions(ctx->kBD);
740:   }

742:   if (mat_ctx->l2g_lambda_only) {
743:     PetscObjectReference((PetscObject)mat_ctx->l2g_lambda_only);
744:     fetidppc_ctx->l2g_lambda = mat_ctx->l2g_lambda_only;
745:   } else {
746:     PetscObjectReference((PetscObject)mat_ctx->l2g_lambda);
747:     fetidppc_ctx->l2g_lambda = mat_ctx->l2g_lambda;
748:   }
749:   /* Dirichlet preconditioner */
750:   PetscOptionsGetBool(NULL,((PetscObject)fetimat)->prefix,"-pc_lumped",&lumped,NULL);
751:   if (!lumped) {
752:     IS        iV;
753:     PetscBool discrete_harmonic = PETSC_FALSE;

755:     PetscObjectQuery((PetscObject)fetidppc_ctx->pc,"__KSPFETIDP_iV",(PetscObject*)&iV);
756:     if (iV) {
757:       PetscOptionsGetBool(NULL,((PetscObject)fetimat)->prefix,"-pc_discrete_harmonic",&discrete_harmonic,NULL);
758:     }
759:     if (discrete_harmonic) {
760:       KSP             sksp;
761:       PC              pc;
762:       PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
763:       Mat             A_II,A_IB,A_BI;
764:       IS              iP = NULL;
765:       PetscBool       isshell,reuse = PETSC_FALSE;
766:       KSPType         ksptype;
767:       const char      *prefix;

769:       /*
770:         We constructs a Schur complement for

772:         | A_II A_ID |
773:         | A_DI A_DD |

775:         instead of

777:         | A_II  B^t_II A_ID |
778:         | B_II -C_II   B_ID |
779:         | A_DI  B^t_ID A_DD |

781:       */
782:       if (sub_schurs && sub_schurs->reuse_solver) {
783:         PetscObjectQuery((PetscObject)sub_schurs->A,"__KSPFETIDP_iP",(PetscObject*)&iP);
784:         if (iP) reuse = PETSC_TRUE;
785:       }
786:       if (!reuse) {
787:         IS       aB;
788:         PetscInt nb;
789:         ISGetLocalSize(pcis->is_B_local,&nb);
790:         ISCreateStride(PetscObjectComm((PetscObject)pcis->A_II),nb,0,1,&aB);
791:         MatCreateSubMatrix(pcis->A_II,iV,iV,MAT_INITIAL_MATRIX,&A_II);
792:         MatCreateSubMatrix(pcis->A_IB,iV,aB,MAT_INITIAL_MATRIX,&A_IB);
793:         MatCreateSubMatrix(pcis->A_BI,aB,iV,MAT_INITIAL_MATRIX,&A_BI);
794:         ISDestroy(&aB);
795:       } else {
796:         MatCreateSubMatrix(sub_schurs->A,pcis->is_I_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&A_IB);
797:         MatCreateSubMatrix(sub_schurs->A,pcis->is_B_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&A_BI);
798:         PetscObjectReference((PetscObject)pcis->A_II);
799:         A_II = pcis->A_II;
800:       }
801:       MatCreateSchurComplement(A_II,A_II,A_IB,A_BI,pcis->A_BB,&fetidppc_ctx->S_j);

803:       /* propagate settings of solver */
804:       MatSchurComplementGetKSP(fetidppc_ctx->S_j,&sksp);
805:       KSPGetType(pcis->ksp_D,&ksptype);
806:       KSPSetType(sksp,ksptype);
807:       KSPGetPC(pcis->ksp_D,&pc);
808:       PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&isshell);
809:       if (!isshell) {
810:         MatSolverType    solver;
811:         PCType           pctype;

813:         PCGetType(pc,&pctype);
814:         PCFactorGetMatSolverType(pc,(MatSolverType*)&solver);
815:         KSPGetPC(sksp,&pc);
816:         PCSetType(pc,pctype);
817:         if (solver) {
818:           PCFactorSetMatSolverType(pc,solver);
819:         }
820:       } else {
821:         KSPGetPC(sksp,&pc);
822:         PCSetType(pc,PCLU);
823:       }
824:       MatDestroy(&A_II);
825:       MatDestroy(&A_IB);
826:       MatDestroy(&A_BI);
827:       MatGetOptionsPrefix(fetimat,&prefix);
828:       KSPSetOptionsPrefix(sksp,prefix);
829:       KSPAppendOptionsPrefix(sksp,"harmonic_");
830:       KSPSetFromOptions(sksp);
831:       if (reuse) {
832:         KSPSetPC(sksp,sub_schurs->reuse_solver->interior_solver);
833:         PetscObjectIncrementTabLevel((PetscObject)sub_schurs->reuse_solver->interior_solver,(PetscObject)sksp,0);
834:       }
835:     } else { /* default Dirichlet preconditioner is pde-harmonic */
836:       MatCreateSchurComplement(pcis->A_II,pcis->A_II,pcis->A_IB,pcis->A_BI,pcis->A_BB,&fetidppc_ctx->S_j);
837:       MatSchurComplementSetKSP(fetidppc_ctx->S_j,pcis->ksp_D);
838:     }
839:   } else {
840:     PetscObjectReference((PetscObject)pcis->A_BB);
841:     fetidppc_ctx->S_j = pcis->A_BB;
842:   }
843:   /* saddle-point */
844:   if (mat_ctx->xPg) {
845:     PetscObjectReference((PetscObject)mat_ctx->xPg);
846:     fetidppc_ctx->xPg = mat_ctx->xPg;
847:     PetscObjectReference((PetscObject)mat_ctx->yPg);
848:     fetidppc_ctx->yPg = mat_ctx->yPg;
849:   }
850:   return(0);
851: }

853: PetscErrorCode FETIDPMatMult_Kernel(Mat fetimat, Vec x, Vec y, PetscBool trans)
854: {
855:   FETIDPMat_ctx  mat_ctx;
856:   PC_BDDC        *pcbddc;
857:   PC_IS          *pcis;

861:   MatShellGetContext(fetimat,&mat_ctx);
862:   pcis = (PC_IS*)mat_ctx->pc->data;
863:   pcbddc = (PC_BDDC*)mat_ctx->pc->data;
864:   /* Application of B_delta^T */
865:   VecSet(pcis->vec1_B,0.);
866:   VecScatterBegin(mat_ctx->l2g_lambda,x,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
867:   VecScatterEnd(mat_ctx->l2g_lambda,x,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
868:   MatMultTranspose(mat_ctx->B_delta,mat_ctx->lambda_local,pcis->vec1_B);

870:   /* Add contribution from saddle point */
871:   if (mat_ctx->l2g_p) {
872:     VecScatterBegin(mat_ctx->l2g_p,x,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
873:     VecScatterEnd(mat_ctx->l2g_p,x,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
874:     if (pcbddc->switch_static) {
875:       if (trans) {
876:         MatMultTranspose(mat_ctx->B_BI,mat_ctx->vP,pcis->vec1_D);
877:       } else {
878:         MatMult(mat_ctx->Bt_BI,mat_ctx->vP,pcis->vec1_D);
879:       }
880:     }
881:     if (trans) {
882:       MatMultTransposeAdd(mat_ctx->B_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
883:     } else {
884:       MatMultAdd(mat_ctx->Bt_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
885:     }
886:   } else {
887:     if (pcbddc->switch_static) {
888:       VecSet(pcis->vec1_D,0.0);
889:     }
890:   }
891:   /* Application of \widetilde{S}^-1 */
892:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
893:   PCBDDCApplyInterfacePreconditioner(mat_ctx->pc,trans);
894:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
895:   VecSet(y,0.0);
896:   /* Application of B_delta */
897:   MatMult(mat_ctx->B_delta,pcis->vec1_B,mat_ctx->lambda_local);
898:   /* Contribution from boundary pressures */
899:   if (mat_ctx->C) {
900:     const PetscScalar *lx;
901:     PetscScalar       *ly;

903:     /* pressure ordered first in the local part of x and y */
904:     VecGetArrayRead(x,&lx);
905:     VecGetArray(y,&ly);
906:     VecPlaceArray(mat_ctx->xPg,lx);
907:     VecPlaceArray(mat_ctx->yPg,ly);
908:     if (trans) {
909:       MatMultTranspose(mat_ctx->C,mat_ctx->xPg,mat_ctx->yPg);
910:     } else {
911:       MatMult(mat_ctx->C,mat_ctx->xPg,mat_ctx->yPg);
912:     }
913:     VecResetArray(mat_ctx->xPg);
914:     VecResetArray(mat_ctx->yPg);
915:     VecRestoreArrayRead(x,&lx);
916:     VecRestoreArray(y,&ly);
917:   }
918:   /* Add contribution from saddle point */
919:   if (mat_ctx->l2g_p) {
920:     if (trans) {
921:       MatMultTranspose(mat_ctx->Bt_BB,pcis->vec1_B,mat_ctx->vP);
922:     } else {
923:       MatMult(mat_ctx->B_BB,pcis->vec1_B,mat_ctx->vP);
924:     }
925:     if (pcbddc->switch_static) {
926:       if (trans) {
927:         MatMultTransposeAdd(mat_ctx->Bt_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
928:       } else {
929:         MatMultAdd(mat_ctx->B_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
930:       }
931:     }
932:     VecScatterBegin(mat_ctx->l2g_p,mat_ctx->vP,y,ADD_VALUES,SCATTER_FORWARD);
933:     VecScatterEnd(mat_ctx->l2g_p,mat_ctx->vP,y,ADD_VALUES,SCATTER_FORWARD);
934:   }
935:   VecScatterBegin(mat_ctx->l2g_lambda,mat_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
936:   VecScatterEnd(mat_ctx->l2g_lambda,mat_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
937:   return(0);
938: }

940: PetscErrorCode FETIDPMatMult(Mat fetimat, Vec x, Vec y)
941: {

945:   FETIDPMatMult_Kernel(fetimat,x,y,PETSC_FALSE);
946:   return(0);
947: }

949: PetscErrorCode FETIDPMatMultTranspose(Mat fetimat, Vec x, Vec y)
950: {

954:   FETIDPMatMult_Kernel(fetimat,x,y,PETSC_TRUE);
955:   return(0);
956: }

958: PetscErrorCode FETIDPPCApply_Kernel(PC fetipc, Vec x, Vec y, PetscBool trans)
959: {
960:   FETIDPPC_ctx   pc_ctx;
961:   PC_IS          *pcis;

965:   PCShellGetContext(fetipc,&pc_ctx);
966:   pcis = (PC_IS*)pc_ctx->pc->data;
967:   /* Application of B_Ddelta^T */
968:   VecScatterBegin(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
969:   VecScatterEnd(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
970:   VecSet(pcis->vec2_B,0.0);
971:   MatMultTranspose(pc_ctx->B_Ddelta,pc_ctx->lambda_local,pcis->vec2_B);
972:   /* Application of local Schur complement */
973:   if (trans) {
974:     MatMultTranspose(pc_ctx->S_j,pcis->vec2_B,pcis->vec1_B);
975:   } else {
976:     MatMult(pc_ctx->S_j,pcis->vec2_B,pcis->vec1_B);
977:   }
978:   /* Application of B_Ddelta */
979:   MatMult(pc_ctx->B_Ddelta,pcis->vec1_B,pc_ctx->lambda_local);
980:   VecSet(y,0.0);
981:   VecScatterBegin(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
982:   VecScatterEnd(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
983:   return(0);
984: }

986: PetscErrorCode FETIDPPCApply(PC pc, Vec x, Vec y)
987: {

991:   FETIDPPCApply_Kernel(pc,x,y,PETSC_FALSE);
992:   return(0);
993: }

995: PetscErrorCode FETIDPPCApplyTranspose(PC pc, Vec x, Vec y)
996: {

1000:   FETIDPPCApply_Kernel(pc,x,y,PETSC_TRUE);
1001:   return(0);
1002: }

1004: PetscErrorCode FETIDPPCView(PC pc, PetscViewer viewer)
1005: {
1006:   FETIDPPC_ctx      pc_ctx;
1007:   PetscBool         iascii;
1008:   PetscViewer       sviewer;
1009:   PetscErrorCode    ierr;

1012:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1013:   if (iascii) {
1014:     PetscMPIInt rank;
1015:     PetscBool   isschur,isshell;

1017:     PCShellGetContext(pc,&pc_ctx);
1018:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
1019:     PetscObjectTypeCompare((PetscObject)pc_ctx->S_j,MATSCHURCOMPLEMENT,&isschur);
1020:     if (isschur) {
1021:       PetscViewerASCIIPrintf(viewer,"  Dirichlet preconditioner (just from rank 0)\n");
1022:     } else {
1023:       PetscViewerASCIIPrintf(viewer,"  Lumped preconditioner (just from rank 0)\n");
1024:     }
1025:     PetscViewerGetSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1026:     if (rank == 0) {
1027:       PetscViewerPushFormat(sviewer,PETSC_VIEWER_ASCII_INFO);
1028:       PetscViewerASCIIPushTab(sviewer);
1029:       MatView(pc_ctx->S_j,sviewer);
1030:       PetscViewerASCIIPopTab(sviewer);
1031:       PetscViewerPopFormat(sviewer);
1032:     }
1033:     PetscViewerRestoreSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1034:     PetscObjectTypeCompare((PetscObject)pc_ctx->B_Ddelta,MATSHELL,&isshell);
1035:     if (isshell) {
1036:       BDdelta_DN ctx;
1037:       PetscViewerASCIIPrintf(viewer,"  FETI-DP BDdelta: DB^t * (B D^-1 B^t)^-1 for deluxe scaling (just from rank 0)\n");
1038:       MatShellGetContext(pc_ctx->B_Ddelta,&ctx);
1039:       PetscViewerGetSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1040:       if (rank == 0) {
1041:         PetscInt tl;

1043:         PetscViewerASCIIGetTab(sviewer,&tl);
1044:         PetscObjectSetTabLevel((PetscObject)ctx->kBD,tl);
1045:         KSPView(ctx->kBD,sviewer);
1046:         PetscViewerPushFormat(sviewer,PETSC_VIEWER_ASCII_INFO);
1047:         MatView(ctx->BD,sviewer);
1048:         PetscViewerPopFormat(sviewer);
1049:       }
1050:       PetscViewerRestoreSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1051:     }
1052:     PetscViewerFlush(viewer);
1053:   }
1054:   return(0);
1055: }