Skip to content

Commit

Permalink
Output diagnostic information about core sizes (#1591)
Browse files Browse the repository at this point in the history
* Add an optional parameter for setting state size via params
* Write out core sizes to JSON
* Add and fix docs
* Fix clang-tidy warnings
  • Loading branch information
sethrj authored Jan 24, 2025
1 parent 4dbeff8 commit 29931ad
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 20 deletions.
10 changes: 6 additions & 4 deletions app/celer-sim/Runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ void Runner::build_core_params(RunnerInput const& inp,
"streams ("
<< params.max_streams << " requested)");

// Store number of tracks per stream
CELER_VALIDATE(inp.num_track_slots > 0,
<< "nonpositive num_track_slots=" << inp.num_track_slots);
params.tracks_per_stream
= ceil_div(inp.num_track_slots, params.max_streams);

// Construct track initialization params
params.init = [&inp, &params, num_events] {
CELER_VALIDATE(inp.initializer_capacity > 0,
Expand All @@ -469,14 +475,10 @@ void Runner::build_core_params(RunnerInput const& inp,
*/
void Runner::build_transporter_input(RunnerInput const& inp)
{
CELER_VALIDATE(inp.num_track_slots > 0,
<< "nonpositive num_track_slots=" << inp.num_track_slots);
CELER_VALIDATE(inp.max_steps > 0,
<< "nonpositive max_steps=" << inp.max_steps);

transporter_input_ = std::make_shared<TransporterInput>();
transporter_input_->num_track_slots
= ceil_div(inp.num_track_slots, core_params_->max_streams());
transporter_input_->max_steps = inp.max_steps;
transporter_input_->store_track_counts = inp.write_track_counts;
transporter_input_->store_step_times = inp.write_step_times;
Expand Down
1 change: 0 additions & 1 deletion app/celer-sim/Transporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ Transporter<M>::Transporter(TransporterInput inp)
CELER_LOG_LOCAL(status) << "Creating states";
StepperInput step_input;
step_input.params = inp.params;
step_input.num_track_slots = inp.num_track_slots;
step_input.stream_id = inp.stream_id;
step_input.action_times = inp.action_times;
stepper_ = std::make_shared<Stepper<M>>(std::move(step_input));
Expand Down
4 changes: 1 addition & 3 deletions app/celer-sim/Transporter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct TransporterInput
{
// Stepper input
std::shared_ptr<CoreParams const> params;
size_type num_track_slots{}; //!< AKA max_num_tracks
bool action_times{false}; //!< Whether to synchronize device between
//!< actions for timing

Expand All @@ -51,8 +50,7 @@ struct TransporterInput
//! True if all params are assigned
explicit operator bool() const
{
return params && num_track_slots > 0 && max_steps > 0
&& log_progress > 0;
return params && max_steps > 0 && log_progress > 0;
}
};

Expand Down
1 change: 0 additions & 1 deletion src/accel/LocalTransporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ LocalTransporter::LocalTransporter(SetupOptions const& options,
StepperInput inp;
inp.params = params.Params();
inp.stream_id = stream_id;
inp.num_track_slots = options.max_num_tracks;
inp.action_times = options.action_times;

if (celeritas::device())
Expand Down
3 changes: 3 additions & 0 deletions src/accel/SharedParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ void SharedParams::initialize_core(SetupOptions const& options)
params.max_streams = this->num_streams();
}

// Set state size
params.tracks_per_stream = options.max_num_tracks;

// Allocate device streams, or use the default stream if there is only one.
if (celeritas::device() && !options.default_stream
&& params.max_streams > 1)
Expand Down
31 changes: 31 additions & 0 deletions src/celeritas/global/CoreParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

#include "ActionInterface.hh"

#include "detail/CoreSizes.json.hh"

#if CELERITAS_CORE_GEO == CELERITAS_CORE_GEO_ORANGE
# include "orange/OrangeParams.hh"
# include "orange/OrangeParamsOutput.hh"
Expand Down Expand Up @@ -205,6 +207,30 @@ CoreScalars build_actions(ActionRegistry* reg)
return scalars;
}

//---------------------------------------------------------------------------//
auto get_core_sizes(CoreParams const& cp)
{
auto const& init = *cp.init();

detail::CoreSizes result;
result.processes = comm_world().size();
result.streams = cp.max_streams();

// NOTE: quantities are *per-process* quantities: integrated over streams,
// but not processes
result.initializers = result.streams * init.capacity();
result.tracks = result.streams * cp.tracks_per_stream();
// Number of secondaries is currently based on track size
result.secondaries = static_cast<size_type>(
cp.physics()->host_ref().scalars.secondary_stack_factor
* result.tracks);
// Event IDs are the same across all threads so this is *not* multiplied by
// streams
result.events = init.max_events();

return result;
}

//---------------------------------------------------------------------------//
} // namespace

Expand Down Expand Up @@ -311,6 +337,11 @@ CoreParams::CoreParams(Input input) : input_(std::move(input))
input_.output_reg->insert(OutputInterfaceAdapter<Environment>::from_const_ref(
OutputInterface::Category::system, "environ", celeritas::environment()));
input_.output_reg->insert(std::make_shared<BuildOutput>());
input_.output_reg->insert(
OutputInterfaceAdapter<detail::CoreSizes>::from_rvalue_ref(
OutputInterface::Category::internal,
"core_sizes",
get_core_sizes(*this)));

// Save core diagnostic information
input_.output_reg->insert(
Expand Down
9 changes: 9 additions & 0 deletions src/celeritas/global/CoreParams.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class WentzelOKVIParams;
//---------------------------------------------------------------------------//
/*!
* Global parameters required to run a problem.
*
* \todo Applications specify \c tracks_per_stream to build the states, but
* unit tests currently omit this option.
*/
class CoreParams final : public ParamsDataInterface<CoreParamsData>
{
Expand Down Expand Up @@ -85,6 +88,9 @@ class CoreParams final : public ParamsDataInterface<CoreParamsData>
//! Maximum number of simultaneous threads/tasks per process
StreamId::size_type max_streams{1};

//! Number of track slots per stream
StreamId::size_type tracks_per_stream{0};

//! True if all params are assigned and valid
explicit operator bool() const
{
Expand Down Expand Up @@ -135,6 +141,9 @@ class CoreParams final : public ParamsDataInterface<CoreParamsData>
//! Maximum number of streams
size_type max_streams() const { return input_.max_streams; }

//! Number of track slots per stream
size_type tracks_per_stream() const { return input_.tracks_per_stream; }

private:
Input input_;
HostRef host_ref_;
Expand Down
13 changes: 13 additions & 0 deletions src/celeritas/global/CoreState.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ CoreStateInterface::~CoreStateInterface() = default;
* Construct from CoreParams.
*/
template<MemSpace M>
CoreState<M>::CoreState(CoreParams const& params, StreamId stream_id)
: CoreState{params, stream_id, params.tracks_per_stream()}
{
}

//---------------------------------------------------------------------------//
/*!
* Construct with manual slot count.
*
* This is currently used for unit tests, and temporarily used by the \c
* Stepper constructor.
*/
template<MemSpace M>
CoreState<M>::CoreState(CoreParams const& params,
StreamId stream_id,
size_type num_track_slots)
Expand Down
3 changes: 3 additions & 0 deletions src/celeritas/global/CoreState.hh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class CoreState final : public CoreStateInterface

public:
// Construct from CoreParams
CoreState(CoreParams const& params, StreamId stream_id);

// Construct with manual slot count
CoreState(CoreParams const& params,
StreamId stream_id,
size_type num_track_slots);
Expand Down
8 changes: 7 additions & 1 deletion src/celeritas/global/Stepper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ Stepper<M>::Stepper(Input input)
CELER_VALIDATE(primaries_action_,
<< "primary generator was not added to the stepping loop");

size_type const track_slots = (input.num_track_slots == 0
? params_->tracks_per_stream()
: input.num_track_slots);
CELER_VALIDATE(track_slots > 0,
<< "track slots were specified neither in core params nor "
"stepper input");
// Create state, including aux data
state_ = std::make_shared<CoreState<M>>(
*params_, input.stream_id, input.num_track_slots);
*params_, input.stream_id, track_slots);

// Execute beginning-of-run action
ScopedProfiling profile_this{"begin-run"};
Expand Down
6 changes: 2 additions & 4 deletions src/celeritas/global/Stepper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ActionSequence;
*
* - \c params : Problem definition
* - \c num_track_slots : Maximum number of threads to run in parallel on GPU
* (optional, could be set by params)
* \c stream_id : Unique (thread/task) ID for this process
* - \c action_times : Whether to synchronize device between actions for timing
*/
Expand All @@ -47,10 +48,7 @@ struct StepperInput
bool action_times{false};

//! True if defined
explicit operator bool() const
{
return params && stream_id && num_track_slots > 0;
}
explicit operator bool() const { return params && stream_id; }
};

//---------------------------------------------------------------------------//
Expand Down
51 changes: 51 additions & 0 deletions src/celeritas/global/detail/CoreSizes.json.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/global/detail/CoreSizes.json.hh
//---------------------------------------------------------------------------//
#pragma once

#include <nlohmann/json.hpp>

#include "corecel/Types.hh"
#include "corecel/io/JsonUtils.json.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Save state size/capacity counters.
*
* These should be *integrated* across streams, *per process*.
*/
struct CoreSizes
{
size_type initializers{};
size_type tracks{};
size_type secondaries{};
size_type events{};

size_type streams{};
size_type processes{};
};

//---------------------------------------------------------------------------//

void to_json(nlohmann::json& j, CoreSizes const& inp)
{
j = {
CELER_JSON_PAIR(inp, initializers),
CELER_JSON_PAIR(inp, tracks),
CELER_JSON_PAIR(inp, secondaries),
CELER_JSON_PAIR(inp, events),
CELER_JSON_PAIR(inp, streams),
CELER_JSON_PAIR(inp, processes),
};
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
9 changes: 9 additions & 0 deletions src/celeritas/optical/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class MfpBuilder;
*/
class Model : public OpticalStepActionInterface, public ConcreteAction
{
public:
//!@{
//! \name Type aliases

//! Function to build optical models with a given action id
using ModelBuilder = std::function<std::shared_ptr<Model>(ActionId)>;

//!@}

public:
using ConcreteAction::ConcreteAction;

Expand Down
3 changes: 3 additions & 0 deletions src/celeritas/optical/ModelImporter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "celeritas/io/ImportOpticalModel.hh"

#include "Model.hh"
#include "Types.hh"

namespace celeritas
Expand Down Expand Up @@ -56,6 +57,7 @@ class ModelImporter

//!@{
//! \name User builder type aliases
using ModelBuilder = Model::ModelBuilder;
using UserBuildFunction
= std::function<std::optional<ModelBuilder>(UserBuildInput const&)>;
using UserBuildMap = std::unordered_map<IMC, UserBuildFunction>;
Expand Down Expand Up @@ -107,6 +109,7 @@ struct WarnAndIgnoreModel
//!@{
//! \name Type aliases
using UserBuildInput = ModelImporter::UserBuildInput;
using ModelBuilder = ModelImporter::ModelBuilder;
//!@}

// Warn about a missing optical model and ignore it
Expand Down
5 changes: 1 addition & 4 deletions src/celeritas/optical/Types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ using ParticleScintSpectrumId = OpaqueId<struct ParScintSpectrumRecord_>;
*/
namespace optical
{

//---------------------------------------------------------------------------//
//! Alias for MaterialId in core Celeritas namespace
using CoreMaterialId = ::celeritas::MaterialId;

//! Function to build optical models with a given action id
using ModelBuilder = std::function<std::shared_ptr<class Model>(ActionId)>;

//---------------------------------------------------------------------------//
} // namespace optical
} // namespace celeritas
2 changes: 1 addition & 1 deletion src/celeritas/optical/model/AbsorptionModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace optical
/*!
* Create a model builder for the optical absorption model.
*/
ModelBuilder AbsorptionModel::make_builder(SPConstImported imported)
auto AbsorptionModel::make_builder(SPConstImported imported) -> ModelBuilder
{
CELER_EXPECT(imported);
return [i = std::move(imported)](ActionId id) {
Expand Down
3 changes: 2 additions & 1 deletion src/celeritas/optical/model/RayleighModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace optical
* Create a model builder for Rayleigh scattering from imported data and
* material parameters.
*/
ModelBuilder RayleighModel::make_builder(SPConstImported imported, Input input)
auto RayleighModel::make_builder(SPConstImported imported,
Input input) -> ModelBuilder
{
CELER_EXPECT(imported);
return [imported = std::move(imported),
Expand Down

0 comments on commit 29931ad

Please sign in to comment.