Actual source code: ex45.c
petsc-3.13.4 2020-08-01
2: static char help[] = "Demonstrates VecStrideSubSetScatter() and VecStrideSubSetGather().\n\n";
4: /*T
5: Concepts: vectors^sub-vectors;
6: Processors: n
8: Allows one to easily pull out some components of a multi-component vector and put them in another vector.
10: Note that these are special cases of VecScatter
11: T*/
13: /*
14: Include "petscvec.h" so that we can use vectors. Note that this file
15: automatically includes:
16: petscsys.h - base PETSc routines petscis.h - index sets
17: petscviewer.h - viewers
18: */
20: #include <petscvec.h>
22: int main(int argc,char **argv)
23: {
24: Vec v,s;
25: PetscInt i,start,end,n = 8;
27: PetscScalar value;
28: const PetscInt vidx[] = {1,2},sidx[] = {1,0};
30: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
31: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
33: /*
34: Create multi-component vector with 4 components
35: */
36: VecCreate(PETSC_COMM_WORLD,&v);
37: VecSetSizes(v,PETSC_DECIDE,n);
38: VecSetBlockSize(v,4);
39: VecSetFromOptions(v);
41: /*
42: Create double-component vectors
43: */
44: VecCreate(PETSC_COMM_WORLD,&s);
45: VecSetSizes(s,PETSC_DECIDE,n/2);
46: VecSetBlockSize(s,2);
47: VecSetFromOptions(s);
49: /*
50: Set the vector values
51: */
52: VecGetOwnershipRange(v,&start,&end);
53: for (i=start; i<end; i++) {
54: value = i;
55: VecSetValues(v,1,&i,&value,INSERT_VALUES);
56: }
58: /*
59: Get the components from the large multi-component vector to the small multi-component vector,
60: scale the smaller vector and then move values back to the large vector
61: */
62: VecStrideSubSetGather(v,PETSC_DETERMINE,vidx,NULL,s,INSERT_VALUES);
63: VecView(s,PETSC_VIEWER_STDOUT_WORLD);
64: VecScale(s,100.0);
66: VecStrideSubSetScatter(s,PETSC_DETERMINE,NULL,vidx,v,ADD_VALUES);
67: VecView(v,PETSC_VIEWER_STDOUT_WORLD);
69: /*
70: Get the components from the large multi-component vector to the small multi-component vector,
71: scale the smaller vector and then move values back to the large vector
72: */
73: VecStrideSubSetGather(v,2,vidx,sidx,s,INSERT_VALUES);
74: VecView(s,PETSC_VIEWER_STDOUT_WORLD);
75: VecScale(s,100.0);
77: VecStrideSubSetScatter(s,2,sidx,vidx,v,ADD_VALUES);
78: VecView(v,PETSC_VIEWER_STDOUT_WORLD);
80: /*
81: Free work space. All PETSc objects should be destroyed when they
82: are no longer needed.
83: */
84: VecDestroy(&v);
85: VecDestroy(&s);
86: PetscFinalize();
87: return ierr;
88: }
92: /*TEST
94: test:
95: nsize: 2
97: TEST*/