Skip to content

Commit

Permalink
Extend user-facing app/environment options (#816)
Browse files Browse the repository at this point in the history
* Allow dumping of "default invalid" strings
* Add `--dump-default` argument to celer-sim
* Fix root step writer output for "unset" options
* Allow empty physics file name to load from geometry description via geant4
* Use `CELER_COLOR` instead of `GTEST_COLOR`
* Fix build when +vecgeom~geant4
* Define reusable 'UI' commands for altering Celeritas
* Add an interactive UI mode to celer-g4
* Apply suggestions from code review
  • Loading branch information
sethrj authored Jun 14, 2023
1 parent 3a3482b commit 5aaa7cc
Show file tree
Hide file tree
Showing 24 changed files with 440 additions and 120 deletions.
2 changes: 1 addition & 1 deletion app/celer-dump-data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ int main(int argc, char* argv[])
if (argc != 2)
{
// If number of arguments is incorrect, print help
std::cerr << "Usage: " << argv[0] << " {output}.root" << std::endl;
std::cerr << "usage: " << argv[0] << " {output}.root" << std::endl;
return 2;
}

Expand Down
16 changes: 10 additions & 6 deletions app/celer-export-geant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ namespace
//---------------------------------------------------------------------------//
void print_usage(char const* exec_name)
{
std::cerr << "Usage: " << exec_name
<< " {input}.gdml [{options}.json, -, ''] {output}.root"
<< std::endl;
// clang-format off
std::cerr
<< "usage: " << exec_name << " {input}.gdml "
"[{options}.json, -, ''] {output}.root\n"
" " << exec_name << " --dump-default\n";
// clang-format on
}

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -115,18 +118,19 @@ int main(int argc, char* argv[])
if (args.size() == 1 && (args.front() == "--help" || args.front() == "-h"))
{
print_usage(argv[0]);
return 0;
return EXIT_SUCCESS;
}
if (args.size() == 1 && args.front() == "--options")
if (args.size() == 1 && args.front() == "--dump-default")
{
#if CELERITAS_USE_JSON
GeantPhysicsOptions options;
constexpr int indent = 1;
std::cout << nlohmann::json{options}.dump(indent) << std::endl;
return EXIT_SUCCESS;
#else
CELER_LOG(error) << "JSON is unavailable: can't output geant options";
return EXIT_FAILURE;
#endif
return 0;
}
if (args.size() != 3)
{
Expand Down
20 changes: 10 additions & 10 deletions app/celer-g4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,30 @@ endif()

if(CELERITAS_USE_CUDA OR CELERITAS_USE_HIP)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(DEMO_MAX_NUM_TRACKS 524288)
set(DEMO_INIT_CAPACITY 4194304)
set(CELERG4_MAX_NUM_TRACKS 524288)
set(CELERG4_INIT_CAPACITY 4194304)
else()
# Use smaller number of tracks when running on CPU
set(DEMO_MAX_NUM_TRACKS 2048)
set(DEMO_INIT_CAPACITY 65536)
set(CELERG4_MAX_NUM_TRACKS 2048)
set(CELERG4_INIT_CAPACITY 65536)
endif()
else()
# Use smaller number of tracks when running on CPU
set(DEMO_MAX_NUM_TRACKS 1024)
set(DEMO_INIT_CAPACITY 32768)
set(CELERG4_MAX_NUM_TRACKS 1024)
set(CELERG4_INIT_CAPACITY 32768)
endif()

if(NOT CELERITAS_DEBUG)
set(DEMO_EVENTS
set(CELERG4_EVENTS
"${CELER_APP_DATA_DIR}/ttbarsplit-12evt-1108prim.hepmc3")
else()
# Use fewer events for debug
set(DEMO_EVENTS
set(CELERG4_EVENTS
"${CELER_APP_DATA_DIR}/ttbarbits-12evt-118prim.hepmc3")
endif()
if(CELERITAS_USE_ROOT)
set(DEMO_ROOT_OPTIONS "/setup/rootBufferSize 128000
/setup/writeSDHits true")
set(CELERG4_ROOT_OPTIONS "/celerg4/rootBufferSize 128000
/celerg4/writeSDHits true")
endif()
configure_file(
"simple-cms-test.mac.in"
Expand Down
58 changes: 6 additions & 52 deletions app/celer-g4/GlobalSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "corecel/Assert.hh"
#include "corecel/sys/Device.hh"
#include "accel/AlongStepFactory.hh"
#include "accel/SetupOptionsMessenger.hh"

namespace celeritas
{
Expand All @@ -21,10 +22,14 @@ namespace app
//---------------------------------------------------------------------------//
/*!
* Return non-owning pointer to a singleton.
*
* Creating the instance also creates a "messenger" that allows control over
* the Celeritas user inputs.
*/
GlobalSetup* GlobalSetup::Instance()
{
static GlobalSetup setup;
static SetupOptionsMessenger mess{setup.options_.get()};
return &setup;
}

Expand All @@ -37,7 +42,7 @@ GlobalSetup::GlobalSetup()
options_ = std::make_shared<SetupOptions>();
field_ = G4ThreeVector(0, 0, 0);
messenger_ = std::make_unique<G4GenericMessenger>(
this, "/setup/", "Demo geant integration setup");
this, "/celerg4/", "Demo geant integration setup");

{
auto& cmd = messenger_->DeclareProperty("geometryFile", geometry_file_);
Expand Down Expand Up @@ -65,57 +70,6 @@ GlobalSetup::GlobalSetup()
"Remove pointer suffix from input logical volume names");
cmd.SetDefaultValue("true");
}
{
auto& cmd
= messenger_->DeclareProperty("outputFile", options_->output_file);
cmd.SetGuidance("Set the JSON output file name");
}
{
auto& cmd = messenger_->DeclareProperty("maxNumTracks",
options_->max_num_tracks);
cmd.SetGuidance("Set the maximum number of track slots");
options_->max_num_tracks = Device::num_devices() > 0 ? 524288 : 64;
cmd.SetDefaultValue(std::to_string(options_->max_num_tracks));
}
{
auto& cmd = messenger_->DeclareProperty("maxNumEvents",
options_->max_num_events);
cmd.SetGuidance("Set the maximum number of events in the run");
options_->max_num_events = 1024;
cmd.SetDefaultValue(std::to_string(options_->max_num_events));
}
{
auto& cmd = messenger_->DeclareProperty(
"secondaryStackFactor", options_->secondary_stack_factor);
cmd.SetGuidance("Set the number of secondary slots per track slot");
options_->secondary_stack_factor = 3;
cmd.SetDefaultValue(std::to_string(options_->secondary_stack_factor));
}
{
auto& cmd = messenger_->DeclareProperty(
"initializerCapacity", options_->initializer_capacity);
cmd.SetGuidance("Set the maximum number of queued tracks");
options_->initializer_capacity = 1048576;
cmd.SetDefaultValue(std::to_string(options_->initializer_capacity));
}
{
auto& cmd = messenger_->DeclareProperty("cudaStackSize",
options_->cuda_stack_size);
cmd.SetGuidance("Set the per-thread dynamic CUDA stack size (bytes)");
options_->cuda_stack_size = 0;
}
{
auto& cmd = messenger_->DeclareProperty("cudaHeapSize",
options_->cuda_heap_size);
cmd.SetGuidance("Set the shared dynamic CUDA heap size (bytes)");
options_->cuda_heap_size = 0;
}
{
auto& cmd = messenger_->DeclareProperty("defaultStream",
options_->default_stream);
cmd.SetGuidance("Launch all kernels on the default stream");
options_->default_stream = false;
}
{
messenger_->DeclareMethod("magFieldZ",
&GlobalSetup::SetMagFieldZTesla,
Expand Down
24 changes: 18 additions & 6 deletions app/celer-g4/celer-g4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <CLHEP/Random/Random.h>
#include <G4GlobalConfig.hh>
#include <G4RunManager.hh>
#include <G4UIExecutive.hh>
#include <G4UImanager.hh>
#include <G4Version.hh>

Expand All @@ -38,14 +39,16 @@
#include "GlobalSetup.hh"
#include "PrimaryGeneratorAction.hh"

using namespace std::literals::string_view_literals;

namespace celeritas
{
namespace app
{
namespace
{
//---------------------------------------------------------------------------//
void run(std::string const& macro_filename)
void run(int argc, char** argv)
{
ScopedRootErrorHandler scoped_root_error;

Expand Down Expand Up @@ -88,9 +91,18 @@ void run(std::string const& macro_filename)

G4UImanager* ui = G4UImanager::GetUIpointer();
CELER_ASSERT(ui);
std::string_view macro_filename{argv[1]};
if (macro_filename == "--interactive")
{
G4UIExecutive exec(argc, argv);
exec.SessionStart();
return;
}

CELER_LOG(status) << "Executing macro commands from '" << macro_filename
<< "'";
ui->ApplyCommand("/control/execute " + macro_filename);
ui->ApplyCommand(std::string("/control/execute ")
+ std::string(macro_filename));

// Initialize run and process events
CELER_LOG(status) << "Initializing run manager";
Expand All @@ -116,10 +128,10 @@ void run(std::string const& macro_filename)
*/
int main(int argc, char* argv[])
{
std::vector<std::string> args(argv, argv + argc);
if (args.size() != 2 || args[1] == "--help" || args[1] == "-h")
if (argc != 2 || argv[1] == "--help"sv || argv[1] == "-h"sv)
{
std::cerr << "usage: " << args[0] << " {commands}.mac\n"
std::cerr << "usage: " << argv[0] << " {commands}.mac\n"
<< " " << argv[0] << " --interactive\n"
<< "Environment variables:\n"
<< " G4FORCE_RUN_MANAGER_TYPE: MT or Serial\n"
<< " G4FORCENUMBEROFTHREADS: set CPU worker thread count\n"
Expand All @@ -131,7 +143,7 @@ int main(int argc, char* argv[])
}

// Run with threads and macro filename
celeritas::app::run(args[1]);
celeritas::app::run(argc, argv);

CELER_LOG(status) << "Run completed successfully; exiting";
return EXIT_SUCCESS;
Expand Down
17 changes: 9 additions & 8 deletions app/celer-g4/simple-cms-test.mac.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
/run/verbose 0
/event/verbose 0

/setup/geometryFile @PROJECT_SOURCE_DIR@/app/data/simple-cms.gdml
/setup/eventFile @DEMO_EVENTS@
/setup/outputFile simple-cms.out.json
/setup/maxNumTracks @DEMO_MAX_NUM_TRACKS@
/setup/maxNumEvents 1024
@DEMO_ROOT_OPTIONS@
/setup/secondaryStackFactor 3
/setup/initializerCapacity @DEMO_INIT_CAPACITY@
/celer/outputFile simple-cms.out.json
/celer/maxNumTracks @CELERG4_MAX_NUM_TRACKS@
/celer/maxNumEvents 1024
/celer/maxInitializers @CELERG4_INIT_CAPACITY@
/celer/secondaryStackFactor 3

/celerg4/geometryFile @PROJECT_SOURCE_DIR@/app/data/simple-cms.gdml
/celerg4/eventFile @CELERG4_EVENTS@
@CELERG4_ROOT_OPTIONS@
16 changes: 7 additions & 9 deletions app/celer-sim/Runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,16 @@ void Runner::build_core_params(RunnerInput const& inp,
ImportData const imported = [&inp] {
if (ends_with(inp.physics_filename, ".root"))
{
// Load imported from ROOT file
return RootImporter(inp.physics_filename.c_str())();
// Load from ROOT file
return RootImporter(inp.physics_filename)();
}
else if (ends_with(inp.physics_filename, ".gdml"))
std::string filename = inp.physics_filename;
if (filename.empty())
{
// Load imported directly from Geant4
return GeantImporter(
GeantSetup(inp.physics_filename, inp.geant_options))();
filename = inp.geometry_filename;
}
CELER_VALIDATE(false,
<< "invalid physics filename '" << inp.physics_filename
<< "' (expected gdml or root)");
// Load imported data directly from Geant4
return GeantImporter(GeantSetup(filename, inp.geant_options))();
}();

// Create action manager
Expand Down
2 changes: 1 addition & 1 deletion app/celer-sim/RunnerInput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct RunnerInput
//! Whether the run arguments are valid
explicit operator bool() const
{
return !geometry_filename.empty() && !physics_filename.empty()
return !geometry_filename.empty()
&& (primary_gen_options || !hepmc3_filename.empty())
&& num_track_slots > 0 && max_steps > 0
&& initializer_capacity > 0 && max_events > 0
Expand Down
4 changes: 2 additions & 2 deletions app/celer-sim/RunnerInputIO.json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void from_json(nlohmann::json const& j, RunnerInput& v)
LDIO_LOAD_OPTION(environ);

LDIO_LOAD_REQUIRED(geometry_filename);
LDIO_LOAD_REQUIRED(physics_filename);
LDIO_LOAD_OPTION(physics_filename);
LDIO_LOAD_OPTION(hepmc3_filename);

LDIO_LOAD_OPTION(primary_gen_options);
Expand Down Expand Up @@ -174,7 +174,7 @@ void to_json(nlohmann::json& j, RunnerInput const& v)
LDIO_SAVE_OPTION(brem_combined);

LDIO_SAVE_OPTION(track_order);
if (ends_with(v.physics_filename, ".gdml"))
if (v.physics_filename.empty() || !ends_with(v.physics_filename, ".root"))
{
LDIO_SAVE_REQUIRED(geant_options);
}
Expand Down
22 changes: 17 additions & 5 deletions app/celer-sim/celer-sim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
# include "RunnerInputIO.json.hh"
#endif

using namespace std::literals::string_view_literals;

namespace celeritas
{
namespace app
Expand Down Expand Up @@ -148,7 +150,8 @@ void print_usage(char const* exec_name)
std::cerr << "usage: " << exec_name << " {input}.json\n"
" " << exec_name << " [--help|-h]\n"
" " << exec_name << " --version\n"
" " << exec_name << " --config\n";
" " << exec_name << " --config\n"
" " << exec_name << " --dump-default\n";
// clang-format on
}

Expand All @@ -165,6 +168,7 @@ int main(int argc, char* argv[])
{
using celeritas::MpiCommunicator;
using celeritas::ScopedMpiInit;
using celeritas::to_string;
using std::cerr;
using std::cout;
using std::endl;
Expand All @@ -191,29 +195,37 @@ int main(int argc, char* argv[])
return EXIT_FAILURE;
}
std::string_view filename{argv[1]};
if (filename == "--help" || filename == "-h")
if (filename == "--help"sv || filename == "-h"sv)
{
celeritas::app::print_usage(argv[0]);
return EXIT_SUCCESS;
}
if (filename == "--version" || filename == "-v")
if (filename == "--version"sv || filename == "-v"sv)
{
std::cout << celeritas_version << std::endl;
return EXIT_SUCCESS;
}
if (!CELERITAS_USE_JSON)
{
// Check for JSON *before* checking the config option
// Check for JSON *before* checking the options below
std::cerr << argv[0]
<< ": JSON is not enabled in this build of Celeritas"
<< std::endl;
return EXIT_FAILURE;
}
if (filename == "--config")
if (filename == "--config"sv)
{
std::cout << to_string(celeritas::BuildOutput{}) << std::endl;
return EXIT_SUCCESS;
}
if (filename == "--dump-default"sv)
{
#if CELERITAS_USE_JSON
std::cout << nlohmann::json{celeritas::app::RunnerInput{}}.dump(1)
<< std::endl;
#endif
return EXIT_SUCCESS;
}

// Initialize GPU
activate_device(comm);
Expand Down
Loading

0 comments on commit 5aaa7cc

Please sign in to comment.