Actual source code: ex14f.F
1: !
2: ! "$Id: ex14f.F,v 1.8 2001/01/17 22:21:32 bsmith Exp $";
3: !
4: ! Description: Illustrates the use of VecCreateGhost()
5: !
6: !/*T
7: ! Concepts: vectors^assembling vectors; ghost padding
8: ! Processors: n
9: !
10: ! Comment: 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: implicit none
20: !
21: ! The following include statements are required for Fortran programs
22: ! that use PETSc vectors:
23: ! petsc.h - base PETSc routines
24: ! petscvec.h - vectors
25: !
27: #include "include/finclude/petsc.h"
28: #include "include/finclude/petscvec.h"
30: integer rank,nlocal,nghost,ifrom(2),size,ierr,i,rstart,rend
31: integer flag,bs
32: Scalar value,tarray(20)
33: Vec lx,gx,gxs
35: nlocal = 6
36: nghost = 2
37: bs = 2
38: nlocal = bs*nlocal
40: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
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
45: SETERRQ(1,'Must run with two processors')
46: endif
48: !
49: ! Construct a two dimensional graph connecting nlocal degrees of
50: ! freedom per processor. From this we will generate the global
51: ! indices of needed ghost values
52: !
53: ! For simplicity we generate the entire graph on each processor:
54: ! in real application the graph would stored in parallel, but this
55: ! example is only to demonstrate the management of ghost padding
56: ! with VecCreateGhost().
57: !
58: ! In this example we consider the vector as representing
59: ! degrees of freedom in a one dimensional grid with periodic
60: ! boundary conditions.
61: !
62: ! ----Processor 1----------- ----Processor 2 --------
63: ! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
64: ! |--|----|---|
65: ! |-|--------------------------------------------------------|--|
66: !
69: if (rank .eq. 0) then
70: ifrom(1) = bs*11
71: ifrom(2) = bs*6
72: else
73: ifrom(1) = bs*0
74: ifrom(2) = bs*5
75: endif
77: ! Create the vector with two slots for ghost points. Note that both
78: ! the local vector (lx) and the global vector (gx) share the same
79: ! array for storing vector values.
81: call PetscOptionsHasName(PETSC_NULL_CHARACTER,'-allocate',flag,ierr)
82: if (flag .ne. 0) 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: do 10, i=rstart,rend-1
105: value = i
106: call VecSetValues(gx,1,i,value,INSERT_VALUES,ierr)
107: 10 continue
109: call VecAssemblyBegin(gx,ierr)
110: call VecAssemblyEnd(gx,ierr)
112: call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
113: call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
115: ! Print out each vector, including the ghost padding region.
117: call VecView(lx,PETSC_VIEWER_STDOUT_SELF,ierr)
119: call VecGhostRestoreLocalForm(gx,lx,ierr)
120: call VecDestroy(gx,ierr)
121: call PetscFinalize(ierr)
122: end
123: