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: #undef __FUNCT__
23: int main(int argc,char **argv)
24: {
25: Vec v,s; /* vectors */
26: int n = 20,ierr;
27: PetscScalar one = 1.0;
29: PetscInitialize(&argc,&argv,(char*)0,help);
30: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
32: /*
33: Create multi-component vector with 2 components
34: */
35: VecCreate(PETSC_COMM_WORLD,&v);
36: VecSetSizes(v,PETSC_DECIDE,n);
37: VecSetBlockSize(v,2);
38: VecSetFromOptions(v);
40: /*
41: Create single-component vector
42: */
43: VecCreate(PETSC_COMM_WORLD,&s);
44: VecSetSizes(s,PETSC_DECIDE,n/2);
45: VecSetFromOptions(s);
47: /*
48: Set the vectors to entries to a constant value.
49: */
50: VecSet(&one,v);
52: /*
53: Get the first component from the multi-component vector to the single vector
54: */
55: VecStrideGather(v,0,s,INSERT_VALUES);
57: VecView(s,PETSC_VIEWER_STDOUT_WORLD);
59: /*
60: Put the values back into the second component
61: */
62: VecStrideScatter(s,1,v,ADD_VALUES);
64: VecView(v,PETSC_VIEWER_STDOUT_WORLD);
66: /*
67: Free work space. All PETSc objects should be destroyed when they
68: are no longer needed.
69: */
70: VecDestroy(v);
71: VecDestroy(s);
72: PetscFinalize();
73: return 0;
74: }
75: