Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:04

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 corecel/data/Filler.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <algorithm>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 #include "corecel/cont/Span.hh"
0015 #include "corecel/sys/ThreadId.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Fill contiguous data with copies of a scalar value.
0022  */
0023 template<class T, MemSpace M>
0024 class Filler
0025 {
0026   public:
0027     //! Construct with target value and default stream
0028     explicit Filler(T value) : value_{value} {}
0029 
0030     //! Construct with target value and specific stream
0031     Filler(T value, StreamId stream) : value_{value}, stream_{stream} {}
0032 
0033     // Fill the span with the stored value
0034     inline void operator()(Span<T> data) const;
0035 
0036   private:
0037     T value_;
0038     StreamId stream_;
0039 
0040     void fill_device_impl(Span<T> data) const;
0041 };
0042 
0043 //---------------------------------------------------------------------------//
0044 /*!
0045  * Fill the span with the stored value.
0046  */
0047 template<class T, MemSpace M>
0048 void Filler<T, M>::operator()(Span<T> data) const
0049 {
0050     if constexpr (M == MemSpace::device)
0051     {
0052         this->fill_device_impl(data);
0053     }
0054     else
0055     {
0056         std::fill(data.begin(), data.end(), value_);
0057     }
0058 }
0059 
0060 //---------------------------------------------------------------------------//
0061 #if !CELER_USE_DEVICE
0062 template<class T, MemSpace M>
0063 CELER_FORCEINLINE void Filler<T, M>::fill_device_impl(Span<T>) const
0064 {
0065     CELER_DISCARD(stream_);
0066     CELER_ASSERT_UNREACHABLE();
0067 }
0068 #else
0069 extern template class Filler<real_type, MemSpace::device>;
0070 extern template class Filler<size_type, MemSpace::device>;
0071 extern template class Filler<int, MemSpace::device>;
0072 extern template class Filler<TrackSlotId, MemSpace::device>;
0073 #endif
0074 
0075 //---------------------------------------------------------------------------//
0076 }  // namespace celeritas