Actual source code: vecmpitoseq.c

  1: #define PETSCVEC_DLL

 3:  #include private/vecimpl.h

  7: /*@
  8:       VecScatterCreateToAll - Creates a vector and 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:    Note: vout may be PETSC_NULL [PETSC_NULL_OBJECT from fortran] if you do not 
 23:    need to have it created

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


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

 39: @*/
 40: PetscErrorCode  VecScatterCreateToAll(Vec vin,VecScatter *ctx,Vec *vout)
 41: {

 44:   PetscInt       N;
 45:   IS             is;
 46:   PetscTruth     tmpvout = PETSC_FALSE;

 53:   else tmpvout = PETSC_TRUE;

 55:   /* Create seq vec on each proc, with the same size of the original mpi vec */
 56:   VecGetSize(vin,&N);
 57:   VecCreateSeq(PETSC_COMM_SELF,N,vout);
 58:   /* Create the VecScatter ctx with the communication info */
 59:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
 60:   VecScatterCreate(vin,is,*vout,is,ctx);
 61:   ISDestroy(is);
 62:   if (tmpvout) {VecDestroy(*vout);}
 63:   return(0);
 64: }


 69: /*@
 70:       VecScatterCreateToZero - Creates an output vector and a scatter context used to 
 71:               copy all vector values into the output vector on the zeroth processor

 73:   Collective

 75:   Input Parameter: 
 76: .  vin  - input MPIVEC

 78:   Output Parameter:
 79: +  ctx - scatter context
 80: -  vout - output SEQVEC that is large enough to scatter into on processor 0 and
 81:           of length zero on all other processors

 83:   Level: intermediate

 85:    Note: vout may be PETSC_NULL [PETSC_NULL_OBJECT from fortran] if you do not 
 86:    need to have it created

 88:    Usage:
 89: $        VecScatterCreateToZero(vin,&ctx,&vout);
 90: $
 91: $        // scatter as many times as you need 
 92: $        VecScatterBegin(ctx,vin,vout,INSERT_VALUES,SCATTER_FORWARD);
 93: $        VecScatterEnd(ctx,vin,vout,INSERT_VALUES,SCATTER_FORWARD);
 94: $
 95: $        // destroy scatter context and local vector when no longer needed
 96: $        VecScatterDestroy(ctx);
 97: $        VecDestroy(vout);

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

101: @*/
102: PetscErrorCode  VecScatterCreateToZero(Vec vin,VecScatter *ctx,Vec *vout)
103: {

106:   PetscInt       N;
107:   PetscMPIInt    rank;
108:   IS             is;
109:   PetscTruth     tmpvout = PETSC_FALSE;

116:   else tmpvout = PETSC_TRUE;

118:   /* Create vec on each proc, with the same size of the original mpi vec (all on process 0)*/
119:   VecGetSize(vin,&N);
120:   MPI_Comm_rank(((PetscObject)vin)->comm,&rank);
121:   if (rank) N = 0;
122:   VecCreateSeq(PETSC_COMM_SELF,N,vout);
123:   /* Create the VecScatter ctx with the communication info */
124:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
125:   VecScatterCreate(vin,is,*vout,is,ctx);
126:   ISDestroy(is);
127:   if (tmpvout) {VecDestroy(*vout);}
128:   return(0);
129: }