org.knowceans.topics.cgen
Class ParallelFor

java.lang.Object
  extended by org.knowceans.topics.cgen.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 Heinrich, semaphore approach by Bernd Amend

Field Summary
protected  boolean isStopping
          stop flag
protected  java.util.concurrent.atomic.AtomicInteger iteration
          current item index
protected  int nthreads
          number of threads = number of processors / cores
(package private) static long start
           
(package private) static java.util.concurrent.atomic.AtomicInteger test
           
protected  java.util.concurrent.ExecutorService threadpool
          pool of threads spread over processors
 
Constructor Summary
ParallelFor()
          instantiate with as many threads as there are processors and block-wise execution
ParallelFor(int nthreads)
          number of threads and mode: 1 = serial, negative = element-wise, positive = block-wise execution
 
Method Summary
 void loop(int range)
          Loop through all iterations.
 void loopOnce(int N)
          loops once, then shuts down
static void main(java.lang.String[] args)
           
abstract  void process(int iteration, int thread)
          processing method
 void shutdown()
          shut down the thread pool after final usage
 void stop()
          stop loop execution
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nthreads

protected int nthreads
number of threads = number of processors / cores


iteration

protected java.util.concurrent.atomic.AtomicInteger iteration
current item index


threadpool

protected java.util.concurrent.ExecutorService threadpool
pool of threads spread over processors


isStopping

protected boolean isStopping
stop flag


test

static java.util.concurrent.atomic.AtomicInteger test

start

static long start
Constructor Detail

ParallelFor

public ParallelFor()
instantiate with as many threads as there are processors and block-wise execution


ParallelFor

public ParallelFor(int nthreads)
number of threads and mode: 1 = serial, negative = element-wise, positive = block-wise execution

Parameters:
nthreads -
Method Detail

loop

public void loop(int range)
Loop through all iterations. Should never be called while the loop instance is still running.

Parameters:
range -

shutdown

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


loopOnce

public void loopOnce(int N)
              throws java.lang.InterruptedException
loops once, then shuts down

Parameters:
N -
Throws:
java.lang.InterruptedException

stop

public void stop()
stop loop execution


process

public abstract void process(int iteration,
                             int thread)
processing method

Parameters:
iteration -
thread -

main

public static void main(java.lang.String[] args)
                 throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException