CMS 3D CMS Logo

DeviceAdditionOpaque.cc
Go to the documentation of this file.
1 #include <cstddef>
2 
3 #include <hip/hip_runtime.h>
4 
8 
9 namespace cms::rocmtest {
10 
11  void opaque_add_vectors_f(const float* in1_h, const float* in2_h, float* out_h, size_t size) {
12  // allocate input and output buffers on the device
13  float* in1_d;
14  float* in2_d;
15  float* out_d;
16  hipCheck(hipMalloc(&in1_d, size * sizeof(float)));
17  hipCheck(hipMalloc(&in2_d, size * sizeof(float)));
18  hipCheck(hipMalloc(&out_d, size * sizeof(float)));
19 
20  // copy the input data to the device
21  hipCheck(hipMemcpy(in1_d, in1_h, size * sizeof(float), hipMemcpyHostToDevice));
22  hipCheck(hipMemcpy(in2_d, in2_h, size * sizeof(float), hipMemcpyHostToDevice));
23 
24  // fill the output buffer with zeros
25  hipCheck(hipMemset(out_d, 0, size * sizeof(float)));
26 
27  // launch the 1-dimensional kernel for vector addition
28  wrapper_add_vectors_f(in1_d, in2_d, out_d, size);
29 
30  // copy the results from the device to the host
31  hipCheck(hipMemcpy(out_h, out_d, size * sizeof(float), hipMemcpyDeviceToHost));
32 
33  // wait for all the operations to complete
34  hipCheck(hipDeviceSynchronize());
35 
36  // free the input and output buffers on the device
37  hipCheck(hipFree(in1_d));
38  hipCheck(hipFree(in2_d));
39  hipCheck(hipFree(out_d));
40  }
41 
42  void opaque_add_vectors_d(const double* in1_h, const double* in2_h, double* out_h, size_t size) {
43  // allocate input and output buffers on the device
44  double* in1_d;
45  double* in2_d;
46  double* out_d;
47  hipCheck(hipMalloc(&in1_d, size * sizeof(double)));
48  hipCheck(hipMalloc(&in2_d, size * sizeof(double)));
49  hipCheck(hipMalloc(&out_d, size * sizeof(double)));
50 
51  // copy the input data to the device
52  hipCheck(hipMemcpy(in1_d, in1_h, size * sizeof(double), hipMemcpyHostToDevice));
53  hipCheck(hipMemcpy(in2_d, in2_h, size * sizeof(double), hipMemcpyHostToDevice));
54 
55  // fill the output buffer with zeros
56  hipCheck(hipMemset(out_d, 0, size * sizeof(double)));
57 
58  // launch the 1-dimensional kernel for vector addition
59  wrapper_add_vectors_d(in1_d, in2_d, out_d, size);
60 
61  // copy the results from the device to the host
62  hipCheck(hipMemcpy(out_h, out_d, size * sizeof(double), hipMemcpyDeviceToHost));
63 
64  // wait for all the operations to complete
65  hipCheck(hipDeviceSynchronize());
66 
67  // free the input and output buffers on the device
68  hipCheck(hipFree(in1_d));
69  hipCheck(hipFree(in2_d));
70  hipCheck(hipFree(out_d));
71  }
72 
73 } // namespace cms::rocmtest
void opaque_add_vectors_d(const double *in1, const double *in2, double *out, size_t size)
void wrapper_add_vectors_f(const float *__restrict__ in1, const float *__restrict__ in2, float *__restrict__ out, size_t size)
void opaque_add_vectors_f(const float *in1, const float *in2, float *out, size_t size)
#define hipCheck(ARG,...)
Definition: hipCheck.h:52
void wrapper_add_vectors_d(const double *__restrict__ in1, const double *__restrict__ in2, double *__restrict__ out, size_t size)