From 25263b855335ecd325c45a76d8daa09c36136a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mirza=20Halil=C4=8Devi=C4=87?= <109971222+mirza-halilcevic@users.noreply.github.com> Date: Thu, 28 Dec 2023 19:33:58 +0100 Subject: [PATCH] EXSWHTEC-102 - Implement tests for hipMemcpyParam2D APIs #54 Change-Id: Ieac4d5000915b80f579c8e5f72d8d072bde63ab9 [ROCm/hip-tests commit: 9a3fd8ec41a11051ae5e1fd069d517cb68ca5993] --- .../catch/unit/memory/CMakeLists.txt | 2 + .../catch/unit/memory/hipMemcpyParam2D.cc | 462 +++++--------- .../unit/memory/hipMemcpyParam2DAsync.cc | 585 ++++++------------ .../unit/memory/hipMemcpyParam2DAsync_old.cc | 441 +++++++++++++ .../catch/unit/memory/hipMemcpyParam2D_old.cc | 337 ++++++++++ .../unit/memory/memcpy2d_tests_common.hh | 211 +++++++ 6 files changed, 1333 insertions(+), 705 deletions(-) create mode 100644 projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync_old.cc create mode 100644 projects/hip-tests/catch/unit/memory/hipMemcpyParam2D_old.cc diff --git a/projects/hip-tests/catch/unit/memory/CMakeLists.txt b/projects/hip-tests/catch/unit/memory/CMakeLists.txt index 2b4e0e6dd9..fda74f5b2e 100644 --- a/projects/hip-tests/catch/unit/memory/CMakeLists.txt +++ b/projects/hip-tests/catch/unit/memory/CMakeLists.txt @@ -38,7 +38,9 @@ set(TEST_SRC hipMemcpy3DAsync.cc hipMemcpy3DAsync_old.cc hipMemcpyParam2D.cc + hipMemcpyParam2D_old.cc hipMemcpyParam2DAsync.cc + hipMemcpyParam2DAsync_old.cc hipMemcpy2D.cc hipMemcpy2D_old.cc hipMemcpy2DAsync.cc diff --git a/projects/hip-tests/catch/unit/memory/hipMemcpyParam2D.cc b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2D.cc index 3a35a1a16e..f2b5b270ce 100644 --- a/projects/hip-tests/catch/unit/memory/hipMemcpyParam2D.cc +++ b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2D.cc @@ -1,337 +1,195 @@ /* -Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved. +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 WARRANNTY OF ANY KIND, EXPRESS OR + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +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 INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +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. */ -/* -This testfile verifies the following scenarios of hipMemcpyParam2D API -1. Negative Scenarios -2. Extent Validation Scenarios -3. D2D copy for different datatypes -4. H2D and D2H copy for different datatypes -*/ +#include "memcpy2d_tests_common.hh" #include -#include +#include +#include +#include -static constexpr size_t NUM_W{10}; -static constexpr size_t NUM_H{10}; -/* - * This testcase verifies D2D functionality of hipMemcpyParam2D API - * Input: Intializing "A_d" device variable with "C_h" host variable - * Output: "A_d" device variable to "E_d" device variable - * - * Validating the result by copying "E_d" to "A_h" and checking - * it with the initalized data "C_h". - * - */ -TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2D_multiDevice-D2D", "[hipMemcpyParam2D]", char, float, int, - double, long double) { - CHECK_IMAGE_SUPPORT +TEST_CASE("Unit_hipMemcpyParam2D_Positive_Basic") { + constexpr bool async = false; - int numDevices = 0; - HIP_CHECK(hipGetDeviceCount(&numDevices)); - if (numDevices > 1) { - // Initialize and Allocating Memory - HIP_CHECK(hipSetDevice(0)); - TestType* A_h{nullptr}, *C_h{nullptr}, *A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(TestType)}; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, false); - HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-236 + SECTION("Device to Host") { Memcpy2DDeviceToHostShell(MemcpyParam2DAdapter()); } +#endif - int peerAccess = 0; - HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); - if (!peerAccess) { - SUCCEED("Skipped the test as there is no peer access"); - } else { - HIP_CHECK(hipSetDevice(1)); - char *E_d; - size_t pitch_E; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&E_d), - &pitch_E, width, NUM_H)); - - // Initalizing A_d with C_h - HIP_CHECK(hipMemcpy2D(A_d, pitch_A, C_h, width, - NUM_W * sizeof(TestType), NUM_H, hipMemcpyHostToDevice)); - - // Device to Device - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeDevice; - desc.dstHost = E_d; - desc.dstDevice = hipDeviceptr_t(E_d); - desc.dstPitch = pitch_E; - desc.WidthInBytes = NUM_W * sizeof(TestType); - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); - - // Copying E_d to A_h - HIP_CHECK(hipMemcpy2D(A_h, width, E_d, pitch_E, - NUM_W * sizeof(TestType), NUM_H, - hipMemcpyDeviceToHost)); - - // Validating the result - REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); - - // DeAllocating the memory - HIP_CHECK(hipFree(A_d)); - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, nullptr, C_h, false); + SECTION("Device to Device") { + SECTION("Peer access disabled") { + Memcpy2DDeviceToDeviceShell(MemcpyParam2DAdapter()); } - } else { - SUCCEED("skipping the testcases as numDevices < 2"); - } -} - -/* - * This testcase verifies H2D & D2H functionality of hipMemcpyParam2D API - * H2D case: - * Input: "C_h" host variable initialized with default data - * Output: "A_d" device variable - * - * D2H case: - * Input: "A_d" device variable from the previous output - * OutPut: "A_h" variable - * - * Validating the result by comparing "A_h" to "C_h" - */ -TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2D_multiDevice-H2D-D2H", "[hipMemcpyParam2D]", char, float, - int, double, long double) { - CHECK_IMAGE_SUPPORT - - // 1 refers to pinned host memory and 0 refers - // to unpinned memory - auto memory_type = GENERATE(0, 1); - int numDevices = 0; - HIP_CHECK(hipGetDeviceCount(&numDevices)); - if (numDevices > 1) { - HIP_CHECK(hipSetDevice(0)); - - // Initialize and Allocating Memory - TestType* A_h{nullptr}, *C_h{nullptr}, - *A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(TestType)}; - - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - - // Based on memory type (pinned/unpinned) allocating memory - if (memory_type) { - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, true); - } else { - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, false); + SECTION("Peer access enabled") { + Memcpy2DDeviceToDeviceShell(MemcpyParam2DAdapter()); } - HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); - int peerAccess = 0; - HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); - if (!peerAccess) { - SUCCEED("Skipped the test as there is no peer access"); - } else { - // Host to Device - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeHost; - desc.srcHost = C_h; - desc.srcDevice = hipDeviceptr_t(C_h); - desc.srcPitch = width; - desc.dstMemoryType = hipMemoryTypeDevice; - desc.dstHost = A_d; - desc.dstDevice = hipDeviceptr_t(A_d); - desc.dstPitch = pitch_A; - desc.WidthInBytes = NUM_W*sizeof(TestType); - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + } - // Device to Host - memset(&desc, 0x0, sizeof(hip_Memcpy2D)); - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeHost; - desc.dstHost = A_h; - desc.dstDevice = hipDeviceptr_t(A_h); - desc.dstPitch = width; - desc.WidthInBytes = NUM_W*sizeof(TestType); - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + SECTION("Host to Device") { Memcpy2DHostToDeviceShell(MemcpyParam2DAdapter()); } - // Validating the result - REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-236 + SECTION("Host to Host") { Memcpy2DHostToHostShell(MemcpyParam2DAdapter()); } +#endif +} - // DeAllocating the Memory - HIP_CHECK(hipFree(A_d)); - if (memory_type) { - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, nullptr, C_h, true); - } else { - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, nullptr, C_h, false); - } +TEST_CASE("Unit_hipMemcpyParam2D_Positive_Synchronization_Behavior") { + HIP_CHECK(hipDeviceSynchronize()); + + SECTION("Host to Device") { Memcpy2DHtoDSyncBehavior(MemcpyParam2DAdapter<>(), true); } + + SECTION("Device to Pageable Host") { + Memcpy2DDtoHPageableSyncBehavior(MemcpyParam2DAdapter<>(), true); + } + +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-236 + SECTION("Device to Pinned Host") { + Memcpy2DDtoHPinnedSyncBehavior(MemcpyParam2DAdapter<>(), true); + } +#endif + + SECTION("Device to Device") { +#if HT_NVIDIA + Memcpy2DDtoDSyncBehavior(MemcpyParam2DAdapter<>(), false); +#else + Memcpy2DDtoDSyncBehavior(MemcpyParam2DAdapter<>(), true); +#endif + } + +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-232 + SECTION("Host to Host") { Memcpy2DHtoHSyncBehavior(MemcpyParam2DAdapter<>(), true); } +#endif +} + +TEST_CASE("Unit_hipMemcpyParam2D_Positive_Parameters") { + constexpr bool async = false; + Memcpy2DZeroWidthHeight(MemcpyParam2DAdapter()); +} + +TEST_CASE("Unit_hipMemcpyParam2D_Positive_Array") { + constexpr bool async = false; + SECTION("Array from/to Host") { + MemcpyParam2DArrayHostShell(MemcpyParam2DAdapter()); + } + SECTION("Array from/to Device") { + MemcpyParam2DArrayDeviceShell(MemcpyParam2DAdapter()); + } +} + +TEST_CASE("Unit_hipMemcpyParam2D_Negative_Parameters") { + constexpr size_t cols = 128; + constexpr size_t rows = 128; + + constexpr auto NegativeTests = [](void* dst, size_t dpitch, void* src, size_t spitch, + size_t width, size_t height, hipMemcpyKind kind) { + SECTION("dst == nullptr") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>()(static_cast(nullptr), dpitch, src, spitch, + width, height, kind), + hipErrorInvalidValue); } - } else { - SUCCEED("skipping the testcases as numDevices < 2"); - } -} -/* - * This testcase verifies the extent validation scenarios - */ -TEST_CASE("Unit_hipMemcpyParam2D_ExtentValidation") { - CHECK_IMAGE_SUPPORT - // Allocating memory and Initializing the data - HIP_CHECK(hipSetDevice(0)); - char* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, - * A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(char)}; - constexpr auto memsetval{100}; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, false); - HipTest::initArrays(nullptr, nullptr, nullptr, - &B_h, nullptr, nullptr, - width*NUM_H, false); - HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); - HipTest::setDefaultData(NUM_W*NUM_H, B_h, nullptr, nullptr); - HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); + SECTION("src == nullptr") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>()(dst, dpitch, static_cast(nullptr), spitch, + width, height, kind), + hipErrorInvalidValue); + } - // Device to Host - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeHost; - desc.dstHost = A_h; - desc.dstDevice = hipDeviceptr_t(A_h); - desc.dstPitch = width; - desc.WidthInBytes = NUM_W; - desc.Height = NUM_H; + SECTION("dstPitch < WithInBytes") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>()(dst, width - 1, src, spitch, width, height, kind), + hipErrorInvalidValue); + } - SECTION("Destination Pitch is 0") { - desc.dstPitch = 0; - REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + SECTION("srcPitch < WidthInBytes") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>()(dst, dpitch, src, width - 1, width, height, kind), + hipErrorInvalidValue); + } + + SECTION("dstPitch > max pitch") { + int attr = 0; + HIP_CHECK(hipDeviceGetAttribute(&attr, hipDeviceAttributeMaxPitch, 0)); + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>()(dst, static_cast(attr) + 1, src, spitch, + width, height, kind), + hipErrorInvalidValue); + } + + SECTION("srcPitch > max pitch") { + int attr = 0; + HIP_CHECK(hipDeviceGetAttribute(&attr, hipDeviceAttributeMaxPitch, 0)); + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>()(dst, dpitch, src, static_cast(attr) + 1, + width, height, kind), + hipErrorInvalidValue); + } + +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-237 + SECTION("WidthInBytes + srcXInBytes > srcPitch") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>(make_hipExtent(spitch - width + 1, 0, 0))( + dst, dpitch, src, spitch, width, height, kind), + hipErrorInvalidValue); + } + + SECTION("WidthInBytes + dstXInBytes > dstPitch") { + HIP_CHECK_ERROR( + MemcpyParam2DAdapter<>(make_hipExtent(0, 0, 0), make_hipExtent(dpitch - width + 1, 0, 0))( + dst, dpitch, src, spitch, width, height, kind), + hipErrorInvalidValue); + } + + SECTION("srcY out of bounds") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>(make_hipExtent(0, 1, 0))(dst, dpitch, src, spitch, + width, height, kind), + hipErrorInvalidValue); + } + + SECTION("dstY out of bounds") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter<>(make_hipExtent(0, 0, 0), make_hipExtent(0, 1, 0))( + dst, dpitch, src, spitch, width, height, kind), + hipErrorInvalidValue); + } +#endif + }; + + SECTION("Host to Device") { + LinearAllocGuard2D device_alloc(cols, rows); + LinearAllocGuard host_alloc(LinearAllocs::hipHostMalloc, device_alloc.pitch() * rows); + NegativeTests(device_alloc.ptr(), device_alloc.pitch(), host_alloc.ptr(), device_alloc.pitch(), + device_alloc.width(), device_alloc.height(), hipMemcpyHostToDevice); } - SECTION("Source Pitch is 0") { - desc.srcPitch = 0; - REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + SECTION("Device to Host") { + LinearAllocGuard2D device_alloc(cols, rows); + LinearAllocGuard host_alloc(LinearAllocs::hipHostMalloc, device_alloc.pitch() * rows); + NegativeTests(host_alloc.ptr(), device_alloc.pitch(), device_alloc.ptr(), device_alloc.pitch(), + device_alloc.width(), device_alloc.height(), hipMemcpyDeviceToHost); } - SECTION("Height is 0") { - desc.Height = 0; - REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); - REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + SECTION("Host to Host") { + LinearAllocGuard src_alloc(LinearAllocs::hipHostMalloc, cols * rows * sizeof(int)); + LinearAllocGuard dst_alloc(LinearAllocs::hipHostMalloc, cols * rows * sizeof(int)); + NegativeTests(dst_alloc.ptr(), cols * sizeof(int), src_alloc.ptr(), cols * sizeof(int), + cols * sizeof(int), rows, hipMemcpyHostToHost); } - SECTION("Width is 0") { - desc.WidthInBytes = 0; - REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); - REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + SECTION("Device to Device") { + LinearAllocGuard2D src_alloc(cols, rows); + LinearAllocGuard2D dst_alloc(cols, rows); + NegativeTests(dst_alloc.ptr(), dst_alloc.pitch(), src_alloc.ptr(), src_alloc.pitch(), + dst_alloc.width(), dst_alloc.height(), hipMemcpyDeviceToDevice); } - - // DeAllocating the Memory - HIP_CHECK(hipFree(A_d)); - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, B_h, C_h, false); -} - -/* - * This testcase verifies the negative scenarios - */ -TEST_CASE("Unit_hipMemcpyParam2D_Negative") { - CHECK_IMAGE_SUPPORT - - HIP_CHECK(hipSetDevice(0)); - - // Allocating and Initializing the data - float* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, - * A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(float)}; - constexpr auto memsetval{100}; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, &B_h, &C_h, - width*NUM_H, false); - HipTest::setDefaultData(NUM_W*NUM_H, A_h, B_h, C_h); - HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); - - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeHost; - desc.dstHost = A_h; - desc.dstDevice = hipDeviceptr_t(A_h); - desc.dstPitch = width; - desc.WidthInBytes = NUM_W; - desc.Height = NUM_H; - - SECTION("Null Pointer to Source Device Pointer") { - desc.srcDevice = hipDeviceptr_t(nullptr); - REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); - } - - SECTION("Null Pointer to Destination Device Pointer") { - memset(&desc, 0x0, sizeof(hip_Memcpy2D)); - desc.srcMemoryType = hipMemoryTypeHost; - desc.srcHost = A_h; - desc.srcDevice = hipDeviceptr_t(A_h); - desc.srcPitch = width; - desc.dstMemoryType = hipMemoryTypeDevice; - desc.dstHost = A_d; - desc.dstDevice = hipDeviceptr_t(nullptr); - desc.dstPitch = pitch_A; - desc.WidthInBytes = NUM_W; - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); - } - - SECTION("Null Pointer to both Src & Dst Device Pointer") { - desc.srcDevice = hipDeviceptr_t(nullptr); - desc.dstDevice = hipDeviceptr_t(nullptr); - REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); - } - - SECTION("Width > src/dest pitches") { - desc.WidthInBytes = pitch_A+1; - REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); - } - - // DeAllocating the Memory - HIP_CHECK(hipFree(A_d)); - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, B_h, C_h, false); -} +} \ No newline at end of file diff --git a/projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync.cc b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync.cc index 763ef9185d..44ec9b01cb 100644 --- a/projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync.cc +++ b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync.cc @@ -1,441 +1,220 @@ /* 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 WARRANNTY OF ANY KIND, EXPRESS OR + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +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 INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +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. */ -/* -This testfile verifies the following scenarios of hipMemcpyParam2DAsync API -1. Negative Scenarios -2. Extent Validation Scenarios -3. D2D copy for different datatypes -4. H2D and D2H copy for different datatypes -5. Device context change scenario where memory allocated in one GPU - stream created in another GPU -*/ +#include "memcpy2d_tests_common.hh" #include -#include +#include +#include +#include -static constexpr size_t NUM_W{10}; -static constexpr size_t NUM_H{10}; -/* - * This testcase verifies D2D functionality of hipMemcpyParam2DAsync API - * Where Memory is allocated in GPU-0 and stream is created in GPU-1 - * - * Input: Intializing "A_d" device variable with "C_h" host variable - * Output: "A_d" device variable to "E_d" device variable - * - * Validating the result by copying "E_d" to "A_h" and checking - * it with the initalized data "C_h". - * - */ -TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2DAsync_multiDevice-StreamOnDiffDevice", - "[hipMemcpyParam2DAsync]", char, float, int, double, long double) { - CHECK_IMAGE_SUPPORT +TEST_CASE("Unit_hipMemcpyParam2DAsync_Positive_Basic") { + using namespace std::placeholders; - int numDevices = 0; - HIP_CHECK(hipGetDeviceCount(&numDevices)); - if (numDevices > 1) { - // Allocating and Initializing the data - HIP_CHECK(hipSetDevice(0)); - TestType* A_h{nullptr}, *C_h{nullptr}, *A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(TestType)}; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, false); - HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); - int peerAccess = 0; - HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); - if (!peerAccess) { - SUCCEED("Skipped the test as there is no peer access"); - } else { - TestType *E_d{nullptr}; - size_t pitch_E; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&E_d), - &pitch_E, width, NUM_H)); + constexpr bool async = true; - // Initalizing A_d with C_h - HIP_CHECK(hipSetDevice(1)); - hipStream_t stream; - HIP_CHECK(hipStreamCreate(&stream)); + const auto stream_type = GENERATE(Streams::nullstream, Streams::perThread, Streams::created); + const StreamGuard stream_guard(stream_type); + const hipStream_t stream = stream_guard.stream(); - HIP_CHECK(hipMemcpy2DAsync(A_d, pitch_A, C_h, width, - NUM_W*sizeof(TestType), NUM_H, - hipMemcpyHostToDevice, stream)); - HIP_CHECK(hipStreamSynchronize(stream)); - // Device to Device - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeDevice; - desc.dstHost = E_d; - desc.dstDevice = hipDeviceptr_t(E_d); - desc.dstPitch = pitch_E; - desc.WidthInBytes = NUM_W*sizeof(TestType); - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); - HIP_CHECK(hipStreamSynchronize(stream)); - - // Copying the result E_d to A_h host variable - HIP_CHECK(hipMemcpy2D(A_h, width, E_d, pitch_E, - NUM_W*sizeof(TestType), NUM_H, - hipMemcpyDeviceToHost)); - HIP_CHECK(hipDeviceSynchronize()); - // Validating the result - REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); - - // DeAllocating the memory - HIP_CHECK(hipFree(E_d)); - HIP_CHECK(hipFree(A_d)); - HIP_CHECK(hipStreamDestroy(stream)); - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, nullptr, C_h, false); +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-236 + SECTION("Device to Host") { + Memcpy2DDeviceToHostShell( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, stream), stream); + } +#endif + SECTION("Device to Device") { + SECTION("Peer access disabled") { + Memcpy2DDeviceToDeviceShell( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, stream), stream); } - } else { - SUCCEED("skipping the testcases as numDevices < 2"); - } -} - -/* - * This testcase verifies D2D functionality of hipMemcpyParam2DAsync API - * Input: Intializing "A_d" device variable with "C_h" host variable - * Output: "A_d" device variable to "E_d" device variable - * - * Validating the result by copying "E_d" to "A_h" and checking - * it with the initalized data "C_h". - * - */ -TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2DAsync_multiDevice-D2D", "[hipMemcpyParam2DAsync]", char, - int, float, double, long double) { - CHECK_IMAGE_SUPPORT - - int numDevices = 0; - HIP_CHECK(hipGetDeviceCount(&numDevices)); - if (numDevices > 1) { - // Allocating and Initializing the data - HIP_CHECK(hipSetDevice(0)); - TestType* A_h{nullptr}, *C_h{nullptr}, *A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(TestType)}; - hipStream_t stream; - HIP_CHECK(hipStreamCreate(&stream)); - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, false); - HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); - - int peerAccess = 0; - HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); - if (!peerAccess) { - SUCCEED("Skipped the test as there is no peer access"); - } else { - HIP_CHECK(hipSetDevice(1)); - TestType *E_d; - size_t pitch_E; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&E_d), - &pitch_E, width, NUM_H)); - - // Initializing A_d with C_h - HIP_CHECK(hipMemcpy2D(A_d, pitch_A, C_h, width, - NUM_W*sizeof(TestType), NUM_H, hipMemcpyHostToDevice)); - - // Device to Device - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeDevice; - desc.dstHost = E_d; - desc.dstDevice = hipDeviceptr_t(E_d); - desc.dstPitch = pitch_E; - desc.WidthInBytes = NUM_W*sizeof(TestType); - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); - HIP_CHECK(hipStreamSynchronize(stream)); - - // Copying the result E_d to A_h host variable - HIP_CHECK(hipMemcpy2D(A_h, width, E_d, pitch_E, - NUM_W*sizeof(TestType), NUM_H, hipMemcpyDeviceToHost)); - - // Validating the result - REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); - - // DeAllocating the memory - HIP_CHECK(hipFree(A_d)); - HIP_CHECK(hipStreamDestroy(stream)); - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, nullptr, C_h, false); + SECTION("Peer access enabled") { + Memcpy2DDeviceToDeviceShell( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, stream), stream); } - } else { - SUCCEED("skipping the testcases as numDevices < 2"); + } + SECTION("Host to Device") { + Memcpy2DHostToDeviceShell( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, stream), stream); + } +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-236 + SECTION("Host to Host") { + Memcpy2DHostToHostShell( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, stream), stream); + } +#endif +} + +TEST_CASE("Unit_hipMemcpyParam2DAsync_Positive_Synchronization_Behavior") { + using namespace std::placeholders; + + constexpr bool async = true; + + HIP_CHECK(hipDeviceSynchronize()); + + SECTION("Host to Device") { + Memcpy2DHtoDSyncBehavior( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, nullptr), false); + } +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-233 + SECTION("Device to Pageable Host") { + Memcpy2DDtoHPageableSyncBehavior( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, nullptr), true); + } +#endif +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-236 + SECTION("Device to Pinned Host") { + Memcpy2DDtoHPinnedSyncBehavior( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, nullptr), false); + } +#endif + SECTION("Device to Device") { + Memcpy2DDtoDSyncBehavior( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, nullptr), false); + } +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-233 + SECTION("Host to Host") { + Memcpy2DHtoHSyncBehavior( + std::bind(MemcpyParam2DAdapter(), _1, _2, _3, _4, _5, _6, _7, nullptr), true); + } +#endif +} + +TEST_CASE("Unit_hipMemcpyParam2DAsync_Positive_Parameters") { + constexpr bool async = true; + Memcpy2DZeroWidthHeight(MemcpyParam2DAdapter()); +} + +TEST_CASE("Unit_hipMemcpyParam2DAsync_Positive_Array") { + constexpr bool async = true; + SECTION("Array from/to Host") { + MemcpyParam2DArrayHostShell(MemcpyParam2DAdapter()); + } + SECTION("Array from/to Device") { + MemcpyParam2DArrayDeviceShell(MemcpyParam2DAdapter()); } } -/* - * This testcase verifies H2D & D2H functionality of hipMemcpyParam2DAsync API - * H2D case: - * Input: "C_h" host variable initialized with default data - * Output: "A_d" device variable - * - * D2H case: - * Input: "A_d" device variable from the previous output - * OutPut: "A_h" variable - * - * Validating the result by comparing "A_h" to "C_h" - */ -TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2DAsync_multiDevice-H2D-D2H", "[hipMemcpyParam2DAsync]", - char, int, float, double, long double) { - CHECK_IMAGE_SUPPORT +TEST_CASE("Unit_hipMemcpyParam2DAsync_Negative_Parameters") { + constexpr bool async = true; - // 1 refers to pinned host memory and 0 refers - // to unpinned memory - auto memory_type = GENERATE(0, 1); - int numDevices = 0; - HIP_CHECK(hipGetDeviceCount(&numDevices)); - if (numDevices > 1) { - // Allocating and Initializing the data - HIP_CHECK(hipSetDevice(0)); - TestType* A_h{nullptr}, *C_h{nullptr}, - *A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(TestType)}; - hipStream_t stream; + constexpr size_t cols = 128; + constexpr size_t rows = 128; - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - - // Based on memory type (pinned/unpinned) allocating memory - if (memory_type) { - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, true); - } else { - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, false); + constexpr auto NegativeTests = [](void* dst, size_t dpitch, void* src, size_t spitch, + size_t width, size_t height, hipMemcpyKind kind) { + SECTION("dst == nullptr") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter()(static_cast(nullptr), dpitch, src, + spitch, width, height, kind), + hipErrorInvalidValue); } - HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); - int peerAccess = 0; - HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); - if (!peerAccess) { - SUCCEED("Skipped the test as there is no peer access"); - } else { - // Host to Device - hip_Memcpy2D desc = {}; - HIP_CHECK(hipStreamCreate(&stream)); - desc.srcMemoryType = hipMemoryTypeHost; - desc.srcHost = C_h; - desc.srcDevice = hipDeviceptr_t(C_h); - desc.srcPitch = width; - desc.dstMemoryType = hipMemoryTypeDevice; - desc.dstHost = A_d; - desc.dstDevice = hipDeviceptr_t(A_d); - desc.dstPitch = pitch_A; - desc.WidthInBytes = NUM_W*sizeof(TestType); - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); - HIP_CHECK(hipStreamSynchronize(stream)); - - // Device to Host - memset(&desc, 0x0, sizeof(hip_Memcpy2D)); - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeHost; - desc.dstHost = A_h; - desc.dstDevice = hipDeviceptr_t(A_h); - desc.dstPitch = width; - desc.WidthInBytes = NUM_W*sizeof(TestType); - desc.Height = NUM_H; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); - HIP_CHECK(hipStreamSynchronize(stream)); - - // Validating the result - REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); - - // DeAllocating the memory - HIP_CHECK(hipFree(A_d)); - HIP_CHECK(hipStreamDestroy(stream)); - if (memory_type) { - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, nullptr, C_h, true); - } else { - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, nullptr, C_h, false); - } + SECTION("src == nullptr") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter()(dst, dpitch, static_cast(nullptr), + spitch, width, height, kind), + hipErrorInvalidValue); } - } else { - SUCCEED("skipping the testcases as numDevices < 2"); - } -} -/* - * This testcase verifies the extent validation scenarios - */ -TEST_CASE("Unit_hipMemcpyParam2DAsync_ExtentValidation") { - CHECK_IMAGE_SUPPORT + SECTION("dstPitch < WidthInBytes") { + HIP_CHECK_ERROR( + MemcpyParam2DAdapter()(dst, width - 1, src, spitch, width, height, kind), + hipErrorInvalidValue); + } + SECTION("srcPitch < WidthInBytes") { + HIP_CHECK_ERROR( + MemcpyParam2DAdapter()(dst, dpitch, src, width - 1, width, height, kind), + hipErrorInvalidValue); + } + SECTION("dpitch > max pitch") { + int attr = 0; + HIP_CHECK(hipDeviceGetAttribute(&attr, hipDeviceAttributeMaxPitch, 0)); + HIP_CHECK_ERROR(MemcpyParam2DAdapter()(dst, static_cast(attr) + 1, src, spitch, + width, height, kind), + hipErrorInvalidValue); + } + SECTION("spitch > max pitch") { + int attr = 0; + HIP_CHECK(hipDeviceGetAttribute(&attr, hipDeviceAttributeMaxPitch, 0)); + HIP_CHECK_ERROR(MemcpyParam2DAdapter()(dst, dpitch, src, static_cast(attr) + 1, + width, height, kind), + hipErrorInvalidValue); + } +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-237 + SECTION("WidthInBytes + srcXInBytes > srcPitch") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter(make_hipExtent(spitch - width + 1, 0, 0))( + dst, dpitch, src, spitch, width, height, kind), + hipErrorInvalidValue); + } + SECTION("WidthInBytes + dstXInBytes > dstPitch") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter(make_hipExtent(0, 0, 0), + make_hipExtent(dpitch - width + 1, 0, 0))( + dst, dpitch, src, spitch, width, height, kind), + hipErrorInvalidValue); + } + SECTION("srcY out of bounds") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter(make_hipExtent(0, 1, 0))(dst, dpitch, src, spitch, + width, height, kind), + hipErrorInvalidValue); + } + SECTION("dstY out of bounds") { + HIP_CHECK_ERROR(MemcpyParam2DAdapter(make_hipExtent(0, 0, 0), make_hipExtent(0, 1, 0))( + dst, dpitch, src, spitch, width, height, kind), + hipErrorInvalidValue); + } +#endif +#if HT_NVIDIA // Disabled on AMD due to defect - EXSWHTEC-235 + SECTION("Invalid stream") { + StreamGuard stream_guard(Streams::created); + HIP_CHECK(hipStreamDestroy(stream_guard.stream())); + HIP_CHECK_ERROR(MemcpyParam2DAdapter()(dst, dpitch, src, spitch, width, height, kind, + stream_guard.stream()), + hipErrorContextIsDestroyed); + } +#endif + }; - HIP_CHECK(hipSetDevice(0)); - char* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, - * A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(char)}; - constexpr auto memsetval{100}; - hipStream_t stream; - HIP_CHECK(hipStreamCreate(&stream)); - - // Allocating and Initializing the data - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, nullptr, &C_h, - width*NUM_H, false); - HipTest::initArrays(nullptr, nullptr, nullptr, - &B_h, nullptr, nullptr, - width*NUM_H, false); - HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); - HipTest::setDefaultData(NUM_W*NUM_H, B_h, nullptr, nullptr); - HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); - - // Device to Host - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeHost; - desc.dstHost = A_h; - desc.dstDevice = hipDeviceptr_t(A_h); - desc.dstPitch = width; - desc.WidthInBytes = NUM_W; - desc.Height = NUM_H; - - SECTION("Destination Pitch is 0") { - desc.dstPitch = 0; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + SECTION("Host to device") { + LinearAllocGuard2D device_alloc(cols, rows); + LinearAllocGuard host_alloc(LinearAllocs::hipHostMalloc, device_alloc.pitch() * rows); + NegativeTests(device_alloc.ptr(), device_alloc.pitch(), host_alloc.ptr(), device_alloc.pitch(), + device_alloc.width(), device_alloc.height(), hipMemcpyHostToDevice); } - SECTION("Source Pitch is 0") { - desc.srcPitch = 0; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + SECTION("Device to host") { + LinearAllocGuard2D device_alloc(cols, rows); + LinearAllocGuard host_alloc(LinearAllocs::hipHostMalloc, device_alloc.pitch() * rows); + NegativeTests(host_alloc.ptr(), device_alloc.pitch(), device_alloc.ptr(), device_alloc.pitch(), + device_alloc.width(), device_alloc.height(), hipMemcpyDeviceToHost); } - SECTION("Height is 0") { - desc.Height = 0; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); - HIP_CHECK(hipStreamSynchronize(stream)); - REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + SECTION("Host to host") { + LinearAllocGuard src_alloc(LinearAllocs::hipHostMalloc, cols * rows * sizeof(int)); + LinearAllocGuard dst_alloc(LinearAllocs::hipHostMalloc, cols * rows * sizeof(int)); + NegativeTests(dst_alloc.ptr(), cols * sizeof(int), src_alloc.ptr(), cols * sizeof(int), + cols * sizeof(int), rows, hipMemcpyHostToHost); } - SECTION("Width is 0") { - desc.Height = 0; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); - HIP_CHECK(hipStreamSynchronize(stream)); - REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + SECTION("Device to device") { + LinearAllocGuard2D src_alloc(cols, rows); + LinearAllocGuard2D dst_alloc(cols, rows); + NegativeTests(dst_alloc.ptr(), dst_alloc.pitch(), src_alloc.ptr(), src_alloc.pitch(), + dst_alloc.width(), dst_alloc.height(), hipMemcpyDeviceToDevice); } - - // DeAllocating the Memory - HIP_CHECK(hipFree(A_d)); - HIP_CHECK(hipStreamDestroy(stream)); - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, B_h, C_h, false); -} - -/* - * This testcase verifies the negative scenarios - */ -TEST_CASE("Unit_hipMemcpyParam2DAsync_Negative") { - CHECK_IMAGE_SUPPORT - - HIP_CHECK(hipSetDevice(0)); - float* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, - * A_d{nullptr}; - size_t pitch_A; - size_t width{NUM_W * sizeof(float)}; - constexpr auto memsetval{100}; - hipStream_t stream; - HIP_CHECK(hipStreamCreate(&stream)); - - // Allocating and Initializing the data - HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), - &pitch_A, width, NUM_H)); - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, &B_h, &C_h, - width*NUM_H, false); - HipTest::setDefaultData(NUM_W*NUM_H, A_h, B_h, C_h); - HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); - - // Device to Host - hip_Memcpy2D desc = {}; - desc.srcMemoryType = hipMemoryTypeDevice; - desc.srcHost = A_d; - desc.srcDevice = hipDeviceptr_t(A_d); - desc.srcPitch = pitch_A; - desc.dstMemoryType = hipMemoryTypeHost; - desc.dstHost = A_h; - desc.dstDevice = hipDeviceptr_t(A_h); - desc.dstPitch = width; - desc.WidthInBytes = NUM_W; - desc.Height = NUM_H; - - SECTION("Null Pointer to Source Device Pointer") { - desc.srcDevice = hipDeviceptr_t(nullptr); - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); - } - - SECTION("Null Pointer to Destination Device Pointer") { - memset(&desc, 0x0, sizeof(hip_Memcpy2D)); - desc.srcMemoryType = hipMemoryTypeHost; - desc.srcHost = A_h; - desc.srcDevice = hipDeviceptr_t(A_h); - desc.srcPitch = width; - desc.dstMemoryType = hipMemoryTypeDevice; - desc.dstHost = A_d; - desc.dstDevice = hipDeviceptr_t(nullptr); - desc.dstPitch = pitch_A; - desc.WidthInBytes = NUM_W; - desc.Height = NUM_H; - - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); - } - - SECTION("Null Pointer to both Src & Dst Device Pointer") { - desc.srcDevice = hipDeviceptr_t(nullptr); - desc.dstDevice = hipDeviceptr_t(nullptr); - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); - } - - SECTION("Width > src/dest pitches") { - desc.WidthInBytes = pitch_A+1; - REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); - } - - // DeAllocating the memory - HIP_CHECK(hipFree(A_d)); - HIP_CHECK(hipStreamSynchronize(stream)); - HIP_CHECK(hipStreamDestroy(stream)); - HipTest::freeArrays(nullptr, nullptr, nullptr, - A_h, B_h, C_h, false); -} +} \ No newline at end of file diff --git a/projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync_old.cc b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync_old.cc new file mode 100644 index 0000000000..763ef9185d --- /dev/null +++ b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2DAsync_old.cc @@ -0,0 +1,441 @@ +/* +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 WARRANNTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNNESS 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 INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +/* +This testfile verifies the following scenarios of hipMemcpyParam2DAsync API +1. Negative Scenarios +2. Extent Validation Scenarios +3. D2D copy for different datatypes +4. H2D and D2H copy for different datatypes +5. Device context change scenario where memory allocated in one GPU + stream created in another GPU +*/ + +#include +#include + +static constexpr size_t NUM_W{10}; +static constexpr size_t NUM_H{10}; +/* + * This testcase verifies D2D functionality of hipMemcpyParam2DAsync API + * Where Memory is allocated in GPU-0 and stream is created in GPU-1 + * + * Input: Intializing "A_d" device variable with "C_h" host variable + * Output: "A_d" device variable to "E_d" device variable + * + * Validating the result by copying "E_d" to "A_h" and checking + * it with the initalized data "C_h". + * + */ +TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2DAsync_multiDevice-StreamOnDiffDevice", + "[hipMemcpyParam2DAsync]", char, float, int, double, long double) { + CHECK_IMAGE_SUPPORT + + int numDevices = 0; + HIP_CHECK(hipGetDeviceCount(&numDevices)); + if (numDevices > 1) { + // Allocating and Initializing the data + HIP_CHECK(hipSetDevice(0)); + TestType* A_h{nullptr}, *C_h{nullptr}, *A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(TestType)}; + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, false); + HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); + int peerAccess = 0; + HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); + if (!peerAccess) { + SUCCEED("Skipped the test as there is no peer access"); + } else { + TestType *E_d{nullptr}; + size_t pitch_E; + HIP_CHECK(hipMallocPitch(reinterpret_cast(&E_d), + &pitch_E, width, NUM_H)); + + // Initalizing A_d with C_h + HIP_CHECK(hipSetDevice(1)); + hipStream_t stream; + HIP_CHECK(hipStreamCreate(&stream)); + + HIP_CHECK(hipMemcpy2DAsync(A_d, pitch_A, C_h, width, + NUM_W*sizeof(TestType), NUM_H, + hipMemcpyHostToDevice, stream)); + HIP_CHECK(hipStreamSynchronize(stream)); + // Device to Device + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeDevice; + desc.dstHost = E_d; + desc.dstDevice = hipDeviceptr_t(E_d); + desc.dstPitch = pitch_E; + desc.WidthInBytes = NUM_W*sizeof(TestType); + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + HIP_CHECK(hipStreamSynchronize(stream)); + + // Copying the result E_d to A_h host variable + HIP_CHECK(hipMemcpy2D(A_h, width, E_d, pitch_E, + NUM_W*sizeof(TestType), NUM_H, + hipMemcpyDeviceToHost)); + HIP_CHECK(hipDeviceSynchronize()); + // Validating the result + REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); + + // DeAllocating the memory + HIP_CHECK(hipFree(E_d)); + HIP_CHECK(hipFree(A_d)); + HIP_CHECK(hipStreamDestroy(stream)); + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, nullptr, C_h, false); + } + } else { + SUCCEED("skipping the testcases as numDevices < 2"); + } +} + +/* + * This testcase verifies D2D functionality of hipMemcpyParam2DAsync API + * Input: Intializing "A_d" device variable with "C_h" host variable + * Output: "A_d" device variable to "E_d" device variable + * + * Validating the result by copying "E_d" to "A_h" and checking + * it with the initalized data "C_h". + * + */ +TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2DAsync_multiDevice-D2D", "[hipMemcpyParam2DAsync]", char, + int, float, double, long double) { + CHECK_IMAGE_SUPPORT + + int numDevices = 0; + HIP_CHECK(hipGetDeviceCount(&numDevices)); + if (numDevices > 1) { + // Allocating and Initializing the data + HIP_CHECK(hipSetDevice(0)); + TestType* A_h{nullptr}, *C_h{nullptr}, *A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(TestType)}; + hipStream_t stream; + HIP_CHECK(hipStreamCreate(&stream)); + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, false); + HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); + + int peerAccess = 0; + HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); + if (!peerAccess) { + SUCCEED("Skipped the test as there is no peer access"); + } else { + HIP_CHECK(hipSetDevice(1)); + TestType *E_d; + size_t pitch_E; + HIP_CHECK(hipMallocPitch(reinterpret_cast(&E_d), + &pitch_E, width, NUM_H)); + + // Initializing A_d with C_h + HIP_CHECK(hipMemcpy2D(A_d, pitch_A, C_h, width, + NUM_W*sizeof(TestType), NUM_H, hipMemcpyHostToDevice)); + + // Device to Device + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeDevice; + desc.dstHost = E_d; + desc.dstDevice = hipDeviceptr_t(E_d); + desc.dstPitch = pitch_E; + desc.WidthInBytes = NUM_W*sizeof(TestType); + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + HIP_CHECK(hipStreamSynchronize(stream)); + + // Copying the result E_d to A_h host variable + HIP_CHECK(hipMemcpy2D(A_h, width, E_d, pitch_E, + NUM_W*sizeof(TestType), NUM_H, hipMemcpyDeviceToHost)); + + // Validating the result + REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); + + // DeAllocating the memory + HIP_CHECK(hipFree(A_d)); + HIP_CHECK(hipStreamDestroy(stream)); + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, nullptr, C_h, false); + } + } else { + SUCCEED("skipping the testcases as numDevices < 2"); + } +} + +/* + * This testcase verifies H2D & D2H functionality of hipMemcpyParam2DAsync API + * H2D case: + * Input: "C_h" host variable initialized with default data + * Output: "A_d" device variable + * + * D2H case: + * Input: "A_d" device variable from the previous output + * OutPut: "A_h" variable + * + * Validating the result by comparing "A_h" to "C_h" + */ +TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2DAsync_multiDevice-H2D-D2H", "[hipMemcpyParam2DAsync]", + char, int, float, double, long double) { + CHECK_IMAGE_SUPPORT + + // 1 refers to pinned host memory and 0 refers + // to unpinned memory + auto memory_type = GENERATE(0, 1); + int numDevices = 0; + HIP_CHECK(hipGetDeviceCount(&numDevices)); + if (numDevices > 1) { + // Allocating and Initializing the data + HIP_CHECK(hipSetDevice(0)); + TestType* A_h{nullptr}, *C_h{nullptr}, + *A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(TestType)}; + hipStream_t stream; + + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + + // Based on memory type (pinned/unpinned) allocating memory + if (memory_type) { + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, true); + } else { + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, false); + } + HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); + int peerAccess = 0; + HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); + if (!peerAccess) { + SUCCEED("Skipped the test as there is no peer access"); + } else { + // Host to Device + hip_Memcpy2D desc = {}; + HIP_CHECK(hipStreamCreate(&stream)); + desc.srcMemoryType = hipMemoryTypeHost; + desc.srcHost = C_h; + desc.srcDevice = hipDeviceptr_t(C_h); + desc.srcPitch = width; + desc.dstMemoryType = hipMemoryTypeDevice; + desc.dstHost = A_d; + desc.dstDevice = hipDeviceptr_t(A_d); + desc.dstPitch = pitch_A; + desc.WidthInBytes = NUM_W*sizeof(TestType); + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + HIP_CHECK(hipStreamSynchronize(stream)); + + // Device to Host + memset(&desc, 0x0, sizeof(hip_Memcpy2D)); + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeHost; + desc.dstHost = A_h; + desc.dstDevice = hipDeviceptr_t(A_h); + desc.dstPitch = width; + desc.WidthInBytes = NUM_W*sizeof(TestType); + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + HIP_CHECK(hipStreamSynchronize(stream)); + + // Validating the result + REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); + + // DeAllocating the memory + HIP_CHECK(hipFree(A_d)); + HIP_CHECK(hipStreamDestroy(stream)); + if (memory_type) { + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, nullptr, C_h, true); + } else { + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, nullptr, C_h, false); + } + } + } else { + SUCCEED("skipping the testcases as numDevices < 2"); + } +} +/* + * This testcase verifies the extent validation scenarios + */ +TEST_CASE("Unit_hipMemcpyParam2DAsync_ExtentValidation") { + CHECK_IMAGE_SUPPORT + + HIP_CHECK(hipSetDevice(0)); + char* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, + * A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(char)}; + constexpr auto memsetval{100}; + hipStream_t stream; + HIP_CHECK(hipStreamCreate(&stream)); + + // Allocating and Initializing the data + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, false); + HipTest::initArrays(nullptr, nullptr, nullptr, + &B_h, nullptr, nullptr, + width*NUM_H, false); + HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); + HipTest::setDefaultData(NUM_W*NUM_H, B_h, nullptr, nullptr); + HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); + + // Device to Host + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeHost; + desc.dstHost = A_h; + desc.dstDevice = hipDeviceptr_t(A_h); + desc.dstPitch = width; + desc.WidthInBytes = NUM_W; + desc.Height = NUM_H; + + SECTION("Destination Pitch is 0") { + desc.dstPitch = 0; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + } + + SECTION("Source Pitch is 0") { + desc.srcPitch = 0; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + } + + SECTION("Height is 0") { + desc.Height = 0; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + HIP_CHECK(hipStreamSynchronize(stream)); + REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + } + + SECTION("Width is 0") { + desc.Height = 0; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) == hipSuccess); + HIP_CHECK(hipStreamSynchronize(stream)); + REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + } + + // DeAllocating the Memory + HIP_CHECK(hipFree(A_d)); + HIP_CHECK(hipStreamDestroy(stream)); + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, B_h, C_h, false); +} + +/* + * This testcase verifies the negative scenarios + */ +TEST_CASE("Unit_hipMemcpyParam2DAsync_Negative") { + CHECK_IMAGE_SUPPORT + + HIP_CHECK(hipSetDevice(0)); + float* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, + * A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(float)}; + constexpr auto memsetval{100}; + hipStream_t stream; + HIP_CHECK(hipStreamCreate(&stream)); + + // Allocating and Initializing the data + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, &B_h, &C_h, + width*NUM_H, false); + HipTest::setDefaultData(NUM_W*NUM_H, A_h, B_h, C_h); + HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); + + // Device to Host + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeHost; + desc.dstHost = A_h; + desc.dstDevice = hipDeviceptr_t(A_h); + desc.dstPitch = width; + desc.WidthInBytes = NUM_W; + desc.Height = NUM_H; + + SECTION("Null Pointer to Source Device Pointer") { + desc.srcDevice = hipDeviceptr_t(nullptr); + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); + } + + SECTION("Null Pointer to Destination Device Pointer") { + memset(&desc, 0x0, sizeof(hip_Memcpy2D)); + desc.srcMemoryType = hipMemoryTypeHost; + desc.srcHost = A_h; + desc.srcDevice = hipDeviceptr_t(A_h); + desc.srcPitch = width; + desc.dstMemoryType = hipMemoryTypeDevice; + desc.dstHost = A_d; + desc.dstDevice = hipDeviceptr_t(nullptr); + desc.dstPitch = pitch_A; + desc.WidthInBytes = NUM_W; + desc.Height = NUM_H; + + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); + } + + SECTION("Null Pointer to both Src & Dst Device Pointer") { + desc.srcDevice = hipDeviceptr_t(nullptr); + desc.dstDevice = hipDeviceptr_t(nullptr); + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); + } + + SECTION("Width > src/dest pitches") { + desc.WidthInBytes = pitch_A+1; + REQUIRE(hipMemcpyParam2DAsync(&desc, stream) != hipSuccess); + } + + // DeAllocating the memory + HIP_CHECK(hipFree(A_d)); + HIP_CHECK(hipStreamSynchronize(stream)); + HIP_CHECK(hipStreamDestroy(stream)); + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, B_h, C_h, false); +} diff --git a/projects/hip-tests/catch/unit/memory/hipMemcpyParam2D_old.cc b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2D_old.cc new file mode 100644 index 0000000000..3a35a1a16e --- /dev/null +++ b/projects/hip-tests/catch/unit/memory/hipMemcpyParam2D_old.cc @@ -0,0 +1,337 @@ +/* +Copyright (c) 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 WARRANNTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNNESS 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 INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +/* +This testfile verifies the following scenarios of hipMemcpyParam2D API +1. Negative Scenarios +2. Extent Validation Scenarios +3. D2D copy for different datatypes +4. H2D and D2H copy for different datatypes +*/ + +#include +#include + +static constexpr size_t NUM_W{10}; +static constexpr size_t NUM_H{10}; +/* + * This testcase verifies D2D functionality of hipMemcpyParam2D API + * Input: Intializing "A_d" device variable with "C_h" host variable + * Output: "A_d" device variable to "E_d" device variable + * + * Validating the result by copying "E_d" to "A_h" and checking + * it with the initalized data "C_h". + * + */ +TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2D_multiDevice-D2D", "[hipMemcpyParam2D]", char, float, int, + double, long double) { + CHECK_IMAGE_SUPPORT + + int numDevices = 0; + HIP_CHECK(hipGetDeviceCount(&numDevices)); + if (numDevices > 1) { + // Initialize and Allocating Memory + HIP_CHECK(hipSetDevice(0)); + TestType* A_h{nullptr}, *C_h{nullptr}, *A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(TestType)}; + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, false); + HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); + + int peerAccess = 0; + HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); + if (!peerAccess) { + SUCCEED("Skipped the test as there is no peer access"); + } else { + HIP_CHECK(hipSetDevice(1)); + char *E_d; + size_t pitch_E; + HIP_CHECK(hipMallocPitch(reinterpret_cast(&E_d), + &pitch_E, width, NUM_H)); + + // Initalizing A_d with C_h + HIP_CHECK(hipMemcpy2D(A_d, pitch_A, C_h, width, + NUM_W * sizeof(TestType), NUM_H, hipMemcpyHostToDevice)); + + // Device to Device + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeDevice; + desc.dstHost = E_d; + desc.dstDevice = hipDeviceptr_t(E_d); + desc.dstPitch = pitch_E; + desc.WidthInBytes = NUM_W * sizeof(TestType); + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + + // Copying E_d to A_h + HIP_CHECK(hipMemcpy2D(A_h, width, E_d, pitch_E, + NUM_W * sizeof(TestType), NUM_H, + hipMemcpyDeviceToHost)); + + // Validating the result + REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); + + // DeAllocating the memory + HIP_CHECK(hipFree(A_d)); + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, nullptr, C_h, false); + } + } else { + SUCCEED("skipping the testcases as numDevices < 2"); + } +} + +/* + * This testcase verifies H2D & D2H functionality of hipMemcpyParam2D API + * H2D case: + * Input: "C_h" host variable initialized with default data + * Output: "A_d" device variable + * + * D2H case: + * Input: "A_d" device variable from the previous output + * OutPut: "A_h" variable + * + * Validating the result by comparing "A_h" to "C_h" + */ +TEMPLATE_TEST_CASE("Unit_hipMemcpyParam2D_multiDevice-H2D-D2H", "[hipMemcpyParam2D]", char, float, + int, double, long double) { + CHECK_IMAGE_SUPPORT + + // 1 refers to pinned host memory and 0 refers + // to unpinned memory + auto memory_type = GENERATE(0, 1); + int numDevices = 0; + HIP_CHECK(hipGetDeviceCount(&numDevices)); + if (numDevices > 1) { + HIP_CHECK(hipSetDevice(0)); + + // Initialize and Allocating Memory + TestType* A_h{nullptr}, *C_h{nullptr}, + *A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(TestType)}; + + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + + // Based on memory type (pinned/unpinned) allocating memory + if (memory_type) { + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, true); + } else { + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, false); + } + HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); + int peerAccess = 0; + HIP_CHECK(hipDeviceCanAccessPeer(&peerAccess, 1, 0)); + if (!peerAccess) { + SUCCEED("Skipped the test as there is no peer access"); + } else { + // Host to Device + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeHost; + desc.srcHost = C_h; + desc.srcDevice = hipDeviceptr_t(C_h); + desc.srcPitch = width; + desc.dstMemoryType = hipMemoryTypeDevice; + desc.dstHost = A_d; + desc.dstDevice = hipDeviceptr_t(A_d); + desc.dstPitch = pitch_A; + desc.WidthInBytes = NUM_W*sizeof(TestType); + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + + // Device to Host + memset(&desc, 0x0, sizeof(hip_Memcpy2D)); + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeHost; + desc.dstHost = A_h; + desc.dstDevice = hipDeviceptr_t(A_h); + desc.dstPitch = width; + desc.WidthInBytes = NUM_W*sizeof(TestType); + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + + // Validating the result + REQUIRE(HipTest::checkArray(A_h, C_h, NUM_W, NUM_H) == true); + + // DeAllocating the Memory + HIP_CHECK(hipFree(A_d)); + if (memory_type) { + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, nullptr, C_h, true); + } else { + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, nullptr, C_h, false); + } + } + } else { + SUCCEED("skipping the testcases as numDevices < 2"); + } +} +/* + * This testcase verifies the extent validation scenarios + */ +TEST_CASE("Unit_hipMemcpyParam2D_ExtentValidation") { + CHECK_IMAGE_SUPPORT + + // Allocating memory and Initializing the data + HIP_CHECK(hipSetDevice(0)); + char* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, + * A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(char)}; + constexpr auto memsetval{100}; + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, nullptr, &C_h, + width*NUM_H, false); + HipTest::initArrays(nullptr, nullptr, nullptr, + &B_h, nullptr, nullptr, + width*NUM_H, false); + HipTest::setDefaultData(NUM_W*NUM_H, A_h, nullptr, C_h); + HipTest::setDefaultData(NUM_W*NUM_H, B_h, nullptr, nullptr); + HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); + + // Device to Host + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeHost; + desc.dstHost = A_h; + desc.dstDevice = hipDeviceptr_t(A_h); + desc.dstPitch = width; + desc.WidthInBytes = NUM_W; + desc.Height = NUM_H; + + SECTION("Destination Pitch is 0") { + desc.dstPitch = 0; + REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + } + + SECTION("Source Pitch is 0") { + desc.srcPitch = 0; + REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + } + + SECTION("Height is 0") { + desc.Height = 0; + REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + } + + SECTION("Width is 0") { + desc.WidthInBytes = 0; + REQUIRE(hipMemcpyParam2D(&desc) == hipSuccess); + REQUIRE(HipTest::checkArray(A_h, B_h, NUM_W, NUM_H) == true); + } + + // DeAllocating the Memory + HIP_CHECK(hipFree(A_d)); + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, B_h, C_h, false); +} + +/* + * This testcase verifies the negative scenarios + */ +TEST_CASE("Unit_hipMemcpyParam2D_Negative") { + CHECK_IMAGE_SUPPORT + + HIP_CHECK(hipSetDevice(0)); + + // Allocating and Initializing the data + float* A_h{nullptr}, *B_h{nullptr}, *C_h{nullptr}, + * A_d{nullptr}; + size_t pitch_A; + size_t width{NUM_W * sizeof(float)}; + constexpr auto memsetval{100}; + HIP_CHECK(hipMallocPitch(reinterpret_cast(&A_d), + &pitch_A, width, NUM_H)); + HipTest::initArrays(nullptr, nullptr, nullptr, + &A_h, &B_h, &C_h, + width*NUM_H, false); + HipTest::setDefaultData(NUM_W*NUM_H, A_h, B_h, C_h); + HIP_CHECK(hipMemset2D(A_d, pitch_A, memsetval, NUM_W, NUM_H)); + + hip_Memcpy2D desc = {}; + desc.srcMemoryType = hipMemoryTypeDevice; + desc.srcHost = A_d; + desc.srcDevice = hipDeviceptr_t(A_d); + desc.srcPitch = pitch_A; + desc.dstMemoryType = hipMemoryTypeHost; + desc.dstHost = A_h; + desc.dstDevice = hipDeviceptr_t(A_h); + desc.dstPitch = width; + desc.WidthInBytes = NUM_W; + desc.Height = NUM_H; + + SECTION("Null Pointer to Source Device Pointer") { + desc.srcDevice = hipDeviceptr_t(nullptr); + REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); + } + + SECTION("Null Pointer to Destination Device Pointer") { + memset(&desc, 0x0, sizeof(hip_Memcpy2D)); + desc.srcMemoryType = hipMemoryTypeHost; + desc.srcHost = A_h; + desc.srcDevice = hipDeviceptr_t(A_h); + desc.srcPitch = width; + desc.dstMemoryType = hipMemoryTypeDevice; + desc.dstHost = A_d; + desc.dstDevice = hipDeviceptr_t(nullptr); + desc.dstPitch = pitch_A; + desc.WidthInBytes = NUM_W; + desc.Height = NUM_H; + REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); + } + + SECTION("Null Pointer to both Src & Dst Device Pointer") { + desc.srcDevice = hipDeviceptr_t(nullptr); + desc.dstDevice = hipDeviceptr_t(nullptr); + REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); + } + + SECTION("Width > src/dest pitches") { + desc.WidthInBytes = pitch_A+1; + REQUIRE(hipMemcpyParam2D(&desc) != hipSuccess); + } + + // DeAllocating the Memory + HIP_CHECK(hipFree(A_d)); + HipTest::freeArrays(nullptr, nullptr, nullptr, + A_h, B_h, C_h, false); +} diff --git a/projects/hip-tests/catch/unit/memory/memcpy2d_tests_common.hh b/projects/hip-tests/catch/unit/memory/memcpy2d_tests_common.hh index 990dd9dcdc..9f489a2e23 100644 --- a/projects/hip-tests/catch/unit/memory/memcpy2d_tests_common.hh +++ b/projects/hip-tests/catch/unit/memory/memcpy2d_tests_common.hh @@ -22,10 +22,13 @@ THE SOFTWARE. #pragma once +#include + #include #include #include #include +#include template void Memcpy2DDeviceToHostShell(F memcpy_func, const hipStream_t kernel_stream = nullptr) { @@ -322,4 +325,212 @@ void Memcpy2DZeroWidthHeight(F memcpy_func, const hipStream_t stream = nullptr) } ArrayFindIfNot(dst_alloc.ptr(), static_cast(42), alloc_size); } +} + +constexpr auto MemTypeHost() { +#if HT_AMD + return hipMemoryTypeHost; +#else + return CU_MEMORYTYPE_HOST; +#endif +} + +constexpr auto MemTypeDevice() { +#if HT_AMD + return hipMemoryTypeDevice; +#else + return CU_MEMORYTYPE_DEVICE; +#endif +} + +constexpr auto MemTypeArray() { +#if HT_AMD + return hipMemoryTypeArray; +#else + return CU_MEMORYTYPE_ARRAY; +#endif +} + +constexpr auto MemTypeUnified() { +#if HT_AMD + return hipMemoryTypeUnified; +#else + return CU_MEMORYTYPE_UNIFIED; +#endif +} + +using PtrVariant = std::variant; + +template +constexpr auto MemcpyParam2DAdapter(const hipExtent src_offset = {0, 0, 0}, + const hipExtent dst_offset = {0, 0, 0}) { + return [=](PtrVariant dst, size_t dpitch, PtrVariant src, size_t spitch, size_t width, + size_t height, hipMemcpyKind kind, hipStream_t stream = nullptr) { + hip_Memcpy2D parms = {}; + + if (std::holds_alternative(dst)) { + parms.dstMemoryType = MemTypeArray(); + parms.dstArray = std::get(dst); + } else { + parms.dstPitch = dpitch; + auto ptr = std::get(dst); + switch (kind) { + case hipMemcpyDeviceToHost: + case hipMemcpyHostToHost: + parms.dstMemoryType = MemTypeHost(); + parms.dstHost = ptr; + break; + case hipMemcpyDeviceToDevice: + case hipMemcpyHostToDevice: + parms.dstMemoryType = MemTypeDevice(); + parms.dstDevice = reinterpret_cast(ptr); + break; + case hipMemcpyDefault: + parms.dstMemoryType = MemTypeUnified(); + parms.dstDevice = reinterpret_cast(ptr); + break; + default: + assert(false); + } + } + + if (std::holds_alternative(src)) { + parms.srcMemoryType = MemTypeArray(); + parms.srcArray = std::get(src); + } else { + parms.srcPitch = spitch; + auto ptr = std::get(src); + switch (kind) { + case hipMemcpyDeviceToHost: + case hipMemcpyDeviceToDevice: + parms.srcMemoryType = MemTypeDevice(); + parms.srcDevice = reinterpret_cast(ptr); + break; + case hipMemcpyHostToDevice: + case hipMemcpyHostToHost: + parms.srcMemoryType = MemTypeHost(); + parms.srcHost = ptr; + break; + case hipMemcpyDefault: + parms.srcMemoryType = MemTypeUnified(); + parms.srcDevice = reinterpret_cast(ptr); + break; + default: + assert(false); + } + } + + parms.WidthInBytes = width; + parms.Height = height; + parms.srcXInBytes = src_offset.width; + parms.srcY = src_offset.height; + parms.dstXInBytes = dst_offset.width; + parms.dstY = dst_offset.height; + + if constexpr (async) { + return hipMemcpyParam2DAsync(&parms, stream); + } else { + return hipMemcpyParam2D(&parms); + } + }; +} + +template +void MemcpyParam2DArrayHostShell(F memcpy_func, const hipStream_t kernel_stream = nullptr) { + constexpr hipExtent extent{127 * sizeof(int), 128, 1}; + + LinearAllocGuard src_host(LinearAllocs::hipHostMalloc, + extent.width * extent.height * extent.depth); + LinearAllocGuard dst_host(LinearAllocs::hipHostMalloc, + extent.width * extent.height * extent.depth); + + DrvArrayAllocGuard src_array(extent); + DrvArrayAllocGuard dst_array(extent); + + const auto f = [](size_t x, size_t y, size_t z) { + constexpr auto width_logical = extent.width / sizeof(int); + return z * width_logical * extent.height + y * width_logical + x; + }; + PitchedMemorySet(src_host.ptr(), extent.width, extent.width / sizeof(int), extent.height, + extent.depth, f); + + // Host -> Array + HIP_CHECK(memcpy_func(src_array.ptr(), 0, src_host.ptr(), extent.width, extent.width, + extent.height, hipMemcpyHostToDevice, kernel_stream)); + if constexpr (should_synchronize) { + HIP_CHECK(hipStreamSynchronize(kernel_stream)); + } + + // Array -> Array + HIP_CHECK(memcpy_func(dst_array.ptr(), 0, src_array.ptr(), 0, extent.width, extent.height, + hipMemcpyDeviceToDevice, kernel_stream)); + if constexpr (should_synchronize) { + HIP_CHECK(hipStreamSynchronize(kernel_stream)); + } + + // Array -> Host + HIP_CHECK(memcpy_func(dst_host.ptr(), extent.width, dst_array.ptr(), 0, extent.width, + extent.height, hipMemcpyDeviceToHost, kernel_stream)); + if constexpr (should_synchronize) { + HIP_CHECK(hipStreamSynchronize(kernel_stream)); + } + + PitchedMemoryVerify(dst_host.ptr(), extent.width, extent.width / sizeof(int), extent.height, + extent.depth, f); +} + +template +void MemcpyParam2DArrayDeviceShell(F memcpy_func, const hipStream_t kernel_stream = nullptr) { + constexpr hipExtent extent{127 * sizeof(int), 128, 1}; + + LinearAllocGuard host_alloc(LinearAllocs::hipHostMalloc, + extent.width * extent.height * extent.depth); + + DrvArrayAllocGuard src_array(extent); + DrvArrayAllocGuard dst_array(extent); + + LinearAllocGuard3D src_device(extent); + LinearAllocGuard3D dst_device(extent); + + const dim3 threads_per_block(32, 32); + const dim3 blocks(src_device.width_logical() / threads_per_block.x + 1, + src_device.height() / threads_per_block.y + 1, src_device.depth()); + Iota<<>>(src_device.ptr(), src_device.pitch(), + src_device.width_logical(), src_device.height(), + src_device.depth()); + HIP_CHECK(hipGetLastError()); + + // Device -> Array + HIP_CHECK(memcpy_func(src_array.ptr(), 0, src_device.ptr(), src_device.pitch(), extent.width, + extent.height, hipMemcpyDeviceToDevice, kernel_stream)); + if constexpr (should_synchronize) { + HIP_CHECK(hipStreamSynchronize(kernel_stream)); + } + + // Array -> Array + HIP_CHECK(memcpy_func(dst_array.ptr(), 0, src_array.ptr(), 0, extent.width, extent.height, + hipMemcpyDeviceToDevice, kernel_stream)); + if constexpr (should_synchronize) { + HIP_CHECK(hipStreamSynchronize(kernel_stream)); + } + + // Array -> Device + HIP_CHECK(memcpy_func(dst_device.ptr(), dst_device.pitch(), dst_array.ptr(), 0, extent.width, + extent.height, hipMemcpyDeviceToDevice, kernel_stream)); + if constexpr (should_synchronize) { + HIP_CHECK(hipStreamSynchronize(kernel_stream)); + } + + HIP_CHECK(memcpy_func(host_alloc.ptr(), extent.width, dst_device.ptr(), dst_device.pitch(), + extent.width, extent.height, hipMemcpyDeviceToHost, kernel_stream)); + if constexpr (should_synchronize) { + HIP_CHECK(hipStreamSynchronize(kernel_stream)); + } + + const auto f = [](size_t x, size_t y, size_t z) { + constexpr auto width_logical = extent.width / sizeof(int); + return z * width_logical * extent.height + y * width_logical + x; + }; + PitchedMemoryVerify(host_alloc.ptr(), extent.width, extent.width / sizeof(int), extent.height, + extent.depth, f); } \ No newline at end of file