Skip to content

Commit a390532

Browse files
committed
make pval atomic to avoid data race
1 parent bcc6af2 commit a390532

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/FStatistics/FstPermutations.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ function _fst_permutation(data::PopData, method::Function, iterations::Int64)
4040
n_pop2 = size(pop2, 1)
4141
merged = vcat(pop1, pop2)
4242
fst_val = method(pop1,pop2)
43-
pval = 0
44-
@inbounds @sync for iter in 1:iterations-1
43+
pval = Threads.Atomic{Int}(0)
44+
@inbounds for iter in 1:iterations-1
4545
Base.Threads.@spawn begin
46-
@inbounds perm_p1, perm_p2 = _fst_permute(merged, n_pop1, n_pop2)
47-
pval += fst_val <= method(perm_p1, perm_p2)
46+
@inbounds perm_p1, perm_p2 = _fst_permute(merged, n_pop1, n_pop2)
47+
Base.Threads.atomic_add!(pval, Int(fst_val <= method(perm_p1, perm_p2)))
4848
end
4949
end
5050
@inbounds results[i,j] = fst_val
51-
@inbounds results[j,i] = (pval + 1) / iterations
51+
@inbounds results[j,i] = (pval[] + 1) / iterations
5252
update!(job)
5353
end
5454
end
@@ -95,8 +95,7 @@ function _amovafst_permutation(data::PopData, iterations::Int64)
9595
FST = σ²_among / (σ²_among + σ²_within)
9696
#@inbounds results[i,j] = results[j,i] = FST
9797
@inbounds results[i,j] = FST
98-
99-
pval = 0
98+
pval = Threads.Atomic{Int}(0)
10099
@inbounds @sync for iter in 1:iterations-1
101100
Base.Threads.@spawn begin
102101
p1, p2 = partitionarray(shuffle(vcat(pop1, pop2)), [n1, n2])
@@ -111,10 +110,10 @@ function _amovafst_permutation(data::PopData, iterations::Int64)
111110
i_n_c = (N - ((n1^2 + n2^2)/N)) #/ df_among
112111
i_σ²_among = ((i_SS_among / df_among) - i_σ²_within) / i_n_c
113112
i_FST = i_σ²_among / (i_σ²_among + i_σ²_within)
114-
pval += FST <= i_FST
113+
Base.Threads.atomic_add!(pval, Int(FST <= i_FST))
115114
end
116115
end
117-
@inbounds results[j,i] = (pval + 1) / iterations
116+
@inbounds results[j,i] = (pval[] + 1) / iterations
118117
update!(job)
119118
end
120119
end

0 commit comments

Comments
 (0)