Skip to content

Commit

Permalink
Add structured configuration printing
Browse files Browse the repository at this point in the history
The current configuration printing code is a hodgepodge of unstructured
printing which facilitates reading by neither humans nor machines. This
commit replaces the existing output system by a much more structured,
JSON-like format which pretty prints the output and will, in the future,
allow us to very easily output configurations in machine-readable
formats.
  • Loading branch information
stephenswat committed Jan 23, 2025
1 parent b7ad1f2 commit 834d3f7
Show file tree
Hide file tree
Showing 36 changed files with 610 additions and 184 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Project include(s).
include( traccc-compiler-options-cpp )

add_subdirectory(utils)
add_subdirectory(options)
add_subdirectory(run)
add_subdirectory(io)
Expand Down
1 change: 1 addition & 0 deletions examples/options/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ target_link_libraries( traccc_options
Boost::program_options
traccc::io
traccc::performance
traccc::utils
)
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/accelerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ class accelerator : public interface {
/// Constructor
accelerator();

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // struct accelerator

} // namespace traccc::opts
6 changes: 2 additions & 4 deletions examples/options/include/traccc/options/clusterization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class clusterization
operator clustering_config() const override;
operator host::clusterization_algorithm::config_type() const override;

std::unique_ptr<configuration_printable> as_printable() const override;

private:
/// @name Options
/// @{
Expand All @@ -38,10 +40,6 @@ class clusterization
unsigned int target_cells_per_thread;
unsigned int backup_size_multiplier;
/// @}

/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

}; // class clusterization

} // namespace traccc::opts
18 changes: 5 additions & 13 deletions examples/options/include/traccc/options/details/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#pragma once

#include "traccc/examples/utils/printable.hpp"

// Boost include(s).
#include <boost/program_options.hpp>

// System include(s).
#include <iosfwd>
#include <memory>
#include <string>
#include <string_view>

Expand All @@ -35,20 +38,13 @@ class interface {
///
virtual void read(const boost::program_options::variables_map& vm);

/// Helper for printing the options to an output stream
///
/// @param out The output stream to print to
/// @return The output stream
///
std::ostream& print(std::ostream& out) const;
/// Helper for turning an interface into a printable set of options.
virtual std::unique_ptr<configuration_printable> as_printable() const = 0;

/// Get the description of this program option group
const boost::program_options::options_description& options() const;

protected:
/// Print the specific options of the derived class
virtual std::ostream& print_impl(std::ostream& out) const;

/// (Boost) Description of this program option group
boost::program_options::options_description m_desc;

Expand All @@ -57,8 +53,4 @@ class interface {
std::string m_description;

}; // class interface

/// Printout helper for @c traccc::opts::interface and types derived from it
std::ostream& operator<<(std::ostream& out, const interface& opt);

} // namespace traccc::opts
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/detector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ class detector : public interface {
/// Constructor
detector();

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // struct detector

} // namespace traccc::opts
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/generation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ class generation : public interface {
///
void read(const boost::program_options::variables_map& vm) override;

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // struct generation

} // namespace traccc::opts
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/input_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ class input_data : public interface {
///
void read(const boost::program_options::variables_map& vm) override;

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // class input_data

} // namespace traccc::opts
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/output_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ class output_data : public interface {
///
void read(const boost::program_options::variables_map& vm) override;

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // class output_data

} // namespace traccc::opts
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/performance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ class performance : public interface {
/// Constructor
performance();

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // struct performance

} // namespace traccc::opts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ class telescope_detector : public interface {
///
void read(const boost::program_options::variables_map& vm) override;

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // ckass telescope_detector

} // namespace traccc::opts
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/threading.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ class threading : public interface {
///
void read(const boost::program_options::variables_map& vm) override;

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // struct threading

} // namespace traccc::opts
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/throughput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ class throughput : public interface {
/// Constructor
throughput();

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // class throughput

} // namespace traccc::opts
6 changes: 2 additions & 4 deletions examples/options/include/traccc/options/track_finding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class track_finding : public interface, public config_provider<finding_config> {
/// Configuration conversion operators
operator finding_config() const override;

std::unique_ptr<configuration_printable> as_printable() const override;

private:
/// @name Options
/// @{
Expand All @@ -55,10 +57,6 @@ class track_finding : public interface, public config_provider<finding_config> {
/// PDG number for particle hypothesis (Default: muon)
int pdg_number = 13;
/// @}

/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

}; // class track_finding

} // namespace traccc::opts
5 changes: 2 additions & 3 deletions examples/options/include/traccc/options/track_propagation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ class track_propagation : public interface,
/// Configuration provider
operator detray::propagation::config() const override;

std::unique_ptr<configuration_printable> as_printable() const override;

private:
/// @name Options
/// @{
/// Propagation configuration object
detray::propagation::config config;
/// @}

/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

/// Search window (helper variable)
value_array<unsigned int, 2> m_search_window = {0u, 0u};

Expand Down
5 changes: 1 addition & 4 deletions examples/options/include/traccc/options/track_resolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ class track_resolution : public interface {
/// Constructor
track_resolution();

private:
/// Print the specific options of this class
std::ostream& print_impl(std::ostream& out) const override;

std::unique_ptr<configuration_printable> as_printable() const override;
}; // class track_resolution

} // namespace traccc::opts
1 change: 1 addition & 0 deletions examples/options/include/traccc/options/track_seeding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class track_seeding : public interface {
/// Constructor
track_seeding();

std::unique_ptr<configuration_printable> as_printable() const override;
}; // struct track_seeding

} // namespace traccc::opts
13 changes: 10 additions & 3 deletions examples/options/src/accelerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// Local include(s).
#include "traccc/options/accelerator.hpp"

#include "traccc/examples/utils/printable.hpp"

// System include(s).
#include <iostream>

Expand All @@ -20,10 +22,15 @@ accelerator::accelerator() : interface("Accelerator Options") {
"Compare accelerator output with that of the CPU");
}

std::ostream& accelerator::print_impl(std::ostream& out) const {
std::unique_ptr<configuration_printable> accelerator::as_printable() const {
std::unique_ptr<configuration_printable> cat =
std::make_unique<configuration_category>("Accelerator options");

dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>(
"Compare with CPU output", compare_with_cpu ? "yes" : "no"));

out << " Compare with CPU results: " << (compare_with_cpu ? "yes" : "no");
return out;
return cat;
}

} // namespace traccc::opts
28 changes: 21 additions & 7 deletions examples/options/src/clusterization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "traccc/options/clusterization.hpp"

#include "traccc/clusterization/clusterization_algorithm.hpp"
#include "traccc/examples/utils/printable.hpp"

// System include(s).
#include <iostream>
Expand Down Expand Up @@ -50,12 +51,25 @@ clusterization::operator host::clusterization_algorithm::config_type() const {
return {};
}

std::ostream& clusterization::print_impl(std::ostream& out) const {
out << " Threads per partition: " << threads_per_partition << "\n";
out << " Target cells per thread: " << target_cells_per_thread << "\n";
out << " Max cells per thread: " << max_cells_per_thread << "\n";
out << " Scratch space size mult.: " << backup_size_multiplier;
return out;
}
std::unique_ptr<configuration_printable> clusterization::as_printable() const {
std::unique_ptr<configuration_printable> cat =
std::make_unique<configuration_category>("Clustering options");

dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>(
"Threads per partition", std::to_string(threads_per_partition)));
dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>(
"Target cells per thread",
std::to_string(target_cells_per_thread)));
dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>(
"Max cells per thread", std::to_string(max_cells_per_thread)));
dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>(
"Scratch space multiplier",
std::to_string(backup_size_multiplier)));

return cat;
}
} // namespace traccc::opts
17 changes: 0 additions & 17 deletions examples/options/src/details/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,8 @@ interface::interface(std::string_view group_desc)

void interface::read(const boost::program_options::variables_map&) {}

std::ostream& interface::print(std::ostream& out) const {

out << ">>> " << m_description << " <<<\n";
return print_impl(out);
}

const boost::program_options::options_description& interface::options() const {

return m_desc;
}

std::ostream& interface::print_impl(std::ostream& out) const {

return (out << " None");
}

std::ostream& operator<<(std::ostream& out, const interface& opt) {

return opt.print(out);
}

} // namespace traccc::opts
30 changes: 22 additions & 8 deletions examples/options/src/detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// Project include(s).
#include "traccc/options/detector.hpp"

#include "traccc/examples/utils/printable.hpp"

// System include(s).
#include <iostream>

Expand Down Expand Up @@ -37,15 +39,27 @@ detector::detector() : interface("Detector Options") {
"Digitization file");
}

std::ostream& detector::print_impl(std::ostream& out) const {
std::unique_ptr<configuration_printable> detector::as_printable() const {
std::unique_ptr<configuration_printable> cat =
std::make_unique<configuration_category>("Detector options");

dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>("Detector file",
detector_file));
dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>("Material file",
material_file));
dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>("Surface grid file",
grid_file));
dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>(
"Use detray detector", use_detray_detector ? "yes" : "no"));
dynamic_cast<configuration_category &>(*cat).add_child(
std::make_unique<configuration_kv_pair>("Digitization file",
digitization_file));

out << " Detector file : " << detector_file << "\n"
<< " Material file : " << material_file << "\n"
<< " Surface grid file : " << grid_file << "\n"
<< " Use detray::detector: " << (use_detray_detector ? "yes" : "no")
<< "\n"
<< " Digitization file : " << digitization_file;
return out;
return cat;
}

} // namespace traccc::opts
Loading

0 comments on commit 834d3f7

Please sign in to comment.