From f90501aa4a6ae909c74d0dfa8f15bd77f736f943 Mon Sep 17 00:00:00 2001 From: Jatin Chaudhary Date: Fri, 10 Jun 2022 16:46:54 +0100 Subject: [PATCH] Add test for hipHostGetDevicePointer (#2611) --- catch/unit/memory/CMakeLists.txt | 2 + catch/unit/memory/hipHostGetDevicePointer.cc | 78 ++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 catch/unit/memory/hipHostGetDevicePointer.cc diff --git a/catch/unit/memory/CMakeLists.txt b/catch/unit/memory/CMakeLists.txt index 3ce80dd780..d36f0a1b85 100644 --- a/catch/unit/memory/CMakeLists.txt +++ b/catch/unit/memory/CMakeLists.txt @@ -41,6 +41,7 @@ set(TEST_SRC hipMemPtrGetInfo.cc hipPointerGetAttributes.cc hipHostGetFlags.cc + hipHostGetDevicePointer.cc hipMallocManaged_MultiScenario.cc hipMemsetNegative.cc hipMemset.cc @@ -106,6 +107,7 @@ set(TEST_SRC hipMemcpy_MultiThread.cc hipHostRegister.cc hipHostGetFlags.cc + hipHostGetDevicePointer.cc hipMallocManaged_MultiScenario.cc hipMemsetNegative.cc hipMemset.cc diff --git a/catch/unit/memory/hipHostGetDevicePointer.cc b/catch/unit/memory/hipHostGetDevicePointer.cc new file mode 100644 index 0000000000..7c3e689e05 --- /dev/null +++ b/catch/unit/memory/hipHostGetDevicePointer.cc @@ -0,0 +1,78 @@ +/* +Copyright (c) 2022 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 + +TEST_CASE("Unit_hipHostGetDevicePointer_Negative") { + int* hPtr{nullptr}; + HIP_CHECK(hipHostMalloc(&hPtr, sizeof(int))); + + SECTION("Nullptr as device") { + HIP_CHECK_ERROR(hipHostGetDevicePointer(nullptr, hPtr, 0), hipErrorInvalidValue); + } + + SECTION("Nullptr as host") { + int* dPtr{nullptr}; + HIP_CHECK_ERROR(hipHostGetDevicePointer(reinterpret_cast(&dPtr), nullptr, 0), + hipErrorInvalidValue); + } + + // Not adding check for flags since CUDA spec states that there might be more values added to it + HIP_CHECK(hipHostFree(hPtr)); +} + +template __global__ void set(T* ptr, T val) { *ptr = val; } + +TEST_CASE("Unit_hipHostGetDevicePointer_UseCase") { + int* hPtr{nullptr}; + HIP_CHECK(hipHostMalloc(&hPtr, sizeof(int))); + + auto kernel = set; + constexpr int value = 10; + + SECTION("Set the value on device - Get device ptr") { + int* dPtr{nullptr}; + HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast(&dPtr), hPtr, 0)); + REQUIRE(dPtr != nullptr); + + kernel<<<1, 1>>>(dPtr, value); + HIP_CHECK(hipDeviceSynchronize()); + + REQUIRE(*hPtr == value); + } + + SECTION("Set the value on device - by hipHostRegister") { + int res{0}; // Stuff on stack + HIP_CHECK(hipHostRegister(&res, sizeof(int), 0)); // Lets map stack memory :) + + int* dPtr{nullptr}; + HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast(&dPtr), &res, 0)) + + kernel<<<1, 1>>>(dPtr, value); + HIP_CHECK(hipDeviceSynchronize()); + HIP_CHECK(hipHostUnregister(&res)); + + REQUIRE(value == 10); + } + + HIP_CHECK(hipHostFree(hPtr)); +}