From 56ef43810bb41ed8955eb56560f71cf697df78c0 Mon Sep 17 00:00:00 2001 From: Marko Arandjelovic Date: Thu, 25 Jul 2024 17:08:44 +0200 Subject: [PATCH] SWDEV-475749 - Add unit test for hipMallocHost Change-Id: I0e94342ba68c12f64ac94a04a46f1c478e6f01be --- catch/unit/memory/CMakeLists.txt | 1 + catch/unit/memory/hipMallocHost.cc | 73 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 catch/unit/memory/hipMallocHost.cc diff --git a/catch/unit/memory/CMakeLists.txt b/catch/unit/memory/CMakeLists.txt index a125dbd4fe..02a95b758c 100644 --- a/catch/unit/memory/CMakeLists.txt +++ b/catch/unit/memory/CMakeLists.txt @@ -62,6 +62,7 @@ set(TEST_SRC hipHostUnregister.cc hipHostGetFlags.cc hipHostGetDevicePointer.cc + hipMallocHost.cc hipMallocManaged_MultiScenario.cc hipMemsetNegative.cc hipMemset.cc diff --git a/catch/unit/memory/hipMallocHost.cc b/catch/unit/memory/hipMallocHost.cc new file mode 100644 index 0000000000..25da88df21 --- /dev/null +++ b/catch/unit/memory/hipMallocHost.cc @@ -0,0 +1,73 @@ +/* +Copyright (c) 2024 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 + +static __global__ void write_integer(int* memory, int value) { *memory = value; } + +TEST_CASE("Unit_hipMallocHost_Positive") { + int* host_memory = nullptr; + + HIP_CHECK(hipMallocHost(reinterpret_cast(&host_memory), sizeof(int))); + REQUIRE(host_memory != nullptr); + HIP_CHECK(hipHostFree(host_memory)); +} + +TEST_CASE("Unit_hipMallocHost_DataValidation") { + int validation_number = 10; + int* host_memory = nullptr; + hipEvent_t event = nullptr; + + HIP_CHECK(hipMallocHost(reinterpret_cast(&host_memory), sizeof(int))); + + write_integer<<<1, 1>>>(host_memory, validation_number); + + SECTION("device sync") { HIP_CHECK(hipDeviceSynchronize()); } + + SECTION("event sync") { + HIP_CHECK(hipEventCreateWithFlags(&event, 0)); + HIP_CHECK(hipEventRecord(event, nullptr)); + HIP_CHECK(hipEventSynchronize(event)); + } + + SECTION("stream sync") { HIP_CHECK(hipStreamSynchronize(nullptr)); } + + REQUIRE(*host_memory == validation_number); + + if (event != nullptr) { + HIP_CHECK(hipEventDestroy(event)); + } + + HIP_CHECK(hipHostFree(host_memory)); +} + +TEST_CASE("Unit_hipMallocHost_Negative") { + int* host_memory = nullptr; + + SECTION("host memory is nullptr") { + HIP_CHECK_ERROR(hipMallocHost(nullptr, sizeof(int)), hipErrorInvalidValue); + } + + SECTION("size is negative") { + HIP_CHECK_ERROR(hipMallocHost(reinterpret_cast(&host_memory), -1), hipErrorOutOfMemory); + } +}