From f1eed08e07812d367c7e9c1b30f92c70aebcd8d7 Mon Sep 17 00:00:00 2001 From: Filippo Martini Date: Fri, 18 Dec 2020 15:03:50 +0100 Subject: [PATCH] Refactor shared_memory init in StructureEstimator --- PyCTBN/PyCTBN/structure_estimator.py | 34 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/PyCTBN/PyCTBN/structure_estimator.py b/PyCTBN/PyCTBN/structure_estimator.py index 26e2fec..47c2fd4 100644 --- a/PyCTBN/PyCTBN/structure_estimator.py +++ b/PyCTBN/PyCTBN/structure_estimator.py @@ -42,19 +42,8 @@ class StructureEstimator: #self._sample_path = sample_path self._nodes = np.array(sample_path.structure.nodes_labels) self._tot_vars_number = sample_path.total_variables_count - self._shm_times = multiprocessing.shared_memory.SharedMemory(name='sh_times', create=True, - size=sample_path.trajectories.times.nbytes) - self._shm_trajectories = multiprocessing.shared_memory.SharedMemory(name='sh_traj', create=True, - size=sample_path.trajectories.complete_trajectory.nbytes) - self._times = np.ndarray(sample_path.trajectories.times.shape, sample_path.trajectories.times.dtype, - self._shm_times.buf) - self._times[:] = sample_path.trajectories.times[:] - - self._trajectories = np.ndarray(sample_path.trajectories.complete_trajectory.shape, - sample_path.trajectories.complete_trajectory.dtype, self._shm_trajectories.buf) - self._trajectories[:] = sample_path.trajectories.complete_trajectory[:] - - + self._times = sample_path.trajectories.times + self._trajectories = sample_path.trajectories.complete_trajectory self._nodes_vals = sample_path.structure.nodes_values self._nodes_indxs = sample_path.structure.nodes_indexes self._complete_graph = self.build_complete_graph(sample_path.structure.nodes_labels) @@ -262,6 +251,17 @@ class StructureEstimator: """Compute the CTPC algorithm over the entire net. """ ctpc_algo = StructureEstimator.one_iteration_of_CTPC_algorithm + shm_times = multiprocessing.shared_memory.SharedMemory(name='sh_times', create=True, + size=self._.times.nbytes) + shm_trajectories = multiprocessing.shared_memory.SharedMemory(name='sh_traj', create=True, + size=self._trajectories.nbytes) + times_arr = np.ndarray(self._times.shape, self._times.dtype, + shm_times.buf) + times_arr[:] = self._times[:] + + trajectories_arr = np.ndarray(self._trajectories.shape, + self._trajectories.dtype, shm_trajectories.buf) + trajectories_arr[:] = self._trajectories[:] total_vars_numb_list = [self._tot_vars_number] * len(self._nodes) parents_list = [list(self._complete_graph.predecessors(var_id)) for var_id in self._nodes] nodes_array_list = [self._nodes] * len(self._nodes) @@ -280,10 +280,10 @@ class StructureEstimator: nodes_array_list, nodes_indxs_array_list, nodes_vals_array_list, tests_alfa_dims_list, datas_dims_list)) - self._shm_times.close() - self._shm_times.unlink() - self._shm_trajectories.close() - self._shm_trajectories.unlink() + shm_times.close() + shm_times.unlink() + shm_trajectories.close() + shm_trajectories.unlink() self._result_graph = self.build_result_graph(self._nodes, parent_sets) def save_results(self) -> None: