Skip to content

Instantly share code, notes, and snippets.

@geggo
Last active February 7, 2019 09:47
Show Gist options
  • Save geggo/d9b4016ff5b77dab262be7b30a98d862 to your computer and use it in GitHub Desktop.
Save geggo/d9b4016ff5b77dab262be7b30a98d862 to your computer and use it in GitHub Desktop.
import gc
import numpy as np
import pyopencl as cl
import pyopencl.array as cla
import threading
import sys
import time
ctx = cl.create_some_context(answers=['0', '1'])
queue = cl.CommandQueue(ctx)
def create_nanny(n=1):
# creates NannyEvent caught in reference cycle
for i in range(n):
acl = cla.zeros(queue, shape=(8,1), dtype=np.float32)
a = acl.get()
evt = cl.enqueue_copy(queue, acl.data, a)
l = [evt, a]
l.append(l)
def create_nanny_nanny(n1=10, n2=100, name='nannynanny'):
for i1 in range(n2):
create_nanny(n1)
print (name, i1)
sys.stdout.flush()
#gc.set_debug(gc.DEBUG_STATS)
gc.set_threshold(5)
T1 = threading.Thread(target=create_nanny_nanny, args=(10,1000, 'thread 1'))
T2 = threading.Thread(target=create_nanny_nanny, args=(10,1000, 'thread 2'))
T1.start()
T2.start()
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import gc\n",
"import numpy as np\n",
"import pyopencl as cl\n",
"import pyopencl.array as cla\n",
"import threading"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pyopencl.Context at 0x7f8ad9eb35c0 on <pyopencl.Device 'Iris Pro' on 'Apple' at 0x1024500>>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ctx = cl.create_some_context(answers=['0', '1'])\n",
"queue = cl.CommandQueue(ctx)\n",
"ctx"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pyopencl._cl.NannyEvent at 0x119f11678>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#create Nanny Event\n",
"acl = cla.zeros(queue, (8,1), np.float32)\n",
"a = acl.get()\n",
"evt = cl.enqueue_copy(queue, acl.data, a)\n",
"evt"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def create_nanny(n=1):\n",
" # creates NannyEvent caught in reference cycle\n",
" for i in range(n):\n",
" acl = cla.zeros(queue, shape=(8,1), dtype=np.float32)\n",
" a = acl.get()\n",
" evt = cl.enqueue_copy(queue, acl.data, a)\n",
" l = [evt, a]\n",
" l.append(l)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"gc: collectable <pyopencl._cl.NannyEvent 0x11a02c8e0>\n",
"gc: collectable <list 0x11a2acc48>\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gc.collect()\n",
"gc.set_debug(gc.DEBUG_COLLECTABLE)\n",
"create_nanny(1)\n",
"gc.collect()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# crash with message:\n",
"# libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: pybind11_object_dealloc(): Tried to deallocate unregistered instance!\n",
"\n",
"gc.set_debug(gc.DEBUG_STATS)\n",
"gc.set_threshold(10)\n",
"T = threading.Thread(target=create_nanny, args=(1000,))\n",
"T.run()"
]
},
{
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
import gc
import numpy as np
import pyopencl as cl
import pyopencl.array as cla
import threading
import sys
import time
ctx = cl.create_some_context(answers=['0', '1'])
queue = cl.CommandQueue(ctx)
def create_nanny(n=1):
# creates NannyEvent caught in reference cycle
for i in range(n):
acl = cla.zeros(queue, shape=(8,1), dtype=np.float32)
a = acl.get()
evt = cl.enqueue_copy(queue, acl.data, a)
l = [evt, a]
l.append(l)
def create_nanny_nanny(n1=10, n2=100, name='nannynanny'):
for i1 in range(n2):
create_nanny(n1)
print (name, i1)
sys.stdout.flush()
#gc.set_debug(gc.DEBUG_STATS)
gc.set_threshold(5)
T1 = threading.Thread(target=create_nanny_nanny, args=(10,1000, 'thread 1'))
T2 = threading.Thread(target=create_nanny_nanny, args=(10,1000, 'thread 2'))
T1.start()
T2.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment