Actual source code: vecmpitoseq.c
1: #include "vecimpl.h"
5: /*@C
6: VecScatterCreateToAll - Creates a scatter context that copies all
7: vector values to each processor
9: Collective
11: Input Parameter:
12: . vin - input MPIVEC
14: Output Parameter:
15: + ctx - scatter context
16: - vout - output SEQVEC that is large enough to scatter into
18: Level: intermediate
20: Usage:
21: $ VecScatterCreateToAll(vin,&ctx,&vout);
22: $
23: $ // scatter as many times as you need
24: $ VecScatterBegin(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
25: $ VecScatterEnd(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
26: $
27: $ // destroy scatter context and local vector when no longer needed
28: $ VecScatterDestroy(ctx);
29: $ VecDestroy(vout);
31: .seealso VecScatterCreate(), VecScatterCreateToZero(), VecScatterBegin(), VecScatterEnd()
33: @*/
34: PetscErrorCode VecScatterCreateToAll(Vec vin,VecScatter *ctx,Vec *vout)
35: {
38: PetscInt N;
39: IS is;
47: /* Create seq vec on each proc, with the same size of the original mpi vec */
48: VecGetSize(vin,&N);
49: VecCreateSeq(PETSC_COMM_SELF,N,vout);
50: /* Create the VecScatter ctx with the communication info */
51: ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
52: VecScatterCreate(vin,is,*vout,is,ctx);
53: ISDestroy(is);
54: return(0);
55: }
60: /*@C
61: VecScatterCreateToZero - Creates a scatter context that copies all
62: vector values to a vector on the zeroth processor
64: Collective
66: Input Parameter:
67: . vin - input MPIVEC
69: Output Parameter:
70: + ctx - scatter context
71: - vout - output MPIVEC that is large enough to scatter into on processor 0 and
72: of length zero on all other processors
74: Level: intermediate
76: Usage:
77: $ VecScatterCreateToZero(vin,&ctx,&vout);
78: $
79: $ // scatter as many times as you need
80: $ VecScatterBegin(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
81: $ VecScatterEnd(vin,vout,INSERT_VALUES,SCATTER_FORWARD,ctx);
82: $
83: $ // destroy scatter context and local vector when no longer needed
84: $ VecScatterDestroy(ctx);
85: $ VecDestroy(vout);
87: Note: If you want to treat the vector on processor zero as a sequential vector call
88: VecGetArray() on it and create a sequential vector with VecCreateSeqWithArray().
90: .seealso VecScatterCreate(), VecScatterCreateToAll(), VecScatterBegin(), VecScatterEnd()
92: @*/
93: PetscErrorCode VecScatterCreateToZero(Vec vin,VecScatter *ctx,Vec *vout)
94: {
97: PetscInt N;
98: PetscMPIInt rank;
99: IS is;
107: /* Create seq vec on each proc, with the same size of the original mpi vec */
108: VecGetSize(vin,&N);
109: MPI_Comm_rank(vin->comm,&rank);
110: if (!rank) {
111: VecCreateMPI(vin->comm,N,N,vout);
112: } else {
113: VecCreateMPI(vin->comm,0,N,vout);
114: }
115: /* Create the VecScatter ctx with the communication info */
116: ISCreateStride(PETSC_COMM_SELF,N,0,1,&is);
117: VecScatterCreate(vin,is,*vout,is,ctx);
118: ISDestroy(is);
119: return(0);
120: }