Exemple #1
0
  public void runForLoop(
      int loopId, ForLoop forLoop, ChunkProcessorFactory chFactory, int threadNum, boolean nowait) {
    if (!active) {
      throw new IllegalStateException("Unable to run a for loop in inactive state");
    }

    ChunkProcessor chProcessor = forLoops.get(loopId);
    if (chProcessor == null) {
      forLock.lock();
      try {
        chProcessor = forLoops.get(loopId);
        if (chProcessor == null) {
          LoggingUtils.debug(log, "Adding for loop construct with id " + loopId);
          long start = System.currentTimeMillis();
          chProcessor = chFactory.createChunkProcessor();
          log.debug("Creating chunk processor " + (System.currentTimeMillis() - start));
          forLoops.put(loopId, chProcessor);
        }
      } finally {
        forLock.unlock();
      }
    }

    Chunk chunk;
    while ((chunk = chProcessor.getNextChunk(threadNum)) != null) {
      forLoop.execute(chunk);
    }
    if (!nowait) {
      team[threadNum].getActiveTask().barrier(team[threadNum].getLevel());
    }
    LoggingUtils.debug(log, "OMP thread " + threadNum + " finished for loop construct " + loopId);
  }
Exemple #2
0
  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));
  }
Exemple #3
0
  public void runSections(int sectionsId, int threadNum, ParallelTask[] tasks, boolean nowait) {
    if (!active) {
      throw new IllegalStateException("Unable to run a sections construct in inactive state");
    }

    Sections sections = sectionsMap.get(sectionsId);
    if (sections == null) {
      sectionsLock.lock();
      try {
        sections = sectionsMap.get(sectionsId);
        if (sections == null) {
          sections = new Sections(tasks);
          sectionsMap.put(sectionsId, sections);
        }
      } finally {
        sectionsLock.unlock();
      }
    }
    ParallelTask toRun = sections.getNextTask();
    if (toRun != null) {
      LoggingUtils.debug(
          log, "OMP Thread " + threadNum + " executing section from sections " + sectionsId);
      toRun.execute();
    }
    if (!nowait) {
      team[threadNum].getActiveTask().barrier(team[threadNum].getLevel());
    }
  }
Exemple #4
0
  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]);
    }
  }
Exemple #5
0
  public void runSingle(int singleId, int threadNum, ParallelTask task, boolean nowait) {
    if (!active) {
      throw new IllegalStateException("Unable to run a single construct in inactive state");
    }

    if (!singles.contains(singleId)) {

      singleLock.lock();
      try {
        if (!singles.contains(singleId)) {
          singles.add(singleId);
          LoggingUtils.debug(log, "OMP Thread " + threadNum + " executing single task " + singleId);
          task.execute();
        }
      } finally {
        singleLock.unlock();
      }
    }
    if (!nowait) {
      team[threadNum].getActiveTask().barrier(team[threadNum].getLevel());
    }
  }