def test_no_return(self): """ Test issue #1291 """ @njit def foo(n): for i in range(n): temp = np.zeros(2) return 0 n = 10 init_stats = rtsys.get_allocation_stats() foo(n) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, n) self.assertEqual(cur_stats.free - init_stats.free, n)
def assertNoNRTLeak(self): """ A context manager that asserts no NRT leak was created during the execution of the enclosed block. """ old = rtsys.get_allocation_stats() yield new = rtsys.get_allocation_stats() total_alloc = new.alloc - old.alloc total_free = new.free - old.free total_mi_alloc = new.mi_alloc - old.mi_alloc total_mi_free = new.mi_free - old.mi_free self.assertEqual(total_alloc, total_free, "number of data allocs != number of data frees") self.assertEqual(total_mi_alloc, total_mi_free, "number of meminfo allocs != number of meminfo frees")
def assertNoNRTLeak(self): """ A context manager that asserts no NRT leak was created during the execution of the enclosed block. """ old = rtsys.get_allocation_stats() yield new = rtsys.get_allocation_stats() total_alloc = new.alloc - old.alloc total_free = new.free - old.free total_mi_alloc = new.mi_alloc - old.mi_alloc total_mi_free = new.mi_free - old.mi_free self.assertEqual(total_alloc, total_free, "number of data allocs != number of data frees") self.assertEqual(total_mi_alloc, total_mi_free, "number of meminfo allocs != number of meminfo frees")
def test_no_return(self): """ Test issue #1291 """ @njit def foo(n): for i in range(n): temp = np.zeros(2) return 0 n = 10 init_stats = rtsys.get_allocation_stats() foo(n) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, n) self.assertEqual(cur_stats.free - init_stats.free, n)
def assert_no_memory_leak(self): old = self.__init_stats new = rtsys.get_allocation_stats() total_alloc = new.alloc - old.alloc total_free = new.free - old.free total_mi_alloc = new.mi_alloc - old.mi_alloc total_mi_free = new.mi_free - old.mi_free self.assertEqual(total_alloc, total_free) self.assertEqual(total_mi_alloc, total_mi_free)
def assert_no_memory_leak(self): old = self.__init_stats new = rtsys.get_allocation_stats() total_alloc = new.alloc - old.alloc total_free = new.free - old.free total_mi_alloc = new.mi_alloc - old.mi_alloc total_mi_free = new.mi_free - old.mi_free self.assertEqual(total_alloc, total_free) self.assertEqual(total_mi_alloc, total_mi_free)
def memory_leak_teardown(self): if self.__enable_leak_check: old = self.__init_stats new = rtsys.get_allocation_stats() total_alloc = new.alloc - old.alloc total_free = new.free - old.free total_mi_alloc = new.mi_alloc - old.mi_alloc total_mi_free = new.mi_free - old.mi_free self.assertEqual(total_alloc, total_free) self.assertEqual(total_mi_alloc, total_mi_free)
def memory_leak_teardown(self): if self.__enable_leak_check: old = self.__init_stats new = rtsys.get_allocation_stats() total_alloc = new.alloc - old.alloc total_free = new.free - old.free total_mi_alloc = new.mi_alloc - old.mi_alloc total_mi_free = new.mi_free - old.mi_free self.assertEqual(total_alloc, total_free) self.assertEqual(total_mi_alloc, total_mi_free)
def test_escaping_var_init_in_loop(self): """ Test issue #1297 """ @njit def g(n): x = np.zeros((n, 2)) for i in range(n): y = x[i] for i in range(n): y = x[i] return 0 init_stats = rtsys.get_allocation_stats() g(10) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, 1) self.assertEqual(cur_stats.free - init_stats.free, 1)
def test_invalid_computation_of_lifetime(self): """ Test issue #1573 """ @njit def if_with_allocation_and_initialization(arr1, test1): tmp_arr = np.zeros_like(arr1) for i in range(tmp_arr.shape[0]): pass if test1: np.zeros_like(arr1) return tmp_arr arr = np.random.random((5, 5)) # the values are not consumed init_stats = rtsys.get_allocation_stats() if_with_allocation_and_initialization(arr, False) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, cur_stats.free - init_stats.free)
def test_escaping_var_init_in_loop(self): """ Test issue #1297 """ @njit def g(n): x = np.zeros((n, 2)) for i in range(n): y = x[i] for i in range(n): y = x[i] return 0 init_stats = rtsys.get_allocation_stats() g(10) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, 1) self.assertEqual(cur_stats.free - init_stats.free, 1)
def test_del_at_beginning_of_loop(self): """ Test issue #1734 """ @njit def f(arr): res = 0 for i in (0, 1): # `del t` is issued here before defining t. It must be # correctly handled by the lowering phase. t = arr[i] if t[i] > 1: res += t[i] return res arr = np.ones((2, 2)) init_stats = rtsys.get_allocation_stats() f(arr) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, cur_stats.free - init_stats.free)
def test_invalid_computation_of_lifetime(self): """ Test issue #1573 """ @njit def if_with_allocation_and_initialization(arr1, test1): tmp_arr = np.zeros_like(arr1) for i in range(tmp_arr.shape[0]): pass if test1: np.zeros_like(arr1) return tmp_arr arr = np.random.random((5, 5)) # the values are not consumed init_stats = rtsys.get_allocation_stats() if_with_allocation_and_initialization(arr, False) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, cur_stats.free - init_stats.free)
def test_del_at_beginning_of_loop(self): """ Test issue #1734 """ @njit def f(arr): res = 0 for i in (0, 1): # `del t` is issued here before defining t. It must be # correctly handled by the lowering phase. t = arr[i] if t[i] > 1: res += t[i] return res arr = np.ones((2, 2)) init_stats = rtsys.get_allocation_stats() f(arr) cur_stats = rtsys.get_allocation_stats() self.assertEqual(cur_stats.alloc - init_stats.alloc, cur_stats.free - init_stats.free)
def test_dmc_est_sampling(): """Testing the DMC sampling to evaluate several estimators.""" exec_logger.info('Init sampling...') # TODO: Improve this test. exec_logger.info('Init VMC sampling...') vmc_chain_data = vmc_sampling.as_chain(num_steps, vmc_ini_state) sys_conf_set = vmc_chain_data.confs ar_ = vmc_chain_data.accept_rate print(f"Acceptance ratio: {ar_:.5g}") exec_logger.info('Finished sampling...') ini_sys_conf_set = sys_conf_set[-128:] dmc_ini_state = dmc_sampling.build_state(ini_sys_conf_set, ini_ref_energy) num_modes = 2 * BOSON_NUMBER ssf_est_spec = mrbp_qmc.dmc.SSFEstSpec(num_modes) dmc_ssf_sampling = attr.evolve(dmc_sampling, ssf_est_spec=ssf_est_spec) dmc_es_blocks = dmc_ssf_sampling.blocks(dmc_ini_state, num_time_steps_block, burn_in_blocks) es_blocks: dmc_base.T_SBlocksIter = \ islice(dmc_es_blocks, num_blocks) exec_logger.info('Init DMC sampling...') for block in es_blocks: state_props = block.iter_props nw_iter = state_props.num_walkers iter_ssf = block.iter_ssf # print(state_props) ssf_block_data = iter_ssf / nw_iter[:, np.newaxis, np.newaxis] print(ssf_block_data) # print(nw_iter) # This helps to catch memory leaks in numba compiled functions. print(rtsys.get_allocation_stats()) print('---') exec_logger.info('Finish DMC sampling.')
def memory_leak_setup(self): # Clean up any NRT-backed objects hanging in a dead reference cycle gc.collect() self.__init_stats = rtsys.get_allocation_stats()
def memory_leak_setup(self): self.__init_stats = rtsys.get_allocation_stats()
def memory_leak_setup(self): # Clean up any NRT-backed objects hanging in a dead reference cycle gc.collect() self.__init_stats = rtsys.get_allocation_stats()
""" Fills the array with n, n - 1, n - 2 and so on First we populate a linked list with values 1 ... n Then, we traverse the the linked list in reverse and put the value into the array from the index. """ head = make_linked_node(0) for i in range(1, arr.size): head = head.prepend(i) c = 0 while head is not None: arr[c] = head.data head = head.next c += 1 def runme(): arr = np.zeros(10, dtype=np.int32) fill_array(arr) print("== Result ==") print(arr) # Check answer np.testing.assert_equal(arr, np.arange(arr.size, dtype=arr.dtype)[::-1]) if __name__ == '__main__': runme() print("== Print memory allocation information == ") print(rtsys.get_allocation_stats())