From 8780d5d47af33fc00bb5159c810c3e68827cd43a Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Wed, 20 Apr 2016 12:15:04 +0200 Subject: [PATCH] option for parallelization --- .../typeinference/unify/TypeUnify.java | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java index 60e775369..2e0d70fd1 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnify.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.typeinference.unify; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; @@ -10,6 +11,7 @@ import java.util.List; import java.util.Map.Entry; import java.util.Optional; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; @@ -47,6 +49,8 @@ public class TypeUnify { */ protected IRuleSet rules = new RuleSet(); + protected boolean parallel = true; + /** * Computes all principal type unifiers for a 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 // "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>> secondLevelSet : secondLevelSets) { List>> secondLevelSetList = new ArrayList<>(secondLevelSet); Set>> cartResult = setOps.cartesianProduct(secondLevelSetList); @@ -133,7 +137,7 @@ public class TypeUnify { //System.out.println(result); // Flatten the cartesian product - // TODO parallelisierung möglich + // TODO parallelisierung möglich (scheint sich nicht zu lohnen) Set> eqPrimeSetFlat = new HashSet<>(); for(Set> setToFlatten : eqPrimeSet) { Set buffer = new HashSet<>(); @@ -148,15 +152,33 @@ public class TypeUnify { Set> restartSet = new HashSet<>(); Set> eqPrimePrimeSet = new HashSet<>(); - for(Set eqPrime : eqPrimeSetFlat) { - Optional> eqPrimePrime = rules.subst(eqPrime); + + if(parallel) { + Set> restartSetSync = Collections.synchronizedSet(restartSet); + Set> eqPrimePrimeSetSync = Collections.synchronizedSet(eqPrimePrimeSet); - if (eqPrime.equals(eq)) - eqPrimePrimeSet.add(eqPrime); - else if(eqPrimePrime.isPresent()) - restartSet.add(eqPrimePrime.get()); - else - restartSet.add(eqPrime); + eqPrimeSetFlat.parallelStream().forEach(eqPrime -> { + Optional> eqPrimePrime = rules.subst(eqPrime); + + if (eqPrime.equals(eq)) + eqPrimePrimeSetSync.add(eqPrime); + else if(eqPrimePrime.isPresent()) + restartSetSync.add(eqPrimePrime.get()); + else + restartSetSync.add(eqPrime); + }); + } + else { + for(Set eqPrime : eqPrimeSetFlat) { + Optional> 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 */ // TODO parallelisierung möglich (lohnt sich vermutlich) - for(Set eqss : restartSet) - eqPrimePrimeSet.addAll(this.unify(eqss, fc)); - /*restartSet.parallelStream().forEach( - x -> eqPrimePrimeSet.addAll(unify(x, fc)));*/ + + if(parallel) { + Set> eqPrimePrimeSetSync = Collections.synchronizedSet(eqPrimePrimeSet); + restartSet.parallelStream().forEach( x -> eqPrimePrimeSetSync.addAll(unify(x, fc))); + } + else { + for(Set eqss : restartSet) + eqPrimePrimeSet.addAll(this.unify(eqss, fc)); + } + /* * Step 7: Filter empty sets; */