Skip to content

Commit

Permalink
Write celer-sim results as 'null' when they're disabled (#1223)
Browse files Browse the repository at this point in the history
* Add step timer to disable gettime calls
* Write 'null' time and track counts when they're disabled
  • Loading branch information
sethrj committed May 7, 2024
1 parent 12c9dbe commit 50109fe
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 30 deletions.
2 changes: 2 additions & 0 deletions app/celer-sim/RunnerInput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace app
//---------------------------------------------------------------------------//
/*!
* Input for a single run.
*
* TODO for v1.0: unify these names
*/
struct RunnerInput
{
Expand Down
44 changes: 30 additions & 14 deletions app/celer-sim/RunnerOutput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ void RunnerOutput::output(JsonPimpl* j) const
#if CELERITAS_USE_JSON
using json = nlohmann::json;

auto obj = json::object();

auto active = json::array();
auto alive = json::array();
auto initializers = json::array();
Expand Down Expand Up @@ -73,23 +71,41 @@ void RunnerOutput::output(JsonPimpl* j) const
step_times.push_back(event.step_times);
}
}
obj["_index"] = {"event", "step"};
obj["active"] = std::move(active);
obj["alive"] = std::move(alive);
obj["initializers"] = std::move(initializers);
obj["num_track_slots"] = std::move(num_track_slots);
obj["num_step_iterations"] = std::move(num_step_iterations);
obj["num_steps"] = std::move(num_steps);
obj["num_aborted"] = std::move(num_aborted);
obj["max_queued"] = std::move(max_queued);
obj["time"] = {

if (active.empty())
{
// Track count output is disabled
active = nullptr;
alive = nullptr;
initializers = nullptr;
}

if (step_times.empty())
{
// Step time output is disabled
step_times = nullptr;
}

auto times = json::object({
{"steps", std::move(step_times)},
{"actions", result_.action_times},
{"total", result_.total_time},
{"setup", result_.setup_time},
{"warmup", result_.warmup_time},
};
obj["num_streams"] = result_.num_streams;
});

auto obj = json::object(
{{"_index", json::array({"event", "step"})},
{"active", std::move(active)},
{"alive", std::move(alive)},
{"initializers", std::move(initializers)},
{"num_track_slots", std::move(num_track_slots)},
{"num_step_iterations", std::move(num_step_iterations)},
{"num_steps", std::move(num_steps)},
{"num_aborted", std::move(num_aborted)},
{"max_queued", std::move(max_queued)},
{"num_streams", result_.num_streams},
{"time", std::move(times)}});

j->obj = std::move(obj);
#else
Expand Down
67 changes: 67 additions & 0 deletions app/celer-sim/StepTimer.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celer-sim/StepTimer.hh
//---------------------------------------------------------------------------//
#pragma once

#include <vector>

#include "corecel/sys/Stopwatch.hh"

namespace celeritas
{
namespace app
{
//---------------------------------------------------------------------------//
/*!
* Optionally append a time at every call.
*/
class StepTimer
{
public:
//!@{
//! \name Type aliases
using VecDbl = std::vector<double>;
//!@}

public:
// Construct with a pointer to the times being appended, possibly null
explicit inline StepTimer(VecDbl* times);

// If enabled, push back the time and reset the timer
inline void operator()();

private:
VecDbl* times_;
Stopwatch get_step_time_;
};

//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Construct with a pointer to the times being appended.
*
* If this is null, the stopwatch will not be enabled.
*/
StepTimer::StepTimer(VecDbl* times) : times_{times} {}

//---------------------------------------------------------------------------//
/*!
* Push back the time and reset the timer, if requested.
*/
void StepTimer::operator()()
{
if (times_)
{
times_->push_back(get_step_time_());
get_step_time_ = {};
}
}

//---------------------------------------------------------------------------//
} // namespace app
} // namespace celeritas
18 changes: 6 additions & 12 deletions app/celer-sim/Transporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
#include "corecel/io/Logger.hh"
#include "corecel/io/ScopedTimeLog.hh"
#include "corecel/sys/ScopedSignalHandler.hh"
#include "corecel/sys/Stopwatch.hh"
#include "celeritas/Types.hh"
#include "celeritas/global/CoreParams.hh"
#include "celeritas/global/Stepper.hh"
#include "celeritas/global/detail/ActionSequence.hh"
#include "celeritas/grid/VectorUtils.hh"
#include "celeritas/phys/Model.hh"

#include "StepTimer.hh"

namespace celeritas
{
namespace app
Expand Down Expand Up @@ -113,17 +114,15 @@ auto Transporter<M>::operator()(SpanConstPrimary primaries)
CELER_LOG_LOCAL(status)
<< "Transporting " << primaries.size() << " primaries";

Stopwatch get_step_time;
StepTimer record_step_time{store_step_times_ ? &result.step_times
: nullptr};
size_type remaining_steps = max_steps_;

auto& step = *stepper_;
// Copy primaries to device and transport the first step
auto track_counts = step(primaries);
append_track_counts(track_counts);
if (store_step_times_)
{
result.step_times.push_back(get_step_time());
}
record_step_time();

while (track_counts)
{
Expand All @@ -141,14 +140,9 @@ auto Transporter<M>::operator()(SpanConstPrimary primaries)
break;
}

get_step_time = {};
track_counts = step();

append_track_counts(track_counts);
if (store_step_times_)
{
result.step_times.push_back(get_step_time());
}
record_step_time();
}

result.num_aborted = track_counts.alive + track_counts.queued;
Expand Down
3 changes: 1 addition & 2 deletions app/celer-sim/Transporter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ struct TransporterInput
*/
struct TransporterResult
{
using VecReal = std::vector<real_type>;
using VecCount = std::vector<size_type>;

// Per-step diagnostics
VecCount initializers; //!< Num starting track initializers
VecCount active; //!< Num tracks active at beginning of step
VecCount alive; //!< Num living tracks at end of step
VecReal step_times; //!< Real time per step
std::vector<double> step_times; //!< Real time per step

// Always-on basic diagnostics
size_type num_track_slots{}; //!< Number of total track slots
Expand Down
11 changes: 9 additions & 2 deletions app/celer-sim/simple-driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def strtobool(text):
'action_diagnostic': True,
'step_diagnostic': True,
'step_diagnostic_bins': 200,
'write_step_times': use_device,
'simple_calo': simple_calo,
'sync': True,
'merge_events': False,
Expand Down Expand Up @@ -141,6 +142,12 @@ def strtobool(text):
json.dump(j, f, indent=1)
print("Results written to", outfilename, file=stderr)

time = j['result']['runner']['time'].copy()
time.pop('steps')
run_output =j['result']['runner']
time = run_output['time'].copy()
steps = time.pop('steps')
if use_device:
assert(len(steps) == run_output['num_step_iterations'][0])
else:
# Step times disabled on CPU from input
assert(steps is None)
print(json.dumps(time, indent=1))

0 comments on commit 50109fe

Please sign in to comment.