Actual source code: vecmpitoseq.c

      1: #define PETSCVEC_DLL

     3:  #include vecimpl.h

      7: /*@C
      8:       VecScatterCreateToAll - Creates a scatter context that copies all
      9:           vector values to each processor

     11:   Collective

     13:   Input Parameter:
     14: .  vin  - input MPIVEC

     16:   Output Parameter:
     17: +  ctx - scatter context
     18: -  vout - output SEQVEC that is large enough to scatter into

     20:   Level: intermediate

     22:    Usage:
     23: $        VecScatterCreateToAll(vin,&ctx,&vout);
     24: $
     25: $        // scatter as many times as you need
     26: $        VecScatterBegin(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
     27: $        VecScatterEnd(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
     28: $
     29: $        // destroy scatter context and local vector when no longer needed
     30: $        VecScatterDestroy(ctx);
     31: $        VecDestroy(vout);

     33: .seealso VecScatterCreate(), VecScatterCreateToZero(), VecScatterBegin(), VecScatterEnd()

     35: @*/
     36: PetscErrorCode PETSCVEC_DLLEXPORT VecScatterCreateToAll(Vec vin,VecScatter *ctx,Vec *vout)
     37: {

     40:   PetscInt       N;
     41:   IS             is;

     49:   /* Create seq vec on each proc, with the same size of the original mpi vec */
     50:   VecGetSize(vin,&N);
     51:   VecCreateSeq(PETSC_COMM_SELF,N,vout);
     52:   /* Create the VecScatter ctx with the communication info */
     53:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
     54:   VecScatterCreate(vin,is,*vout,is,ctx);
     55:   ISDestroy(is);
     56:   return(0);
     57: }

     62: /*@C
     63:       VecScatterCreateToZero - Creates a scatter context that copies all
     64:           vector values to a vector on the zeroth processor

     66:   Collective

     68:   Input Parameter:
     69: .  vin  - input MPIVEC

     71:   Output Parameter:
     72: +  ctx - scatter context
     73: -  vout - output MPIVEC that is large enough to scatter into on processor 0 and
     74:           of length zero on all other processors

     76:   Level: intermediate

     78:    Usage:
     79: $        VecScatterCreateToZero(vin,&ctx,&vout);
     80: $
     81: $        // scatter as many times as you need
     82: $        VecScatterBegin(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
     83: $        VecScatterEnd(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
     84: $
     85: $        // destroy scatter context and local vector when no longer needed
     86: $        VecScatterDestroy(ctx);
     87: $        VecDestroy(vout);

     89:    Note: If you want to treat the vector on processor zero as a sequential vector call
     90:          VecGetArray() on it and create a sequential vector with VecCreateSeqWithArray().

     92: .seealso VecScatterCreate(), VecScatterCreateToAll(), VecScatterBegin(), VecScatterEnd()

     94: @*/
     95: PetscErrorCode PETSCVEC_DLLEXPORT VecScatterCreateToZero(Vec vin,VecScatter *ctx,Vec *vout)
     96: {

     99:   PetscInt       N;
    100:   PetscMPIInt    rank;
    101:   IS             is;

    109:   /* Create seq vec on each proc, with the same size of the original mpi vec */
    110:   VecGetSize(vin,&N);
    111:   MPI_Comm_rank(vin->comm,&rank);
    112:   if (!rank) {
    113:     VecCreateMPI(vin->comm,N,N,vout);
    114:   } else {
    115:     VecCreateMPI(vin->comm,0,N,vout);
    116:   }
    117:   /* Create the VecScatter ctx with the communication info */
    118:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
    119:   VecScatterCreate(vin,is,*vout,is,ctx);
    120:   ISDestroy(is);
    121:   return(0);
    122: }