EXSWHTEC-339 - Implement unit tests for MemAlloc Graph Node APIs (#423)

Change-Id: I6fa905982fc5a65bedfb3242ce18590e2ac813c2


[ROCm/hip-tests commit: 37f45585d2]
This commit is contained in:
Nives Vukovic
2023-12-15 15:41:32 +05:30
committed by Rakesh Roy
parent 11e92e0fdd
commit 6e5ef40db9
3 changed files with 652 additions and 122 deletions
@@ -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 <hip_test_common.hh>
#include <hip_test_checkers.hh>
#include <hip_test_kernels.hh>
#include <resource_guards.hh>
#include <utils.hh>
/**
* 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, &params_in));
int *A_d = reinterpret_cast<int *>(params_in.dptr);
int* A_d = reinterpret_cast<int*>(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<int>(nullptr, nullptr, nullptr,
&A_h, &B_h, &C_h, N, false);
HipTest::initArrays<int>(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, &params_in));
REQUIRE(params_in.dptr != nullptr);
A_d = reinterpret_cast<int *>(params_in.dptr);
HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeB, graph,
&allocNodeA, 1, &params_in));
A_d = reinterpret_cast<int*>(params_in.dptr);
HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeB, graph, &allocNodeA, 1, &params_in));
REQUIRE(params_in.dptr != nullptr);
B_d = reinterpret_cast<int *>(params_in.dptr);
HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeC, graph,
&allocNodeB, 1, &params_in));
B_d = reinterpret_cast<int*>(params_in.dptr);
HIP_CHECK(hipGraphAddMemAllocNode(&allocNodeC, graph, &allocNodeB, 1, &params_in));
REQUIRE(params_in.dptr != nullptr);
C_d = reinterpret_cast<int *>(params_in.dptr);
C_d = reinterpret_cast<int*>(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<void *>(&NElem)};
kernelNodeParams.func = reinterpret_cast<void *>(HipTest::vectorADD<int>);
void* kernelArgs[] = {&A_d, &B_d, &C_d, reinterpret_cast<void*>(&NElem)};
kernelNodeParams.func = reinterpret_cast<void*>(HipTest::vectorADD<int>);
kernelNodeParams.gridDim = dim3(blocks);
kernelNodeParams.blockDim = dim3(threadsPerBlock);
kernelNodeParams.sharedMemBytes = 0;
kernelNodeParams.kernelParams = reinterpret_cast<void**>(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<void *>(A_d)));
HIP_CHECK(hipGraphAddMemFreeNode(&freeNodeB, graph, &memcpyD2H_C,
1, reinterpret_cast<void *>(B_d)));
HIP_CHECK(hipGraphAddMemFreeNode(&freeNodeC, graph, &memcpyD2H_C,
1, reinterpret_cast<void *>(C_d)));
HIP_CHECK(
hipGraphAddMemFreeNode(&freeNodeA, graph, &memcpyD2H_C, 1, reinterpret_cast<void*>(A_d)));
HIP_CHECK(
hipGraphAddMemFreeNode(&freeNodeB, graph, &memcpyD2H_C, 1, reinterpret_cast<void*>(B_d)));
HIP_CHECK(
hipGraphAddMemFreeNode(&freeNodeC, graph, &memcpyD2H_C, 1, reinterpret_cast<void*>(C_d)));
HIP_CHECK(hipGraphMemAllocNodeGetParams(allocNodeA, &params_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<void *>(temp)));
HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeB,
reinterpret_cast<void *>(temp)));
HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeC,
reinterpret_cast<void *>(temp)));
HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeA, reinterpret_cast<void*>(temp)));
HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeB, reinterpret_cast<void*>(temp)));
HIP_CHECK(hipGraphMemFreeNodeGetParams(freeNodeC, reinterpret_cast<void*>(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<int> A_h =
LinearAllocGuard<int>(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<int*>(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<void*>(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<int*>(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, &params_in));
int *A_d = reinterpret_cast<int *>(params_in.dptr);
int* A_d = reinterpret_cast<int*>(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<void *>(temp));
ret = hipGraphMemFreeNodeGetParams(nullptr, reinterpret_cast<void*>(temp));
REQUIRE(hipErrorInvalidValue == ret);
}
SECTION("Pass MemFreeNode as empty node") {
hipGraphNode_t freeNode_empty{};
ret = hipGraphMemFreeNodeGetParams(freeNode_empty,
reinterpret_cast<void *>(temp));
ret = hipGraphMemFreeNodeGetParams(freeNode_empty, reinterpret_cast<void*>(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<void *>(temp));
ret = hipGraphMemFreeNodeGetParams(allocNodeA, reinterpret_cast<void*>(temp));
REQUIRE(hipErrorInvalidValue == ret);
}