How it works...

The root process of rank equal to 0 instantiates a variable, variable_to_share, which is equal to 100. This variable will be shared with the other processes of the communication group:

if rank == 0: 
   variable_to_share = 100  

To perform this, we also introduce the broadcast communication statement:

variable_to_share = comm.bcast(variable_to_share, root=0) 

Here, the parameters in the function are as follows:

  • The data to be shared (variable_to_share).
  • The root process, that is, the process of rank equal to 0 (root=0).

Running the code, we have a communication group of 10 processes, and variable_to_share is shared between the other processes in the group. Finally, the print statement visualizes the rank of the running process and the value of its variable:

print("process = %d" %rank + " variable shared  = %d "    
                     %variable_to_share) 

After setting 10 processes, the output obtained is as follows:

C:>mpiexec -n 10 python broadcast.py 
process = 0
variable shared = 100
process = 8
variable shared = 100
process = 2 variable
shared = 100
process = 3
variable shared = 100
process = 4
variable shared = 100
process = 5
variable shared = 100
process = 9
variable shared = 100
process = 6
variable shared = 100
process = 1
variable shared = 100
process = 7
variable shared = 100
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.206