Edy Bourne
Edy Bourne

Reputation: 6187

In Numba, how can I invoke a recursive function running on the GPU?

According to the docs, calling a recursive function seems supported. But I try it out with a simple example, it fails:

@cuda.jit
def call_recursive(out):
    out[0] = recursive(0)


@cuda.jit(types.int64(types.int64), device=True)
def recursive(val):
    if val == 10:
        return val
    else:
        return recursive(val + 1)

# copy an array into the device where we will store the output
data = np.zeros(1)
d_data = cuda.to_device(data)

# invoking kernel with one block and thread, just for testing
call_recursive[1,1](d_data)

# get the data back
h_data = cuda.to_host(d_data)

print(h_data[0])

In this case all I do is call a function that calls a recursive function. That one calls itself 10 times and then returns a number, that is stored in the given array and returned to the host.

I was expecting the host to receive the populated array and print 10. Instead, I see this error:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
NameError: name 'recursive' is not defined

I'm on Python 3.7 and Numba 0.50.1, the latest.

Any help is greatly appreciated!

Upvotes: 3

Views: 337

Answers (1)

Edy Bourne
Edy Bourne

Reputation: 6187

Ah, just found out it apparently is not supported. The doc I was reading was a proposal for enhancement - not actual documentation.

Gotta simulate recursion using an iteration and a stack or an array to keep state.

Well, hope this helps someone.

Upvotes: 4

Related Questions