def _test_get_num_threads_truth_outside_jit(self): for mask in range(2, min(6, config.NUMBA_NUM_THREADS + 1)): set_num_threads(mask) # a lot of work, hopefully will trigger "mask" count of threads to # join the parallel region (for those backends with dynamic threads) @njit(parallel=True) def test_func(): x = 5000000 buf = np.empty((x, )) for i in prange(x): buf[i] = _get_thread_id() return len(np.unique(buf)), get_num_threads() out = test_func() self.check_mask((mask, mask), out) @guvectorize(['void(int64[:], int64[:])'], '(n), (m)', nopython=True, target='parallel') def test_gufunc(x, out): x[:] = _get_thread_id() out[0] = get_num_threads() # Reshape to force parallelism x = np.full((5000000, ), -1, dtype=np.int64).reshape((100, 50000)) out = np.zeros((1, ), dtype=np.int64) test_gufunc(x, out) self.check_mask(mask, out) self.check_mask(mask, len(np.unique(x)))
def _test_set_num_threads_outside_jit(self): # Test set_num_threads outside a jitted function set_num_threads(2) @njit(parallel=True) def test_func(): x = 5 buf = np.empty((x, )) for i in prange(x): buf[i] = get_num_threads() return buf @guvectorize(['void(int64[:])'], '(n)', nopython=True, target='parallel') def test_gufunc(x): x[:] = get_num_threads() out = test_func() np.testing.assert_equal(out, 2) x = np.zeros((5000000, ), dtype=np.int64) test_gufunc(x) np.testing.assert_equal(x, 2)
def test_func(nthreads): x = 5 buf = np.empty((x, )) set_num_threads(nthreads) for i in prange(x): buf[i] = get_num_threads() return buf
def get_commandline_arguments(self, cmdline=None): args = super().get_commandline_arguments(cmdline) if args["output"] is None: args["output"] = pathlib.Path(args["config_yml"]).stem if args["threads"] is not None: nb.set_num_threads(args["threads"]) return args
def _test_set_num_threads_basic_guvectorize(self): max_threads = config.NUMBA_NUM_THREADS @guvectorize(["void(int64[:])"], "(n)", nopython=True, target="parallel") def get_n(x): x[:] = get_num_threads() x = np.zeros((5000000,), dtype=np.int64) get_n(x) np.testing.assert_equal(x, max_threads) set_num_threads(2) x = np.zeros((5000000,), dtype=np.int64) get_n(x) np.testing.assert_equal(x, 2) set_num_threads(max_threads) x = np.zeros((5000000,), dtype=np.int64) get_n(x) np.testing.assert_equal(x, max_threads) @guvectorize(["void(int64[:])"], "(n)", nopython=True, target="parallel") def set_get_n(n): set_num_threads(n[0]) n[:] = get_num_threads() x = np.zeros((5000000,), dtype=np.int64) x[0] = 2 set_get_n(x) np.testing.assert_equal(x, 2) x = np.zeros((5000000,), dtype=np.int64) x[0] = max_threads set_get_n(x) np.testing.assert_equal(x, max_threads)
def _transform(self, X, y=None): """Transform input time series. Parameters ---------- X : 3D np.ndarray of shape = [n_instances, n_dimensions, series_length] panel of time series to transform y : ignored argument for interface compatibility Returns ------- pandas DataFrame, transformed features """ X = X[:, 0, :].astype(np.float32) # change n_jobs dependend on value and existing cores prev_threads = get_num_threads() if self.n_jobs < 1 or self.n_jobs > multiprocessing.cpu_count(): n_jobs = multiprocessing.cpu_count() else: n_jobs = self.n_jobs set_num_threads(n_jobs) X_ = _transform(X, self.parameters) set_num_threads(prev_threads) return pd.DataFrame(X_)
def _choose_optimal_function( y_true, y_pred, f_name, f_single, f_parallel, f_additional_args={}, return_mean=True, sort=False, threads=1, ): set_num_threads(threads) # Check y_true ------------------------------------------------------------- if type(y_true) == np.ndarray and y_true.ndim == 2: y_true_type = "single" elif type(y_true) == np.ndarray and y_true.ndim == 3: y_true_type = "parallel" elif type(y_true) == nb.typed.typedlist.List and y_true[0].ndim == 1: y_true_type = "single" elif type(y_true) == nb.typed.typedlist.List and y_true[0].ndim == 2: y_true_type = "parallel" else: raise TypeError("y_true type not supported.") # Check y_pred ------------------------------------------------------------- if type(y_pred) == np.ndarray and y_pred.ndim == 2: y_pred_type = "single" elif type(y_pred) == np.ndarray and y_pred.ndim == 3: y_pred_type = "parallel" elif type(y_pred) == nb.typed.typedlist.List and y_pred[0].ndim == 1: y_pred_type = "single" elif type(y_pred) == nb.typed.typedlist.List and y_pred[0].ndim == 2: y_pred_type = "parallel" else: raise TypeError("y_pred type not supported.") # Check y_true and y_pred are aligned -------------------------------------- if y_true_type != y_pred_type: raise TypeError("y_true and y_pred types not supported.") if y_true_type == "single": if f_name == "ndcg": y_true = _descending_sort(y_true) if sort: y_pred = _descending_sort(y_pred) return f_single(y_true, y_pred, **f_additional_args) else: if f_name == "ndcg": y_true = _descending_sort_parallel(y_true) if sort: y_pred = _descending_sort_parallel(y_pred) scores = f_parallel(y_true, y_pred, **f_additional_args) if return_mean: return np.mean(scores) return scores
def run( self, model, plasma, no_of_packets, no_of_virtual_packets=0, nthreads=1, last_run=False, iteration=0, total_iterations=0, show_progress_bars=True, ): """ Run the montecarlo calculation Parameters ---------- model : tardis.model.Radial1DModel plasma : tardis.plasma.BasePlasma no_of_packets : int no_of_virtual_packets : int nthreads : int last_run : bool total_iterations : int The total number of iterations in the simulation. Returns ------- None """ set_num_threads(nthreads) self.time_of_simulation = self.calculate_time_of_simulation(model) self.volume = model.volume # Initializing estimator array self._initialize_estimator_arrays(plasma.tau_sobolevs.shape) self._initialize_geometry_arrays(model) self._initialize_packets(model.t_inner.value, no_of_packets, iteration, model.r_inner[0]) configuration_initialize(self, no_of_virtual_packets) montecarlo_radial1d( model, plasma, iteration, no_of_packets, total_iterations, show_progress_bars, self, ) self._integrator = FormalIntegrator(model, plasma, self)