Files
TheRock/rocm-systems/projects/hip-tests/catch/unit/memory/hipHostMalloc.cc
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

247 lines
8.4 KiB
C++

/*
* 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 <hip_test_checkers.hh>
#include <kernels.hh>
#include <hip_test_common.hh>
#include <hip_test_context.hh>
#include <hip_test_helper.hh>
#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<std::string> 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<void**>(&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<void**>(&B_h), SIZE, flag));
HIP_CHECK(hipHostMalloc(reinterpret_cast<void**>(&C_h), SIZE, hipHostMallocMapped));
HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast<void**>(&A_d), A_h, 0));
HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast<void**>(&C_d), C_h, 0));
HipTest::setDefaultData<float>(LEN, A_h, B_h, C_h);
HIP_CHECK(hipMalloc(reinterpret_cast<void**>(&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<float>(HipTest::vectorADD<float>, dimGrid, dimBlock, 0, 0,
static_cast<const float*>(A_d), static_cast<const float*>(B_d),
C_d, static_cast<size_t>(LEN));
HIP_CHECK(hipMemcpy(C_h, C_d, LEN * sizeof(float), hipMemcpyDeviceToHost));
HIP_CHECK(hipDeviceSynchronize());
HipTest::checkVectorADD<float>(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<void**>(&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<void**>(&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<void**>(&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<void**>(&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<void**>(&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<void**>(&host_ptr), sizeBytes), capture_error);
END_CAPTURE_SYNC(capture_error);
if (host_ptr != nullptr) {
HIP_CHECK(hipHostFree(host_ptr));
}
}