Creating `numpy` arrays in nopython mode?

0 views
Skip to first unread message

Brian Merchant

unread,
May 24, 2015, 2:54:49 PM5/24/15
to numba...@continuum.io
So, I know that it isn't possible to use `numpy` functions like `numpy.zeros` in nopython decorated functions. Otherwise, I will get errors like:

 UntypedAttributeError: Unknown attribute "zeros" of type Module(<module 'numpy' from 'A:\Anaconda\lib\site-packages\numpy\__init__.pyc'>)

If I need to create arrays while in a nopython function, how *should* I go about it? Or, is the best way to pass empty result arrays as arguments to the function, which will then be filled in? I am looking for "best practice" type advice.

Victor Gabriel

unread,
May 24, 2015, 7:10:27 PM5/24/15
to numba...@continuum.io
Just pass your zeros array as a function parameter

N=100
 output = numpy.zeros(N)

@njit
function(data, output):
....





--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/670635b2-0afb-4ea0-a1ef-188bfb7c5b3e%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.



--
Dr. Victor Gabriel Leandro Alves, D. Sc.
Clinical Medical Physicist - Quality Audits
Instituto Nacional de Câncer - Brazil   

Victor Gabriel

unread,
May 24, 2015, 7:15:15 PM5/24/15
to numba...@continuum.io
I think it is the "best practice" solution. :)

Nuno Calaim

unread,
May 25, 2015, 6:19:29 AM5/25/15
to numba...@continuum.io
Isn't the new version of numba coming out soon able to create numpy arrays in nopython mode?

I think I heard something on those lines

nuno

Stanley Seibert

unread,
May 25, 2015, 12:13:29 PM5/25/15
to Numba Public Discussion - Public
Hi Brian,

You ask your question at an interesting time.  :)    The short answer is: Wait a week, and array creation will be supported in nopython mode in upcoming Numba version 0.19:

https://groups.google.com/a/continuum.io/d/msg/numba-users/hcqaxHoTRXY/EJnjmNSsL7IJ

Prior to that, the best practices are either:
  • Compile your function in object mode (the automatic fallback if you are using @jit) and rely on loop-lifting to extract and compile loops in your function in nopython mode.  This actually works better than most people imagine, and is generically useful when you have performance-critical looping code that is sandwiched between calls to uncompilable functions.
  • Move array allocation outside the function and pass the desired array in.

But really, you should go try out the beta release of Numba described in that post I link to above, which directly supports the usage you want.


--

Victor Gabriel

unread,
May 25, 2015, 12:19:16 PM5/25/15
to numba...@continuum.io
"array creation will be supported in nopython mode in upcoming Numba version 0.19"

Wow! Nice! I really need functions with less arguments, code refactoring wants me!



Hernan Grecco

unread,
May 25, 2015, 3:50:33 PM5/25/15
to numba...@continuum.io
HI,

On Mon, May 25, 2015 at 1:19 PM Victor Gabriel <victo...@gmail.com> wrote:
"array creation will be supported in nopython mode in upcoming Numba version 0.19"

This is great news! Together with the improvements in 0.18 are a big step towards making easier to jit existing code in nopython mode.

Still I have found that having an `out` parameter (like many numpy functions) is very convenient, in particular for functions called inside loops as it avoids reallocating memory (I think this cannot be optimized in other ways).

What will be very cool with 0.19 is that if out is not provided, then we could just return a new array like numpy does.

cheers,

Hernán
 

Brian Merchant

unread,
May 25, 2015, 6:13:04 PM5/25/15
to numba...@continuum.io
Hi Stanley,

One word: awesome!

Kind regards,
Brian



--
You received this message because you are subscribed to a topic in the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/numba-users/GwqW0qPQFRc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to numba-users...@continuum.io.

To post to this group, send email to numba...@continuum.io.
Reply all
Reply to author
Forward
0 new messages