Skip to content

Commit

Permalink
Add read streamer and validation printout to check the memory layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcano authored and fwyzard committed May 28, 2022
1 parent 48078c0 commit 3243228
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
31 changes: 29 additions & 2 deletions DataFormats/Portable/interface/PortableCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class PortableCollection {
public:
using Buffer = alpaka::Buf<TDev, std::byte, alpaka::DimInt<1u>, uint32_t>;

PortableCollection() : buffer_{}, layout_{} {}
PortableCollection() : buffer_{}, layout_{} {
#ifdef DEBUG_COLLECTION_CTOR_DTOR
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
#endif // DEBUG_COLLECTION_CTOR_DTOR
}

PortableCollection(int32_t elements, TDev const &device)
: buffer_{alpaka::allocBuf<std::byte, uint32_t>(
Expand All @@ -22,16 +26,30 @@ class PortableCollection {
// Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
assert(reinterpret_cast<uintptr_t>(buffer_->data()) % T::alignment == 0);
alpaka::pin(*buffer_);
#ifdef DEBUG_COLLECTION_CTOR_DTOR
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
#endif // DEBUG_COLLECTION_CTOR_DTOR
}

~PortableCollection() {}
~PortableCollection() {
#ifdef DEBUG_COLLECTION_CTOR_DTOR
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
#endif // DEBUG_COLLECTION_CTOR_DTOR
}

// non-copyable
PortableCollection(PortableCollection const &) = delete;
PortableCollection &operator=(PortableCollection const &) = delete;

// movable
#ifdef DEBUG_COLLECTION_CTOR_DTOR
PortableCollection(PortableCollection &&other)
: buffer_{std::move(other.buffer_)}, layout_{std::move(other.layout_)} {
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
}
#else
PortableCollection(PortableCollection &&other) = default;
#endif // DEBUG_COLLECTION_CTOR_DTOR
PortableCollection &operator=(PortableCollection &&other) = default;

T &operator*() { return layout_; }
Expand All @@ -46,6 +64,15 @@ class PortableCollection {

Buffer const &buffer() const { return *buffer_; }

#if defined ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
template <typename U>
static void ROOTReadStreamer(PortableCollection *newObj, U onfile) {
newObj->~PortableCollection();
new (newObj) PortableCollection(onfile.layout_.size(), host);
newObj->layout_.ROOTReadStreamer(onfile);
}
#endif

private:
std::optional<Buffer> buffer_; //!
T layout_;
Expand Down
34 changes: 31 additions & 3 deletions DataFormats/Portable/interface/PortableHostCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,67 @@
#include "DataFormats/Portable/interface/PortableCollection.h"
#include "HeterogeneousCore/AlpakaInterface/interface/alpaka/config.h"

// generic SoA-based product in pinned host memory
// generic SoA-based product in host memory
template <typename T>
class PortableCollection<T, alpaka_common::DevHost> {
public:
using Buffer = alpaka::Buf<alpaka_common::DevHost, std::byte, alpaka::DimInt<1u>, uint32_t>;

PortableCollection() : buffer_{}, layout_{} {}
PortableCollection() : buffer_{}, layout_{} {
#ifdef DEBUG_COLLECTION_CTOR_DTOR
std::cout << "[partial template specialisation]" << std::endl;
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
#endif // DEBUG_COLLECTION_CTOR_DTOR
}

PortableCollection(int32_t elements, alpaka_common::DevHost const &host)
// allocate pageable host memory
: buffer_{alpaka::allocBuf<std::byte, uint32_t>(
host, alpaka::Vec<alpaka::DimInt<1u>, uint32_t>{T::compute_size(elements)})},
layout_{elements, buffer_->data()} {
// Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
assert(reinterpret_cast<uintptr_t>(buffer_->data()) % T::alignment == 0);
#ifdef DEBUG_COLLECTION_CTOR_DTOR
std::cout << "[partial template specialisation]" << std::endl;
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
#endif // DEBUG_COLLECTION_CTOR_DTOR
}

template <typename TDev>
PortableCollection(int32_t elements, alpaka_common::DevHost const &host, TDev const &device)
// allocate pinned host memory, accessible by the given device
: buffer_{alpaka::allocMappedBuf<std::byte, uint32_t>(
host, device, alpaka::Vec<alpaka::DimInt<1u>, uint32_t>{T::compute_size(elements)})},
layout_{elements, buffer_->data()} {
// Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
assert(reinterpret_cast<uintptr_t>(buffer_->data()) % T::alignment == 0);
#ifdef DEBUG_COLLECTION_CTOR_DTOR
std::cout << "[partial template specialisation]" << std::endl;
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
#endif // DEBUG_COLLECTION_CTOR_DTOR
}

~PortableCollection() {}
~PortableCollection() {
#ifdef DEBUG_COLLECTION_CTOR_DTOR
std::cout << "[partial template specialisation]" << std::endl;
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
#endif // DEBUG_COLLECTION_CTOR_DTOR
}

// non-copyable
PortableCollection(PortableCollection const &) = delete;
PortableCollection &operator=(PortableCollection const &) = delete;

// movable
#ifdef DEBUG_COLLECTION_CTOR_DTOR
PortableCollection(PortableCollection &&other)
: buffer_{std::move(other.buffer_)}, layout_{std::move(other.layout_)} {
std::cout << "[partial template specialisation]" << std::endl;
std::cout << __PRETTY_FUNCTION__ << " [this=" << this << "]" << std::endl;
}
#else
PortableCollection(PortableCollection &&other) = default;
#endif // DEBUG_COLLECTION_CTOR_DTOR
PortableCollection &operator=(PortableCollection &&other) = default;

T &operator*() { return layout_; }
Expand Down
9 changes: 9 additions & 0 deletions DataFormats/XyzId/interface/XyzIdSoA.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ class XyzIdSoA {
elements * sizeof(int32_t); // id - no need to pad the last field
}

template <typename T>
void ROOTReadStreamer(T onfile) {
auto size = onfile.layout_.size();
memcpy(x_, &onfile.layout_.x(0), size * sizeof(*x_));
memcpy(y_, &onfile.layout_.y(0), size * sizeof(*y_));
memcpy(z_, &onfile.layout_.z(0), size * sizeof(*z_));
memcpy(id_, &onfile.layout_.id(0), size * sizeof(*id_));
}

private:
// non-owned memory
cms_int32_t size_; // must be the same as ROOT's Int_t
Expand Down
11 changes: 11 additions & 0 deletions DataFormats/XyzId/src/alpaka/classes_serial_def.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<lcgdict>
<class name="alpaka_serial_sync::XyzIdDeviceCollection"/>
<read
sourceClass="alpaka_serial_sync::XyzIdDeviceCollection"
targetClass="alpaka_serial_sync::XyzIdDeviceCollection"
version="[1-]"
source="XyzIdSoA layout_;"
target="buffer_"
embed="false">
<![CDATA[
alpaka_serial_sync::XyzIdDeviceCollection::ROOTReadStreamer(newObj, onfile);
]]>
</read>
<class name="edm::Wrapper<alpaka_serial_sync::XyzIdDeviceCollection>" splitLevel="0"/>
</lcgdict>
12 changes: 12 additions & 0 deletions HeterogeneousCore/AlpakaTest/plugins/alpaka/XyzIdAlpakaAnalyzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
assert(product->id(i) == i);
}
std::cout << "XyzIdAlpakaAnalyzer:\n" << source_.encode() << ".size() = " << product->size() << std::endl;
/*
std::cout << "data = " << product->data() << " x = " << &product->x(0) << " y = " << &product->y(0)
<< " z = " << &product->z(0) << " id = " << &product->id(0) << std::endl;
std::cout << "[y - x] = " << std::hex
<< reinterpret_cast<intptr_t>(&product->y(0)) - reinterpret_cast<intptr_t>(&product->x(0))
<< " [z - y] = "
<< reinterpret_cast<intptr_t>(&product->z(0)) - reinterpret_cast<intptr_t>(&product->y(0))
<< " [id - z] = "
<< reinterpret_cast<intptr_t>(&product->id(0)) - reinterpret_cast<intptr_t>(&product->z(0)) << std::dec
<< std::endl
<< std::endl;
*/
}

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
Expand Down

0 comments on commit 3243228

Please sign in to comment.