Actual source code: ex16f.F90
petsc-3.14.6 2021-03-30
1: program main
2: #include <petsc/finclude/petscvec.h>
3: use petscvec
4: implicit none
6: Vec :: v,s,r
7: Vec,pointer,dimension(:) :: vecs
8: PetscInt :: i,start
9: PetscInt :: endd
10: PetscInt,parameter :: n = 20, four = 4, two = 2, one = 1
11: PetscErrorCode ierr
12: PetscScalar :: myValue
13: PetscBool :: flg
15: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
17: if (ierr /= 0) then
18: print*,'PetscInitialize failed'
19: stop
20: endif
22: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-n",n,flg,ierr);CHKERRA(ierr)
25: !Create multi-component vector with 2 components
26: call VecCreate(PETSC_COMM_WORLD,v,ierr);CHKERRA(ierr)
27: call VecSetSizes(v,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
28: call VecSetBlockSize(v,four,ierr);CHKERRA(ierr)
29: call VecSetFromOptions(v,ierr);CHKERRA(ierr)
32: !Create double-component vectors
34: call VecCreate(PETSC_COMM_WORLD,s,ierr);CHKERRA(ierr)
35: call VecSetSizes(s,PETSC_DECIDE,n/two,ierr);CHKERRA(ierr)
36: call VecSetBlockSize(s,two,ierr);CHKERRA(ierr)
37: call VecSetFromOptions(s,ierr);CHKERRA(ierr)
38: call VecDuplicate(s,r,ierr);CHKERRA(ierr)
39: allocate(vecs(0:2))
41: vecs(0) = s
42: vecs(1) = r
44: !Set the vector values
46: call VecGetOwnershipRange(v,start,endd,ierr);CHKERRA(ierr)
47: do i=start,endd-1
48: myValue = real(i)
49: call VecSetValues(v,one,i,myValue,INSERT_VALUES,ierr);CHKERRA(ierr)
50: end do
53: ! Get the components from the multi-component vector to the other vectors
55: call VecStrideGatherAll(v,vecs,INSERT_VALUES,ierr);CHKERRA(ierr)
57: call VecView(s,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
58: call VecView(r,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
60: call VecStrideScatterAll(vecs,v,ADD_VALUES,ierr);CHKERRA(ierr)
62: call VecView(v,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
65: !Free work space.All PETSc objects should be destroyed when they are no longer needed.
67: deallocate(vecs)
68: call VecDestroy(v,ierr);CHKERRA(ierr)
69: call VecDestroy(s,ierr);CHKERRA(ierr)
70: call VecDestroy(r,ierr);CHKERRA(ierr)
71: call PetscFinalize(ierr);CHKERRA(ierr)
73: end program
75: !/*TEST
76: !
77: ! test:
78: ! nsize: 2
79: ! output_file: output/ex16_1.out
80: !
81: !TEST*/