Skip to content

Commit

Permalink
Remove config, add flag
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielaBreitman committed Mar 25, 2024
1 parent e1a9360 commit 95ffea7
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 367 deletions.
213 changes: 58 additions & 155 deletions docs/demos/mpi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If we want to run powerbox in parallel via multiprocessing, it's important to control the number of threads used during the FFT operation. The simplest way of dealing with this is to uninstall `pyFFTW` from the environment you use to run `powerbox`, that way only the single-threaded `numpy` `FFT` routine is used. However, if you want to keep `pyFFTW` installed in your environment, this can be done in two ways: (i) Use `pyFFTW` while manually setting the number of threads to 1. (ii) Revert to using the single-threaded `numpy` implementation of `FFT` by setting `USE_FFTW` to False. Both methods require a change to the default configuration of `powerbox` which will be demonstrated in this tutorial."
"If we want to run powerbox in parallel via multiprocessing, it's important to control the number of threads used during the FFT operation. The simplest way of dealing with this is to uninstall `pyFFTW` from the environment you use to run `powerbox`, that way only the single-threaded `numpy` `FFT` routine is used. However, if you want to keep `pyFFTW` installed in your environment, this can be done in two ways: (i) Use `pyFFTW` while manually setting the number of threads to 1. (ii) Revert to using the single-threaded `numpy` implementation of `FFT`. In this tutorial, we demonstrate how to implement these two methods using the `threads` flag."
]
},
{
Expand All @@ -25,62 +25,28 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/dani/anaconda3/envs/py10/lib/python3.10/site-packages/powerbox/dft.py:40: UserWarning: Using pyFFTW with 8 threads...\n",
" warnings.warn(\"Using pyFFTW with \" + str(THREADS) + \" threads...\")\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'USE_FFTW': True, 'THREADS': 8}\n"
]
}
],
"source": [
"import powerbox as pb\n",
"pb.__version__\n",
"from powerbox import CONFIG\n",
"print(dict(CONFIG.items()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You might need to delete the config file if you have run this before and the defaults do not restore properly. Check the location of the config file:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PosixPath('/mnt/data/PhD/Trantor/CV_PS_denoising/powerbox/docs/demos/config.toml')"
"'0.7.4.dev21+ge1a9360.d20240325'"
]
},
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CONFIG.config_file"
"import powerbox as pb\n",
"pb.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, let's define a simple `powerbox` operation to test. This function calculates a power spectrum on a random box of dimension $300^3$ and returns the time the computation took."
"First, let's define a simple `powerbox` operation to test. This function calculates a power spectrum on a random box of dimension $300^3$ and returns the computation time."
]
},
{
Expand All @@ -92,17 +58,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Done: 0.96\n",
"Done: 1.03\n",
"Done: 1.02\n",
"Done: 0.9\n",
"Done: 0.89\n",
"Done: 0.89\n",
"Done: 0.97\n",
"Done: 0.92\n",
"Done: 0.94\n",
"Done: 1.13\n",
"Done: 0.95\n",
"Single iteration time: 0.95 s\n"
"Done: 0.91\n",
"Done: 0.9\n",
"Done: 0.91\n",
"Done: 0.9\n",
"Done: 0.9\n",
"Done: 0.9\n",
"Done: 0.9\n",
"Done: 0.9\n",
"Single iteration time: 0.93 s\n"
]
}
],
Expand Down Expand Up @@ -139,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -149,7 +115,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[4], line 8\u001b[0m\n\u001b[1;32m 6\u001b[0m p\u001b[38;5;241m.\u001b[39mapply_async(run_pb, args \u001b[38;5;241m=\u001b[39m (i,))\n\u001b[1;32m 7\u001b[0m p\u001b[38;5;241m.\u001b[39mclose()\n\u001b[0;32m----> 8\u001b[0m \u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTotal:\u001b[39m\u001b[38;5;124m'\u001b[39m, np\u001b[38;5;241m.\u001b[39mround(time() \u001b[38;5;241m-\u001b[39m start,\u001b[38;5;241m2\u001b[39m),\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Runs for > 2 mins\u001b[39;00m\n",
"Cell \u001b[0;32mIn[3], line 8\u001b[0m\n\u001b[1;32m 6\u001b[0m p\u001b[38;5;241m.\u001b[39mapply_async(run_pb, args \u001b[38;5;241m=\u001b[39m (i,))\n\u001b[1;32m 7\u001b[0m p\u001b[38;5;241m.\u001b[39mclose()\n\u001b[0;32m----> 8\u001b[0m \u001b[43mp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTotal:\u001b[39m\u001b[38;5;124m'\u001b[39m, np\u001b[38;5;241m.\u001b[39mround(time() \u001b[38;5;241m-\u001b[39m start,\u001b[38;5;241m2\u001b[39m),\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Runs for > 3 mins\u001b[39;00m\n",
"File \u001b[0;32m~/anaconda3/envs/py10/lib/python3.10/multiprocessing/pool.py:665\u001b[0m, in \u001b[0;36mPool.join\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 663\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_state \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m (CLOSE, TERMINATE):\n\u001b[1;32m 664\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIn unknown state\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 665\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_worker_handler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 666\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_task_handler\u001b[38;5;241m.\u001b[39mjoin()\n\u001b[1;32m 667\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_result_handler\u001b[38;5;241m.\u001b[39mjoin()\n",
"File \u001b[0;32m~/anaconda3/envs/py10/lib/python3.10/threading.py:1096\u001b[0m, in \u001b[0;36mThread.join\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 1093\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot join current thread\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1095\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m timeout \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1096\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_wait_for_tstate_lock\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1097\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1098\u001b[0m \u001b[38;5;66;03m# the behavior of a negative timeout isn't documented, but\u001b[39;00m\n\u001b[1;32m 1099\u001b[0m \u001b[38;5;66;03m# historically .join(timeout=x) for x<0 has acted as if timeout=0\u001b[39;00m\n\u001b[1;32m 1100\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_wait_for_tstate_lock(timeout\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mmax\u001b[39m(timeout, \u001b[38;5;241m0\u001b[39m))\n",
"File \u001b[0;32m~/anaconda3/envs/py10/lib/python3.10/threading.py:1116\u001b[0m, in \u001b[0;36mThread._wait_for_tstate_lock\u001b[0;34m(self, block, timeout)\u001b[0m\n\u001b[1;32m 1113\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m 1115\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1116\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mlock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43macquire\u001b[49m\u001b[43m(\u001b[49m\u001b[43mblock\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m 1117\u001b[0m lock\u001b[38;5;241m.\u001b[39mrelease()\n\u001b[1;32m 1118\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_stop()\n",
Expand Down Expand Up @@ -179,55 +145,19 @@
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'USE_FFTW': True, 'THREADS': 1}\n"
]
}
],
"source": [
"CONFIG[\"THREADS\"] = 1\n",
"print(dict(CONFIG.items()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After changing the config file, re-start the kernel and re-run the cell defining `run_pb`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/dani/anaconda3/envs/py10/lib/python3.10/site-packages/powerbox/dft.py:40: UserWarning: Using pyFFTW with 1 threads...\n",
" warnings.warn(\"Using pyFFTW with \" + str(THREADS) + \" threads...\")\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'USE_FFTW': True, 'THREADS': 1}\n"
]
}
],
"outputs": [],
"source": [
"import powerbox as pb\n",
"pb.__version__\n",
"from powerbox import CONFIG\n",
"print(dict(CONFIG.items()))"
"def run_pb(an_arg):\n",
" shape = (200,200,200) # Size of one chunk\n",
" arr = np.random.rand(np.prod(shape)).reshape(shape) * an_arg\n",
" t0 = time()\n",
" out = get_power(arr, (300,300,300),\n",
" bins = 50,\n",
" threads = 1)\n",
" print('Done: ', np.round(time() - t0,2), flush = True)\n",
" return time() - t0"
]
},
{
Expand All @@ -239,22 +169,22 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done: Done: 10.4Done: \n",
" \n",
"Done: \n",
"Done: 10.5710.4110.58 10.63\n",
"Done: Done: 22.8324.25\n",
"\n",
"Done: Done: 10.63\n",
" 10.65\n",
"Done: 10.56\n",
"Total: 11.08 s\n"
"Done: Done: 24.15\n",
" 24.73Done: \n",
"24.37\n",
"Done: 24.83Done: \n",
" 24.62\n",
"Done: 24.43\n",
"Total: 25.41 s\n"
]
}
],
Expand Down Expand Up @@ -286,66 +216,39 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"CONFIG[\"USE_FFTW\"] = False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, the kernel must be restarted in order for the config file changes to take effect."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/dani/anaconda3/envs/py10/lib/python3.10/site-packages/powerbox/dft.py:66: UserWarning: Using numpy FFT...\n",
" warnings.warn(\"Using numpy FFT...\")\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'USE_FFTW': False, 'THREADS': 1}\n"
]
}
],
"source": [
"import powerbox as pb\n",
"pb.__version__\n",
"from powerbox import CONFIG\n",
"print(dict(CONFIG.items()))"
"def run_pb(an_arg):\n",
" shape = (200,200,200) # Size of one chunk\n",
" arr = np.random.rand(np.prod(shape)).reshape(shape) * an_arg\n",
" t0 = time()\n",
" out = get_power(arr, (300,300,300),\n",
" bins = 50,\n",
" threads = False)\n",
" print('Done: ', np.round(time() - t0,2), flush = True)\n",
" return time() - t0"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done: 10.34\n",
"Done: 10.63Done: \n",
" Done: 10.6\n",
" 10.66\n",
"Done: 10.59\n",
"Done: Done: 10.64 \n",
"Done: 10.59\n",
" 10.65\n",
"Total: 11.11 s\n"
"Done: 23.74\n",
"Done: Done: 24.26 24.3\n",
"Done: \n",
" 24.37Done: \n",
" 24.22\n",
"Done: 24.09\n",
"Done: Done: 24.3124.36\n",
"\n",
"Total: 24.79 s\n"
]
}
],
Expand Down
3 changes: 1 addition & 2 deletions src/powerbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# package is not installed
__version__ = "unknown"

from .dft import fft, fftfreq, ifft
from .powerbox import LogNormalPowerBox, PowerBox
from .tools import angular_average, angular_average_nd, get_power
from .config import CONFIG
from .import_fft import config
Loading

0 comments on commit 95ffea7

Please sign in to comment.