From 36786e898b00b8e6f8ad77b0965e8938c3493f86 Mon Sep 17 00:00:00 2001 From: Moritz Sallermann Date: Wed, 13 Nov 2019 11:07:16 +0100 Subject: [PATCH] API: Added a method Simulation_N_Shot. Works similar to Simulation_SingleShot but performs multiple iterations. Can reduce overhead of calling multiple singleshots in some applications. --- core/docs/c-api/Simulation.md | 13 ++++++++ core/include/Spirit/Simulation.h | 8 +++++ core/python/spirit/simulation.py | 13 +++++++- core/src/Spirit/Simulation.cpp | 53 +++++++++++++++++--------------- 4 files changed, 61 insertions(+), 26 deletions(-) diff --git a/core/docs/c-api/Simulation.md b/core/docs/c-api/Simulation.md index a0f91a1a3..8bf6f18b4 100644 --- a/core/docs/c-api/Simulation.md +++ b/core/docs/c-api/Simulation.md @@ -160,6 +160,19 @@ Otherwise, nothing will happen. +### Simulation_N_Shot + +```C +void Simulation_N_Shot(State *state, int N, int idx_image=-1, int idx_chain=-1); +``` + +N iterations of a Method + +If `singleshot=true` was passed to `Simulation_..._Start` before, this will perform N iterations. +Otherwise, nothing will happen. + + + ### Simulation_Stop ```C diff --git a/core/include/Spirit/Simulation.h b/core/include/Spirit/Simulation.h index e4e3280dc..0a8fa4a55 100644 --- a/core/include/Spirit/Simulation.h +++ b/core/include/Spirit/Simulation.h @@ -85,6 +85,14 @@ Otherwise, nothing will happen. */ PREFIX void Simulation_SingleShot(State *state, int idx_image=-1, int idx_chain=-1) SUFFIX; +/* +N iterations of a Method + +If `singleshot=true` was passed to `Simulation_..._Start` before, this will perform N iterations. +Otherwise, nothing will happen. +*/ +PREFIX void Simulation_N_Shot(State *state, int N, int idx_image=-1, int idx_chain=-1) SUFFIX; + // Stop a simulation running on an image or chain PREFIX void Simulation_Stop(State *state, int idx_image=-1, int idx_chain=-1) SUFFIX; diff --git a/core/python/spirit/simulation.py b/core/python/spirit/simulation.py index 3023b4cfd..73f53747c 100644 --- a/core/python/spirit/simulation.py +++ b/core/python/spirit/simulation.py @@ -173,7 +173,18 @@ def single_shot(p_state, idx_image=-1, idx_chain=-1): """ spiritlib.wrap_function(_SingleShot, [ctypes.c_void_p(p_state), ctypes.c_int(idx_image), ctypes.c_int(idx_chain)]) - # _SingleShot(ctypes.c_void_p(p_state), ctypes.c_int(idx_image), ctypes.c_int(idx_chain)) + +_N_Shot = _spirit.Simulation_N_Shot +_N_Shot.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int] +_N_Shot.restype = None +def n_shot(p_state, N, idx_image=-1, idx_chain=-1): + """Perform a single iteration. + + In order to use this, a single shot simulation must be running on the corresponding image or chain. + """ + spiritlib.wrap_function(_N_Shot, [ctypes.c_void_p(p_state), ctypes.c_int(N), + ctypes.c_int(idx_image), ctypes.c_int(idx_chain)]) + _Stop = _spirit.Simulation_Stop _Stop.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int] diff --git a/core/src/Spirit/Simulation.cpp b/core/src/Spirit/Simulation.cpp index 45c922aba..0c1c142aa 100644 --- a/core/src/Spirit/Simulation.cpp +++ b/core/src/Spirit/Simulation.cpp @@ -385,8 +385,12 @@ catch( ... ) spirit_handle_exception_api(idx_image, idx_chain); } - void Simulation_SingleShot(State *state, int idx_image, int idx_chain) noexcept +{ + Simulation_N_Shot(state, 1, idx_image, idx_chain); +} + +void Simulation_N_Shot(State *state, int N, int idx_image, int idx_chain) noexcept try { // Fetch correct indices and pointers for image and chain @@ -415,34 +419,33 @@ try if( method->ContinueIterating() && !method->Walltime_Expired(t_current - method->t_start) ) { - // Lock Systems method->Lock(); - - // Pre-iteration hook - method->Hook_Pre_Iteration(); - // Do one single Iteration - method->Iteration(); - // Post-iteration hook - method->Hook_Post_Iteration(); - - // Recalculate FPS - method->t_iterations.pop_front(); - method->t_iterations.push_back(system_clock::now()); - - // Log Output every n_iterations_log steps - bool log = false; - if( method->n_iterations_log > 0 ) - log = method->iteration > 0 && 0 == fmod(method->iteration, method->n_iterations_log); - if( log ) + for(int i=0; istep; - method->Message_Step(); - method->Save_Current(method->starttime, method->iteration, false, false); + // Pre-iteration hook + method->Hook_Pre_Iteration(); + // Do one single Iteration + method->Iteration(); + // Post-iteration hook + method->Hook_Post_Iteration(); + + // Recalculate FPS + method->t_iterations.pop_front(); + method->t_iterations.push_back(system_clock::now()); + + // Log Output every n_iterations_log steps + bool log = false; + if( method->n_iterations_log > 0 ) + log = method->iteration > 0 && 0 == fmod(method->iteration, method->n_iterations_log); + if( log ) + { + ++method->step; + method->Message_Step(); + method->Save_Current(method->starttime, method->iteration, false, false); + } + ++method->iteration; } - - ++method->iteration; - // Unlock systems method->Unlock(); }