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: !
12: #include <petsc/finclude/petscvec.h>
13: program main
14: use petscvec
15: implicit none
17: PetscMPIInt rank, size
18: PetscInt nlocal, nghost, ifrom(2)
19: PetscErrorCode ierr
20: PetscInt i, rstart, rend, ione
21: PetscBool flag
22: PetscScalar value, tarray(20)
23: Vec lx, gx, gxs
24: PetscViewer subviewer
26: nlocal = 6
27: nghost = 2
29: PetscCallA(PetscInitialize(ierr))
30: PetscCallMPIA(MPI_Comm_rank(PETSC_COMM_WORLD, rank, ierr))
31: PetscCallMPIA(MPI_Comm_size(PETSC_COMM_WORLD, size, ierr))
33: PetscCheckA(size == 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, 'Requires 2 processors')
35: !
36: ! Construct a two dimensional graph connecting nlocal degrees of
37: ! freedom per processor. From this we will generate the global
38: ! indices of needed ghost values
39: !
40: ! For simplicity we generate the entire graph on each processor:
41: ! in real application the graph would stored in parallel, but this
42: ! example is only to demonstrate the management of ghost padding
43: ! with VecCreateGhost().
44: !
45: ! In this example we consider the vector as representing
46: ! degrees of freedom in a one dimensional grid with periodic
47: ! boundary conditions.
48: !
49: ! ----Processor 1--------- ----Processor 2 --------
50: ! 0 1 2 3 4 5 6 7 8 9 10 11
51: ! |----|
52: ! |-------------------------------------------------|
53: !
55: if (rank == 0) then
56: ifrom(1) = 11
57: ifrom(2) = 6
58: else
59: ifrom(1) = 0
60: ifrom(2) = 5
61: end if
63: ! Create the vector with two slots for ghost points. Note that both
64: ! the local vector (lx) and the global vector (gx) share the same
65: ! array for storing vector values.
67: PetscCallA(PetscOptionsHasName(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-allocate', flag, ierr))
68: if (flag) then
69: PetscCallA(VecCreateGhostWithArray(PETSC_COMM_WORLD, nlocal, PETSC_DECIDE, nghost, ifrom, tarray, gxs, ierr))
70: else
71: PetscCallA(VecCreateGhost(PETSC_COMM_WORLD, nlocal, PETSC_DECIDE, nghost, ifrom, gxs, ierr))
72: end if
74: ! Test VecDuplicate
76: PetscCallA(VecDuplicate(gxs, gx, ierr))
77: PetscCallA(VecDestroy(gxs, ierr))
79: ! Access the local Form
81: PetscCallA(VecGhostGetLocalForm(gx, lx, ierr))
83: ! Set the values from 0 to 12 into the 'global' vector
85: PetscCallA(VecGetOwnershipRange(gx, rstart, rend, ierr))
87: ione = 1
88: do i = rstart, rend - 1
89: value = real(i)
90: PetscCallA(VecSetValues(gx, ione, [i], [value], INSERT_VALUES, ierr))
91: end do
93: PetscCallA(VecAssemblyBegin(gx, ierr))
94: PetscCallA(VecAssemblyEnd(gx, ierr))
96: PetscCallA(VecGhostUpdateBegin(gx, INSERT_VALUES, SCATTER_FORWARD, ierr))
97: PetscCallA(VecGhostUpdateEnd(gx, INSERT_VALUES, SCATTER_FORWARD, ierr))
99: ! Print out each vector, including the ghost padding region.
101: PetscCallA(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, subviewer, ierr))
102: PetscCallA(VecView(lx, subviewer, ierr))
103: PetscCallA(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, subviewer, ierr))
105: PetscCallA(VecGhostRestoreLocalForm(gx, lx, ierr))
106: PetscCallA(VecDestroy(gx, ierr))
107: PetscCallA(PetscFinalize(ierr))
108: end
110: !/*TEST
111: !
112: ! test:
113: ! nsize: 2
114: !
115: ! test:
116: ! suffix: 2
117: ! nsize: 2
118: ! args: -allocate
119: !
120: !TEST*/