Question

This is related to the earlier thread on "Using MPI_Send/Recv to handle chunk of multi-dim array in Fortran 90". My array is real and 3-dim say 5 by 5 by 5, i.e x(1:5,1:5,1:5) If I want to send the following part of my array say

x(2:3,2:5,4:5)

to Proc 1 from 0, I am using the following test program

program mpi
implicit none
include "mpif.h"

integer :: ierr,myid,nprocs,status(mpi_status_size),i,j,k,&
&          starts(3),newsize(3),oldsize(3)
real    :: x(1:5,1:5,1:5),y(1:5,1:5,1:5),z(2:3,2:5,4:5)
integer :: arr

call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world,myid,ierr)
call mpi_comm_size(mpi_comm_world,nprocs,ierr)

if(myid == 0) then
  x = 0.0
  call random_number(x)
  starts = (/2,2,4/)
  newsize = (/2,4,2/)
  oldsize = (/5,5,5/)
  call mpi_type_create_subarray(3,oldsize,newsize,starts,mpi_order_fortran, &
  & mpi_real,arr,ierr)
  call mpi_type_commit(arr,ierr)
  call mpi_send(x,1,arr,1,1,mpi_comm_world,ierr)
  do i = 2,3
    do j = 2,5
      do k = 4,5
        print*,'#1',x(i,j,k)
      enddo
    enddo
  enddo
  print*,' '
else
  y = 0.0
  call mpi_recv(z,16,mpi_real,0,1,mpi_comm_world,status,ierr)
  do i = 2,3
    do j = 2,5
      do k = 4,5
        print*,'#2',z(i,j,k)
      enddo
    enddo
  enddo
endif

call mpi_finalize(ierr)

stop
end

I am getting runtime errors related to the 'start' array. It's elements must be 0 or 1. What am I missing here? What is the correct form for this? I could not find a FORTRAN example for using this.

Was it helpful?

Solution

The standard specifies that the array_of_starts (your starts) starts indexing at zero, not at one! So if a Fortran numbering starts at one, you have to subtract 1. Consequently, your starts array should be (/ 1, 1, 3 /).

From the MPI 3.0 Standard, Ch. 4.1.3:

Advice to users. In a Fortran program with arrays indexed starting from 1, if the starting coordinate of a particular dimension of the subarray is N, then the entry in array_of_starts for that dimension is n-1. (End of advice to users.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top