Skip to content
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

Merged
merged 1 commit into from Nov 1, 2018

Conversation

henryiii
Copy link
Contributor

@henryiii henryiii commented Nov 1, 2018

This is an attempt to address #30.

@astrofrog
Copy link
Owner

@henryiii - I don't understand the implications fully of doing this - in particular, is this safe given that we are modifying count and dataptr inside these blocks? Doesn't it mean that two threads could accidentally modify the same array?

Do you see any performance improvements with this?

@henryiii
Copy link
Contributor Author

henryiii commented Nov 1, 2018

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 fast_histogram is now "nice" in threading, allowing python to continue while it calculates. I'll test it in a little example and post that in a minute.

@henryiii
Copy link
Contributor Author

henryiii commented Nov 1, 2018

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)))
8.57 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%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)
4.14 ms ± 65.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

I believe, on an 24 core machine with 10M elements, this can be about 8x faster.

@astrofrog
Copy link
Owner

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.

@henryiii
Copy link
Contributor Author

henryiii commented Nov 1, 2018

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.

Copy link
Owner

@astrofrog astrofrog left a 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!

@astrofrog astrofrog merged commit e208d3d into astrofrog:master Nov 1, 2018
@henryiii henryiii deleted the release_gil branch November 1, 2018 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants