Giter Site home page Giter Site logo

Can't multiply two arrays? about numba HOT 7 CLOSED

numba avatar numba commented on July 22, 2024
Can't multiply two arrays?

from numba.

Comments (7)

teoliphant avatar teoliphant commented on July 22, 2024

It looks like you have 1-d arrays in your examples, but your jit decorator is indicating 2-d arrays (with [:,:]).

Try changing your decorator to:

cmult2arr = autojit(mult2arr)

-Travis

On Dec 21, 2012, at 9:10 AM, mattbellis wrote:

Hi numba developers. I only just learned about this project and it looks extremely interesting. I got everything to build fine and the examples work great. I then went to write my own test to see if there is any speed-up with multiplying two arrays in numba vs. in numpy.

However, I get an error that suggests that multiplication of doubles is not yet supported? I just wanted to make sure I'm interpreting this correctly.

I'm not sure if this is really an ``Issue", but I wasn't sure where else to post . Thanks!

--- snip ---
numba.error.NumbaError: 23:20: Binary operations mul on values typed double[:] and double[:] not (yet) supported
--- snip ---

Here's my code, inspired by the current examples.

--- snip ---

from numpy import random,zeros
from numba import double
from numba.decorators import jit as jit

import time

####################################################################
def mult2arr_np(x,y):

result = x*y

return result
################################################################################

####################################################################
def mult2arr(x,y):

ix = len(x)
jy = len(y)
result = zeros(ix)
for i in range(ix):
result[i] = x[i]*y[i]

return result
################################################################################

cmult2arr = jit(restype=double[:,:], argtypes=[double[:,:],double[:,:]])(mult2arr)

x = random.random(100000)
y = random.random(100000)

################################################################################

start = time.time()
res = mult2arr(x,y)
duration = time.time() - start
print "Result from python is %s in %s (msec)" % (res, duration*1000)

################################################################################

start = time.time()
res = cmult2arr(x,y)
duration2 = time.time() - start
print "Result from compiled is %s in %s (msec)" % (res, duration2*1000)

print "Speed up is %s" % (duration / duration2)

################################################################################

start = time.time()
res = mult2arr_np(x,y)
duration3 = time.time() - start
print "Result from numpy is %s in %s (msec)" % (res, duration3*1000)

################################################################################


Reply to this email directly or view it on GitHub.

from numba.

mattbellis avatar mattbellis commented on July 22, 2024

Ah, perfect! Thanks!

Obviously, this was not a bug...merely my ignorance. Where is the right place to post questions of this sort?

from numba.

teoliphant avatar teoliphant commented on July 22, 2024

Oh.

One more thing. Rather than import zeros from numpy like you have done, you should do:

import numpy
numpy.zeros(..)

or

import numpy as np
np.zeros(...)

The type-inference only currently works if you use these name-spaces.

If you find your code slower than you expect (i.e. only about 5-6 times faster than Python rather than closer to 100x faster), then try this...

-Travis

On Dec 21, 2012, at 9:10 AM, mattbellis wrote:

Hi numba developers. I only just learned about this project and it looks extremely interesting. I got everything to build fine and the examples work great. I then went to write my own test to see if there is any speed-up with multiplying two arrays in numba vs. in numpy.

However, I get an error that suggests that multiplication of doubles is not yet supported? I just wanted to make sure I'm interpreting this correctly.

I'm not sure if this is really an ``Issue", but I wasn't sure where else to post . Thanks!

--- snip ---
numba.error.NumbaError: 23:20: Binary operations mul on values typed double[:] and double[:] not (yet) supported
--- snip ---

Here's my code, inspired by the current examples.

--- snip ---

from numpy import random,zeros
from numba import double
from numba.decorators import jit as jit

import time

####################################################################
def mult2arr_np(x,y):

result = x*y

return result
################################################################################

####################################################################
def mult2arr(x,y):

ix = len(x)
jy = len(y)
result = zeros(ix)
for i in range(ix):
result[i] = x[i]*y[i]

return result
################################################################################

cmult2arr = jit(restype=double[:,:], argtypes=[double[:,:],double[:,:]])(mult2arr)

x = random.random(100000)
y = random.random(100000)

################################################################################

start = time.time()
res = mult2arr(x,y)
duration = time.time() - start
print "Result from python is %s in %s (msec)" % (res, duration*1000)

################################################################################

start = time.time()
res = cmult2arr(x,y)
duration2 = time.time() - start
print "Result from compiled is %s in %s (msec)" % (res, duration2*1000)

print "Speed up is %s" % (duration / duration2)

################################################################################

start = time.time()
res = mult2arr_np(x,y)
duration3 = time.time() - start
print "Result from numpy is %s in %s (msec)" % (res, duration3*1000)

################################################################################


Reply to this email directly or view it on GitHub.

from numba.

mattbellis avatar mattbellis commented on July 22, 2024

Hi Travis,

Thanks for the suggestions. Using numba is faster than python, but only about 4x faster than regular python, even with those changes. I'm trying this out with 1M events in the two arrays that I am multiplying. Numpy is about 100x faster than the regular python. You can see the code here:

http://code.google.com/p/matts-work-environment/source/browse/python/numba/hello_world/hello_world0.py

When I run this, the output is.

Result from python is [ 0.12852594 0.17960238 0.23382419]
in 418.634176254 (msec)
Result from compiled is [ 0.12852594 0.17960238 0.23382419]
in 103.728055954 (msec)
Result from numpy is [ 0.12852594 0.17960238 0.23382419]
in 4.23502922058 (msec)

 This isn't pressing for my work or anything, so don't let this distract you from your work on this project. I'm just trying to learn more about it and provide some feedback.  :)    I'm probably just not using it properly. 

Matt

from numba.

markflorisson avatar markflorisson commented on July 22, 2024

You are using autojit, which means it will be compiled when you call it, unless it has a cached version ready. If you use the 'jit' version, it should be significantly better. I'm getting:

Result from python is [ 0.07991542 0.43555985 0.30507656]
in 804.233789444 (msec)
Result from compiled is [ 0.07991542 0.43555985 0.30507656]
in 7.45105743408 (msec)
Result from numpy is [ 0.07991542 0.43555985 0.30507656]
in 2.65884399414 (msec)

So it's still slower, but within a reasonable factor, maybe because it's not vectorized.

from numba.

mattbellis avatar mattbellis commented on July 22, 2024

Ah, ok. I've changed...

cmult2arr = autojit(mult2arr)

to

cmult2arr = jit(restype=double[:], argtypes=[double[:],double[:]])(mult2arr)

I get not quite the numbers you see, but still, significantly better.

Result from python is [ 0.54940225 0.17901628 0.11145486]
in 438.78698349 (msec)
Result from compiled is [ 0.54940225 0.17901628 0.11145486]
in 84.3341350555 (msec)
Result from numpy is [ 0.54940225 0.17901628 0.11145486]
in 3.68285179138 (msec)

I think I understand this all a little better now. For my own purposes, I'm using numpy arrays/functions for much of my work, so I'm getting a pretty big speed-up there. I'll keep following numba and I'm sure in the near future, I'll be using it. Thanks for all the feedback!

from numba.

jriehl avatar jriehl commented on July 22, 2024

Looks like this has been resolved. Closing.

from numba.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.