Actual source code: ex9f.F90
1: !
2: !
3: ! Description: Illustrates the use of VecCreateGhost()
4: !
5: !
6: ! Ghost padding is one way to handle local calculations that
7: ! involve values from other processors. VecCreateGhost() provides
8: ! a way to create vectors with extra room at the end of the vector
9: ! array to contain the needed ghost values from other processors,
10: ! vector computations are otherwise unaffected.
11: !
13: program main
14: #include <petsc/finclude/petscvec.h>
15: use petscvec
16: implicit none
18: PetscMPIInt rank,size
19: PetscInt nlocal,nghost,ifrom(2)
20: PetscErrorCode ierr
21: PetscInt i,rstart,rend,ione
22: PetscBool flag
23: PetscScalar value,tarray(20)
24: Vec lx,gx,gxs
25: PetscViewer subviewer
27: nlocal = 6
28: nghost = 2
30: PetscCallA(PetscInitialize(ierr))
31: PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr))
32: PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD,size,ierr))
34: PetscCheckA(size .eq. 2,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,'Requires 2 processors')
36: !
37: ! Construct a two dimensional graph connecting nlocal degrees of
38: ! freedom per processor. From this we will generate the global
39: ! indices of needed ghost values
40: !
41: ! For simplicity we generate the entire graph on each processor:
42: ! in real application the graph would stored in parallel, but this
43: ! example is only to demonstrate the management of ghost padding
44: ! with VecCreateGhost().
45: !
46: ! In this example we consider the vector as representing
47: ! degrees of freedom in a one dimensional grid with periodic
48: ! boundary conditions.
49: !
50: ! ----Processor 1--------- ----Processor 2 --------
51: ! 0 1 2 3 4 5 6 7 8 9 10 11
52: ! |----|
53: ! |-------------------------------------------------|
54: !
56: if (rank .eq. 0) then
57: ifrom(1) = 11
58: ifrom(2) = 6
59: else
60: ifrom(1) = 0
61: ifrom(2) = 5
62: endif
64: ! Create the vector with two slots for ghost points. Note that both
65: ! the local vector (lx) and the global vector (gx) share the same
66: ! array for storing vector values.
68: PetscCallA(PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-allocate',flag,ierr))
69: if (flag) then
70: PetscCallA(VecCreateGhostWithArray(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr))
71: else
72: PetscCallA(VecCreateGhost(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE,nghost,ifrom,gxs,ierr))
73: endif
75: ! Test VecDuplicate
77: PetscCallA(VecDuplicate(gxs,gx,ierr))
78: PetscCallA(VecDestroy(gxs,ierr))
80: ! Access the local Form
82: PetscCallA(VecGhostGetLocalForm(gx,lx,ierr))
84: ! Set the values from 0 to 12 into the 'global' vector
86: PetscCallA(VecGetOwnershipRange(gx,rstart,rend,ierr))
88: ione = 1
89: do 10, i=rstart,rend-1
90: value = real(i)
91: PetscCallA(VecSetValues(gx,ione,i,value,INSERT_VALUES,ierr))
92: 10 continue
94: PetscCallA(VecAssemblyBegin(gx,ierr))
95: PetscCallA(VecAssemblyEnd(gx,ierr))
97: PetscCallA(VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr))
98: PetscCallA(VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr))
100: ! Print out each vector, including the ghost padding region.
102: PetscCallA(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,subviewer,ierr))
103: PetscCallA(VecView(lx,subviewer,ierr))
104: PetscCallA(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,subviewer,ierr))
106: PetscCallA(VecGhostRestoreLocalForm(gx,lx,ierr))
107: PetscCallA(VecDestroy(gx,ierr))
108: PetscCallA(PetscFinalize(ierr))
109: end
111: !/*TEST
112: !
113: ! test:
114: ! nsize: 2
115: !
116: ! test:
117: ! suffix: 2
118: ! nsize: 2
119: ! args: -allocate
120: !
121: !TEST*/