# SharedArray of composite types

**URL:** <https://discourse.julialang.org/t/sharedarray-of-composite-types/9868>\
**Category:** New to Julia\
**Created:** [March 21, 2018, 8:25pm UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868 "2018-03-21T20:25:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![remoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remoore/32/2978_2.png) [@remoore](https://discourse.julialang.org/u/remoore)\
**Post date:** [March 21, 2018, 8:25pm UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868/1 "2018-03-21T20:25:28Z")

</div>

I would like to be able to construct a SharedArray-like object of composite types. It says in the [documentation](https://docs.julialang.org/en/stable/manual/parallel-computing.html#man-shared-arrays-1) that the shared array must be of a bits type. What is the best way of going about this?

Thanks

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 21, 2018, 10:13pm UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868/2 "2018-03-21T22:13:55Z")

</div>

```julia
julia> struct Foo
           a::Int
           b::Float64
       end

julia> isbits(Foo)
true

julia> SharedArray{Foo, 1}((10,))
10-element SharedArray{Foo,1}:
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)
 Foo(0, 0.0)

```

---

<div class="post-metadata">

**Author:** ![remoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remoore/32/2978_2.png) [@remoore](https://discourse.julialang.org/u/remoore)\
**Post date:** [March 22, 2018, 12:44pm UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868/3 "2018-03-22T12:44:53Z")

</div>

Thanks for the response. My type is not a bits type, and this throws an error. Any idea how I could get this to work?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 22, 2018, 1:06pm UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868/4 "2018-03-22T13:06:07Z")

</div>

You just can’t use `SharedArray` then. You could think about switching to using `@threads` (shared-memory threads) instead of `@parallel`.

Edit: that or use `@spawn`, `remotecall`, etc.

---

<div class="post-metadata">

**Author:** ![remoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remoore/32/2978_2.png) [@remoore](https://discourse.julialang.org/u/remoore)\
**Post date:** [March 26, 2018, 10:46am UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868/5 "2018-03-26T10:46:07Z")

</div>

Thanks for the help. I’ll explore the options you’ve suggested.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 2, 2018, 3:11pm UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868/6 "2018-05-02T15:11:53Z")

</div>

Use a bitstype instead. What is preventing it from being a bitstype? Eg. Replace arrays with tuples.

---

<div class="post-metadata">

**Author:** ![remoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remoore/32/2978_2.png) [@remoore](https://discourse.julialang.org/u/remoore)\
**Post date:** [May 8, 2018, 4:40pm UTC](https://discourse.julialang.org/t/sharedarray-of-composite-types/9868/7 "2018-05-08T16:40:37Z")

</div>

Thanks for the suggestion - I might try that in the future. Using @threads, as suggested by @tkoolen, is working well for me.
