Actual source code: ex14f.F90
petsc-3.8.4 2018-03-24
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. VecCreateGhostBlock() 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 size,rank
23: PetscInt nlocal,nghost,ifrom(2)
24: PetscInt i,rstart,rend,bs,ione
25: PetscBool flag
26: PetscErrorCode ierr
27: PetscScalar value,tarray(20)
28: Vec lx,gx,gxs
30: nlocal = 6
31: nghost = 2
32: bs = 2
33: nlocal = bs*nlocal
35: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
36: if (ierr .ne. 0) then
37: print*,'Unable to initialize PETSc'
38: stop
39: endif
40: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
41: call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
43: if (size .ne. 2) then
44: SETERRA(PETSC_COMM_WORLD,1,'Requires 2 processors')
45: endif
47: !
48: ! Construct a two dimensional graph connecting nlocal degrees of
49: ! freedom per processor. From this we will generate the global
50: ! indices of needed ghost values
51: !
52: ! For simplicity we generate the entire graph on each processor:
53: ! in real application the graph would stored in parallel, but this
54: ! example is only to demonstrate the management of ghost padding
55: ! with VecCreateGhost().
56: !
57: ! In this example we consider the vector as representing
58: ! degrees of freedom in a one dimensional grid with periodic
59: ! boundary conditions.
60: !
61: ! ----Processor 1----------- ----Processor 2 --------
62: ! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
63: ! |--|----|---|
64: ! |-|--------------------------------------------------------|--|
65: !
68: if (rank .eq. 0) then
69: ifrom(1) = 11
70: ifrom(2) = 6
71: else
72: ifrom(1) = 0
73: ifrom(2) = 5
74: endif
76: ! Create the vector with two slots for ghost points. Note that both
77: ! the local vector (lx) and the global vector (gx) share the same
78: ! array for storing vector values.
80: call PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
81: & '-allocate',flag,ierr)
82: if (flag) then
83: call VecCreateGhostBlockWithArray(PETSC_COMM_WORLD,bs,nlocal, &
84: & PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
85: else
86: call VecCreateGhostBlock(PETSC_COMM_WORLD,bs,nlocal, &
87: & PETSC_DECIDE,nghost,ifrom,gxs,ierr)
88: endif
91: ! Test VecDuplicate
93: call VecDuplicate(gxs,gx,ierr)
94: call VecDestroy(gxs,ierr)
96: ! Access the local Form
98: call VecGhostGetLocalForm(gx,lx,ierr)
100: ! Set the values from 0 to 12 into the "global" vector
102: call VecGetOwnershipRange(gx,rstart,rend,ierr)
104: ione = 1
105: do 10, i=rstart,rend-1
106: value = i
107: call VecSetValues(gx,ione,i,value,INSERT_VALUES,ierr)
108: 10 continue
110: call VecAssemblyBegin(gx,ierr)
111: call VecAssemblyEnd(gx,ierr)
113: call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
114: call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
116: ! Print out each vector, including the ghost padding region.
118: call VecView(lx,PETSC_VIEWER_STDOUT_SELF,ierr)
120: call VecGhostRestoreLocalForm(gx,lx,ierr)
121: call VecDestroy(gx,ierr)
122: call PetscFinalize(ierr)
123: end