Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does mpi_byte work?

Tags:

c

fortran

mpi

I saw a post on Stackoverflow about a simple program with mpi:

Simple MPI Program

in that program, the user was trying to broadcast a char variable length equal to 20 by using the MPI_BYTE mpi-type instead of MPI_CHAR.

I was trying (probably not very clever) to broad cast an integer variable "n" using the MPI_BYTE type:

call MPI_BCAST(n,1,MPI_BYTE,0,MPI_COMM_WORLD,ierr)

but I got simply a big number when I print the value of "n" for the processors different than the root.

My question is when could I use MPI_BYTE?

I have read that it is machine-architecture dependent but I still don't get the point.

regards.

like image 320
armando Avatar asked Dec 17 '22 02:12

armando


1 Answers

Your issue is rather simple to solve: Your use of MPI_BYTE is (almost) perfectly fine, however your size/number of elements (in this case: number of bytes) to send is wrong.

Depending on the length of an integer (e.g. 4 bytes), you'd have to set the second parameter to 4:

call MPI_BCAST(n,4,MPI_BYTE,0,MPI_COMM_WORLD,ierr)

Whether you use MPI_BYTE or MPI_CHAR here won't matter as both are the same length (this is like unsigned char vs. char). But you have to keep in mind that this won't work between different architectures.

To solve this, use the correct type (MPI_INTEGER) to ensure (behind the scenes) conversion, e.g. from little endian to big endian:

call MPI_BCAST(n,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
like image 99
Mario Avatar answered Jan 01 '23 05:01

Mario