From 69402677f787f1c63f594f0acbfdbae39d9fcf9c Mon Sep 17 00:00:00 2001 From: NoName11234 <47484268+NoName11234@users.noreply.github.com> Date: Mon, 29 Apr 2024 22:14:24 +0200 Subject: [PATCH] implemented UnifyTaskModelParallel --- .../typeinference/unify/TypeUnify.java | 21 +++++---- .../typeinference/unify/TypeUnify2Task.java | 17 +++---- .../typeinference/unify/TypeUnifyTask.java | 44 +++---------------- .../typeinference/unify/UnifyTaskModel.java | 18 -------- src/test/java/UnifyTest.java | 4 +- 5 files changed, 25 insertions(+), 79 deletions(-) delete mode 100644 src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyTaskModel.java diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java index f988235..8212489 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java @@ -28,9 +28,10 @@ public class TypeUnify { * @param cons * @return */ - public Set> unify(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { - TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); + public Set> unify(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + usedTasks.setPool(pool); + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret); pool.invoke(unifyTask); Set> res = unifyTask.join(); try { @@ -54,9 +55,10 @@ public class TypeUnify { * @param ret * @return */ - public UnifyResultModel unifyAsync(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { - TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); + public UnifyResultModel unifyAsync(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + usedTasks.setPool(pool); + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret); pool.invoke(unifyTask); return ret; } @@ -72,10 +74,11 @@ public class TypeUnify { * @param ret * @return */ - public UnifyResultModel unifyParallel(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { - TypeUnifyTask unifyTask = //new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); - new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks, statistics); + public UnifyResultModel unifyParallel(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + usedTasks.setPool(pool); + TypeUnifyTask unifyTask = //new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); + new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, statistics); pool.invoke(unifyTask); Set> res = unifyTask.join(); try { @@ -108,8 +111,8 @@ public class TypeUnify { * @param cons * @return */ - public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { - TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret, usedTasks); + public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret); unifyTask.statistics = statistics; Set> res = unifyTask.compute(); try { diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java index 61d6ce8..d0742ea 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java @@ -23,14 +23,14 @@ public class TypeUnify2Task extends TypeUnifyTask { TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, - IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks, + IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, Set methodSignatureConstraintUebergabe, Writer statistics) { - this(setToFlatten, eq, oderConstraints, nextSetElement, fc, parallel, logFile, log, rekTiefe, urm, usedTasks, methodSignatureConstraintUebergabe ); + this(setToFlatten, eq, oderConstraints, nextSetElement, fc, parallel, logFile, log, rekTiefe, urm, methodSignatureConstraintUebergabe ); } - public TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks, Set methodSignatureConstraintUebergabe) { - super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, usedTasks); + public TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, Set methodSignatureConstraintUebergabe) { + super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm); this.setToFlatten = setToFlatten; this.nextSetElement = nextSetElement; this.methodSignatureConstraintUebergabe = methodSignatureConstraintUebergabe; @@ -53,14 +53,7 @@ public class TypeUnify2Task extends TypeUnifyTask { */ //writeLog("xxx"); //noOfThread--; - synchronized (usedTasks) { - if (this.myIsCancelled()) { - return new HashSet<>(); - } - else { - return res; - } - } + return res; } public void closeLogFile() { diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 9efd728..24d3968 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -125,10 +125,6 @@ public class TypeUnifyTask extends RecursiveTask>> { static Integer noShortendElements = 0; - Boolean myIsCanceled = false; - - volatile UnifyTaskModel usedTasks; - static Writer statistics; public TypeUnifyTask() { @@ -150,11 +146,11 @@ public class TypeUnifyTask extends RecursiveTask>> { */ //statistics - public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks, Writer statistics) { - this(eq,oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, usedTasks); + public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, Writer statistics) { + this(eq,oderConstraints, fc, parallel, logFile, log, rekTiefe, urm); this.statistics = statistics; } - public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) { + public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm) { synchronized (this) { if(statistics==null){ statistics = new NullWriter(); @@ -207,8 +203,6 @@ public class TypeUnifyTask extends RecursiveTask>> { rules = new RuleSet(logFile); this.rekTiefeField = rekTiefe; this.urm = urm; - this.usedTasks = usedTasks; - this.usedTasks.add(this); } } @@ -244,13 +238,6 @@ public class TypeUnifyTask extends RecursiveTask>> { } } */ - void myCancel(Boolean b) { - myIsCanceled = true; - } - - public boolean myIsCancelled() { - return myIsCanceled; - } protected Set> compute() { if (one) { @@ -279,14 +266,7 @@ public class TypeUnifyTask extends RecursiveTask>> { throw new DebugException("Unresolved constraints: " + res.toString()); //return new HashSet<>(); } else { - synchronized (usedTasks) { - if (this.myIsCancelled()) { - return new HashSet<>(); - } - else { - return res; - } - } + return res; } } /* @@ -319,12 +299,6 @@ public class TypeUnifyTask extends RecursiveTask>> { //.collect(Collectors.toCollection(HashSet::new))); - synchronized (usedTasks) { - if (this.myIsCancelled()) { - return new HashSet<>(); - } - } - rekTiefe++; nOfUnify++; writeLog(nOfUnify.toString() + " Unifikation: " + eq.toString()); @@ -506,12 +480,6 @@ public class TypeUnifyTask extends RecursiveTask>> { // .collect(Collectors.toCollection(HashSet::new)); //Muss auskommentiert werden, wenn computeCartesianRecursive ENDE - synchronized (usedTasks) { - if (this.myIsCancelled()) { - return new HashSet<>(); - } - } - Set> eqPrimePrimeSet = new HashSet<>(); Set forks = new HashSet<>(); @@ -819,7 +787,7 @@ public class TypeUnifyTask extends RecursiveTask>> { Set> newElemsOrig = new HashSet<>(elems); List>> newOderConstraintsOrig = new ArrayList<>(oderConstraints); newElemsOrig.add(a); - TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm, usedTasks, methodSignatureConstraint); + TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm, methodSignatureConstraint); //Überprüfen, ob das Set bereits berechnet wurde forkOrig.fork(); @@ -834,7 +802,7 @@ public class TypeUnifyTask extends RecursiveTask>> { Set> newElems = new HashSet<>(elems); List>> newOderConstraints = new ArrayList<>(oderConstraints); newElems.add(nSaL); - TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm, usedTasks, new HashSet<>(methodSignatureConstraint)); + TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm, new HashSet<>(methodSignatureConstraint)); forks.add(fork); //Überprüfen, ob das Set bereits berechnet wurde fork.fork(); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyTaskModel.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyTaskModel.java deleted file mode 100644 index e60054f..0000000 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyTaskModel.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.dhbwstuttgart.typeinference.unify; - -import java.util.ArrayList; - -public class UnifyTaskModel { - - ArrayList usedTasks = new ArrayList<>(); - - public synchronized void add(TypeUnifyTask t) { - usedTasks.add(t); - } - - public synchronized void cancel() { - for(TypeUnifyTask t : usedTasks) { - t.myCancel(true); - } - } -} diff --git a/src/test/java/UnifyTest.java b/src/test/java/UnifyTest.java index 6f75a89..84b0d50 100644 --- a/src/test/java/UnifyTest.java +++ b/src/test/java/UnifyTest.java @@ -3,7 +3,7 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.TypeUnify; import de.dhbwstuttgart.typeinference.unify.UnifyResultModel; -import de.dhbwstuttgart.typeinference.unify.UnifyTaskModel; +import de.dhbwstuttgart.typeinference.unify.UnifyTaskModelParallel; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.*; import org.apache.commons.io.output.NullWriter; @@ -77,7 +77,7 @@ public class UnifyTest { TypeUnify unifyAlgo = new TypeUnify(); ConstraintSet cons = new ConstraintSet<>(); UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure); - UnifyTaskModel tasks = new UnifyTaskModel(); + UnifyTaskModelParallel tasks = new UnifyTaskModelParallel(); Set> solution = unifyAlgo.unify(undConstraints, oderConstraints, finiteClosure, new NullWriter(), false, urm, tasks); System.out.println(solution.size()); //System.out.println(solution);