MPI + C broadcast operation

Source: Internet
Author: User

Int mpi_bcast (void * buffer, int count, mpi_datatype datatype, int root, mpi_comm comm)

Mpi_bcast must be called during blocking communication;

You also need to call mpi_bcast when receiving the message and check whether the Root parameter is the same as your own ID number. If the Root parameter is different, the message is received;

 

 

1. broadcast an element in the main process array and output it at the corresponding position in the other process array.

 

 

# Include "MPI. H "<br/> # include <stdio. h> <br/> const int n = 10; <br/> int main (INT argc, char * argv []) {<br/> int I, rank, numprocs, master; <br/> int DP [N]; <br/> mpi_init (& argc, & argv); <br/> mpi_comm_rank (mpi_comm_world, & rank ); <br/> mpi_comm_size (mpi_comm_world, & numprocs); <br/> for (I = 0; I <n; I ++) {<br/> DP [I] = rank; <br/>}< br/> // DP [Rank] = rank; </P> <p> If (Rank = Master) mpi_bcast (& DP [Master], 1, mpi_int, Master, mpi_comm_world ); </P> <p> mpi_bcast (& DP [Master], 1, mpi_int, Master, mpi_comm_world); <br/> If (Rank = numprocs-1) {<br/> for (I = 0; I <n; I ++) <br/> printf ("% d", DP [I]); <br/> printf ("/N"); <br/>}</P> <p> mpi_finalize (); <br/> return 0; <br/>} 

Result:

[Root @ c0101 ~] # Mpicxx bcast. cpp-o a <br/> [root @ c0101 ~] # Mpiexec-N 5./A <br/> 0 4 4 4 4 4 4 4 4 4 4 <br/> [root @ c0101 ~] # 

 

 

 

2. Read a value from the main process, broadcast it, and output it.

 

# Include "MPI. H "<br/> # include <stdio. h> <br/> int main (INT argc, char * argv []) {<br/> int I, rank, numprocs, master; <br/> int value =-1; <br/> mpi_init (& argc, & argv); <br/> mpi_comm_rank (mpi_comm_world, & rank ); <br/> mpi_comm_size (mpi_comm_world, & numprocs); </P> <p> If (Rank = Master) {<br/> printf ("Please input a positive value: "); <br/> scanf (" % d ", & value); <br/> mpi_bcast (& Value, 1, mpi_int, Master, mpi_comm_world ); <br/>}</P> <p> mpi_bcast (& Value, 1, mpi_int, Master, mpi_comm_world); </P> <p> mpi_barrier (mpi_comm_world ); <br/> printf ("value of process % d is: % d/N", rank, value); <br/> mpi_finalize (); <br/> return 0; <br/>} 

Result:

[Root @ c0101 ~] # Mpicxx bcast. cpp-o a <br/> [root @ c0101 ~] # Mpiexec-N 5. /A <br/> Please input a positive value: 8 <br/> value of process 2 is: 8 <br/> value of process 0 is: 8 <br/> value of process 3 is: 8 <br/> value of process 4 is: 8 <br/> value of process 1 is: 8 <br/> [root @ c0101 ~] # 

 

 

In addition, the following deadlock occurs. In fact, it can be replaced by non-blocking communication mpi_isend.

Program Main <br/> implicit none <br/> include 'mpif. h' <br/> integer, parameter: n = 10 <br/> integer myid, numprocs, ierr, I <br/> integer dp (0: 10) <br/> call mpi_init (ierr) <br/> call mpi_comm_rank (mpi_comm_world, myid, ierr) <br/> call mpi_comm_size (mpi_comm_world, numprocs, ierr) <br/> do I = 0, n <br/> dp (I) =-1 <br/> enddo <br/> dp (myid) = myid <br/> call mpi_barrier (mpi_comm_world, ierr) <br/> If (myid. ne. numprocs-1) Call mpi_bcast (dp (myid), 1, mpi_integer, <br/> & myid, mpi_comm_world, ierr) <br/> If (myid. eq. (numprocs-1) Then <br/> do I = 0, numprocs-2 <br/> call mpi_bcast (dp (I), 1, mpi_integer, I, <br/> & mpi_comm_world, ierr) <br/> enddo <br/> Print *, dp <br/> endif <br/> call mpi_finalize (ierr) <br/> end 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.