Skip to content

Commit

Permalink
Merge pull request #39312 from fwyzard/alpaka_framework
Browse files Browse the repository at this point in the history
Update alpaka::allocMappedBuf memory functions [12.5.x]
  • Loading branch information
cmsbuild committed Sep 7, 2022
2 parents d08a098 + da2b7bc commit 50fe088
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 49 deletions.
1 change: 0 additions & 1 deletion DataFormats/Portable/BuildFile.xml
@@ -1,3 +1,2 @@
<use name="alpaka"/>
<use name="DataFormats/SoATemplate" source_only="1"/>
<use name="HeterogeneousCore/AlpakaInterface" source_only="1"/>
33 changes: 18 additions & 15 deletions DataFormats/Portable/interface/PortableDeviceCollection.h
Expand Up @@ -23,7 +23,7 @@ class PortableDeviceCollection {

PortableDeviceCollection() = default;

PortableDeviceCollection(int32_t elements, TDev const &device)
PortableDeviceCollection(int32_t elements, TDev const& device)
: buffer_{cms::alpakatools::make_device_buffer<std::byte[]>(device, Layout::computeDataSize(elements))},
layout_{buffer_->data(), elements},
view_{layout_} {
Expand All @@ -32,34 +32,37 @@ class PortableDeviceCollection {
}

template <typename TQueue, typename = std::enable_if_t<cms::alpakatools::is_queue_v<TQueue>>>
PortableDeviceCollection(int32_t elements, TQueue const &queue)
PortableDeviceCollection(int32_t elements, TQueue const& queue)
: buffer_{cms::alpakatools::make_device_buffer<std::byte[]>(queue, Layout::computeDataSize(elements))},
layout_{buffer_->data(), elements},
view_{layout_} {
// Alpaka set to a default alignment of 128 bytes defining ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT=128
assert(reinterpret_cast<uintptr_t>(buffer_->data()) % Layout::alignment == 0);
}

~PortableDeviceCollection() = default;

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

// movable
PortableDeviceCollection(PortableDeviceCollection &&other) = default;
PortableDeviceCollection &operator=(PortableDeviceCollection &&other) = default;
PortableDeviceCollection(PortableDeviceCollection&&) = default;
PortableDeviceCollection& operator=(PortableDeviceCollection&&) = default;

// default destructor
~PortableDeviceCollection() = default;

View &view() { return view_; }
ConstView const &view() const { return view_; }
ConstView const &const_view() const { return view_; }
// access the View
View& view() { return view_; }
ConstView const& view() const { return view_; }
ConstView const& const_view() const { return view_; }

View &operator*() { return view_; }
ConstView const &operator*() const { return view_; }
View& operator*() { return view_; }
ConstView const& operator*() const { return view_; }

View *operator->() { return &view_; }
ConstView const *operator->() const { return &view_; }
View* operator->() { return &view_; }
ConstView const* operator->() const { return &view_; }

// access the Buffer
Buffer buffer() { return *buffer_; }
ConstBuffer buffer() const { return *buffer_; }
ConstBuffer const_buffer() const { return *buffer_; }
Expand Down
36 changes: 19 additions & 17 deletions DataFormats/Portable/interface/PortableHostCollection.h
Expand Up @@ -3,7 +3,6 @@

#include <optional>

#include "DataFormats/SoATemplate/interface/SoACommon.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/host.h"
#include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
Expand All @@ -21,7 +20,7 @@ class PortableHostCollection {

PortableHostCollection() = default;

PortableHostCollection(int32_t elements, alpaka_common::DevHost const &host)
PortableHostCollection(int32_t elements, alpaka_common::DevHost const& host)
// allocate pageable host memory
: buffer_{cms::alpakatools::make_host_buffer<std::byte[]>(Layout::computeDataSize(elements))},
layout_{buffer_->data(), elements},
Expand All @@ -31,7 +30,7 @@ class PortableHostCollection {
}

template <typename TQueue, typename = std::enable_if_t<cms::alpakatools::is_queue_v<TQueue>>>
PortableHostCollection(int32_t elements, TQueue const &queue)
PortableHostCollection(int32_t elements, TQueue const& queue)
// allocate pinned host memory associated to the given work queue, accessible by the queue's device
: buffer_{cms::alpakatools::make_host_buffer<std::byte[]>(queue, Layout::computeDataSize(elements))},
layout_{buffer_->data(), elements},
Expand All @@ -40,32 +39,35 @@ class PortableHostCollection {
assert(reinterpret_cast<uintptr_t>(buffer_->data()) % Layout::alignment == 0);
}

~PortableHostCollection() = default;

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

// movable
PortableHostCollection(PortableHostCollection &&other) = default;
PortableHostCollection &operator=(PortableHostCollection &&other) = default;
PortableHostCollection(PortableHostCollection&&) = default;
PortableHostCollection& operator=(PortableHostCollection&&) = default;

// default destructor
~PortableHostCollection() = default;

View &view() { return view_; }
ConstView const &view() const { return view_; }
ConstView const &const_view() const { return view_; }
// access the View
View& view() { return view_; }
ConstView const& view() const { return view_; }
ConstView const& const_view() const { return view_; }

View &operator*() { return view_; }
ConstView const &operator*() const { return view_; }
View& operator*() { return view_; }
ConstView const& operator*() const { return view_; }

View *operator->() { return &view_; }
ConstView const *operator->() const { return &view_; }
View* operator->() { return &view_; }
ConstView const* operator->() const { return &view_; }

// access the Buffer
Buffer buffer() { return *buffer_; }
ConstBuffer buffer() const { return *buffer_; }
ConstBuffer const_buffer() const { return *buffer_; }

// part of the ROOT read streamer
static void ROOTReadStreamer(PortableHostCollection *newObj, Layout const &layout) {
static void ROOTReadStreamer(PortableHostCollection* newObj, Layout const& layout) {
newObj->~PortableHostCollection();
// use the global "host" object returned by cms::alpakatools::host()
new (newObj) PortableHostCollection(layout.metadata().size(), cms::alpakatools::host());
Expand Down
Expand Up @@ -9,7 +9,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {

namespace portabletest {

// import the top-level portabletest namespace
// make the names from the top-level portabletest namespace visible for unqualified lookup
// inside the ALPAKA_ACCELERATOR_NAMESPACE::portabletest namespace
using namespace ::portabletest;

// SoA with x, y, z, id fields in device global memory
Expand Down
Expand Up @@ -336,8 +336,8 @@ namespace cms::alpakatools {
// allocate device memory
return alpaka::allocBuf<std::byte, size_t>(device_, bytes);
} else if constexpr (std::is_same_v<Device, alpaka::DevCpu>) {
// allocate pinned host memory
return alpaka::allocMappedBuf<std::byte, size_t>(device_, alpaka::getDev(queue), bytes);
// allocate pinned host memory accessible by the queue's platform
return alpaka::allocMappedBuf<alpaka::Pltf<alpaka::Dev<Queue>>, std::byte, size_t>(device_, bytes);
} else {
// unsupported combination
static_assert(std::is_same_v<Device, alpaka::Dev<Queue>> or std::is_same_v<Device, alpaka::DevCpu>,
Expand Down
29 changes: 25 additions & 4 deletions HeterogeneousCore/AlpakaInterface/interface/memory.h
Expand Up @@ -77,6 +77,26 @@ namespace cms::alpakatools {
return alpaka::allocBuf<std::remove_extent_t<T>, Idx>(host(), Vec1D{std::extent_v<T>});
}

// non-cached, pinned, scalar and 1-dimensional host buffers
// the memory is pinned according to the device associated to the platform

template <typename T, typename TPlatform>
std::enable_if_t<not std::is_array_v<T>, host_buffer<T>> make_host_buffer() {
return alpaka::allocMappedBuf<TPlatform, T, Idx>(host(), Scalar{});
}

template <typename T, typename TPlatform>
std::enable_if_t<cms::is_unbounded_array_v<T> and not std::is_array_v<std::remove_extent_t<T>>, host_buffer<T>>
make_host_buffer(Extent extent) {
return alpaka::allocMappedBuf<TPlatform, std::remove_extent_t<T>, Idx>(host(), Vec1D{extent});
}

template <typename T, typename TPlatform>
std::enable_if_t<cms::is_bounded_array_v<T> and not std::is_array_v<std::remove_extent_t<T>>, host_buffer<T>>
make_host_buffer() {
return alpaka::allocMappedBuf<TPlatform, std::remove_extent_t<T>, Idx>(host(), Vec1D{std::extent_v<T>});
}

// potentially cached, pinned, scalar and 1-dimensional host buffers, associated to a work queue
// the memory is pinned according to the device associated to the queue

Expand All @@ -85,7 +105,7 @@ namespace cms::alpakatools {
if constexpr (allocator_policy<alpaka::Dev<TQueue>> == AllocatorPolicy::Caching) {
return allocCachedBuf<T, Idx>(host(), queue, Scalar{});
} else {
return alpaka::allocMappedBuf<T, Idx>(host(), alpaka::getDev(queue), Scalar{});
return alpaka::allocMappedBuf<alpaka::Pltf<alpaka::Dev<TQueue>>, T, Idx>(host(), Scalar{});
}
}

Expand All @@ -96,7 +116,8 @@ namespace cms::alpakatools {
if constexpr (allocator_policy<alpaka::Dev<TQueue>> == AllocatorPolicy::Caching) {
return allocCachedBuf<std::remove_extent_t<T>, Idx>(host(), queue, Vec1D{extent});
} else {
return alpaka::allocMappedBuf<std::remove_extent_t<T>, Idx>(host(), alpaka::getDev(queue), Vec1D{extent});
return alpaka::allocMappedBuf<alpaka::Pltf<alpaka::Dev<TQueue>>, std::remove_extent_t<T>, Idx>(host(),
Vec1D{extent});
}
}

Expand All @@ -107,8 +128,8 @@ namespace cms::alpakatools {
if constexpr (allocator_policy<alpaka::Dev<TQueue>> == AllocatorPolicy::Caching) {
return allocCachedBuf<std::remove_extent_t<T>, Idx>(host(), queue, Vec1D{std::extent_v<T>});
} else {
return alpaka::allocMappedBuf<std::remove_extent_t<T>, Idx>(
host(), alpaka::getDev(queue), Vec1D{std::extent_v<T>});
return alpaka::allocMappedBuf<alpaka::Pltf<alpaka::Dev<TQueue>>, std::remove_extent_t<T>, Idx>(
host(), Vec1D{std::extent_v<T>});
}
}

Expand Down
1 change: 0 additions & 1 deletion HeterogeneousCore/AlpakaTest/plugins/TestAlpakaAnalyzer.cc
@@ -1,5 +1,4 @@
#include <cassert>
#include <string>

#include "DataFormats/PortableTestObjects/interface/TestHostCollection.h"
#include "FWCore/Framework/interface/Event.h"
Expand Down
@@ -1,8 +1,3 @@
#include <optional>
#include <string>

#include <alpaka/alpaka.hpp>

#include "DataFormats/Portable/interface/Product.h"
#include "DataFormats/PortableTestObjects/interface/alpaka/TestDeviceCollection.h"
#include "FWCore/Framework/interface/Event.h"
Expand Down
@@ -1,6 +1,3 @@
#include <optional>
#include <string>

#include <alpaka/alpaka.hpp>

#include "DataFormats/Portable/interface/Product.h"
Expand Down

0 comments on commit 50fe088

Please sign in to comment.