Actual source code: ex12.c
petsc-3.13.4 2020-08-01
2: static char help[] = "Scatters from a sequential vector to a parallel vector.\n\
3: This does case when we are merely selecting the local part of the\n\
4: parallel vector.\n";
6: #include <petscvec.h>
8: int main(int argc,char **argv)
9: {
11: PetscMPIInt size,rank;
12: PetscInt n = 5,i;
13: PetscScalar value;
14: Vec x,y;
15: IS is1,is2;
16: VecScatter ctx = 0;
18: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
20: MPI_Comm_size(PETSC_COMM_WORLD,&size);
21: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
23: /* create two vectors */
24: VecCreate(PETSC_COMM_WORLD,&x);
25: VecSetSizes(x,PETSC_DECIDE,size*n);
26: VecSetFromOptions(x);
27: VecCreateSeq(PETSC_COMM_SELF,n,&y);
29: /* create two index sets */
30: ISCreateStride(PETSC_COMM_SELF,n,n*rank,1,&is1);
31: ISCreateStride(PETSC_COMM_SELF,n,0,1,&is2);
33: /* each processor inserts the entire vector */
34: /* this is redundant but tests assembly */
35: for (i=0; i<n; i++) {
36: value = (PetscScalar) (i + 10*rank);
37: VecSetValues(y,1,&i,&value,INSERT_VALUES);
38: }
39: VecAssemblyBegin(y);
40: VecAssemblyEnd(y);
42: VecScatterCreate(y,is2,x,is1,&ctx);
43: VecScatterBegin(ctx,y,x,INSERT_VALUES,SCATTER_FORWARD);
44: VecScatterEnd(ctx,y,x,INSERT_VALUES,SCATTER_FORWARD);
45: VecScatterDestroy(&ctx);
47: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
49: VecDestroy(&x);
50: VecDestroy(&y);
51: ISDestroy(&is1);
52: ISDestroy(&is2);
54: PetscFinalize();
55: return ierr;
56: }
60: /*TEST
62: test:
63: nsize: 2
65: TEST*/