From 38d44817564d024241c534802ae96f1f35d1dee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Wed, 16 Jan 2019 22:57:24 +0100 Subject: [PATCH] modified: ../../../main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java Es muss noch geklaert werden, wann ein neuer Thread generiert werden darf. modified: ../../resources/bytecode/javFiles/MatrixOP.jav --- .../typeinference/unify/TypeUnifyTask.java | 144 ++++++++++++++++-- .../resources/bytecode/javFiles/MatrixOP.jav | 2 +- 2 files changed, 130 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index c08ace4f..5c52c237 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -56,12 +56,13 @@ public class TypeUnifyTask extends RecursiveTask>> { private boolean printtag = false; Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"/test/logFiles/log" geschrieben werden soll? - /* + /** * Fuer die Threads */ private static int noOfThread = 0; private int thNo; protected boolean one = false; + Integer MaxNoOfThreads = 4; public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/"; FileWriter logFile; @@ -141,7 +142,7 @@ public class TypeUnifyTask extends RecursiveTask>> { this.log = log; rules = new RuleSet(logFile); this.rekTiefeField = rekTiefe; - noOfThread++; + synchronized (this) { noOfThread++; } thNo = noOfThread; } @@ -790,23 +791,34 @@ public class TypeUnifyTask extends RecursiveTask>> { Set a = null; while (nextSetasList.size() > 0) { //(nextSetasList.size() != 0) { Set a_last = a; - List> nextSetasListRest= new ArrayList<>(); + List> nextSetasListRestMax = new ArrayList<>(); + List> nextSetasListRestMin = new ArrayList<>(); + List> nextSetasListRestOder = new ArrayList<>(); if (variance == 1) { a = oup.max(nextSetasList.iterator()); nextSetasList.remove(a); - nextSetasListRest = new ArrayList<>(nextSetasList); - Iterator> nextSetasListItRest = new ArrayList>(nextSetasListRest).iterator(); + nextSetasListRestMax = new ArrayList<>(nextSetasList); + Iterator> nextSetasListItRest = new ArrayList>(nextSetasListRestMax).iterator(); while (nextSetasListItRest.hasNext()) { Set a_next = nextSetasListItRest.next(); if (//a.equals(a_next) || (oup.compare(a, a_next) == 1)) { - nextSetasListRest.remove(a_next); + nextSetasListRestMax.remove(a_next); } } } else if (variance == -1) { a = oup.min(nextSetasList.iterator()); nextSetasList.remove(a); + nextSetasListRestMin = new ArrayList<>(nextSetasList); + Iterator> nextSetasListItRest = new ArrayList>(nextSetasListRestMin).iterator(); + while (nextSetasListItRest.hasNext()) { + Set a_next = nextSetasListItRest.next(); + if (//a.equals(a_next) || + (oup.compare(a, a_next) == -1)) { + nextSetasListRestMin.remove(a_next); + } + } } else if (variance == 0 || variance == 2) { a = nextSetasList.remove(0); @@ -843,7 +855,7 @@ public class TypeUnifyTask extends RecursiveTask>> { //writeLog("Vor unify2 Aufruf: " + elems.toString()); Set> res = new HashSet<>(); Set>> add_res = new HashSet<>(); - if(parallel && (variance == 1)) { + if(parallel && (variance == 1) && noOfThread <= MaxNoOfThreads) { /* elems.add(a); TypeUnify2Task fork = new TypeUnify2Task(elems, eq, fc, parallel, logFile, log); @@ -864,8 +876,8 @@ public class TypeUnifyTask extends RecursiveTask>> { forks.add(forkOrig); forkOrig.fork(); - while (!nextSetasListRest.isEmpty()) { - Set nSaL = nextSetasListRest.remove(0); + while (!nextSetasListRestMax.isEmpty()) { + Set nSaL = nextSetasListRestMax.remove(0); Set newEq = new HashSet<>(eq); Set> newElems = new HashSet<>(elems); List>> newOderConstraints = new ArrayList<>(oderConstraints); @@ -878,13 +890,70 @@ public class TypeUnifyTask extends RecursiveTask>> { res = forkOrig.join(); for(TypeUnifyTask fork : forks) { Set> fork_res = fork.join(); + synchronized (this) { noOfThread--; }; add_res.add(fork_res); } - } - else { + } else { + if(parallel && (variance == -1) && noOfThread <= MaxNoOfThreads) { + Set forks = new HashSet<>(); + Set newEqOrig = new HashSet<>(eq); + Set> newElemsOrig = new HashSet<>(elems); + List>> newOderConstraintsOrig = new ArrayList<>(oderConstraints); + newElemsOrig.add(a); + TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, fc, parallel, logFile, log, rekTiefe); + forks.add(forkOrig); + forkOrig.fork(); + + while (!nextSetasListRestMin.isEmpty()) { + Set nSaL = nextSetasListRestMin.remove(0); + Set newEq = new HashSet<>(eq); + Set> newElems = new HashSet<>(elems); + List>> newOderConstraints = new ArrayList<>(oderConstraints); + newElems.add(nSaL); + TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, fc, parallel, logFile, log, rekTiefe); + forks.add(fork); + fork.fork(); + } + //res = unify2(elems, eq, fc, parallel); + res = forkOrig.join(); + for(TypeUnifyTask fork : forks) { + Set> fork_res = fork.join(); + synchronized (this) { noOfThread--; }; + add_res.add(fork_res); + } + } else { + if(parallel && (variance == 2) && noOfThread <= MaxNoOfThreads) { + Set forks = new HashSet<>(); + Set newEqOrig = new HashSet<>(eq); + Set> newElemsOrig = new HashSet<>(elems); + List>> newOderConstraintsOrig = new ArrayList<>(oderConstraints); + newElemsOrig.add(a); + TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, fc, parallel, logFile, log, rekTiefe); + forks.add(forkOrig); + forkOrig.fork(); + + while (!nextSetasListRestOder.isEmpty()) { + Set nSaL = nextSetasListRestOder.remove(0); + Set newEq = new HashSet<>(eq); + Set> newElems = new HashSet<>(elems); + List>> newOderConstraints = new ArrayList<>(oderConstraints); + newElems.add(nSaL); + TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, fc, parallel, logFile, log, rekTiefe); + forks.add(fork); + fork.fork(); + } + //res = unify2(elems, eq, fc, parallel); + res = forkOrig.join(); + for(TypeUnifyTask fork : forks) { + Set> fork_res = fork.join(); + synchronized (this) { noOfThread--; }; + add_res.add(fork_res); + }} + else { + parallel = false; //Wenn MaxNoOfThreads erreicht ist, sequentiell weiterarbeiten elems.add(a); //PL 2019-01-16 muss das wirklich hin steht schon in Zeile 833 res = unify2(elems, eq, oderConstraints, fc, parallel, rekTiefe); - } + }}} if (!isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result)) { //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen result = res; @@ -1032,6 +1101,27 @@ public class TypeUnifyTask extends RecursiveTask>> { } } else { if (variance == -1) { + if (parallel) { + for (Set> par_res : add_res) { + if (!isUndefinedPairSetSet(par_res) && isUndefinedPairSetSet(result)) { + //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen + result = par_res; + if (par_res.iterator().next() instanceof WildcardType) { + System.out.println(""); + } + } + else { + if ((isUndefinedPairSetSet(par_res) && isUndefinedPairSetSet(result)) + || (!isUndefinedPairSetSet(par_res) && !isUndefinedPairSetSet(result)) + || result.isEmpty()) { + //alle Fehlerfaelle und alle korrekten Ergebnis jeweils adden + writeLog("RESADD:" + result.toString() + " " + par_res.toString()); + result.addAll(par_res); + } + } + } + break; + } System.out.println(""); writeLog("a: " + rekTiefe + " variance: " + variance + a.toString()); while (nextSetasListIt.hasNext()) { @@ -1047,10 +1137,32 @@ public class TypeUnifyTask extends RecursiveTask>> { } } } - else if (variance == 0) { + else { if (variance == 0) { writeLog("a: " + rekTiefe + " variance: " + variance + a.toString()); - break; - } + break; } + else { if (variance == 2) { + if (parallel) { + for (Set> par_res : add_res) { + if (!isUndefinedPairSetSet(par_res) && isUndefinedPairSetSet(result)) { + //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen + result = par_res; + if (par_res.iterator().next() instanceof WildcardType) { + System.out.println(""); + } + } + else { + if ((isUndefinedPairSetSet(par_res) && isUndefinedPairSetSet(result)) + || (!isUndefinedPairSetSet(par_res) && !isUndefinedPairSetSet(result)) + || result.isEmpty()) { + //alle Fehlerfaelle und alle korrekten Ergebnis jeweils adden + writeLog("RESADD:" + result.toString() + " " + par_res.toString()); + result.addAll(par_res); + } + } + } + break; + } + }}} writeLog("a: " + rekTiefe + " variance: " + variance + a.toString()); } } @@ -1944,6 +2056,8 @@ public class TypeUnifyTask extends RecursiveTask>> { if (log) { try { logFile.write("Thread no.:" + thNo + "\n"); + logFile.write("noOfThread:" + noOfThread + "\n"); + logFile.write("parallel:" + parallel + "\n"); logFile.write(str+"\n\n"); logFile.flush(); diff --git a/src/test/resources/bytecode/javFiles/MatrixOP.jav b/src/test/resources/bytecode/javFiles/MatrixOP.jav index 828a270b..c78c42c7 100644 --- a/src/test/resources/bytecode/javFiles/MatrixOP.jav +++ b/src/test/resources/bytecode/javFiles/MatrixOP.jav @@ -1,6 +1,6 @@ import java.util.Vector; import java.lang.Integer; -//import java.lang.Byte; +import java.lang.Byte; import java.lang.Boolean; public class MatrixOP extends Vector> {