diff --git a/catch/unit/graph/CMakeLists.txt b/catch/unit/graph/CMakeLists.txt index d9a529fa1b..03d2927ffc 100644 --- a/catch/unit/graph/CMakeLists.txt +++ b/catch/unit/graph/CMakeLists.txt @@ -141,6 +141,7 @@ set(TEST_SRC hipGraphKernelNodeSetAttribute.cc hipGraphMemAllocNodeGetParams.cc hipDrvGraphAddMemcpyNode.cc + hipGraphAddMemAllocNode.cc ) add_custom_target(add_Kernel.code COMMAND ${CMAKE_CXX_COMPILER} --genco ${OFFLOAD_ARCH_STR} ${CMAKE_CURRENT_SOURCE_DIR}/add_Kernel.cpp -o ${CMAKE_CURRENT_BINARY_DIR}/../graph/add_Kernel.code -I${HIP_PATH}/include/ -I${CMAKE_CURRENT_SOURCE_DIR}/../../include --rocm-path=${ROCM_PATH}) diff --git a/catch/unit/graph/hipGraphAddMemAllocNode.cc b/catch/unit/graph/hipGraphAddMemAllocNode.cc new file mode 100644 index 0000000000..4c99e25473 --- /dev/null +++ b/catch/unit/graph/hipGraphAddMemAllocNode.cc @@ -0,0 +1,458 @@ +/* +Copyright (c) 2023 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 + +/** + * @addtogroup hipGraphAddMemAllocNode hipGraphAddMemAllocNode + * @{ + * @ingroup GraphTest + * `hipGraphAddMemAllocNode (hipGraphNode_t *pGraphNode, hipGraph_t graph, const hipGraphNode_t + * *pDependencies, size_t numDependencies, hipMemAllocNodeParams *pNodeParams)` - + * Creates a memory allocation node and adds it to a graph. + */ + +static constexpr auto element_count{512 * 1024 * 1024}; + +__global__ void validateGPU(int* const vec, const int value, size_t N, unsigned int* mismatch) { + int idx = blockIdx.x * blockDim.x + threadIdx.x; + + if (idx < N) { + if (vec[idx] != value) { + atomicAdd(mismatch, 1); + } + } +} + +/** + * Test Description + * ------------------------ + * - Test to verify hipGraphAddMemAllocNode behavior with invalid arguments: + * -# Null graph node + * -# Null graph node + * -# Invalid numDependencies for null list of dependencies + * -# Invalid numDependencies and valid list for dependencies + * -# Null alloc params + * -# Invalid poolProps alloc type + * -# Invalid poolProps location type + * -# Invalid poolProps location id + * -# Bytesize is max size_t + * -# Invalid accessDescCount + * Test source + * ------------------------ + * - /unit/graph/hipGraphAddMemAllocNode.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 6.0 + */ +TEST_CASE("Unit_hipGraphAddMemAllocNode_Negative_Params") { + constexpr size_t N = 1024; + hipGraph_t graph; + hipGraphNode_t alloc_node; + std::vector dependencies; + + HIP_CHECK(hipGraphCreate(&graph, 0)); + + int num_dev = 0; + HIP_CHECK(hipGetDeviceCount(&num_dev)); + + hipMemAccessDesc desc; + memset(&desc, 0, sizeof(hipMemAccessDesc)); + desc.location.type = hipMemLocationTypeDevice; + desc.location.id = 0; + desc.flags = hipMemAccessFlagsProtReadWrite; + + hipMemAllocNodeParams alloc_param; + memset(&alloc_param, 0, sizeof(alloc_param)); + alloc_param.bytesize = N; + alloc_param.poolProps.allocType = hipMemAllocationTypePinned; + alloc_param.poolProps.location.id = 0; + alloc_param.poolProps.location.type = hipMemLocationTypeDevice; + alloc_param.accessDescs = &desc; + alloc_param.accessDescCount = 1; + + SECTION("Passing nullptr to graph node") { + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(nullptr, graph, nullptr, 0, &alloc_param), + hipErrorInvalidValue); + } + + SECTION("Passing nullptr to graph") { + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, nullptr, nullptr, 0, &alloc_param), + hipErrorInvalidValue); + } + + SECTION("Pass invalid numDependencies") { + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 11, &alloc_param), + hipErrorInvalidValue); + } + + SECTION("Pass invalid numDependencies and valid list for dependencies") { + HIP_CHECK(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param)); + dependencies.push_back(alloc_node); + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, dependencies.data(), + dependencies.size() + 1, &alloc_param), + hipErrorInvalidValue); + } + + SECTION("Passing nullptr to alloc params") { + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, nullptr), + hipErrorInvalidValue); + } + + SECTION("Passing invalid poolProps alloc type") { + alloc_param.poolProps.allocType = hipMemAllocationTypeInvalid; + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param), + hipErrorInvalidValue); + alloc_param.poolProps.allocType = hipMemAllocationTypePinned; + } + + SECTION("Passing invalid poolProps location type") { + alloc_param.poolProps.location.type = hipMemLocationTypeInvalid; + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param), + hipErrorInvalidValue); + alloc_param.poolProps.location.type = hipMemLocationTypeDevice; + } + + SECTION("Passing invalid poolProps location id") { + alloc_param.poolProps.location.id = num_dev; + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param), + hipErrorInvalidValue); + alloc_param.poolProps.location.id = 0; + } + +#if HT_NVIDIA //EXSWHTEC-353 + SECTION("Passing max size_t bytesize") { + alloc_param.bytesize = std::numeric_limits::max(); + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param), + hipErrorOutOfMemory); + alloc_param.bytesize = N; + } + + SECTION("Passing invalid accessDescCount") { + alloc_param.accessDescCount = num_dev + 1; + HIP_CHECK_ERROR(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param), + hipErrorInvalidValue); + alloc_param.accessDescCount = 0; + } +#endif + + HIP_CHECK(hipGraphDestroy(graph)); +} + +/** + * Test Description + * ------------------------ + * - Test to verify hipGraphAddMemAllocNode unsupported behavior: + * -# More than one instantiation of the graph exist at the same time + * -# Clone graph with mem alloc node + * -# Use graph with mem alloc node in a child node + * -# Delete edge of the graph with mem alloc node + * Test source + * ------------------------ + * - /unit/graph/hipGraphAddMemAllocNode.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 6.0 + */ +TEST_CASE("Unit_hipGraphAddMemAllocNode_Negative_NotSupported") { + constexpr size_t N = 1024; + hipGraph_t graph; + hipGraphNode_t alloc_node; + + HIP_CHECK(hipGraphCreate(&graph, 0)); + + hipMemAllocNodeParams alloc_param; + memset(&alloc_param, 0, sizeof(alloc_param)); + alloc_param.bytesize = N; + alloc_param.poolProps.allocType = hipMemAllocationTypePinned; + alloc_param.poolProps.location.id = 0; + alloc_param.poolProps.location.type = hipMemLocationTypeDevice; + + HIP_CHECK(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param)); + + SECTION("More than one instantation of the graph exists") { + hipGraphExec_t graph_exec1, graph_exec2; + HIP_CHECK(hipGraphInstantiate(&graph_exec1, graph, nullptr, nullptr, 0)); + HIP_CHECK_ERROR(hipGraphInstantiate(&graph_exec2, graph, nullptr, nullptr, 0), + hipErrorNotSupported); + HIP_CHECK(hipGraphExecDestroy(graph_exec1)); + } + +#if HT_NVIDIA //EXSWHTEC-353 + SECTION("Clone graph with mem alloc node") { + hipGraph_t cloned_graph; + HIP_CHECK_ERROR(hipGraphClone(&cloned_graph, graph), hipErrorNotSupported); + } + + SECTION("Use graph in a child node") { + hipGraph_t parent_graph; + HIP_CHECK(hipGraphCreate(&parent_graph, 0)); + hipGraphNode_t child_graph_node; + HIP_CHECK_ERROR(hipGraphAddChildGraphNode(&child_graph_node, parent_graph, nullptr, 0, graph), + hipErrorNotSupported); + HIP_CHECK(hipGraphDestroy(parent_graph)); + } + + SECTION("Delete edge of the graph") { + hipGraphNode_t empty_node; + HIP_CHECK(hipGraphAddEmptyNode(&empty_node, graph, &alloc_node, 1)); + HIP_CHECK_ERROR(hipGraphRemoveDependencies(graph, &alloc_node, &empty_node, 1), + hipErrorNotSupported); + } +#endif + + HIP_CHECK(hipGraphDestroy(graph)); +} + +/* Create graph with memory nodes that copies memset data to host array */ +static void createGraph(hipGraphExec_t* graph_exec, int* A_h, int fill_value, + int** device_alloc = nullptr) { + constexpr size_t num_bytes = element_count * sizeof(int); + + hipGraph_t graph; + HIP_CHECK(hipGraphCreate(&graph, 0)); + + hipGraphNode_t alloc_node; + hipMemAllocNodeParams alloc_param; + memset(&alloc_param, 0, sizeof(alloc_param)); + alloc_param.bytesize = num_bytes; + alloc_param.poolProps.allocType = hipMemAllocationTypePinned; + alloc_param.poolProps.location.id = 0; + alloc_param.poolProps.location.type = hipMemLocationTypeDevice; + + HIP_CHECK(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param)); + REQUIRE(alloc_param.dptr != nullptr); + int* A_d = reinterpret_cast(alloc_param.dptr); + + hipGraphNode_t memset_node; + hipMemsetParams memset_params{}; + memset(&memset_params, 0, sizeof(memset_params)); + memset_params.dst = reinterpret_cast(A_d); + memset_params.value = fill_value; + memset_params.pitch = 0; + memset_params.elementSize = sizeof(int); + memset_params.width = element_count; + memset_params.height = 1; + HIP_CHECK(hipGraphAddMemsetNode(&memset_node, graph, &alloc_node, 1, &memset_params)); + + hipGraphNode_t memcpy_node; + HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpy_node, graph, &memset_node, 1, A_h, A_d, num_bytes, + hipMemcpyDeviceToHost)); + + if (device_alloc == nullptr) { + hipGraphNode_t free_node; + HIP_CHECK(hipGraphAddMemFreeNode(&free_node, graph, &memcpy_node, 1, (void*)A_d)); + } else { + *device_alloc = A_d; + } + + // Instantiate graph + HIP_CHECK(hipGraphInstantiate(graph_exec, graph, nullptr, nullptr, 0)); + + HIP_CHECK(hipGraphDestroy(graph)); +} + +static void createFreeGraph(hipGraphExec_t* graph_exec, int* device_alloc) { + hipGraph_t graph; + hipGraphNode_t free_node; + HIP_CHECK(hipGraphCreate(&graph, 0)); + + HIP_CHECK(hipGraphAddMemFreeNode(&free_node, graph, nullptr, 0, (void*)device_alloc)); + + // Instantiate graph + HIP_CHECK(hipGraphInstantiate(graph_exec, graph, nullptr, nullptr, 0)); + + HIP_CHECK(hipGraphDestroy(graph)); +} + +/** + * Test Description + * ------------------------ + * - Test to verify hipGraphAddMemAllocNode allocates memory correctly and graph behaves as + * expected when free node is added to the same graph. + * Test source + * ------------------------ + * - /unit/graph/hipGraphAddMemAllocNode.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 6.0 + */ +TEST_CASE("Unit_hipGraphAddMemAllocNode_Positive_FreeInGraph") { + hipGraphExec_t graph_exec; + + LinearAllocGuard host_alloc = + LinearAllocGuard(LinearAllocs::malloc, element_count * sizeof(int)); + + StreamGuard stream_guard(Streams::created); + hipStream_t stream = stream_guard.stream(); + + constexpr int fill_value = 11; + createGraph(&graph_exec, host_alloc.ptr(), fill_value); + HIP_CHECK(hipGraphLaunch(graph_exec, stream)); + HIP_CHECK(hipStreamSynchronize(stream)); + + ArrayFindIfNot(host_alloc.host_ptr(), fill_value, element_count); + + HIP_CHECK(hipGraphExecDestroy(graph_exec)); +} + +/** + * Test Description + * ------------------------ + * - Test to verify hipGraphAddMemAllocNode allocates memory correctly, graph behaves as expected + * and allocated memory can can be accessed by outside the graph before memory is freed outside the + * stream. + * Test source + * ------------------------ + * - /unit/graph/hipGraphAddMemAllocNode.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 6.0 + */ +TEST_CASE("Unit_hipGraphAddMemAllocNode_Positive_FreeOutsideStream") { + hipGraphExec_t graph_exec; + + LinearAllocGuard host_alloc(LinearAllocs::malloc, element_count * sizeof(int)); + LinearAllocGuard mismatch_count_h = + LinearAllocGuard(LinearAllocs::malloc, sizeof(unsigned int)); + LinearAllocGuard mismatch_count_d = + LinearAllocGuard(LinearAllocs::hipMalloc, sizeof(unsigned int)); + HIP_CHECK(hipMemset(mismatch_count_d.ptr(), 0, sizeof(unsigned int))); + int* dev_p; + + StreamGuard stream_guard(Streams::created); + hipStream_t stream = stream_guard.stream(); + + constexpr auto thread_count = 1024; + const auto block_count = element_count / thread_count + 1; + constexpr int fill_value = 12; + + createGraph(&graph_exec, host_alloc.ptr(), fill_value, &dev_p); + HIP_CHECK(hipGraphLaunch(graph_exec, stream)); + validateGPU<<>>(dev_p, fill_value, element_count, + mismatch_count_d.ptr()); + // Since hipFree is synchronous, the stream must synchronize before freeing dev_p + HIP_CHECK(hipStreamSynchronize(stream)); + HIP_CHECK(hipFree(dev_p)); + + HIP_CHECK(hipMemcpy(mismatch_count_h.host_ptr(), mismatch_count_d.ptr(), sizeof(unsigned int), + hipMemcpyDeviceToHost)); + REQUIRE(mismatch_count_h.host_ptr()[0] == 0); + ArrayFindIfNot(host_alloc.host_ptr(), fill_value, element_count); + + HIP_CHECK(hipGraphExecDestroy(graph_exec)); +} + +/** + * Test Description + * ------------------------ + * - Test to verify hipGraphAddMemAllocNode allocates memory correctly, graph behaves as expected + * and allocated memory can can be accessed by outside the graph before memory is freed. + * Test source + * ------------------------ + * - /unit/graph/hipGraphAddMemAllocNode.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 6.0 + */ +TEST_CASE("Unit_hipGraphAddMemAllocNode_Positive_FreeOutsideGraph") { + hipGraphExec_t graph_exec; + + LinearAllocGuard host_alloc(LinearAllocs::malloc, element_count * sizeof(int)); + LinearAllocGuard mismatch_count_h = + LinearAllocGuard(LinearAllocs::malloc, sizeof(unsigned int)); + LinearAllocGuard mismatch_count_d = + LinearAllocGuard(LinearAllocs::hipMalloc, sizeof(unsigned int)); + HIP_CHECK(hipMemset(mismatch_count_d.ptr(), 0, sizeof(unsigned int))); + int* dev_p; + + StreamGuard stream_guard(Streams::created); + hipStream_t stream = stream_guard.stream(); + + constexpr auto thread_count = 1024; + const auto block_count = element_count / thread_count + 1; + constexpr int fill_value = 13; + + createGraph(&graph_exec, host_alloc.ptr(), fill_value, &dev_p); + HIP_CHECK(hipGraphLaunch(graph_exec, stream)); + validateGPU<<>>(dev_p, fill_value, element_count, + mismatch_count_d.ptr()); + HIP_CHECK(hipFreeAsync(dev_p, stream)); + HIP_CHECK(hipStreamSynchronize(stream)); + + HIP_CHECK(hipMemcpy(mismatch_count_h.host_ptr(), mismatch_count_d.ptr(), sizeof(unsigned int), + hipMemcpyDeviceToHost)); + REQUIRE(mismatch_count_h.host_ptr()[0] == 0); + ArrayFindIfNot(host_alloc.host_ptr(), fill_value, element_count); + + HIP_CHECK(hipGraphExecDestroy(graph_exec)); +} + +/** + * Test Description + * ------------------------ + * - Test to verify hipGraphAddMemAllocNode allocates memory correctly, graph behaves as expected + * and allocated memory can can be accessed by outside the graph before memory is freed in a + * different graph. + * Test source + * ------------------------ + * - /unit/graph/hipGraphAddMemAllocNode.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 6.0 + */ +TEST_CASE("Unit_hipGraphAddMemAllocNode_Positive_FreeSeparateGraph") { + hipGraphExec_t graph_exec1, graph_exec2; + + LinearAllocGuard host_alloc(LinearAllocs::malloc, element_count * sizeof(int)); + LinearAllocGuard mismatch_count_h = + LinearAllocGuard(LinearAllocs::malloc, sizeof(unsigned int)); + LinearAllocGuard mismatch_count_d = + LinearAllocGuard(LinearAllocs::hipMalloc, sizeof(unsigned int)); + HIP_CHECK(hipMemset(mismatch_count_d.ptr(), 0, sizeof(unsigned int))); + int* dev_p; + + StreamGuard stream_guard(Streams::created); + hipStream_t stream = stream_guard.stream(); + + constexpr auto thread_count = 1024; + const auto block_count = element_count / thread_count + 1; + constexpr int fill_value = 13; + + createGraph(&graph_exec1, host_alloc.ptr(), fill_value, &dev_p); + createFreeGraph(&graph_exec2, dev_p); + HIP_CHECK(hipGraphLaunch(graph_exec1, stream)); + validateGPU<<>>(dev_p, fill_value, element_count, + mismatch_count_d.ptr()); + HIP_CHECK(hipGraphLaunch(graph_exec2, stream)); + HIP_CHECK(hipStreamSynchronize(stream)); + + HIP_CHECK(hipMemcpy(mismatch_count_h.host_ptr(), mismatch_count_d.ptr(), sizeof(unsigned int), + hipMemcpyDeviceToHost)); + REQUIRE(mismatch_count_h.host_ptr()[0] == 0); + ArrayFindIfNot(host_alloc.host_ptr(), fill_value, element_count); + + HIP_CHECK(hipGraphExecDestroy(graph_exec1)); + HIP_CHECK(hipGraphExecDestroy(graph_exec2)); +} diff --git a/catch/unit/graph/hipGraphMemAllocNodeGetParams.cc b/catch/unit/graph/hipGraphMemAllocNodeGetParams.cc index b8e6e42661..99e341a77d 100644 --- a/catch/unit/graph/hipGraphMemAllocNodeGetParams.cc +++ b/catch/unit/graph/hipGraphMemAllocNodeGetParams.cc @@ -18,47 +18,44 @@ THE SOFTWARE. */ /** -* @addtogroup hipGraphMemAllocNodeGetParams hipGraphMemAllocNodeGetParams -* @{ -* @ingroup GraphTest -* `hipGraphMemAllocNodeGetParams(hipGraphNode_t node, hipMemAllocNodeParams* params_out)` -* Returns a memory alloc node's parameters. -* `hipGraphMemFreeNodeGetParams(hipGraphNode_t node, void* dptr_out)` - -* Returns a memory free node's parameters. -*/ + * @addtogroup hipGraphMemAllocNodeGetParams hipGraphMemAllocNodeGetParams + * @{ + * @ingroup GraphTest + * `hipGraphMemAllocNodeGetParams(hipGraphNode_t node, hipMemAllocNodeParams* params_out)` + * Returns a memory alloc node's parameters. + * `hipGraphMemFreeNodeGetParams(hipGraphNode_t node, void* dptr_out)` - + * Returns a memory free node's parameters. + */ #include #include #include +#include +#include /** -* Test Description -* ------------------------ -*  - Functional Test for API - hipGraphMemAllocNodeGetParams -* Create a graph and add a node with hipGraphAddMemAllocNode -* and hipGraphAddMemFreeNode and launch it. -* 1) Get alloc node by calling hipGraphMemAllocNodeGetParams and Validate. -* 2) Get Free Node ptr by calling hipGraphMemFreeNodeGetParams and Validate. -* 3) Check for multiple devices case. -* 4) Allocate multiple alloc node and validate by calling its get param. -* Test source -* ------------------------ -*  - /unit/graph/hipGraphMemAllocNodeGetParams.cc -* Test requirements -* ------------------------ -*  - HIP_VERSION >= 6.0 -*/ + * Test Description + * ------------------------ + *  - Functional Test for API - hipGraphMemAllocNodeGetParams + * Create a graph and add a node with hipGraphAddMemAllocNode + * and hipGraphAddMemFreeNode and launch it. + * 1) Get alloc node by calling hipGraphMemAllocNodeGetParams and Validate. + * 2) Get Free Node ptr by calling hipGraphMemFreeNodeGetParams and Validate. + * 3) Check for multiple devices case. + * 4) Allocate multiple alloc node and validate by calling its get param. + * Test source + * ------------------------ + *  - /unit/graph/hipGraphMemAllocNodeGetParams.cc + * Test requirements + * ------------------------ + *  - HIP_VERSION >= 6.0 + */ -static bool validateAllocParam(hipMemAllocNodeParams in, - hipMemAllocNodeParams out) { - if (in.bytesize != out.bytesize) - return false; - if (in.poolProps.allocType != out.poolProps.allocType) - return false; - if (in.poolProps.location.id != out.poolProps.location.id) - return false; - if (in.poolProps.location.type != out.poolProps.location.type) - return false; +static bool validateAllocParam(hipMemAllocNodeParams in, hipMemAllocNodeParams out) { + if (in.bytesize != out.bytesize) return false; + if (in.poolProps.allocType != out.poolProps.allocType) return false; + if (in.poolProps.location.id != out.poolProps.location.id) return false; + if (in.poolProps.location.type != out.poolProps.location.type) return false; return true; } @@ -85,7 +82,7 @@ static void hipGraphMemAllocNodeGetParams_Functional(unsigned deviceId = 0) { params_in.poolProps.location.type = hipMemLocationTypeDevice; HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, ¶ms_in)); - int *A_d = reinterpret_cast(params_in.dptr); + int* A_d = reinterpret_cast(params_in.dptr); REQUIRE(A_d != nullptr); HIP_CHECK(hipGraphAddMemFreeNode(&freeNodeA, graph, &allocNodeA, 1, A_d)); @@ -105,21 +102,21 @@ static void hipGraphMemAllocNodeGetParams_Functional(unsigned deviceId = 0) { } /** -* Test Description -* ------------------------ -*  - Functional Test for API - hipGraphMemAllocNodeGetParams -* Create a graph and add a node with hipGraphAddMemAllocNode -* and hipGraphAddMemFreeNode and launch it. -* 1) Get alloc node by calling hipGraphMemAllocNodeGetParams and Validate it. -* 2) Get Free node ptr by calling hipGraphMemFreeNodeGetParams and Validate it. -* 3) Check for multiple devices case. -* Test source -* ------------------------ -*  - /unit/graph/hipGraphMemAllocNodeGetParams.cc -* Test requirements -* ------------------------ -*  - HIP_VERSION >= 6.0 -*/ + * Test Description + * ------------------------ + *  - Functional Test for API - hipGraphMemAllocNodeGetParams + * Create a graph and add a node with hipGraphAddMemAllocNode + * and hipGraphAddMemFreeNode and launch it. + * 1) Get alloc node by calling hipGraphMemAllocNodeGetParams and Validate it. + * 2) Get Free node ptr by calling hipGraphMemFreeNodeGetParams and Validate it. + * 3) Check for multiple devices case. + * Test source + * ------------------------ + *  - /unit/graph/hipGraphMemAllocNodeGetParams.cc + * Test requirements + * ------------------------ + *  - HIP_VERSION >= 6.0 + */ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional") { hipGraphMemAllocNodeGetParams_Functional(); @@ -130,7 +127,7 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_MultiDevice") { HIP_CHECK(hipGetDeviceCount(&numDevices)); if (numDevices > 0) { - for ( int i = 0; i < numDevices; ++i ) { + for (int i = 0; i < numDevices; ++i) { hipGraphMemAllocNodeGetParams_Functional(i); } } else { @@ -139,19 +136,19 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_MultiDevice") { } /** -* Test Description -* ------------------------ -*  - Functional Test for API - hipGraphMemAllocNodeGetParams -* Create a graph and add multiple node with hipGraphAddMemAllocNode -* and hipGraphAddMemFreeNode and launch it. -* 1) Allocate multiple alloc node and validate by calling its get param. -* Test source -* ------------------------ -*  - /unit/graph/hipGraphMemAllocNodeGetParams.cc -* Test requirements -* ------------------------ -*  - HIP_VERSION >= 6.0 -*/ + * Test Description + * ------------------------ + *  - Functional Test for API - hipGraphMemAllocNodeGetParams + * Create a graph and add multiple node with hipGraphAddMemAllocNode + * and hipGraphAddMemFreeNode and launch it. + * 1) Allocate multiple alloc node and validate by calling its get param. + * Test source + * ------------------------ + *  - /unit/graph/hipGraphMemAllocNodeGetParams.cc + * Test requirements + * ------------------------ + *  - HIP_VERSION >= 6.0 + */ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_2") { constexpr size_t N = 1024 * 1024; @@ -173,8 +170,7 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_2") { int *A_d, *B_d, *C_d; int *A_h, *B_h, *C_h; - HipTest::initArrays(nullptr, nullptr, nullptr, - &A_h, &B_h, &C_h, N, false); + HipTest::initArrays(nullptr, nullptr, nullptr, &A_h, &B_h, &C_h, N, false); HIP_CHECK(hipGraphCreate(&graph, 0)); HIP_CHECK(hipStreamCreate(&stream)); @@ -187,49 +183,46 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_2") { HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, ¶ms_in)); REQUIRE(params_in.dptr != nullptr); - A_d = reinterpret_cast(params_in.dptr); - HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeB, graph, - &allocNodeA, 1, ¶ms_in)); + A_d = reinterpret_cast(params_in.dptr); + HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeB, graph, &allocNodeA, 1, ¶ms_in)); REQUIRE(params_in.dptr != nullptr); - B_d = reinterpret_cast(params_in.dptr); - HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeC, graph, - &allocNodeB, 1, ¶ms_in)); + B_d = reinterpret_cast(params_in.dptr); + HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeC, graph, &allocNodeB, 1, ¶ms_in)); REQUIRE(params_in.dptr != nullptr); - C_d = reinterpret_cast(params_in.dptr); + C_d = reinterpret_cast(params_in.dptr); // Check shows that A_d, B_d & C_d DON'T share any virtual address each other REQUIRE(A_d != B_d); REQUIRE(B_d != C_d); REQUIRE(A_d != C_d); - HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpyH2D_A, graph, &allocNodeC, 1, A_d, - A_h, Nbytes, hipMemcpyHostToDevice)); - HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpyH2D_B, graph, &allocNodeC, 1, B_d, - B_h, Nbytes, hipMemcpyHostToDevice)); + HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpyH2D_A, graph, &allocNodeC, 1, A_d, A_h, Nbytes, + hipMemcpyHostToDevice)); + HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpyH2D_B, graph, &allocNodeC, 1, B_d, B_h, Nbytes, + hipMemcpyHostToDevice)); - void* kernelArgs[] = {&A_d, &B_d, &C_d, reinterpret_cast(&NElem)}; - kernelNodeParams.func = reinterpret_cast(HipTest::vectorADD); + void* kernelArgs[] = {&A_d, &B_d, &C_d, reinterpret_cast(&NElem)}; + kernelNodeParams.func = reinterpret_cast(HipTest::vectorADD); kernelNodeParams.gridDim = dim3(blocks); kernelNodeParams.blockDim = dim3(threadsPerBlock); kernelNodeParams.sharedMemBytes = 0; kernelNodeParams.kernelParams = reinterpret_cast(kernelArgs); kernelNodeParams.extra = nullptr; - HIP_CHECK(hipGraphAddKernelNode(&kernel_vecAdd, graph, nullptr, 0, - &kernelNodeParams)); + HIP_CHECK(hipGraphAddKernelNode(&kernel_vecAdd, graph, nullptr, 0, &kernelNodeParams)); // Create dependencies HIP_CHECK(hipGraphAddDependencies(graph, &memcpyH2D_A, &kernel_vecAdd, 1)); HIP_CHECK(hipGraphAddDependencies(graph, &memcpyH2D_B, &kernel_vecAdd, 1)); - HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpyD2H_C, graph, &kernel_vecAdd, 1, - C_h, C_d, Nbytes, hipMemcpyDeviceToHost)); + HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpyD2H_C, graph, &kernel_vecAdd, 1, C_h, C_d, Nbytes, + hipMemcpyDeviceToHost)); - HIP_CHECK(hipGraphAddMemFreeNode(&freeNodeA, graph, &memcpyD2H_C, - 1, reinterpret_cast(A_d))); - HIP_CHECK(hipGraphAddMemFreeNode(&freeNodeB, graph, &memcpyD2H_C, - 1, reinterpret_cast(B_d))); - HIP_CHECK(hipGraphAddMemFreeNode(&freeNodeC, graph, &memcpyD2H_C, - 1, reinterpret_cast(C_d))); + HIP_CHECK( + hipGraphAddMemFreeNode(&freeNodeA, graph, &memcpyD2H_C, 1, reinterpret_cast(A_d))); + HIP_CHECK( + hipGraphAddMemFreeNode(&freeNodeB, graph, &memcpyD2H_C, 1, reinterpret_cast(B_d))); + HIP_CHECK( + hipGraphAddMemFreeNode(&freeNodeC, graph, &memcpyD2H_C, 1, reinterpret_cast(C_d))); HIP_CHECK(hipGraphMemAllocNodeGetParams(allocNodeA, ¶ms_out)); REQUIRE(true == validateAllocParam(params_in, params_out)); @@ -239,12 +232,9 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_2") { REQUIRE(true == validateAllocParam(params_in, params_out)); int temp[] = {0}; - HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeA, - reinterpret_cast(temp))); - HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeB, - reinterpret_cast(temp))); - HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeC, - reinterpret_cast(temp))); + HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeA, reinterpret_cast(temp))); + HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeB, reinterpret_cast(temp))); + HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeC, reinterpret_cast(temp))); HIP_CHECK(hipGraphInstantiate(&graphExec, graph, nullptr, nullptr, 0)); HIP_CHECK(hipGraphLaunch(graphExec, stream)); @@ -261,27 +251,111 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_2") { } /** -* Test Description -* ------------------------ -*  - Negative Test for API - hipGraphMemAllocNodeGetParams -* 1) Pass MemAllocNode as nullptr -* 2) Pass MemAllocNode as empty node -* 3) Pass params_out as nullptr -* 4) Pass MemFreeNode inplace of MemAllocNode in 1st arguments -* - Negative Test for API - hipGraphMemFreeNodeGetParams -* 1) Pass MemFreeNode as nullptr -* 2) Pass MemFreeNode as empty node -* 3) Pass free pointer as nullptr -* 4) Pass free pointer as invalid pointer -* 5) Pass MemAllocNode inplace of MemFreeNode in 1st arguments -* Test source -* ------------------------ -*  - /unit/graph/hipGraphMemAllocNodeGetParams.cc -* Test requirements -* ------------------------ -*  - HIP_VERSION >= 6.0 -*/ + * Test Description + * ------------------------ + * - Functional Test for API - hipGraphMemAllocNodeGetParams. Create a graph and add a node with + * hipGraphAddMemAllocNode and hipGraphAddMemFreeNode and launch it. Check both pool props and + * access descriptor. + * Test source + * ------------------------ + * - /unit/graph/hipGraphMemAllocNodeGetParams.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 6.0 + */ +TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Functional_3") { + constexpr auto element_count{512 * 1024 * 1024}; + constexpr size_t num_bytes = element_count * sizeof(int); + hipGraphExec_t graph_exec; + hipGraph_t graph; + + LinearAllocGuard A_h = + LinearAllocGuard(LinearAllocs::malloc, element_count * sizeof(int)); + + StreamGuard stream_guard(Streams::created); + hipStream_t stream = stream_guard.stream(); + HIP_CHECK(hipGraphCreate(&graph, 0)); + + hipMemAccessDesc desc; + memset(&desc, 0, sizeof(hipMemAccessDesc)); + desc.location.type = hipMemLocationTypeDevice; + desc.location.id = 0; + desc.flags = hipMemAccessFlagsProtReadWrite; + + hipGraphNode_t alloc_node; + hipMemAllocNodeParams alloc_param; + memset(&alloc_param, 0, sizeof(alloc_param)); + alloc_param.bytesize = num_bytes; + alloc_param.poolProps.allocType = hipMemAllocationTypePinned; + alloc_param.poolProps.location.id = 0; + alloc_param.poolProps.location.type = hipMemLocationTypeDevice; + alloc_param.accessDescs = &desc; + alloc_param.accessDescCount = 1; + + HIP_CHECK(hipGraphAddMemAllocNode(&alloc_node, graph, nullptr, 0, &alloc_param)); + REQUIRE(alloc_param.dptr != nullptr); + int* A_d = reinterpret_cast(alloc_param.dptr); + + hipMemAllocNodeParams get_alloc_params; + HIP_CHECK(hipGraphMemAllocNodeGetParams(alloc_node, &get_alloc_params)); + REQUIRE(memcmp(&alloc_param, &get_alloc_params, sizeof(hipMemAllocNodeParams)) == 0); + + constexpr int fill_value = 11; + hipGraphNode_t memset_node; + hipMemsetParams memset_params{}; + memset(&memset_params, 0, sizeof(memset_params)); + memset_params.dst = reinterpret_cast(A_d); + memset_params.value = fill_value; + memset_params.pitch = 0; + memset_params.elementSize = sizeof(int); + memset_params.width = element_count; + memset_params.height = 1; + HIP_CHECK(hipGraphAddMemsetNode(&memset_node, graph, &alloc_node, 1, &memset_params)); + + hipGraphNode_t memcpy_node; + HIP_CHECK(hipGraphAddMemcpyNode1D(&memcpy_node, graph, &memset_node, 1, A_h.host_ptr(), A_d, + num_bytes, hipMemcpyDeviceToHost)); + + hipGraphNode_t free_node; + HIP_CHECK(hipGraphAddMemFreeNode(&free_node, graph, &memcpy_node, 1, (void*)A_d)); + + void* dptr_out; + HIP_CHECK(hipGraphMemFreeNodeGetParams(free_node, &dptr_out)); + REQUIRE(A_d == static_cast(dptr_out)); + + // Instantiate graph + HIP_CHECK(hipGraphInstantiate(&graph_exec, graph, nullptr, nullptr, 0)); + HIP_CHECK(hipGraphLaunch(graph_exec, stream)); + HIP_CHECK(hipStreamSynchronize(stream)); + + ArrayFindIfNot(A_h.host_ptr(), fill_value, element_count); + + HIP_CHECK(hipGraphExecDestroy(graph_exec)); + HIP_CHECK(hipGraphDestroy(graph)); +} + +/** + * Test Description + * ------------------------ + *  - Negative Test for API - hipGraphMemAllocNodeGetParams + * 1) Pass MemAllocNode as nullptr + * 2) Pass MemAllocNode as empty node + * 3) Pass params_out as nullptr + * 4) Pass MemFreeNode inplace of MemAllocNode in 1st arguments + * - Negative Test for API - hipGraphMemFreeNodeGetParams + * 1) Pass MemFreeNode as nullptr + * 2) Pass MemFreeNode as empty node + * 3) Pass free pointer as nullptr + * 4) Pass free pointer as invalid pointer + * 5) Pass MemAllocNode inplace of MemFreeNode in 1st arguments + * Test source + * ------------------------ + *  - /unit/graph/hipGraphMemAllocNodeGetParams.cc + * Test requirements + * ------------------------ + *  - HIP_VERSION >= 6.0 + */ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Negative") { hipError_t ret; constexpr size_t N = 1024 * 1024; @@ -303,7 +377,7 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Negative") { params_in.poolProps.location.type = hipMemLocationTypeDevice; HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeA, graph, NULL, 0, ¶ms_in)); - int *A_d = reinterpret_cast(params_in.dptr); + int* A_d = reinterpret_cast(params_in.dptr); REQUIRE(A_d != nullptr); HIP_CHECK(hipGraphAddMemFreeNode(&freeNodeA, graph, &allocNodeA, 1, A_d)); @@ -328,14 +402,12 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Negative") { int temp[] = {0}; SECTION("Pass MemFreeNode as nullptr") { - ret = hipGraphMemFreeNodeGetParams(nullptr, - reinterpret_cast(temp)); + ret = hipGraphMemFreeNodeGetParams(nullptr, reinterpret_cast(temp)); REQUIRE(hipErrorInvalidValue == ret); } SECTION("Pass MemFreeNode as empty node") { hipGraphNode_t freeNode_empty{}; - ret = hipGraphMemFreeNodeGetParams(freeNode_empty, - reinterpret_cast(temp)); + ret = hipGraphMemFreeNodeGetParams(freeNode_empty, reinterpret_cast(temp)); REQUIRE(hipErrorInvalidValue == ret); } SECTION("Pass free pointer as nullptr") { @@ -343,8 +415,7 @@ TEST_CASE("Unit_hipGraphMem_Alloc_Free_NodeGetParams_Negative") { REQUIRE(hipErrorInvalidValue == ret); } SECTION("Pass MemAllocNode inplace of MemFreeNode in 1st arguments") { - ret = hipGraphMemFreeNodeGetParams(allocNodeA, - reinterpret_cast(temp)); + ret = hipGraphMemFreeNodeGetParams(allocNodeA, reinterpret_cast(temp)); REQUIRE(hipErrorInvalidValue == ret); }