# Inplace MPI\_Allreduce give wrong results. Solved: better doc is needed

**URL:** <https://discourse.julialang.org/t/inplace-mpi-allreduce-give-wrong-results-solved-better-doc-is-needed/124395>\
**Category:** General Usage\
**Created:** [January 3, 2025, 2:16am UTC](https://discourse.julialang.org/t/inplace-mpi-allreduce-give-wrong-results-solved-better-doc-is-needed/124395 "2025-01-03T02:16:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsjie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsjie/32/22477_2.png) [@jsjie](https://discourse.julialang.org/u/jsjie)\
**Post date:** [January 3, 2025, 2:16am UTC](https://discourse.julialang.org/t/inplace-mpi-allreduce-give-wrong-results-solved-better-doc-is-needed/124395/1 "2025-01-03T02:16:32Z")

</div>

A MWE:

```julia
using MPI

a = [1.0, 2.0, 3.0, 4.0]
MPI.Init()
MPI.API.MPI_Allreduce(MPI.API.MPI_IN_PLACE, a, 4, MPI.Datatype(Float64), MPI.Op(+, Float64), MPI.COMM_WORLD)
MPI.Finalize()
println(a)

```

which outputs

```julia
[NaN, 1.386670151907536e-309, 1.38666778029815e-309, 1.38667015182163e-309]

```

A non-inplace one runs correctly:

```julia
using MPI

a = [1.0, 2.0, 3.0, 4.0]
b = zeros(4)
MPI.Init()
MPI.API.MPI_Allreduce(a, b, 4, MPI.Datatype(Float64), MPI.Op(+, Float64), MPI.COMM_WORLD)
MPI.Finalize()
println(a)
println(b)

```

which outputs

```julia
[1.0, 2.0, 3.0, 4.0]
[2.0, 4.0, 6.0, 8.0]

```

The first two arguments are written according to [openmpi manual](https://docs.open-mpi.org/en/v5.0.1/man-openmpi/man3/MPI_Allreduce.3.html#use-of-in-place-option), MPI\_IN\_PLACE for sendbuf, and the real send-and-receive buffer for recvbuf.

* * *

I’ve found the solution. Change `MPI.API.MPI_IN_PLACE` to `MPI.IN_PLACE` solve the problem. However, since I’m directly calling a lower function through `MPI.API`, it surprises me that I need to use a constant from `MPI` instead of `MPI.API`.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 3, 2025, 6:38am UTC](https://discourse.julialang.org/t/inplace-mpi-allreduce-give-wrong-results-solved-better-doc-is-needed/124395/2 "2025-01-03T06:38:35Z")

</div>

Not at a computer, so can’t test. But isn’t `MPI.API.MPI_IN_PLACE` a ref? Try using `MPI.API.MPI_IN_PLACE[]`.

---

<div class="post-metadata">

**Author:** ![jsjie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsjie/32/22477_2.png) [@jsjie](https://discourse.julialang.org/u/jsjie)\
**Post date:** [January 3, 2025, 6:59am UTC](https://discourse.julialang.org/t/inplace-mpi-allreduce-give-wrong-results-solved-better-doc-is-needed/124395/3 "2025-01-03T06:59:58Z")

</div>

That also works. I examine the source codes and found the following:

```julia
const IN_PLACE = InPlace()

```

and

```julia
struct InPlace
end
Base.cconvert(::Type{MPIPtr}, ::InPlace) = API.MPI_IN_PLACE[]

```

So they are effectively the same.
