diff --git a/projects/hip-tests/catch/unit/memory/CMakeLists.txt b/projects/hip-tests/catch/unit/memory/CMakeLists.txt index 2b846d7c41..6554caec00 100644 --- a/projects/hip-tests/catch/unit/memory/CMakeLists.txt +++ b/projects/hip-tests/catch/unit/memory/CMakeLists.txt @@ -80,6 +80,8 @@ set(TEST_SRC hipMemcpy_derivatives.cc hipMemcpyAsync.cc hipMemsetFunctional.cc + hipMalloc.cc + hipExtMallocWithFlags.cc hipMallocPitch.cc hipMallocArray.cc hipMalloc3D.cc @@ -160,6 +162,7 @@ set(TEST_SRC hipMemcpy_derivatives.cc hipMemcpyAsync.cc hipMemsetFunctional.cc + hipMalloc.cc hipMallocPitch.cc hipMallocArray.cc hipMalloc3D.cc diff --git a/projects/hip-tests/catch/unit/memory/hipExtMallocWithFlags.cc b/projects/hip-tests/catch/unit/memory/hipExtMallocWithFlags.cc new file mode 100644 index 0000000000..8743254283 --- /dev/null +++ b/projects/hip-tests/catch/unit/memory/hipExtMallocWithFlags.cc @@ -0,0 +1,134 @@ +/* +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 +#include + +TEST_CASE("Unit_hipExtMallocWithFlags_Positive_Basic") { + void* ptr = nullptr; + + SECTION("hipDeviceMallocDefault") { + const auto alloc_size = + GENERATE_COPY(10, kPageSize / 2, kPageSize, kPageSize * 3 / 2, kPageSize * 2); + HIP_CHECK(hipExtMallocWithFlags(&ptr, alloc_size, hipDeviceMallocDefault)); + CHECK(ptr != nullptr); + CHECK(reinterpret_cast(ptr) % 256 == 0); + HIP_CHECK(hipFree(ptr)); + } + + SECTION("hipDeviceMallocFinegrained") { + if (!DeviceAttributesSupport(0, hipDeviceAttributeFineGrainSupport)) { + HipTest::HIP_SKIP_TEST("Device does not support fine-grained memory allocations"); + return; + } + const auto alloc_size = + GENERATE_COPY(10, kPageSize / 2, kPageSize, kPageSize * 3 / 2, kPageSize * 2); + HIP_CHECK(hipExtMallocWithFlags(&ptr, alloc_size, hipDeviceMallocFinegrained)); + CHECK(ptr != nullptr); + CHECK(reinterpret_cast(ptr) % 256 == 0); + HIP_CHECK(hipFree(ptr)); + } + + SECTION("hipMallocSignalMemory") { + HIP_CHECK(hipExtMallocWithFlags(&ptr, 8, hipMallocSignalMemory)); + CHECK(ptr != nullptr); + HIP_CHECK(hipFree(ptr)); + } +} + +TEST_CASE("Unit_hipExtMallocWithFlags_Positive_Zero_Size") { + void* ptr = reinterpret_cast(0x1); + const auto flag = GENERATE(hipDeviceMallocDefault, hipDeviceMallocFinegrained); + HIP_CHECK(hipExtMallocWithFlags(&ptr, 0, flag)); + REQUIRE(ptr == nullptr); +} + +TEST_CASE("Unit_hipExtMallocWithFlags_Positive_Alignment") { + void *ptr1 = nullptr, *ptr2 = nullptr; + const auto flag = GENERATE(hipDeviceMallocDefault, hipDeviceMallocFinegrained); + if (flag == hipDeviceMallocFinegrained && + !DeviceAttributesSupport(0, hipDeviceAttributeFineGrainSupport)) { + HipTest::HIP_SKIP_TEST("Device does not support fine-grained memory allocations"); + return; + } + HIP_CHECK(hipExtMallocWithFlags(&ptr1, 1, flag)); + HIP_CHECK(hipExtMallocWithFlags(&ptr2, 10, flag)); + CHECK(reinterpret_cast(ptr1) % 256 == 0); + CHECK(reinterpret_cast(ptr2) % 256 == 0); + HIP_CHECK(hipFree(ptr1)); + HIP_CHECK(hipFree(ptr2)); +} + +TEST_CASE("Unit_hipExtMallocWithFlags_Negative_Parameters") { + SECTION("Invalid flags") { + void* ptr = nullptr; + HIP_CHECK_ERROR( + hipExtMallocWithFlags(&ptr, 4096, hipDeviceMallocDefault | hipMallocSignalMemory), + hipErrorInvalidValue); + } + + SECTION("hipDeviceMallocDefault") { + SECTION("ptr == nullptr") { + HIP_CHECK_ERROR(hipExtMallocWithFlags(nullptr, 4096, hipDeviceMallocDefault), + hipErrorInvalidValue); + } + + SECTION("size == max size_t") { + void* ptr; + HIP_CHECK_ERROR( + hipExtMallocWithFlags(&ptr, std::numeric_limits::max(), hipDeviceMallocDefault), + hipErrorOutOfMemory); + } + } + + SECTION("hipDeviceMallocFinegrained") { + SECTION("ptr == nullptr") { + HIP_CHECK_ERROR(hipExtMallocWithFlags(nullptr, 4096, hipDeviceMallocFinegrained), + hipErrorInvalidValue); + } + + SECTION("size == max size_t") { + void* ptr; + HIP_CHECK_ERROR(hipExtMallocWithFlags(&ptr, std::numeric_limits::max(), + hipDeviceMallocFinegrained), + hipErrorOutOfMemory); + } + } + + SECTION("hipMallocSignalMemory") { + SECTION("ptr == nullptr") { + HIP_CHECK_ERROR(hipExtMallocWithFlags(nullptr, 4096, hipMallocSignalMemory), + hipErrorInvalidValue); + } + + SECTION("size == 0") { + void* ptr; + HIP_CHECK_ERROR(hipExtMallocWithFlags(&ptr, 0, hipMallocSignalMemory), hipErrorInvalidValue); + } + + SECTION("size != 8") { + void* ptr; + HIP_CHECK_ERROR(hipExtMallocWithFlags(&ptr, 16, hipMallocSignalMemory), hipErrorInvalidValue); + } + } +} \ No newline at end of file diff --git a/projects/hip-tests/catch/unit/memory/hipMalloc.cc b/projects/hip-tests/catch/unit/memory/hipMalloc.cc new file mode 100644 index 0000000000..8692f5d59f --- /dev/null +++ b/projects/hip-tests/catch/unit/memory/hipMalloc.cc @@ -0,0 +1,59 @@ +/* +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 + +TEST_CASE("Unit_hipMalloc_Positive_Basic") { + constexpr size_t page_size = 4096; + void* ptr = nullptr; + const auto alloc_size = + GENERATE_COPY(10, page_size / 2, page_size, page_size * 3 / 2, page_size * 2); + HIP_CHECK(hipMalloc(&ptr, alloc_size)); + CHECK(ptr != nullptr); + CHECK(reinterpret_cast(ptr) % 256 == 0); + HIP_CHECK(hipFree(ptr)); +} + +TEST_CASE("Unit_hipMalloc_Positive_Zero_Size") { + void* ptr = reinterpret_cast(0x1); + HIP_CHECK(hipMalloc(&ptr, 0)); + REQUIRE(ptr == nullptr); +} + +TEST_CASE("Unit_hipMalloc_Positive_Alignment") { + void *ptr1 = nullptr, *ptr2 = nullptr; + HIP_CHECK(hipMalloc(&ptr1, 1)); + HIP_CHECK(hipMalloc(&ptr2, 10)); + CHECK(reinterpret_cast(ptr1) % 256 == 0); + CHECK(reinterpret_cast(ptr2) % 256 == 0); + HIP_CHECK(hipFree(ptr1)); + HIP_CHECK(hipFree(ptr2)); +} + +TEST_CASE("Unit_hipMalloc_Negative_Parameters") { + SECTION("ptr == nullptr") { HIP_CHECK_ERROR(hipMalloc(nullptr, 4096), hipErrorInvalidValue); } + SECTION("size == max size_t") { + void* ptr; + HIP_CHECK_ERROR(hipMalloc(&ptr, std::numeric_limits::max()), hipErrorOutOfMemory); + } +} \ No newline at end of file