public interface CalculationTaskRunner extends AutoCloseable
This interface is the lower-level counterpart to CalculationRunner.
It provides the ability to calculate results based on CalculationTasks.
Unless you need to optimize, the CalculationRunner is a simpler entry point.
The purpose of the runner is to produce a grid of results, with a row for each target
and a column for each measure. The targets and columns that define the grid of results
are passed in using an instance of CalculationTasks.
The CalculationTasks instance is obtained using
static factory method.
It consists of a list of CalculationTask instances, where each task instance
corresponds to a single cell in the grid of results. When the CalculationTasks
instance is created for a set of trades and measures some one-off initialization is performed.
Providing access to the instance allows the initialization to occur once, which could
be a performance optimization if many different calculations are performed with the
same set of trades and measures.
Once obtained, the CalculationTasks instance may be used to calculate results.
The four "calculate" methods handle the combination of single versus scenario market data,
and synchronous versus asynchronous.
A calculation runner is typically obtained using the static methods on this interface. The instance contains an executor thread-pool, thus care should be taken to ensure the thread-pool is correctly managed. For example, try-with-resources could be used:
try (CalculationTaskRunner runner = CalculationTaskRunner.ofMultiThreaded()) {
// use the runner
}
| Modifier and Type | Method and Description |
|---|---|
Results |
calculate(CalculationTasks tasks,
MarketData marketData,
ReferenceData refData)
Performs calculations for a single set of market data.
|
void |
calculateAsync(CalculationTasks tasks,
MarketData marketData,
ReferenceData refData,
CalculationListener listener)
Performs calculations asynchronously for a single set of market data,
invoking a listener as each calculation completes.
|
Results |
calculateMultiScenario(CalculationTasks tasks,
ScenarioMarketData marketData,
ReferenceData refData)
Performs calculations for multiple scenarios, each with a different set of market data.
|
void |
calculateMultiScenarioAsync(CalculationTasks tasks,
ScenarioMarketData marketData,
ReferenceData refData,
CalculationListener listener)
Performs calculations asynchronously for multiple scenarios, each with a different set of market data,
invoking a listener as each calculation completes.
|
void |
close()
Closes any resources held by the component.
|
static CalculationTaskRunner |
of(ExecutorService executor)
Creates a calculation task runner capable of performing calculations, specifying the executor.
|
static CalculationTaskRunner |
ofMultiThreaded()
Creates a standard multi-threaded calculation task runner capable of performing calculations.
|
static CalculationTaskRunner ofMultiThreaded()
This factory creates an executor basing the number of threads on the number of available processors. It is recommended to use try-with-resources to manage the runner:
try (CalculationTaskRunner runner = CalculationTaskRunner.ofMultiThreaded()) {
// use the runner
}
static CalculationTaskRunner of(ExecutorService executor)
It is the callers responsibility to manage the life-cycle of the executor.
executor - the executor to useResults calculate(CalculationTasks tasks, MarketData marketData, ReferenceData refData)
This returns a grid of results based on the specified tasks and market data. The grid will contain a row for each target and a column for each measure.
If the thread is interrupted while this method is blocked, calculations will stop
and a result returned indicating the failed tasks, with the interrupted flag set.
For additional control, use calculateAsync(CalculationTasks, MarketData, ReferenceData, CalculationListener).
tasks - the calculation tasks to invokemarketData - the market data to be used in the calculationsrefData - the reference data to be used in the calculationsvoid calculateAsync(CalculationTasks tasks, MarketData marketData, ReferenceData refData, CalculationListener listener)
This method requires the listener to assemble the results, but it can be much more memory efficient when calculating aggregate results. If the individual results are discarded after they are incorporated into the aggregate they can be garbage collected.
tasks - the calculation tasks to invokemarketData - the market data to be used in the calculationsrefData - the reference data to be used in the calculationslistener - listener that is invoked when individual results are calculatedResults calculateMultiScenario(CalculationTasks tasks, ScenarioMarketData marketData, ReferenceData refData)
This returns a grid of results based on the specified tasks and market data. The grid will contain a row for each target and a column for each measure. Each cell will contain multiple results, one for each scenario.
If the thread is interrupted while this method is blocked, calculations will stop
and a result returned indicating the failed tasks, with the interrupted flag set.
For additional control, use
calculateMultiScenarioAsync(CalculationTasks, ScenarioMarketData, ReferenceData, CalculationListener).
tasks - the calculation tasks to invokemarketData - the market data to be used in the calculationsrefData - the reference data to be used in the calculationsvoid calculateMultiScenarioAsync(CalculationTasks tasks, ScenarioMarketData marketData, ReferenceData refData, CalculationListener listener)
This method requires the listener to assemble the results, but it can be much more memory efficient when calculating aggregate results. If the individual results are discarded after they are incorporated into the aggregate they can be garbage collected.
tasks - the calculation tasks to invokemarketData - the market data to be used in the calculationsrefData - the reference data to be used in the calculationslistener - listener that is invoked when individual results are calculatedvoid close()
If the component holds an ExecutorService, this method will typically
call ExecutorService.shutdown().
close in interface AutoCloseableCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.