Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-23 09:43:48

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 celeritas/global/ActionLauncher.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <utility>
0010 
0011 #include "corecel/Config.hh"
0012 
0013 #include "corecel/Assert.hh"
0014 #include "corecel/Types.hh"
0015 #include "corecel/sys/MultiExceptionHandler.hh"
0016 #include "corecel/sys/ThreadId.hh"
0017 
0018 #include "ActionInterface.hh"
0019 #include "CoreParams.hh"
0020 #include "CoreState.hh"
0021 #include "KernelContextException.hh"
0022 
0023 namespace celeritas
0024 {
0025 //---------------------------------------------------------------------------//
0026 /*!
0027  * Helper function to run an executor in parallel on CPU.
0028  *
0029  * This allows using a custom number of threads rather than the state size.
0030  */
0031 template<class F>
0032 void launch_core(size_type num_threads,
0033                  std::string_view label,
0034                  celeritas::CoreParams const& params,
0035                  celeritas::CoreState<MemSpace::host>& state,
0036                  F&& execute_thread)
0037 {
0038     MultiExceptionHandler capture_exception;
0039 #if defined(_OPENMP) && CELERITAS_OPENMP == CELERITAS_OPENMP_TRACK
0040 #    pragma omp parallel for
0041 #endif
0042     for (size_type i = 0; i < num_threads; ++i)
0043     {
0044         CELER_TRY_HANDLE_CONTEXT(
0045             execute_thread(ThreadId{i}),
0046             capture_exception,
0047             KernelContextException(
0048                 params.ref<MemSpace::host>(), state.ref(), ThreadId{i}, label));
0049     }
0050     log_and_rethrow(std::move(capture_exception));
0051 }
0052 
0053 //---------------------------------------------------------------------------//
0054 /*!
0055  * Helper function to run an executor in parallel on CPU over all states.
0056  *
0057  * Example:
0058  * \code
0059  void FooHelper::step(CoreParams const& params,
0060                          CoreStateHost& state) const
0061  {
0062     launch_core(params, state, "foo-helper", make_blah_executor(blah));
0063  }
0064  * \endcode
0065  */
0066 template<class F>
0067 void launch_core(std::string_view label,
0068                  celeritas::CoreParams const& params,
0069                  celeritas::CoreState<MemSpace::host>& state,
0070                  F&& execute_thread)
0071 {
0072     return launch_core(
0073         state.size(), label, params, state, std::forward<F>(execute_thread));
0074 }
0075 
0076 //---------------------------------------------------------------------------//
0077 /*!
0078  * Helper function to run an action in parallel on CPU.
0079  *
0080  * This allows using a custom number of threads rather than the state size.
0081  */
0082 template<class F>
0083 void launch_action(CoreStepActionInterface const& action,
0084                    size_type num_threads,
0085                    celeritas::CoreParams const& params,
0086                    celeritas::CoreState<MemSpace::host>& state,
0087                    F&& execute_thread)
0088 {
0089     return launch_core(num_threads,
0090                        action.label(),
0091                        params,
0092                        state,
0093                        std::forward<F>(execute_thread));
0094 }
0095 
0096 //---------------------------------------------------------------------------//
0097 /*!
0098  * Helper function to run an action in parallel on CPU over all states.
0099  *
0100  * Example:
0101  * \code
0102  void FooAction::step(CoreParams const& params,
0103                          CoreStateHost& state) const
0104  {
0105     launch_action(*this, params, state, make_blah_executor(blah));
0106  }
0107  * \endcode
0108  */
0109 template<class F>
0110 void launch_action(CoreStepActionInterface const& action,
0111                    celeritas::CoreParams const& params,
0112                    celeritas::CoreState<MemSpace::host>& state,
0113                    F&& execute_thread)
0114 {
0115     return launch_core(
0116         action.label(), params, state, std::forward<F>(execute_thread));
0117 }
0118 
0119 //---------------------------------------------------------------------------//
0120 }  // namespace celeritas