Actual source code: ex14f.F
1: !
2: ! "$Id: ex14f.F,v 1.9 2001/08/07 03:02:34 balay Exp $";
3: !
4: ! Description: Illustrates the use of VecCreateGhost()
5: !
6: !/*T
7: ! Concepts: vectors^assembling vectors;
8: ! Concepts: vectors^ghost padding;
9: ! Processors: n
10: !
11: ! Description: Ghost padding is one way to handle local calculations that
12: ! involve values from other processors. VecCreateGhostBlock() provides
13: ! a way to create vectors with extra room at the end of the vector
14: ! array to contain the needed ghost values from other processors,
15: ! vector computations are otherwise unaffected.
16: !T*/
18: program main
19: implicit none
21: !
22: ! The following include statements are required for Fortran programs
23: ! that use PETSc vectors:
24: ! petsc.h - base PETSc routines
25: ! petscvec.h - vectors
26: !
28: #include include/finclude/petsc.h
29: #include include/finclude/petscvec.h
31: integer rank,nlocal,nghost,ifrom(2),size,ierr,i,rstart,rend
32: integer flag,bs
33: PetscScalar value,tarray(20)
34: Vec lx,gx,gxs
36: nlocal = 6
37: nghost = 2
38: bs = 2
39: nlocal = bs*nlocal
41: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
42: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
43: call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
45: if (size .ne. 2) then
46: SETERRQ(1,'Must run with two processors')
47: endif
49: !
50: ! Construct a two dimensional graph connecting nlocal degrees of
51: ! freedom per processor. From this we will generate the global
52: ! indices of needed ghost values
53: !
54: ! For simplicity we generate the entire graph on each processor:
55: ! in real application the graph would stored in parallel, but this
56: ! example is only to demonstrate the management of ghost padding
57: ! with VecCreateGhost().
58: !
59: ! In this example we consider the vector as representing
60: ! degrees of freedom in a one dimensional grid with periodic
61: ! boundary conditions.
62: !
63: ! ----Processor 1----------- ----Processor 2 --------
64: ! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
65: ! |--|----|---|
66: ! |-|--------------------------------------------------------|--|
67: !
70: if (rank .eq. 0) then
71: ifrom(1) = bs*11
72: ifrom(2) = bs*6
73: else
74: ifrom(1) = bs*0
75: ifrom(2) = bs*5
76: endif
78: ! Create the vector with two slots for ghost points. Note that both
79: ! the local vector (lx) and the global vector (gx) share the same
80: ! array for storing vector values.
82: call PetscOptionsHasName(PETSC_NULL_CHARACTER,'-allocate',flag,ierr)
83: if (flag .ne. 0) then
84: call VecCreateGhostBlockWithArray(PETSC_COMM_WORLD,bs,nlocal, &
85: & PETSC_DECIDE,nghost,ifrom,tarray,gxs,ierr)
86: else
87: call VecCreateGhostBlock(PETSC_COMM_WORLD,bs,nlocal, &
88: & PETSC_DECIDE,nghost,ifrom,gxs,ierr)
89: endif
92: ! Test VecDuplicate
94: call VecDuplicate(gxs,gx,ierr)
95: call VecDestroy(gxs,ierr)
97: ! Access the local Form
99: call VecGhostGetLocalForm(gx,lx,ierr)
101: ! Set the values from 0 to 12 into the "global" vector
103: call VecGetOwnershipRange(gx,rstart,rend,ierr)
105: do 10, i=rstart,rend-1
106: value = i
107: call VecSetValues(gx,1,i,value,INSERT_VALUES,ierr)
108: 10 continue
110: call VecAssemblyBegin(gx,ierr)
111: call VecAssemblyEnd(gx,ierr)
113: call VecGhostUpdateBegin(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
114: call VecGhostUpdateEnd(gx,INSERT_VALUES,SCATTER_FORWARD,ierr)
116: ! Print out each vector, including the ghost padding region.
118: call VecView(lx,PETSC_VIEWER_STDOUT_SELF,ierr)
120: call VecGhostRestoreLocalForm(gx,lx,ierr)
121: call VecDestroy(gx,ierr)
122: call PetscFinalize(ierr)
123: end
124: