Removing MPI_Bcast ()

So, I have a code in which I use MPI_Bcast to send information from the root node to all nodes, but instead want my P0 to send pieces of the array to separate processes.

How to do this using MPI_Send and MPI_Receive?

I have never used them before, and I don’t know if I need to loop my MPI_Receive in order to send all or what efficiently.

I put comments about blocking giant caps into code, where I need to replace my MPI_Bcast (), sorry in advance for the code falls.

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;
}
+4
source share
2 answers

You need to MPI_Scatter. Good introduction here: http://mpitutorial.com/tutorials/mpi-scatter-gather-and-allgather/

, :

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);
+5

MPI_Send MPI_Recv, - :

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);
}
+3

Source: https://habr.com/ru/post/1612669/


All Articles