Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: [OMPI users] Fortran MPI Struct with Allocatable Array
From: Jeremy Roberts (j.alyn.roberts_at_[hidden])
Date: 2010-07-30 16:02:43

I'm trying to parallelize a Fortran code with rather complicated derived
types full of pointer arrays. When I build the MPI type for sending, all
the static components are sent, but the pointer arrays are not (and retain
initial values). I imagine this has to do with memory addresses when
creating the MPI struct, but I have no idea how to fix it.

I've included a simple code illustrating my issue below. Any suggestions?


program mpi_struct_example
      use mpi
      implicit none
      ! declarations
      type :: small
          real, pointer :: array(:)
      end type small
      type(small) :: lala
      integer :: stat, counts(1), types(1), ierr, iam, n=0, MPI_SMALL
      integer (kind=MPI_ADDRESS_KIND) :: displs(1)
      ! initialize MPI and get my rank
      call MPI_INIT( ierr )
      call MPI_COMM_RANK( MPI_COMM_WORLD, iam, ierr )
      n = 20
      allocate( lala%array(n) )
      lala%array = 2.0
      ! build block counts, displacements, and oldtypes
      counts = (/n/)
      displs = (/0/)
      types = (/MPI_REAL/)
      ! make and commit new type
      call MPI_TYPE_CREATE_STRUCT( 1, counts, displs, types, MPI_SMALL, ierr
      call MPI_TYPE_COMMIT( MPI_SMALL, ierr )
      if (iam .eq. 0) then
            ! reset the value of the array
            lala%array = 1.0
            call MPI_SEND( lala, 1, MPI_SMALL, 1, 1, MPI_COMM_WORLD,
ierr) ! this doesn't work
            !call MPI_SEND( lala%array, n, MPI_REAL, 1, 1, MPI_COMM_WORLD,
ierr) ! this does work
            write (*,*) "iam ",iam," and lala%array(1) = ", lala%array(1)
            call MPI_RECV( lala, 1, MPI_SMALL, 0, 1, MPI_COMM_WORLD, stat,
ierr ) ! this doesn't work
            !call MPI_RECV( lala%array, n, MPI_REAL, 0, 1, MPI_COMM_WORLD,
stat, ierr ) ! this does work
            write (*,*) "iam ",iam," and lala%array(1) = ", lala%array(1),
" ( should be 1.0)"
      end if
      call MPI_FINALIZE(ierr)
end program mpi_struct_example