Skip to content

Commit

Permalink
Fix deprecations for 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
awllee committed Jun 1, 2018
1 parent 4c99f03 commit d9c5ff4
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ os:
- osx
julia:
- 0.6
- 0.7
- nightly
notifications:
email: false
Expand All @@ -15,6 +16,7 @@ git:
## (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia: 0.7
- julia: nightly

## uncomment and modify the following lines to manually install system packages
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Pkg.checkout("SequentialMonteCarlo")
## Load the package

using SequentialMonteCarlo
import Compat.Nothing

## Define a particle consisting of one Float64

Expand All @@ -39,7 +40,7 @@ end
## define a non-stationary Markov chain, since 1.5 > 1.

function M!(newParticle::Float64Particle, rng::SMCRNG, p::Int64,
particle::Float64Particle, ::Void)
particle::Float64Particle, ::Nothing)
if p == 1
newParticle.x = randn(rng)
else
Expand All @@ -50,7 +51,7 @@ end
## The log potential function is x -> -x^2 so the potential function is
## x -> exp(-x^2).

function lG(p::Int64, particle::Float64Particle, ::Void)
function lG(p::Int64, particle::Float64Particle, ::Nothing)
return - particle.x * particle.x
end

Expand All @@ -61,10 +62,10 @@ end

## Specify the model using M! and lG, stating that the maximum
## length of the model is 100, and specifying the types of the particle and
## particle scratch space. The latter is Void in this case as no scratch space
## particle scratch space. The latter is Nothing in this case as no scratch space
## is required.

model = SMCModel(M!, lG, 100, Float64Particle, Void)
model = SMCModel(M!, lG, 100, Float64Particle, Nothing)

## Create the SMC input/output struct, specifying the number of particles N as
## 2^20 = 1048576, that the algorithm should be run for 10 steps, that 1 thread
Expand Down
2 changes: 1 addition & 1 deletion docs/src/smcinterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lG(p::Int64, particle::Particle, scratch::ParticleScratch)

There is a correspondence between the function ```M!``` and $M_1, \ldots, M_n$: calling ```M!(x', rng, p, x, scratch)```, should make $x'$ a realization of a sample from $M_p(x, \cdot)$ with the convention that $M_1(x,\cdot) = M_1(\cdot)$ for any $x$. Similarly, ```lG``` and $G_1, \ldots, G_n$ correspond in that ```lG(p,x)``` $ = \log G_p(x)$. Logarithms are used to avoid numerical issues. ```maxn``` is the maximum value of ```n``` for which ```M!``` and ```lG``` are well-defined; users may choose to run the SMC algorithm for any integer value of ```n``` up to and including ```maxn```.

The types ```Particle``` and ```ParticleScratch``` must have constructors that take no arguments. One may choose ```ParticleScratch = Void```, in which case ```nothing``` will be passed to ```M!``` and ```lG```. Using scratch space is optional but can significantly improve performance in certain scenarios; it provides a mechanism for users to avoid dynamic memory allocations in ```M!``` and/or ```lG```. This scratch space will be used by every particle associated with a given thread. A thread-specific pseudo-random number generator (RNG) ```rng``` will be passed to the ```M!``` function by the algorithm, and should be used in lieu of Julia's global RNG.
The types ```Particle``` and ```ParticleScratch``` must have constructors that take no arguments. One may choose ```ParticleScratch = Nothing```, in which case ```nothing``` will be passed to ```M!``` and ```lG```. Using scratch space is optional but can significantly improve performance in certain scenarios; it provides a mechanism for users to avoid dynamic memory allocations in ```M!``` and/or ```lG```. This scratch space will be used by every particle associated with a given thread. A thread-specific pseudo-random number generator (RNG) ```rng``` will be passed to the ```M!``` function by the algorithm, and should be used in lieu of Julia's global RNG.

## Running the SMC algorithm

Expand Down
2 changes: 1 addition & 1 deletion src/SequentialMonteCarlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ __precompile__()

module SequentialMonteCarlo

import Compat.uninitialized
import Compat.undef

include("smcrng.jl")
include("structures.jl")
Expand Down
9 changes: 5 additions & 4 deletions src/common.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import NonUniformRandomVariateGeneration.sampleCategorical
import Compat: hasmethod, copyto!

# basic routines used by both smcSerial and smcParallel

Expand All @@ -7,15 +8,15 @@ import NonUniformRandomVariateGeneration.sampleCategorical
fieldNames = fieldnames(dest)
fieldTypes = dest.types
numFields = length(fieldNames)
expressions = Array{Expr}(uninitialized, numFields)
expressions = Array{Expr}(undef, numFields)

for i = 1:numFields
fieldName = fieldNames[i]
fieldType = fieldTypes[i]
@assert !fieldType.mutable || method_exists(Base.copy!, (fieldType,
fieldType)) "$fieldName::$fieldType : Base.copy! must exist for mutable Particle fields"
@assert !fieldType.mutable || hasmethod(copyto!, (fieldType,
fieldType)) "$fieldName::$fieldType : copyto! must exist for mutable Particle fields"
if fieldType.mutable
@inbounds expressions[i] = :(Base.copy!(dest.$fieldName, src.$fieldName))
@inbounds expressions[i] = :(copyto!(dest.$fieldName, src.$fieldName))
else
@inbounds expressions[i] = :(dest.$fieldName = src.$fieldName)
end
Expand Down
13 changes: 8 additions & 5 deletions src/interface.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Compat.copyto!

"""
smc!(model::SMCModel, smcio::SMCIO)
Run the SMC algorithm for the given model and input/output arguments.
Expand Down Expand Up @@ -79,7 +81,7 @@ Compute ```eta(smcio::SMCIO, f::F, hat::Bool, p)``` for p in {1, …, smcio.n}
"""
function allEtas(smcio::SMCIO, f::F, hat::Bool) where F<:Function
T = typeof(f(smcio.zetas[1]) / smcio.N)
result::Vector{T} = Vector{T}(uninitialized, smcio.n)
result::Vector{T} = Vector{T}(undef, smcio.n)
for p = 1:smcio.n
@inbounds result[p] = eta(smcio, f, hat, p)
end
Expand Down Expand Up @@ -112,7 +114,8 @@ end
Compute ```slgamma(smcio::SMCIO, f::F, hat::Bool, p)``` for p in {1, …, smcio.n}
"""
function allGammas(smcio::SMCIO, f::F, hat::Bool) where F<:Function
result::Vector{Tuple{Bool,Float64}} = Vector{Tuple{Bool,Float64}}(smcio.n)
result::Vector{Tuple{Bool, Float64}} =
Vector{Tuple{Bool, Float64}}(undef, smcio.n)
for p = 1:smcio.n
@inbounds result[p] = slgamma(smcio, f, hat, p)
end
Expand Down Expand Up @@ -234,7 +237,7 @@ function vpns(smcio::SMCIO, f::F, hat::Bool, centred::Bool,
end
end

result = Vector{Float64}(n)
result = Vector{Float64}(undef, n)
factor0::Float64 = (N/(N-1))^n
factor1::Float64 = (N-1)*factor0
vp::Float64 = 0.0
Expand Down Expand Up @@ -266,7 +269,7 @@ function vpns(smcio::SMCIO, f::F, hat::Bool, centred::Bool,
@inbounds vp += (R1*R1 - R2) * (1.0 - Glocal[eves[p][a]])
end
@inbounds result[p] = factor1 * vp
copy!(Stmp, Slocal)
copyto!(Stmp, Slocal)
fill!(Slocal, 0.0)
for i = 1:N
@inbounds Slocal[as[p][i]] += Stmp[i]
Expand All @@ -280,7 +283,7 @@ function vpns(smcio::SMCIO, f::F, hat::Bool, centred::Bool,
mstar::Float64 = factor0 * (etafSq - vp)
result .-= mstar

actualResult = Vector{Float64}(sum(smcio.resample[1:n-1]) + 1)
actualResult = Vector{Float64}(undef, sum(smcio.resample[1:n-1]) + 1)
j::Int64 = 0
for i = 1:length(actualResult)-1
j += 1
Expand Down
6 changes: 3 additions & 3 deletions src/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ end

@inline function _MutateWeight!(p::Int64, model::SMCModel,
smcio::SMCIO{Particle, ParticleScratch},
ref::Union{Void, Particle} = nothing) where {Particle, ParticleScratch}
ref::Union{Nothing, Particle} = nothing) where {Particle, ParticleScratch}
Threads.@threads for i = 1:smcio.nthreads
ip::_SMCInternalParallel{Particle, ParticleScratch} = smcio.internal.parallel
@inbounds localZetas::SubVector{Particle} = ip.localZetas[i]
Expand Down Expand Up @@ -197,8 +197,8 @@ end

# Parallel implementation of SMC
@inline function _smcParallel!(model::SMCModel, smcio::SMCIO{Particle},
ref::Union{Void, Vector{Particle}} = nothing,
refout::Union{Void, Vector{Particle}} = nothing) where Particle
ref::Union{Nothing, Vector{Particle}} = nothing,
refout::Union{Nothing, Vector{Particle}} = nothing) where Particle
smcio.internal.nresamples = 0
lZ::Float64 = 0.0
_iotaParallel!(smcio.eves, smcio.N, smcio.nthreads,
Expand Down
6 changes: 4 additions & 2 deletions src/serial.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Compat.Nothing

@inline function _iota!(array::Vector{Int64})
for i in eachindex(array)
@inbounds array[i] = i
Expand Down Expand Up @@ -55,8 +57,8 @@ end

# Serial implementation of SMC and cSMC
@inline function _smcSerial!(model::SMCModel, smcio::SMCIO{Particle},
ref::Union{Void, Vector{Particle}} = nothing,
refout::Union{Void, Vector{Particle}} = nothing) where Particle
ref::Union{Nothing, Vector{Particle}} = nothing,
refout::Union{Nothing, Vector{Particle}} = nothing) where Particle
zetas = smcio.zetas
zetaAncs = smcio.internal.zetaAncs
lws = smcio.internal.lws
Expand Down
8 changes: 5 additions & 3 deletions src/smcrng.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Compat.Random

# import RandomNumbers.Random123.Threefry4x
#
# const SMCRNG = Threefry4x{UInt64, 20}
Expand Down Expand Up @@ -26,7 +28,7 @@
const SMCRNG = MersenneTwister

let
engines::Vector{SMCRNG} = Vector{SMCRNG}(uninitialized, 0)
engines::Vector{SMCRNG} = Vector{SMCRNG}(undef, 0)
global function getSMCRNG()
@inbounds return engines[Threads.threadid()]
end
Expand All @@ -38,10 +40,10 @@ let
end
## happens at runtime to avoid false sharing
global function initializeSMCRNGs()
engines = Vector{SMCRNG}(uninitialized, Threads.nthreads())
engines = Vector{SMCRNG}(undef, Threads.nthreads())
Threads.@threads for i = 1:length(engines)
@inbounds engines[Threads.threadid()] =
MersenneTwister(Base.Random.make_seed())
MersenneTwister(Random.make_seed())
end
setSMCRNGs(0)
end
Expand Down

0 comments on commit d9c5ff4

Please sign in to comment.