'MPI_Op_create. Problem with user function for scalar product
I have a task to perform scalar product using user functions via MPI_Op_create
and MPI_Reduce
. I have performed the function, but it works in a wrong way a bit.
The problem itself, as it seems to me, is hidden in replacing invec
and inoutvec
after one process performs its operations. After the first operation is done correctly, inoutvec
stays the same and invec
is being replaced by a vector which is filled by 0. Can somebody where the error is hidden?
void func_for_scalar_mult(double *invec, double *inoutvec, int* len, MPI_Datatype
*dtptr)
{
for(int i = 0; i < (*len - 1) / 2; ++i) {
inoutvec[*len - 1] += invec[i] * invec[(*len - 1) / 2 + i];// + inoutvec[i]
*inoutvec[(*len - 1) / 2 + i];
}
}
P.s. The program works correctly using MPI_SUM instead of user function so that I can conclude that the error is hidden in the shared block of code.
I must sorry for misusage of terms due to the lack of experience performing MPI programs. Saying “first operation”, I meant exactly the situation when processes 1 and 2 combine. Vectors which I give to ‘MPI_Reduce’ are filled by dim / (commSize -1) numbers from first vector and dim / (commSize - 1) numbers from the second vector (dim is the dimension of the space) and the last element of ‘invec’ and ‘inoutvec’, as I planned, should be the partial scalar production of first half of ‘invec’ and the second half of ‘invec’. So that the dimension of ‘invec’ is 2 * dim / (commSize - 1).
Solution 1:[1]
You say you want to compute a "scalar product". You mean that you multiply all the scalars in a vector together? That's not what the MPI_Op
is for: a reduction on an array is a pointwise reduction of components: reduce item 0 from all processes into location 0, reduce item 1 from all processes into location 1, et cetera. I'm not sure if you can somehow hack the reduction to do what you want.
Also, you talk about "the first operation". There is no such thing: the reduction is probably done in a treelike manner: processes 0 and 1 combine, and 2 and 3 combine, and then those partial results combine. So there are probably p/2 "first" operations.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Victor Eijkhout |