Actual source code: vecmpitoseq.c

petsc-3.3-p7 2013-05-11
  2: #include <petsc-private/vecimpl.h>    /*I "petscvec.h" I*/

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

 10:   Collective on Vec

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

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

 19:   Level: intermediate

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

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

 35:     Do NOT create a vector and then pass it in as the final argument vout! vout is created by this routine
 36:   automatically (unless you pass PETSC_NULL in for that argument if you do not need it).

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

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

 45:   PetscInt       N;
 46:   IS             is;
 47:   Vec            tmp;
 48:   Vec           *tmpv;
 49:   PetscBool      tmpvout = PETSC_FALSE;

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

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


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

 81:   Collective on Vec

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

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

 91:   Level: intermediate

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

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

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

109:     Do NOT create a vector and then pass it in as the final argument vout! vout is created by this routine
110:   automatically (unless you pass PETSC_NULL in for that argument if you do not need it).

112: @*/
113: PetscErrorCode  VecScatterCreateToZero(Vec vin,VecScatter *ctx,Vec *vout)
114: {

117:   PetscInt       N;
118:   PetscMPIInt    rank;
119:   IS             is;
120:   Vec            tmp;
121:   Vec           *tmpv;
122:   PetscBool      tmpvout = PETSC_FALSE;

128:   if (vout) {
130:     tmpv = vout;
131:   } else {
132:     tmpvout = PETSC_TRUE;
133:     tmpv = &tmp;
134:   }

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