Skip to content

Commit

Permalink
make pval atomic to avoid data race
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimens committed Jun 21, 2023
1 parent bcc6af2 commit a390532
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/FStatistics/FstPermutations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ function _fst_permutation(data::PopData, method::Function, iterations::Int64)
n_pop2 = size(pop2, 1)
merged = vcat(pop1, pop2)
fst_val = method(pop1,pop2)
pval = 0
@inbounds @sync for iter in 1:iterations-1
pval = Threads.Atomic{Int}(0)
@inbounds for iter in 1:iterations-1
Base.Threads.@spawn begin
@inbounds perm_p1, perm_p2 = _fst_permute(merged, n_pop1, n_pop2)
pval += fst_val <= method(perm_p1, perm_p2)
@inbounds perm_p1, perm_p2 = _fst_permute(merged, n_pop1, n_pop2)
Base.Threads.atomic_add!(pval, Int(fst_val <= method(perm_p1, perm_p2)))
end
end
@inbounds results[i,j] = fst_val
@inbounds results[j,i] = (pval + 1) / iterations
@inbounds results[j,i] = (pval[] + 1) / iterations
update!(job)
end
end
Expand Down Expand Up @@ -95,8 +95,7 @@ function _amovafst_permutation(data::PopData, iterations::Int64)
FST = σ²_among / (σ²_among + σ²_within)
#@inbounds results[i,j] = results[j,i] = FST
@inbounds results[i,j] = FST

pval = 0
pval = Threads.Atomic{Int}(0)
@inbounds @sync for iter in 1:iterations-1
Base.Threads.@spawn begin
p1, p2 = partitionarray(shuffle(vcat(pop1, pop2)), [n1, n2])
Expand All @@ -111,10 +110,10 @@ function _amovafst_permutation(data::PopData, iterations::Int64)
i_n_c = (N - ((n1^2 + n2^2)/N)) #/ df_among
i_σ²_among = ((i_SS_among / df_among) - i_σ²_within) / i_n_c
i_FST = i_σ²_among / (i_σ²_among + i_σ²_within)
pval += FST <= i_FST
Base.Threads.atomic_add!(pval, Int(FST <= i_FST))
end
end
@inbounds results[j,i] = (pval + 1) / iterations
@inbounds results[j,i] = (pval[] + 1) / iterations
update!(job)
end
end
Expand Down

1 comment on commit a390532

@pdimens
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addresses #116

Please sign in to comment.