forked from minaskar/zeus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
13,699 additions
and
446 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Parallelizing sampling using MPI" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To take advantage of modern high performance computing facilities such as clusters with hundreds of CPUs we recommend to use MPI instead of multiprocessing.\n", | ||
"\n", | ||
"To do this we will use the MPIPoolExecutor of mpi4py.futures package.\n", | ||
"\n", | ||
"In order to run this example, copy and paste the following script into a file called 'test_mpi.py' and run the following command in the terminal:\n", | ||
"\n", | ||
"```\n", | ||
"mpiexec -n 1 python3 test_mpi.py\n", | ||
"```\n", | ||
"\n", | ||
"This will spawn 1 MPI process initially, and only add more when actually needed. Unfortunately MPI is not compatible with Jupyter notebooks." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Save this as 'test_mpi.py'\n", | ||
"\n", | ||
"```python\n", | ||
"\n", | ||
"import numpy as np\n", | ||
"import zeus\n", | ||
"from mpi4py.futures import MPIPoolExecutor\n", | ||
"\n", | ||
"ndim = 5\n", | ||
"nwalkers = 2 * ndim\n", | ||
"nsteps = 100\n", | ||
"\n", | ||
"def log_prob(x):\n", | ||
" return -0.5 * np.sum(x**2.0)\n", | ||
"\n", | ||
"start = np.random.randn(nwalkers, ndim)\n", | ||
"\n", | ||
"\n", | ||
"if __name__ == '__main__':\n", | ||
"\n", | ||
" with MPIPoolExecutor() as executor:\n", | ||
" sampler = zeus.sampler(nwalkers, ndim, log_prob, pool=executor)\n", | ||
" sampler.run_mcmc(start, nsteps)\n", | ||
" \n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.6" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": false, | ||
"sideBar": true, | ||
"skip_h1_title": false, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": { | ||
"height": "calc(100% - 180px)", | ||
"left": "10px", | ||
"top": "150px", | ||
"width": "288px" | ||
}, | ||
"toc_section_display": true, | ||
"toc_window_display": false | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
188 changes: 188 additions & 0 deletions
188
docs/_build/doctrees/nbsphinx/notebooks/multiprocessing.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Parallelizing sampling using multiprocessing" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We are going to use the multiprocessing Pool to parallelize and accelerate sampling.\n", | ||
"\n", | ||
"This approach is ideal for personal computers, laptops, or small clusters and should work even in Jupyter notebooks. \n", | ||
"\n", | ||
"In order to simulate a computationaly expensive log probability density function we will use the time package." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import zeus\n", | ||
"import numpy as np\n", | ||
"import time\n", | ||
"from multiprocessing import Pool" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We define an uncorrelated normal distribution as our target distribution." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ndim = 5\n", | ||
"nwalkers = 2 * ndim\n", | ||
"nsteps = 100\n", | ||
"\n", | ||
"def log_prob(x):\n", | ||
" time.sleep(0.003)\n", | ||
" return -0.5 * np.sum(x**2.0)\n", | ||
"\n", | ||
"start = np.random.randn(nwalkers, ndim)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We first run the sampler without in serial, without multiprocessing:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Initialising ensemble of 10 walkers...\n", | ||
"Sampling progress : 100%|██████████| 100/100 [00:19<00:00, 5.18it/s]" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Serial took 19.3 seconds\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"t0 = time.time()\n", | ||
"\n", | ||
"sampler = zeus.sampler(nwalkers, ndim, log_prob)\n", | ||
"sampler.run_mcmc(start, nsteps)\n", | ||
"\n", | ||
"print(\"Serial took {0:.1f} seconds\".format(time.time()-t0))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"And then run the sampler with multiprocessing:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Initialising ensemble of 10 walkers...\n", | ||
"Sampling progress : 100%|██████████| 100/100 [00:07<00:00, 12.93it/s]" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Multiprocessing took 7.8 seconds\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"t0 = time.time()\n", | ||
"\n", | ||
"with Pool() as pool:\n", | ||
" sampler = zeus.sampler(nwalkers, ndim, log_prob, pool=pool)\n", | ||
" sampler.run_mcmc(start, nsteps)\n", | ||
"\n", | ||
"print(\"Multiprocessing took {0:.1f} seconds\".format(time.time()-t0))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.6" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": false, | ||
"sideBar": true, | ||
"skip_h1_title": false, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": {}, | ||
"toc_section_display": true, | ||
"toc_window_display": false | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.