Actual source code: ex4f.F90

  1: program main
  2: #include <petsc/finclude/petscvec.h>
  3: #include <petsc/finclude/petscmat.h>

  5: use petscvec
  6: use petscmat

  8: implicit none

 10:   Mat             A
 11:   PetscInt,parameter ::  n=5,m=5
 12:   PetscScalar,parameter ::  two =2.0, one = 1.0
 13:   PetscInt,pointer,dimension(:) ::  dnnz,onnz
 14:   PetscInt    ::  i,rstart,rend,M1,N1
 15:   PetscErrorCode  ierr

 17:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

 19:   if (ierr /= 0) then
 20:    print*,'PetscInitialize failed'
 21:    stop
 22:   endif

 24:   allocate(dnnz(0:m-1))
 25:   allocate(onnz(0:m-1))

 27:   do i=0,m-1
 28:    dnnz(i) = 1
 29:    onnz(i) = 1
 30:   end do

 32:   call MatCreateAIJ(PETSC_COMM_WORLD,m,n,PETSC_DETERMINE,PETSC_DETERMINE,PETSC_DECIDE,dnnz,PETSC_DECIDE,onnz,A,ierr);CHKERRA(ierr)
 33:   call MatSetFromOptions(A,ierr);CHKERRA(ierr)
 34:   call MatSetUp(A,ierr);CHKERRA(ierr)
 35:   deallocate(dnnz)
 36:   deallocate(onnz)

 38:   !/* This assembly shrinks memory because we do not insert enough number of values */
 39:   call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr);CHKERRA(ierr)
 40:   call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr);CHKERRA(ierr)

 42:   !/* MatResetPreallocation restores the memory required by users */
 43:   call MatResetPreallocation(A,ierr);CHKERRA(ierr)
 44:   call MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE,ierr);CHKERRA(ierr)
 45:   call MatGetOwnershipRange(A,rstart,rend,ierr);CHKERRA(ierr)
 46:   call MatGetSize(A,M1,N1,ierr);CHKERRA(ierr)
 47:   do i=rstart,rend-1
 48:    call MatSetValue(A,i,i,two,INSERT_VALUES,ierr);CHKERRA(ierr)
 49:    if (rend<N1) call MatSetValue(A,i,rend,one,INSERT_VALUES,ierr);CHKERRA(ierr)
 50:   end do
 51:   call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr);CHKERRA(ierr)
 52:   call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr);CHKERRA(ierr)
 53:   call MatView(A,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)
 54:   call MatDestroy(A,ierr);CHKERRA(ierr)
 55:   call PetscFinalize(ierr);CHKERRA(ierr)

 57: end program

 59: !/*TEST
 60: !
 61: !   test:
 62: !      suffix: 1
 63: !      output_file: output/ex4_1.out
 64: !
 65: !   test:
 66: !      suffix: 2
 67: !      nsize: 2
 68: !      output_file: output/ex4_2.out
 69: !
 70: !TEST*/