Swarm-NG
1.1
|
A “monitor” class is responsible for determining when the system state should be logged and when the GPU should cease integrating a given system.
If the monitor determines that logging is needed, then the system’s current state is written to a buffer in the GPU global device memory.
Let's examine the tutorial included in this distribution src/tutorials/tutorial_monitor.hpp. In this tutorial, we write a monitor that examines the distance of bodies to the origin. For planetary systems with barycenter or star at origin, this is generally means that the planet is is considered ejected and is no longer not considered part of the planetary system.
If any planet is too far from origin, planet's information is written to a log and the system will be disabled. Disabling a system (by setting the system state to disabled) prevents the system from advancing in time until the state is set back to active. Usually the user application should examine the system and decide if the system should be re-activated.
Let's take a look at the code, first comes the preamble:
Similar to a propagator, the parameters that need to be speficied in the configuration files should be stored in a separate struct because the main class is a template.
For our simple monitor we only need to read in the maximum allowed distance of planet to origin.
Notice that the parameter is optional, if it is not provided, we use a default value of POSITIVE_INFINITY, meaning that by default there is no restriction.
Constructor
Now the defining class of the monitor begins. The class should work with any implementation of logging system (There are two in Swarm-NG: host log and GPU log). So the log_t class is provided as a template parameter.
The parameter struct should be typedefed as params so the generic integrator can find it.
These references are necessary for the monitor to function _params is our parameters, _sys is a reference to the system we are working on, and _log is a reference to the object the performs all the logging.
These variable are provided in the constructor.
Default constructor
The monitor is implemented as a function object. It has a default function: to monitor the system. When it is called like a function with the system relative thread ID, it should perform the test and make changes as necessary. The parameter thread_in_system is provided for multi-threaded monitors. For single-threaded monitors We just use the thread 0 and don't do anything for other threads.
The plan is simple, we test the system to see if there is any ejection (more details below). If there is any, we need to take action, otherwise our work is done We take two actions:
The developer is free choose to do any of these two, or both. Here we show both actions.
For the test, we write a loop over all the bodies and use the accessor method distance_to_origin to get the value. If the distance is greater than the provided parameter then we break out of the loop and report that there is an ejection.
Otherwise, the test returns false.
This concludes the implementation of the monitor
The following three lines are just closing braces for class and namespace
To use the monitor, we should write a plugin (or change any plugin to use our monitor.
Here we examine src/tutorials/tutorial_monitor_plugin.cu for an example:
First we should include the source files that define implementation of
Some types need to be imported.
We define the plugin in one line. The inner-working of the plugin system uses static initialization of object with constructors. We do not need to worry about that. We just use the integrator_plugin_initializer template as required. The template parameter to he integrator_plugin_initializer is Our integrator, composed of TutorialPropagator, TutorialMonitor<device_log> (our monitor uses a GPU log object (device_log) since we are making a GPU integrator and GravitationAccJerk (TutorialPropagator uses jerk(third derivative of position as well as acceleration).
The two strings are just a name and a description. Choosing a name is important since the only way to access this plugin is through that name.
To use this plugin, you should set the configuration parameter integrator=tutorial before creating the integrator. for example
swarm --defaults integrate integrator=tutorial
For complete listing of these two files look at src/tutorials/tutorial_monitor.hpp and src/tutorials/tutorial_monitor_plugin.cu