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,mySize
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: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
31: if (ierr /= 0) then
32: print*,'PetscInitialize failed'
33: stop
34: endif
35: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
36: call MPI_Comm_size(PETSC_COMM_WORLD,mySize,ierr)
38: if (mySize /= 2) then; SETERRA(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,'Requires 2 processors'); endif
40: !
41: ! Construct a two dimensional graph connecting nlocal degrees of
42: ! freedom per processor. From this we will generate the global
43: ! indices of needed ghost values
44: !
45: ! For simplicity we generate the entire graph on each processor:
46: ! in real application the graph would stored in parallel, but this
47: ! example is only to demonstrate the management of ghost padding
48: ! with VecCreateGhost().
49: !
50: ! In this example we consider the vector as representing
51: ! degrees of freedom in a one dimensional grid with periodic
52: ! boundary conditions.
53: !
54: ! ----Processor 1--------- ----Processor 2 --------
55: ! 0 1 2 3 4 5 6 7 8 9 10 11
56: ! |----|
57: ! |-------------------------------------------------|
58: !
60: if (rank .eq. 0) then
61: ifrom(1) = 11
62: ifrom(2) = 6
63: else
64: ifrom(1) = 0
65: ifrom(2) = 5
66: endif
68: ! Create the vector with two slots for ghost points. Note that both
69: ! the local vector (lx) and the global vector (gx) share the same
70: ! array for storing vector values.
72: call PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
73: & '-allocate',flag,ierr)
74: if (flag) then
75: call VecCreateGhostWithArray(PETSC_COMM_WORLD,nlocal, &
76: & PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
77: else
78: call VecCreateGhost(PETSC_COMM_WORLD,nlocal,PETSC_DECIDE, &
79: & nghost,ifrom,gxs,ierr)
80: endif
82: ! Test VecDuplicate
84: call VecDuplicate(gxs,gx,ierr)
85: call VecDestroy(gxs,ierr)
87: ! Access the local Form
89: call VecGhostGetLocalForm(gx,lx,ierr)
91: ! Set the values from 0 to 12 into the 'global' vector
93: call VecGetOwnershipRange(gx,rstart,rend,ierr)
95: ione = 1
96: do 10, i=rstart,rend-1
97: value = real(i)
98: call VecSetValues(gx,ione,i,value,INSERT_VALUES,ierr)
99: 10 continue
101: call VecAssemblyBegin(gx,ierr)
102: call VecAssemblyEnd(gx,ierr)
104: call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
105: call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
107: ! Print out each vector, including the ghost padding region.
109: call PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,subviewer,ierr)
110: call VecView(lx,subviewer,ierr)
111: call PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,subviewer,ierr)
113: call VecGhostRestoreLocalForm(gx,lx,ierr)
114: call VecDestroy(gx,ierr)
115: call PetscFinalize(ierr)
116: end
118: !/*TEST
119: !
120: ! test:
121: ! nsize: 2
122: !
123: ! test:
124: ! suffix: 2
125: ! nsize: 2
126: ! args: -allocate
127: !
128: !TEST*/