Skip to content

Commit

Permalink
[RFC] Add multi-threaded example to readme (#197)
Browse files Browse the repository at this point in the history
* Add multithreaded example to readme

* Remove concatenation and address review suggestions
  • Loading branch information
Vaibhavdixit02 committed Jun 14, 2020
1 parent 394ab45 commit ef6de39
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -63,6 +63,31 @@ adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(0.8, integra
samples, stats = sample(hamiltonian, proposal, initial_θ, n_samples, adaptor, n_adapts; progress=true)
```

### Parallel sampling

AdvancedHMC enables parallel sampling (either distributed or multi-thread) via Julia's [parallel computing functions](https://docs.julialang.org/en/v1/manual/parallel-computing/).
It also supports vectorized sampling for static HMC and has been discussed in more detail in the documentation [here](https://turing.ml/dev/docs/library/advancedhmc/parallel_sampling).

The below example utilizes the `@threads` macro to sample 4 chains across 4 threads.

```julia
# Ensure that julia was launched with appropriate number of threads
println(Threads.nthreads())

# Number of chains to sample
nchains = 4

# Cache to store the chains
chains = Vector{Any}(undef, nchains)

# The `samples` from each parallel chain is stored in the `chains` vector
# Adjust the `verbose` flag as per need
Threads.@threads for i in 1:nchains
samples, stats = sample(hamiltonian, proposal, initial_θ, n_samples, adaptor, n_adapts; verbose=false)
chains[i] = samples
end
```

## API and supported HMC algorithms

An important design goal of AdvancedHMC.jl is modularity; we would like to support algorithmic research on HMC.
Expand Down

0 comments on commit ef6de39

Please sign in to comment.