Skip to content

Instantly share code, notes, and snippets.

@yuxuanzhuang
Created October 12, 2020 13:27
Show Gist options
  • Save yuxuanzhuang/05b0da16a51f567e54f7f3f22591e316 to your computer and use it in GitHub Desktop.
Save yuxuanzhuang/05b0da16a51f567e54f7f3f22591e316 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Thread limit test"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from contextlib import ContextDecorator \n",
"from threadpoolctl import threadpool_limits \n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class threadpool_limits_decorator(threadpool_limits, ContextDecorator):\n",
" def __enter__(self):\n",
" return self\n",
" def __exit__(self, *exc):\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def thread_test_1():\n",
" for i in range(100):\n",
" _ = np.dot(np.random.rand(10**3, 10**3),np.random.rand(10**3, 10**3))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"thread_test_1() # this will run with all threads"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def thread_test_2():\n",
" with threadpool_limits(1):\n",
" for i in range(100):\n",
" _ = np.dot(np.random.rand(10**3, 10**3),np.random.rand(10**3, 10**3))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"thread_test_2() # this will run with one threads"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"thread_test_1() # this will run with all threads"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"def thread_test_3():\n",
" for i in range(100):\n",
" _ = np.dot(np.random.rand(10**3, 10**3),np.random.rand(10**3, 10**3))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"thread_test_3 = threadpool_limits_decorator(1)(thread_test_3)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"thread_test_3() # this will run with one thread"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"thread_test_1() # this will run with one thread"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "gsoc",
"language": "python",
"name": "gsoc"
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment