Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using scipy.weave.inline

I am using several techniques (NumPy, Weave, Cython, Numba) to perform a Python performance benchmark. The code takes two numpy arrays of size NxN and multiplies them element-wise and stores the values in another array C.

My weave.inline() code gives me a scipy.weave.build_tools.CompileError. I have created a minimalist piece of code which generates the same error. Could someone please help?

import time

import numpy as np
from scipy import weave
from scipy.weave import converters


def benchmark():

    N = np.array(5000, dtype=np.int)

    A = np.random.rand(N, N)
    B = np.random.rand(N, N)
    C = np.zeros([N, N], dtype=float)

    t = time.clock()
    weave_inline_loop(A, B, C, N)
    print time.clock() - t


def weave_inline_loop(A, B, C, N):
    code = """
           int i, j;
           for (i = 0; i < N; ++i)
           {
               for (j = 0; j < N; ++j)
               {
                   C(i, j) = A(i, j) * B(i, j);
               }
           }
           return_val = C;
           """
    C = weave.inline(code, ['A', 'B', 'C', 'N'], type_converters=converters.blitz, compiler='gcc')

benchmark()
like image 715
Aeronaelius Avatar asked Dec 14 '25 05:12

Aeronaelius


2 Answers

Three small changes are needed:

  • N can't be a 0D-numpy array (it has to be an integer so that i < N works in the C code). You should write N = 5000 instead of N = np.array(5000, dtype=np.int).

  • The C array is being modified in-place so it doesn't have to be returned. I don't know the restrictions on the kind of objects than return_val can handle, but if you try to keep return_val = C; it fails compiling: don't know how to convert ‘blitz::Array<double, 2>’ to ‘const py::object&’.

  • After that, weave.inline returns None. Keeping the assignment C = weave.inline(... makes the code look confusing, even if it works fine and the array named C will hold the result in the benchmark scope.

This is the end result:

import time
import numpy as np
from scipy import weave
from scipy.weave import converters


def benchmark():
    N = 5000

    A = np.random.rand(N, N)
    B = np.random.rand(N, N)
    C = np.zeros([N, N], dtype=float)

    t = time.clock()
    weave_inline_loop(A, B, C, N)
    print time.clock() - t


def weave_inline_loop(A, B, C, N):
    code = """
           int i, j;
           for (i = 0; i < N; ++i)
           {
               for (j = 0; j < N; ++j)
               {
                   C(i, j) = A(i, j) * B(i, j);
               }
           }
           """
    weave.inline(code, ['A', 'B', 'C', 'N'], type_converters=converters.blitz, compiler='gcc')
like image 188
jorgeca Avatar answered Dec 15 '25 18:12

jorgeca


Two issues. First, you don't need the line return_val = C. You are directly manipulating the data in the variable C in your inlined code, so its already available to python and there's no need to explicitly return it to the environment (and trying to do so is causing errors when trying to do the appropriate type conversions). So change your function to:

def weave_inline_loop(A, B, C, N):
    code = """
           int i, j;
           for (i = 0; i < N; ++i)
           {
               for (j = 0; j < N; ++j)
               {
                   C(i, j) = A(i, j) * B(i, j);
               }
           }
           """
    weave.inline(code, ['A', 'B', 'C', 'N'], type_converters=converters.blitz, compiler='gcc')
    return C

Second issue. You are comparing i and j (both ints), to N an array of length 1. This also generated an error. But if you call your code as:

def benchmark():

    N = np.array(5000, dtype=np.int)

    A = np.random.rand(N, N)
    B = np.random.rand(N, N)
    C = np.zeros([N, N], dtype=float)

    t = time.clock()
    print weave_inline_loop(A, B, C, int(N)) 
    # I added a print statement so you can see that C is being 
    # populated with the new 2d array
    print time.clock() - t
like image 40
dr jimbob Avatar answered Dec 15 '25 19:12

dr jimbob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!