# Vector of Atomics

**URL:** <https://discourse.julialang.org/t/vector-of-atomics/64984>\
**Category:** General Usage\
**Tags:** multithreading, atomic\
**Created:** [July 20, 2021, 2:04pm UTC](https://discourse.julialang.org/t/vector-of-atomics/64984 "2021-07-20T14:04:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nabla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nabla/32/26089_2.png) [@nabla](https://discourse.julialang.org/u/nabla)\
**Post date:** [July 20, 2021, 2:04pm UTC](https://discourse.julialang.org/t/vector-of-atomics/64984/1 "2021-07-20T14:04:53Z")

</div>

I’m aware of the `Atomic{Int8}` type, which wraps an `Int8` into an atomically modifiable value. Internally, this is a `mutable struct` containing the `Int8`, and it seems to be the only possibility since by nature the `Int8` must reside in memory.

However, I’d like to have an array of (say 10 billion) atomics. Performance and memory footprint become horrendous with an array of 10 billion mutable structs. Is there a way in Julia to create such an array (and apply `atomic_cas!` etc. to its individual entries)?

---

<div class="post-metadata">

**Author:** ![pitsianis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitsianis/32/26588_2.png) [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Post date:** [August 10, 2021, 3:02pm UTC](https://discourse.julialang.org/t/vector-of-atomics/64984/2 "2021-08-10T15:02:10Z")

</div>

I would map 1:n, the vector locations, to 1:m mutex keys and make each thread to acquire the appropriate key to enter the critical section that would allow the update of that vector position.

Tune m to the needs of your application. Try a block, cyclic, or block-cyclic mapping.  
The larger the m, the larger the memory footprint, the fewer false conflicts.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 10, 2021, 3:04pm UTC](https://discourse.julialang.org/t/vector-of-atomics/64984/3 "2021-08-10T15:04:28Z")

</div>

why do you need a 75GB array to be individually atomic? (btw vectors are thread-safe as long as you don’t access the same index at the same time).

You have plenty of space to let each thread handle a portion of the vector.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 13, 2021, 8:27am UTC](https://discourse.julialang.org/t/vector-of-atomics/64984/4 "2021-08-13T08:27:19Z")

</div>

When I played with Intel’s hardware transactional memory in Julia ([TSXPlayground.jl](https://github.com/tkf/TSXPlayground.jl)) I also tried a “pure atomics” benchmark as a baseline. As you can see, it’s rather hard to get a good performance when just using atomics (the first row; see TSXPlayground.jl’s README for more info). At least, the access pattern needs to be very sparse:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/3/d31129c822b88bfdcce3522d427c4111b5bc4784.png)

Anyway, you can have a look at [https://github.com/tkf/TSXPlayground.jl/blob/master/src/UnsafeAtomics.jl](https://github.com/tkf/TSXPlayground.jl/blob/master/src/UnsafeAtomics.jl) for how to update the array elements atomically by generating LLVM IR directly.
