MCPcopy Create free account
hub / github.com/Kaggle/docker-python / TestNumba

Class TestNumba

tests/test_numba.py:8–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6from common import gpu_test
7
8class TestNumba(unittest.TestCase):
9 def test_jit(self):
10 x = np.arange(100).reshape(10, 10)
11
12 @jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
13 def go_fast(a): # Function is compiled to machine code when called the first time
14 trace = 0.0
15 for i in range(a.shape[0]): # Numba likes loops
16 trace += np.tanh(a[i, i]) # Numba likes NumPy functions
17 return a + trace # Numba likes NumPy broadcasting
18
19 self.assertEqual(10, go_fast(x).shape[0])
20
21 @gpu_test
22 def test_cuda_jit(self):
23 from numba import cuda
24
25 x = np.arange(10)
26
27 @cuda.jit
28 def increment_by_one(an_array):
29 pos = cuda.grid(1)
30 if pos < an_array.size:
31 an_array[pos] += 1
32
33 threadsperblock = 32
34 blockspergrid = (x.size + (threadsperblock - 1))
35 self.assertEqual(0, x[0])
36 increment_by_one[blockspergrid, threadsperblock](x)
37 self.assertEqual(1, x[0])

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected