Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <cstddef>
0002 
0003 #include <hip/hip_runtime.h>
0004 
0005 #include "HeterogeneousTest/ROCmWrapper/interface/DeviceAdditionWrapper.h"
0006 #include "HeterogeneousTest/ROCmOpaque/interface/DeviceAdditionOpaque.h"
0007 #include "HeterogeneousCore/ROCmUtilities/interface/hipCheck.h"
0008 
0009 namespace cms::rocmtest {
0010 
0011   void opaque_add_vectors_f(const float* in1_h, const float* in2_h, float* out_h, size_t size) {
0012     // allocate input and output buffers on the device
0013     float* in1_d;
0014     float* in2_d;
0015     float* out_d;
0016     hipCheck(hipMalloc(&in1_d, size * sizeof(float)));
0017     hipCheck(hipMalloc(&in2_d, size * sizeof(float)));
0018     hipCheck(hipMalloc(&out_d, size * sizeof(float)));
0019 
0020     // copy the input data to the device
0021     hipCheck(hipMemcpy(in1_d, in1_h, size * sizeof(float), hipMemcpyHostToDevice));
0022     hipCheck(hipMemcpy(in2_d, in2_h, size * sizeof(float), hipMemcpyHostToDevice));
0023 
0024     // fill the output buffer with zeros
0025     hipCheck(hipMemset(out_d, 0, size * sizeof(float)));
0026 
0027     // launch the 1-dimensional kernel for vector addition
0028     wrapper_add_vectors_f(in1_d, in2_d, out_d, size);
0029 
0030     // copy the results from the device to the host
0031     hipCheck(hipMemcpy(out_h, out_d, size * sizeof(float), hipMemcpyDeviceToHost));
0032 
0033     // wait for all the operations to complete
0034     hipCheck(hipDeviceSynchronize());
0035 
0036     // free the input and output buffers on the device
0037     hipCheck(hipFree(in1_d));
0038     hipCheck(hipFree(in2_d));
0039     hipCheck(hipFree(out_d));
0040   }
0041 
0042   void opaque_add_vectors_d(const double* in1_h, const double* in2_h, double* out_h, size_t size) {
0043     // allocate input and output buffers on the device
0044     double* in1_d;
0045     double* in2_d;
0046     double* out_d;
0047     hipCheck(hipMalloc(&in1_d, size * sizeof(double)));
0048     hipCheck(hipMalloc(&in2_d, size * sizeof(double)));
0049     hipCheck(hipMalloc(&out_d, size * sizeof(double)));
0050 
0051     // copy the input data to the device
0052     hipCheck(hipMemcpy(in1_d, in1_h, size * sizeof(double), hipMemcpyHostToDevice));
0053     hipCheck(hipMemcpy(in2_d, in2_h, size * sizeof(double), hipMemcpyHostToDevice));
0054 
0055     // fill the output buffer with zeros
0056     hipCheck(hipMemset(out_d, 0, size * sizeof(double)));
0057 
0058     // launch the 1-dimensional kernel for vector addition
0059     wrapper_add_vectors_d(in1_d, in2_d, out_d, size);
0060 
0061     // copy the results from the device to the host
0062     hipCheck(hipMemcpy(out_h, out_d, size * sizeof(double), hipMemcpyDeviceToHost));
0063 
0064     // wait for all the operations to complete
0065     hipCheck(hipDeviceSynchronize());
0066 
0067     // free the input and output buffers on the device
0068     hipCheck(hipFree(in1_d));
0069     hipCheck(hipFree(in2_d));
0070     hipCheck(hipFree(out_d));
0071   }
0072 
0073 }  // namespace cms::rocmtest