Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mkolod/1cda6d25cc7506933c66aedfa2077094 to your computer and use it in GitHub Desktop.
Save mkolod/1cda6d25cc7506933c66aedfa2077094 to your computer and use it in GitHub Desktop.
Parallel Reservoir Sampling Equivalent
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Reservoir sampling is not normally parallel,\n",
"# but RNG generation, sorting and gather can be parallelized.\n",
"# This is for demonstrating the capability only\n",
"# See more here: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Sorting\n",
"def sim_parallel_shuffle(data):\n",
" rands = np.random.rand(data.size)\n",
" indices = np.argsort(rands)\n",
" return data[indices]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3 4 5 6 7 8 9]\n",
"[5 7 4 1 6 9 8 3 2 0]\n"
]
}
],
"source": [
"data = np.arange(10)\n",
"print(data)\n",
"shuffled = sim_parallel_shuffle(data)\n",
"print(shuffled)"
]
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment