# How can I allocate a buffer contains different types of data (for MPI communication, for example)?

**URL:** https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478
**Category:** Performance
**Tags:** question, mpi
**Created:** [October 19, 2024, 10:25am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478 "2024-10-19T10:25:17Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Blumenkranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blumenkranz/32/210335_2.png) [@Blumenkranz](https://discourse.julialang.org/u/Blumenkranz)
#### Post date: [October 19, 2024, 10:25am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/1 "2024-10-19T10:25:18Z")

</div>

I need to communicate data of `Vector{Int8}` and `Vector{Float64}` using `MPI.jl`. How can I achieve this with a single communication?

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [October 19, 2024, 7:10pm UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/2 "2024-10-19T19:10:56Z")

</div>

The easiest way is probably to put these into a struct and then communicate the struct. I am not sure whether

> [@Blumenkranz](#):
>
> achieve this with a single communication

matters for performance. So before you make large changes to your code that might reduce readability, I recommend to benchmark whether these changes help at all.

---

<div class="post-metadata">

### Author: ![Blumenkranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blumenkranz/32/210335_2.png) [@Blumenkranz](https://discourse.julialang.org/u/Blumenkranz)
#### Post date: [October 20, 2024, 5:40am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/3 "2024-10-20T05:40:32Z")

</div>

Thanks for your advice. The problem of communicating a struct is that the struct should satisfy `isbitstype(T)` is `true`. Thus no `AbstractArray` type can be used, which will cause severe performance problem if the data is changed frequently. It is indeed necessary to test whether multiple communications will affect performance, and I will conduct this test afterwards.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [October 20, 2024, 6:09am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/4 "2024-10-20T06:09:21Z")

</div>

Ah now I understand better what you are getting at. You say you need a `isbitstype` for communication? Then ordinary `AbstractArray`s already don’t work, so packing them into a struct does of course not work as well.

But then I don’t think that your approach does make sense. Say you have two arrays (of different type if you wish). Then you’d need to allocate some appropriate Buffer (could try e.g. a `Memory` in 1.11), copy all of your array’s contents into there and then communicate that. For anything but the smallest arrays, I would assume that this overhead (allocation and copying) would dwarf the cost of a second communication. But then again I have never used MPI myself, so can’t speak from practical experience.

If you know the amount of memory needed beforehand, then you could try to preallocate a sufficient amount of memory and wrap sections of that into `Array{T}` to work with it. Then you communicate the `MemoryRef` (which is isbits) of the whole memory.

---

<div class="post-metadata">

### Author: ![Blumenkranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blumenkranz/32/210335_2.png) [@Blumenkranz](https://discourse.julialang.org/u/Blumenkranz)
#### Post date: [October 20, 2024, 6:39am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/5 "2024-10-20T06:39:04Z")

</div>

> If you know the amount of memory needed beforehand, then you could try to preallocate a sufficient amount of memory and wrap sections of that into `Array{T}` to work with it. Then you communicate the `MemoryRef` (which is isbits) of the whole memory.

That’s exactly what I want to know. How to preallocate the memory according to the size of data? And how to wrap one segment into `Array{T}`? This can be done easily in C, but I have no idea in Julia…

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [October 20, 2024, 6:59am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/6 "2024-10-20T06:59:19Z")

</div>

I don’t quite understand what you want to do. Do you want to copy the arrays into a buffer before sending with mpi? Do you create a `MPI_Type_struct`? What does the C-program doing this look like?

Or do you want something like this:

```julia
buf = Memory{UInt8}(undef, 32)
bufptr = pointer(buf)

ivec = unsafe_wrap(Array, Ptr{Int}(bufptr), (2,), own=false)
fvec = unsafe_wrap(Array, Ptr{Float64}(bufptr+16), (2,), own=false)

```

where `ivec` uses the first part of `buf`, and `fvec` uses the second part?

---

<div class="post-metadata">

### Author: ![Blumenkranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blumenkranz/32/210335_2.png) [@Blumenkranz](https://discourse.julialang.org/u/Blumenkranz)
#### Post date: [October 20, 2024, 7:19am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/7 "2024-10-20T07:19:54Z")

</div>

> [@sgaure](#):
>
> Or do you want something like this:
> 
> ```julia
> buf = Memory{UInt8}(undef, 32)
> bufptr = pointer(buf)
> 
> ivec = unsafe_wrap(Array, Ptr{Int}(bufptr), (2,), own=false)
> fvec = unsafe_wrap(Array, Ptr{Float64}(bufptr+16), (2,), own=false)
> 
> ```

Ah, yes, that’s what I want. So we need to use `UInt8` to allocate the memory in Julia. It’s a bit strange that Julia doesn’t provide a more direct interface for allocating memory of known size. Thanks for your reply!

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [October 20, 2024, 7:23am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/8 "2024-10-20T07:23:06Z")

</div>

> [@Blumenkranz](#):
>
> It’s a bit strange that Julia doesn’t provide a more direct interface for allocating memory of known size.

Oh, you can always do

```julia
bufptr = @ccall malloc(32::Int)::Ptr{Nothing}

```

---

<div class="post-metadata">

### Author: ![Blumenkranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blumenkranz/32/210335_2.png) [@Blumenkranz](https://discourse.julialang.org/u/Blumenkranz)
#### Post date: [October 20, 2024, 7:27am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/9 "2024-10-20T07:27:24Z")

</div>

This is even less Julia-like 😄

---

<div class="post-metadata">

### Author: ![AMJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amj/32/214096_2.png) [@AMJ](https://discourse.julialang.org/u/AMJ)
#### Post date: [October 20, 2024, 8:07am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/10 "2024-10-20T08:07:05Z")

</div>

There is also `Libc.malloc(32)`

---

<div class="post-metadata">

### Author: ![Blumenkranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blumenkranz/32/210335_2.png) [@Blumenkranz](https://discourse.julialang.org/u/Blumenkranz)
#### Post date: [October 20, 2024, 8:12am UTC](https://discourse.julialang.org/t/how-can-i-allocate-a-buffer-contains-different-types-of-data-for-mpi-communication-for-example/121478/11 "2024-10-20T08:12:23Z")

</div>

Wow, looks much better 😃
