def test_compile(*filenames): ''' Compile rules -> programs for the virtual machine. ''' if not filenames: filenames = find_theories() for stem_rules in filenames: programs = ['# {filename}'.format(filename=stem_rules)] assert stem_rules[-len('.theory'):] == '.theory', stem_rules sequents = load_theory(stem_rules)['rules'] for sequent in sequents: for cost, seq, plan in compile_full(sequent): programs += [ '', '# using {}'.format(sequent), '# infer '.format(seq), '# cost = '.format(cost), ] plan.program(programs) for event in get_events(sequent): for cost, seq, plan in compile_given(sequent, event): programs += [ '', '# given {}'.format(event), '# using {}'.format(sequent), '# infer {}'.format(seq), '# cost {}'.format(cost), ] plan.program(programs) print '\n'.join(programs)
def test_compile(*filenames): """ Compile rules -> programs for the virtual machine. """ if not filenames: filenames = find_theories() for stem_rules in filenames: programs = ["# {filename}".format(filename=stem_rules)] assert stem_rules[-len(".theory") :] == ".theory", stem_rules sequents = load_theory(stem_rules)["rules"] for sequent in sequents: for cost, seq, plan in compile_full(sequent): programs += ["", "# using {}".format(sequent), "# infer ".format(seq), "# cost = ".format(cost)] plan.program(programs) for event in get_events(sequent): for cost, seq, plan in compile_given(sequent, event): programs += [ "", "# given {}".format(event), "# using {}".format(sequent), "# infer {}".format(seq), "# cost {}".format(cost), ] plan.program(programs) print "\n".join(programs)
def test_compile(*filenames): ''' Compile rules -> C++ ''' for stem_rules in filenames: assert stem_rules[-6:] == '.rules', stem_rules sequents = parser.parse_rules(stem_rules) for sequent in sequents: for cost, strategy in compile_full(sequent): print '\n'.join(strategy.cpp_lines()) for event in get_events(sequent): for cost, strategy in compile_given(sequent, event): print '\n'.join(strategy.cpp_lines())
def write_full_programs(programs, sequents, can_parallelize=True): full_tasks = [] for sequent in sequents: for cost, seq, plan in compiler.compile_full(sequent): full_tasks.append((cost, sequent, seq, plan)) full_tasks.sort() for plan_id, (cost, sequent, seq, plan) in enumerate(full_tasks): poll = can_parallelize and (cost >= MIN_SPLIT_COST) programs += [ '', '# plan {}: cost = {:0.1f}'.format(plan_id, cost), '# using {}'.format(sequent), '# infer {}'.format(seq), ] if poll: programs.append('FOR_BLOCK') plan.program(programs, poll=poll)
def _test_sequent(*args): sequent = Sequent(*args) print '-' * 78 print 'Compiling full search: {0}'.format(sequent) compiles = compile_full(sequent) print_compiles(compiles) full_cost = add_costs(c for (c, _, _) in compiles) incremental_cost = None for event in get_events(sequent): print 'Compiling incremental search given: {0}'.format(event) compiles = compile_given(sequent, event) print_compiles(compiles) if event.args: cost = add_costs(c for (c, _, _) in compiles) if incremental_cost: incremental_cost = add_costs([incremental_cost, cost]) else: incremental_cost = cost print '# full cost =', full_cost, 'incremental cost =', incremental_cost
def measure_sequent(sequent): print '-' * 78 print 'Compiling full search: {0}'.format(sequent) compiles = compile_full(sequent) print_compiles(compiles) full_cost = add_costs(c for (c, _, _) in compiles) incremental_cost = None for event in get_events(sequent): print 'Compiling incremental search given: {0}'.format(event) compiles = compile_given(sequent, event) print_compiles(compiles) if event.args: cost = add_costs(c for (c, _, _) in compiles) if incremental_cost: incremental_cost = add_costs([incremental_cost, cost]) else: incremental_cost = cost else: pass # event is only triggered once, so ignore cost print '# full cost =', full_cost, 'incremental cost =', incremental_cost
def write_full_tasks(code, sequents): full_tasks = [] for sequent in sequents: full_tasks += compiler.compile_full(sequent) full_tasks.sort(key=(lambda (cost, _): cost)) type_count = len(full_tasks) block_size = 64 split = 'if (*iter / {} != block) {{ continue; }}'.format(block_size) min_split_cost = 1.5 # above which we split the outermost for loop unsplit_count = sum(1 for cost, _ in full_tasks if cost < min_split_cost) cases = Code() for i, (cost, strategy) in enumerate(full_tasks): case = Code() strategy.cpp(case, split if cost >= min_split_cost else None) cases( ''' case $index: { // cost = $cost $case } break; ''', index=i, cost=cost, case=wrapindent(case), ).newline() code( ''' $bar // cleanup tasks const size_t g_cleanup_type_count = $type_count; const size_t g_cleanup_block_count = carrier.item_dim() / $block_size + 1; const size_t g_cleanup_task_count = $unsplit_count + ($type_count - $unsplit_count) * g_cleanup_block_count; std::atomic<unsigned long> g_cleanup_type(0); std::atomic<unsigned long> g_cleanup_remaining(0); CleanupProfiler g_cleanup_profiler(g_cleanup_type_count); inline unsigned long next_cleanup_type (const unsigned long & type) { unsigned long next = type < $unsplit_count * g_cleanup_block_count ? type + g_cleanup_block_count : type + 1; return next % (g_cleanup_type_count * g_cleanup_block_count); } void cleanup_tasks_push_all() { g_cleanup_remaining.store(g_cleanup_task_count); } bool cleanup_tasks_try_pop (CleanupTask & task) { unsigned long remaining = 1; while (not g_cleanup_remaining.compare_exchange_weak( remaining, remaining - 1)) { if (remaining == 0) { return false; } } // is this absolutely correct? unsigned long type = 0; while (not g_cleanup_type.compare_exchange_weak( type, next_cleanup_type(type))) { } task.type = type; return true; } void execute (const CleanupTask & task) { const unsigned long type = task.type / g_cleanup_block_count; const unsigned long block = task.type % g_cleanup_block_count; POMAGMA_DEBUG( "executing cleanup task" " type " << (1 + type) << "/" << g_cleanup_type_count << " block " << (1 + block) << "/" << g_cleanup_block_count); CleanupProfiler::Block profiler_block(type); switch (type) { $cases default: POMAGMA_ERROR("bad cleanup type " << type); } } ''', bar=bar, type_count=type_count, unsplit_count=unsplit_count, block_size=block_size, cases=wrapindent(cases, ' '), ).newline()