Hi, would appreciate some feedback about this issue. I’m trying to understand the difference between @threads and @spawn.
I think the former is easy to understand. I have an experiment shuffling cards, many many times. See if royal flush appears. I have supplied --threads=8 in the command line. Thus the following loop processes 8 batches in parallel at any one time. It will finish only when all 20 batches complete.
batches = 20
@threads for i in 1:batches
batch!(...)
end
Indeed, that’s what it does.
But I read about @spawn first before I understand the simpler syntax of @threads. So I had written the loop this way in the beginning:
@sync for i in 1:batches
Threads.@spawn batch!(...)
end
I thought it also does the same thing as above… Are they equivalent? This spawns 20 threads at once. I guess 8 of them will run at any one time. @sync should wait until they all completed.
Apparently performance-wise they are not the same. The second one doesn’t behave like the first. I can see in “top” the julia process started with 800% cpu (correct… 8 threads). But after completing two batches, CPU dropped down to 400%… then after a much longer time a couple more batches completed. Then CPU dropped down to 200%. Eventually CPU dropped down to 100% without any more batch completion. It’s obviously not what I intended. And the experiment ran too long I had to Ctrl-C it.
How come?
Thanks
Happy to supply source code (about 100 lines). Let me know if anyone wants to see it.