# How can I pass the pointer of MPI communicator to c function?

**URL:** https://discourse.julialang.org/t/how-can-i-pass-the-pointer-of-mpi-communicator-to-c-function/87070
**Category:** Julia at Scale
**Tags:** mpi
**Created:** [September 11, 2022, 9:44am UTC](https://discourse.julialang.org/t/how-can-i-pass-the-pointer-of-mpi-communicator-to-c-function/87070 "2022-09-11T09:44:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dora\_ho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dora_ho/32/42582_2.png) [@dora\_ho](https://discourse.julialang.org/u/dora_ho)
#### Post date: [September 11, 2022, 9:44am UTC](https://discourse.julialang.org/t/how-can-i-pass-the-pointer-of-mpi-communicator-to-c-function/87070/1 "2022-09-11T09:44:33Z")

</div>

I have a c function that requires me to pass a comm pointer to the function

I converted the function using Clang.jl showed below

```julia
function Comm(comm_handle)
    ccall((:Comm, librarypath), Cint, (Ptr{Cvoid}), comm_handle)
end

```

But I am getting an error if I pass `MPI.MPI_COMM_WORLD` to the function  
`MethodError: no method matching unsafe_convert(::Type{Ptr{Cvoid}}, ::MPI.Comm)`

Did I pass the wrong communicator or a wrong type of pointer is declared?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 11, 2022, 12:40pm UTC](https://discourse.julialang.org/t/how-can-i-pass-the-pointer-of-mpi-communicator-to-c-function/87070/2 "2022-09-11T12:40:05Z")

</div>

Look at some [examples in the MPI.jl code](https://github.com/JuliaParallel/MPI.jl/blob/master/src/comm.jl#L37-L43): you declare the argument as a `Ptr{MPI.MPI_Comm}`.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 11, 2022, 12:44pm UTC](https://discourse.julialang.org/t/how-can-i-pass-the-pointer-of-mpi-communicator-to-c-function/87070/3 "2022-09-11T12:44:15Z")

</div>

I submitted a pull request to add this to the MPI.jl docs: [document how to pass MPI.Comm objects to C by stevengj · Pull Request #626 · JuliaParallel/MPI.jl · GitHub](https://github.com/JuliaParallel/MPI.jl/pull/626)

---

<div class="post-metadata">

### Author: ![dora\_ho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dora_ho/32/42582_2.png) [@dora\_ho](https://discourse.julialang.org/u/dora_ho)
#### Post date: [September 11, 2022, 8:55pm UTC](https://discourse.julialang.org/t/how-can-i-pass-the-pointer-of-mpi-communicator-to-c-function/87070/4 "2022-09-11T20:55:51Z")

</div>

Thanks for the information!  
I can pass the pointer right now.
