Actual source code: ex14f.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. 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
29: PetscViewer singleton
31: nlocal = 6
32: nghost = 2
33: bs = 2
34: nlocal = bs*nlocal
36: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
37: if (ierr .ne. 0) then
38: print*,'Unable to initialize PETSc'
39: stop
40: endif
41: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
42: call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
44: if (size .ne. 2) then; SETERRA(PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,'Requires 2 processors'); endif
46: !
47: ! Construct a two dimensional graph connecting nlocal degrees of
48: ! freedom per processor. From this we will generate the global
49: ! indices of needed ghost values
50: !
51: ! For simplicity we generate the entire graph on each processor:
52: ! in real application the graph would stored in parallel, but this
53: ! example is only to demonstrate the management of ghost padding
54: ! with VecCreateGhost().
55: !
56: ! In this example we consider the vector as representing
57: ! degrees of freedom in a one dimensional grid with periodic
58: ! boundary conditions.
59: !
60: ! ----Processor 1----------- ----Processor 2 --------
61: ! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
62: ! |--|----|---|
63: ! |-|--------------------------------------------------------|--|
64: !
66: if (rank .eq. 0) then
67: ifrom(1) = 11
68: ifrom(2) = 6
69: else
70: ifrom(1) = 0
71: ifrom(2) = 5
72: endif
74: ! Create the vector with two slots for ghost points. Note that both
75: ! the local vector (lx) and the global vector (gx) share the same
76: ! array for storing vector values.
78: call PetscOptionsHasName(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
79: & '-allocate',flag,ierr)
80: if (flag) then
81: call VecCreateGhostBlockWithArray(PETSC_COMM_WORLD,bs,nlocal, &
82: & PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
83: else
84: call VecCreateGhostBlock(PETSC_COMM_WORLD,bs,nlocal, &
85: & PETSC_DECIDE,nghost,ifrom,gxs,ierr)
86: endif
88: ! Test VecDuplicate
90: call VecDuplicate(gxs,gx,ierr)
91: call VecDestroy(gxs,ierr)
93: ! Access the local Form
95: call VecGhostGetLocalForm(gx,lx,ierr)
97: ! Set the values from 0 to 12 into the "global" vector
99: call VecGetOwnershipRange(gx,rstart,rend,ierr)
101: ione = 1
102: do 10, i=rstart,rend-1
103: value = i
104: call VecSetValues(gx,ione,i,value,INSERT_VALUES,ierr)
105: 10 continue
107: call VecAssemblyBegin(gx,ierr)
108: call VecAssemblyEnd(gx,ierr)
110: call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
111: call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
113: ! Print out each vector, including the ghost padding region.
115: call PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,singleton,ierr)
116: call VecView(lx,singleton,ierr)
117: call PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,singleton,ierr)
119: call VecGhostRestoreLocalForm(gx,lx,ierr)
120: call VecDestroy(gx,ierr)
121: call PetscFinalize(ierr)
122: end
124: !/*TEST
125: !
126: ! test:
127: ! nsize: 2
128: !
129: !TEST*/