edu.jhu.cs.cisst.algorithms.optimize.fmg.parallel
Class Parallelizer

java.lang.Object
  extended by edu.jhu.cs.cisst.algorithms.optimize.fmg.parallel.Parallelizer
All Implemented Interfaces:
Parallelizable
Direct Known Subclasses:
CalcResidualParallelizer, DampedJacobiSmoothParallelizer, RestrictByStencilParallelizer, SolveThroughSmootherParallelizer

public class Parallelizer
extends java.lang.Object
implements Parallelizable

A class that simplifies the task of scheduling one thread per processor of a SMP machine and calling a parallel algorithm in each of these threads, so that together these threads solve a common problem in parallel.

Author:
Gerald Loeffler (Gerald.Loeffler@univie.ac.at)

Constructor Summary
Parallelizer()
          construct using the default implementation of runParallel() and using the default number of threads.
Parallelizer(int numThreads)
          construct given a certain number of threads and using the default implementation of runParallel().
Parallelizer(Parallelizable target)
          construct given a target object and using the default number of threads.
Parallelizer(Parallelizable target, int numThreads)
          construct given a target object and a certain number of threads.
 
Method Summary
 void finished()
          called by the run() method of each thread as it finishes.
static IntRange1D partition(IntRange1D totalRange, int myNum, int totalNum)
          partition a 1-dimensional range of (consecutive!) numbers (usually values for a loop variable) into a given number of portions and return one of these portions as a range.
 void runParallel(int myNum, int totalNum)
          the default implementation of the parallel algorithm, which does nothing.
static void setDefaultNumberOfThreads(int defaultNum)
          set the default number of threads for all objects of this type.
 void start()
          start the Parallelizer by creating the requested number of threads, calling the runParallel() method of the target object (of type Parallelizable) in each thread and waiting for all threads to finish before returning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Parallelizer

public Parallelizer()
construct using the default implementation of runParallel() and using the default number of threads.

See Also:
setDefaultNumberOfThreads(int)

Parallelizer

public Parallelizer(int numThreads)
construct given a certain number of threads and using the default implementation of runParallel().

Parameters:
numThreads - the number of threads that will be created and put to work on the problem ( > 0)

Parallelizer

public Parallelizer(Parallelizable target)
construct given a target object and using the default number of threads.

Parameters:
target - interface to the parallel algorithm that will be called in each thread
See Also:
setDefaultNumberOfThreads(int)

Parallelizer

public Parallelizer(Parallelizable target,
                    int numThreads)
construct given a target object and a certain number of threads.

Parameters:
target - interface to the parallel algorithm that will be called in each thread
numThreads - the number of threads that will be created and put to work on the problem ( > 0)
Method Detail

start

public void start()
start the Parallelizer by creating the requested number of threads, calling the runParallel() method of the target object (of type Parallelizable) in each thread and waiting for all threads to finish before returning.

All threads will be put into a newly created thread group.


runParallel

public void runParallel(int myNum,
                        int totalNum)
the default implementation of the parallel algorithm, which does nothing.

If a target was supplied when creating this object, this method is not consulted.

Specified by:
runParallel in interface Parallelizable
Parameters:
myNum - the my num
totalNum - the total num

setDefaultNumberOfThreads

public static void setDefaultNumberOfThreads(int defaultNum)
set the default number of threads for all objects of this type.

Before calling this method, the default number of threads defaults to 1 (corresponding to serial execution), because there is no system-independent way of querying the number of processors on a machine from Java.

Parameters:
defaultNum - the default number of threads to create from now on ( > 0)

partition

public static IntRange1D partition(IntRange1D totalRange,
                                   int myNum,
                                   int totalNum)
partition a 1-dimensional range of (consecutive!) numbers (usually values for a loop variable) into a given number of portions and return one of these portions as a range.

When the total workload of an algorithm can be evenly mapped to the given range of numbers, and this method is supplied with the total number of threads working on the algorithm, it can determine the percentage of the total workload that should be carried by each processor for optimal load balancing.

Parameters:
totalRange - the consecutive range of integer values that should be partitioned
myNum - the number of the thread for which the partition is sought (0 <= myNum < totalNum)
totalNum - the total number of threads working on this problem ( > 0)
Returns:
the int range1 d

finished

public void finished()
called by the run() method of each thread as it finishes.