Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-09 02:22:22

0001 #include <cstdint>
0002 
0003 #include <alpaka/alpaka.hpp>
0004 
0005 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0006 #include "HeterogeneousCore/AlpakaInterface/interface/devices.h"
0007 #include "HeterogeneousCore/AlpakaInterface/interface/memory.h"
0008 #include "HeterogeneousTest/AlpakaOpaque/interface/alpaka/DeviceAdditionOpaque.h"
0009 #include "HeterogeneousTest/AlpakaWrapper/interface/alpaka/DeviceAdditionWrapper.h"
0010 
0011 namespace ALPAKA_ACCELERATOR_NAMESPACE::test {
0012 
0013   void opaque_add_vectors_f(const float* in1, const float* in2, float* out, uint32_t size) {
0014     // run on the first available devices
0015     auto const& device = cms::alpakatools::devices<Platform>()[0];
0016     Queue queue{device};
0017 
0018     // wrap the input and output data in views
0019     auto in1_h = cms::alpakatools::make_host_view<const float>(in1, size);
0020     auto in2_h = cms::alpakatools::make_host_view<const float>(in2, size);
0021     auto out_h = cms::alpakatools::make_host_view<float>(out, size);
0022 
0023     // allocate input and output buffers on the device
0024     auto in1_d = cms::alpakatools::make_device_buffer<float[]>(queue, size);
0025     auto in2_d = cms::alpakatools::make_device_buffer<float[]>(queue, size);
0026     auto out_d = cms::alpakatools::make_device_buffer<float[]>(queue, size);
0027 
0028     // copy the input data to the device
0029     // FIXME: pass the explicit size of type uint32_t to avoid compilation error
0030     // The destination view and the extent are required to have compatible index types!
0031     alpaka::memcpy(queue, in1_d, in1_h, size);
0032     alpaka::memcpy(queue, in2_d, in2_h, size);
0033 
0034     // fill the output buffer with zeros
0035     alpaka::memset(queue, out_d, 0);
0036 
0037     // launch the 1-dimensional kernel for vector addition
0038     test::wrapper_add_vectors_f(queue, in1_d.data(), in2_d.data(), out_d.data(), size);
0039 
0040     // copy the results from the device to the host
0041     alpaka::memcpy(queue, out_h, out_d);
0042 
0043     // wait for all the operations to complete
0044     alpaka::wait(queue);
0045 
0046     // the device buffers are freed automatically
0047   }
0048 
0049   void opaque_add_vectors_d(const double* in1, const double* in2, double* out, uint32_t size) {
0050     // run on the first available devices
0051     auto const& device = cms::alpakatools::devices<Platform>()[0];
0052     Queue queue{device};
0053 
0054     // wrap the input and output data in views
0055     auto in1_h = cms::alpakatools::make_host_view<const double>(in1, size);
0056     auto in2_h = cms::alpakatools::make_host_view<const double>(in2, size);
0057     auto out_h = cms::alpakatools::make_host_view<double>(out, size);
0058 
0059     // allocate input and output buffers on the device
0060     auto in1_d = cms::alpakatools::make_device_buffer<double[]>(queue, size);
0061     auto in2_d = cms::alpakatools::make_device_buffer<double[]>(queue, size);
0062     auto out_d = cms::alpakatools::make_device_buffer<double[]>(queue, size);
0063 
0064     // copy the input data to the device
0065     // FIXME: pass the explicit size of type uint32_t to avoid compilation error
0066     // The destination view and the extent are required to have compatible index types!
0067     alpaka::memcpy(queue, in1_d, in1_h, size);
0068     alpaka::memcpy(queue, in2_d, in2_h, size);
0069 
0070     // fill the output buffer with zeros
0071     alpaka::memset(queue, out_d, 0);
0072 
0073     // launch the 1-dimensional kernel for vector addition
0074     test::wrapper_add_vectors_d(queue, in1_d.data(), in2_d.data(), out_d.data(), size);
0075 
0076     // copy the results from the device to the host
0077     alpaka::memcpy(queue, out_h, out_d);
0078 
0079     // wait for all the operations to complete
0080     alpaka::wait(queue);
0081 
0082     // the device buffers are freed automatically
0083   }
0084 
0085 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE::test