mem_for_mpi_test Program

Uses

  • program~~mem_for_mpi_test~~UsesGraph program~mem_for_mpi_test mem_for_mpi_test mem_for_mpi_module mem_for_mpi_module program~mem_for_mpi_test->mem_for_mpi_module mpi mpi program~mem_for_mpi_test->mpi iso_c_binding iso_c_binding program~mem_for_mpi_test->iso_c_binding hvs_sizeof_module hvs_sizeof_module program~mem_for_mpi_test->hvs_sizeof_module

Calls

program~~mem_for_mpi_test~~CallsGraph program~mem_for_mpi_test mem_for_mpi_test mpi_comm_rank mpi_comm_rank program~mem_for_mpi_test->mpi_comm_rank free_mpif_mem free_mpif_mem program~mem_for_mpi_test->free_mpif_mem mpi_recv mpi_recv program~mem_for_mpi_test->mpi_recv mpi_finalize mpi_finalize program~mem_for_mpi_test->mpi_finalize mpi_init mpi_init program~mem_for_mpi_test->mpi_init mpi_send mpi_send program~mem_for_mpi_test->mpi_send alloc_mpif_mem alloc_mpif_mem program~mem_for_mpi_test->alloc_mpif_mem

Contents

Source Code


Variables

Type AttributesNameInitial
integer :: iError
type(c_ptr) :: buffer
integer, pointer:: fordat(:)
integer :: intsize
integer :: myrank
integer :: rstat(MPI_STATUS_SIZE)
logical :: success

Source Code

program mem_for_mpi_test
  use, intrinsic :: iso_c_binding, only: c_ptr, c_f_pointer
  use hvs_sizeof_module, only: c_sizeof
  use mpi
  use mem_for_mpi_module

  implicit none

  integer          :: iError
  type(c_ptr)      :: buffer
  integer, pointer :: fordat(:)
  integer          :: intsize
  integer          :: myrank
  integer          :: rstat(MPI_STATUS_SIZE)
  logical          :: success

  call MPI_Init(iError)

  call MPI_Comm_rank(MPI_COMM_WORLD, myrank, iError)

  if (myrank == 0) then
    write(*,*) 'Starting test for alloc_mpif_mem'
  end if
  intsize = c_sizeof(iError)

  ! Allocate 10 integers:
  call alloc_mpif_mem( asize   = intsize*10_MPI_ADDRESS_KIND, &
    &                  baseptr = buffer,                      &
    &                  ierr    = iError                       )
  if (myrank == 0) then
    write(*,*) 'Allocated memory with iError=', iError

    write(*,*) 'Converting C pointer to Fortran array'
  end if
  call c_f_pointer(buffer, fordat, [10])

  if (myrank == 0) then
    write(*,*) 'Assigning a value to the buffer with size:', size(fordat)
    fordat = 42
    write(*,*) 'Sending data from rank 0 to rank 1'
    call MPI_Send(fordat, 10, MPI_Integer, 1, 23, MPI_COMM_WORLD, iError)
    call MPI_Recv(success, 1, MPI_Logical, 1, 32, MPI_COMM_WORLD, rstat, iError)
    if (success) then
      write(*,*) 'PASSED'
    else
      write(*,*) 'FAILED'
    end if
  else
    call MPI_Recv(fordat, 10, MPI_Integer, 0, 23, MPI_COMM_WORLD, rstat, iError)
    if (all(fordat == 42)) then
      nullify(fordat)
      call free_mpif_mem(buffer)
      success = .true.
    else
      success = .false.
    end if
    call MPI_Send(success, 1, MPI_Logical, 0, 32, MPI_COMM_WORLD, iError)
  end if

  call MPI_Finalize(iError)

end program mem_for_mpi_test