Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-29 08:39:39

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/CollectionAlgorithms.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <numeric>
0010 #include <vector>
0011 
0012 #include "Collection.hh"
0013 #include "Copier.hh"
0014 #include "Filler.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Fill the collection with the given value.
0021  *
0022  * This should only be used during initialization when stream synchronization
0023  * is OK. Use the \c Filler directly during runtime since it supports streams.
0024  */
0025 template<class T, Ownership W, MemSpace M, class I>
0026 void fill(T&& value, Collection<T, W, M, I>* col)
0027 {
0028     static_assert(W != Ownership::const_reference,
0029                   "const references cannot be filled");
0030     CELER_EXPECT(col);
0031     Filler<T, M> fill_impl{std::forward<T>(value)};
0032     fill_impl((*col)[AllItems<T, M>{}]);
0033 }
0034 
0035 //---------------------------------------------------------------------------//
0036 /*!
0037  * Fill the collection with sequentially increasing values starting from zero.
0038  */
0039 template<class T, Ownership W, MemSpace M, class I>
0040 void fill_sequence(Collection<T, W, M, I>* dst, StreamId stream)
0041 {
0042     static_assert(W != Ownership::const_reference,
0043                   "const references cannot be filled");
0044     CELER_EXPECT(dst);
0045 
0046     std::vector<T> src(dst->size());
0047     std::iota(src.begin(), src.end(), T{0});
0048     Copier<T, M> copy{(*dst)[AllItems<T, M>{}], stream};
0049     copy(MemSpace::host, make_span(src));
0050 }
0051 
0052 //---------------------------------------------------------------------------//
0053 /*!
0054  * Copy from the given collection to host.
0055  */
0056 template<class T, Ownership W, MemSpace M, class I, std::size_t E>
0057 void copy_to_host(Collection<T, W, M, I> const& src,
0058                   Span<T, E> dst,
0059                   StreamId sid = {})
0060 {
0061     CELER_EXPECT(src.size() == dst.size());
0062     Copier<T, MemSpace::host> copy_to_result{dst, sid};
0063     copy_to_result(M, src[AllItems<T, M>{}]);
0064 }
0065 
0066 //---------------------------------------------------------------------------//
0067 /*!
0068  * Create a new host collection from the given collection.
0069  *
0070  * This is useful for debugging.
0071  */
0072 template<class T, Ownership W, MemSpace M, class I>
0073 auto copy_to_host(Collection<T, W, M, I> const& src)
0074 {
0075     Collection<T, Ownership::value, MemSpace::host, I> result;
0076     result = src;
0077     return result;
0078 }
0079 
0080 //---------------------------------------------------------------------------//
0081 }  // namespace celeritas