Actual source code: ex12.c
1: /*$Id: ex12.c,v 1.18 2001/09/11 16:32:18 bsmith Exp $*/
3: /* Program usage: mpirun ex1 [-help] [all PETSc options] */
5: static char help[] = "Demonstrates VecStrideScatter() and VecStrideGather().nn";
7: /*T
8: Concepts: vectors^sub-vectors;
9: Processors: n
10: T*/
12: /*
13: Include "petscvec.h" so that we can use vectors. Note that this file
14: automatically includes:
15: petsc.h - base PETSc routines petscis.h - index sets
16: petscsys.h - system routines petscviewer.h - viewers
17: */
19: #include petscvec.h
21: int main(int argc,char **argv)
22: {
23: Vec v,s; /* vectors */
24: int n = 20,ierr;
25: PetscScalar one = 1.0;
27: PetscInitialize(&argc,&argv,(char*)0,help);
28: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
30: /*
31: Create multi-component vector with 2 components
32: */
33: VecCreate(PETSC_COMM_WORLD,&v);
34: VecSetSizes(v,PETSC_DECIDE,n);
35: VecSetBlockSize(v,2);
36: VecSetFromOptions(v);
38: /*
39: Create single-component vector
40: */
41: VecCreate(PETSC_COMM_WORLD,&s);
42: VecSetSizes(s,PETSC_DECIDE,n/2);
43: VecSetFromOptions(s);
45: /*
46: Set the vectors to entries to a constant value.
47: */
48: VecSet(&one,v);
50: /*
51: Get the first component from the multi-component vector to the single vector
52: */
53: VecStrideGather(v,0,s,INSERT_VALUES);
55: VecView(s,PETSC_VIEWER_STDOUT_WORLD);
57: /*
58: Put the values back into the second component
59: */
60: VecStrideScatter(s,1,v,ADD_VALUES);
62: VecView(v,PETSC_VIEWER_STDOUT_WORLD);
64: /*
65: Free work space. All PETSc objects should be destroyed when they
66: are no longer needed.
67: */
68: VecDestroy(v);
69: VecDestroy(s);
70: PetscFinalize();
71: return 0;
72: }
73: