Reputation: 63
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.
Upvotes: 3
Views: 753
Reputation: 9519
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 )
Upvotes: 3