Ryan Tibbetts
Ryan Tibbetts

Reputation: 412

Removing MPI_Bcast()

So I have a some code where I am using MPI_Bcast to send information from the root node to all nodes, but instead I want to get my P0 to send chunks of the array to individual processes.

How do I do this with MPI_Send and MPI_Receive?

I've never used them before and I don't know if I need to loop my MPI_Receive to effectively send everything or what.

I've put giant caps lock comments in the code where I need to replace my MPI_Bcast(), sorry in advance for the waterfall of code.

Code:

#include "mpi.h"
#include <stdio.h>
#include <math.h>

#define MAXSIZE 10000000

int add(int *A, int low, int high)
{
  int res = 0, i;

  for(i=low; i<=high; i++)
    res += A[i];

  return(res);
}

int main(argc,argv)
int argc;
char *argv[];
{
    int myid, numprocs, x;
    int data[MAXSIZE];
    int i, low, high, myres, res;
    double elapsed_time;

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);

        if (myid == 0)
          {
                for(i=0; i<MAXSIZE; i++)
                  data[i]=1;
          }

/* star the timer */
        elapsed_time = -MPI_Wtime();

//THIS IS WHERE I GET CONFUSED ABOUT MPI_SEND AND MPI_RECIEVE!!!
        MPI_Bcast(data, MAXSIZE, MPI_INT, 0, MPI_COMM_WORLD);

            x = MAXSIZE/numprocs;
            low = myid * x;
            high = low + x - 1;
        if (myid == numprocs - 1) 
        high = MAXSIZE-1;

            myres = add(data, low, high);
            printf("I got %d from %d\n", myres, myid);

        MPI_Reduce(&myres, &res, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
/* stop the timer*/
        elapsed_time += MPI_Wtime();

        if (myid == 0)
            printf("The sum is %d, time taken = %f.\n", res,elapsed_time);

    MPI_Barrier(MPI_COMM_WORLD);

            printf("The sum is %d at process %d.\n", res,myid);

    MPI_Finalize();
    return 0;
}

Upvotes: 4

Views: 283

Answers (2)

Danil Prokhorenko
Danil Prokhorenko

Reputation: 1104

If you really want use MPI_Send and MPI_Recv, then you can use something like this:

int x = MAXSIZE / numprocs;
int *procData = new int[x];

if (rank == 0) {
    for (int i = 1; i < num; i++) {
        MPI_Send(data + i*x, x, MPI_INT, i, 0, MPI_COMM_WORLD);
    }
} else {
    MPI_Recv(procData, x, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
}

Upvotes: 3

Adam
Adam

Reputation: 17339

You need MPI_Scatter. A good intro is here: http://mpitutorial.com/tutorials/mpi-scatter-gather-and-allgather/

I think in your code it could look like this:

elements_per_proc = MAXSIZE/numprocs;

// Create a buffer that will hold a chunk of the global array
int *data_chunk = malloc(sizeof(int) * elements_per_proc);

MPI_Scatter(data, elements_per_proc, MPI_INT, data_chunk,
            elements_per_proc, MPI_INT, 0, MPI_COMM_WORLD);

Upvotes: 5

Related Questions