Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that Run and Cleanup are called after Prepare #898

Merged
merged 3 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions nestkernel/simulation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ nest::SimulationManager::initialize()
Time::reset_resolution();
clock_.calibrate();

prepared_ = false;
simulating_ = false;
simulated_ = false;
exit_on_user_signal_ = false;
Expand Down Expand Up @@ -378,6 +379,13 @@ nest::SimulationManager::prepare()
{
assert( kernel().is_initialized() );

if ( prepared_ )
{
std::string msg = "Prepare called twice.";
LOG( M_ERROR, "SimulationManager::prepare", msg );
throw KernelException();
}

if ( inconsistent_state_ )
{
throw KernelException(
Expand Down Expand Up @@ -430,6 +438,7 @@ nest::SimulationManager::prepare()
* kernel().connection_manager.get_min_delay();
kernel().music_manager.enter_runtime( tick );
}
prepared_ = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check at the top of the function that ! prepared_ and otherwise throw an exception like in the other checks.

}

void
Expand Down Expand Up @@ -487,6 +496,13 @@ nest::SimulationManager::run( Time const& t )
{
assert_valid_simtime( t );

if ( not prepared_ )
{
std::string msg = "Run called without calling Prepare.";
LOG( M_ERROR, "SimulationManager::run", msg );
throw KernelException();
}

to_do_ += t.get_steps();
to_do_total_ = to_do_;

Expand Down Expand Up @@ -542,6 +558,13 @@ nest::SimulationManager::run( Time const& t )
void
nest::SimulationManager::cleanup()
{
if ( not prepared_ )
{
std::string msg = "Cleanup called without calling Prepare.";
LOG( M_ERROR, "SimulationManager::cleanup", msg );
throw KernelException();
}

if ( not simulated_ )
{
return;
Expand All @@ -561,6 +584,7 @@ nest::SimulationManager::cleanup()
}

kernel().node_manager.finalize_nodes();
prepared_ = false;
}

void
Expand Down
4 changes: 3 additions & 1 deletion nestkernel/simulation_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ class SimulationManager : public ManagerInterface
delay to_step_; //!< update clock_+from_step<=T<clock_+to_step_
timeval t_slice_begin_; //!< Wall-clock time at the begin of a time slice
timeval t_slice_end_; //!< Wall-clock time at the end of time slice
long t_real_; //!< Accumunated wall-clock time spent simulating (in us)
long t_real_; //!< Accumulated wall-clock time spent simulating (in us)
bool prepared_; //!< Indicates whether the SimulationManager is in a prepared
//!< state
bool simulating_; //!< true if simulation in progress
bool simulated_; //!< indicates whether the SimulationManager has already been
//!< simulated for sometime
Expand Down
64 changes: 64 additions & 0 deletions testsuite/regressiontests/issue-659.sli
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* issue-659.sli
*
* This file is part of NEST.
*
* Copyright (C) 2004 The NEST Initiative
*
* NEST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* NEST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NEST. If not, see <http://www.gnu.org/licenses/>.
*
*/

/* BeginDocumentation

Name: testsuite::issue-659 Check that we get an error if trying to call Run or Cleanup without calling Prepare first.

Synopsis: (issue-659) run -> NEST exits if test fails

Author: Håkon Mørk, 2018-03-07
*/

(unittest) run
/unittest using

M_ERROR setverbosity

{
% Run without prepare.
ResetKernel
10 Run
} fail_or_die

{
% Cleanup without prepare.
ResetKernel
Cleanup
} fail_or_die

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And prepare after prepare test.

{
% Prepare twice.
ResetKernel
Prepare
Prepare
} fail_or_die

{
% Run after cleanup, without prepare.
ResetKernel
Prepare
10 Run
Cleanup
10 Run
} fail_or_die