Fix Multiprocessing on Windows (#27)

This commit is contained in:
Brandon Rozek 2024-12-10 18:39:03 -05:00 committed by GitHub
commit 9f80fb8bba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 12 deletions

5
vsp.py
View file

@ -98,14 +98,11 @@ Subalgebra 1: {set_to_str(self.subalgebra1)}
Subalgebra 2: {set_to_str(self.subalgebra2)}
"""
def has_vsp(model: Model, interpretation: Dict[Operation, ModelFunction]) -> VSP_Result:
def has_vsp(model: Model, impfunction: ModelFunction, mconjunction: Optional[ModelFunction] = None, mdisjunction: Optional[ModelFunction] = None) -> VSP_Result:
"""
Checks whether a model has the variable
sharing property.
"""
impfunction = interpretation[Implication]
mconjunction = interpretation.get(Conjunction)
mdisjunction = interpretation.get(Disjunction)
top = find_top(model.carrier_set, mconjunction, mdisjunction)
bottom = find_bottom(model.carrier_set, mconjunction, mdisjunction)

View file

@ -1,12 +1,10 @@
#!/usr/bin/env python3
from os import cpu_count
import argparse
import multiprocessing
import multiprocessing as mp
from parse_magic import (
SourceFile,
parse_matrices
)
from logic import Conjunction, Disjunction, Implication
from parse_magic import SourceFile, parse_matrices
from vsp import has_vsp, VSP_Result
if __name__ == "__main__":
@ -24,11 +22,21 @@ if __name__ == "__main__":
solutions = parse_matrices(SourceFile(data_file))
print(f"Parsed {len(solutions)} matrices")
# NOTE: When subprocess gets spawned, the logical operations will
# have a different memory address than what's expected in interpretation.
# This will make it so that we can pass the model functions directly instead.
solutions_expanded = []
for model, interpretation in solutions:
impfunction = interpretation[Implication]
mconjunction = interpretation.get(Conjunction)
mdisjunction = interpretation.get(Disjunction)
solutions_expanded.append((model, impfunction, mconjunction, mdisjunction))
num_has_vsp = 0
with multiprocessing.Pool(processes=max(cpu_count() - 2, 1)) as pool:
with mp.Pool(processes=max(cpu_count() - 2, 1)) as pool:
results = [
pool.apply_async(has_vsp, (model, interpretation,))
for model, interpretation in solutions
pool.apply_async(has_vsp, (model, impfunction, mconjunction, mdisjunction,))
for model, impfunction, mconjunction, mdisjunction in solutions_expanded
]
for i, result in enumerate(results):