Skip to content

Commit

Permalink
Simplify the definition of Portable{Collection,Object}<T, TDev> alias…
Browse files Browse the repository at this point in the history
… templates

Also move the CopyTo{Host,Device} specialisations to interface/ as
they are independent from ALPAKA_ACCELERATOR_NAMESPACE.
  • Loading branch information
makortel committed Dec 22, 2023
1 parent c607f8a commit 1143991
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 131 deletions.
39 changes: 37 additions & 2 deletions DataFormats/Portable/interface/PortableCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,53 @@

#include <alpaka/alpaka.hpp>

#include "HeterogeneousCore/AlpakaInterface/interface/traits.h"
#include "DataFormats/Portable/interface/PortableHostCollection.h"
#include "DataFormats/Portable/interface/PortableDeviceCollection.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToDevice.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToHost.h"

namespace traits {

// trait for a generic SoA-based product
template <typename T, typename TDev, typename = std::enable_if_t<alpaka::isDevice<TDev>>>
struct PortableCollectionTrait;
struct PortableCollectionTrait {
using CollectionType = PortableDeviceCollection<T, TDev>;
};

// specialise for host device
template <typename T>
struct PortableCollectionTrait<T, alpaka_common::DevHost> {
using CollectionType = PortableHostCollection<T>;
};

} // namespace traits

// type alias for a generic SoA-based product
template <typename T, typename TDev, typename = std::enable_if_t<alpaka::isDevice<TDev>>>
using PortableCollection = typename traits::PortableCollectionTrait<T, TDev>::CollectionType;

// define how to copy PortableCollection between host and device
namespace cms::alpakatools {
template <typename TLayout, typename TDevice>
struct CopyToHost<PortableDeviceCollection<TLayout, TDevice>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableDeviceCollection<TLayout, TDevice> const& srcData) {
PortableHostCollection<TLayout> dstData(srcData->metadata().size(), queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};

template <typename TLayout>
struct CopyToDevice<PortableHostCollection<TLayout>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableHostCollection<TLayout> const& srcData) {
using TDevice = typename alpaka::trait::DevType<TQueue>::type;
PortableDeviceCollection<TLayout, TDevice> dstData(srcData->metadata().size(), queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};
} // namespace cms::alpakatools

#endif // DataFormats_Portable_interface_PortableCollection_h
10 changes: 0 additions & 10 deletions DataFormats/Portable/interface/PortableHostCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <alpaka/alpaka.hpp>

#include "DataFormats/Portable/interface/PortableCollection.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/host.h"
#include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
Expand Down Expand Up @@ -87,13 +86,4 @@ class PortableHostCollection {
View view_; //!
};

// Make PortableCollection<T, TDev> alias template to work also
// independently of ALPAKA_ACCELERATOR_NAMESPACE
namespace traits {
template <typename T>
struct PortableCollectionTrait<T, alpaka::DevCpu> {
using CollectionType = PortableHostCollection<T>;
};
} // namespace traits

#endif // DataFormats_Portable_interface_PortableHostCollection_h
10 changes: 0 additions & 10 deletions DataFormats/Portable/interface/PortableHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <alpaka/alpaka.hpp>

#include "DataFormats/Portable/interface/PortableObject.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/host.h"
#include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
Expand Down Expand Up @@ -79,13 +78,4 @@ class PortableHostObject {
Product* product_;
};

// Make PortableObject<T, TDev> alias template to work also
// independently of ALPAKA_ACCELERATOR_NAMESPACE
namespace traits {
template <typename T>
struct PortableObjectTrait<T, alpaka::DevCpu> {
using ProductType = PortableHostObject<T>;
};
} // namespace traits

#endif // DataFormats_Portable_interface_PortableHostObject_h
43 changes: 40 additions & 3 deletions DataFormats/Portable/interface/PortableObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,53 @@

#include <alpaka/alpaka.hpp>

#include "DataFormats/Portable/interface/PortableHostObject.h"
#include "DataFormats/Portable/interface/PortableDeviceObject.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToDevice.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToHost.h"

namespace traits {

// trait for a generic SoA-based product
// trait for a generic struct-based product
template <typename T, typename TDev, typename = std::enable_if_t<alpaka::isDevice<TDev>>>
struct PortableObjectTrait;
struct PortableObjectTrait {
using ProductType = PortableDeviceObject<T, TDev>;
};

// specialise for host device
template <typename T>
struct PortableObjectTrait<T, alpaka_common::DevHost> {
using ProductType = PortableHostObject<T>;
};

} // namespace traits

// type alias for a generic SoA-based product
// type alias for a generic struct-based product
template <typename T, typename TDev, typename = std::enable_if_t<alpaka::isDevice<TDev>>>
using PortableObject = typename traits::PortableObjectTrait<T, TDev>::ProductType;

// define how to copy PortableObject between host and device
namespace cms::alpakatools {
template <typename TProduct, typename TDevice>
struct CopyToHost<PortableDeviceObject<TProduct, TDevice>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableDeviceObject<TProduct, TDevice> const& srcData) {
PortableHostObject<TProduct> dstData(queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};

template <typename TProduct>
struct CopyToDevice<PortableHostObject<TProduct>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableHostObject<TProduct> const& srcData) {
using TDevice = typename alpaka::trait::DevType<TQueue>::type;
PortableDeviceObject<TProduct, TDevice> dstData(queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};
} // namespace cms::alpakatools

#endif // DataFormats_Portable_interface_PortableObject_h
55 changes: 2 additions & 53 deletions DataFormats/Portable/interface/alpaka/PortableCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,18 @@
#include <alpaka/alpaka.hpp>

#include "DataFormats/Portable/interface/PortableCollection.h"
#include "DataFormats/Portable/interface/PortableHostCollection.h"
#include "DataFormats/Portable/interface/PortableDeviceCollection.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToDevice.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToHost.h"

// This header is not used by PortableCollection, but is included here to automatically
// provide its content to users of ALPAKA_ACCELERATOR_NAMESPACE::PortableCollection.
#include "HeterogeneousCore/AlpakaInterface/interface/AssertDeviceMatchesHostCollection.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE {

#if defined ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
// ... or any other CPU-based accelerators

// generic SoA-based product in host memory
template <typename T>
using PortableCollection = ::PortableHostCollection<T>;

#else

// generic SoA-based product in device memory
// generic SoA-based product in the device (that may be host) memory
template <typename T>
using PortableCollection = ::PortableDeviceCollection<T, Device>;

#endif // ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
using PortableCollection = ::PortableCollection<T, Device>;

} // namespace ALPAKA_ACCELERATOR_NAMESPACE

// For DevHost the specialisation is already done in the PortableHostCollection.h
#ifndef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
namespace traits {

// specialise the trait for the device provided by the ALPAKA_ACCELERATOR_NAMESPACE
template <typename T>
struct PortableCollectionTrait<T, ALPAKA_ACCELERATOR_NAMESPACE::Device> {
using CollectionType = ALPAKA_ACCELERATOR_NAMESPACE::PortableCollection<T>;
};

} // namespace traits
#endif

namespace cms::alpakatools {
template <typename TLayout, typename TDevice>
struct CopyToHost<PortableDeviceCollection<TLayout, TDevice>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableDeviceCollection<TLayout, TDevice> const& srcData) {
PortableHostCollection<TLayout> dstData(srcData->metadata().size(), queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};

template <typename TLayout>
struct CopyToDevice<PortableHostCollection<TLayout>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableHostCollection<TLayout> const& srcData) {
using TDevice = typename alpaka::trait::DevType<TQueue>::type;
PortableDeviceCollection<TLayout, TDevice> dstData(srcData->metadata().size(), queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};
} // namespace cms::alpakatools

#endif // DataFormats_Portable_interface_alpaka_PortableCollection_h
55 changes: 2 additions & 53 deletions DataFormats/Portable/interface/alpaka/PortableObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,18 @@
#include <alpaka/alpaka.hpp>

#include "DataFormats/Portable/interface/PortableObject.h"
#include "DataFormats/Portable/interface/PortableHostObject.h"
#include "DataFormats/Portable/interface/PortableDeviceObject.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToDevice.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToHost.h"

// This header is not used by PortableObject, but is included here to automatically
// provide its content to users of ALPAKA_ACCELERATOR_NAMESPACE::PortableObject.
#include "HeterogeneousCore/AlpakaInterface/interface/AssertDeviceMatchesHostCollection.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE {

#if defined ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
// ... or any other CPU-based accelerators

// generic SoA-based product in host memory
template <typename T>
using PortableObject = ::PortableHostObject<T>;

#else

// generic SoA-based product in device memory
// generic struct-based product in the device (that may be host) memory
template <typename T>
using PortableObject = ::PortableDeviceObject<T, Device>;

#endif // ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
using PortableObject = ::PortableObject<T, Device>;

} // namespace ALPAKA_ACCELERATOR_NAMESPACE

// For DevHost the specialisation is already done in the PortableHostCollection.h
#ifndef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
namespace traits {

// specialise the trait for the device provided by the ALPAKA_ACCELERATOR_NAMESPACE
template <typename T>
struct PortableObjectTrait<T, ALPAKA_ACCELERATOR_NAMESPACE::Device> {
using ProductType = ALPAKA_ACCELERATOR_NAMESPACE::PortableObject<T>;
};

} // namespace traits
#endif

namespace cms::alpakatools {
template <typename TProduct, typename TDevice>
struct CopyToHost<PortableDeviceObject<TProduct, TDevice>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableDeviceObject<TProduct, TDevice> const& srcData) {
PortableHostObject<TProduct> dstData(queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};

template <typename TProduct>
struct CopyToDevice<PortableHostObject<TProduct>> {
template <typename TQueue>
static auto copyAsync(TQueue& queue, PortableHostObject<TProduct> const& srcData) {
using TDevice = typename alpaka::trait::DevType<TQueue>::type;
PortableDeviceObject<TProduct, TDevice> dstData(queue);
alpaka::memcpy(queue, dstData.buffer(), srcData.buffer());
return dstData;
}
};
} // namespace cms::alpakatools

#endif // DataFormats_Portable_interface_alpaka_PortableObject_h

0 comments on commit 1143991

Please sign in to comment.