public ParallelRegion(int threadNum, NativeThreadPool<OMPThread> pool) { long start = System.currentTimeMillis(); if (threadNum < 1) { throw new IllegalArgumentException("Thread number must be > 0"); } this.pool = pool; team = new OMPThread[threadNum]; finalBarrier = new CyclicBarrier(threadNum); // Check if already inside a parallel region OMPThread master = OMPEngine.getInstance().tryGetOmpThread(); if (master != null) { parent = master.getActiveRegion(); levelNumber = parent.getLevelNumber() + 1; team[0] = master; team[0].setLevel(new Level(master.getLevel(), 0, this)); } else { team[0] = new OMPThread(new Level(0, this), pool); } for (int i = 1; i < threadNum; i++) { team[i] = new OMPThread(new Level(i, this), pool); } LoggingUtils.debug(log, "Initializing parallel region " + (System.currentTimeMillis() - start)); }
public void runParallel(ParallelTask task) { long start = System.currentTimeMillis(); if (active) { throw new IllegalStateException("Unable to run already active parallel region"); } for (OMPThread t : team) { t.setRootTask(TaskFactory.newTiedTask(task)); } active = true; for (int i = 1; i < team.length; i++) { pool.executeTask(team[i]); } LoggingUtils.debug(log, "Starting tasks " + (System.currentTimeMillis() - start)); team[0].run(); start = System.currentTimeMillis(); if (!tasks.isEmpty()) { throw new OMPRuntimeException( "Parallel region finished bu there are some tasks waiting to execute!"); } active = false; barriers.clear(); forLoops.clear(); singles.clear(); pool.kill(); LoggingUtils.debug(log, "Killing region " + (System.currentTimeMillis() - start)); if (levelNumber > 1) { team[0].setLevel(team[0].getLevel().getParent()); OMPEngine.getInstance().putJavaOmpThreadRelation(team[0]); } }