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