From 17050703b1f698116fa789e8bca9835bd229b030 Mon Sep 17 00:00:00 2001 From: Finlay Date: Fri, 5 Aug 2022 06:29:19 +0100 Subject: [PATCH] EXSWCPHIPT-139 - added tests for hipStreamDestroy (#2548) --- catch/unit/stream/CMakeLists.txt | 2 + catch/unit/stream/hipStreamDestroy.cc | 97 +++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 catch/unit/stream/hipStreamDestroy.cc diff --git a/catch/unit/stream/CMakeLists.txt b/catch/unit/stream/CMakeLists.txt index 6cdbac9a4e..9b829207cf 100644 --- a/catch/unit/stream/CMakeLists.txt +++ b/catch/unit/stream/CMakeLists.txt @@ -7,6 +7,7 @@ set(TEST_SRC hipStreamAddCallback.cc hipStreamCreateWithFlags.cc hipStreamCreateWithPriority.cc + hipStreamDestroy.cc hipStreamGetCUMask.cc hipAPIStreamDisable.cc streamCommon.cc @@ -25,6 +26,7 @@ set(TEST_SRC hipStreamAddCallback.cc hipStreamCreateWithFlags.cc hipStreamCreateWithPriority.cc + hipStreamDestroy.cc hipAPIStreamDisable.cc # hipStreamAttachMemAsync.cc # Disabling it on nvidia due to issue in function definition of hipStreamAttachMemAsync # Fixing would break ABI, to be re-enabled when the fix is made. diff --git a/catch/unit/stream/hipStreamDestroy.cc b/catch/unit/stream/hipStreamDestroy.cc new file mode 100644 index 0000000000..54edddc7c6 --- /dev/null +++ b/catch/unit/stream/hipStreamDestroy.cc @@ -0,0 +1,97 @@ +/* +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 +#include + +namespace hipStreamDestroyTests { + +TEST_CASE("Unit_hipStreamDestroy_Default") { + hipStream_t stream{}; + HIP_CHECK(hipStreamCreate(&stream)); + HIP_CHECK(hipStreamDestroy(stream)); +} + +TEST_CASE("Unit_hipStreamDestroy_Negative_DoubleDestroy") { + hipStream_t stream{}; + HIP_CHECK(hipStreamCreate(&stream)); + HIP_CHECK(hipStreamDestroy(stream)); + HIP_CHECK_ERROR(hipStreamDestroy(stream), hipErrorContextIsDestroyed); +} + +TEST_CASE("Unit_hipStreamDestroy_Negative_NullStream") { + HIP_CHECK_ERROR(hipStreamDestroy(nullptr), hipErrorInvalidResourceHandle); +} + +template void checkDataSet(int* deviceData) { + HIP_CHECK(hipStreamSynchronize(nullptr)); + std::array hostData{}; + HIP_CHECK( + hipMemcpy(hostData.data(), deviceData, sizeof(int) * numDataPoints, hipMemcpyDeviceToHost)); + REQUIRE(std::all_of(std::begin(hostData), std::end(hostData), [](int x) { return x == 1; })); +} + +__global__ void setToOne(int* x, size_t size) { + unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x; + if (idx < size) { + x[idx] = 1; + } +} + +TEST_CASE("Unit_hipStreamDestroy_WithFinishedWork") { + hipStream_t stream{}; + HIP_CHECK(hipStreamCreate(&stream)); + + constexpr int numDataPoints = 10; + int* deviceData{}; + HIP_CHECK(hipMalloc(&deviceData, sizeof(int) * numDataPoints)); + HIP_CHECK(hipMemset(deviceData, 0, sizeof(int) * numDataPoints)); + + setToOne<<<1, numDataPoints, 0, stream>>>(deviceData, numDataPoints); + checkDataSet(deviceData); + HIP_CHECK(hipStreamDestroy(stream)); + HIP_CHECK(hipFree(deviceData)); +} + +// hipStreamDestroy should return immediately then clean up the resources when the stream is empty +// of work +#if HT_AMD /* Disabled because frequency based wait is timing out on nvidia platforms */ +TEST_CASE("Unit_hipStreamDestroy_WithPendingWork") { +#if HT_AMD + HipTest::HIP_SKIP_TEST( + "EXSWCPHIPT-44 - expected hipStreamDestroy to return immediately then release the resources " + "when the queued jobs are finished"); + return; +#endif + hipStream_t stream{}; + HIP_CHECK(hipStreamCreate(&stream)); + constexpr int numDataPoints = 10; + int* deviceData{}; + HIP_CHECK(hipMalloc(&deviceData, sizeof(int) * numDataPoints)); + HIP_CHECK(hipMemset(deviceData, 0, sizeof(int) * numDataPoints)); + + HipTest::runKernelForDuration(std::chrono::milliseconds(500), stream); + setToOne<<<1, numDataPoints, 0, stream>>>(deviceData, numDataPoints); + HIP_CHECK_ERROR(hipStreamQuery(stream), hipErrorNotReady); + HIP_CHECK_ERROR(hipStreamQuery(nullptr), hipErrorNotReady); + HIP_CHECK(hipStreamDestroy(stream)); + checkDataSet(deviceData); + HIP_CHECK(hipFree(deviceData)); +} +#endif +} // namespace hipStreamDestroyTests