From 8e6542c86ccba0d1eae86d44ff43fff71ee22679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 19 May 2015 23:03:46 -0700 Subject: [PATCH] Do not acquire GIL for heap --- skimage/graph/heap.pxd | 14 +++++++------- skimage/graph/heap.pyx | 22 ++++++++++++---------- skimage/graph/tests/test_heap.py | 2 ++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/skimage/graph/heap.pxd b/skimage/graph/heap.pxd index e31aa150..71b8f2b0 100644 --- a/skimage/graph/heap.pxd +++ b/skimage/graph/heap.pxd @@ -19,13 +19,13 @@ cdef class BinaryHeap: cdef REFERENCE_T *_references cdef REFERENCE_T _popped_ref - cdef void _add_or_remove_level(self, LEVELS_T add_or_remove) - cdef void _update(self) - cdef void _update_one(self, INDEX_T i) - cdef void _remove(self, INDEX_T i) + cdef void _add_or_remove_level(self, LEVELS_T add_or_remove) nogil + cdef void _update(self) nogil + cdef void _update_one(self, INDEX_T i) nogil + cdef void _remove(self, INDEX_T i) nogil - cdef INDEX_T push_fast(self, VALUE_T value, REFERENCE_T reference) - cdef VALUE_T pop_fast(self) + cdef INDEX_T push_fast(self, VALUE_T value, REFERENCE_T reference) nogil + cdef VALUE_T pop_fast(self) nogil cdef class FastUpdateBinaryHeap(BinaryHeap): cdef readonly REFERENCE_T max_reference @@ -35,4 +35,4 @@ cdef class FastUpdateBinaryHeap(BinaryHeap): cdef VALUE_T value_of_fast(self, REFERENCE_T reference) cdef INDEX_T push_if_lower_fast(self, VALUE_T value, - REFERENCE_T reference) + REFERENCE_T reference) nogil diff --git a/skimage/graph/heap.pyx b/skimage/graph/heap.pyx index a4368595..4be48e46 100644 --- a/skimage/graph/heap.pyx +++ b/skimage/graph/heap.pyx @@ -43,7 +43,8 @@ cdef extern from "pyport.h": cdef VALUE_T inf = Py_HUGE_VAL # this is handy -cdef inline INDEX_T index_min(INDEX_T a, INDEX_T b): return a if a <= b else b +cdef inline INDEX_T index_min(INDEX_T a, INDEX_T b) nogil: + return a if a <= b else b cdef class BinaryHeap: @@ -185,7 +186,7 @@ cdef class BinaryHeap: ## C Maintanance methods - cdef void _add_or_remove_level(self, LEVELS_T add_or_remove): + cdef void _add_or_remove_level(self, LEVELS_T add_or_remove) nogil: # init indexing ints cdef INDEX_T i, i1, i2, n @@ -228,7 +229,7 @@ cdef class BinaryHeap: self._update() - cdef void _update(self): + cdef void _update(self) nogil: """Update the full tree from the bottom up. This should be done after resizing. """ @@ -251,7 +252,7 @@ cdef class BinaryHeap: values[ii] = values[i+1] - cdef void _update_one(self, INDEX_T i): + cdef void _update_one(self, INDEX_T i) nogil: """Update the tree for one value.""" # shorter name for values @@ -279,7 +280,7 @@ cdef class BinaryHeap: i = ii-1 - cdef void _remove(self, INDEX_T i1): + cdef void _remove(self, INDEX_T i1) nogil: """Remove a value from the heap. By index.""" cdef LEVELS_T levels = self.levels @@ -314,7 +315,7 @@ cdef class BinaryHeap: ## C Public methods - cdef INDEX_T push_fast(self, VALUE_T value, REFERENCE_T reference): + cdef INDEX_T push_fast(self, VALUE_T value, REFERENCE_T reference) nogil: """The c-method for fast pushing. Returns the index relative to the start of the last level in the heap.""" @@ -338,7 +339,7 @@ cdef class BinaryHeap: return count - cdef VALUE_T pop_fast(self): + cdef VALUE_T pop_fast(self) nogil: """The c-method for fast popping. Returns the minimum value. The reference is put in self._popped_ref""" @@ -543,7 +544,7 @@ cdef class FastUpdateBinaryHeap(BinaryHeap): self._crossref[i] = -1 - cdef void _remove(self, INDEX_T i1): + cdef void _remove(self, INDEX_T i1) nogil: """ Remove a value from the heap. By index. """ cdef LEVELS_T levels = self.levels cdef INDEX_T count = self.count @@ -581,7 +582,7 @@ cdef class FastUpdateBinaryHeap(BinaryHeap): self._update_one(i2) - cdef INDEX_T push_fast(self, VALUE_T value, REFERENCE_T reference): + cdef INDEX_T push_fast(self, VALUE_T value, REFERENCE_T reference) nogil: """The c method for fast pushing. If the reference is already present, will update its value, otherwise @@ -611,7 +612,8 @@ cdef class FastUpdateBinaryHeap(BinaryHeap): self._crossref[reference] = ir return ir - cdef INDEX_T push_if_lower_fast(self, VALUE_T value, REFERENCE_T reference): + cdef INDEX_T push_if_lower_fast(self, VALUE_T value, + REFERENCE_T reference) nogil: """If the reference is already present, will update its value ONLY if the new value is lower than the old one. If the reference is not present, this append it. If a value was appended, self._pushed is diff --git a/skimage/graph/tests/test_heap.py b/skimage/graph/tests/test_heap.py index cf7e3d0b..856b8e30 100644 --- a/skimage/graph/tests/test_heap.py +++ b/skimage/graph/tests/test_heap.py @@ -1,8 +1,10 @@ import time import random import skimage.graph.heap as heap +from skimage._shared.testing import test_parallel +@test_parallel() def test_heap(): _test_heap(100000, True) _test_heap(100000, False)