# Saving SharedArrays

**URL:** <https://discourse.julialang.org/t/saving-sharedarrays/73723>\
**Category:** General Usage\
**Tags:** distributed\
**Created:** [December 28, 2021, 8:07pm UTC](https://discourse.julialang.org/t/saving-sharedarrays/73723 "2021-12-28T20:07:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [December 28, 2021, 8:07pm UTC](https://discourse.julialang.org/t/saving-sharedarrays/73723/1 "2021-12-28T20:07:52Z")

</div>

If I generate data as:

```julia
using Distributed
using SharedArrays
using JLD2
n_trials = 10^2;

results = SharedArray{Float64}(n_trials);

@sync @distributed for i in 1:n_trials
    results[i] = i;
end
jldsave("test.jld2"; results)

```

when I then go to load it:

```julia
using JLD2
data = jldopen("test.jld2")
results = data["results"]

```

I get the error:

```julia
ERROR: ConcurrencyViolationError("setfield!: atomic field cannot be written non-atomically")
Stacktrace:
  [1] macro expansion
    @ ~/.julia/packages/JLD2/AilrO/src/data/reconstructing_datatypes.jl:563 [inlined]
  [2] jlconvert(#unused#::JLD2.ReadRepresentation{Distributed.Future, JLD2.OnDiskRepresentation{(0, 8, 16, 24, 32), Tuple{Int64, Int64, Int64, ReentrantLock, Union{Nothing, Some{Any}}}, Tuple{Int64, Int64, Int64, JLD2.RelOffset, JLD2.RelOffset}}()}, f::JLD2.JLDFile{JLD2.MmapIO}, ptr::Ptr{Nothing}, header_offset::JLD2.RelOffset)
    @ JLD2 ~/.julia/packages/JLD2/AilrO/src/data/reconstructing_datatypes.jl:508
  [3] read_scalar
# and it continues for quite a bit

```

Is this a bug? Or is there an alternative way to save SharedArrays?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 28, 2021, 8:15pm UTC](https://discourse.julialang.org/t/saving-sharedarrays/73723/2 "2021-12-28T20:15:33Z")

</div>

I assume if you `jldsave("test.jld2"; results = results.s)`, loading it works?

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [December 28, 2021, 8:22pm UTC](https://discourse.julialang.org/t/saving-sharedarrays/73723/3 "2021-12-28T20:22:53Z")

</div>

Yes, that works.
