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

probvec: Use guvectorize with target='parallel' #253

Merged
merged 4 commits into from
Jul 6, 2016
Merged

Conversation

oyamad
Copy link
Member

@oyamad oyamad commented Apr 17, 2016

This is to modify probvec to use guvectorize with target='parallel'.
(probvec is mainly used to generate samples of MarkovChain or DiscreteDP.)

  • Use of guvectorize simplifies the logic slightly; we don't need to explicitly write the outer loop.
  • This version puts sort() in a jitted loop with nopython mode, the bottleneck of the current probvec.
  • Sorting by Numba may be slower than Numpy, but with target='parallel' this version is faster than the current version on master for cases with large size, whereas it seems slower with small size (probably due to overhead).

Timing on a machine with 4 cores:

master:

In [1]: import quantecon as qe

In [2]: seed = 1234

In [3]: m, k = 5000, 5000

In [4]: %timeit qe.random.probvec(m, k, random_state=seed)
1 loop, best of 3: 1.63 s per loop

In [5]: beta = 0.95

In [6]: num_states, num_actions = 500, 100

In [7]: %timeit qe.markov.random_discrete_dp(num_states, num_actions, beta, random_state=seed)
1 loop, best of 3: 1.45 s per loop

This version:

In [1]: import quantecon as qe

In [2]: seed = 1234

In [3]: m, k = 5000, 5000

In [4]: %timeit qe.random.probvec(m, k, random_state=seed)
1 loop, best of 3: 681 ms per loop

In [5]: beta = 0.95

In [6]: num_states, num_actions = 500, 100

In [7]: %timeit qe.markov.random_discrete_dp(num_states, num_actions, beta, random_state=seed)
1 loop, best of 3: 747 ms per loop

This is the first case in quantecon where guvectorize is used; we might discuss its pros and cons here.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.003%) to 86.693% when pulling 102fc7a on probvec-guvectorize into a8ff846 on master.

@sglyon
Copy link
Member

sglyon commented Apr 18, 2016

I'm not opposed to using guvectorize at any point in the library, but I don't love this particular change. The reason is that if I was using these tools within a larger algorithm, I would almost certainly want to parallelize a different section of my code. Having this routine always run in parallel might make that difficult.

@oyamad
Copy link
Member Author

oyamad commented Apr 19, 2016

@spencerlyon2 We shouldn't accept this PR if it can interfere anything more important; probvec is just used mainly (or only) for benchmark testing.

But can you elaborate on your point? Do you have in mind any case where you may use this particular routine within a larger algorithm?

@mmcky
Copy link
Contributor

mmcky commented Jun 20, 2016

Hi @oyamad and @spencerlyon2. Where are we up to with this PR? Should we add the guvectorize / jit as an option to a top level function (with default to jit?)

@sglyon
Copy link
Member

sglyon commented Jun 20, 2016

I thought about it more. I vote that we accept this.

It would be great it numba allowed us to use guvectorize and then select the execution target at runtime instead of compile time, but I don't think that is possible.

@oyamad
Copy link
Member Author

oyamad commented Jun 27, 2016

We could possibly do:

def probvec(m, k, random_state=None, parallel=True):
    ...
    if parallel:
        _probvec_parallel(r, x)
    else:
        _probvec_cpu(r, x)


def _probvec(r, out):
    ...

_probvec_parallel =
    guvectorize(['(f8[:], f8[:])'], '(n), (k)', nopython=True, target='parallel')(_probvec)

_probvec_cpu =
    guvectorize(['(f8[:], f8[:])'], '(n), (k)', nopython=True, target='cpu')(_probvec)

@mmcky
Copy link
Contributor

mmcky commented Jun 27, 2016

@spencerlyon2 Are you still concerned with:

Having this routine always run in parallel might make that difficult.

We can implement options through a wrapper (as per @oyamad solution) or make parallel the default.

@sglyon
Copy link
Member

sglyon commented Jun 27, 2016

I think the proposed solution sounds great. Let's go with that!
On Jun 27, 2016 5:09 AM, "mmcky" [email protected] wrote:

@spencerlyon2 https://github.com/spencerlyon2 Are you still concerned
with:

Having this routine always run in parallel might make that difficult.

We can implement options through a wrapper (as per @oyamad
https://github.com/oyamad solution) or make parallel the default.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#253 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AA9J3Ic1HS7bFzQg_-JSLYoWuKiFB9fXks5qP-d_gaJpZM4IJLXu
.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.1%) to 86.591% when pulling 0fcdf56 on probvec-guvectorize into a8ff846 on master.

@mmcky
Copy link
Contributor

mmcky commented Jul 5, 2016

@oyamad please let me know if you are happy with this update then we can merge this into the master branch. Thanks.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 86.645% when pulling d1d87e6 on probvec-guvectorize into a8ff846 on master.

@oyamad
Copy link
Member Author

oyamad commented Jul 6, 2016

@mmcky Thanks, please do merge. (I added some docstring and tests.)

@mmcky
Copy link
Contributor

mmcky commented Jul 6, 2016

Thanks @oyamad! Merging now.

@mmcky mmcky merged commit 3b48f13 into master Jul 6, 2016
@mmcky mmcky deleted the probvec-guvectorize branch July 6, 2016 16:18
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.

4 participants