CopyToHost

TestSoALayoutWithPtr

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#ifndef DataFormats_PortableTest_interface_TestProductWithPtr_h
#define DataFormats_PortableTest_interface_TestProductWithPtr_h

#include "DataFormats/Portable/interface/PortableCollection.h"
#include "DataFormats/SoATemplate/interface/SoACommon.h"
#include "DataFormats/SoATemplate/interface/SoALayout.h"
#include "DataFormats/SoATemplate/interface/SoAView.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToHost.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToDevice.h"

#include <alpaka/alpaka.hpp>

/**
 * This data product is part of a test for CopyToHost::postCopy()
 * (i.e. updating a data product after the device-to-host copy). For
 * any practical purposes the indirection to 'buffer' array via the
 * 'ptr' pointer scalar is completely unnecessary. Do not take this
 * case as an example for good design of a data product.
 */
namespace portabletest {
  GENERATE_SOA_LAYOUT(TestSoALayoutWithPtr, SOA_COLUMN(int, buffer), SOA_SCALAR(int*, ptr));
  using TestSoAWithPtr = TestSoALayoutWithPtr<>;

  template <typename TDev>
  using TestProductWithPtr = PortableCollection<TestSoAWithPtr, TDev>;

  ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE void setPtrInTestProductWithPtr(TestSoAWithPtr::View view) {
    view.ptr() = &view.buffer(0);
  }
}  // namespace portabletest

namespace cms::alpakatools {
  template <typename TDev>
  struct CopyToHost<PortableDeviceCollection<portabletest::TestSoAWithPtr, TDev>> {
    template <typename TQueue>
    static auto copyAsync(TQueue& queue, PortableDeviceCollection<portabletest::TestSoAWithPtr, TDev> const& src) {
      PortableHostCollection<portabletest::TestSoAWithPtr> dst(src->metadata().size(), queue);
      alpaka::memcpy(queue, dst.buffer(), src.buffer());
      return dst;
    }

    static void postCopy(PortableHostCollection<portabletest::TestSoAWithPtr>& dst) {
      portabletest::setPtrInTestProductWithPtr(dst.view());
    }
  };
}  // namespace cms::alpakatools

#endif