diff --git a/src/numba/openmp/config.py b/src/numba/openmp/config.py index 84e2edc48545..10fc2b2357d4 100644 --- a/src/numba/openmp/config.py +++ b/src/numba/openmp/config.py @@ -25,3 +25,4 @@ def _safe_readenv(name, ctor, default): OPENMP_DISABLED = _safe_readenv("NUMBA_OPENMP_DISABLED", int, 0) # Use toolchain for device code compilation by default to avoid issues with libomptarget compatibility checks. OPENMP_DEVICE_TOOLCHAIN = _safe_readenv("NUMBA_OPENMP_DEVICE_TOOLCHAIN", int, 1) +SMART_PRIVATIZE = _safe_readenv("NUMBA_SMART_PRIVATIZE", int, 0) diff --git a/src/numba/openmp/omp_lower.py b/src/numba/openmp/omp_lower.py index 955fe81e8d7a..bfca1eaecf3b 100644 --- a/src/numba/openmp/omp_lower.py +++ b/src/numba/openmp/omp_lower.py @@ -18,7 +18,7 @@ import sys import os -from .config import DEBUG_OPENMP +from .config import DEBUG_OPENMP, SMART_PRIVATIZE from .parser import openmp_parser from .analysis import ( remove_ssa, @@ -304,7 +304,10 @@ def should_be_firstprivate(var_name): # All private variables (user-defined and compiler-generated) for var_name in sorted(private_to_region): - add_clause(var_name, "QUAL.OMP.PRIVATE") + if SMART_PRIVATIZE or is_internal_var(ir.Var(scope, var_name, self.loc)): + add_clause(var_name, "QUAL.OMP.PRIVATE") + else: + add_clause(var_name, "QUAL.OMP.SHARED") def make_implicit_explicit_target( self, @@ -649,7 +652,7 @@ def get_loops_in_region(all_loops): ) deconstruct_indices = [] - new_deconstruct_var = new_var_scope.redefine("deconstruct", self.loc) + new_deconstruct_var = new_var_scope.redefine("$deconstruct", self.loc) deconstruct_indices.append( ir.Assign(loop_bounds[-1][1], new_deconstruct_var, self.loc) ) @@ -665,7 +668,7 @@ def get_loops_in_region(all_loops): operator.floordiv, new_deconstruct_var, cur_iterspace_var, self.loc ) new_deconstruct_var_loop = new_var_scope.redefine( - "deconstruct" + str(deconstruct_index), self.loc + "$deconstruct" + str(deconstruct_index), self.loc ) deconstruct_indices.append( ir.Assign(deconstruct_div, cur_loop_bound, self.loc) @@ -673,7 +676,7 @@ def get_loops_in_region(all_loops): # if DEBUG_OPENMP >= 1: # deconstruct_indices.append(ir.Print([cur_loop_bound], None, self.loc)) new_deconstruct_var_mul = new_var_scope.redefine( - "deconstruct_mul" + str(deconstruct_index), self.loc + "$deconstruct_mul" + str(deconstruct_index), self.loc ) deconstruct_indices.append( ir.Assign( @@ -935,7 +938,7 @@ def _get_loop_kind(func_var, call_table): if latest_index.name not in vars_in_explicit_clauses: new_index_clause = openmp_tag( "QUAL.OMP.PRIVATE", - ir.Var(loop_index.scope, latest_index.name, inst.loc), + latest_index.name, ) clauses.append(new_index_clause) vars_in_explicit_clauses[latest_index.name] = new_index_clause diff --git a/src/numba/openmp/tests/test_openmp.py b/src/numba/openmp/tests/test_openmp.py index c3aee25fa37c..93366e8e04ff 100644 --- a/src/numba/openmp/tests/test_openmp.py +++ b/src/numba/openmp/tests/test_openmp.py @@ -2050,7 +2050,7 @@ def test_impl(N, iters): with openmp("parallel"): with openmp("barrier"): pass - with openmp("for private(p, sum)"): + with openmp("for private(p, sum, i)"): for _ in range(iters): with openmp("critical"): p = count @@ -2074,7 +2074,7 @@ def test_impl(N): omp_set_num_threads(N) ca = np.zeros(N) sum = 0 - with openmp("parallel private(sum) shared(c)"): + with openmp("parallel private(sum, i) shared(c)"): c = N with openmp("barrier"): pass @@ -2117,7 +2117,7 @@ def test_impl(N): omp_set_num_threads(N) a = np.zeros((2, N)) sa = np.zeros(N) - with openmp("parallel private(a0c, sum, tn)"): + with openmp("parallel private(a0c, sum, tn, j)"): tn = omp_get_thread_num() with openmp("barrier"): pass @@ -2371,7 +2371,7 @@ def test_task_basic(self): @njit def test_impl(ntsks): a = np.zeros(ntsks) - with openmp("parallel"): + with openmp("parallel private(i)"): with openmp("single"): for i in range(ntsks): with openmp("task"): @@ -4477,8 +4477,8 @@ def test_impl(n): b = np.ones((n, n)) c = np.zeros((n, n)) with openmp(target_pragma): - with openmp("teams"): - with openmp("loop collapse(2)"): + with openmp("teams private(i,j)"): + with openmp("loop collapse(2) private(i,j)"): for i in range(n): for j in range(n): c[i, j] = a[i, j] + b[i, j] @@ -4610,7 +4610,7 @@ def test_impl(num_steps): omp_set_num_threads(4) with openmp("parallel"): - with openmp("for reduction(+:the_sum) schedule(static)"): + with openmp("for private(x) reduction(+:the_sum) schedule(static)"): for j in range(num_steps): x = ((j - 1) - 0.5) * step the_sum += 4.0 / (1.0 + x * x) @@ -4629,7 +4629,7 @@ def test_impl(num_steps): the_sum = 0.0 omp_set_num_threads(4) - with openmp("parallel for reduction(+:the_sum) schedule(static)"): + with openmp("parallel for private(x) reduction(+:the_sum) schedule(static)"): for j in range(num_steps): x = ((j - 1) - 0.5) * step the_sum += 4.0 / (1.0 + x * x) @@ -4647,7 +4647,7 @@ def test_impl(num_steps): the_sum = 0.0 omp_set_num_threads(4) - with openmp("loop reduction(+:the_sum) schedule(static)"): + with openmp("loop private(x) reduction(+:the_sum) schedule(static)"): for j in range(num_steps): x = ((j - 1) - 0.5) * step the_sum += 4.0 / (1.0 + x * x) @@ -4670,7 +4670,7 @@ def test_impl(num_steps): omp_set_num_threads(j) full_sum = 0.0 - with openmp("parallel private(tid, numthreads, local_sum, x)"): + with openmp("parallel private(tid, numthreads, local_sum, x, i)"): tid = omp_get_thread_num() numthreads = omp_get_num_threads() local_sum = 0.0