/* Copyright (c) 2021 - 2021 Advanced Micro Devices, Inc. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include static std::random_device dev; static std::mt19937 rng(dev()); template __host__ __device__ inline constexpr int count() { return sizeof(T) / sizeof(M); } inline float getRandomFloat(float min = 10, float max = 100) { std::uniform_real_distribution gen(min, max); return gen(rng); } template void fillMatrix(T* a, int size) { for (int i = 0; i < size; i++) { T t; t.x = getRandomFloat(); if constexpr (count() >= 2) t.y = getRandomFloat(); if constexpr (count() >= 3) t.z = getRandomFloat(); if constexpr (count() >= 4) t.w = getRandomFloat(); a[i] = t; } } // Test operations template __host__ __device__ void testOperations(T& a, T& b) { a.x += b.x; a.x++; b.x++; if constexpr (count() >= 2) { a.y = b.x; a.x = b.y; } if constexpr (count() >= 3) { if (a.x > 0) b.x /= a.x; a.x *= b.z; a.y--; } if constexpr (count() >= 4) { b.w = a.x; a.w += (-b.y); } } template __global__ void testOperationsGPU(T* d_a, T* d_b, int size) { int id = threadIdx.x; if (id > size) return; T& a = d_a[id]; T& b = d_b[id]; testOperations(a, b); } template void dcopy(T* a, T* b, int size) { for (int i = 0; i < size; i++) { a[i] = b[i]; } } template bool isEqual(T* a, T* b, int size) { for (int i = 0; i < size; i++) { if (a[i] != b[i]) { return false; } } return true; } // Main function that tests type // T = what you want to test // D = pack of 1 i.e. float1 int1 template void testType(int msize) { T *fa, *fb, *fc, *h_fa, *h_fb; fa = new T[msize]; fb = new T[msize]; fc = new T[msize]; h_fa = new T[msize]; h_fb = new T[msize]; T *d_fa, *d_fb; constexpr int c = count(); if (c <= 0 || c >= 5) { INFO("It should be between 1 to 5"); REQUIRE(false); } fillMatrix(fa, msize); dcopy(fb, fa, msize); dcopy(h_fa, fa, msize); dcopy(h_fb, fa, msize); for (int i = 0; i < msize; i++) testOperations(h_fa[i], h_fb[i]); HIP_CHECK(hipMalloc(&d_fa, sizeof(T) * msize)); HIP_CHECK(hipMalloc(&d_fb, sizeof(T) * msize)); HIP_CHECK(hipMemcpy(d_fa, fa, sizeof(T) * msize, hipMemcpyHostToDevice)); HIP_CHECK(hipMemcpy(d_fb, fb, sizeof(T) * msize, hipMemcpyHostToDevice)); auto kernel = testOperationsGPU; hipLaunchKernelGGL(kernel, 1, msize, 0, 0, d_fa, d_fb, msize); HIP_CHECK(hipGetLastError()); HIP_CHECK(hipMemcpy(fc, d_fa, sizeof(T) * msize, hipMemcpyDeviceToHost)); bool pass = true; if (!isEqual(h_fa, fc, msize)) { pass = false; } delete[] fa; delete[] fb; delete[] fc; delete[] h_fa; delete[] h_fb; HIP_CHECK(hipFree(d_fa)); HIP_CHECK(hipFree(d_fb)); REQUIRE(pass); } TEST_CASE("Unit_floatTM") { constexpr int msize = 100; // double testType(msize); testType(msize); testType(msize); testType(msize); // floats testType(msize); testType(msize); testType(msize); testType(msize); // ints testType(msize); testType(msize); testType(msize); testType(msize); // chars testType(msize); testType(msize); testType(msize); testType(msize); // long testType(msize); testType(msize); testType(msize); testType(msize); // longlong testType(msize); testType(msize); testType(msize); testType(msize); // short testType(msize); testType(msize); testType(msize); testType(msize); // uints testType(msize); testType(msize); testType(msize); testType(msize); // uchars testType(msize); testType(msize); testType(msize); testType(msize); // ulong testType(msize); testType(msize); testType(msize); testType(msize); // ulonglong testType(msize); testType(msize); testType(msize); testType(msize); // ushort testType(msize); testType(msize); testType(msize); testType(msize); }