Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# ...
# for (j1 = ...) // second source of nested parallelism
# ...
mapper = {}
for tree in retrieve_iteration_tree(partree):
index = tree.index(partree)
outer = tree[index:index + partree.ncollapsed]
inner = tree[index + partree.ncollapsed:]
# Heuristic: nested parallelism is applied only if the top nested
# parallel Iteration iterates *within* the top outer parallel Iteration
# (i.e., the outer is a loop over blocks, while the nested is a loop
# within a block)
candidates = []
for i in inner:
if any(is_integer(j.step - i.symbolic_size) for j in outer):
candidates.append(i)
elif candidates:
# If there's at least one candidate but `i` doesn't honor the
# heuristic above, then we break, as the candidates must be
# perfectly nested
break
if not candidates:
continue
# Introduce nested parallelism
omp_pragma = lambda i: self.lang['par-for'](i, nhyperthreads())
subroot, subpartree, _ = self._make_partree(candidates, omp_pragma)
mapper[subroot] = subpartree
partree = Transformer(mapper).visit(partree)
def index_apply_modulo(idx, modulo):
if is_integer(idx):
return idx % modulo
elif isinstance(idx, slice):
if idx.start is None:
start = idx.start
elif idx.start >= 0:
start = idx.start % modulo
else:
start = -(idx.start % modulo)
if idx.stop is None:
stop = idx.stop
elif idx.stop >= 0:
stop = idx.stop % (modulo + 1)
else:
stop = -(idx.stop % (modulo + 1))
return slice(start, stop, idx.step)
elif isinstance(idx, (tuple, list)):
retval = []
for i in as_tuple(loc_idx):
if isinstance(i, slice) and i.step is not None and i.step == -1:
if i.stop is None:
retval.append(slice(0, i.start+1, -i.step))
else:
retval.append(slice(i.stop+1, i.start+1, -i.step))
elif isinstance(i, slice) and i.step is not None and i.step < -1:
if i.stop is None:
lmin = i.start
while lmin >= 0:
lmin += i.step
retval.append(slice(lmin-i.step, i.start+1, -i.step))
else:
retval.append(slice(i.stop+1, i.start+1, -i.step))
elif is_integer(i):
retval.append(slice(i, i+1, 1))
else:
retval.append(i)
return as_tuple(retval)
def index_glb_to_loc(idx, decomposition):
"""Convert a global index into a local index."""
if is_integer(idx) or isinstance(idx, slice):
return decomposition(idx)
elif isinstance(idx, (tuple, list)):
return [decomposition(i) for i in idx]
elif isinstance(idx, np.ndarray):
return np.vectorize(lambda i: decomposition(i))(idx)
else:
raise ValueError("Cannot convert global index of type `%s` into a local index"
% type(idx))
def index_is_basic(idx):
if is_integer(idx):
return True
elif isinstance(idx, (slice, np.ndarray)):
return False
else:
return all(is_integer(i) or (i is NONLOCAL) for i in idx)
def __new__(cls, params):
args = []
for p in as_tuple(params):
if isinstance(p, str):
args.append(Symbol(p))
elif is_integer(p):
args.append(Integer(p))
elif not isinstance(p, Expr):
raise ValueError("`params` must be an iterable of Expr or str")
else:
args.append(p)
obj = sympy.Expr.__new__(cls, *args)
obj.params = tuple(args)
return obj
def q_constant(expr):
"""
Return True if ``expr`` is a constant, possibly symbolic, value, False otherwise.
Examples of non-constants are expressions containing Dimensions.
"""
if is_integer(expr):
return True
for i in expr.free_symbols:
try:
if not i.is_const:
return False
except AttributeError:
return False
return True
(0, 2)
Retrieve absolute local min/max given global min/max
>>> d.index_glb_to_loc((5, 9), rel=False)
(5, 7)
>>> d.index_glb_to_loc((1, 6), rel=False)
(5, 6)
"""
base = self.loc_abs_min if rel is True else 0
top = self.loc_abs_max
if len(args) == 1:
glb_idx = args[0]
if is_integer(glb_idx):
# index_glb_to_loc(index)
# -> Base case, empty local subdomain
if self.loc_empty:
return None
# -> Handle negative index
if glb_idx < 0:
glb_idx = self.glb_max + glb_idx + 1
# -> Do the actual conversion
if glb_idx in self.loc_abs_numb:
return glb_idx - base
elif self.glb_min <= glb_idx <= self.glb_max:
return None
else:
# This should raise an exception when used to access a numpy.array
return glb_idx
else:
@property
def _time_buffering(self):
return not is_integer(self.save)
(0, 2)
Retrieve absolute local min/max given global min/max
>>> d.index_glb_to_loc((5, 9), rel=False)
(5, 7)
>>> d.index_glb_to_loc((1, 6), rel=False)
(5, 6)
"""
base = self.loc_abs_min if rel is True else 0
top = self.loc_abs_max
if len(args) == 1:
glb_idx = args[0]
if is_integer(glb_idx):
# index_glb_to_loc(index)
# -> Base case, empty local subdomain
if self.loc_empty:
return None
# -> Handle negative index
if glb_idx < 0:
glb_idx = self.glb_max + glb_idx + 1
# -> Do the actual conversion
if glb_idx in self.loc_abs_numb:
return glb_idx - base
elif self.glb_min <= glb_idx <= self.glb_max:
return None
else:
# This should raise an exception when used to access a numpy.array
return glb_idx
else: