Actual source code: ex16f.F90

  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)

 24:   !Create multi-component vector with 2 components
 25:   call VecCreate(PETSC_COMM_WORLD,v,ierr);CHKERRA(ierr)
 26:   call VecSetSizes(v,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
 27:   call VecSetBlockSize(v,four,ierr);CHKERRA(ierr)
 28:   call VecSetFromOptions(v,ierr);CHKERRA(ierr)

 30:   ! Create double-component vectors

 32:   call VecCreate(PETSC_COMM_WORLD,s,ierr);CHKERRA(ierr)
 33:   call VecSetSizes(s,PETSC_DECIDE,n/two,ierr);CHKERRA(ierr)
 34:   call VecSetBlockSize(s,two,ierr);CHKERRA(ierr)
 35:   call VecSetFromOptions(s,ierr);CHKERRA(ierr)
 36:   call VecDuplicate(s,r,ierr);CHKERRA(ierr)
 37:   allocate(vecs(0:2))

 39:   vecs(0) = s
 40:   vecs(1) = r

 42:   !Set the vector values

 44:   call VecGetOwnershipRange(v,start,endd,ierr);CHKERRA(ierr)
 45:   do i=start,endd-1
 46:      myValue = real(i)
 47:      call VecSetValues(v,one,i,myValue,INSERT_VALUES,ierr);CHKERRA(ierr)
 48:   end do

 50:   ! Get the components from the multi-component vector to the other vectors

 52:   call VecStrideGatherAll(v,vecs,INSERT_VALUES,ierr);CHKERRA(ierr)

 54:   call VecView(s,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
 55:   call VecView(r,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)

 57:   call VecStrideScatterAll(vecs,v,ADD_VALUES,ierr);CHKERRA(ierr)

 59:   call VecView(v,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)

 61:   !Free work space.All PETSc objects should be destroyed when they are no longer needed.

 63:   deallocate(vecs)
 64:   call VecDestroy(v,ierr);CHKERRA(ierr)
 65:   call VecDestroy(s,ierr);CHKERRA(ierr)
 66:   call VecDestroy(r,ierr);CHKERRA(ierr)
 67:   call PetscFinalize(ierr);CHKERRA(ierr)

 69: end program

 71: !/*TEST
 72: !
 73: !     test:
 74: !       nsize: 2
 75: !       output_file: output/ex16_1.out
 76: !
 77: !TEST*/