samedi 29 septembre 2018

How to use numba.cuda.jit to generate random numbers?

Below are my first attempts to use Python Numba for CUDA.

import numba
import random

@numba.jit
def test_jit():
    total = 0
    for i in range(10**6):
        total += random.gauss(0, 1)
    return total

print('jit', test_jit())

@numba.cuda.jit
def test_cuda():
    total = 0
    for i in range(10**6):
        total += random.gauss(0, 1)
    return total

print('cuda', test_cuda())

Running the above yields

jit -1358.6125717954903
[...]
TypingError: Failed at nopython (nopython frontend)
Unknown attribute 'gauss' of type Module(<module 'random' from 'D:\\Anaconda3\\lib\\random.py'>)
[...]

While using jit on my Gaussian Monte Carlo sum test works fine, cuda.jit does not understand random.gauss resulting in an error message. Using 'from random import gauss' and calling 'gauss(0, 1)' without 'random.' instead yields:

[...]
TypingError: Failed at nopython (nopython frontend)
Untyped global name 'gauss': cannot determine Numba type of <class 'method'>
[...]

How to fix this? Is there maybe some CUDA random number generator that couldbe used instead (maybe involving Cython)?




Aucun commentaire:

Enregistrer un commentaire