11#include "vecmem/containers/details/resize_jagged_vector.hpp"
12#include "vecmem/containers/jagged_vector.hpp"
13#include "vecmem/edm/details/schema_traits.hpp"
14#include "vecmem/memory/host_memory_resource.hpp"
15#include "vecmem/utils/debug.hpp"
16#include "vecmem/utils/type_traits.hpp"
28template <
typename TYPE>
32 if ((data.size_ptr() ==
nullptr) || (data.capacity() == 0)) {
40 "Prepared a device vector buffer of capacity %u "
41 "for use on a device (ptr: %p)",
42 data.capacity(),
static_cast<void*
>(data.size_ptr()));
48template <
typename TYPE>
52 if (data.capacity() == 0) {
57 do_memset(data.capacity() *
sizeof(TYPE), data.ptr(), value);
58 VECMEM_DEBUG_MSG(2,
"Set %u vector elements to %i at ptr: %p",
59 data.capacity(), value,
static_cast<void*
>(data.ptr()));
65template <
typename TYPE>
83template <
typename TYPE>
97template <
typename TYPE,
typename ALLOC>
99 const data::vector_view<std::add_const_t<TYPE>>& from_view,
100 std::vector<TYPE, ALLOC>& to_vec, type::copy_type cptype)
const {
103 const typename data::vector_view<std::add_const_t<TYPE>>::size_type size =
114 do_copy(size *
sizeof(TYPE), from_view.ptr(), to_vec.data(), cptype);
120template <
typename TYPE>
144template <
typename TYPE>
148 if (data.
size() == 0) {
154 if (data.
host_ptr()[0].size_ptr() !=
nullptr) {
172 "Prepared a jagged device vector buffer of size %lu "
173 "for use on a device",
180template <
typename TYPE>
185 if (is_contiguous(data.host_ptr(), data.capacity())) {
189 const std::size_t total_size = std::accumulate(
190 data.host_ptr(), data.host_ptr() + data.size(),
191 static_cast<std::size_t
>(0u),
192 [](std::size_t sum,
const data::vector_view<TYPE>& iv) {
193 return sum + iv.capacity();
196 for (std::size_t i = 0; i < data.size(); ++i) {
197 data::vector_view<TYPE>& iv = data.host_ptr()[i];
198 if ((iv.capacity() != 0u) && (iv.ptr() !=
nullptr)) {
200 do_memset(total_size *
sizeof(TYPE), iv.ptr(), value);
210 for (std::size_t i = 0; i < data.size(); ++i) {
211 data::vector_view<TYPE>& iv = data.host_ptr()[i];
212 do_memset(iv.capacity() *
sizeof(TYPE), iv.ptr(), value);
220template <
typename TYPE>
247template <
typename TYPE>
261template <
typename TYPE,
typename ALLOC1,
typename ALLOC2>
263 const data::jagged_vector_view<std::add_const_t<TYPE>>& from_view,
264 std::vector<std::vector<TYPE, ALLOC2>, ALLOC1>& to_vec,
265 type::copy_type cptype)
const {
270 assert(sizes.size() == to_vec.size());
271 for (
typename data::jagged_vector_view<std::add_const_t<TYPE>>::size_type
273 i < from_view.size(); ++i) {
274 to_vec[i].resize(sizes[i]);
281template <
typename TYPE>
289template <
typename TYPE>
295 if ((
sizes.size() == 0) && (data.
size() == 0)) {
299 if (sizes.size() != data.
size()) {
300 std::ostringstream msg;
301 msg <<
"sizes.size() (" << sizes.size() <<
") != data.size() ("
302 << data.
size() <<
")";
303 throw std::length_error(msg.str());
307 bool perform_copy =
true;
309 i < data.
size(); ++i) {
312 if (data.
host_ptr()[i].capacity() < sizes[i]) {
313 std::ostringstream msg;
314 msg <<
"data.host_ptr()[" << i <<
"].capacity() ("
315 << data.
host_ptr()[i].capacity() <<
") < sizes[" << i <<
"] ("
317 throw std::length_error(msg.str());
321 if (data.
host_ptr()[i].size_ptr() ==
nullptr) {
322 perform_copy =
false;
323 }
else if (perform_copy ==
false) {
324 throw std::invalid_argument(
325 "Inconsistent target jagged vector view received for resizing");
329 if (perform_copy ==
false) {
341template <
typename SCHEMA>
345 if (data.layout().ptr() != data.host_layout().ptr()) {
346 assert(data.layout().capacity() > 0u);
347 [[maybe_unused]]
bool did_copy =
348 copy_view_impl(data.host_layout(), data.layout(),
type::unknown);
353 if (data.size().ptr() !=
nullptr) {
354 assert(data.size().capacity() > 0u);
355 do_memset(data.size().capacity() *
sizeof(
char), data.size().ptr(), 0);
358 "Prepared an SoA container of capacity %u "
359 "for use on a device (layout: {%u, %p}, size: {%u, %p})",
360 data.capacity(), data.layout().size(),
361 static_cast<void*
>(data.layout().ptr()),
362 data.size().size(),
static_cast<void*
>(data.size().ptr()));
368template <
typename... VARTYPES>
373 if (data.payload().ptr() !=
nullptr) {
374 memset(data.payload(), value);
377 memset_impl<0>(data, value);
384template <
typename... VARTYPES>
437 if ((
from_view.payload().ptr() !=
nullptr) &&
438 (
to_view.payload().ptr() !=
nullptr) &&
442 if (
from_view.payload().capacity() == 0) {
447 VECMEM_DEBUG_MSG(2,
"Performing simple SoA copy of %u bytes",
448 from_view.payload().size());
451 copy_view_impl(from_view.payload(), to_view.payload(), cptype);
454 if (to_view.
size().ptr() !=
nullptr) {
456 if (from_view.size().ptr() !=
nullptr) {
458 if (from_view.size().capacity() != to_view.
size().capacity()) {
459 std::ostringstream msg;
460 msg <<
"from_view.size().capacity() ("
461 << from_view.size().capacity()
462 <<
") != to_view.size().capacity() ("
463 << to_view.
size().capacity() <<
")";
464 throw std::length_error(msg.str());
467 copy_view_impl(from_view.size(), to_view.
size(), cptype);
470 copy_sizes_impl<0>(from_view, to_view, cptype);
479 copy_payload_impl<0>(from_view, to_view, cptype);
485template <
typename... VARTYPES,
template <
typename>
class INTERFACE>
487 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
489 edm::host<edm::schema<VARTYPES...>, INTERFACE>& to_vec,
490 type::copy_type cptype)
const {
493 resize_impl<0>(from_view, to_vec, cptype);
499template <
typename... VARTYPES>
510 if constexpr (std::disjunction_v<
516 if (data.size().ptr() ==
nullptr) {
521 assert(data.size().size() ==
545 "Function can only be used on containers with jagged vectors");
551template <
typename TYPE>
552bool copy::copy_view_impl(
566 std::ostringstream msg;
567 msg <<
"Target capacity (" << to_view.
capacity() <<
") < source size ("
569 throw std::length_error(msg.str());
574 if (to_view.
size_ptr() !=
nullptr) {
601 do_copy(size *
sizeof(TYPE), from_view.ptr(), to_view.
ptr(), cptype);
605template <
typename TYPE>
606bool copy::copy_view_impl(
607 const data::jagged_vector_view<std::add_const_t<TYPE>>& from_view,
608 data::jagged_vector_view<TYPE> to_view, type::copy_type cptype)
const {
611 if (from_view.size() > to_view.size()) {
612 std::ostringstream msg;
613 msg <<
"from_view.size() (" << from_view.size()
614 <<
") > to_view.size() (" << to_view.size() <<
")";
615 throw std::length_error(msg.str());
619 const typename data::jagged_vector_view<std::add_const_t<TYPE>>::size_type
620 size = from_view.
size();
626 const bool from_is_contiguous = is_contiguous(from_view.host_ptr(), size);
627 const bool to_is_contiguous = is_contiguous(to_view.host_ptr(), size);
628 VECMEM_DEBUG_MSG(3,
"from_is_contiguous = %d, to_is_contiguous = %d",
629 from_is_contiguous, to_is_contiguous);
638 auto set_sizes_event =
set_sizes(sizes, to_view);
642 std::vector<typename data::vector_view<std::add_const_t<TYPE>>::size_type>
644 bool capacities_match =
true;
645 for (std::size_t i = 0; i < size; ++i) {
646 if (from_view.host_ptr()[i].capacity() !=
647 to_view.host_ptr()[i].capacity()) {
648 capacities_match =
false;
651 capacities[i] = from_view.host_ptr()[i].capacity();
655 if (from_is_contiguous && to_is_contiguous && capacities_match) {
657 copy_views_contiguous_impl(capacities, from_view.host_ptr(),
658 to_view.host_ptr(), cptype);
663 copy_views_impl(sizes, from_view.host_ptr(), to_view.host_ptr(),
669 set_sizes_event->wait();
673template <
typename TYPE>
674void copy::copy_views_impl(
676 const data::vector_view<std::add_const_t<TYPE>>* from_view,
677 data::vector_view<TYPE>* to_view, type::copy_type cptype)
const {
680 assert(from_view !=
nullptr);
681 assert(to_view !=
nullptr);
684 const std::size_t size = sizes.size();
685 [[maybe_unused]] std::size_t copy_ops = 0;
688 for (std::size_t i = 0; i < size; ++i) {
696 assert(from_view[i].ptr() !=
nullptr);
697 assert(to_view[i].ptr() !=
nullptr);
698 assert(sizes[i] <= from_view[i].capacity());
699 assert(sizes[i] <= to_view[i].capacity());
702 do_copy(sizes[i] *
sizeof(TYPE), from_view[i].ptr(), to_view[i].ptr(),
709 "Copied the payload of a jagged vector of type "
710 "\"%s\" with %lu copy operation(s)",
711 typeid(TYPE).name(), copy_ops);
714template <
typename TYPE>
715void copy::copy_views_contiguous_impl(
717 const data::vector_view<std::add_const_t<TYPE>>* from_view,
718 data::vector_view<TYPE>* to_view, type::copy_type cptype)
const {
721 assert(from_view !=
nullptr);
722 assert(to_view !=
nullptr);
723 assert(is_contiguous(from_view, sizes.size()));
724 assert(is_contiguous(to_view, sizes.size()));
727 const std::size_t size = sizes.size();
728 const std::size_t total_size =
729 std::accumulate(sizes.begin(), sizes.end(),
730 static_cast<std::size_t
>(0)) *
734 for (std::size_t i = 0; i < size; ++i) {
742 assert(from_view[i].ptr() !=
nullptr);
743 assert(to_view[i].ptr() !=
nullptr);
746 do_copy(total_size, from_view[i].ptr(), to_view[i].ptr(), cptype);
752 "Copied the payload of a jagged vector of type "
753 "\"%s\" with 1 copy operation(s)",
754 typeid(TYPE).name());
757template <
typename TYPE>
758std::vector<typename data::vector_view<TYPE>::size_type> copy::get_sizes_impl(
759 const data::vector_view<TYPE>* data, std::size_t size)
const {
762 std::vector<typename data::vector_view<TYPE>::size_type> result(size, 0);
765 for (std::size_t i = 0; i < size; ++i) {
768 if ((data[i].capacity() != 0) && (data[i].size_ptr() !=
nullptr)) {
785 for (std::size_t i = 0; i < size; ++i) {
786 result[i] = data[i].capacity();
791template <
typename TYPE>
792bool copy::is_contiguous(
const data::vector_view<TYPE>* data,
799 auto ptr = data[0].ptr();
800 for (std::size_t i = 1; i < size; ++i) {
801 if ((ptr + data[i - 1].capacity()) != data[i].ptr()) {
809template <std::size_t INDEX,
typename... VARTYPES>
810void copy::memset_impl(edm::view<edm::schema<VARTYPES...>> data,
814 if constexpr (edm::type::details::is_scalar<
typename std::tuple_element<
815 INDEX, std::tuple<VARTYPES...>>::type>::value) {
816 do_memset(
sizeof(
typename std::tuple_element<
817 INDEX, std::tuple<VARTYPES...>>::type::type),
818 data.template get<INDEX>(), value);
821 memset(data.template get<INDEX>(), value);
824 if constexpr (
sizeof...(VARTYPES) > (INDEX + 1)) {
825 memset_impl<INDEX + 1>(data, value);
829template <std::size_t INDEX,
typename... VARTYPES,
830 template <
typename>
class INTERFACE>
831void copy::resize_impl(
832 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
834 edm::host<edm::schema<VARTYPES...>, INTERFACE>& to_vec,
835 [[maybe_unused]] type::copy_type cptype)
const {
843 if constexpr (std::disjunction_v<
844 edm::type::details::is_jagged_vector<VARTYPES>...> ==
848 edm::details::add_const_t<edm::schema<VARTYPES...>>>::size_type
849 size = from_view.capacity();
851 if (from_view.size().ptr() !=
nullptr) {
853 assert(from_view.size().size() ==
854 sizeof(
typename edm::view<edm::details::add_const_t<
855 edm::schema<VARTYPES...>>>::size_type));
857 do_copy(
sizeof(
typename edm::view<edm::details::add_const_t<
858 edm::schema<VARTYPES...>>>::size_type),
859 from_view.size().ptr(), &size, cptype);
867 VECMEM_DEBUG_MSG(4,
"Resizing a (non-jagged) container to size %u",
874 if constexpr (edm::type::details::is_jagged_vector<
875 typename std::tuple_element<
876 INDEX, std::tuple<VARTYPES...>>::type>::value) {
878 auto sizes =
get_sizes(from_view.template get<INDEX>());
881 4,
"Resizing jagged vector variable at index %lu to size %lu",
882 INDEX, sizes.size());
886 for (std::size_t i = 0; i < sizes.size(); ++i) {
887 to_vec.template get<INDEX>()[i].resize(sizes[i]);
889 }
else if constexpr (edm::type::details::is_vector<
890 typename std::tuple_element<
892 std::tuple<VARTYPES...>>::type>::value) {
894 auto size =
get_size(from_view.template get<INDEX>());
897 "Resizing vector variable at index %lu to size %u",
899 to_vec.template get<INDEX>().resize(size);
902 if constexpr (
sizeof...(VARTYPES) > (INDEX + 1)) {
903 resize_impl<INDEX + 1>(from_view, to_vec, cptype);
908template <std::size_t INDEX,
typename... VARTYPES>
909void copy::copy_sizes_impl(
910 [[maybe_unused]]
const edm::view<
911 edm::details::add_const_t<edm::schema<VARTYPES...>>>& from_view,
912 [[maybe_unused]] edm::view<edm::schema<VARTYPES...>> to_view,
913 [[maybe_unused]] type::copy_type cptype)
const {
917 assert(to_view.size().ptr() !=
nullptr);
918 assert(from_view.size().ptr() ==
nullptr);
921 if constexpr (std::disjunction_v<
922 edm::type::details::is_jagged_vector<VARTYPES>...> ==
926 edm::details::add_const_t<edm::schema<VARTYPES...>>>::size_type
927 size = from_view.capacity();
943 do_copy(
sizeof(
typename edm::view<edm::details::add_const_t<
944 edm::schema<VARTYPES...>>>::size_type),
945 &size, to_view.size().ptr(), size_cptype);
954 if constexpr (edm::type::details::is_jagged_vector<
955 typename std::tuple_element<
956 INDEX, std::tuple<VARTYPES...>>::type>::value) {
958 const auto sizes =
get_sizes(from_view.template get<INDEX>());
959 set_sizes(sizes, to_view.template get<INDEX>())->wait();
962 if constexpr (
sizeof...(VARTYPES) > (INDEX + 1)) {
963 copy_sizes_impl<INDEX + 1>(from_view, to_view, cptype);
968template <std::size_t INDEX,
typename... VARTYPES>
969void copy::copy_payload_impl(
970 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
972 edm::view<edm::schema<VARTYPES...>> to_view, type::copy_type cptype)
const {
975 if constexpr (edm::type::details::is_scalar<
typename std::tuple_element<
976 INDEX, std::tuple<VARTYPES...>>::type>::value) {
977 do_copy(
sizeof(
typename std::tuple_element<
978 INDEX, std::tuple<VARTYPES...>>::type::type),
979 from_view.template get<INDEX>(), to_view.template get<INDEX>(),
983 copy_view_impl(from_view.template get<INDEX>(),
984 to_view.template get<INDEX>(), cptype);
987 if constexpr (
sizeof...(VARTYPES) > (INDEX + 1)) {
988 copy_payload_impl<INDEX + 1>(from_view, to_view, cptype);
992template <std::size_t INDEX,
typename... VARTYPES>
993std::vector<data::vector_view<int>::size_type> copy::get_sizes_impl(
994 const edm::view<edm::schema<VARTYPES...>>& view)
const {
998 edm::details::has_jagged_vector<edm::schema<VARTYPES...>>::value,
999 "Function can only be used on containers with jagged vectors");
1002 if constexpr (edm::type::details::is_jagged_vector<
1003 typename std::tuple_element<
1004 INDEX, std::tuple<VARTYPES...>>::type>::value) {
1006 return get_sizes(view.template get<INDEX>());
1007 }
else if constexpr (
sizeof...(VARTYPES) > (INDEX + 1)) {
1010 return get_sizes_impl<INDEX + 1>(view);
1014#if defined(__GNUC__)
1015 __builtin_unreachable();
1016#elif defined(_MSC_VER)
An allocator class that wraps a memory resource.
Definition allocator.hpp:37
VECMEM_NODISCARD event_type set_sizes(const std::vector< typename data::vector_view< TYPE >::size_type > &sizes, data::jagged_vector_view< TYPE > data) const
Helper function for setting the sizes of a resizable jagged vector.
VECMEM_NODISCARD event_type operator()(const data::vector_view< std::add_const_t< TYPE > > &from, data::vector_view< TYPE > to, type::copy_type cptype=type::unknown) const
Copy a 1-dimensional vector's data between two existing memory blocks.
VECMEM_NODISCARD event_type setup(data::vector_view< TYPE > data) const
Set up the internal state of a vector buffer correctly on a device.
virtual void do_copy(std::size_t size, const void *from, void *to, type::copy_type cptype) const
Perform a "low level" memory copy.
Definition copy.cpp:31
std::unique_ptr< abstract_event > event_type
Event type used by the copy class.
Definition copy.hpp:70
data::vector_view< TYPE >::size_type get_size(const data::vector_view< TYPE > &data) const
Helper function for getting the size of a resizable 1D buffer.
Definition copy.ipp:121
virtual void do_memset(std::size_t size, void *ptr, int value) const
Perform a "low level" memory filling operation.
Definition copy.cpp:44
virtual VECMEM_NODISCARD event_type create_event() const
Create an event for synchronization.
Definition copy.cpp:54
std::vector< typename data::vector_view< TYPE >::size_type > get_sizes(const data::jagged_vector_view< TYPE > &data) const
Helper function for getting the sizes of a resizable jagged vector.
Definition copy.ipp:282
VECMEM_NODISCARD event_type memset(data::vector_view< TYPE > data, int value) const
Set all bytes of the vector to some value.
data::vector_buffer< std::remove_cv_t< TYPE > > to(const data::vector_view< TYPE > &data, memory_resource &resource, type::copy_type cptype=type::unknown) const
Copy a 1-dimensional vector to the specified memory resource.
Definition copy.ipp:66
Object owning all the data of a jagged vector.
Definition jagged_vector_buffer.hpp:30
typename base_type::value_type value_type
Use the base class's value_type.
Definition jagged_vector_buffer.hpp:38
A view for jagged vectors.
Definition jagged_vector_view.hpp:45
VECMEM_HOST_AND_DEVICE pointer host_ptr() const
Access the host accessible array describing the inner vectors.
Definition jagged_vector_view.ipp:105
std::size_t size_type
We cannot use boolean types.
Definition jagged_vector_view.hpp:53
VECMEM_HOST_AND_DEVICE size_type capacity() const
Get the maximum capacity of the "outer" vector.
Definition jagged_vector_view.ipp:92
VECMEM_HOST_AND_DEVICE pointer ptr() const
Get a pointer to the vector elements.
Definition jagged_vector_view.ipp:99
VECMEM_HOST_AND_DEVICE size_type size() const
Get the "outer" size of the jagged vector.
Definition jagged_vector_view.ipp:86
Object owning the data held by it.
Definition vector_buffer.hpp:29
typename base_type::size_type size_type
Size type definition coming from the base class.
Definition vector_buffer.hpp:35
Class holding data about a 1 dimensional vector/array.
Definition vector_view.hpp:38
VECMEM_HOST_AND_DEVICE pointer ptr() const
Get a pointer to the vector elements.
Definition vector_view.ipp:91
unsigned int size_type
We cannot use boolean types.
Definition vector_view.hpp:47
VECMEM_HOST_AND_DEVICE size_pointer size_ptr() const
Get a pointer to the size of the vector.
Definition vector_view.ipp:84
VECMEM_HOST_AND_DEVICE size_type capacity() const
Get the maximum capacity of the vector.
Definition vector_view.ipp:78
VECMEM_HOST_AND_DEVICE size_type size() const
Get the size of the vector.
Definition vector_view.ipp:72
Technical base type for buffer<schema<VARTYPES...>>
Definition buffer.hpp:28
Technical base type for view<schema<VARTYPES...>>
Definition view.hpp:29
VECMEM_HOST std::vector< typename vector_view< T >::size_type > get_capacities(const jagged_vector_view< T > &data)
Get the capacities of the inner vectors of a jagged vector.
Definition jagged_vector_view.ipp:111
buffer_type
"Overall type" for a buffer object
Definition buffer_type.hpp:13
@ fixed_size
The buffer has a fixed number of elements.
@ resizable
The buffer is resizable/expandable.
void resize_jagged_vector(std::vector< std::vector< T, ALLOC1 >, ALLOC2 > &vec, std::size_t size)
Resize a generic jagged vector.
Definition resize_jagged_vector.hpp:23
VECMEM_HOST std::vector< vecmem::data::vector_view< int >::size_type > get_capacities(const view< schema< VARTYPES... > > &soa)
Helper function to get the capacities of the jagged vectors in a view.
Definition view.ipp:198
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
VECMEM_HOST data::vector_view< T > get_data(array< T, N > &a)
Helper function creating a vecmem::data::vector_view object.
Definition array.ipp:217
copy_type
Types of memory copies to handle.
Definition copy.hpp:53
@ device_to_host
Copy operation between a device and the host.
Definition copy.hpp:57
@ device_to_device
Copy operation between two devices.
Definition copy.hpp:61
@ unknown
Unknown copy type, determined at runtime.
Definition copy.hpp:63
@ host_to_host
Copy operation on the host.
Definition copy.hpp:59
@ host_to_device
Copy operation between the host and a device.
Definition copy.hpp:55
Technical base type for has_jagged_vector<schema<VARTYPES...>>
Definition schema_traits.hpp:238
Meta type describing the "schema" of an SoA container.
Definition schema.hpp:46
Definition schema_traits.hpp:108