Vector of Atomics

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)?

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.

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.

When I played with Intel’s hardware transactional memory in Julia (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:

Anyway, you can have a look at TSXPlayground.jl/UnsafeAtomics.jl at master · tkf/TSXPlayground.jl · GitHub for how to update the array elements atomically by generating LLVM IR directly.