Skip to content

Commit

Permalink
Merge pull request #197 from krasznaa/JaggedVectorViewRedesign-main-2…
Browse files Browse the repository at this point in the history
…0220929

Jagged Vector View Redesign, main branch (2022.09.30.)
  • Loading branch information
krasznaa authored Sep 30, 2022
2 parents 93b25cd + 950fefd commit 0db4705
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 365 deletions.
12 changes: 0 additions & 12 deletions core/include/vecmem/containers/data/jagged_vector_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,6 @@ class jagged_vector_buffer : public jagged_vector_view<TYPE> {
memory_resource& resource,
memory_resource* host_access_resource = nullptr);

/// Access the host accessible array describing the inner vectors
///
/// This may or may not return the same pointer that
/// @c vecmem::data::jagged_vector_view::m_ptr holds. If the buffer is set
/// up on top of a "shared" (both host- and device accessible) memory
/// resource, then the two will be the same. If not, then
/// @c vecmem::data::jagged_vector_view::m_ptr is set up to point at the
/// device accessible array, and this function returns a pointer to the
/// host accessible one.
///
pointer host_ptr() const;

private:
/// Data object for the @c vecmem::data::vector_view array
vecmem::unique_alloc_ptr<value_type[]> m_outer_memory;
Expand Down
58 changes: 54 additions & 4 deletions core/include/vecmem/containers/data/jagged_vector_view.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -41,14 +41,17 @@ namespace data {
* an undefined state.
*/
template <typename T>
struct jagged_vector_view {
class jagged_vector_view {

public:
/// Size type used in the class
typedef std::size_t size_type;
/// Value type of the jagged array
typedef vector_view<T> value_type;
/// Pointer type to the jagged array
typedef value_type* pointer;
/// Constant pointer type to the jagged array
typedef const value_type* const_pointer;

/**
* Default constructor
Expand All @@ -58,7 +61,7 @@ struct jagged_vector_view {
* Constructor with all the information held by the object.
*/
VECMEM_HOST_AND_DEVICE
jagged_vector_view(size_type size, pointer ptr);
jagged_vector_view(size_type size, pointer ptr, pointer host_ptr = nullptr);

/**
* Constructor from a "slightly different" @c
Expand All @@ -73,8 +76,52 @@ struct jagged_vector_view {
typename OTHERTYPE,
std::enable_if_t<details::is_same_nc<T, OTHERTYPE>::value, bool> = true>
VECMEM_HOST_AND_DEVICE jagged_vector_view(
const jagged_vector_view<OTHERTYPE>& parent);
jagged_vector_view<OTHERTYPE> parent);

/// Assignment operator from a "slightly different" object
template <
typename OTHERTYPE,
std::enable_if_t<details::is_same_nc<T, OTHERTYPE>::value, bool> = true>
VECMEM_HOST_AND_DEVICE jagged_vector_view& operator=(
jagged_vector_view<OTHERTYPE> rhs);

/// Get the "outer" size of the jagged vector
VECMEM_HOST_AND_DEVICE
size_type size() const;
/// Get the maximum capacity of the "outer" vector
VECMEM_HOST_AND_DEVICE
size_type capacity() const;

/// Get a pointer to the vector elements (non-const)
VECMEM_HOST_AND_DEVICE
pointer ptr();
/// Get a pointer to the vector elements (const)
VECMEM_HOST_AND_DEVICE
const_pointer ptr() const;

/// Access the host accessible non-const array describing the inner vectors
///
/// This may or may not return the same pointer as @c ptr(). If the
/// underlying data is stored in host-accessible memory, then the two will
/// be the same.
///
/// If not, then @c ptr() will return the device accessible array, and this
/// function returns a host-accessible one.
///
/// @return A host-accessible pointer to the array describing the inner
/// vectors
///
VECMEM_HOST_AND_DEVICE
pointer host_ptr();
/// Access the host accessible const array describing the inner vectors
///
/// @return A host-accessible pointer to the array describing the inner
/// vectors
///
VECMEM_HOST_AND_DEVICE
const_pointer host_ptr() const;

protected:
/**
* The number of rows in this jagged vector.
*/
Expand All @@ -86,6 +133,9 @@ struct jagged_vector_view {
*/
pointer m_ptr;

/// Host-accessible pointer to the inner vector array
pointer m_host_ptr;

}; // struct jagged_vector_view

} // namespace data
Expand Down
18 changes: 3 additions & 15 deletions core/include/vecmem/containers/impl/jagged_device_vector.ipp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -15,20 +15,8 @@ namespace vecmem {

template <typename T>
VECMEM_HOST_AND_DEVICE jagged_device_vector<T>::jagged_device_vector(
const data::jagged_vector_view<T>& data)
: m_size(data.m_size), m_ptr(data.m_ptr) {}

template <typename T>
template <typename OTHERTYPE,
std::enable_if_t<details::is_same_nc<T, OTHERTYPE>::value, bool> >
VECMEM_HOST_AND_DEVICE jagged_device_vector<T>::jagged_device_vector(
const data::jagged_vector_view<OTHERTYPE>& data)
: m_size(data.m_size),
// This looks scarier than it really is. We "just" reinterpret a
// vecmem::data::vector_view<T> pointer to be seen as
// vecmem::data::vector_view<const T> instead.
m_ptr(reinterpret_cast<typename data::jagged_vector_view<T>::pointer>(
data.m_ptr)) {}
data::jagged_vector_view<T> data)
: m_size(data.size()), m_ptr(data.ptr()) {}

template <typename T>
VECMEM_HOST_AND_DEVICE jagged_device_vector<T>::jagged_device_vector(
Expand Down
8 changes: 4 additions & 4 deletions core/include/vecmem/containers/impl/jagged_vector.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data::jagged_vector_data<TYPE> get_data(jagged_vector<TYPE>& vec,

// Fill the result object with information.
for (std::size_t i = 0; i < size; ++i) {
result.m_ptr[i] =
result.host_ptr()[i] =
value_type(static_cast<size_type>(vec[i].size()), vec[i].data());
}

Expand Down Expand Up @@ -58,7 +58,7 @@ data::jagged_vector_data<TYPE> get_data(

// Fill the result object with information.
for (std::size_t i = 0; i < size; ++i) {
result.m_ptr[i] =
result.host_ptr()[i] =
value_type(static_cast<size_type>(vec[i].size()), vec[i].data());
}

Expand All @@ -85,7 +85,7 @@ data::jagged_vector_data<const TYPE> get_data(const jagged_vector<TYPE>& vec,

// Fill the result object with information.
for (std::size_t i = 0; i < size; ++i) {
result.m_ptr[i] =
result.host_ptr()[i] =
value_type(static_cast<size_type>(vec[i].size()), vec[i].data());
}

Expand Down Expand Up @@ -114,7 +114,7 @@ data::jagged_vector_data<const TYPE> get_data(

// Fill the result object with information.
for (std::size_t i = 0; i < size; ++i) {
result.m_ptr[i] =
result.host_ptr()[i] =
value_type(static_cast<size_type>(vec[i].size()), vec[i].data());
}

Expand Down
19 changes: 8 additions & 11 deletions core/include/vecmem/containers/impl/jagged_vector_buffer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ template <typename TYPE>
std::vector<std::size_t> get_sizes(
const vecmem::data::jagged_vector_view<TYPE>& jvv) {

std::vector<std::size_t> result(jvv.m_size);
for (std::size_t i = 0; i < jvv.m_size; ++i) {
result[i] = jvv.m_ptr[i].size();
std::vector<std::size_t> result(jvv.size());
for (typename vecmem::data::jagged_vector_view<TYPE>::size_type i = 0;
i < jvv.size(); ++i) {
result[i] = jvv.host_ptr()[i].size();
}
return result;
}
Expand Down Expand Up @@ -82,11 +83,12 @@ jagged_vector_buffer<TYPE>::jagged_vector_buffer(
base_type::m_ptr =
((host_access_resource != nullptr) ? m_outer_memory.get()
: m_outer_host_memory.get());
base_type::m_host_ptr = m_outer_host_memory.get();

// Set up the host accessible memory array.
std::ptrdiff_t ptrdiff = 0;
for (std::size_t i = 0; i < sizes.size(); ++i) {
new (host_ptr() + i)
new (base_type::host_ptr() + i)
value_type(static_cast<typename value_type::size_type>(sizes[i]),
reinterpret_cast<TYPE*>(m_inner_memory.get() + ptrdiff));
ptrdiff += sizes[i] * sizeof(TYPE);
Expand Down Expand Up @@ -124,23 +126,18 @@ jagged_vector_buffer<TYPE>::jagged_vector_buffer(
base_type::m_ptr =
((host_access_resource != nullptr) ? m_outer_memory.get()
: m_outer_host_memory.get());
base_type::m_host_ptr = m_outer_host_memory.get();

// Set up the vecmem::vector_view objects in the host accessible memory.
std::ptrdiff_t ptrdiff = 0;
for (std::size_t i = 0; i < capacities.size(); ++i) {
new (host_ptr() + i) value_type(
new (base_type::host_ptr() + i) value_type(
static_cast<typename value_type::size_type>(capacities[i]),
&header_ptr[i], data_ptr + ptrdiff);
ptrdiff += capacities[i];
}
}

template <typename TYPE>
auto jagged_vector_buffer<TYPE>::host_ptr() const -> pointer {

return m_outer_host_memory.get();
}

} // namespace data

template <typename TYPE>
Expand Down
3 changes: 2 additions & 1 deletion core/include/vecmem/containers/impl/jagged_vector_data.ipp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -41,6 +41,7 @@ jagged_vector_data<T>::jagged_vector_data(size_type size, memory_resource& mem)
m_memory(::allocate_jagged_memory<T>(size, mem)) {
// Point the base class at the newly allocated memory.
base_type::m_ptr = m_memory.get();
base_type::m_host_ptr = m_memory.get();

/*
* Construct vecmem::data::vector_view objects in the allocated area.
Expand Down
76 changes: 69 additions & 7 deletions core/include/vecmem/containers/impl/jagged_vector_view.ipp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -12,20 +12,82 @@ namespace vecmem {
namespace data {

template <typename T>
VECMEM_HOST_AND_DEVICE jagged_vector_view<T>::jagged_vector_view(size_type size,
pointer ptr)
: m_size(size), m_ptr(ptr) {}
VECMEM_HOST_AND_DEVICE jagged_vector_view<T>::jagged_vector_view(
size_type size, pointer ptr, pointer host_ptr)
: m_size(size),
m_ptr(ptr),
m_host_ptr(host_ptr != nullptr ? host_ptr : ptr) {}

template <typename T>
template <typename OTHERTYPE,
std::enable_if_t<details::is_same_nc<T, OTHERTYPE>::value, bool> >
VECMEM_HOST_AND_DEVICE jagged_vector_view<T>::jagged_vector_view(
const jagged_vector_view<OTHERTYPE>& parent)
: m_size(parent.m_size),
jagged_vector_view<OTHERTYPE> parent)
: m_size(parent.size()),
// This looks scarier than it really is. We "just" reinterpret a
// vecmem::data::vector_view<T> pointer to be seen as
// vecmem::data::vector_view<const T> instead.
m_ptr(reinterpret_cast<pointer>(parent.m_ptr)) {}
m_ptr(reinterpret_cast<pointer>(parent.ptr())),
m_host_ptr(reinterpret_cast<pointer>(parent.host_ptr())) {}

template <typename T>
template <typename OTHERTYPE,
std::enable_if_t<details::is_same_nc<T, OTHERTYPE>::value, bool> >
VECMEM_HOST_AND_DEVICE jagged_vector_view<T>& jagged_vector_view<T>::operator=(
jagged_vector_view<OTHERTYPE> rhs) {

// Avoid self-assignment.
if (this == &rhs) {
return *this;
}

// Perform the assignment.
m_size = rhs.m_size;
m_ptr = reinterpret_cast<pointer>(rhs.m_ptr);
m_host_ptr = reinterpret_cast<pointer>(rhs.m_host_ptr);
}

template <typename T>
VECMEM_HOST_AND_DEVICE typename jagged_vector_view<T>::size_type
jagged_vector_view<T>::size() const {

return m_size;
}

template <typename T>
VECMEM_HOST_AND_DEVICE typename jagged_vector_view<T>::size_type
jagged_vector_view<T>::capacity() const {

return m_size;
}

template <typename T>
VECMEM_HOST_AND_DEVICE typename jagged_vector_view<T>::pointer
jagged_vector_view<T>::ptr() {

return m_ptr;
}

template <typename T>
VECMEM_HOST_AND_DEVICE typename jagged_vector_view<T>::const_pointer
jagged_vector_view<T>::ptr() const {

return m_ptr;
}

template <typename T>
VECMEM_HOST_AND_DEVICE typename jagged_vector_view<T>::pointer
jagged_vector_view<T>::host_ptr() {

return m_host_ptr;
}

template <typename T>
VECMEM_HOST_AND_DEVICE typename jagged_vector_view<T>::const_pointer
jagged_vector_view<T>::host_ptr() const {

return m_host_ptr;
}

} // namespace data
} // namespace vecmem
10 changes: 2 additions & 8 deletions core/include/vecmem/containers/jagged_device_vector.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* VecMem project, part of the ACTS project (R&D line)
*
* (c) 2021 CERN for the benefit of the ACTS project
* (c) 2021-2022 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -78,13 +78,7 @@ class jagged_device_vector {
* object.
*/
VECMEM_HOST_AND_DEVICE
jagged_device_vector(const data::jagged_vector_view<T>& data);
/// Construct a const jagged device vector from a non-const data object
template <
typename OTHERTYPE,
std::enable_if_t<details::is_same_nc<T, OTHERTYPE>::value, bool> = true>
VECMEM_HOST_AND_DEVICE jagged_device_vector(
const data::jagged_vector_view<OTHERTYPE>& data);
jagged_device_vector(data::jagged_vector_view<T> data);

/// Copy constructor
VECMEM_HOST_AND_DEVICE
Expand Down
Loading

0 comments on commit 0db4705

Please sign in to comment.