xref: /petsc/src/vec/is/sf/tests/ex7.c (revision 2fa40bb9206b96114faa7cb222621ec184d31cd2)
1 static char help[]= "Test vecscatter of different block sizes across processes\n\n";
2 
3 #include <petscvec.h>
4 int main(int argc,char **argv)
5 {
6   PetscErrorCode     ierr;
7   PetscInt           i,bs,n,low,high;
8   PetscMPIInt        nproc,rank;
9   Vec                x,y,z;
10   IS                 ix,iy;
11   VecScatter         vscat;
12   const PetscScalar  *yv;
13 
14   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
15   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&nproc);CHKERRMPI(ierr);
16   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
17   if (nproc != 2) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"This test can only run on two MPI ranks");
18 
19   /* Create an MPI vector x of size 12 on two processes, and set x = {0, 1, 2, .., 11} */
20   ierr = VecCreateMPI(PETSC_COMM_WORLD,6,PETSC_DECIDE,&x);CHKERRQ(ierr);
21   ierr = VecGetOwnershipRange(x,&low,&high);CHKERRQ(ierr);
22   for (i=low; i<high; i++) {ierr = VecSetValue(x,i,(PetscScalar)i,INSERT_VALUES);CHKERRQ(ierr);}
23   ierr = VecAssemblyBegin(x);CHKERRQ(ierr);
24   ierr = VecAssemblyEnd(x);CHKERRQ(ierr);
25 
26   /* Create a seq vector y, and a parallel to sequential (PtoS) vecscatter to scatter x to y */
27   if (rank == 0) {
28     /* On rank 0, seq y is of size 6. We will scatter x[0,1,2,6,7,8] to y[0,1,2,3,4,5] using IS with bs=3 */
29     PetscInt idx[2]={0,2};
30     PetscInt idy[2]={0,1};
31     n    = 6;
32     bs   = 3;
33     ierr = VecCreateSeq(PETSC_COMM_SELF,n,&y);CHKERRQ(ierr);
34     ierr = ISCreateBlock(PETSC_COMM_SELF,bs,2,idx,PETSC_COPY_VALUES,&ix);CHKERRQ(ierr);
35     ierr = ISCreateBlock(PETSC_COMM_SELF,bs,2,idy,PETSC_COPY_VALUES,&iy);CHKERRQ(ierr);
36   } else {
37     /* On rank 1, seq y is of size 4. We will scatter x[4,5,10,11] to y[0,1,2,3] using IS with bs=2 */
38     PetscInt idx[2]= {2,5};
39     PetscInt idy[2]= {0,1};
40     n    = 4;
41     bs   = 2;
42     ierr = VecCreateSeq(PETSC_COMM_SELF,n,&y);CHKERRQ(ierr);
43     ierr = ISCreateBlock(PETSC_COMM_SELF,bs,2,idx,PETSC_COPY_VALUES,&ix);CHKERRQ(ierr);
44     ierr = ISCreateBlock(PETSC_COMM_SELF,bs,2,idy,PETSC_COPY_VALUES,&iy);CHKERRQ(ierr);
45   }
46   ierr = VecScatterCreate(x,ix,y,iy,&vscat);CHKERRQ(ierr);
47 
48   /* Do the vecscatter */
49   ierr = VecScatterBegin(vscat,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
50   ierr = VecScatterEnd(vscat,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
51 
52   /* Print y. Since y is sequential, we put y in a parallel z to print its value on both ranks */
53   ierr = VecGetArrayRead(y,&yv);CHKERRQ(ierr);
54   ierr = VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,yv,&z);CHKERRQ(ierr);
55   ierr = VecView(z,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
56   ierr = VecRestoreArrayRead(y,&yv);CHKERRQ(ierr);
57 
58   ierr = ISDestroy(&ix);CHKERRQ(ierr);
59   ierr = ISDestroy(&iy);CHKERRQ(ierr);
60   ierr = VecDestroy(&x);CHKERRQ(ierr);
61   ierr = VecDestroy(&y);CHKERRQ(ierr);
62   ierr = VecDestroy(&z);CHKERRQ(ierr);
63   ierr = VecScatterDestroy(&vscat);CHKERRQ(ierr);
64 
65   ierr = PetscFinalize();
66   return ierr;
67 }
68 
69 /*TEST
70 
71    test:
72       nsize: 2
73       args:
74       requires:
75 TEST*/
76 
77