Parallel Java ForLoop model element
PJForLoop defines a for loop of integer or long index that runs in parallel on a shared memory parallel (SMP) computer
The element encapsulates access to both IntegerForLoop and
LongForLoop
in the Parallel Java library.
Usage
To add a PJForLoop element to your model, drag the icon element to the list of your model elements
and edit it to select the number of processors to use, the index range, the scheduling policy, and to enter the code for each of the parts of your parallel for loop.
The element implements the following basic methods:
- long startAndWait(int nThreads): runs the code in nThreads parallel threads and waits for completion. Returns the time elapsed in milliseconds.
- boolean startAndReturn(int nThreads): executes the code in parallel with nThreads starting in a separate Thread. (That it, the call returns immediately.) Returns true is successfully started.
- int getThreadCount(): returns the number of threads actually used in the last execution.
- long getExecutionTime(): returns the time (in milliseconds) of the last execution. -1 if the region has never been run.
- void resetInitialTime(): resets the initial time to measure the execution time. (The initial time is reset automatically when the startAndReturn method is called.)
- boolean isAlive(): whether the code is currently running.
- void release(): releases the thread created for the element.(The element is released automatically when the simulation quits.)
Example of use
pjForLoop.startAndWait(4); // runs the parallel loop using 4 threads and waits for completion
pjForLoop.startAndWait(0); // runs the parallel loop using the default number of threads
_println ("The loop executed on "+pjForLoop.getThreadCount()+" threads,");
_println ("and took "+pjForLoop.getExecutionTime()+" milliseconds.");