removed the busy wait from requestEval and added a Semaphore instead => much more CPU friendly and no unnecessary sleeps anymore

This commit is contained in:
Michael de Paly 2011-06-20 16:11:45 +00:00
parent 2ae16f0f43
commit 06d8f87da6

View File

@ -1,6 +1,7 @@
package eva2.server.go.problems; package eva2.server.go.problems;
import java.util.BitSet; import java.util.BitSet;
import java.util.concurrent.Semaphore;
import eva2.gui.BeanInspector; import eva2.gui.BeanInspector;
@ -33,6 +34,7 @@ public class MatlabEvalMediator {
volatile boolean quit = false; volatile boolean quit = false;
volatile Object optSolution = null; volatile Object optSolution = null;
volatile Object[] optSolSet = null; volatile Object[] optSolSet = null;
volatile Semaphore requests;
int runID=-1; int runID=-1;
volatile MatlabProblem mp = null; volatile MatlabProblem mp = null;
// no good: even when waiting for only 1 ms the Matlab execution time increases by a factor of 5-10 // no good: even when waiting for only 1 ms the Matlab execution time increases by a factor of 5-10
@ -46,6 +48,7 @@ public class MatlabEvalMediator {
*/ */
public MatlabEvalMediator(int threadSleepTime) { public MatlabEvalMediator(int threadSleepTime) {
sleepTime=threadSleepTime; sleepTime=threadSleepTime;
requests=new Semaphore(0);
} }
/** /**
@ -53,6 +56,7 @@ public class MatlabEvalMediator {
*/ */
public MatlabEvalMediator() { public MatlabEvalMediator() {
sleepTime=0; sleepTime=0;
requests=new Semaphore(0);
} }
public void setMatlabProblem(MatlabProblem theMP) { public void setMatlabProblem(MatlabProblem theMP) {
@ -81,7 +85,7 @@ public class MatlabEvalMediator {
if (!(x instanceof BitSet)) System.err.println("Error, requesting evaluation for invalid data type! " + question.getClass()); if (!(x instanceof BitSet)) System.err.println("Error, requesting evaluation for invalid data type! " + question.getClass());
} }
// logMPAndSysOut("Synch requesting A requestEval " + getState()); // logMPAndSysOut("Synch requesting A requestEval " + getState());
synchronized(requesting) { synchronized(requesting) { //MdP
// logMPAndSysOut(" in synch requesting A requestEval " + getState()); // logMPAndSysOut(" in synch requesting A requestEval " + getState());
if (requesting) { if (requesting) {
String msg="Warning: already in requesting state when request arrived!"; String msg="Warning: already in requesting state when request arrived!";
@ -91,8 +95,10 @@ public class MatlabEvalMediator {
requesting = true; requesting = true;
// logMPAndSysOut("-- Requesting eval for " + BeanInspector.toString(x) + ", req state is " + requesting + "\n"); // logMPAndSysOut("-- Requesting eval for " + BeanInspector.toString(x) + ", req state is " + requesting + "\n");
} }
// logMPAndSysOut("Synch requesting A done " + getState()); // logMPAndSysOut("Synch requesting A done " + getState());
int k=0; int mod=25; /*int k=0; int mod=25;
while (requesting && !quit) { while (requesting && !quit) {
// wait for matlab to answer the question // wait for matlab to answer the question
if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) { if (sleepTime > 0) try { Thread.sleep(sleepTime); } catch(Exception e) {
@ -106,6 +112,12 @@ public class MatlabEvalMediator {
if (mod <=0) mod=Integer.MAX_VALUE; if (mod <=0) mod=Integer.MAX_VALUE;
} }
}*/
try {
requests.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
logMP("-- Requesting done\n"); logMP("-- Requesting done\n");
// matlab is finished, answer is here // matlab is finished, answer is here
@ -207,6 +219,7 @@ public class MatlabEvalMediator {
} }
answer = y; answer = y;
requesting = false; // answer is finished, break request loop requesting = false; // answer is finished, break request loop
requests.release();
logMP("-- setAnswer: " + BeanInspector.toString(y) + ", req state is " + requesting + "\n"); logMP("-- setAnswer: " + BeanInspector.toString(y) + ", req state is " + requesting + "\n");
} }
// logMPAndSysOut("Synch requesting B done " + getState()); // logMPAndSysOut("Synch requesting B done " + getState());