7

So I was trying to optimize an array operation in Julia, but noticed that I was getting a rather large error on my matrix occasionally. I also noticed that there existed the possibility of concurrently writing to the same index of a SharedArray in Julia. I was wondering if Julia can safely handle it. If not, how may I able able to handle it?

Here is a basic example of my issue

for a list of arbitrary x,y indexes in array J
    j[x,y] += some_value
end

Can Julia handle this case or, like C, will there exist the possibility of overwriting the data. Are their atomic operations in Julia to compensate ffor this?

1 Answer 1

5

Shared arrays deliberately have no locking, since locking can be expensive. The easiest approach is to assign non-overlapping work to different processes. However, you might search to see whether someone has written a locking library, or have a go at it yourself: https://en.wikipedia.org/wiki/Mutual_exclusion

4
  • Apparently Julia does have support for locking. It's part of the Base Package: stackoverflow.com/questions/33778907/how-to-use-lock-in-julia .
    – Skylion
    May 13, 2016 at 18:16
  • 1
    That's true, although keep in mind that solution is designed to work even for work distributed across multiple computers. If you want one lock per element of the array, I suspect you will be unhappy with the amount of overhead of the general solution, and will want something optimized for SharedArrays.
    – tholy
    May 14, 2016 at 8:53
  • Given this information, what would you suggest then? I suppose if I wait until Julia 0.5 is released we can add support for PThread and use Atomics unless you have a more elegant solution in mind? Ideally, it would be nice if the code could run on a distributed system, but not a necessity.
    – Skylion
    May 15, 2016 at 16:39
  • 1
    Well, first, feel free to try the built-in locking; if you're happy with the performance, great. If not, I'd just pick one of those algorithms (Szymanski?) on the page I linked to and implement it. I can't say for sure since I haven't tried, but I'd be surprised if it was more than 50 lines of code...and you'll create a package that might be valued by many people.
    – tholy
    May 16, 2016 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.