Showing results for 
Search instead for 
Did you mean: 
1 View

Seg fault when using MPI_TYPE_CONTIGUOUS


I have following code which gives a seg fault. I am sending a declared type containing 3 allocatable float variables using MPI_TYPE_CONTIGUOUS.


I start by allocating each member and then I declare and commit the mpi declared type. I send from rank 0 to rank 1 and do a print statement in the end to make sure I am receiving the expected value.

The print statement gives a segfault for rank 1. Does anybody know if it is allowed to use this approach for declared types with allocatable, and if there is a workaround.


module md_

   use mpi_f08

   integer numtasks, rank, tag, i,  ierr
   type(mpi_status) stat

   type T_PART
      real, allocatable :: x(:), y(:), z(:)
   end type


end module

program struct
   use md_
   implicit none
   type(t_part)       :: test_
   type(mpi_datatype) :: mpidt
   integer            :: sz, szz

   call MPI_INIT(ierr)
   call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
   call MPI_COMM_SIZE(MPI_COMM_WORLD, numtasks, ierr)

   tag = 1

   szz= 1
   allocate(test_% x(szz),test_% y(szz), test_% z(szz) )

   sz = sizeof(test_)
   call MPI_Type_contiguous(sz, MPI_BYTE ,mpidt, ierr)
   call MPI_TYPE_COMMIT(mpidt, ierr)

   if (rank .eq. 0) then
      do i=1,szz
         test_%x(i) = i*1+mod(i,3)
         test_%y(i) = i*2+mod(i,3)
         test_%z(i) = i*3+mod(i,3)
      end do

      call MPI_SEND(test_, 1, mpidt, 1, tag, &
                    MPI_COMM_WORLD, ierr)

         call MPI_RECV(test_, 1, mpidt, 0, tag, &
                    MPI_COMM_WORLD, stat, ierr)

   print *, 'rank= ',rank,' test_% x= ', test_%z(1) ! seg fault

   call mpi_barrier(MPI_COMM_WORLD, ierr)
   ! free datatype when done using it
   call MPI_TYPE_FREE(mpidt, ierr)
   call MPI_FINALIZE(ierr)



0 Kudos