Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-19 04:04:41

0001 #ifndef HeterogeneousCore_AlpakaInterface_interface_CopyToDevice_h
0002 #define HeterogeneousCore_AlpakaInterface_interface_CopyToDevice_h
0003 
0004 // TODO: this utility class is specific to CMSSW, but needs to be in a
0005 // package that is suitable as DataFormat dependence
0006 
0007 namespace cms::alpakatools {
0008   /**
0009    * This class template needs to be specialized for each host-side
0010    * EventSetup data product that should be implicitly copied to the
0011    * device memory. The specialization is expected to define static
0012    * copyAsync() function as in the following example
0013    *
0014    * \code
0015    * template <>
0016    * struct CopyToDevice<ExampleHostProduct> {
0017    *   template <typename TQueue>
0018    *   static auto copyAsync(TQueue& queue, ExampleHostProduct const& hostData) {
0019    *     // construct ExampleDeviceProduct corresponding the device of the TQueue
0020    *     // asynchronous copy hostData to the ExampleDeviceProduct object
0021    *     // return ExampleDeviceProduct object by value
0022    *   }
0023    * };
0024    * \endcode
0025    *
0026    * The copyAsync() function should not explicitly synchronize the
0027    * queue. The ExampleHostProduct and ExampleDevicxeProduct can be the
0028    * same type, if they internally are able to handle the memory
0029    * allocation difference between host and device.
0030    */
0031   template <typename THostData>
0032   struct CopyToDevice;
0033 }  // namespace cms::alpakatools
0034 
0035 // specialize to Alpaka buffer
0036 #include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
0037 namespace cms::alpakatools {
0038   // Note: can't do partial specializations along
0039   // - CopyToDevice<host_buffer<TObject>>
0040   // - CopyToDevice<alpaka::Buf<alpaka_common::DevHost, TObject, alpaka_common::Dim0D, alpaka_common::Idx>e
0041   // because both host_buffer and alpaka::Buf use trait-style
0042   // indirection that prevents template argument type deduction
0043   template <typename TObject>
0044   struct CopyToDevice<alpaka::BufCpu<TObject, alpaka_common::Dim0D, alpaka_common::Idx>> {
0045     template <typename TQueue>
0046     static auto copyAsync(TQueue& queue, host_buffer<TObject> const& src) {
0047       using TDevice = alpaka::Dev<TQueue>;
0048       auto dst = make_device_buffer<TObject>(queue);
0049       alpaka::memcpy(queue, dst, src);
0050       return dst;
0051     }
0052   };
0053 
0054   template <typename TObject>
0055   struct CopyToDevice<alpaka::BufCpu<TObject, alpaka_common::Dim1D, alpaka_common::Idx>> {
0056     template <typename TQueue>
0057     static auto copyAsync(TQueue& queue, host_buffer<TObject[]> const& src) {
0058       using TDevice = alpaka::Dev<TQueue>;
0059       auto dst = make_device_buffer<TObject[]>(queue, alpaka::getExtentProduct(src));
0060       alpaka::memcpy(queue, dst, src);
0061       return dst;
0062     }
0063   };
0064 }  // namespace cms::alpakatools
0065 
0066 #endif