option for parallelization

This commit is contained in:
Florian Steurer 2016-04-20 12:15:04 +02:00
parent 863bfa40d3
commit 8780d5d47a

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.typeinference.unify;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.LinkedList; import java.util.LinkedList;
@ -10,6 +11,7 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
@ -47,6 +49,8 @@ public class TypeUnify {
*/ */
protected IRuleSet rules = new RuleSet(); protected IRuleSet rules = new RuleSet();
protected boolean parallel = true;
/** /**
* Computes all principal type unifiers for a set of constraints. * Computes all principal type unifiers for a set of constraints.
* @param eq The set of constraints * @param eq The set of constraints
@ -110,7 +114,7 @@ public class TypeUnify {
// Sub cartesian products of the second level (pattern matched) sets // Sub cartesian products of the second level (pattern matched) sets
// "the big (x)" // "the big (x)"
// TODO Optimierungsmöglichkeit: Parallelisierung der Schleife möglich // TODO Optimierungsmöglichkeit: Parallelisierung der Schleife möglich (scheint sich nicht zu lohnen)
for(Set<Set<Set<UnifyPair>>> secondLevelSet : secondLevelSets) { for(Set<Set<Set<UnifyPair>>> secondLevelSet : secondLevelSets) {
List<Set<Set<UnifyPair>>> secondLevelSetList = new ArrayList<>(secondLevelSet); List<Set<Set<UnifyPair>>> secondLevelSetList = new ArrayList<>(secondLevelSet);
Set<List<Set<UnifyPair>>> cartResult = setOps.cartesianProduct(secondLevelSetList); Set<List<Set<UnifyPair>>> cartResult = setOps.cartesianProduct(secondLevelSetList);
@ -133,7 +137,7 @@ public class TypeUnify {
//System.out.println(result); //System.out.println(result);
// Flatten the cartesian product // Flatten the cartesian product
// TODO parallelisierung möglich // TODO parallelisierung möglich (scheint sich nicht zu lohnen)
Set<Set<UnifyPair>> eqPrimeSetFlat = new HashSet<>(); Set<Set<UnifyPair>> eqPrimeSetFlat = new HashSet<>();
for(Set<Set<UnifyPair>> setToFlatten : eqPrimeSet) { for(Set<Set<UnifyPair>> setToFlatten : eqPrimeSet) {
Set<UnifyPair> buffer = new HashSet<>(); Set<UnifyPair> buffer = new HashSet<>();
@ -148,15 +152,33 @@ public class TypeUnify {
Set<Set<UnifyPair>> restartSet = new HashSet<>(); Set<Set<UnifyPair>> restartSet = new HashSet<>();
Set<Set<UnifyPair>> eqPrimePrimeSet = new HashSet<>(); Set<Set<UnifyPair>> eqPrimePrimeSet = new HashSet<>();
for(Set<UnifyPair> eqPrime : eqPrimeSetFlat) {
Optional<Set<UnifyPair>> eqPrimePrime = rules.subst(eqPrime); if(parallel) {
Set<Set<UnifyPair>> restartSetSync = Collections.synchronizedSet(restartSet);
Set<Set<UnifyPair>> eqPrimePrimeSetSync = Collections.synchronizedSet(eqPrimePrimeSet);
if (eqPrime.equals(eq)) eqPrimeSetFlat.parallelStream().forEach(eqPrime -> {
eqPrimePrimeSet.add(eqPrime); Optional<Set<UnifyPair>> eqPrimePrime = rules.subst(eqPrime);
else if(eqPrimePrime.isPresent())
restartSet.add(eqPrimePrime.get()); if (eqPrime.equals(eq))
else eqPrimePrimeSetSync.add(eqPrime);
restartSet.add(eqPrime); else if(eqPrimePrime.isPresent())
restartSetSync.add(eqPrimePrime.get());
else
restartSetSync.add(eqPrime);
});
}
else {
for(Set<UnifyPair> eqPrime : eqPrimeSetFlat) {
Optional<Set<UnifyPair>> eqPrimePrime = rules.subst(eqPrime);
if (eqPrime.equals(eq))
eqPrimePrimeSet.add(eqPrime);
else if(eqPrimePrime.isPresent())
restartSet.add(eqPrimePrime.get());
else
restartSet.add(eqPrime);
}
} }
/* /*
@ -164,10 +186,16 @@ public class TypeUnify {
* b) Build the union over everything * b) Build the union over everything
*/ */
// TODO parallelisierung möglich (lohnt sich vermutlich) // TODO parallelisierung möglich (lohnt sich vermutlich)
for(Set<UnifyPair> eqss : restartSet)
eqPrimePrimeSet.addAll(this.unify(eqss, fc)); if(parallel) {
/*restartSet.parallelStream().forEach( Set<Set<UnifyPair>> eqPrimePrimeSetSync = Collections.synchronizedSet(eqPrimePrimeSet);
x -> eqPrimePrimeSet.addAll(unify(x, fc)));*/ restartSet.parallelStream().forEach( x -> eqPrimePrimeSetSync.addAll(unify(x, fc)));
}
else {
for(Set<UnifyPair> eqss : restartSet)
eqPrimePrimeSet.addAll(this.unify(eqss, fc));
}
/* /*
* Step 7: Filter empty sets; * Step 7: Filter empty sets;
*/ */