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