Actual source code: ex12.c
1: /*$Id: ex12.c,v 1.16 2001/03/23 23:21:37 balay 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: Scalar 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,PETSC_DECIDE,n,&v);
34: VecSetFromOptions(v);
35: VecSetBlockSize(v,2);
37: /*
38: Create single-component vector
39: */
40: VecCreate(PETSC_COMM_WORLD,PETSC_DECIDE,n/2,&s);
41: VecSetFromOptions(s);
43: /*
44: Set the vectors to entries to a constant value.
45: */
46: VecSet(&one,v);
48: /*
49: Get the first component from the multi-component vector to the single vector
50: */
51: VecStrideGather(v,0,s,INSERT_VALUES);
53: VecView(s,PETSC_VIEWER_STDOUT_WORLD);
55: /*
56: Put the values back into the second component
57: */
58: VecStrideScatter(s,1,v,ADD_VALUES);
60: VecView(v,PETSC_VIEWER_STDOUT_WORLD);
62: /*
63: Free work space. All PETSc objects should be destroyed when they
64: are no longer needed.
65: */
66: VecDestroy(v);
67: VecDestroy(s);
68: PetscFinalize();
69: return 0;
70: }
71: