Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:47

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