New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop the gil during calculations #31
Conversation
@henryiii - I don't understand the implications fully of doing this - in particular, is this safe given that we are modifying Do you see any performance improvements with this? |
Releasing the GIL means Python can continue, and potentially change the variables users have access to. Since a user does not have access to count (you created the array it points at), and you are only reading from dataptr, it should be safe. For benefit, it means that |
Before the patch, the "split" version takes about the same amount of time as the normal version. This is on a dual-core processor with hyperthreading. Splits of 2 is about 4.8 ms. import fast_histogram
import numpy as np
from concurrent.futures import ThreadPoolExecutor
from functools import partial ranges=((-1,1),(-1,1)) vals = np.random.normal(size=[2, 1000000]).astype(np.float32) %%timeit
results = fast_histogram.histogram2d(*vals, bins=100, range=((-1,1),(-1,1)))
%%timeit
splits = 4
with ThreadPoolExecutor(max_workers=splits) as pool:
chunk = vals.shape[1] // splits
chunk0 = [vals[0,i*chunk:(i+1)*chunk] for i in range(splits)]
chunk1 = [vals[1,i*chunk:(i+1)*chunk] for i in range(splits)]
f = partial(fast_histogram.histogram2d, bins=100, range=((-1,1),(-1,1)))
results = pool.map(f, chunk0, chunk1)
results = sum(results)
I believe, on an 24 core machine with 10M elements, this can be about 8x faster. |
Thanks for the explanation! This makes sense. So just to check, does this mean one could potentially run into issues if modifying the input arrays while the calculation is running? I think this is probably an acceptable risk, but just wanted to check. |
Yes, you might grab the old or the new version of the array values. Since you are not modifying them, you can't create a problem though. Reading is not an issue in multithreading, only writing is. You still own a reference to the memory, so you can't have the array deleted from under you or anything like that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good - thanks for the explanations!
This is an attempt to address #30.