File indexing completed on 2025-01-18 09:59:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
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
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
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
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
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
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
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
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
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
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 }