xref: /petsc/src/vec/vec/tests/ex24.c (revision e84e3fd21fa5912dca3017339ab4b3699e3a9c51)
1 
2 static char help[] = "Scatters from a parallel vector to a sequential vector.\n\
3 Tests where the local part of the scatter is a copy.\n\n";
4 
5 #include <petscvec.h>
6 
7 int main(int argc,char **argv)
8 {
9   PetscMPIInt    size,rank;
10   PetscInt       n = 5,i,*blks,bs = 1,m = 2;
11   PetscScalar    value;
12   Vec            x,y;
13   IS             is1,is2;
14   VecScatter     ctx = 0;
15   PetscViewer    sviewer;
16 
17   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
18 
19   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
20   PetscCall(PetscOptionsGetInt(NULL,NULL,"-bs",&bs,NULL));
21 
22   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
23   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
24 
25   /* create two vectors */
26   PetscCall(VecCreate(PETSC_COMM_WORLD,&x));
27   PetscCall(VecSetSizes(x,PETSC_DECIDE,size*bs*n));
28   PetscCall(VecSetFromOptions(x));
29 
30   /* create two index sets */
31   if (rank < size-1) m = n + 2;
32   else m = n;
33 
34   PetscCall(PetscMalloc1(m,&blks));
35   blks[0] = n*rank;
36   for (i=1; i<m; i++) blks[i] = blks[i-1] + 1;
37   PetscCall(ISCreateBlock(PETSC_COMM_SELF,bs,m,blks,PETSC_COPY_VALUES,&is1));
38   PetscCall(PetscFree(blks));
39 
40   PetscCall(VecCreateSeq(PETSC_COMM_SELF,bs*m,&y));
41   PetscCall(ISCreateStride(PETSC_COMM_SELF,bs*m,0,1,&is2));
42 
43   /* each processor inserts the entire vector */
44   /* this is redundant but tests assembly */
45   for (i=0; i<bs*n*size; i++) {
46     value = (PetscScalar) i;
47     PetscCall(VecSetValues(x,1,&i,&value,INSERT_VALUES));
48   }
49   PetscCall(VecAssemblyBegin(x));
50   PetscCall(VecAssemblyEnd(x));
51   PetscCall(VecView(x,PETSC_VIEWER_STDOUT_WORLD));
52 
53   PetscCall(VecScatterCreate(x,is1,y,is2,&ctx));
54   PetscCall(VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD));
55   PetscCall(VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD));
56 
57   PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
58   PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"----\n"));
59   PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
60   PetscCall(VecView(y,sviewer)); fflush(stdout);
61   PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer));
62   PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
63   PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
64 
65   PetscCall(VecScatterDestroy(&ctx));
66 
67   PetscCall(VecDestroy(&x));
68   PetscCall(VecDestroy(&y));
69   PetscCall(ISDestroy(&is1));
70   PetscCall(ISDestroy(&is2));
71 
72   PetscCall(PetscFinalize());
73   return 0;
74 }
75 
76 /*TEST
77 
78    testset:
79       nsize: 3
80       output_file: output/ex24_1.out
81       filter: grep -v "  type:"
82       test:
83         suffix: standard
84         args: -vec_type standard
85       test:
86         requires: cuda
87         suffix: cuda
88         args: -vec_type cuda
89       test:
90         requires: viennacl
91         suffix:  viennacl
92         args: -vec_type viennacl
93 
94 TEST*/
95