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:   Vec            tmp;
 47:   Vec           *tmpv;
 48:   PetscTruth     tmpvout = PETSC_FALSE;

 54:   if (vout) {
 56:     tmpv = vout;
 57:   } else {
 58:     tmpvout = PETSC_TRUE;
 59:     tmpv = &tmp;
 60:   }

 62:   /* Create seq vec on each proc, with the same size of the original mpi vec */
 63:   VecGetSize(vin,&N);
 64:   VecCreateSeq(PETSC_COMM_SELF,N,tmpv);
 65:   /* Create the VecScatter ctx with the communication info */
 66:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
 67:   VecScatterCreate(vin,is,*tmpv,is,ctx);
 68:   ISDestroy(is);
 69:   if (tmpvout) {VecDestroy(*tmpv);}
 70:   return(0);
 71: }


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

 80:   Collective

 82:   Input Parameter: 
 83: .  vin  - input MPIVEC

 85:   Output Parameter:
 86: +  ctx - scatter context
 87: -  vout - output SEQVEC that is large enough to scatter into on processor 0 and
 88:           of length zero on all other processors

 90:   Level: intermediate

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

 95:    Usage:
 96: $        VecScatterCreateToZero(vin,&ctx,&vout);
 97: $
 98: $        // scatter as many times as you need 
 99: $        VecScatterBegin(ctx,vin,vout,INSERT_VALUES,SCATTER_FORWARD);
100: $        VecScatterEnd(ctx,vin,vout,INSERT_VALUES,SCATTER_FORWARD);
101: $
102: $        // destroy scatter context and local vector when no longer needed
103: $        VecScatterDestroy(ctx);
104: $        VecDestroy(vout);

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

108: @*/
109: PetscErrorCode  VecScatterCreateToZero(Vec vin,VecScatter *ctx,Vec *vout)
110: {

113:   PetscInt       N;
114:   PetscMPIInt    rank;
115:   IS             is;
116:   Vec            tmp;
117:   Vec           *tmpv;
118:   PetscTruth     tmpvout = PETSC_FALSE;

124:   if (vout) {
126:     tmpv = vout;
127:   } else {
128:     tmpvout = PETSC_TRUE;
129:     tmpv = &tmp;
130:   }

132:   /* Create vec on each proc, with the same size of the original mpi vec (all on process 0)*/
133:   VecGetSize(vin,&N);
134:   MPI_Comm_rank(((PetscObject)vin)->comm,&rank);
135:   if (rank) N = 0;
136:   VecCreateSeq(PETSC_COMM_SELF,N,tmpv);
137:   /* Create the VecScatter ctx with the communication info */
138:   ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
139:   VecScatterCreate(vin,is,*tmpv,is,ctx);
140:   ISDestroy(is);
141:   if (tmpvout) {VecDestroy(*tmpv);}
142:   return(0);
143: }