<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Shared MPI (MPI-3) Deallocate Error in Intel® Moderncode for Parallel Architectures</title>
    <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/Shared-MPI-MPI-3-Deallocate-Error/m-p/1163927#M7992</link>
    <description>&lt;P&gt;Good day,&lt;/P&gt;

&lt;P&gt;I'm working with Shared MPI (MPI-3) in Fortran and I came across an odd deallocation error, so I'm reaching out to the community for assistance. I've followed the&lt;A href="https://stackoverflow.com/questions/24797298/mpi-fortran-code-how-to-share-data-on-node-via-openmp"&gt; Shared MPI Fortran example&lt;/A&gt; provided on StackOverflow that works very well when the shared arrays are declared and destroyed from the main program; however, should one of these arrays be created/destroyed&amp;nbsp;within a routine, a deallocation error occurs. To illustrate this point, I've created a simple working example, which needs 3 files: &lt;EM&gt;main&lt;/EM&gt;, &lt;EM&gt;mod_setup&lt;/EM&gt;, &lt;EM&gt;mod_work &lt;/EM&gt;(code provided below).&lt;/P&gt;

&lt;P&gt;In main, variable&amp;nbsp;&lt;EM&gt;b&lt;/EM&gt;&amp;nbsp;is created (a 2D matrix size m x n) using &lt;EM&gt;mpi_win_allocate_shared&lt;/EM&gt;; &lt;EM&gt;b&lt;/EM&gt; is passed to the &lt;EM&gt;setup &lt;/EM&gt;module, and variable &lt;EM&gt;a&lt;/EM&gt;&amp;nbsp;is created (3D matrix sized m x n x n) also using &lt;EM&gt;mpi_win_allocate_shared&lt;/EM&gt;. Both &lt;EM&gt;a&amp;nbsp;&lt;/EM&gt;and&amp;nbsp;&lt;EM&gt;b&lt;/EM&gt;&amp;nbsp;are then passed to the &lt;EM&gt;work &lt;/EM&gt;module where a simple update is performed on the data and&amp;nbsp;&lt;EM&gt;work&amp;nbsp;&lt;/EM&gt;returns without error. Before returning from &lt;EM&gt;setup&lt;/EM&gt;, variable &lt;EM&gt;a&amp;nbsp;&lt;/EM&gt;is deallocated using &lt;EM&gt;mpi_win_free&lt;/EM&gt;, which returns an error code of 0, but then when exiting &lt;EM&gt;setup&lt;/EM&gt;, a deallocation error is thrown (for variable &lt;EM&gt;a&lt;/EM&gt;).&lt;/P&gt;

&lt;P&gt;This error should not occur because &lt;EM&gt;mpi_win_free&lt;/EM&gt; is expected to perform the deallocation, correct?&amp;nbsp;&lt;SPAN style="font-size: 13.008px;"&gt;Can shared arrays only be allocated from the main program? Is there a way to avoid this error?&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;Thank you for your assistance,&lt;BR /&gt;
	Gary&lt;/P&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;[glaws003@turing1 simp]$ make&lt;BR /&gt;
		mpiifort -fpe0 -traceback mod_work.F -c&lt;BR /&gt;
		mpiifort -fpe0 -traceback mod_setup.F -c&lt;BR /&gt;
		mpiifort -fpe0 -traceback main.F -I. -c&lt;BR /&gt;
		mpiifort -fpe0 -traceback mod_work.o mod_setup.o main.o -o simp.x&lt;/P&gt;

	&lt;P&gt;&lt;BR /&gt;
		[glaws003@turing1 simp]$ mpirun -np 2 ./simp.x&lt;BR /&gt;
		&amp;nbsp;srank: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 of &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;
		&amp;nbsp;srank: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 of &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;
		&amp;nbsp;start: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 end: &amp;nbsp; &amp;nbsp; &amp;nbsp;500000&lt;BR /&gt;
		&amp;nbsp;start: &amp;nbsp; &amp;nbsp; &amp;nbsp;500001 end: &amp;nbsp; &amp;nbsp; 1000000&lt;BR /&gt;
		&amp;nbsp;SMPI deallocation- a with error code: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;
		&lt;STRONG&gt;forrtl: severe (173): A pointer passed to DEALLOCATE points to an object that cannot be deallocated&lt;/STRONG&gt;&lt;BR /&gt;
		Image &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PC &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Routine &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Line &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Source &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 000000000040B7E8 &amp;nbsp;Unknown &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 000000000040465F &amp;nbsp;mod_setup_mp_setu &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;59 &amp;nbsp;mod_setup.F&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000000000404927 &amp;nbsp;MAIN__ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50 &amp;nbsp;main.F&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000000000403BDE &amp;nbsp;Unknown &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;BR /&gt;
		libc-2.12.so &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000003134E1ED1D &amp;nbsp;__libc_start_main &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000000000403AE9 &amp;nbsp;Unknown &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;/P&gt;

	&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;main.F&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;PRE class="brush:fortran;"&gt;! A Simple Working Example of an SMPI Deallocation Error
!
      program smpi_deallocation_error
      use mod_setup
      use, intrinsic :: iso_c_binding
      include 'mpif.h'
      integer, parameter :: rtype = selected_real_kind(15, 307)
      !
      integer :: m=1000000, n=11, rnk
      integer :: rank,nranks,scomm,srank,nsranks,mpierr
      real(rtype), dimension(:, :), allocatable :: b
      integer :: bwin,disp_unit=1,smpi_root=1
      integer, dimension(2) :: shp
      type(c_ptr)           :: array_ptr
      integer(kind=mpi_address_kind)   :: ln
      common/mpi_blk/ rank,nranks,scomm,srank,nsranks,mpierr
      !
      ! MPI Initialization
      call mpi_init(mpierr)
      call mpi_comm_rank(mpi_comm_world,rank,mpierr)
      call mpi_comm_size(mpi_comm_world,nranks,mpierr)
      !
      ! SMPI Initialization 
      call mpi_comm_split_type(mpi_comm_world,mpi_comm_type_shared, 
     &amp;amp; 0,mpi_info_null,scomm,mpierr)
      call mpi_comm_rank(scomm,srank,mpierr)
      call mpi_comm_size(scomm,nsranks,mpierr)
      write(*,*) 'srank:',srank,'of ',nsranks
      !
      shp(1) = m
      shp(2) = n
      ! 1.    declare size of memory window
      if (srank == smpi_root) then    ! If master shared proc
         ln=int(shp(1)*shp(2),mpi_address_kind)*8_mpi_address_kind
      else                              ! If slave shared proc
         ln=0_mpi_address_kind
      end if
      ! 
      ! 2.    allocate memory (if smpi rank 0)
      call mpi_win_allocate_shared(ln,disp_unit,mpi_info_null,scomm,
     &amp;amp; array_ptr,bwin,mpierr)
      !
      ! 3.    query memory window (if smpi &amp;gt; 0)
      if (srank &amp;gt; smpi_root) then
         call mpi_win_shared_query(bwin,smpi_root,ln,disp_unit,
     &amp;amp; array_ptr,mpierr)
      end if
      !
      ! 4.    convert C pointer to Fortran pointer
      call c_f_pointer(array_ptr,b,shp)
      !
      call setup_work(n, m, b)
      !
      ! Clean up SMPI memory windows
      call mpi_win_free(bwin,mpierr)
      write(*,*) 'SMPI deallocation- b has error code:',mpierr
      call mpi_finalize(mpierr)
      !
      end program smpi_deallocation_error&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;mod_setup.F&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;PRE class="brush:fortran;"&gt;! A Simple Working Example of an SMPI Deallocation Error
!
      module mod_setup
        use mod_work
        use, intrinsic :: iso_c_binding
        include 'mpif.h'
        private
        public :: setup_work
        !
      contains
        !
      subroutine setup_work(n, m, b)
        integer, parameter :: rtype = selected_real_kind(15, 307)
        integer,   intent(in) :: n, m
        real(rtype), dimension(1:m,1:n), intent(inout) :: b
        !
        real(rtype), allocatable, dimension(:,:,:)  :: a
        !
        integer :: i,j,k,awin,disp_unit=1,smpi_root=1
        integer :: rank,nranks,scomm,srank,nsranks,mpierr
        integer, dimension(3) :: shp
        type(c_ptr)           :: array_ptr
        integer(kind=mpi_address_kind)   :: ln
        common/mpi_blk/ rank,nranks,scomm,srank,nsranks,mpierr
        !
      continue
        !
        shp(1) = m
        shp(2) = n
        shp(3) = n
        ! 1.    declare size of memory window
        if (srank == smpi_root) then    ! If master shared proc
          ln=int(shp(1)*shp(2)*shp(3),mpi_address_kind)
     &amp;amp; *8_mpi_address_kind
        else                              ! If slave shared proc
           ln=0_mpi_address_kind
        end if
        ! 
        ! 2.    allocate memory (if smpi rank 0)
        call mpi_win_allocate_shared(ln,disp_unit,mpi_info_null,scomm,
     &amp;amp; array_ptr,awin,mpierr)
        !
        ! 3.    query memory window (if smpi &amp;gt; 0)
        if (srank &amp;gt; smpi_root) then
           call mpi_win_shared_query(awin,smpi_root,ln,disp_unit,
     &amp;amp; array_ptr,mpierr)
        end if
        !
        ! 4.    convert C pointer to Fortran pointer
        call c_f_pointer(array_ptr,a,shp)
        !
        ! Do work
        call do_work(n, m, a, b)
        !
        ! Clean up SMPI memory windows
        call mpi_win_free(awin,mpierr)
        write(*,*) 'SMPI deallocation- a with error code:',mpierr
        !
        ! SMPI error occurs as this routine ends
      end subroutine setup_work 
      !
      end module mod_setup
&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;mod_work.F&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;PRE class="brush:fortran;"&gt;! A Simple Working Example of an SMPI Deallocation Error
!
      module mod_work
        include 'mpif.h'
        private
        public :: do_work
        !
      contains
        !
      subroutine do_work(n, m, a, b)
        integer, parameter :: rtype = selected_real_kind(15, 307)
        integer,   intent(in) :: n, m
        real(rtype), dimension(1:m,1:n,1:n), intent(inout) :: a
        real(rtype), dimension(1:m,1:n), intent(inout) :: b
        !
        integer :: i,j,k,ms,me
        integer :: rank,nranks,scomm,srank,nsranks,mpierr
        common/mpi_blk/ rank,nranks,scomm,srank,nsranks,mpierr
        !
      continue
        !
        ! Divide work among shared MPI ranks
        ms = 1 + (srank * (m/nsranks))
        me = (1 + srank) * (m/nsranks)
        if (srank == nsranks-1) then  me = m end if
        write(*,*) 'start:',ms,'end:',me
        !
        do k=1,n
          do j=1,n
            do i=ms,me
              a(i,j,k) = i + j * k
              if (k == 1) then
                b(i,j) = i + j
              end if
            end do
          end do
        end do
        !
      end subroutine do_work
      !
      end module mod_work
&lt;/PRE&gt;

	&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Aug 2017 21:44:42 GMT</pubDate>
    <dc:creator>Gary_L_</dc:creator>
    <dc:date>2017-08-08T21:44:42Z</dc:date>
    <item>
      <title>Shared MPI (MPI-3) Deallocate Error</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/Shared-MPI-MPI-3-Deallocate-Error/m-p/1163927#M7992</link>
      <description>&lt;P&gt;Good day,&lt;/P&gt;

&lt;P&gt;I'm working with Shared MPI (MPI-3) in Fortran and I came across an odd deallocation error, so I'm reaching out to the community for assistance. I've followed the&lt;A href="https://stackoverflow.com/questions/24797298/mpi-fortran-code-how-to-share-data-on-node-via-openmp"&gt; Shared MPI Fortran example&lt;/A&gt; provided on StackOverflow that works very well when the shared arrays are declared and destroyed from the main program; however, should one of these arrays be created/destroyed&amp;nbsp;within a routine, a deallocation error occurs. To illustrate this point, I've created a simple working example, which needs 3 files: &lt;EM&gt;main&lt;/EM&gt;, &lt;EM&gt;mod_setup&lt;/EM&gt;, &lt;EM&gt;mod_work &lt;/EM&gt;(code provided below).&lt;/P&gt;

&lt;P&gt;In main, variable&amp;nbsp;&lt;EM&gt;b&lt;/EM&gt;&amp;nbsp;is created (a 2D matrix size m x n) using &lt;EM&gt;mpi_win_allocate_shared&lt;/EM&gt;; &lt;EM&gt;b&lt;/EM&gt; is passed to the &lt;EM&gt;setup &lt;/EM&gt;module, and variable &lt;EM&gt;a&lt;/EM&gt;&amp;nbsp;is created (3D matrix sized m x n x n) also using &lt;EM&gt;mpi_win_allocate_shared&lt;/EM&gt;. Both &lt;EM&gt;a&amp;nbsp;&lt;/EM&gt;and&amp;nbsp;&lt;EM&gt;b&lt;/EM&gt;&amp;nbsp;are then passed to the &lt;EM&gt;work &lt;/EM&gt;module where a simple update is performed on the data and&amp;nbsp;&lt;EM&gt;work&amp;nbsp;&lt;/EM&gt;returns without error. Before returning from &lt;EM&gt;setup&lt;/EM&gt;, variable &lt;EM&gt;a&amp;nbsp;&lt;/EM&gt;is deallocated using &lt;EM&gt;mpi_win_free&lt;/EM&gt;, which returns an error code of 0, but then when exiting &lt;EM&gt;setup&lt;/EM&gt;, a deallocation error is thrown (for variable &lt;EM&gt;a&lt;/EM&gt;).&lt;/P&gt;

&lt;P&gt;This error should not occur because &lt;EM&gt;mpi_win_free&lt;/EM&gt; is expected to perform the deallocation, correct?&amp;nbsp;&lt;SPAN style="font-size: 13.008px;"&gt;Can shared arrays only be allocated from the main program? Is there a way to avoid this error?&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;Thank you for your assistance,&lt;BR /&gt;
	Gary&lt;/P&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;[glaws003@turing1 simp]$ make&lt;BR /&gt;
		mpiifort -fpe0 -traceback mod_work.F -c&lt;BR /&gt;
		mpiifort -fpe0 -traceback mod_setup.F -c&lt;BR /&gt;
		mpiifort -fpe0 -traceback main.F -I. -c&lt;BR /&gt;
		mpiifort -fpe0 -traceback mod_work.o mod_setup.o main.o -o simp.x&lt;/P&gt;

	&lt;P&gt;&lt;BR /&gt;
		[glaws003@turing1 simp]$ mpirun -np 2 ./simp.x&lt;BR /&gt;
		&amp;nbsp;srank: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 of &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;
		&amp;nbsp;srank: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 of &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;
		&amp;nbsp;start: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 end: &amp;nbsp; &amp;nbsp; &amp;nbsp;500000&lt;BR /&gt;
		&amp;nbsp;start: &amp;nbsp; &amp;nbsp; &amp;nbsp;500001 end: &amp;nbsp; &amp;nbsp; 1000000&lt;BR /&gt;
		&amp;nbsp;SMPI deallocation- a with error code: &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;
		&lt;STRONG&gt;forrtl: severe (173): A pointer passed to DEALLOCATE points to an object that cannot be deallocated&lt;/STRONG&gt;&lt;BR /&gt;
		Image &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PC &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Routine &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Line &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Source &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 000000000040B7E8 &amp;nbsp;Unknown &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 000000000040465F &amp;nbsp;mod_setup_mp_setu &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;59 &amp;nbsp;mod_setup.F&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000000000404927 &amp;nbsp;MAIN__ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50 &amp;nbsp;main.F&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000000000403BDE &amp;nbsp;Unknown &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;BR /&gt;
		libc-2.12.so &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000003134E1ED1D &amp;nbsp;__libc_start_main &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;BR /&gt;
		simp.x &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0000000000403AE9 &amp;nbsp;Unknown &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Unknown &amp;nbsp;Unknown&lt;/P&gt;

	&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;main.F&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;PRE class="brush:fortran;"&gt;! A Simple Working Example of an SMPI Deallocation Error
!
      program smpi_deallocation_error
      use mod_setup
      use, intrinsic :: iso_c_binding
      include 'mpif.h'
      integer, parameter :: rtype = selected_real_kind(15, 307)
      !
      integer :: m=1000000, n=11, rnk
      integer :: rank,nranks,scomm,srank,nsranks,mpierr
      real(rtype), dimension(:, :), allocatable :: b
      integer :: bwin,disp_unit=1,smpi_root=1
      integer, dimension(2) :: shp
      type(c_ptr)           :: array_ptr
      integer(kind=mpi_address_kind)   :: ln
      common/mpi_blk/ rank,nranks,scomm,srank,nsranks,mpierr
      !
      ! MPI Initialization
      call mpi_init(mpierr)
      call mpi_comm_rank(mpi_comm_world,rank,mpierr)
      call mpi_comm_size(mpi_comm_world,nranks,mpierr)
      !
      ! SMPI Initialization 
      call mpi_comm_split_type(mpi_comm_world,mpi_comm_type_shared, 
     &amp;amp; 0,mpi_info_null,scomm,mpierr)
      call mpi_comm_rank(scomm,srank,mpierr)
      call mpi_comm_size(scomm,nsranks,mpierr)
      write(*,*) 'srank:',srank,'of ',nsranks
      !
      shp(1) = m
      shp(2) = n
      ! 1.    declare size of memory window
      if (srank == smpi_root) then    ! If master shared proc
         ln=int(shp(1)*shp(2),mpi_address_kind)*8_mpi_address_kind
      else                              ! If slave shared proc
         ln=0_mpi_address_kind
      end if
      ! 
      ! 2.    allocate memory (if smpi rank 0)
      call mpi_win_allocate_shared(ln,disp_unit,mpi_info_null,scomm,
     &amp;amp; array_ptr,bwin,mpierr)
      !
      ! 3.    query memory window (if smpi &amp;gt; 0)
      if (srank &amp;gt; smpi_root) then
         call mpi_win_shared_query(bwin,smpi_root,ln,disp_unit,
     &amp;amp; array_ptr,mpierr)
      end if
      !
      ! 4.    convert C pointer to Fortran pointer
      call c_f_pointer(array_ptr,b,shp)
      !
      call setup_work(n, m, b)
      !
      ! Clean up SMPI memory windows
      call mpi_win_free(bwin,mpierr)
      write(*,*) 'SMPI deallocation- b has error code:',mpierr
      call mpi_finalize(mpierr)
      !
      end program smpi_deallocation_error&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;mod_setup.F&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;PRE class="brush:fortran;"&gt;! A Simple Working Example of an SMPI Deallocation Error
!
      module mod_setup
        use mod_work
        use, intrinsic :: iso_c_binding
        include 'mpif.h'
        private
        public :: setup_work
        !
      contains
        !
      subroutine setup_work(n, m, b)
        integer, parameter :: rtype = selected_real_kind(15, 307)
        integer,   intent(in) :: n, m
        real(rtype), dimension(1:m,1:n), intent(inout) :: b
        !
        real(rtype), allocatable, dimension(:,:,:)  :: a
        !
        integer :: i,j,k,awin,disp_unit=1,smpi_root=1
        integer :: rank,nranks,scomm,srank,nsranks,mpierr
        integer, dimension(3) :: shp
        type(c_ptr)           :: array_ptr
        integer(kind=mpi_address_kind)   :: ln
        common/mpi_blk/ rank,nranks,scomm,srank,nsranks,mpierr
        !
      continue
        !
        shp(1) = m
        shp(2) = n
        shp(3) = n
        ! 1.    declare size of memory window
        if (srank == smpi_root) then    ! If master shared proc
          ln=int(shp(1)*shp(2)*shp(3),mpi_address_kind)
     &amp;amp; *8_mpi_address_kind
        else                              ! If slave shared proc
           ln=0_mpi_address_kind
        end if
        ! 
        ! 2.    allocate memory (if smpi rank 0)
        call mpi_win_allocate_shared(ln,disp_unit,mpi_info_null,scomm,
     &amp;amp; array_ptr,awin,mpierr)
        !
        ! 3.    query memory window (if smpi &amp;gt; 0)
        if (srank &amp;gt; smpi_root) then
           call mpi_win_shared_query(awin,smpi_root,ln,disp_unit,
     &amp;amp; array_ptr,mpierr)
        end if
        !
        ! 4.    convert C pointer to Fortran pointer
        call c_f_pointer(array_ptr,a,shp)
        !
        ! Do work
        call do_work(n, m, a, b)
        !
        ! Clean up SMPI memory windows
        call mpi_win_free(awin,mpierr)
        write(*,*) 'SMPI deallocation- a with error code:',mpierr
        !
        ! SMPI error occurs as this routine ends
      end subroutine setup_work 
      !
      end module mod_setup
&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;mod_work.F&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;PRE class="brush:fortran;"&gt;! A Simple Working Example of an SMPI Deallocation Error
!
      module mod_work
        include 'mpif.h'
        private
        public :: do_work
        !
      contains
        !
      subroutine do_work(n, m, a, b)
        integer, parameter :: rtype = selected_real_kind(15, 307)
        integer,   intent(in) :: n, m
        real(rtype), dimension(1:m,1:n,1:n), intent(inout) :: a
        real(rtype), dimension(1:m,1:n), intent(inout) :: b
        !
        integer :: i,j,k,ms,me
        integer :: rank,nranks,scomm,srank,nsranks,mpierr
        common/mpi_blk/ rank,nranks,scomm,srank,nsranks,mpierr
        !
      continue
        !
        ! Divide work among shared MPI ranks
        ms = 1 + (srank * (m/nsranks))
        me = (1 + srank) * (m/nsranks)
        if (srank == nsranks-1) then  me = m end if
        write(*,*) 'start:',ms,'end:',me
        !
        do k=1,n
          do j=1,n
            do i=ms,me
              a(i,j,k) = i + j * k
              if (k == 1) then
                b(i,j) = i + j
              end if
            end do
          end do
        end do
        !
      end subroutine do_work
      !
      end module mod_work
&lt;/PRE&gt;

	&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2017 21:44:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/Shared-MPI-MPI-3-Deallocate-Error/m-p/1163927#M7992</guid>
      <dc:creator>Gary_L_</dc:creator>
      <dc:date>2017-08-08T21:44:42Z</dc:date>
    </item>
  </channel>
</rss>

