[SOLVED] numba did not speed up the compilation of code

Issue

I tried this code with numba as well as normal mode but both were completed in 13 seconds and numba did not add speed

How can I set numba for this situation?

import numpy as np
from numba import jit, cuda
a=[]
@jit(target_backend="cuda")
def func():
    for i in range(100000):
        a.append(i)
    return a

print(func())


 

Solution

CUDA cannot be used here because this code cannot run on the GPU (and even if would be modified, it would be inefficient). GPUs are very different from CPUs and they are also programmed differently. To understand why, please read the CUDA programming guide

If you just run the code and read the warnings from Numba, then you can see that the code fallback to a basic Python implementation:

Compilation is falling back to object mode WITH looplifting enabled because Function "func" failed type inference due to: Untyped global name ‘a’: Cannot type empty list

The reason is that the type of a is not provided and Numba fail to find it. Numba is fast because of its typing system which enable it to compile the code in a native binary.

Additionally, you should not modify global variables. This is not a good idea in term of software engineering and Numba does not support that anyway.

Thus, you need to use a typed lists returned from the function. Not that typed lists are not much faster than Python list when read/written from/to CPython because Numba has to make the conversion from/to CPython lists which is an expensive operation. Still, on my machine this is about 3 times faster.

Corrected code:

import numpy as np
from numba import jit, cuda

@jit
def func():
    a=[]
    for i in range(100000):
        a.append(i)
    return a

func() # Compile the function during the first run

a = func() # Execute quickly the code

print(a) # Printing is slow

For more information about the lazy compilation please read the Numba documentation.

Answered By – Jérôme Richard

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *