Actual source code: ex3f.F90
1: !
2: !
3: ! Description: Displays a vector visually.
4: !
5: ! -----------------------------------------------------------------------
7: program main
8: #include <petsc/finclude/petscvec.h>
9: use petscvec
10: implicit none
12: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13: ! Beginning of program
14: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16: Vec x
17: PetscViewer viewer
18: PetscScalar v
19: PetscInt :: i,istart,iend
20: PetscInt, parameter :: ione = 1, n = 50
21: PetscErrorCode ierr
22: PetscBool flg
24: PetscCallA(PetscInitialize(ierr))
25: PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-n',n,flg,ierr))
27: ! Create a vector, specifying only its global dimension.
28: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
29: ! the vector format (currently parallel
30: ! or sequential) is determined at runtime. Also, the parallel
31: ! partitioning of the vector is determined by PETSc at runtime.
32: PetscCallA(VecCreate(PETSC_COMM_WORLD,x,ierr))
33: PetscCallA(VecSetSizes(x,PETSC_DECIDE,n,ierr))
34: PetscCallA(VecSetFromOptions(x,ierr))
36: ! Currently, all PETSc parallel vectors are partitioned by
37: ! contiguous chunks of rows across the processors. Determine
38: ! which vector are locally owned.
39: PetscCallA(VecGetOwnershipRange(x,istart,iend,ierr))
41: ! Set the vector elements.
42: ! - Always specify global locations of vector entries.
43: ! - Each processor needs to insert only elements that it owns locally.
44: do 100 i=istart,iend-1
45: v = 1.0*real(i)
46: PetscCallA(VecSetValues(x,ione,i,v,INSERT_VALUES,ierr))
47: 100 continue
49: ! Assemble vector, using the 2-step process:
50: ! VecAssemblyBegin(), VecAssemblyEnd()
51: ! Computations can be done while messages are in transition
52: ! by placing code between these two statements.
53: PetscCallA(VecAssemblyBegin(x,ierr))
54: PetscCallA(VecAssemblyEnd(x,ierr))
56: ! Open an X-window viewer. Note that we specify the same communicator
57: ! for the viewer as we used for the distributed vector (PETSC_COMM_WORLD).
58: ! - Helpful runtime option:
59: ! -draw_pause <pause> : sets time (in seconds) that the
60: ! program pauses after PetscDrawPause() has been called
61: ! (0 is default, -1 implies until user input).
63: PetscCallA(PetscViewerDrawOpen(PETSC_COMM_WORLD,PETSC_NULL_CHARACTER,PETSC_NULL_CHARACTER,0,0,300,300,viewer,ierr))
65: ! View the vector
66: PetscCallA(VecView(x,viewer,ierr))
68: ! Free work space. All PETSc objects should be destroyed when they
69: ! are no longer needed.
71: PetscCallA(PetscViewerDestroy(viewer,ierr))
72: PetscCallA(VecDestroy(x,ierr))
74: PetscCallA(PetscFinalize(ierr))
75: end
77: !/*TEST
78: !
79: ! test:
80: ! nsize: 2
81: !
82: !TEST*/