de.jstacs.classifiers.differentiableSequenceScoreBased
Class AbstractMultiThreadedOptimizableFunction

java.lang.Object
  extended by de.jstacs.algorithms.optimization.DifferentiableFunction
      extended by de.jstacs.classifiers.differentiableSequenceScoreBased.OptimizableFunction
          extended by de.jstacs.classifiers.differentiableSequenceScoreBased.AbstractOptimizableFunction
              extended by de.jstacs.classifiers.differentiableSequenceScoreBased.AbstractMultiThreadedOptimizableFunction
All Implemented Interfaces:
Function
Direct Known Subclasses:
DiffSSBasedOptimizableFunction

public abstract class AbstractMultiThreadedOptimizableFunction
extends AbstractOptimizableFunction

This class enables the user to exploit all CPUs of an computer by using threads. Each thread computes the function for a part of the data. The number of compute threads can be determined in the constructor.

The main goal is on the one hand to hide the complete infrastructure that is used for the multi-threading from the programmer and on the other hand to provide an simple interface in form of few abstract methods.

It is very important for this class that the clone() method (of the used items) works correctly, since each thread works on its own clones.

Author:
Jens Keilwagen

Nested Class Summary
 
Nested classes/interfaces inherited from class de.jstacs.classifiers.differentiableSequenceScoreBased.OptimizableFunction
OptimizableFunction.KindOfParameter
 
Field Summary
protected  double[] params
          This is a pointer for the current parameters.
protected  de.jstacs.classifiers.differentiableSequenceScoreBased.AbstractMultiThreadedOptimizableFunction.Worker[] worker
          The worker threads for computing value and gradient of this AbstractMultiThreadedOptimizableFunction
 
Fields inherited from class de.jstacs.classifiers.differentiableSequenceScoreBased.AbstractOptimizableFunction
cl, clazz, data, freeParams, logClazz, norm, sum, weights
 
Constructor Summary
AbstractMultiThreadedOptimizableFunction(int threads, DataSet[] data, double[][] weights, boolean norm, boolean freeParams)
          The constructor for an multi-threaded instance.
 
Method Summary
 double evaluateFunction(double[] x)
          Evaluates the function at a certain vector (in mathematical sense) x.
protected abstract  void evaluateFunction(int index, int startClass, int startSeq, int endClass, int endSeq)
          This method evaluates the function for a part of the data.
 double[] evaluateGradientOfFunction(double[] x)
          Evaluates the gradient of a function at a certain vector (in mathematical sense) x, i.e., $\nabla f(\underline{x}) = \left(\frac{\partial f(\underline{x})}{\partial x_1},\ldots,\frac{\partial f(\underline{x})}{\partial x_n}\right)$.
protected abstract  void evaluateGradientOfFunction(int index, int startClass, int startSeq, int endClass, int endSeq)
          This method evaluates the gradient of the function for a part of the data.
static int getNumberOfAvailableProcessors()
          This method returns the number of available processors.
 int getNumberOfThreads()
          Returns the number of used threads for evaluating the function and for determining the gradient of the function.
protected abstract  double joinFunction()
          This method joins the partial results that have been computed using evaluateFunction(int, int, int, int, int).
protected abstract  double[] joinGradients()
          This method joins the gradients of each part that have been computed using evaluateGradientOfFunction(int, int, int, int, int).
protected  void prepareThreads()
          Assigns parts of the data to the threads
 void setDataAndWeights(DataSet[] data, double[][] weights)
          This method sets the data set and the sequence weights to be used.
 void setParams(double[] params)
          Sets the current values as parameters.
protected abstract  void setParams(int index)
          This method sets the parameters for thread index
protected abstract  void setThreadIndependentParameters()
          This method allows to set thread independent parameters.
 void stopThreads()
          This method can and should be used to stop all threads if they are not needed any longer.
 
Methods inherited from class de.jstacs.classifiers.differentiableSequenceScoreBased.AbstractOptimizableFunction
getData, getParameters, getParameters, getSequenceWeights
 
Methods inherited from class de.jstacs.classifiers.differentiableSequenceScoreBased.OptimizableFunction
reset
 
Methods inherited from class de.jstacs.algorithms.optimization.DifferentiableFunction
findOneDimensionalMin
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.jstacs.algorithms.optimization.Function
getDimensionOfScope
 

Field Detail

worker

protected de.jstacs.classifiers.differentiableSequenceScoreBased.AbstractMultiThreadedOptimizableFunction.Worker[] worker
The worker threads for computing value and gradient of this AbstractMultiThreadedOptimizableFunction


params

protected double[] params
This is a pointer for the current parameters.

Constructor Detail

AbstractMultiThreadedOptimizableFunction

public AbstractMultiThreadedOptimizableFunction(int threads,
                                                DataSet[] data,
                                                double[][] weights,
                                                boolean norm,
                                                boolean freeParams)
                                         throws IllegalArgumentException
The constructor for an multi-threaded instance.

Parameters:
threads - the number of threads used for evaluating the function and determining the gradient of the function
data - the array of DataSets containing the data that is needed to evaluate the function
weights - the weights for each Sequence in each DataSet of data
norm - the switch for using the normalization (division by the number of sequences)
freeParams - the switch for using only the free parameters
Throws:
IllegalArgumentException - if the number of threads is not positive, the DataSets contain too less Sequence for the number of threads, the number of classes or the dimension of the weights is not correct
Method Detail

getNumberOfAvailableProcessors

public static final int getNumberOfAvailableProcessors()
This method returns the number of available processors. So if no other job is running this is the recommended number of threads.

Returns:
the number of available processors

setDataAndWeights

public void setDataAndWeights(DataSet[] data,
                              double[][] weights)
                       throws IllegalArgumentException
Description copied from class: OptimizableFunction
This method sets the data set and the sequence weights to be used. It also allows to do further preparation for the computation on this data.

Overrides:
setDataAndWeights in class AbstractOptimizableFunction
Parameters:
data - the data sets
weights - the sequence weights for each sequence in each data set
Throws:
IllegalArgumentException - if the data or the weights can not be used

prepareThreads

protected void prepareThreads()
Assigns parts of the data to the threads


evaluateGradientOfFunction

public final double[] evaluateGradientOfFunction(double[] x)
                                          throws DimensionException,
                                                 EvaluationException
Description copied from class: DifferentiableFunction
Evaluates the gradient of a function at a certain vector (in mathematical sense) x, i.e., $\nabla f(\underline{x}) = \left(\frac{\partial f(\underline{x})}{\partial x_1},\ldots,\frac{\partial f(\underline{x})}{\partial x_n}\right)$.

Specified by:
evaluateGradientOfFunction in class DifferentiableFunction
Parameters:
x - the current vector
Returns:
the evaluation of the gradient of a function, has dimension Function.getDimensionOfScope()
Throws:
DimensionException - if dim(x) != n, with $f: \mathbb{R}^n \to \mathbb{R}$
EvaluationException - if there was something wrong during the evaluation of the gradient
See Also:
Function.getDimensionOfScope()

evaluateGradientOfFunction

protected abstract void evaluateGradientOfFunction(int index,
                                                   int startClass,
                                                   int startSeq,
                                                   int endClass,
                                                   int endSeq)
This method evaluates the gradient of the function for a part of the data.

Parameters:
index - the index of the part
startClass - the index of the start class
startSeq - the index of the start sequence
endClass - the index of the end class (inclusive)
endSeq - the index of the end sequence (exclusive)

joinGradients

protected abstract double[] joinGradients()
                                   throws EvaluationException
This method joins the gradients of each part that have been computed using evaluateGradientOfFunction(int, int, int, int, int).

Returns:
the gradient
Throws:
EvaluationException - if the gradient could not be evaluated properly

evaluateFunction

public final double evaluateFunction(double[] x)
                              throws DimensionException,
                                     EvaluationException
Description copied from interface: Function
Evaluates the function at a certain vector (in mathematical sense) x.

Parameters:
x - the current vector
Returns:
the evaluation of the function
Throws:
DimensionException - if dim(x) != n, with $f: \mathbb{R}^n \to \mathbb{R}$
EvaluationException - if there was something wrong during the evaluation of the function

evaluateFunction

protected abstract void evaluateFunction(int index,
                                         int startClass,
                                         int startSeq,
                                         int endClass,
                                         int endSeq)
                                  throws EvaluationException
This method evaluates the function for a part of the data.

Parameters:
index - the index of the part
startClass - the index of the start class
startSeq - the index of the start sequence
endClass - the index of the end class (inclusive)
endSeq - the index of the end sequence (exclusive)
Throws:
EvaluationException - if the gradient could not be evaluated properly

joinFunction

protected abstract double joinFunction()
                                throws EvaluationException,
                                       DimensionException
This method joins the partial results that have been computed using evaluateFunction(int, int, int, int, int).

Returns:
the value of the function
Throws:
EvaluationException - if the gradient could not be evaluated properly
DimensionException - if the parameters could not be set

setParams

public final void setParams(double[] params)
                     throws DimensionException
Description copied from class: OptimizableFunction
Sets the current values as parameters.

Specified by:
setParams in class OptimizableFunction
Parameters:
params - the current values
Throws:
DimensionException - if the dimension of the current values does not match with the dimension of the internal parameters

setThreadIndependentParameters

protected abstract void setThreadIndependentParameters()
                                                throws DimensionException
This method allows to set thread independent parameters. It also allows to check some constraints as for instance the dimension of the parameters.

Throws:
DimensionException - if the dimension of the parameters is wrong
See Also:
setParams(double[])

setParams

protected abstract void setParams(int index)
                           throws DimensionException
This method sets the parameters for thread index

Parameters:
index - the index of the thread
Throws:
DimensionException - if the parameters could not be set
See Also:
params

stopThreads

public final void stopThreads()
This method can and should be used to stop all threads if they are not needed any longer.


getNumberOfThreads

public final int getNumberOfThreads()
Returns the number of used threads for evaluating the function and for determining the gradient of the function.

Returns:
the number of used threads for evaluating the function and for determining the gradient of the function