Sum result of Threads.foreach()

Using atomics, locks, or channels is very likely not the right approach if you are using parallelism for speeding things up (unless you know what you are doing). I normally suggest using Folds.sum or FLoops.@floop for this. For more information, see: A quick introduction to data parallelism in Julia

That said, if “no package” is the hard requirement, you can write something like the following (untested):

topic_word_pairs::AbstractArray  # assumption

basesize = cld(length(topic_word_pairs), Threads.nthreads())
chunks = Iterators.partition(topic_word_pairs, basesize)
sums_coherence = zeros(length(chunks))
nums_paris = zeros(Int, length(chunks))
@sync for (i, chunk) in enumerate(chunks)
    Threads.@spawn begin
        local sum_coherence = 0.0
        local num_pairs = 0
        for pair in chunk
            confirmation = calculate_confirmation(pair, model.corp)
            sum_coherence += confirmation
            num_pairs += 1
        end
        sums_coherence[i] = sum_coherence
        nums_pairs[i] = num_pairs
    end
end

sum_coherence = sum(sums_coherence)
num_pairs = sum(nums_pairs)
1 Like