MPI Broadcast and Collective Communication

So far in the beginner MPI tutorial, we have examined point-to-point communication, which is communication between two processes. This lesson is the start of the collective communication section. Collective communication is a method of communication which involves participation of all processes in a communicator. In this lesson, we will discuss the implications of collective communication and go over a standard collective routine – broadcasting. The code for the lesson can be downloaded here.

Collective Communication and Synchronization Points

One of the things to remember about collective communication is that it implies a synchronization point among processes. This means that all processes must reach a point in their code before they can all begin executing again.

Before going into detail about collective communication routines, let’s examine synchronization in more detail. As it turns out, MPI has a special function that is dedicated to synchronizing processes:

  • MPI_Barrier(MPI_Comm communicator)

The name of the function is quite descriptive – the function forms a barrier, and no processes in the communicator can pass the barrier until all of them call the function. Here’s an illustration. Imagine the horizontal axis represents execution of the program and the circles represent different processes:

MPI_Barrier example

Process zero first calls MPI_Barrier at the first time snapshot (T 1). While process zero is hung up at the barrier, process one and three eventually make it (T 2). When process two finally makes it to the barrier (T 3), all of the processes then begin execution again (T 4).

MPI_Barrier can be useful for many things. One of the primary uses of MPI_Barrier is to synchronize a program so that portions of the parallel code can be timed accurately.

Want to know how MPI_Barrier is implemented? Sure you do :-) Do you remember the ring program from the MPI_Send and MPI_Recv tutorial? To refresh your memory, we wrote a program that passed a token around all processes in a ring-like fashion. This type of program is one of the simplest methods to implement a barrier since a token can’t be passed around completely until all processes work together.

One final note about synchronization – Always remember that every collective call you make is synchronized. In other words, if you can’t successfully complete an MPI_Barrier, then you also can’t successfully complete any collective call. If you try to call MPI_Barrier or other collective routines without ensuring all processes in the communicator will also call it, your program will idle. This can be very confusing for beginners, so be careful!

Broadcasting with MPI_Bcast

A broadcast is one of the standard collective communication techniques. During a broadcast, one process sends the same data to all processes in a communicator. One of the main uses of broadcasting is to send out user input to a parallel program, or send out configuration parameters to all processes.

The communication pattern of a broadcast looks like this:

MPI_Barrier example

In this example, process zero is the root process, and it has the initial copy of data. All of the other processes receive the copy of data.

In MPI, broadcasting can be accomplished by using MPI_Bcast. The function prototype looks like this:

  • MPI_Bcast(void* data, int count, MPI_Datatype datatype, int root, MPI_Comm communicator)

Although the root process and receiver processes do different jobs, they all call the same MPI_Bcast function. When the root process (in our example, it was process zero) calls MPI_Bcast, the data variable will be sent to all other processes. When all of the receiver processes call MPI_Bcast, the data variable will be filled in with the data from the root process.

Broadcasting with MPI_Send and MPI_Recv

At first, it might seem that MPI_Bcast is just a simple wrapper around MPI_Send and MPI_Recv. In fact, we can make this wrapper function right now. Our function, called my_bcast can be downloaded in the example code for this lesson (my_bcast.c). It takes the same arguments as MPI_Bcast and looks like this:

void my_bcast(void* data, int count, MPI_Datatype datatype, int root,
              MPI_Comm communicator) {
  int world_rank;
  MPI_Comm_rank(communicator, &world_rank);
  int world_size;
  MPI_Comm_size(communicator, &world_size);
 
  if (world_rank == root) {
    // If we are the root process, send our data to everyone
    int i;
    for (i = 0; i < world_size; i++) {
      if (i != world_rank) {
        MPI_Send(data, count, datatype, i, 0, communicator);
      }
    }
  } else {
    // If we are a receiver process, receive the data from the root
    MPI_Recv(data, count, datatype, root, 0, communicator,
             MPI_STATUS_IGNORE);
  }
}

The root process sends the data to everyone else while the others receive from the root process. Easy, right? If you download the code and run the program, the program will print output like this:

MPI_Bcast output

Believe it or not, our function is actually very inefficient! Imagine that each process has only one outgoing/incoming network link. Our function is only using one network link from process zero to send all the data. A smarter implementation is a tree-based communication algorithm that can use more of the available network links at once. For example:

MPI_Bcast output

In this illustration, process zero starts off with the data and sends it to process one. Similar to our previous example, process zero also sends the data to process two in the second stage. The difference with this example is that process one is now helping out the root process by forwarding the data to process three. During the second stage, two network connections are being utilized at a time. The network utilization doubles at every subsequent stage of the tree communication until all processes have received the data.

Do you think you can code this? Writing this code is a bit outside of the purpose of the lesson. If you are feeling brave, Parallel Programming with MPI is an excellent book with a complete example of the problem with code.

Comparison of MPI_Bcast with MPI_Send and MPI_Recv

The MPI_Bcast implementation utilizes a similar tree broadcast algorithm for good network utilization. How does our broadcast function compare to MPI_Bcast? We can run compare_bcast, an example program included in the lesson code. Before looking at the code, let’s first go over one of MPI’s timing functions – MPI_Wtime(). MPI_Wtime takes no arguments, and it simply returns a floating-point number of seconds since a set time in the past. Similar to C’s time function, you can call multiple MPI_Wtime functions throughout your program and subtract their differences to obtain timing of code segments.

Let’s take a look of our code that compares my_bcast to MPI_Bcast

  for (i = 0; i < num_trials; i++) {
    // Time my_bcast
    // Synchronize before starting timing
    MPI_Barrier(MPI_COMM_WORLD);
    total_my_bcast_time -= MPI_Wtime();
    my_bcast(data, num_elements, MPI_INT, 0, MPI_COMM_WORLD);
    // Synchronize again before obtaining final time
    MPI_Barrier(MPI_COMM_WORLD);
    total_my_bcast_time += MPI_Wtime();
 
    // Time MPI_Bcast
    MPI_Barrier(MPI_COMM_WORLD);
    total_mpi_bcast_time -= MPI_Wtime();
    MPI_Bcast(data, num_elements, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);
    total_mpi_bcast_time += MPI_Wtime();
  }

In this code, num_trials is a variable stating how many timing experiments should be executed. We keep track of the accumulated time of both functions in two different variables. The average times are printed at the end of the program. To see the entire code, just download the lesson code and look at compare_bcast.c.

When you use the run script to execute the code, the output will look similar to this.

Compare MPI_Bcast output

The run script executes the code using 16 processors, 100,000 integers per broadcast, and 10 trial runs for timing results. As you can see, my experiment using 16 processors connected via ethernet shows significant timing differences between our naive implementation and MPI’s implementation. Here is what the timing results look like at all scales.

Processorsmy_bcastMPI_Bcast
20.03440.0344
40.10250.0817
80.23850.1084
160.51090.1296

As you can see, there is no difference between the two implementations at two processors. This is because MPI_Bcast’s tree implementation does not provide any additional network utilization when using two processors. However, the differences can clearly be observed when going up to even as little as 16 processors.

Try running the code yourself and experiment at larger scales!

Conclusions / Up Next

Feel a little better about collective routines? In the next MPI tutorial, I go over other essential collective communication routines – gathering and scattering.

For all beginner lessons, go the the beginner MPI tutorial.

HAS MPITUTORIAL.COM HELPED YOU?

Donate a small sum of 5 dollars today to help me out with my hosting costs. Thank you!


Related posts:

  1. Point-to-Point Communication Application – Random Walk
  2. MPI Introduction
  3. Installing MPICH2
  4. MPI Hello World
  5. MPI Send and Receive
Wes tagged this post with: , , Read 8 articles by

4 Comments

  1. Graham says:

    Hi Wes,

    Thanks for the tutorials. Will you be posting more?

    • Wes says:

      You are very welcome. I have finally had some time to start posting more tutorials. Thanks for the patience and I appreciate the interest!

  2. Eduardo says:

    Do you plan to continue?

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    RECOMMENDED BOOKS

    My recommendations for great books on MPI.
    Click here for more info

    BEGINNER TUTORIAL

    A step-by-step tutorial on beginner MPI topics.
    Click here for more info

    buy cheap viagra
    buy viagra cheapest
    buy discount soma
    buy real viagra pharmacy online
    buy viagra online no prescription
    buy cialis now
    buy viagra onli
    cheap soft tab viagra
    cialis super active review
    india viagra cialis vicodin
    buy cheap levitra
    cialis no perscription
    female use of viagra
    viagra and cialis
    buy generic viagra pharmacy online
    cheap discount viagra
    viagra dosage
    viagra oral jelly
    buy duscount viagra online
    cheap no prescription viagra
    cheap viagra viagra
    buy viagra online in australia
    buy viagra cialis
    cipro 20
    lowest price viagra
    buy cialis no prescription
    cheap inexpensive viagra
    buy cialis from india
    buy viagra online without prescription
    buy viagra online at cheap price
    viagra without prescription
    buy online online pill viagra viagra
    levitra 100mg uk
    buy discount viagra
    buy internet viagra
    buying viagra online
    buy real viagra online
    viagra super active
    buy viagra and cilas
    levitra online
    buy viagra cheaply
    buy viagra pharmacy online
    buy now levitra
    cheap meltabs viagra
    ordering cialias
    free viagra in the uk
    buy viagra online paypal vipps
    buy cialis online
    buy low price viagra
    buy now viagra
    viagra rrp australia
    cialis no prescription
    cialis replacement
    buy viagra online alternative viagra
    cheap genric viagra online
    buy levitra now
    viagra oral
    buy viagra on the internet
    generic cialis
    viagra for sale
    buy viagra online in uk
    buy online online viagra viagra
    buy female viagra
    buy viagra using paypal
    key buy-viagra
    buy viagra online at
    buy generic viagra
    cheap generic viagra
    amazon viagra
    cost of viagra
    which is better cialis or viagra
    is viagra safe for women
    buy viagra over the counter us
    buy discounted viagra
    real viagra
    cheap viagra generic
    cheap generic viagra no prescription
    cheap pharmaceutical viagra
    natural viagra
    alternative to viagra
    cheap levitra online
    buying viagra in uk
    cheap discount cialis
    buy sale viagra
    viagra over the counter walgreens
    cheap free viagra viagra
    cheap viagra in the uk
    buy cheap tamiflu
    free viagra without prescription
    buy viagra online
    buy cialis canada
    viagra online uk
    female viagra
    cheap generic viagra online
    cheap prescription viagra without
    buy 100 mg viagra
    generic levitra
    cheap discount viagra viagra
    buy viagra online au
    cheap free price viagra
    buy get online prescription viagra
    buy viagra order viagra
    cheap generic 50 mg viagra
    buy no online prescription viagra
    buy generic viagra online pharmacy online
    cheap pill pill sale viagra
    buy nexium
    viagra and cialis cheap
    buy cheap viagra online
    cheap free viagra
    buy viagra online get prescription
    buy viagra online
    homemade viagra
    buy viagra online cheapest
    buy viagra pill
    buy viagra online u
    buy viagra overnight
    cialis overnight shipping
    buy line viagra where
    ordering viagra onl
    buy cheap viagra online uk
    buy lexapro
    buy online order viagra
    cheap viagra canada
    buy cymbalta
    walmart cialis price
    viagra generic
    cheap viagra fast shipping
    cheap mexico viagra
    buy prescription viagra
    buy viagra cialis levitra
    free viagra
    buy viagra online at lowest price
    free viagra sample
    cheap generic substitute viagra
    cheap viagra
    buy viagra online online pharmacy
    guaranteed cheapest viagra
    cheap herbal viagra
    cheap phizer viagra
    buying viagra
    viagra professional
    generic viagra from india
    buy viagra alternative
    cheap discount levitra online
    buy viagra online discount
    buy kamagra viagra india
    buy prescription viagra without
    viagra price in india
    marijuana and viagra
    buy in spain viagra
    shipping viagra to australia
    Viagra Oklahoma city
    where to buy viagra in beijing
    buy generic viagra img
    buy sildenafil viagra
    generic vs brand name viagra
    brand cialis 20
    cheap kamagra viagra
    cheap generic viagra no script
    buy viagra usa
    buy site viagra
    cheap cialis online
    cheapest uk supplier viagra
    cheap generic viagra uk
    cheap pharmacy viagra
    buy in online uk viagra
    online overnight viagra
    cheap deal viagra
    buy viagra pill online
    cheap prescription viagra
    uninsured cost of cialis
    cheap sale viagra
    buy viagra powered by phpbb
    cheap pill viagra
    viagra and cialis and
    cheap kamagra uk viagra
    buy viagra online pharmacy
    non prescription viagra
    buy viagra 50mg
    purchase viagra online
    cialis vs viagra
    buy viagra online canadapurchase cialis
    buy viagra online off pharmacy prices
    viagra uk
    buy locally viagra
    viagra pro online
    buy viagra prescription america
    buy viagra cheapest best prices online
    buy in online usa viagra
    buy generic viagra viagra
    buy internet viagra
    viagra sample
    cheap price viagra
    cheap india viagra
    cheap generic overnight viagra
    cheap viagra india
    buy real viagra
    cheap generic viagra from usa
    buy now online viagra
    buy viagra online australia
    cheap generic online viagra
    buy discount cialis
    where can I buy cialis 20mg
    buy online levitra cialis viagra
    buy viagra onlines
    buy generic viagra buy
    discount viagra
    buy later now pay viagra
    cheap viagra without a prescription
    accutane for sale online
    buy price viagra
    buy viagra online order generic viagra
    buy viagra online and get prescription
    buy viagra montreal
    over the counter viagra
    buy viagra now
    cheap viagra tablets
    ed. trusted medstore in cialis
    watermelon viagra
    buy viagra online in the uk
    buy viagra online web meds
    buy viagra
    buy viagra internet
    cheap generic viagra overnight delivery
    buy viagra online in
    Viagra for sale
    does watermelon have viagra effect
    ebay + cialis
    buy viagra prescription
    viagra coupons and discounts
    buy levitra viagra online
    purchase levitra online
    generic viagra
    cialis coupons
    buy viagra online india
    cialis voucher
    ed pro trial pack
    brand cialis
    buy prescription vaniqa viagra
    generic-viagra
    viagra uk cheap purchase buy
    viagra alternatives
    cheap site viagra
    buy viagra online uk
    buy viagra online ireland
    q buy viagra online
    canadian levitra vs usa levitra
    price of viagra
    buy viagra online a href
    cheap viagra online canada
    cheap herbal viagra viagra
    buy viagra per pill
    buy kamagra viagra
    cheap generic india viagra
    viagra free trial offer
    buy online drug viagra pharmacy
    buy generic viagra usa
    cheap molde ticket viagra
    buy real viagra online pharmacy
    buy cheap cialis
    cheap generic viagra substitutes
    cheap man viagra
    buy in uk viagra
    viagra and hearing loss
    buy viagra online
    how can i buy viagra online in canada
    buy generic viagra
    buy viagra online order
    buy levitra canada
    buy generic viagra online
    cheap viagra in uk
    buy line viagra
    uk viagra sales
    viagra pfizer
    cheap generic viagra substitute
    viagrafree samples
    buy viagra other drug onlinea