ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
tuning.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "tuning.hpp"
23
25#include "communication.hpp"
26#include "errorhandling.hpp"
27#include "integrate.hpp"
29#include "system/System.hpp"
30
32
33#include <boost/mpi/collectives/all_reduce.hpp>
34#include <boost/mpi/collectives/broadcast.hpp>
35
36#include <mpi.h>
37
38#include <algorithm>
39#include <string>
40
41std::string TuningFailed::get_first_error() {
42 using namespace ErrorHandling;
43 auto const queued_warnings = mpi_gather_runtime_errors_all(this_node == 0);
44 auto message = std::string("tuning failed: an exception was thrown while "
45 "benchmarking the integration loop");
46 for (auto const &warning : queued_warnings) {
47 if (warning.level() == RuntimeError::ErrorLevel::ERROR) {
48 message += " (" + warning.what() + ")";
49 break;
50 }
51 }
52 return message;
53}
54
56 if (acc.avg() <= 5 * MPI_Wtick()) {
58 << "Clock resolution is too low to reliably time integration.";
59 }
60 if (acc.sig() >= 0.1 * acc.avg()) {
61 runtimeWarningMsg() << "Statistics of tuning samples is very bad.";
62 }
63}
64
65static void run_full_force_calc(System::System &system, int reuse_forces) {
66 auto const error_code = system.integrate(0, reuse_forces);
67 if (error_code == INTEG_ERROR_RUNTIME) {
68 throw TuningFailed{};
69 }
70}
71
72double benchmark_integration_step(System::System &system, int int_steps) {
74
75 // check if the system can be integrated with the current parameters
77
78 // measure force calculation time
79 for (int i = 0; i < int_steps; i++) {
80 auto const tick = MPI_Wtime();
82 auto const tock = MPI_Wtime();
83 running_average.add_sample((tock - tick));
84 }
85
86 if (this_node == 0) {
87 check_statistics(running_average);
88 }
89
90 /* MPI returns in seconds, returned value should be in ms. */
91 auto retval = 1000. * running_average.avg();
92 boost::mpi::broadcast(::comm_cart, retval, 0);
93 return retval;
94}
95
96/**
97 * \brief Time the integration.
98 * This times the integration and
99 * propagates the system.
100 *
101 * @param system The system to tune.
102 * @param int_steps Number of steps to integrate.
103 * @return Time per integration in ms.
104 */
105static double time_calc(System::System &system, int int_steps) {
106 auto const error_code_init =
108 if (error_code_init == INTEG_ERROR_RUNTIME) {
109 return -1;
110 }
111
112 /* perform force calculation test */
113 auto const tick = MPI_Wtime();
114 auto const error_code = system.integrate(int_steps, INTEG_REUSE_FORCES_NEVER);
115 auto const tock = MPI_Wtime();
116 if (error_code == INTEG_ERROR_RUNTIME) {
117 return -1;
118 }
119
120 /* MPI returns in seconds, returned value should be in ms. */
121 return 1000. * (tock - tick) / int_steps;
122}
123
124namespace System {
125void System::tune_verlet_skin(double min_skin, double max_skin, double tol,
126 int int_steps, bool adjust_max_skin) {
127
128 double a = min_skin;
129 double b = max_skin;
130
131 /* The maximal skin is the remainder from the required cutoff to
132 * the maximal range that can be supported by the cell system, but
133 * never larger than half the box size. */
134 auto const max_permissible_skin = std::min(
135 std::ranges::min(cell_structure->max_cutoff()) - maximal_cutoff(),
136 0.5 * std::ranges::max(box_geo->length()));
137
138 if (adjust_max_skin and max_skin > max_permissible_skin)
139 b = max_permissible_skin;
140
141 while (fabs(a - b) > tol) {
142 cell_structure->set_verlet_skin(a);
143 auto const time_a = time_calc(*this, int_steps);
144
145 cell_structure->set_verlet_skin(b);
146 auto const time_b = time_calc(*this, int_steps);
147
148 if (time_a > time_b) {
149 a = 0.5 * (a + b);
150 } else {
151 b = 0.5 * (a + b);
152 }
153 }
154 cell_structure->set_verlet_skin(0.5 * (a + b));
155}
156} // namespace System
Main system class.
void tune_verlet_skin(double min_skin, double max_skin, double tol, int int_steps, bool adjust_max_skin)
Tune the Verlet skin.
Definition tuning.cpp:125
int integrate(int n_steps, int reuse_forces)
Integrate equations of motion.
Keep running average and variance.
Scalar avg() const
Average of the samples.
Scalar sig() const
Standard deviation of the samples.
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeWarningMsg()
Molecular dynamics integrator.
#define INTEG_ERROR_RUNTIME
Definition integrate.hpp:42
#define INTEG_REUSE_FORCES_CONDITIONALLY
recalculate forces only if Propagation::recalc_forces is set
Definition integrate.hpp:51
#define INTEG_REUSE_FORCES_NEVER
recalculate forces unconditionally (mostly used for timing)
Definition integrate.hpp:49
std::vector< RuntimeError > mpi_gather_runtime_errors_all(bool is_head_node)
Gather messages on main rank.
Various procedures concerning interactions between particles.
static void run_full_force_calc(System::System &system, int reuse_forces)
Definition tuning.cpp:65
static void check_statistics(Utils::Statistics::RunningAverage< double > &acc)
Definition tuning.cpp:55
static double time_calc(System::System &system, int int_steps)
Time the integration.
Definition tuning.cpp:105
double benchmark_integration_step(System::System &system, int int_steps)
Benchmark the integration loop.
Definition tuning.cpp:72