Actual source code: vecmpitoseq.c

petsc-3.14.6 2021-03-30
Report Typos and Errors

  2: #include <petsc/private/vecimpl.h>

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

  8:   Collective on Vec

 10:   Input Parameter:
 11: .  vin  - input MPIVEC

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

 17:   Level: intermediate

 19:    Note: vout may be NULL [PETSC_NULL_VEC from fortran] if you do not
 20:    need to have it created

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

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

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

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

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

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

 61:   /* Create seq vec on each proc, with the same size of the original mpi vec */
 62:   VecGetSize(vin,&N);
 63:   VecCreateSeq(PETSC_COMM_SELF,N,tmpv);
 64:   VecSetFromOptions(*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: }


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

 78:   Collective on Vec

 80:   Input Parameter:
 81: .  vin  - input MPIVEC

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

 88:   Level: intermediate

 90:    Note: vout may be NULL [PETSC_NULL_VEC from fortran] if you do not
 91:    need to have it created

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

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

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

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

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

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

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