Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing MPI_Bcast()

Tags:

mpi

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;
}
like image 331
Ryan Tibbetts Avatar asked Feb 08 '23 18:02

Ryan Tibbetts


1 Answers

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);
like image 148
Adam Avatar answered Apr 28 '23 13:04

Adam