Debian Codegraph @

Tests/HIP/MathFunctions/main.hip

raw
1 #include <cmath>
2 #include <iostream>
3 #include <memory>
4 #include <stdexcept>
5
6 #include <hip/hip_fp16.h>
7 #include <hip/hip_runtime.h>
8 #include <math.h>
9
10 namespace {
11 template <class T, class F>
12 __global__ void global_entry_point(F f, T* out)
13 {
14 *out = f();
15 }
16
17 template <class T, class F>
18 bool verify(F f, T expected)
19 {
20 std::unique_ptr<T> cpu_T(new T);
21 T* gpu_T = nullptr;
22 bool result = true;
23 {
24 hipError_t err = hipMalloc((void**)&gpu_T, sizeof(T));
25 if (err != hipSuccess) {
26 std::cerr << "hipMalloc failed: " << hipGetErrorString(err) << std::endl;
27 result = false;
28 }
29 }
30 hipLaunchKernelGGL(global_entry_point, 1, 1, 0, 0, f, gpu_T);
31 {
32 hipError_t err =
33 hipMemcpy(cpu_T.get(), gpu_T, sizeof(T), hipMemcpyDeviceToHost);
34 if (err != hipSuccess) {
35 std::cerr << "hipMemcpy failed: " << hipGetErrorString(err) << std::endl;
36 result = false;
37 }
38 }
39 result = hipFree(gpu_T) == hipSuccess && result;
40 result = *cpu_T == expected && result;
41 return result;
42 }
43 }
44
45 int main(int argc, char** argv)
46 {
47 bool valid = verify([] __device__() { return std::round(1.4f); }, 1.0f);
48 valid &= verify([] __device__() { return max<_Float16>(1.0f, 2.0f); }, 2.0f);
49 valid &= verify([] __device__() { return min<_Float16>(1.0f, 2.0f); }, 1.0f);
50
51 if (valid) {
52 return 0;
53 } else {
54 return 1;
55 }
56 }