Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:22

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file celeritas/global/ActionLauncher.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <utility>
0011 
0012 #include "corecel/Config.hh"
0013 
0014 #include "corecel/Assert.hh"
0015 #include "corecel/Types.hh"
0016 #include "corecel/sys/MultiExceptionHandler.hh"
0017 #include "corecel/sys/ThreadId.hh"
0018 
0019 #include "ActionInterface.hh"
0020 #include "CoreParams.hh"
0021 #include "CoreState.hh"
0022 #include "KernelContextException.hh"
0023 
0024 namespace celeritas
0025 {
0026 //---------------------------------------------------------------------------//
0027 /*!
0028  * Helper function to run an executor in parallel on CPU.
0029  *
0030  * Example:
0031  * \code
0032  void FooHelper::step(CoreParams const& params,
0033                          CoreStateHost& state) const
0034  {
0035     launch_core(params, state, "foo-helper", make_blah_executor(blah));
0036  }
0037  * \endcode
0038  */
0039 template<class F>
0040 void launch_core(std::string_view label,
0041                  celeritas::CoreParams const& params,
0042                  celeritas::CoreState<MemSpace::host>& state,
0043                  F&& execute_thread)
0044 {
0045     MultiExceptionHandler capture_exception;
0046 #if defined(_OPENMP) && CELERITAS_OPENMP == CELERITAS_OPENMP_TRACK
0047 #    pragma omp parallel for
0048 #endif
0049     for (size_type i = 0, size = state.size(); i != size; ++i)
0050     {
0051         CELER_TRY_HANDLE_CONTEXT(
0052             execute_thread(ThreadId{i}),
0053             capture_exception,
0054             KernelContextException(
0055                 params.ref<MemSpace::host>(), state.ref(), ThreadId{i}, label));
0056     }
0057     log_and_rethrow(std::move(capture_exception));
0058 }
0059 
0060 //---------------------------------------------------------------------------//
0061 /*!
0062  * Helper function to run an action in parallel on CPU over all states.
0063  *
0064  * Example:
0065  * \code
0066  void FooAction::step(CoreParams const& params,
0067                          CoreStateHost& state) const
0068  {
0069     launch_action(*this, params, state, make_blah_executor(blah));
0070  }
0071  * \endcode
0072  */
0073 template<class F>
0074 void launch_action(CoreStepActionInterface const& action,
0075                    celeritas::CoreParams const& params,
0076                    celeritas::CoreState<MemSpace::host>& state,
0077                    F&& execute_thread)
0078 {
0079     return launch_core(
0080         action.label(), params, state, std::forward<F>(execute_thread));
0081 }
0082 
0083 //---------------------------------------------------------------------------//
0084 }  // namespace celeritas