Skip to content

Commit 2574bb9

Browse files
Merge pull request #119 from SciML/dw/sobol_allocations
Faster generation of Sobol samples
2 parents 5c54835 + c71e8fa commit 2574bb9

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "QuasiMonteCarlo"
22
uuid = "8a4e6c94-4038-4cdc-81c3-7e6ffdb2a71b"
33
authors = ["ludoro <ludovicobessi@gmail.com>, Chris Rackauckas <accounts@chrisrackauckas.com>"]
4-
version = "0.3.3"
4+
version = "0.3.4"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
@@ -50,4 +50,4 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
5050
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5151

5252
[targets]
53-
test = ["Aqua", "Combinatorics", "Distributions", "HypothesisTests", "IntervalArithmetic", "LinearAlgebra", "Primes", "Random", "Statistics", "StatsBase", "Test"]
53+
test = ["Aqua", "Combinatorics", "Distributions", "HypothesisTests", "IntervalArithmetic", "LinearAlgebra", "Primes", "Random", "Statistics", "StatsBase", "Test"]

src/Sobol.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,24 @@ Base.@kwdef @concrete struct SobolSample <: DeterministicSamplingAlgorithm
88
end
99

1010
function sample(n::Integer, d::Integer, S::SobolSample, T = Float64)
11-
s = Sobol.SobolSeq(zeros(T, d), ones(T, d))
12-
skip(s, n)
13-
return randomize(reduce(hcat, [Sobol.next!(s) for i in 1:n]), S.R)
11+
if n < 0
12+
throw(ArgumentError("number of samples must be non-negative"))
13+
end
14+
15+
seq = Matrix{T}(undef, d, n)
16+
if n == 0
17+
return seq
18+
end
19+
20+
# Use function barrier since `Sobol.SobolSeq(d)` can't be inferred
21+
return _sample!(seq, Sobol.SobolSeq(d), S.R)
22+
end
23+
24+
function _sample!(seq::AbstractMatrix, s::Sobol.SobolSeq, R::RandomizationMethod)
25+
n = size(seq, 2)
26+
Sobol.skip!(s, n, @view(seq[:, begin]))
27+
for x in eachcol(seq)
28+
Sobol.next!(s, x)
29+
end
30+
return randomize(seq, R)
1431
end

0 commit comments

Comments
 (0)