Skip to content

Commit 8843765

Browse files
committed
move memsys tests to test_nrt_refct
1 parent 8f454ac commit 8843765

File tree

2 files changed

+116
-109
lines changed

2 files changed

+116
-109
lines changed

numba_cuda/numba/cuda/tests/nrt/test_nrt.py

Lines changed: 1 addition & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,14 @@
11
import re
2-
import gc
32
import numpy as np
43
import unittest
54
from unittest.mock import patch
6-
from numba.cuda.runtime import rtsys
7-
from numba.tests.support import EnableNRTStatsMixin
85
from numba.cuda.testing import CUDATestCase
96

10-
from numba.cuda.tests.nrt.mock_numpy import cuda_empty, cuda_empty_like
7+
from numba.cuda.tests.nrt.mock_numpy import cuda_empty
118

129
from numba import cuda
1310

1411

15-
class TestNrtRefCt(EnableNRTStatsMixin, CUDATestCase):
16-
17-
def setUp(self):
18-
# Clean up any NRT-backed objects hanging in a dead reference cycle
19-
gc.collect()
20-
super(TestNrtRefCt, self).setUp()
21-
22-
def test_no_return(self):
23-
"""
24-
Test issue #1291
25-
"""
26-
n = 10
27-
28-
@cuda.jit(debug=True)
29-
def kernel():
30-
for i in range(n):
31-
temp = cuda_empty(2, np.float64) # noqa: F841
32-
return None
33-
34-
init_stats = rtsys.get_allocation_stats()
35-
print("init_stats", init_stats)
36-
37-
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
38-
kernel[1,1]()
39-
print("After kernel launch...")
40-
cur_stats = rtsys.get_allocation_stats()
41-
print("cur_stats", cur_stats)
42-
self.assertEqual(cur_stats.alloc - init_stats.alloc, n)
43-
self.assertEqual(cur_stats.free - init_stats.free, n)
44-
45-
def test_escaping_var_init_in_loop(self):
46-
"""
47-
Test issue #1297
48-
"""
49-
50-
@cuda.jit
51-
def g(n):
52-
53-
x = cuda_empty((n, 2), np.float64)
54-
55-
for i in range(n):
56-
y = x[i]
57-
58-
for i in range(n):
59-
y = x[i] # noqa: F841
60-
61-
return None
62-
63-
init_stats = rtsys.get_allocation_stats()
64-
print("init_stats", init_stats)
65-
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
66-
g[1, 1](10)
67-
print("After kernel launch...")
68-
cur_stats = rtsys.get_allocation_stats()
69-
print("cur_stats", cur_stats)
70-
self.assertEqual(cur_stats.alloc - init_stats.alloc, 1)
71-
self.assertEqual(cur_stats.free - init_stats.free, 1)
72-
73-
def test_invalid_computation_of_lifetime(self):
74-
"""
75-
Test issue #1573
76-
"""
77-
@cuda.jit
78-
def if_with_allocation_and_initialization(arr1, test1):
79-
tmp_arr = cuda_empty_like(arr1)
80-
81-
for i in range(tmp_arr.shape[0]):
82-
pass
83-
84-
if test1:
85-
cuda_empty_like(arr1)
86-
87-
arr = np.random.random((5, 5)) # the values are not consumed
88-
89-
init_stats = rtsys.get_allocation_stats()
90-
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
91-
if_with_allocation_and_initialization[1, 1](arr, False)
92-
cur_stats = rtsys.get_allocation_stats()
93-
self.assertEqual(cur_stats.alloc - init_stats.alloc,
94-
cur_stats.free - init_stats.free)
95-
96-
def test_del_at_beginning_of_loop(self):
97-
"""
98-
Test issue #1734
99-
"""
100-
@cuda.jit
101-
def f(arr):
102-
res = 0
103-
104-
for i in (0, 1):
105-
# `del t` is issued here before defining t. It must be
106-
# correctly handled by the lowering phase.
107-
t = arr[i]
108-
if t[i] > 1:
109-
res += t[i]
110-
111-
arr = np.ones((2, 2))
112-
init_stats = rtsys.get_allocation_stats()
113-
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
114-
f[1, 1](arr)
115-
cur_stats = rtsys.get_allocation_stats()
116-
self.assertEqual(cur_stats.alloc - init_stats.alloc,
117-
cur_stats.free - init_stats.free)
118-
119-
12012
class TestNrtBasic(CUDATestCase):
12113
def test_nrt_launches(self):
12214
@cuda.jit
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
import gc
3+
import numpy as np
4+
import unittest
5+
from unittest.mock import patch
6+
from numba.cuda.runtime import rtsys
7+
from numba.tests.support import EnableNRTStatsMixin
8+
from numba.cuda.testing import CUDATestCase
9+
10+
from numba.cuda.tests.nrt.mock_numpy import cuda_empty, cuda_empty_like
11+
12+
from numba import cuda
13+
14+
15+
class TestNrtRefCt(EnableNRTStatsMixin, CUDATestCase):
16+
17+
def setUp(self):
18+
# Clean up any NRT-backed objects hanging in a dead reference cycle
19+
gc.collect()
20+
super(TestNrtRefCt, self).setUp()
21+
22+
def test_no_return(self):
23+
"""
24+
Test issue #1291
25+
"""
26+
n = 10
27+
28+
@cuda.jit(debug=True)
29+
def kernel():
30+
for i in range(n):
31+
temp = cuda_empty(2, np.float64) # noqa: F841
32+
return None
33+
34+
init_stats = rtsys.get_allocation_stats()
35+
36+
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
37+
kernel[1,1]()
38+
cur_stats = rtsys.get_allocation_stats()
39+
self.assertEqual(cur_stats.alloc - init_stats.alloc, n)
40+
self.assertEqual(cur_stats.free - init_stats.free, n)
41+
42+
def test_escaping_var_init_in_loop(self):
43+
"""
44+
Test issue #1297
45+
"""
46+
47+
@cuda.jit
48+
def g(n):
49+
50+
x = cuda_empty((n, 2), np.float64)
51+
52+
for i in range(n):
53+
y = x[i]
54+
55+
for i in range(n):
56+
y = x[i] # noqa: F841
57+
58+
return None
59+
60+
init_stats = rtsys.get_allocation_stats()
61+
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
62+
g[1, 1](10)
63+
cur_stats = rtsys.get_allocation_stats()
64+
self.assertEqual(cur_stats.alloc - init_stats.alloc, 1)
65+
self.assertEqual(cur_stats.free - init_stats.free, 1)
66+
67+
def test_invalid_computation_of_lifetime(self):
68+
"""
69+
Test issue #1573
70+
"""
71+
@cuda.jit
72+
def if_with_allocation_and_initialization(arr1, test1):
73+
tmp_arr = cuda_empty_like(arr1)
74+
75+
for i in range(tmp_arr.shape[0]):
76+
pass
77+
78+
if test1:
79+
cuda_empty_like(arr1)
80+
81+
arr = np.random.random((5, 5)) # the values are not consumed
82+
83+
init_stats = rtsys.get_allocation_stats()
84+
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
85+
if_with_allocation_and_initialization[1, 1](arr, False)
86+
cur_stats = rtsys.get_allocation_stats()
87+
self.assertEqual(cur_stats.alloc - init_stats.alloc,
88+
cur_stats.free - init_stats.free)
89+
90+
def test_del_at_beginning_of_loop(self):
91+
"""
92+
Test issue #1734
93+
"""
94+
@cuda.jit
95+
def f(arr):
96+
res = 0
97+
98+
for i in (0, 1):
99+
# `del t` is issued here before defining t. It must be
100+
# correctly handled by the lowering phase.
101+
t = arr[i]
102+
if t[i] > 1:
103+
res += t[i]
104+
105+
arr = np.ones((2, 2))
106+
init_stats = rtsys.get_allocation_stats()
107+
with patch('numba.config.CUDA_ENABLE_NRT', True, create=True):
108+
f[1, 1](arr)
109+
cur_stats = rtsys.get_allocation_stats()
110+
self.assertEqual(cur_stats.alloc - init_stats.alloc,
111+
cur_stats.free - init_stats.free)
112+
113+
114+
if __name__ == '__main__':
115+
unittest.main()

0 commit comments

Comments
 (0)