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