python - Solving formulas in parallel with z3 -
let's have z3 solver number of asserted constraints satisfiable. let s set of constraints, verify every constraint in s whether formula still satisfiable when adding constraint solver. can done sequentially in such fashion:
results = [] constraint in s: solver.push() solver.add(constraint) results.append(solver.check() == z3.sat) solver.pop() print all(results)
now, parallelize speed things up, i'm not sure how z3.
here attempt. consider following simple example. variables non negative integers , have sum 1. verify whether every variable x can independently made > 0. case; let x = 1 , assign 0 other variables. here possible parallel implementation:
from multiprocessing import pool functools import partial import z3 def parallel_function(f): def easy_parallize(f, sequence): pool = pool(processes=4) result = pool.map(f, sequence) pool.close() pool.join() return result return partial(easy_parallize, f) def check(v): global solver global variables solver.push() solver.add(variables[v] > 0) result = solver.check() == z3.sat solver.pop() return result range = range(1000) solver = z3.solver() variables = [z3.int('x_{}'.format(i)) in range] solver.add([var >= 0 var in variables]) solver.add(z3.sum(variables) == 1) check.parallel = parallel_function(check) results = check.parallel(range) print all(results)
surprisingly works on machine: results sound , faster. however, doubt safe since i'm working on single global solver , can imagine push/pops happen concurrently. there clean/safe way achieve z3py?
indeed, might work first test, not in general. parallel solving on single context not supported , there data races , segfaults later.
in python, context object hidden, users won't have deal it, run things in parallel, need set 1 context per thread , pass right 1 other functions (which implicitly used hidden context before). note expressions, solvers, tactics, etc, depend on 1 particular context, if objects need cross boundary need translate them (see translate(...) in astref , similar).
see multi-threaded z3? , when z3 parallel version reactivated?
Comments
Post a Comment