org.knowceans.util
Class ParallelFor

java.lang.Object
  extended by org.knowceans.util.ParallelFor

public abstract class ParallelFor
extends java.lang.Object

ParallelFor implements what in OpenMP is called a parallel for loop, i.e., a loop that executes in parallel on several threads with a barrier (join) at the end. Implementors simply subclass and implement the process() method and make sure the loop() method is not called while a loop of one instance is running. Execution can be stopped using stop(), which completes the iterations currently running. After the last usage of the class, it should be shut down properly using function shutdown() or the hard way using System.exit();

Author:
gregor

Constructor Summary
ParallelFor()
          instantiate with as many threads as there are processors
ParallelFor(int nthreads)
          instantiate a parallel for implementation
 
Method Summary
 void loop(int N)
          Start worker threads and loop through the iterations.
 void loopOnce(int N)
          loops once, then shuts down
static void main(java.lang.String[] args)
           
abstract  void process(int iteration, int thread)
          payload for the for loop
 void shutdown()
          shut down the thread pool after final usage
 void stop()
          stop loop execution
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParallelFor

public ParallelFor()
instantiate with as many threads as there are processors


ParallelFor

public ParallelFor(int nthreads)
instantiate a parallel for implementation

Method Detail

loop

public void loop(int N)
Start worker threads and loop through the iterations. Should never be called while the loop instance is still running.

Parameters:
N -

loopOnce

public void loopOnce(int N)
loops once, then shuts down

Parameters:
N -

process

public abstract void process(int iteration,
                             int thread)
payload for the for loop

Parameters:
iteration - in the for loop
thread - on the machine

stop

public void stop()
stop loop execution


shutdown

public void shutdown()
shut down the thread pool after final usage


main

public static void main(java.lang.String[] args)