def fold_blockable_tree(node, exclude_innermost=False): """ Create :class:`IterationFold`s from sequences of nested :class:`Iteration`. """ found = FindAdjacentIterations().visit(node) found.pop('seen_iteration') mapper = {} for k, v in found.items(): for i in v: # Pre-condition: they all must be perfect iterations assert len(i) > 1 if any(not IsPerfectIteration().visit(j) for j in i): continue # Only retain consecutive trees having same depth trees = [retrieve_iteration_tree(j)[0] for j in i] handle = [] for j in trees: if len(j) != len(trees[0]): break handle.append(j) trees = handle if not trees: continue # Check foldability pairwise_folds = list(zip(*reversed(trees))) if any(not is_foldable(j) for j in pairwise_folds): continue # Maybe heuristically exclude innermost Iteration if exclude_innermost is True: pairwise_folds = pairwise_folds[:-1] # Perhaps there's nothing to fold if len(pairwise_folds) == 1: continue # Perform folding for j in pairwise_folds: root, remainder = j[0], j[1:] folds = [(tuple(y - x for x, y in zip(i.offsets, root.offsets)), i.nodes) for i in remainder] mapper[root] = IterationFold(folds=folds, **root.args) for k in remainder: mapper[k] = None # Insert the IterationFolds in the Iteration/Expression tree processed = NestedTransformer(mapper).visit(node) return processed
def test_consistency_perfect_loops(self, tu, tv, ti0, t0, t1): eq1 = Eq(tu, tv * ti0 * t0 + ti0 * t1) eq2 = Eq(ti0, tu + t0 * 3.) eq3 = Eq(tv, ti0 * tu) op1 = Operator([eq1, eq2, eq3], dse='noop', dle='noop') op2 = Operator([eq2, eq1, eq3], dse='noop', dle='noop') op3 = Operator([eq3, eq2, eq1], dse='noop', dle='noop') trees = [retrieve_iteration_tree(i) for i in [op1, op2, op3]] assert all(len(i) == 1 for i in trees) trees = [i[0] for i in trees] for tree in trees: assert IsPerfectIteration().visit(tree[0]) assert len(tree[-1].nodes) == 3 pivot = set([j.expr for j in trees[0][-1].nodes]) assert all(set([j.expr for j in i[-1].nodes]) == pivot for i in trees)
def unfold_blocked_tree(node): """ Unfold nested :class:`IterationFold`. :Example: Given a section of Iteration/Expression tree as below: :: for i = 1 to N-1 // folded for j = 1 to N-1 // folded foo1() Assuming a fold with offset 1 in both /i/ and /j/ and body ``foo2()``, create: :: for i = 1 to N-1 for j = 1 to N-1 foo1() for i = 2 to N-2 for j = 2 to N-2 foo2() """ # Search the unfolding candidates candidates = [] for tree in retrieve_iteration_tree(node): handle = tuple(i for i in tree if i.is_IterationFold) if handle: # Sanity check assert IsPerfectIteration().visit(handle[0]) candidates.append(handle) # Perform unfolding tag = ntags() mapper = {} for tree in candidates: trees = list(zip(*[i.unfold() for i in tree])) # Update tag for i, _tree in enumerate(list(trees)): trees[i] = tuple(j.retag(tag + i) for j in _tree) trees = optimize_unfolded_tree(trees[:-1], trees[-1]) mapper[tree[0]] = List(body=trees) # Insert the unfolded Iterations in the Iteration/Expression tree processed = Transformer(mapper).visit(node) return processed
def test_consistency_coupled_w_ofs(self, exprs, ti0, ti1, ti3): """ Test that no matter what is the order in which the equations are provided to an Operator, the resulting loop nest is the same. The array accesses in the equations may or may not use offsets; these impact the loop bounds, but not the resulting tree structure. """ eq1, eq2, eq3 = EVAL(exprs, ti0.base, ti1.base, ti3.base) op1 = Operator([eq1, eq2, eq3], dse='noop', dle='noop') op2 = Operator([eq2, eq1, eq3], dse='noop', dle='noop') op3 = Operator([eq3, eq2, eq1], dse='noop', dle='noop') trees = [retrieve_iteration_tree(i) for i in [op1, op2, op3]] assert all(len(i) == 1 for i in trees) trees = [i[0] for i in trees] for tree in trees: assert IsPerfectIteration().visit(tree[0]) assert len(tree[-1].nodes) == 3 pivot = set([j.expr for j in trees[0][-1].nodes]) assert all(set([j.expr for j in i[-1].nodes]) == pivot for i in trees)
def test_consistency_coupled_wo_ofs(self, tu, tv, ti0, t0, t1): """ Test that no matter what is the order in which the equations are provided to an Operator, the resulting loop nest is the same. None of the array accesses in the equations use offsets. """ eq1 = Eq(tu, tv*ti0*t0 + ti0*t1) eq2 = Eq(ti0, tu + t0*3.) eq3 = Eq(tv, ti0*tu) op1 = Operator([eq1, eq2, eq3], dse='noop', dle='noop') op2 = Operator([eq2, eq1, eq3], dse='noop', dle='noop') op3 = Operator([eq3, eq2, eq1], dse='noop', dle='noop') trees = [retrieve_iteration_tree(i) for i in [op1, op2, op3]] assert all(len(i) == 1 for i in trees) trees = [i[0] for i in trees] for tree in trees: assert IsPerfectIteration().visit(tree[0]) assert len(tree[-1].nodes) == 3 pivot = set([j.expr for j in trees[0][-1].nodes]) assert all(set([j.expr for j in i[-1].nodes]) == pivot for i in trees)
def _profile_sections(self, nodes): """Introduce C-level profiling nodes within the Iteration/Expression tree.""" mapper = {} for node in nodes: for itspace in FindSections().visit(node).keys(): for i in itspace: if IsPerfectIteration().visit(i): # Insert `TimedList` block. This should come from # the profiler, but we do this manually for now. lname = 'loop_%s_%d' % (i.index, len(mapper)) mapper[i] = TimedList(gname=self.profiler.varname, lname=lname, body=i) self.profiler.add(lname) # Estimate computational properties of the timed section # (operational intensity, memory accesses) expressions = FindNodes(Expression).visit(i) ops = estimate_cost([e.expr for e in expressions]) memory = estimate_memory([e.expr for e in expressions]) self.sections[itspace] = Profile(lname, ops, memory) break processed = Transformer(mapper).visit(List(body=nodes)) return processed
def test_is_perfect_iteration(block1, block2, block3): checker = IsPerfectIteration() assert checker.visit(block1) is True assert checker.visit(block1.nodes[0]) is True assert checker.visit(block1.nodes[0].nodes[0]) is True assert checker.visit(block2) is False assert checker.visit(block2.nodes[1]) is True assert checker.visit(block2.nodes[1].nodes[0]) is True assert checker.visit(block3) is False assert checker.visit(block3.nodes[0]) is True assert checker.visit(block3.nodes[1]) is True assert checker.visit(block3.nodes[2]) is True
def _loop_blocking(self, state, **kwargs): """ Apply loop blocking to :class:`Iteration` trees. Blocking is applied to parallel iteration trees. Heuristically, innermost dimensions are not blocked to maximize the trip count of the SIMD loops. Different heuristics may be specified by passing the keywords ``blockshape`` and ``blockinner`` to the DLE. The former, a dictionary, is used to indicate a specific block size for each blocked dimension. For example, for the :class:`Iteration` tree: :: for i for j for k ... one may provide ``blockshape = {i: 4, j: 7}``, in which case the two outer loops will blocked, and the resulting 2-dimensional block will have size 4x7. The latter may be set to True to also block innermost parallel :class:`Iteration` objects. """ exclude_innermost = not self.params.get('blockinner', False) ignore_heuristic = self.params.get('blockalways', False) blocked = OrderedDict() processed = [] for node in state.nodes: # Make sure loop blocking will span as many Iterations as possible fold = fold_blockable_tree(node, exclude_innermost) mapper = {} for tree in retrieve_iteration_tree(fold): # Is the Iteration tree blockable ? iterations = [i for i in tree if i.is_Parallel] if exclude_innermost: iterations = [ i for i in iterations if not i.is_Vectorizable ] if len(iterations) <= 1: continue root = iterations[0] if not IsPerfectIteration().visit(root): # Illegal/unsupported continue if not tree[0].is_Sequential and not ignore_heuristic: # Heuristic: avoid polluting the generated code with blocked # nests (thus increasing JIT compilation time and affecting # readability) if the blockable tree isn't embedded in a # sequential loop (e.g., a timestepping loop) continue # Decorate intra-block iterations with an IterationProperty TAG = tagger(len(mapper)) # Build all necessary Iteration objects, individually. These will # subsequently be composed to implement loop blocking. inter_blocks = [] intra_blocks = [] remainders = [] for i in iterations: # Build Iteration over blocks dim = blocked.setdefault( i, Dimension("%s_block" % i.dim.name)) block_size = dim.symbolic_size iter_size = i.dim.size or i.dim.symbolic_size start = i.limits[0] - i.offsets[0] finish = iter_size - i.offsets[1] innersize = iter_size - (-i.offsets[0] + i.offsets[1]) finish = finish - (innersize % block_size) inter_block = Iteration([], dim, [start, finish, block_size], properties=PARALLEL) inter_blocks.append(inter_block) # Build Iteration within a block start = inter_block.dim finish = start + block_size intra_block = i._rebuild([], limits=[start, finish, 1], offsets=None, properties=i.properties + (TAG, ELEMENTAL)) intra_blocks.append(intra_block) # Build unitary-increment Iteration over the 'leftover' region. # This will be used for remainder loops, executed when any # dimension size is not a multiple of the block size. start = inter_block.limits[1] finish = iter_size - i.offsets[1] remainder = i._rebuild([], limits=[start, finish, 1], offsets=None) remainders.append(remainder) # Build blocked Iteration nest blocked_tree = compose_nodes(inter_blocks + intra_blocks + [iterations[-1].nodes]) # Build remainder Iterations remainder_trees = [] for n in range(len(iterations)): for c in combinations([i.dim for i in iterations], n + 1): # First all inter-block Interations nodes = [ b._rebuild(properties=b.properties + (REMAINDER, )) for b, r in zip(inter_blocks, remainders) if r.dim not in c ] # Then intra-block or remainder, for each dim (in order) properties = (REMAINDER, TAG, ELEMENTAL) for b, r in zip(intra_blocks, remainders): handle = r if b.dim in c else b nodes.append( handle._rebuild(properties=properties)) nodes.extend([iterations[-1].nodes]) remainder_trees.append(compose_nodes(nodes)) # Will replace with blocked loop tree mapper[root] = List(body=[blocked_tree] + remainder_trees) rebuilt = Transformer(mapper).visit(fold) # Finish unrolling any previously folded Iterations processed.append(unfold_blocked_tree(rebuilt)) # All blocked dimensions if not blocked: return {'nodes': processed} # Determine the block shape blockshape = self.params.get('blockshape') if not blockshape: # Use trivial heuristic for a suitable blockshape def heuristic(dim_size): ths = 8 # FIXME: This really needs to be improved return ths if dim_size > ths else 1 blockshape = {k: heuristic for k in blocked.keys()} else: try: nitems, nrequired = len(blockshape), len(blocked) blockshape = {k: v for k, v in zip(blocked, blockshape)} if nitems > nrequired: dle_warning("Provided 'blockshape' has more entries than " "blocked loops; dropping entries ...") if nitems < nrequired: dle_warning("Provided 'blockshape' has fewer entries than " "blocked loops; dropping dimensions ...") except TypeError: blockshape = {list(blocked)[0]: blockshape} blockshape.update( {k: None for k in blocked.keys() if k not in blockshape}) # Track any additional arguments required to execute /state.nodes/ arguments = [ BlockingArg(v, k, blockshape[k]) for k, v in blocked.items() ] return { 'nodes': processed, 'arguments': arguments, 'flags': 'blocking' }
def _specialize(self, nodes, parameters): """ Create a YASK representation of this Iteration/Expression tree. ``parameters`` is modified in-place adding YASK-related arguments. """ log("Specializing a Devito Operator for YASK...") # Find offloadable Iteration/Expression trees offloadable = [] for tree in retrieve_iteration_tree(nodes): parallel = filter_iterations(tree, lambda i: i.is_Parallel) if not parallel: # Cannot offload non-parallel loops continue if not (IsPerfectIteration().visit(tree) and all(i.is_Expression for i in tree[-1].nodes)): # Don't know how to offload this Iteration/Expression to YASK continue functions = flatten(i.functions for i in tree[-1].nodes) keys = set((i.indices, i.shape, i.dtype) for i in functions if i.is_TimeData) if len(keys) == 0: continue elif len(keys) > 1: exit("Cannot handle Operators w/ heterogeneous grids") dimensions, shape, dtype = keys.pop() if len(dimensions) == len(tree) and\ all(i.dim == j for i, j in zip(tree, dimensions)): # Detected a "full" Iteration/Expression tree (over both # time and space dimensions) offloadable.append((tree, dimensions, shape, dtype)) # Construct YASK ASTs given Devito expressions. New grids may be allocated. if len(offloadable) == 0: # No offloadable trees found self.context = YaskNullContext() self.yk_soln = YaskNullSolution() processed = nodes log("No offloadable trees found") elif len(offloadable) == 1: # Found *the* offloadable tree for this Operator tree, dimensions, shape, dtype = offloadable[0] self.context = contexts.fetch(dimensions, shape, dtype) # Create a YASK compiler solution for this Operator # Note: this can be dropped as soon as the kernel has been built yc_soln = self.context.make_yc_solution(namespace['jit-yc-soln']) transform = sympy2yask(self.context, yc_soln) try: for i in tree[-1].nodes: transform(i.expr) funcall = make_sharedptr_funcall(namespace['code-soln-run'], ['time'], namespace['code-soln-name']) funcall = Element(c.Statement(ccode(funcall))) processed = Transformer({tree[1]: funcall}).visit(nodes) # Track this is an external function call self.func_table[namespace['code-soln-run']] = FunMeta( None, False) # JIT-compile the newly-created YASK kernel self.yk_soln = self.context.make_yk_solution( namespace['jit-yk-soln'], yc_soln) # Now we must drop a pointer to the YASK solution down to C-land parameters.append( Object(namespace['code-soln-name'], namespace['type-solution'], self.yk_soln.rawpointer)) # Print some useful information about the newly constructed solution log("Solution '%s' contains %d grid(s) and %d equation(s)." % (yc_soln.get_name(), yc_soln.get_num_grids(), yc_soln.get_num_equations())) except: self.yk_soln = YaskNullSolution() processed = nodes log("Unable to offload a candidate tree.") else: exit("Found more than one offloadable trees in a single Operator") # Some Iteration/Expression trees are not offloaded to YASK and may # require further processing to be executed in YASK, due to the differences # in storage layout employed by Devito and YASK processed = make_grid_accesses(processed) # Update the parameters list adding all necessary YASK grids for i in list(parameters): try: if i.from_YASK: parameters.append( Object(namespace['code-grid-name'](i.name), namespace['type-grid'], i.data.rawpointer)) except AttributeError: # Ignore e.g. Dimensions pass log("Specialization successfully performed!") return processed
def _loop_blocking(self, state, **kwargs): """ Apply loop blocking to :class:`Iteration` trees. By default, the blocked :class:`Iteration` objects and the block size are determined heuristically. The heuristic consists of searching the deepest Iteration/Expression tree and blocking all dimensions except: * The innermost (eg, to retain SIMD vectorization); * Those dimensions inducing loop-carried dependencies. The caller may take over the heuristic through ``kwargs['blocking']``, a dictionary indicating the block size of each blocked dimension. For example, for the :class:`Iteration` tree below: :: for i for j for k ... one may pass in ``kwargs['blocking'] = {i: 4, j: 7}``, in which case the two outer loops would be blocked, and the resulting 2-dimensional block would be of size 4x7. """ Region = namedtuple('Region', 'main leftover') blocked = OrderedDict() processed = [] for node in state.nodes: mapper = {} for tree in retrieve_iteration_tree(node): # Is the Iteration tree blockable ? iterations = [i for i in tree if i.is_Parallel] if 'blockinner' not in self.params: iterations = [ i for i in iterations if not i.is_Vectorizable ] if not iterations: continue root = iterations[0] if not IsPerfectIteration().visit(root): continue # Construct the blocked loop nest, as well as all necessary # remainder loops regions = OrderedDict() blocked_iterations = [] for i in iterations: # Build Iteration over blocks dim = blocked.setdefault( i, Dimension("%s_block" % i.dim.name)) block_size = dim.symbolic_size iter_size = i.dim.size or i.dim.symbolic_size start = i.limits[0] - i.offsets[0] finish = iter_size - i.offsets[1] finish = finish - ((finish - i.offsets[1]) % block_size) inter_block = Iteration([], dim, [start, finish, block_size], properties=as_tuple('parallel')) # Build Iteration within a block start = inter_block.dim finish = start + block_size properties = 'vector-dim' if i.is_Vectorizable else None intra_block = Iteration([], i.dim, [start, finish, 1], i.index, properties=as_tuple(properties)) blocked_iterations.append((inter_block, intra_block)) # Build unitary-increment Iteration over the 'main' region # (the one blocked); necessary to generate code iterating over # non-blocked ("remainder") iterations. start = inter_block.limits[0] finish = inter_block.limits[1] main = Iteration([], i.dim, [start, finish, 1], i.index, properties=i.properties) # Build unitary-increment Iteration over the 'leftover' region: # again as above, this may be necessary when the dimension size # is not a multiple of the block size. start = inter_block.limits[1] finish = iter_size - i.offsets[1] leftover = Iteration([], i.dim, [start, finish, 1], i.index, properties=i.properties) regions[i] = Region(main, leftover) blocked_tree = list(flatten(zip(*blocked_iterations))) blocked_tree = compose_nodes(blocked_tree + [iterations[-1].nodes]) # Build remainder loops remainder_tree = [] for n in range(len(iterations)): for i in combinations(iterations, n + 1): nodes = [ v.leftover if k in i else v.main for k, v in regions.items() ] nodes += [iterations[-1].nodes] remainder_tree.append(compose_nodes(nodes)) # Will replace with blocked loop tree mapper[root] = List(body=[blocked_tree] + remainder_tree) rebuilt = Transformer(mapper).visit(node) processed.append(rebuilt) # All blocked dimensions if not blocked: return {'nodes': processed} # Determine the block shape blockshape = self.params.get('blockshape') if not blockshape: # Use trivial heuristic for a suitable blockshape def heuristic(dim_size): ths = 8 # FIXME: This really needs to be improved return ths if dim_size > ths else 1 blockshape = {k: heuristic for k in blocked.keys()} else: try: nitems, nrequired = len(blockshape), len(blocked) blockshape = {k: v for k, v in zip(blocked, blockshape)} if nitems > nrequired: dle_warning("Provided 'blockshape' has more entries than " "blocked loops; dropping entries ...") if nitems < nrequired: dle_warning("Provided 'blockshape' has fewer entries than " "blocked loops; dropping dimensions ...") except TypeError: blockshape = {list(blocked)[0]: blockshape} blockshape.update( {k: None for k in blocked.keys() if k not in blockshape}) # Track any additional arguments required to execute /state.nodes/ arguments = [ BlockingArg(v, k, blockshape[k]) for k, v in blocked.items() ] return { 'nodes': processed, 'arguments': arguments, 'flags': 'blocking' }
def create_profile(node): """ Create a :class:`Profiler` for the Iteration/Expression tree ``node``. The following code sections are profiled: :: * The whole ``node``; * A sequence of perfectly nested loops that have common :class:`Iteration` dimensions, but possibly different extent. For example: :: for x = 0 to N .. for x = 1 to N-1 .. Both Iterations have dimension ``x``, and will be profiled as a single section, though their extent is different. * Any perfectly nested loops. """ profiler = Profiler() # Group by root Iteration mapper = OrderedDict() for itspace in FindSections().visit(node): mapper.setdefault(itspace[0], []).append(itspace) # Group sections if their iteration spaces overlap key = lambda itspace: set([i.dim for i in itspace]) found = [] for v in mapper.values(): queue = list(v) handle = [] while queue: item = queue.pop(0) if not handle or key(item) == key(handle[0]): handle.append(item) else: # Found a timing section found.append(tuple(handle)) handle = [item] if handle: found.append(tuple(handle)) # Create and track C-level timers mapper = OrderedDict() for i, group in enumerate(found): name = 'section_%d' % i section, remainder = group[0], group[1:] index = len(section) > 1 and not IsPerfectIteration().visit(section[0]) root = section[index] # Prepare to transform the Iteration/Expression tree body = tuple(j[index] for j in group) mapper[root] = TimedList(gname=profiler.varname, lname=name, body=body) for j in remainder: mapper[j[index]] = None # Estimate computational properties of the profiled section expressions = FindNodes(Expression).visit(body) ops = estimate_cost([e.expr for e in expressions]) memory = estimate_memory([e.expr for e in expressions]) # Keep track of the new profiled section profiler.add(name, section, ops, memory) # Transform the Iteration/Expression tree introducing the C-level timers processed = Transformer(mapper).visit(node) return processed, profiler
def _analyze(self, state): """ Analyze the Iteration/Expression trees in ``state.nodes`` to detect information useful to the subsequent DLE passes. In particular, fully-parallel or "outermost-sequential inner-parallel" (OSIP) :class:`Iteration` trees are searched tracked. In an OSIP :class:`Iteration` tree, the outermost :class:`Iteration` represents a sequential dimension, whereas all inner :class:`Iteration` objects represent parallel dimensions. """ nodes = state.nodes sections = FindSections().visit(nodes) # The analysis below may return "false positives" (ie, absence of fully- # parallel or OSIP trees when this is actually false), but this should # never be the case in practice, given the targeted stencil codes. mapper = OrderedDict() for tree, nexprs in sections.items(): exprs = [e.expr for e in nexprs] # "Prefetch" objects to speed up the analsys terms = {e: tuple(retrieve_terminals(e.rhs)) for e in exprs} # Determine whether the Iteration tree ... is_FP = True # ... is fully parallel (FP) is_OP = True # ... has an outermost parallel dimension (OP) is_OSIP = True # ... is of type OSIP is_US = True # ... has a unit-strided innermost dimension (US) for lhs in [e.lhs for e in exprs if not e.lhs.is_Symbol]: for e in exprs: for i in [j for j in terms[e] if as_symbol(j) == as_symbol(lhs)]: is_FP &= lhs.indices == i.indices is_OP &= lhs.indices[0] == i.indices[0] and\ all(lhs.indices[0].free_symbols.isdisjoint(j.free_symbols) for j in i.indices[1:]) # not A[x,y] = A[x,x+1] is_US &= lhs.indices[-1] == i.indices[-1] lhs_function, i_function = lhs.base.function, i.base.function is_OSIP &= lhs_function.indices[0] == i_function.indices[0] and\ (lhs.indices[0] != i.indices[0] or len(lhs.indices) == 1 or lhs.indices[1] == i.indices[1]) # Build a node->property mapper if is_FP: for i in tree: mapper.setdefault(i, []).append(PARALLEL) elif is_OP: mapper.setdefault(tree[0], []).append(PARALLEL) elif is_OSIP: mapper.setdefault(tree[0], []).append(SEQUENTIAL) for i in tree[1:]: mapper.setdefault(i, []).append(PARALLEL) if IsPerfectIteration().visit(tree[-1]) and (is_FP or is_OSIP or is_US): # Vectorizable if len(tree) > 1 and SEQUENTIAL not in mapper.get(tree[-2], []): # Heuristic: there's at least an outer parallel Iteration mapper.setdefault(tree[-1], []).append(VECTOR) # Store the discovered properties in the Iteration/Expression tree for k, v in list(mapper.items()): args = k.args # SEQUENTIAL kills PARALLEL properties = SEQUENTIAL if (SEQUENTIAL in v or not k.is_Linear) else v properties = as_tuple(args.pop('properties')) + as_tuple(properties) mapper[k] = Iteration(properties=properties, **args) nodes = NestedTransformer(mapper).visit(nodes) return {'nodes': nodes}