def __init__(self, kernel, machine, cores=<NUM_LIT:1>):
CachePredictor.__init__(self, kernel, machine, cores=cores)<EOL>loop_stack = list(self.kernel.get_loop_stack())<EOL>if any([l['<STR_LIT>'] != <NUM_LIT:1> for l in loop_stack]):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>index_order = [symbol_pos_int(l['<STR_LIT:index>']) for l in loop_stack]<EOL>for var_name, arefs in chain(self.kernel.sources.items(), self.kernel.destinations.items()):<EOL><INDENT>if next(iter(arefs)) is None:<EOL><INDENT>continue<EOL><DEDENT>for a in [self.kernel.access_to_sympy(var_name, a) for a in arefs]:<EOL><INDENT>for t in a.expand().as_ordered_terms():<EOL><INDENT>idx = t.free_symbols.intersection(index_order)<EOL>if not idx:<EOL><INDENT>continue<EOL><DEDENT>if len(idx) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>".format(t))<EOL><DEDENT>else:  <EOL><INDENT>idx = idx.pop()<EOL>pow_dict = {k: v for k, v in t.as_powers_dict().items()<EOL>if k != idx}<EOL>stride_dim = sum(pow_dict.values())<EOL>error = False<EOL>try:<EOL><INDENT>if loop_stack[-stride_dim-<NUM_LIT:1>]['<STR_LIT:index>'] != idx.name:<EOL><INDENT>error = True<EOL><DEDENT><DEDENT>except IndexError:<EOL><INDENT>error = True<EOL><DEDENT>if error:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(t))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>inner_index = symbol_pos_int(loop_stack[-<NUM_LIT:1>]['<STR_LIT:index>'])<EOL>inner_increment = loop_stack[-<NUM_LIT:1>]['<STR_LIT>']<EOL>for arefs in chain(chain(*self.kernel.sources.values()),<EOL>chain(*self.kernel.destinations.values())):<EOL><INDENT>if arefs is None:<EOL><INDENT>continue<EOL><DEDENT>for expr in arefs:<EOL><INDENT>diff = expr.subs(inner_index, <NUM_LIT:1>+inner_increment) - expr.subs(inner_index, <NUM_LIT:1>)<EOL>if diff != <NUM_LIT:0> and diff != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>element_size = self.kernel.datatypes_size[self.kernel.datatype]<EOL>accesses = {}<EOL>destinations = set()<EOL>distances = []<EOL>for var_name in self.kernel.variables:<EOL><INDENT>accesses[var_name] = self.kernel.sources.get(var_name, set()).union(<EOL>self.kernel.destinations.get(var_name, set()))<EOL>if not any(accesses[var_name]) or not any(<EOL>[a == inner_index or a.coeff(inner_index) != <NUM_LIT:0><EOL>for a in chain.from_iterable(accesses[var_name])]):<EOL><INDENT>continue<EOL><DEDENT>destinations.update(<EOL>[(var_name, tuple(r)) for r in self.kernel.destinations.get(var_name, [])])<EOL>acs = accesses[var_name]<EOL>acs = [self.kernel.access_to_sympy(var_name, r) for r in acs]<EOL>acs = [self.kernel.subs_consts(e) for e in acs]<EOL>acs.sort(reverse=True)<EOL>distances += [(acs[i-<NUM_LIT:1>]-acs[i]).simplify() for i in range(<NUM_LIT:1>, len(acs))]<EOL>distances.append(sympy.oo)<EOL><DEDENT>distances.sort(reverse=True)<EOL>distances_bytes = [d*element_size for d in distances]<EOL>results = {'<STR_LIT>': {k: list(v) for k,v in accesses.items()},<EOL>'<STR_LIT>': distances,<EOL>'<STR_LIT>': destinations,<EOL>'<STR_LIT>': distances_bytes,<EOL>'<STR_LIT>': []}<EOL>sum_array_sizes = sum(self.kernel.array_sizes(in_bytes=True, subs_consts=True).values())<EOL>for c in self.machine.get_cachesim(self.cores).levels(with_mem=False):<EOL><INDENT>hits = <NUM_LIT:0><EOL>misses = len(distances_bytes)<EOL>cache_requirement = <NUM_LIT:0><EOL>tail = <NUM_LIT:0><EOL>if c.size() > sum_array_sizes:<EOL><INDENT>hits = misses<EOL>misses = <NUM_LIT:0><EOL>cache_requirement = sum_array_sizes<EOL><DEDENT>else:<EOL><INDENT>for tail in sorted(set(distances_bytes), reverse=True):<EOL><INDENT>if tail is sympy.oo:<EOL><INDENT>continue<EOL><DEDENT>cache_requirement = (<EOL>sum([d for d in distances_bytes if d <= tail]) +<EOL>tail*len([d for d in distances_bytes if d > tail]))<EOL>if cache_requirement <= c.size():<EOL><INDENT>hits = len([d for d in distances_bytes if d <= tail])<EOL>misses = len([d for d in distances_bytes if d > tail])<EOL>break<EOL><DEDENT><DEDENT><DEDENT>results['<STR_LIT>'].append({<EOL>'<STR_LIT:name>': c.name,<EOL>'<STR_LIT>': hits,<EOL>'<STR_LIT>': misses,<EOL>'<STR_LIT>': len(destinations) if c.size() < sum_array_sizes else <NUM_LIT:0>,<EOL>'<STR_LIT>': cache_requirement,<EOL>'<STR_LIT>': tail})<EOL><DEDENT>self.results = results<EOL>
Initialize layer condition based predictor from kernel and machine object.