1 #include <hip/hip_runtime.h>
4 #include "test_common.h"
6 static std::random_device dev;
7 static std::mt19937 rng(dev());
9 template <typename T, typename M>
10 __host__ __device__ inline constexpr int count() {
11 return sizeof(T) / sizeof(M);
14 inline float getRandomFloat(float min = 10, float max = 100) {
15 std::uniform_real_distribution<float> gen(min, max);
19 template <typename T, typename B>
20 void fillMatrix(T* a, int size) {
21 for (int i = 0; i < size; i++) {
23 t.x = getRandomFloat();
24 if constexpr (count<T, B>() >= 2) t.y = getRandomFloat();
25 if constexpr (count<T, B>() >= 3) t.z = getRandomFloat();
26 if constexpr (count<T, B>() >= 4) t.w = getRandomFloat();
33 template <typename T, typename B>
34 __host__ __device__ void testOperations(T& a, T& b) {
38 if constexpr (count<T, B>() >= 2) {
42 if constexpr (count<T, B>() >= 3) {
43 if (a.x > 0) b.x /= a.x;
47 if constexpr (count<T, B>() >= 4) {
53 template <typename T, typename B>
54 __global__ void testOperationsGPU(T* d_a, T* d_b, int size) {
56 if (id > size) return;
60 testOperations<T, B>(a, b);
65 void dcopy(T* a, T* b, int size) {
66 for (int i = 0; i < size; i++) {
72 bool isEqual(T* a, T* b, int size) {
73 for (int i = 0; i < size; i++) {
81 // Main function that tests type
82 // T = what you want to test
83 // D = pack of 1 i.e. float1 int1
84 template <typename T, typename D>
85 void testType(int msize) {
86 T *fa, *fb, *fc, *h_fa, *h_fb;
95 constexpr int c = count<T, D>();
97 if (c <= 0 || c >= 5) {
98 failed("Invalid Size\n");
101 fillMatrix<T, D>(fa, msize);
102 dcopy(fb, fa, msize);
103 dcopy(h_fa, fa, msize);
104 dcopy(h_fb, fa, msize);
105 for (int i = 0; i < msize; i++) testOperations<T, D>(h_fa[i], h_fb[i]);
107 hipMalloc(&d_fa, sizeof(T) * msize);
108 hipMalloc(&d_fb, sizeof(T) * msize);
110 hipMemcpy(d_fa, fa, sizeof(T) * msize, hipMemcpyHostToDevice);
111 hipMemcpy(d_fb, fb, sizeof(T) * msize, hipMemcpyHostToDevice);
113 auto kernel = testOperationsGPU<T, D>;
114 hipLaunchKernelGGL(kernel, 1, msize, 0, 0, d_fa, d_fb, msize);
116 hipMemcpy(fc, d_fa, sizeof(T) * msize, hipMemcpyDeviceToHost);
119 if (!isEqual<T>(h_fa, fc, msize)) {
137 const int msize = 100;
139 testType<double1, double1>(msize);
140 testType<double2, double1>(msize);
141 testType<double3, double1>(msize);
142 testType<double4, double1>(msize);
145 testType<float1, float1>(msize);
146 testType<float2, float1>(msize);
147 testType<float3, float1>(msize);
148 testType<float4, float1>(msize);