Skip to content

Commit

Permalink
Add multi-threaded memory IPC handle get/put test
Browse files Browse the repository at this point in the history
Related-To: NEO-9226

Signed-off-by: Lu, Wenbin <[email protected]>
  • Loading branch information
lyu authored and Jemale committed Feb 23, 2024
1 parent 0fe953c commit e2f8b74
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
56 changes: 55 additions & 1 deletion conformance_tests/core/test_ipc/src/test_ipc_put_handle.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 2023 Intel Corporation
* Copyright (C) 2023-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand All @@ -20,6 +20,7 @@
#include "test_ipc_put_handle.hpp"
#include "net/test_ipc_comm.hpp"

#include <thread>
#include <level_zero/ze_api.h>

namespace {
Expand Down Expand Up @@ -476,6 +477,59 @@ TEST_F(
lzt::destroy_context(context_);
}

TEST(
zePutIpcMemHandleMultiThreadedTests,
GivenMultipleThreadsWhenGettingAndPuttingIpcHandlesThenOperationsAreSuccessful) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));

auto ipc_flags = lzt::get_ipc_properties(lzt::get_default_driver()).flags;
if ((ipc_flags & ZE_IPC_PROPERTY_FLAG_MEMORY) == 0) {
GTEST_SKIP() << "Driver does not support memory IPC";
}

constexpr int n_threads = 8;
auto context = lzt::create_context();
void *buf_h = lzt::allocate_host_memory(1, 1, context);
void *buf_d = lzt::allocate_device_memory(1, 0, 0, context);

auto thread_func = [](ze_context_handle_t context, void *buf_h, void *buf_d,
int thread_id) {
constexpr int n_iters = 2000;

for (int i = 0; i < n_iters; i++) {
const int n_handles = 450 - thread_id;
std::vector<ze_ipc_mem_handle_t> handles(n_handles);
for (auto &handle : handles) {
std::fill_n(handle.data, ZE_MAX_IPC_HANDLE_SIZE, 0);
}

for (int j = 0; j < n_handles; j++) {
if ((j + thread_id) % 2 == 0) {
lzt::get_ipc_handle(context, &handles[j], buf_h);
} else {
lzt::get_ipc_handle(context, &handles[j], buf_d);
}
}

for (auto &handle : handles) {
lzt::put_ipc_handle(context, handle);
}
}
};

std::vector<std::thread> threads;
for (int i = 0; i < n_threads; i++) {
threads.emplace_back(std::thread(thread_func, context, buf_h, buf_d, i));
}
for (auto &thread : threads) {
thread.join();
}

lzt::free_memory(context, buf_d);
lzt::free_memory(context, buf_h);
lzt::destroy_context(context);
}

} // namespace

// We put the main here because L0 doesn't currently specify how
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 2019 - 2023 Intel Corporation
* Copyright (C) 2019 - 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -110,8 +110,10 @@ void allocate_mem_and_get_ipc_handle(ze_context_handle_t context,
ze_memory_type_t mem_type, size_t size);
void get_ipc_handle(ze_context_handle_t context, ze_ipc_mem_handle_t *handle,
void *memory);
void put_ipc_handle(ze_context_handle_t context, ze_ipc_mem_handle_t handle);
void open_ipc_handle(ze_context_handle_t context, ze_device_handle_t device,
ze_ipc_mem_handle_t mem_handle, void **memory);
void close_ipc_handle(ze_context_handle_t context, void **memory);
void write_data_pattern(void *buff, size_t size, int8_t data_pattern);
void validate_data_pattern(void *buff, size_t size, int8_t data_pattern);
void get_mem_alloc_properties(
Expand Down
13 changes: 10 additions & 3 deletions utils/test_harness/src/test_harness_memory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 2019 - 2023 Intel Corporation
* Copyright (C) 2019 - 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -315,9 +315,12 @@ void allocate_mem_and_get_ipc_handle(ze_context_handle_t context,

void get_ipc_handle(ze_context_handle_t context,
ze_ipc_mem_handle_t *mem_handle, void *memory) {
auto context_initial = context;
EXPECT_EQ(ZE_RESULT_SUCCESS, zeMemGetIpcHandle(context, memory, mem_handle));
EXPECT_EQ(context, context_initial);
}

void put_ipc_handle(ze_context_handle_t context,
ze_ipc_mem_handle_t mem_handle) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zeMemPutIpcHandle(context, mem_handle));
}

void open_ipc_handle(ze_context_handle_t context, ze_device_handle_t device,
Expand All @@ -326,6 +329,10 @@ void open_ipc_handle(ze_context_handle_t context, ze_device_handle_t device,
zeMemOpenIpcHandle(context, device, mem_handle, 0, memory));
}

void close_ipc_handle(ze_context_handle_t context, void **memory) {
EXPECT_EQ(ZE_RESULT_SUCCESS, zeMemCloseIpcHandle(context, memory));
}

void write_data_pattern(void *buff, size_t size, int8_t data_pattern) {
int8_t *pbuff = static_cast<int8_t *>(buff);
int8_t dp = data_pattern;
Expand Down

0 comments on commit e2f8b74

Please sign in to comment.