use of edu.rit.util.LongRange in project ffx by mjschnie.
the class WorkerRegion method execute.
/**
* Execute a worker for loop within this worker region. For further
* information, see class {@linkplain WorkerLongStrideForLoop}. The loop
* index goes from <TT>first</TT> (inclusive) to <TT>last</TT> (inclusive)
* in steps of <TT>stride</TT>. The stride must be positive. If
* <TT>first</TT> is greater than <TT>last</TT>, then no loop iterations are
* performed.
* <P>
* <I>Note:</I> Either all threads in the worker team must call the
* <TT>execute()</TT> method with identical arguments, or none of the
* threads must call the <TT>execute()</TT> method.
*
* @param first First loop index.
* @param last Last loop index.
* @param stride Loop index stride, >= 1.
* @param theLoop Worker for loop.
* @exception IllegalArgumentException (unchecked exception) Thrown if
* <TT>stride</TT> < 1.
* @exception NullPointerException (unchecked exception) Thrown if
* <TT>theLoop</TT> is null.
* @exception IllegalStateException (unchecked exception) Thrown if no
* worker team is executing this worker region.
* @exception Exception Thrown if one of <TT>theLoop</TT>'s methods throws
* an exception.
* @throws java.lang.Exception if any.
*/
public final void execute(long first, long last, long stride, WorkerLongStrideForLoop theLoop) throws Exception {
// Verify preconditions.
if (stride <= 0) {
throw new IllegalArgumentException("WorkerRegion.execute(): Stride = " + stride + " illegal");
}
if (theLoop == null) {
throw new NullPointerException("WorkerRegion.execute(): Worker for loop is null");
}
if (myTeam == null) {
throw new IllegalStateException("WorkerRegion.execute(): No parallel team executing");
}
try {
// Record parallel team.
theLoop.myTeam = this.myTeam;
// Get current parallel team thread.
WorkerTeamThread currentThread = getCurrentThread();
int w = currentThread.myIndex;
// Do master or worker thread processing.
LongRange range = new LongRange(first, last, stride);
if (w == -1) {
theLoop.masterExecute(range);
} else {
theLoop.workerExecute(w, range);
}
} finally {
// Forget parallel team.
theLoop.myTeam = null;
}
}
use of edu.rit.util.LongRange in project ffx by mjschnie.
the class ParallelRegion method execute.
/**
* Execute a parallel for loop within this parallel region. For further
* information, see class {@linkplain LongForLoop}. The loop index goes from
* <TT>first</TT> (inclusive) to <TT>last</TT> (inclusive) in steps of +1.
* If <TT>first</TT> is greater than <TT>last</TT>, then no loop iterations
* are performed. At the end of the parallel for loop, the parallel team
* threads encounter a barrier, and their behavior depends on the given
* {@linkplain BarrierAction}.
* <P>
* <I>Note:</I> Either all threads in the parallel team must call the
* <TT>execute()</TT> method with identical arguments, or none of the
* threads must call the <TT>execute()</TT> method.
*
* @param first First loop index.
* @param last Last loop index.
* @param theLoop Parallel for loop.
* @param action Barrier action.
* @exception NullPointerException (unchecked exception) Thrown if
* <TT>theLoop</TT> is null. Thrown if
* <TT>action</TT> is null.
* @exception IllegalStateException (unchecked exception) Thrown if no
* parallel team is executing this parallel region.
* @exception Exception Thrown if one of <TT>theLoop</TT>'s methods throws
* an exception.
* @throws java.lang.Exception if any.
*/
public final void execute(long first, long last, LongForLoop theLoop, BarrierAction action) throws Exception {
// Verify preconditions.
if (theLoop == null) {
throw new NullPointerException("ParallelRegion.execute(): Parallel for loop is null");
}
if (action == null) {
throw new NullPointerException("ParallelRegion.execute(): Barrier action is null");
}
if (myTeam == null) {
throw new IllegalStateException("ParallelRegion.execute(): No parallel team executing");
}
try {
// Record parallel team.
theLoop.myTeam = this.myTeam;
// Get current parallel team thread.
ParallelTeamThread currentThread = getCurrentThread();
int currentIndex = currentThread.myIndex;
// Do top-of-parallel-construct processing.
LongSchedule schedule = null;
if (currentThread.arriveAtParallelConstruct()) {
// each team thread.
try {
schedule = theLoop.schedule();
schedule.commonStart(myTeam.K, new LongRange(first, last));
for (ParallelTeamThread thread : myTeam.myThread) {
thread.setLongSchedule(schedule);
}
} catch (Throwable exc) {
for (ParallelTeamThread thread : myTeam.myThread) {
thread.setConstructException(exc);
}
}
}
// Get the shared parallel for loop schedule object.
schedule = currentThread.getLongSchedule();
theLoop.mySchedule = schedule;
// Prepare to catch exceptions thrown by the parallel for loop body.
Throwable runException = null;
try {
// Perform per-thread initialization.
theLoop.start();
// Repeatedly get and process a chunk of loop iterations.
LongRange chunk;
while ((chunk = schedule.commonNext(currentIndex)) != null) {
theLoop.commonRun(chunk.lb(), chunk.ub());
}
// Perform per-thread finalization.
theLoop.finish();
} catch (Throwable exc) {
runException = exc;
schedule.myBreak = true;
}
// Barrier synchronization.
action.doBarrier(currentThread);
// Propagate any exception thrown by the run() method.
ParallelTeam.rethrow(runException);
} finally {
// Forget parallel team.
theLoop.myTeam = null;
theLoop.mySchedule = null;
}
}
use of edu.rit.util.LongRange in project ffx by mjschnie.
the class DynamicLongSchedule method next.
/**
* {@inheritDoc}
*
* Obtain the next chunk of iterations for the given thread index. If there
* are more iterations, a range object is returned whose lower bound, upper
* bound, and stride specify the chunk of iterations to perform. The
* returned range object's stride is the same as that given to the
* <TT>start()</TT> method. The returned range object's lower bound and
* upper bound are contained within the range given to the <TT>start()</TT>
* method. If there are no more iterations, null is returned.
* <P>
* The <TT>next()</TT> method is called by multiple parallel team threads in
* the Parallel Java middleware. The <TT>next()</TT> method must be multiple
* thread safe.
*/
public LongRange next(int theThreadIndex) {
for (; ; ) {
long oldN1 = N1.get();
LongRange result = myLoopRange.chunk(oldN1, N2);
long N = result.length();
if (N == 0) {
return null;
}
long newN1 = oldN1 + N;
if (N1.compareAndSet(oldN1, newN1)) {
return result;
}
}
}
use of edu.rit.util.LongRange in project ffx by mjschnie.
the class FixedLongSchedule method next.
/**
* {@inheritDoc}
*
* Obtain the next chunk of iterations for the given thread index. If there
* are more iterations, a range object is returned whose lower bound, upper
* bound, and stride specify the chunk of iterations to perform. The
* returned range object's stride is the same as that given to the
* <TT>start()</TT> method. The returned range object's lower bound and
* upper bound are contained within the range given to the <TT>start()</TT>
* method. If there are no more iterations, null is returned.
* <P>
* The <TT>next()</TT> method is called by multiple parallel team threads in
* the Parallel Java middleware. The <TT>next()</TT> method must be multiple
* thread safe.
*/
public LongRange next(int theThreadIndex) {
LongRange chunk = myChunk[theThreadIndex];
myChunk[theThreadIndex] = null;
return chunk;
}
use of edu.rit.util.LongRange in project ffx by mjschnie.
the class GuidedLongSchedule method next.
/**
* {@inheritDoc}
*
* Obtain the next chunk of iterations for the given thread index. If there
* are more iterations, a range object is returned whose lower bound, upper
* bound, and stride specify the chunk of iterations to perform. The
* returned range object's stride is the same as that given to the
* <TT>start()</TT> method. The returned range object's lower bound and
* upper bound are contained within the range given to the <TT>start()</TT>
* method. If there are no more iterations, null is returned.
* <P>
* The <TT>next()</TT> method is called by multiple parallel team threads in
* the Parallel Java middleware. The <TT>next()</TT> method must be multiple
* thread safe.
*/
public LongRange next(int theThreadIndex) {
for (; ; ) {
long oldN1 = N1.get();
LongRange result = myLoopRange.chunk(oldN1, Math.max(N2, (myLoopRangeLength - oldN1) / two_K));
long N = result.length();
if (N == 0) {
return null;
}
long newN1 = oldN1 + N;
if (N1.compareAndSet(oldN1, newN1)) {
return result;
}
}
}