Skip to content

Commit

Permalink
API: Added a method Simulation_N_Shot.
Browse files Browse the repository at this point in the history
Works similar to Simulation_SingleShot but performs multiple iterations. Can reduce overhead of calling multiple singleshots in some applications.
  • Loading branch information
Moritz Sallermann committed Nov 13, 2019
1 parent ad38c8c commit 36786e8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
13 changes: 13 additions & 0 deletions core/docs/c-api/Simulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions core/include/Spirit/Simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 12 additions & 1 deletion core/python/spirit/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
53 changes: 28 additions & 25 deletions core/src/Spirit/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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; i<N; i++)
{
++method->step;
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();
}
Expand Down

0 comments on commit 36786e8

Please sign in to comment.