How-to: Timers in samurai#

In this how-to guide, we will show you how to use timers in samurai. Timers are useful for measuring the execution time of different parts of your code, which can help you identify performance bottlenecks and optimize your simulations.

Get default timers output#

By default, samurai provides built-in timers that measure the execution time of various components, such as mesh refinement, field updates, and I/O operations. To print the default timers output, you can use the --timers command line option when running your simulation. This will print a summary of the timers to the console at the end of the simulation.

Using timers#

You can also create your own timers to measure specific parts of your code. samurai provides a simple timer class that you can use to start and stop timers. Here is an example of how to use timers in your code:

#include <samurai/algorithm.hpp>
#include <samurai/box.hpp>
#include <samurai/field.hpp>
#include <samurai/mr/mesh.hpp>
#include <samurai/samurai.hpp>
#include <samurai/timers.hpp>

int main(int argc, char** argv)
{
    static constexpr std::size_t dim = 2;

    samurai::initialize("Custom timer example", argc, argv);

    samurai::Box<double, dim> box({0.0, 0.0}, {1.0, 1.0});
    auto config = samurai::mesh_config<dim>().min_level(2).max_level(5);
    auto mesh   = samurai::mra::make_mesh(box, config);

    auto field = samurai::make_scalar_field<double>("u", mesh);

    samurai::times::timers.start("init field");

    samurai::for_each_cell(mesh,
                           [&](const auto& cell)
                           {
                               auto x = cell.center(0);
                               auto y = cell.center(1);

                               field[cell] = std::exp(-((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) * 20.0);
                           });

    samurai::times::timers.stop("init field");

    samurai::finalize();
    return 0;
}

In this example, we create a 2D multi-resolution mesh and a scalar field on the mesh. We then use the times::timers.start("init field") and times::timers.stop("init field") functions to start and stop a timer named “init field” around the loop that initializes the field values. This will measure the time taken to initialize the field.

Note

Make sure to include the header file samurai/timers.hpp to use the timer functionality.