Skip to content

Commit

Permalink
Merge pull request #756 from veselypeta/petr/adapter-testing
Browse files Browse the repository at this point in the history
[UR] Implement CUDA native handle tests
  • Loading branch information
veselypeta authored Aug 1, 2023
2 parents d582c12 + fcded3d commit 41bcb88
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/conformance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function(add_conformance_test_with_platform_environment name)
endfunction()

add_subdirectory(testing)
add_subdirectory(adapters)

add_subdirectory(platform)
add_subdirectory(device)
Expand Down
3 changes: 3 additions & 0 deletions test/conformance/adapters/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if(UR_BUILD_ADAPTER_CUDA)
add_subdirectory(cuda)
endif()
15 changes: 15 additions & 0 deletions test/conformance/adapters/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

add_conformance_test_with_devices_environment(adapter-cuda
cuda_fixtures.h
cuda_urContextGetNativeHandle.cpp
cuda_urDeviceGetNativeHandle.cpp
cuda_urDeviceCreateWithNativeHandle.cpp
cuda_urEventGetNativeHandle.cpp
cuda_urEventCreateWithNativeHandle.cpp
)
target_link_libraries(test-adapter-cuda PRIVATE cudadrv)

set_tests_properties(adapter-cuda PROPERTIES
LABELS "conformance:cuda"
ENVIRONMENT "UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_cuda>\""
)
38 changes: 38 additions & 0 deletions test/conformance/adapters/cuda/cuda_fixtures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef UR_TEST_CONFORMANCE_ADAPTERS_CUDA_FIXTURES_H_INCLUDED
#define UR_TEST_CONFORMANCE_ADAPTERS_CUDA_FIXTURES_H_INCLUDED
#include <cuda.h>
#include <uur/fixtures.h>

namespace uur {
struct ResultCuda {

constexpr ResultCuda(CUresult result) noexcept : value(result) {}

inline bool operator==(const ResultCuda &rhs) const noexcept {
return rhs.value == value;
}

CUresult value;
};
} // namespace uur

#ifndef ASSERT_EQ_RESULT_CUDA
#define ASSERT_EQ_RESULT_CUDA(EXPECTED, ACTUAL) \
ASSERT_EQ(uur::ResultCuda(EXPECTED), uur::ResultCuda(ACTUAL))
#endif // ASSERT_EQ_RESULT_CUDA

#ifndef ASSERT_SUCCESS_CUDA
#define ASSERT_SUCCESS_CUDA(ACTUAL) ASSERT_EQ_RESULT_CUDA(CUDA_SUCCESS, ACTUAL)
#endif // ASSERT_SUCCESS_CUDA

#ifndef EXPECT_EQ_RESULT_CUDA
#define EXPECT_EQ_RESULT_CUDA(EXPECTED, ACTUAL) \
EXPECT_EQ_RESULT_CUDA(uur::ResultCuda(EXPECTED), uur::ResultCuda(ACTUAL))
#endif // EXPECT_EQ_RESULT_CUDA

#ifndef EXPECT_SUCCESS_CUDA
#define EXPECT_SUCCESS_CUDA(ACTUAL) \
EXPECT_EQ_RESULT_CUDA(UR_RESULT_SUCCESS, ACTUAL)
#endif // EXPECT_EQ_RESULT_CUDA

#endif // UR_TEST_CONFORMANCE_ADAPTERS_CUDA_FIXTURES_H_INCLUDED
13 changes: 13 additions & 0 deletions test/conformance/adapters/cuda/cuda_urContextGetNativeHandle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "cuda_fixtures.h"

using urCudaContextGetNativeHandle = uur::urContextTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaContextGetNativeHandle);

TEST_P(urCudaContextGetNativeHandle, Success) {
ur_native_handle_t native_context = nullptr;
ASSERT_SUCCESS(urContextGetNativeHandle(context, &native_context));
CUcontext cuda_context = reinterpret_cast<CUcontext>(native_context);

unsigned int cudaVersion;
ASSERT_SUCCESS_CUDA(cuCtxGetApiVersion(cuda_context, &cudaVersion));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "cuda_fixtures.h"

using urCudaDeviceCreateWithNativeHandle = uur::urPlatformTest;

TEST_F(urCudaDeviceCreateWithNativeHandle, Success) {
// get a device from cuda
int nCudaDevices;
ASSERT_SUCCESS_CUDA(cuDeviceGetCount(&nCudaDevices));
ASSERT_GT(nCudaDevices, 0);
CUdevice cudaDevice;
ASSERT_SUCCESS_CUDA(cuDeviceGet(&cudaDevice, 0));

ur_native_handle_t nativeCuda =
reinterpret_cast<ur_native_handle_t>(cudaDevice);
ur_device_handle_t urDevice;
ASSERT_SUCCESS(urDeviceCreateWithNativeHandle(nativeCuda, platform, nullptr,
&urDevice));
}
15 changes: 15 additions & 0 deletions test/conformance/adapters/cuda/cuda_urDeviceGetNativeHandle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "cuda_fixtures.h"

using urCudaGetDeviceNativeHandle = uur::urDeviceTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaGetDeviceNativeHandle);

TEST_P(urCudaGetDeviceNativeHandle, Success) {
ur_native_handle_t native_handle;
ASSERT_SUCCESS(urDeviceGetNativeHandle(device, &native_handle));

CUdevice cuda_device = *reinterpret_cast<CUdevice *>(&native_handle);

char cuda_device_name[256];
ASSERT_SUCCESS_CUDA(cuDeviceGetName(cuda_device_name,
sizeof(cuda_device_name), cuda_device));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "cuda_fixtures.h"

using urCudaEventCreateWithNativeHandleTest = uur::urQueueTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaEventCreateWithNativeHandleTest);

TEST_P(urCudaEventCreateWithNativeHandleTest, Success) {

CUevent cuda_event;
ASSERT_SUCCESS_CUDA(cuEventCreate(&cuda_event, CU_EVENT_DEFAULT));

ur_native_handle_t native_event =
reinterpret_cast<ur_native_handle_t>(cuda_event);

ur_event_handle_t event = nullptr;
ASSERT_SUCCESS(
urEventCreateWithNativeHandle(native_event, context, nullptr, &event));

ASSERT_SUCCESS(urEventRelease(event));
}
25 changes: 25 additions & 0 deletions test/conformance/adapters/cuda/cuda_urEventGetNativeHandle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "cuda_fixtures.h"

using urCudaEventGetNativeHandleTest = uur::urQueueTest;
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(urCudaEventGetNativeHandleTest);

TEST_P(urCudaEventGetNativeHandleTest, Success) {
constexpr size_t buffer_size = 1024;
ur_mem_handle_t mem = nullptr;
ASSERT_SUCCESS(urMemBufferCreate(context, UR_MEM_FLAG_READ_WRITE,
buffer_size, nullptr, &mem));

ur_event_handle_t event = nullptr;
uint8_t pattern = 6;
ASSERT_SUCCESS(urEnqueueMemBufferFill(queue, mem, &pattern, sizeof(pattern),
0, buffer_size, 0, nullptr, &event));

ur_native_handle_t native_event = nullptr;
ASSERT_SUCCESS(urEventGetNativeHandle(event, &native_event));
CUevent cuda_event = reinterpret_cast<CUevent>(native_event);

ASSERT_SUCCESS_CUDA(cuEventSynchronize(cuda_event));

ASSERT_SUCCESS(urEventRelease(event));
ASSERT_SUCCESS(urMemRelease(mem));
}

0 comments on commit 41bcb88

Please sign in to comment.