<div dir="ltr"><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:arial,"helvetica neue",helvetica,sans-serif"><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px">My aim is given any m*n matrix (rectangular or square) and given any number of processors..the matrix should be scattered into chunks/ sub arrays into each processor. </span><br></p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:arial,"helvetica neue",helvetica,sans-serif">I want to scatter matrix from root to other processors using scatterv. I am creating a communicator topology using <em style="margin:0px;padding:0px;border:0px">mpi_cart_create</em>. As an example I have the below code in fortran:</p><pre style="white-space:pre-wrap;margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;background-color:rgb(239,240,241);word-wrap:normal;color:rgb(36,39,41)"><code style="margin:0px;padding:0px;border:0px;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;white-space:inherit">PROGRAM SendRecv
USE mpi
IMPLICIT none
integer, PARAMETER :: m = 4, n = 4
integer, DIMENSION(m,n) :: a, b,h
integer :: i,j,count
integer,allocatable, dimension(:,:):: loc ! local piece of global 2d array
INTEGER :: istatus(MPI_STATUS_SIZE),ierr
integer, dimension(2) :: sizes, subsizes, starts
INTEGER :: ista,iend,jsta,jend,ilen,jlen
INTEGER :: iprocs, jprocs, nprocs
integer,allocatable,dimension(<wbr>:):: rcounts, displs
INTEGER :: rcounts0,displs0
integer, PARAMETER :: ROOT = 0
integer :: dims(2),coords(2)
logical :: periods(2)
data periods/2*.false./
integer :: status(MPI_STATUS_SIZE)
integer :: comm2d,source,myrank
integer :: newtype, resizedtype
integer :: comsize,charsize
integer(kind=MPI_ADDRESS_KIND) :: extent, begin
CALL MPI_INIT(ierr)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD, nprocs, ierr)
CALL MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)
! Get a new communicator for a decomposition of the domain.
dims(1) = 0
dims(2) = 0
CALL MPI_DIMS_CREATE(nprocs,2,dims,<wbr>ierr)
if (myrank.EQ.Root) then
print *,nprocs,'processors have been arranged into',dims(1),'X',dims(2),'<wbr>grid'
endif
CALL MPI_CART_CREATE(MPI_COMM_<wbr>WORLD,2,dims,periods,.true., &
comm2d,ierr)
! Get my position in this communicator
CALL MPI_COMM_RANK(comm2d,myrank,<wbr>ierr)
! Get the decomposition
CALL fnd2ddecomp(comm2d,m,n,ista,<wbr>iend,jsta,jend)
! print *,ista,jsta,iend,jend
ilen = iend - ista + 1
jlen = jend - jsta + 1
CALL MPI_Cart_get(comm2d,2,dims,<wbr>periods,coords,ierr)
iprocs = dims(1)
jprocs = dims(2)
! define the global matrix
if (myrank==ROOT) then
count = 0
do j = 1,n
do i = 1,m
a(i,j) = count
count = count+1
enddo
enddo
print *, 'global matrix is: '
do 90 i=1,m
do 80 j = 1,n
write(*,70)a(i,j)
70 format(2x,I5,$)
80 continue
print *, ' '
90 continue
endif
call MPI_Barrier(MPI_COMM_WORLD, ierr)
starts = [0,0]
sizes = [m, n]
subsizes = [ilen, jlen]
call MPI_Type_create_subarray(2, sizes, subsizes, starts, &
MPI_ORDER_FORTRAN, MPI_INTEGER, &
newtype, ierr)
call MPI_Type_size(MPI_INTEGER, charsize, ierr)
begin = 0
extent = charsize
call MPI_Type_create_resized(<wbr>newtype, begin, extent, resizedtype, ierr)
call MPI_Type_commit(resizedtype, ierr)
! get counts and displacmeents
allocate(rcounts(nprocs),<wbr>displs(nprocs))
rcounts0 = 1
displs0 = (ista-1) + (jsta-1)*m
CALL MPI_Allgather(rcounts0,1,MPI_<wbr>INT,rcounts,1,MPI_INT,MPI_<wbr>COMM_WORLD,IERR)
CALL MPI_Allgather(displs0,1,MPI_<wbr>INT,displs,1,MPI_INT,MPI_COMM_<wbr>WORLD,IERR)
CALL MPI_Barrier(MPI_COMM_WORLD, ierr)
! scatter data
allocate(loc(ilen,jlen))
call MPI_Scatterv(a,rcounts,displs,<wbr>resizedtype, &
loc,ilen*jlen,MPI_INTEGER, &
ROOT,MPI_COMM_WORLD,ierr)
! print each processor matrix
do source = 0,nprocs-1
if (myrank.eq.source) then
print *,'myrank:',source
do i=1,ilen
do j = 1,jlen
write(*,701)loc(i,j)
701 format(2x,I5,$)
enddo
print *, ' '
enddo
endif
call MPI_Barrier(MPI_COMM_WORLD, ierr)
enddo
call MPI_Type_free(newtype,ierr)
call MPI_Type_free(resizedtype,<wbr>ierr)
deallocate(rcounts,displs)
deallocate(loc)
CALL MPI_FINALIZE(ierr)
contains
subroutine fnd2ddecomp(comm2d,m,n,ista,<wbr>iend,jsta,jend)
integer comm2d
integer m,n,ista,jsta,iend,jend
integer dims(2),coords(2),ierr
logical periods(2)
! Get (i,j) position of a processor from Cartesian topology.
CALL MPI_Cart_get(comm2d,2,dims,<wbr>periods,coords,ierr)
! Decomposition in first (ie. X) direction
CALL MPE_DECOMP1D(m,dims(1),coords(<wbr>1),ista,iend)
! Decomposition in second (ie. Y) direction
CALL MPE_DECOMP1D(n,dims(2),coords(<wbr>2),jsta,jend)
end subroutine fnd2ddecomp
SUBROUTINE MPE_DECOMP1D(n,numprocs,myid,<wbr>s,e)
integer n,numprocs,myid,s,e,nlocal,<wbr>deficit
nlocal = n / numprocs
s = myid * nlocal + 1
deficit = mod(n,numprocs)
s = s + min(myid,deficit)
! Give one more slice to processors
if (myid .lt. deficit) then
nlocal = nlocal + 1
endif
e = s + nlocal - 1
if (e .gt. n .or. myid .eq. numprocs-1) e = n
end subroutine MPE_DECOMP1D
END program SendRecv
</code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:arial,"helvetica neue",helvetica,sans-serif">I am generating a 4x4 matrix, and using scatterv I am sending the blocks of matrices to other processors. Code works fine for 4,2 and 16 processors. But throws a error for three processors. What modifications I have to do make it work for any number of given processors.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:arial,"helvetica neue",helvetica,sans-serif">Global matrix in Root:</p><pre style="white-space:pre-wrap;margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;background-color:rgb(239,240,241);word-wrap:normal;color:rgb(36,39,41)"><code style="margin:0px;padding:0px;border:0px;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;white-space:inherit">[ 0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15 ]
</code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:arial,"helvetica neue",helvetica,sans-serif">For 4 processors each processors gets.</p><pre style="white-space:pre-wrap;margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;background-color:rgb(239,240,241);word-wrap:normal;color:rgb(36,39,41)"><code style="margin:0px;padding:0px;border:0px;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;white-space:inherit">Rank =0 : [0 4
1 5]
Rank =1 : [8 12
9 13]
Rank =2 : [2 6
3 7]
Rank =3 : [10 14
11 15]
</code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:arial,"helvetica neue",helvetica,sans-serif">Code works for 4, 2 and 16 processors; in fact it works when sub-arrays are of similar size. It fails for 3 processors. For 3 processors I am expecting:</p><pre style="white-space:pre-wrap;margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;background-color:rgb(239,240,241);word-wrap:normal;color:rgb(36,39,41)"><code style="margin:0px;padding:0px;border:0px;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;white-space:inherit">Rank =0 : [0 4 8 12
1 5 9 13]
Rank =1 : [2 6 10 14]
Rank =2 : [3 7 11 15]
</code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:arial,"helvetica neue",helvetica,sans-serif">But I am getting the following error. Where I am missing? what modifications I have to make to make it work. </p><pre style="white-space:pre-wrap;margin-top:0px;margin-bottom:1em;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;background-color:rgb(239,240,241);word-wrap:normal;color:rgb(36,39,41)"><code style="margin:0px;padding:0px;border:0px;font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;white-space:inherit">Fatal error in PMPI_Scatterv: Message truncated, error stack:
PMPI_Scatterv(671)............<wbr>....: MPI_Scatterv(sbuf=0x6b58c0, scnts=0xf95d90, displs=0xfafbe0, dtype=USER<resized>, rbuf=0xfafc00, rcount=4, MPI_INTEGER, root=0, MPI_COMM_WORLD) failed
MPIR_Scatterv_impl(211).......<wbr>....:
I_MPIR_Scatterv_intra(278)....<wbr>....: Failure during collective
I_MPIR_Scatterv_intra(272)....<wbr>....:
MPIR_Scatterv(147)............<wbr>....:
MPIDI_CH3U_Receive_data_found(<wbr>131): Message from rank 0 and tag 6 truncated; 32 bytes received but buffer size is 16
Fatal error in PMPI_Scatterv: Message truncated, error stack:
PMPI_Scatterv(671)............<wbr>....: MPI_Scatterv(sbuf=0x6b58c0, scnts=0x240bda0, displs=0x240be60, dtype=USER<resized>, rbuf=0x240be80, rcount=4, MPI_INTEGER, root=0, MPI_COMM_WORLD) failed
MPIR_Scatterv_impl(211).......<wbr>....:
I_MPIR_Scatterv_intra(278)....<wbr>....: Failure during collective
I_MPIR_Scatterv_intra(272)....<wbr>....:
MPIR_Scatterv(147)............<wbr>....:
MPIDI_CH3U_Receive_data_found(<wbr>131): Message from rank 0 and tag 6 truncated; 32 bytes received but buffer size is 16
forrtl: error (69): process interrupted (SIGINT)
Image PC Routine Line Source
a.out 0000000000479165 Unknown Unknown Unknown
a.out 0000000000476D87 Unknown Unknown Unknown
a.out 000000000044B7C4 Unknown Unknown Unknown
a.out 000000000044B5D6 Unknown Unknown Unknown
a.out 000000000042DB76 Unknown Unknown Unknown
a.out 00000000004053DE Unknown Unknown Unknown
libpthread.so.0 00007F2327456790 Unknown Unknown Unknown
libc.so.6 00007F2326EFE2F7 Unknown Unknown Unknown
libmpi.so.12 00007F2327B899E8 Unknown Unknown Unknown
libmpi.so.12 00007F2327C94E39 Unknown Unknown Unknown
libmpi.so.12 00007F2327C94B32 Unknown Unknown Unknown
libmpi.so.12 00007F2327B6E44A Unknown Unknown Unknown
libmpi.so.12 00007F2327B6DD5D Unknown Unknown Unknown
libmpi.so.12 00007F2327B6DBDC Unknown Unknown Unknown
libmpi.so.12 00007F2327B6DB0C Unknown Unknown Unknown
libmpi.so.12 00007F2327B6F932 Unknown Unknown Unknown
libmpifort.so.12 00007F2328294B1C Unknown Unknown Unknown
a.out 000000000040488B Unknown Unknown Unknown
a.out 000000000040385E Unknown Unknown Unknown
libc.so.6 00007F2326E4DD5D Unknown Unknown Unknown
a.out 0000000000403769 Unknown Unknown Unknown</code></pre><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div></div><div><span style="font-size:12.8px">I understand that for a given three processors, the first processor is getting 32 bytes (2X4), the same data is sent to other processors, but their receive count is 16 bytes (1X4), which is leading to error. How I can modify the program? I got the error reason, but I am not able to make it right to work. What modifications I have to make? </span><br></div><div><br></div><div>_</div><div><font color="#ffffff" face="arial, sans-serif"><span style="color:rgb(0,0,0);font-size:16px;background-color:rgb(255,255,255)"><b><span style="color:rgb(102,153,255);font-size:10pt">SAVE WATER </span></b><b><span style="font-family:webdings;color:rgb(102,153,255);font-size:13.5pt"></span></b><b><span style="color:rgb(102,153,255);font-size:10pt"> ~ </span></b><b><span style="color:red;font-size:10pt">SAVE ENERGY</span></b><b><span style="font-family:webdings;color:red;font-size:13.5pt">~ </span></b><b><span style="color:rgb(102,153,255);font-size:10pt">~ </span></b><b><span style="color:rgb(0,153,51);font-size:10pt">SAVE EARTH </span></b></span></font><img src="http://bestanimations.com/Earth&Space/Earth/Earth-22-june.gif" alt="Earth-22-june.gif (7996 bytes)"></div><div><br></div><div><a href="http://sites.google.com/site/kolukulasivasrinivas/" target="_blank">http://sites.google.com/site/kolukulasivasrinivas/</a></div><div><br></div><div><div><font color="#FF0000"><font face="garamond, serif"><span style="font-size:large">Siva Srinivas Kolukula</span></font>, PhD<br></font></div><div><font color="#FF0000"><b>Scientist - B</b><br></font><span style="font-family:arial"><div style="font-size:small;display:inline"><div style="color:rgb(0,0,0)"><font face="Tahoma"><span style="color:rgb(0,0,255)">Indian Tsunami Early Warning Centre (ITEWC)</span><br>
<span style="color:rgb(102,0,0)">Advisory Services and Satellite Oceanography Group (ASG)</span></font></div><span style="color:rgb(39,78,19)">Indian National Centre for Ocean Information Services (INCOIS)</span><br><div style="color:rgb(0,0,0)"><font face="Tahoma">
<span style="color:rgb(204,0,0)">"Ocean Valley"</span><br>
<span style="color:rgb(127,96,0)">Pragathi Nagar (B.O)</span></font></div>
<div style="color:rgb(0,0,0)"><font face="Tahoma"><span style="color:rgb(153,0,255)"><font face="tahoma">Nizampet (S.O)</font></span><br>
<span style="color:rgb(204,0,0)">Hyderabad - 500 090<br>Telangana, INDIA</span></font></div><font><div style="color:rgb(0,0,0)"><font color="#FF0000"><span style="font-family:arial"><div style="font-size:small;color:rgb(0,0,0);display:inline"><font color="#FF0000"><br></font></div></span></font></div><font color="#000000">Office: 040 23886124 </font></font><font color="#FF0000" style="color:rgb(0,0,0)"> </font></div><div style="color:rgb(0,0,0);font-size:small;display:inline"><font color="#FF0000"> </font></div></span><font color="#ff0000"> </font></div><div><b><font color="#FF9900">Cell: +91 9381403232; +91 8977801947</font></b><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
</div>