/* * Copyright (c) Advanced Micro Devices, Inc., or its affiliates. * * SPDX-License-Identifier: MIT */ /* This testfile verifies the following scenarios of hipHostMalloc API 1. Basic scenario of hipHostMalloc API 2. Negative Scenarios of hipHostMalloc API 3. Allocating memory using hipHostMalloc with Coherent flag 4. Allocating memory using hipHostMalloc with NonCoherent flag 5. Allocating memory using hipHostMalloc with default flag */ #include #include #include #include #include #define SYNC_EVENT 0 #define SYNC_STREAM 1 #define SYNC_DEVICE 2 #define MEMORY_PERCENT 5 #define BLOCK_SIZE 512 #define VALUE 32 std::vector syncMsg = {"event", "stream", "device"}; static constexpr int numElements{1024 * 16}; static constexpr size_t sizeBytes{numElements * sizeof(int)}; void CheckHostPointer(int numElements, int* ptr, unsigned eventFlags, int syncMethod, std::string msg) { std::cerr << "test: CheckHostPointer " << msg << " eventFlags = " << std::hex << eventFlags << ((eventFlags & hipEventReleaseToDevice) ? " hipEventReleaseToDevice" : "") << ((eventFlags & hipEventReleaseToSystem) ? " hipEventReleaseToSystem" : "") << " ptr=" << ptr << " syncMethod=" << syncMsg[syncMethod] << "\n"; hipStream_t s; hipEvent_t e; // Init: HIP_CHECK(hipStreamCreate(&s)); HIP_CHECK(hipEventCreateWithFlags(&e, eventFlags)) dim3 dimBlock(64, 1, 1); dim3 dimGrid(numElements / dimBlock.x, 1, 1); const int expected = 13; // Init array to know state: HipTest::launchKernel(Set, dimGrid, dimBlock, 0, 0x0, ptr, -42); HIP_CHECK(hipDeviceSynchronize()); HipTest::launchKernel(Set, dimGrid, dimBlock, 0, s, ptr, expected); HIP_CHECK(hipEventRecord(e, s)); // Host waits for event : switch (syncMethod) { case SYNC_EVENT: HIP_CHECK(hipEventSynchronize(e)); break; case SYNC_STREAM: HIP_CHECK(hipStreamSynchronize(s)); break; case SYNC_DEVICE: HIP_CHECK(hipDeviceSynchronize()); break; default: assert(0); } for (int i = 0; i < numElements; i++) { if (ptr[i] != expected) { printf("mismatch at %d: %d != %d\n", i, ptr[i], expected); REQUIRE(ptr[i] == expected); } } HIP_CHECK(hipStreamDestroy(s)); HIP_CHECK(hipEventDestroy(e)); } /* This testcase performs the basic scenario of hipHostMalloc API Allocates the memory using hipHostMalloc API Launches the kernel and performs vector addition. validates thes result. */ HIP_TEST_CASE(Unit_hipHostMalloc_Basic) { static constexpr auto LEN{1024 * 1024}; static constexpr auto SIZE{LEN * sizeof(float)}; hipDeviceProp_t prop; int device; HIP_CHECK(hipGetDevice(&device)); HIP_CHECK(hipGetDeviceProperties(&prop, device)); if (prop.canMapHostMemory != 1) { HIP_SKIP_TEST(HipTest::SkipReason::kHostPinnedMemoryUnsupported); } else { float *A_h, *B_h, *C_h; float *A_d, *B_d, *C_d; unsigned int flag = 0; HIP_CHECK(hipHostMalloc(reinterpret_cast(&A_h), SIZE, hipHostMallocWriteCombined | hipHostMallocMapped)); SECTION("hipHostMallocDefault") { flag = hipHostMallocDefault; } #if (HT_AMD == 1) && (HT_LINUX == 1) if (!IsNavi4X()) { SECTION("hipHostMallocUncached") { flag = hipHostMallocUncached; } } SECTION("hipHostMallocCoherent") { flag = hipHostMallocCoherent; } SECTION("hipHostMallocNonCoherent") { flag = hipHostMallocNonCoherent; } #endif HIP_CHECK(hipHostMalloc(reinterpret_cast(&B_h), SIZE, flag)); HIP_CHECK(hipHostMalloc(reinterpret_cast(&C_h), SIZE, hipHostMallocMapped)); HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast(&A_d), A_h, 0)); HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast(&C_d), C_h, 0)); HipTest::setDefaultData(LEN, A_h, B_h, C_h); HIP_CHECK(hipMalloc(reinterpret_cast(&B_d), SIZE)); HIP_CHECK(hipMemcpy(B_d, B_h, SIZE, hipMemcpyHostToDevice)); dim3 dimGrid(LEN / 512, 1, 1); dim3 dimBlock(512, 1, 1); HipTest::launchKernel(HipTest::vectorADD, dimGrid, dimBlock, 0, 0, static_cast(A_d), static_cast(B_d), C_d, static_cast(LEN)); HIP_CHECK(hipMemcpy(C_h, C_d, LEN * sizeof(float), hipMemcpyDeviceToHost)); HIP_CHECK(hipDeviceSynchronize()); HipTest::checkVectorADD(A_h, B_h, C_h, numElements); HIP_CHECK(hipHostFree(A_h)); HIP_CHECK(hipHostFree(B_h)); HIP_CHECK(hipHostFree(C_h)); HIP_CHECK(hipFree(B_d)); } } /* This testcase verifies the hipHostMalloc API by passing nullptr to the pointer variable */ HIP_TEST_CASE(Unit_hipHostMalloc_Negative) { #if HT_AMD { // Stimulate error condition: int* A = nullptr; REQUIRE(hipHostMalloc(reinterpret_cast(&A), sizeBytes, hipHostMallocCoherent | hipHostMallocNonCoherent) != hipSuccess); REQUIRE(A == nullptr); } #endif } /* This testcase verifies the hipHostMalloc API by 1.Allocating memory using noncoherent flag 2. Launches the kernel and modifies the variable using different synchronization techniquies 3. validates the result. */ HIP_TEST_CASE(Unit_hipHostMalloc_NonCoherent) { int* A = nullptr; HIP_CHECK(hipHostMalloc(reinterpret_cast(&A), sizeBytes, hipHostMallocNonCoherent)); const char* ptrType = "non-coherent"; CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_DEVICE, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_STREAM, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_EVENT, ptrType); HIP_CHECK(hipFreeHost(A)); } /* This testcase verifies the hipHostMalloc API by 1.Allocating memory using coherent flag 2. Launches the kernel and modifies the variable using different synchronization techniquies 3. validates the result. */ HIP_TEST_CASE(Unit_hipHostMalloc_Coherent) { int* A = nullptr; if (hipHostMalloc(reinterpret_cast(&A), sizeBytes, hipHostMallocCoherent) == hipSuccess) { const char* ptrType = "coherent"; CheckHostPointer(numElements, A, hipEventReleaseToDevice, SYNC_DEVICE, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToDevice, SYNC_STREAM, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToDevice, SYNC_EVENT, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_DEVICE, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_STREAM, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_EVENT, ptrType); HIP_CHECK(hipFreeHost(A)); } else { HIP_SKIP_TEST(HipTest::SkipReason::kCoherentHostAllocFailed); } } /* This testcase verifies the hipHostMalloc API by 1.Allocating memory using default flag 2. Launches the kernel and modifies the variable using different synchronization techniquies 3. validates the result. */ HIP_TEST_CASE(Unit_hipHostMalloc_Default) { int* A = nullptr; HIP_CHECK(hipHostMalloc(reinterpret_cast(&A), sizeBytes)); const char* ptrType = "default"; CheckHostPointer(numElements, A, 0, SYNC_DEVICE, ptrType); CheckHostPointer(numElements, A, 0, SYNC_STREAM, ptrType); CheckHostPointer(numElements, A, 0, SYNC_EVENT, ptrType); HIP_CHECK(hipFreeHost(A)); } /* This testcase verifies the hipHostMalloc API by 1. Allocating more memory than total system RAM. Should return hipErrorOutOfMemory. */ HIP_TEST_CASE(Unit_hipHostMalloc_AllocateMoreThanTotalSystemMemory) { char* host_ptr = nullptr; const size_t total_ram_mb = HipTest::getTotalSystemMemoryInMB(); if (total_ram_mb == 0) { HIP_SKIP_TEST("total system memory could not be queried."); } const size_t total_ram_bytes = total_ram_mb * 1024 * 1024; const size_t allocsize = total_ram_bytes + ((total_ram_bytes * MEMORY_PERCENT) / 100); HIP_CHECK_ERROR(hipHostMalloc(reinterpret_cast(&host_ptr), allocsize), hipErrorOutOfMemory); REQUIRE(host_ptr == nullptr); } HIP_TEST_CASE(Unit_hipHostMalloc_Capture) { int* host_ptr = nullptr; hipError_t capture_error = hipSuccess; constexpr bool kRelaxedModeAllowed = true; BEGIN_CAPTURE_SYNC(capture_error, kRelaxedModeAllowed); HIP_CHECK_ERROR(hipHostMalloc(reinterpret_cast(&host_ptr), sizeBytes), capture_error); END_CAPTURE_SYNC(capture_error); if (host_ptr != nullptr) { HIP_CHECK(hipHostFree(host_ptr)); } }