Actual source code: ex25.c
1: /*$Id: ex25.c,v 1.14 2001/03/23 23:21:30 balay Exp $*/
3: static char help[] = "Scatters from a parallel vector to a sequential vector. Inn
4: this case processor zero is as long as the entire parallel vector; rest are zero length.nn";
6: #include petscvec.h
7: #include petscsys.h
9: int main(int argc,char **argv)
10: {
11: int n = 5,ierr;
12: int size,rank,N,low,high,iglobal,i;
13: Scalar value,zero = 0.0;
14: Vec x,y;
15: IS is1,is2;
16: VecScatter ctx;
18: PetscInitialize(&argc,&argv,(char*)0,help);
19: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
22: /* create two vectors */
23: N = size*n;
24: VecCreateMPI(PETSC_COMM_WORLD,PETSC_DECIDE,N,&y);
25: if (!rank) {
26: VecCreateSeq(PETSC_COMM_SELF,N,&x);
27: } else {
28: VecCreateSeq(PETSC_COMM_SELF,0,&x);
29: }
31: /* create two index sets */
32: if (!rank) {
33: ISCreateStride(PETSC_COMM_SELF,N,0,1,&is1);
34: ISCreateStride(PETSC_COMM_SELF,N,0,1,&is2);
35: } else {
36: ISCreateStride(PETSC_COMM_SELF,0,0,1,&is1);
37: ISCreateStride(PETSC_COMM_SELF,0,0,1,&is2);
38: }
40: VecSet(&zero,x);
41: VecGetOwnershipRange(y,&low,&high);
42: for (i=0; i<n; i++) {
43: iglobal = i + low; value = (Scalar) (i + 10*rank);
44: VecSetValues(y,1,&iglobal,&value,INSERT_VALUES);
45: }
46: VecAssemblyBegin(y);
47: VecAssemblyEnd(y);
48: VecView(y,PETSC_VIEWER_STDOUT_WORLD);
50: VecScatterCreate(y,is2,x,is1,&ctx);
51: VecScatterBegin(y,x,ADD_VALUES,SCATTER_FORWARD,ctx);
52: VecScatterEnd(y,x,ADD_VALUES,SCATTER_FORWARD,ctx);
53: VecScatterDestroy(ctx);
54:
55: if (!rank)
56: {printf("----n"); VecView(x,PETSC_VIEWER_STDOUT_SELF);}
58: VecDestroy(x);
59: VecDestroy(y);
60: ISDestroy(is1);
61: ISDestroy(is2);
63: PetscFinalize();
64: return 0;
65: }
66: