/* * Copyright (c) Advanced Micro Devices, Inc., or its affiliates. * * SPDX-License-Identifier: MIT */ /* * Tests for hipDrvPointerGetAttributes API Functional Tests: 1. Pass multiple device related attributes in the attributes of hipDrvPointerGetAttributes API for the device pointer and check the behaviour 2. Pass device and host attributes in the attributes of hipDrvPointerGetAttributes API and validate the behaviour 3. Pass invalid pointer to hipDrvPointerGetAttributes API and validate the behaviour. Negative Tests: 1. Pass invalid numAttributes 2. Pass nullptr to attributes 3. Pass nullptr to data 4. Pass nullptr to device pointer */ #include static size_t Nbytes = 0; constexpr size_t N{1000000}; /* This testcase verifies Negative Scenarios of * hipDrvPointerGetAttributes API */ HIP_TEST_CASE(Unit_hipDrvPtrGetAttributes_Negative) { HIP_CHECK(hipSetDevice(0)); Nbytes = N * sizeof(int); int deviceId; int numDevices = 0; HIP_CHECK(hipGetDeviceCount(&numDevices)); int* A_d; HIP_CHECK(hipMalloc(&A_d, Nbytes)); HIP_CHECK(hipGetDevice(&deviceId)); unsigned int device_ordinal; int* dev_ptr{nullptr}; void* data[2]; data[0] = &dev_ptr; data[1] = &device_ordinal; hipPointer_attribute attributes[] = {HIP_POINTER_ATTRIBUTE_DEVICE_POINTER, HIP_POINTER_ATTRIBUTE_DEVICE_ORDINAL}; SECTION("Passing invalid number to numAttributes") { REQUIRE( hipDrvPointerGetAttributes(0, attributes, data, reinterpret_cast(A_d)) == hipErrorInvalidValue); } SECTION("Passing nullptr to attributes") { REQUIRE(hipDrvPointerGetAttributes(2, nullptr, data, reinterpret_cast(A_d)) == hipErrorInvalidValue); } SECTION("Passing nullptr to data") { REQUIRE( hipDrvPointerGetAttributes(2, attributes, nullptr, reinterpret_cast(A_d)) == hipErrorInvalidValue); } #if HT_AMD SECTION("Passing nullptr to device Pointer") { hipDeviceptr_t ptr = 0; REQUIRE(hipDrvPointerGetAttributes(2, attributes, data, ptr) == hipErrorInvalidValue); } #endif #if HT_NVIDIA SECTION("Passing invalid dependencies") { hipPointer_attribute attributes1[] = {HIP_POINTER_ATTRIBUTE_DEVICE_POINTER}; REQUIRE( hipDrvPointerGetAttributes(2, attributes1, data, reinterpret_cast(A_d)) == hipErrorInvalidValue); } #endif HIP_CHECK(hipFree(A_d)); } // Testcase verifies functional scenarios of hipDrvPointerGetAttributes API HIP_TEST_CASE(Unit_hipDrvPtrGetAttributes_Functional) { HIP_CHECK(hipSetDevice(0)); Nbytes = N * sizeof(int); int deviceId; int numDevices = 0; HIP_CHECK(hipGetDeviceCount(&numDevices)); int* A_d; HIP_CHECK(hipMalloc(&A_d, Nbytes)); HIP_CHECK(hipGetDevice(&deviceId)); SECTION("Passing device attributes to device pointer") { unsigned int memory_type; int device_ordinal; int* dev{nullptr}; int* dev_ptr{nullptr}; int* dev_ptr1{nullptr}; unsigned int range_size; int* start_addr{nullptr}; void* data[5]; data[0] = (&memory_type); data[1] = (&device_ordinal); data[2] = (&dev_ptr); data[3] = (&range_size); data[4] = (&start_addr); // Device memory hipPointer_attribute attributes[] = { HIP_POINTER_ATTRIBUTE_MEMORY_TYPE, HIP_POINTER_ATTRIBUTE_DEVICE_ORDINAL, HIP_POINTER_ATTRIBUTE_DEVICE_POINTER, HIP_POINTER_ATTRIBUTE_RANGE_SIZE, HIP_POINTER_ATTRIBUTE_RANGE_START_ADDR}; HIP_CHECK(hipPointerGetAttribute(&dev_ptr1, HIP_POINTER_ATTRIBUTE_DEVICE_POINTER, reinterpret_cast(A_d + 100))); HIP_CHECK(hipPointerGetAttribute(&dev, HIP_POINTER_ATTRIBUTE_DEVICE_POINTER, reinterpret_cast(A_d))); HIP_CHECK(hipDrvPointerGetAttributes(5, attributes, data, reinterpret_cast(A_d + 100))); REQUIRE(dev_ptr == dev_ptr1); REQUIRE(memory_type == hipMemoryTypeDevice); REQUIRE(device_ordinal == deviceId); REQUIRE(range_size == Nbytes); REQUIRE(start_addr == dev); } SECTION("Passing device and host attributes to device pointer") { int device_ordinal; int* host_ptr; void* data[2]; data[0] = (&host_ptr); data[1] = (&device_ordinal); hipPointer_attribute attributes[] = {HIP_POINTER_ATTRIBUTE_HOST_POINTER, HIP_POINTER_ATTRIBUTE_DEVICE_ORDINAL}; HIP_CHECK( hipDrvPointerGetAttributes(2, attributes, data, reinterpret_cast(A_d))); REQUIRE(host_ptr == nullptr); REQUIRE(device_ordinal == deviceId); } SECTION("Passing host related attributes to host pointer") { int device_ordinal; void* data[2]; int* host_ptr; int* pinned_host_ptr; data[0] = (&host_ptr); data[1] = (&device_ordinal); HIP_CHECK(hipHostMalloc(reinterpret_cast(&pinned_host_ptr), Nbytes, hipHostMallocDefault)); hipPointer_attribute attributes[] = {HIP_POINTER_ATTRIBUTE_HOST_POINTER, HIP_POINTER_ATTRIBUTE_DEVICE_ORDINAL}; HIP_CHECK(hipDrvPointerGetAttributes(2, attributes, data, reinterpret_cast(pinned_host_ptr))); REQUIRE(host_ptr == pinned_host_ptr); REQUIRE(device_ordinal == deviceId); HIP_CHECK(hipFreeHost(pinned_host_ptr)); } HIP_CHECK(hipFree(A_d)); }