question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ufunc.at (and possibly other methods) slow

See original GitHub issue

I noticed that in many of my codes, seemingly harmless lines like

numpy.add.at(target, idx, vals)

take a large share of the runtime. I investigated and found that one gets a speed-up of a factor of 40 (!) by simply moving the critical code to C++.

f

(Not sure what’s actually done in numpy.ufunc.) I put this into a very simple module, https://github.com/nschloe/fastfunc, so feel free to take some code out of there.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jaimefriocommented, May 24, 2018

Here I explained what makes ufunc.at slow. Your code here takes a few shortcuts that are not really available to NumPy:

  • You have hardcoded the operations you have implemented, +, -, * and /, which is OK for most use cases, but does not support generic operations. If you had a generic function that took a function pointer that performed the operation, you would have more generic, albeit slower code. That is what NumPy does.

  • You have assumed that the a and b arrays are of the same type, which is not always the case. Not sure if your code can handle cases where these don’t match, but that’s a liberty NumPy can’t take either.

  • I’m not sure how much magic pybind11 packs, but your iteration scheme seems deceptively simple for all the subtleties of fancy indexing that ufunc.at supports. Are you assuming that some/all of the arrays are 1D? That’s again something NumPy isn’t free to do.

1reaction
eric-wiesercommented, May 24, 2018

It would be great if we could start using C++ template instead of the .c.src stuff within numpy, but I imagine there are distribution issues with that…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy multidimensional indexing for np.ufunc.at and np.ix_ ...
I would like to know how I can take index from an array and multiply with another array. I have two 4d arrays...
Read more >
Universal functions (ufunc) — NumPy v1.24 Manual
The Python function max() will find the maximum over a one-dimensional array, but it will do so using a slower sequence interface. The...
Read more >
Universal functions (ufunc) — NumPy v1.13 Manual
If an input has a dimension size of 1 in its shape, the first data entry in that dimension will be used for...
Read more >
Computation on NumPy Arrays: Universal Functions
Computation on NumPy arrays can be very fast, or it can be very slow. ... Another common type of operation available in a...
Read more >
Cloud statistics — How to EUREC⁴A
Here are some ideas which show how this can be done in a vectorized (i.e. numpy friendly) way. This chapter assumes that a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found