|
1 | 1 | import re |
2 | | -import gc |
3 | 2 | import numpy as np |
4 | 3 | import unittest |
5 | 4 | from unittest.mock import patch |
6 | | -from numba.cuda.runtime import rtsys |
7 | | -from numba.tests.support import EnableNRTStatsMixin |
8 | 5 | from numba.cuda.testing import CUDATestCase |
9 | 6 |
|
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 |
11 | 8 |
|
12 | 9 | from numba import cuda |
13 | 10 |
|
14 | 11 |
|
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 | | - |
120 | 12 | class TestNrtBasic(CUDATestCase): |
121 | 13 | def test_nrt_launches(self): |
122 | 14 | @cuda.jit |
|
0 commit comments