Files
TheRock/rocm-systems/projects/hip-tests/catch/unit/memory/hipHostGetFlags.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

223 lines
7.6 KiB
C++

/*
* Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
*
* SPDX-License-Identifier: MIT
*/
/*
This testcase verifies the basic scenario of hipHostGetFlags API
*/
#include <hip_test_common.hh>
#include <hip_test_checkers.hh>
#include <hip_test_kernels.hh>
#include <thread>
#include <vector>
std::vector<unsigned int> FlagPart1Vec{hipHostMallocDefault,
hipHostMallocDefault | hipHostMallocPortable,
hipHostMallocDefault | hipHostMallocMapped,
hipHostMallocDefault | hipHostMallocWriteCombined,
hipHostMallocPortable,
hipHostMallocPortable | hipHostMallocMapped,
hipHostMallocPortable | hipHostMallocWriteCombined,
hipHostMallocMapped,
hipHostMallocMapped | hipHostMallocWriteCombined,
hipHostMallocWriteCombined};
#if HT_AMD
// For cases where flags from FlagPart1Vec are not used,
// hipHostMallocDefault is the default on AMD
// and hipHostMallocMapped on Nvidia
std::vector<unsigned int> FlagPart2Vec{0x0,
hipHostMallocNumaUser,
hipHostMallocNumaUser | hipHostMallocCoherent,
hipHostMallocNumaUser | hipHostMallocNonCoherent,
hipHostMallocCoherent,
hipHostMallocNonCoherent};
#else
std::vector<unsigned int> FlagPart2Vec{0x0};
#endif
static constexpr auto LEN{1024 * 1024};
/*
This testcase verifies hipHostGetFlags API basic scenario
1. Allocates the memory using different flags
2. Gets the flags of the respective variable using
hipHostGetFlags API
3. Validates it with the initial flags used while allocating
memory
*/
/* Possible host flags
* hipHostMallocDefault 0x0
* hipHostMallocPortable 0x1
* hipHostMallocMapped 0x2
* hipHostMallocWriteCombined 0x4
* NOT on Nvidia
* hipHostMallocNumaUser 0x20000000
* hipHostMallocCoherent 0x40000000
* hipHostMallocNonCoherent 0x80000000
*/
inline void checkFlags(unsigned int expected, unsigned int obtained) {
// Account for cases where flags from FlagPart1Vec do not include hipHostMallocMapped,
// on Nvidia devices it is added by default
#if HT_NVIDIA
expected = expected | hipHostMallocMapped;
#endif
REQUIRE(expected == obtained);
}
HIP_TEST_CASE(Unit_hipHostGetFlags_flagCombos) {
constexpr auto SIZE{LEN * sizeof(int)};
int* A_h{nullptr};
const unsigned int FlagPart1 = GENERATE(from_range(FlagPart1Vec.begin(), FlagPart1Vec.end()));
const unsigned int FlagPart2 = GENERATE(from_range(FlagPart2Vec.begin(), FlagPart2Vec.end()));
unsigned int FlagComp = FlagPart1 | FlagPart2;
hipDeviceProp_t prop;
int device{};
HIP_CHECK(hipGetDevice(&device));
HIP_CHECK(hipGetDeviceProperties(&prop, device));
// Skip test if device does not support the property canMapHostMemory
if (prop.canMapHostMemory != 1) {
HIP_SKIP_TEST(HipTest::SkipReason::kHostPinnedMemoryUnsupported);
} else {
// Allocate using the generated flags combos
INFO("Flag passed when allocating: 0x" << std::hex << FlagComp << "\n");
HIP_CHECK(hipHostMalloc(reinterpret_cast<void**>(&A_h), SIZE, FlagComp));
unsigned int flagA{};
// get the flags from allocations and check if they are the same as the one set
HIP_CHECK(hipHostGetFlags(&flagA, A_h));
checkFlags(FlagComp, flagA);
HIP_CHECK(hipHostFree(A_h));
}
}
// Test Allocation with flags and getting flags in another thread
HIP_TEST_CASE(Unit_hipHostGetFlags_DifferentThreads) {
constexpr auto SIZE{LEN * sizeof(int)};
int* A_h{nullptr};
const unsigned int FlagPart1 = GENERATE(from_range(FlagPart1Vec.begin(), FlagPart1Vec.end()));
const unsigned int FlagPart2 = GENERATE(from_range(FlagPart2Vec.begin(), FlagPart2Vec.end()));
unsigned int FlagComp = FlagPart1 | FlagPart2;
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 {
// Make sure we allocate before trying to get the flags
std::thread malloc_thread(
[&]() { HIP_CHECK_THREAD(hipHostMalloc(reinterpret_cast<void**>(&A_h), SIZE, FlagComp)); });
malloc_thread.join();
HIP_CHECK_THREAD_FINALIZE();
unsigned int flagA{};
HIP_CHECK(hipHostGetFlags(&flagA, A_h));
checkFlags(FlagComp, flagA);
HIP_CHECK(hipHostFree(A_h));
}
}
// Test behaviour of hipHostGetFlags with invalid args
HIP_TEST_CASE(Unit_hipHostGetFlags_InvalidArgs) {
constexpr auto SIZE{LEN * sizeof(int)};
int* A_h{nullptr};
hipDeviceProp_t prop;
int device{};
HIP_CHECK(hipGetDevice(&device));
HIP_CHECK(hipGetDeviceProperties(&prop, device));
// Skip test if device does not support the property canMapHostMemory
if (prop.canMapHostMemory != 1) {
HIP_SKIP_TEST(HipTest::SkipReason::kHostPinnedMemoryUnsupported);
} else {
SECTION("Invalid flag ptr being passed to hipHostGetFlags") {
// Use default flag
unsigned int FlagComp = 0x0;
// Allocate using the generated flags combos
HIP_CHECK(hipHostMalloc(reinterpret_cast<void**>(&A_h), SIZE, FlagComp));
// use a nullptr to return flags to
unsigned int* flagA = nullptr;
// get the flags from allocations and check if they are the same as the one set
HIP_CHECK_ERROR(hipHostGetFlags(flagA, A_h), hipErrorInvalidValue);
HIP_CHECK(hipHostFree(A_h));
}
SECTION("Device ptr allocated with hipMalloc passed to hipHostGetFlags") {
unsigned int FlagComp = 0x4;
// Allocate memory on device
HIP_CHECK(hipMalloc(reinterpret_cast<void**>(&A_h), SIZE));
unsigned int flagA{};
// get the flags from allocations and check if they are the same as the one set
HIP_CHECK_ERROR(hipHostGetFlags(&flagA, A_h), hipErrorInvalidValue);
INFO("Flag passed when allocating: " << std::hex << FlagComp << " Returned flag: " << std::hex
<< flagA << "\n");
HIP_CHECK(hipFree(A_h));
}
SECTION("Ptr from hipHostGetDevicePointer passed to hipHostGetFlags") {
unsigned int FlagComp = 0x4;
int* A_d{nullptr};
// Allocate memory on device
HIP_CHECK(hipHostMalloc(reinterpret_cast<void**>(&A_h), SIZE, FlagComp));
HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast<void**>(&A_d), A_h, 0));
unsigned int flagA;
// get the flags from allocations and check if they are the same as the one set
HIP_CHECK(hipHostGetFlags(&flagA, A_d));
INFO("Flag passed when allocating: " << std::hex << FlagComp << " Returned flag: " << std::hex
<< flagA << "\n");
#if HT_NVIDIA
// on Nvidia adjust for cudaHostAllocMapped being set by default
FlagComp = FlagComp | hipHostMallocMapped;
#endif
REQUIRE(flagA == FlagComp);
HIP_CHECK(hipHostFree(A_h));
}
}
}
HIP_TEST_CASE(Unit_hipHostGetFlags_Capture) {
unsigned int host_flags = 0;
void* host_ptr = nullptr;
constexpr size_t kAllocSize = 1024;
HIP_CHECK(hipHostMalloc(reinterpret_cast<void**>(&host_ptr), kAllocSize));
hipStream_t stream = nullptr;
HIP_CHECK(hipStreamCreate(&stream));
GENERATE_CAPTURE();
BEGIN_CAPTURE(stream);
HIP_CHECK(hipHostGetFlags(&host_flags, host_ptr));
END_CAPTURE(stream);
HIP_CHECK(hipHostFree(host_ptr));
HIP_CHECK(hipStreamDestroy(stream));
}