Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:21:12

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file accel/detail/IntegrationSingleton.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 
0011 #include "corecel/sys/Stopwatch.hh"
0012 
0013 #include "../LocalTransporter.hh"
0014 #include "../SetupOptions.hh"
0015 #include "../SharedParams.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 class ScopedMpiInit;
0021 class SetupOptionsMessenger;
0022 
0023 namespace detail
0024 {
0025 //---------------------------------------------------------------------------//
0026 /*!
0027  * Singletons used by the Integration interfaces.
0028  *
0029  * The singleton instance contains global data, including a single copy of the
0030  * options (which are referenced by the UI setup) and an MPI setup/teardown
0031  * helper.
0032  * Thread-local data is managed by the \c local_transporter static function.
0033  * Setup options are permanently referenced by the UI messenger class.
0034  *
0035  * The first call to the singleton initializes MPI if necessary, and MPI will
0036  * be finalized during the termination phase of the program.
0037  */
0038 class IntegrationSingleton
0039 {
0040   public:
0041     enum class Mode
0042     {
0043         disabled,
0044         kill_offload,
0045         enabled,
0046         size_
0047     };
0048 
0049     // Static GLOBAL shared singleton
0050     static IntegrationSingleton& instance();
0051 
0052     // Static THREAD-LOCAL Celeritas state data
0053     static LocalTransporter& local_transporter();
0054 
0055     //// ACCESSORS ////
0056 
0057     // Assign setup options before constructing params
0058     void setup_options(SetupOptions&&);
0059 
0060     //! Static global setup options before or after constructing params
0061     SetupOptions const& setup_options() const { return options_; }
0062 
0063     //! Whether Celeritas is enabled
0064     Mode mode() const { return mode_; }
0065 
0066     //!@{
0067     //! Static global Celeritas problem data
0068     SharedParams& shared_params() { return params_; }
0069     SharedParams const& shared_params() const { return params_; }
0070     //!@}
0071 
0072     //// HELPERS ////
0073 
0074     // Set up logging
0075     void initialize_logger();
0076 
0077     // Construct shared params on master (or single) thread
0078     void initialize_shared_params();
0079 
0080     // Construct thread-local transporter
0081     bool initialize_local_transporter();
0082 
0083     // Destroy local transporter
0084     void finalize_local_transporter();
0085 
0086     // Destroy params
0087     void finalize_shared_params();
0088 
0089     // Start the transport timer [s]
0090     void start_timer() { get_time_ = {}; }
0091 
0092     // Stop the timer and return the elapsed time [s]
0093     real_type stop_timer() { return get_time_(); }
0094 
0095   private:
0096     // Only this class can construct
0097     IntegrationSingleton();
0098 
0099     //// DATA ////
0100     Mode mode_{Mode::size_};
0101     SetupOptions options_;
0102     SharedParams params_;
0103     std::unique_ptr<ScopedMpiInit> scoped_mpi_;
0104     std::unique_ptr<SetupOptionsMessenger> messenger_;
0105     Stopwatch get_time_;
0106 };
0107 
0108 //---------------------------------------------------------------------------//
0109 }  // namespace detail
0110 }  // namespace celeritas