Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:34

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 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 /*!
0007  * \file geocel/rasterize/RaytraceImager.t.hh
0008  * \brief Template definition file for \c RaytraceImager
0009  *
0010  * Include this file in a .cc file and instantiate it explicitly. When
0011  * instantiating, you must provide access to the GeoTraits specialization as
0012  * well as the data classes and track view.
0013  */
0014 //---------------------------------------------------------------------------//
0015 #pragma once
0016 
0017 #include "RaytraceImager.hh"
0018 
0019 #include "corecel/data/CollectionStateStore.hh"
0020 #include "corecel/sys/MultiExceptionHandler.hh"
0021 
0022 #include "Image.hh"
0023 
0024 #include "detail/RaytraceExecutor.hh"
0025 
0026 #define CELER_INST_RAYTRACE_IMAGER
0027 
0028 namespace celeritas
0029 {
0030 //---------------------------------------------------------------------------//
0031 template<class G>
0032 struct RaytraceImager<G>::CachedStates
0033 {
0034     template<MemSpace M>
0035     using StateStore = CollectionStateStore<GeoStateData, M>;
0036 
0037     StateStore<MemSpace::host> host;
0038     StateStore<MemSpace::device> device;
0039 
0040     //! Access the states for the given memspace
0041     template<MemSpace M>
0042     StateStore<M>& get()
0043     {
0044         if constexpr (M == MemSpace::host)
0045         {
0046             return host;
0047         }
0048         else
0049         {
0050             return device;
0051         }
0052     }
0053 };
0054 
0055 //---------------------------------------------------------------------------//
0056 /*!
0057  * Construct with geometry.
0058  */
0059 template<class G>
0060 RaytraceImager<G>::RaytraceImager(SPGeometry geo)
0061     : geo_{std::move(geo)}, cache_{std::make_shared<CachedStates>()}
0062 {
0063     CELER_EXPECT(geo_);
0064     CELER_ENSURE(cache_);
0065 }
0066 
0067 //---------------------------------------------------------------------------//
0068 /*!
0069  * Raytrace an image on host or device.
0070  */
0071 template<class G>
0072 void RaytraceImager<G>::operator()(Image<MemSpace::host>* image)
0073 {
0074     return this->raytrace_impl(image);
0075 }
0076 
0077 //---------------------------------------------------------------------------//
0078 /*!
0079  * Raytrace an image on host or device.
0080  */
0081 template<class G>
0082 void RaytraceImager<G>::operator()(Image<MemSpace::device>* image)
0083 {
0084     return this->raytrace_impl(image);
0085 }
0086 
0087 //---------------------------------------------------------------------------//
0088 /*!
0089  * Raytrace an image on host or device.
0090  */
0091 template<class G>
0092 template<MemSpace M>
0093 void RaytraceImager<G>::raytrace_impl(Image<M>* image)
0094 {
0095     CELER_EXPECT(image);
0096 
0097     auto const& img_params = *image->params();
0098     auto const& geo_params = *geo_;
0099     auto& geo_state_store = cache_->template get<M>();
0100 
0101     if (img_params.num_lines() != geo_state_store.size())
0102     {
0103         using StateStore = typename CachedStates::template StateStore<M>;
0104 
0105         // Allocate (or deallocate and reallocate) geometry states
0106         if (geo_state_store)
0107         {
0108             geo_state_store = {};
0109         }
0110         geo_state_store
0111             = StateStore{geo_params.host_ref(), img_params.num_lines()};
0112     }
0113 
0114     // Raytrace it!
0115     this->launch_raytrace_kernel(geo_params.template ref<M>(),
0116                                  geo_state_store.ref(),
0117                                  img_params.template ref<M>(),
0118                                  image->ref());
0119 }
0120 
0121 //---------------------------------------------------------------------------//
0122 /*!
0123  * Execute the raytrace on the host.
0124  */
0125 template<class G>
0126 void RaytraceImager<G>::launch_raytrace_kernel(
0127     GeoParamsCRef<MemSpace::host> const& geo_params,
0128     GeoStateRef<MemSpace::host> const& geo_states,
0129     ImageParamsCRef<MemSpace::host> const& img_params,
0130     ImageStateRef<MemSpace::host> const& img_state) const
0131 {
0132     using CalcId = detail::VolumeIdCalculator;
0133     using Executor = detail::RaytraceExecutor<GeoTrackView, CalcId>;
0134     Executor execute_thread{
0135         geo_params, geo_states, img_params, img_state, CalcId{}};
0136 
0137     size_type const num_threads = geo_states.size();
0138 
0139     MultiExceptionHandler capture_exception;
0140 #if CELERITAS_OPENMP == CELERITAS_OPENMP_TRACK
0141 #    pragma omp parallel for
0142 #endif
0143     for (size_type i = 0; i < num_threads; ++i)
0144     {
0145         CELER_TRY_HANDLE(execute_thread(ThreadId{i}), capture_exception);
0146     }
0147     log_and_rethrow(std::move(capture_exception));
0148 }
0149 
0150 //---------------------------------------------------------------------------//
0151 /*!
0152  * Execute the raytrace on the device.
0153  */
0154 #if !CELER_USE_DEVICE
0155 template<class G>
0156 void RaytraceImager<G>::launch_raytrace_kernel(
0157     GeoParamsCRef<MemSpace::device> const&,
0158     GeoStateRef<MemSpace::device> const&,
0159     ImageParamsCRef<MemSpace::device> const&,
0160     ImageStateRef<MemSpace::device> const&) const
0161 {
0162     CELER_NOT_CONFIGURED("CUDA OR HIP");
0163 }
0164 #endif
0165 
0166 //---------------------------------------------------------------------------//
0167 }  // namespace celeritas