Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About MPI_Reduce

Tags:

c

mpi

Just a question. If I use the function MPI_Reduce, only the root can allocate the receive buffer, when this is a dynamic array?. For example:

int r = 10;
int *yloc, *y;
...
yloc = calloc(r*sizeof(int)); // for all processes

if (I'm the master process) {
    y = calloc(r*sizeof(int))  // inside the if-block ...
    ...
}
y = calloc(r*sizeof(int))  // ...or outside the if-block?
...
MPI_Reduce(yloc, y, r, MPI_FLOAT, MPI_SUM, ROOT, MPI_COMM_WORLD);

What is correct? Inside or outside the if-block? Thanks in advance.

like image 306
Pax Avatar asked Jan 21 '26 05:01

Pax


1 Answers

Both are correct. But I guess that the answer you want is that whether or not y is a valid memory address only matters for the root process of the MPI_Reduce() call. So there's no need to allocate the memory for any other processes than the root one.

Just to be complete, here is an excerpt of MPI_Reduce's man page, where we can read that the receive buffer is only significant for the root process:

NAME
       MPI_Reduce -  Reduces values on all processes to a single value

SYNOPSIS
       int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
                      MPI_Op op, int root, MPI_Comm comm)

INPUT PARAMETERS
       sendbuf
              - address of send buffer (choice)
       count  - number of elements in send buffer (integer)
       datatype
              - data type of elements of send buffer (handle)
       op     - reduce operation (handle)
       root   - rank of root process (integer)
       comm   - communicator (handle)

OUTPUT PARAMETERS
       recvbuf
              - address of receive buffer (choice, significant only at root )
like image 178
Gilles Avatar answered Jan 22 '26 19:01

Gilles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!