Skip to content

Commit b29ee99

Browse files
author
Steffen Larsen
committed
[SYCL][CUDA] Fixes CUDA unit tests that uses SYCL directly
CUDA-specific unit tests that use SYCL directly fail if another backend is in use. This commit fixes this by actively selecting CUDA platforms and only runs the tests on these. Signed-off-by: Steffen Larsen <[email protected]>
1 parent aa05627 commit b29ee99

File tree

3 files changed

+74
-89
lines changed

3 files changed

+74
-89
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
// See https://llvm.org/LICENSE.txt for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
#pragma once
6+
7+
#include <CL/sycl.hpp>
8+
#include <algorithm>
9+
#include <functional>
10+
#include <vector>
11+
12+
namespace pi {
13+
inline std::vector<cl::sycl::platform> getPlatformsWithName(const char *name) {
14+
std::vector<cl::sycl::platform> platforms =
15+
cl::sycl::platform::get_platforms();
16+
17+
// Remove platforms that have no devices or doesn't contain the name
18+
auto end =
19+
std::remove_if(platforms.begin(), platforms.end(),
20+
[=](const cl::sycl::platform &platform) -> bool {
21+
const std::string platformName =
22+
platform.get_info<cl::sycl::info::platform::name>();
23+
return platformName.find(name) == std::string::npos ||
24+
platform.get_devices().size() == 0;
25+
});
26+
platforms.erase(end, platforms.end());
27+
28+
return platforms;
29+
}
30+
} // namespace pi

sycl/unittests/pi/cuda/test_interop_get_native.cpp

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,44 @@
88

99
#include "gtest/gtest.h"
1010

11+
#include "TestGetPlatforms.hpp"
1112
#include <CL/sycl.hpp>
1213
#include <CL/sycl/backend/cuda.hpp>
1314
#include <cuda.h>
1415
#include <iostream>
1516

1617
using namespace cl::sycl;
1718

18-
struct CudaInteropGetNativeTests : public ::testing::Test {
19+
struct CudaInteropGetNativeTests : public ::testing::TestWithParam<platform> {
1920

2021
protected:
2122
queue syclQueue_;
2223
context syclContext_;
2324
device syclDevice_;
2425

25-
CudaInteropGetNativeTests()
26-
: syclQueue_(cuda_device_selector()),
27-
syclContext_(syclQueue_.get_context()),
28-
syclDevice_(syclQueue_.get_device()) {}
29-
30-
static bool isCudaDevice(const device &dev) {
31-
const platform platform = dev.get_info<info::device::platform>();
32-
const std::string platformVersion =
33-
platform.get_info<info::platform::version>();
34-
const std::string platformName = platform.get_info<info::platform::name>();
35-
// If using PI_CUDA, don't accept a non-CUDA device
36-
return platformVersion.find("CUDA") != std::string::npos &&
37-
platformName.find("NVIDIA CUDA") != std::string::npos;
26+
void SetUp() override {
27+
syclDevice_ = GetParam().get_devices()[0];
28+
syclQueue_ = queue{syclDevice_};
29+
syclContext_ = syclQueue_.get_context();
3830
}
3931

40-
class cuda_device_selector : public device_selector {
41-
public:
42-
int operator()(const device &dev) const {
43-
return isCudaDevice(dev) ? 1000 : -1000;
44-
}
45-
};
46-
47-
void SetUp() override {}
48-
4932
void TearDown() override {}
5033
};
5134

52-
TEST_F(CudaInteropGetNativeTests, getNativeDevice) {
35+
TEST_P(CudaInteropGetNativeTests, getNativeDevice) {
5336
CUdevice cudaDevice = get_native<backend::cuda>(syclDevice_);
5437
char cudaDeviceName[2] = {0, 0};
5538
CUresult result = cuDeviceGetName(cudaDeviceName, 2, cudaDevice);
5639
ASSERT_EQ(result, CUDA_SUCCESS);
5740
ASSERT_NE(cudaDeviceName[0], 0);
5841
}
5942

60-
TEST_F(CudaInteropGetNativeTests, getNativeContext) {
43+
TEST_P(CudaInteropGetNativeTests, getNativeContext) {
6144
CUcontext cudaContext = get_native<backend::cuda>(syclContext_);
6245
ASSERT_NE(cudaContext, nullptr);
6346
}
6447

65-
TEST_F(CudaInteropGetNativeTests, getNativeQueue) {
48+
TEST_P(CudaInteropGetNativeTests, getNativeQueue) {
6649
CUstream cudaStream = get_native<backend::cuda>(syclQueue_);
6750
ASSERT_NE(cudaStream, nullptr);
6851

@@ -74,21 +57,25 @@ TEST_F(CudaInteropGetNativeTests, getNativeQueue) {
7457
ASSERT_EQ(streamContext, cudaContext);
7558
}
7659

77-
TEST_F(CudaInteropGetNativeTests, interopTaskGetMem) {
60+
TEST_P(CudaInteropGetNativeTests, interopTaskGetMem) {
7861
buffer<int, 1> syclBuffer(range<1>{1});
7962
syclQueue_.submit([&](handler &cgh) {
8063
auto syclAccessor = syclBuffer.get_access<access::mode::read>(cgh);
8164
cgh.interop_task([=](interop_handler ih) {
8265
CUdeviceptr cudaPtr = ih.get_mem<backend::cuda>(syclAccessor);
8366
CUdeviceptr cudaPtrBase;
8467
size_t cudaPtrSize = 0;
85-
cuMemGetAddressRange(&cudaPtrBase, &cudaPtrSize, cudaPtr);
86-
ASSERT_EQ(cudaPtrSize, sizeof(int));
68+
CUcontext cudaContext = get_native<backend::cuda>(syclContext_);
69+
ASSERT_EQ(CUDA_SUCCESS, cuCtxPushCurrent(cudaContext));
70+
ASSERT_EQ(CUDA_SUCCESS,
71+
cuMemGetAddressRange(&cudaPtrBase, &cudaPtrSize, cudaPtr));
72+
ASSERT_EQ(CUDA_SUCCESS, cuCtxPopCurrent(nullptr));
73+
ASSERT_EQ(sizeof(int), cudaPtrSize);
8774
});
8875
});
8976
}
9077

91-
TEST_F(CudaInteropGetNativeTests, interopTaskGetBufferMem) {
78+
TEST_P(CudaInteropGetNativeTests, interopTaskGetBufferMem) {
9279
CUstream cudaStream = get_native<backend::cuda>(syclQueue_);
9380
syclQueue_.submit([&](handler &cgh) {
9481
cgh.interop_task([=](interop_handler ih) {
@@ -97,3 +84,7 @@ TEST_F(CudaInteropGetNativeTests, interopTaskGetBufferMem) {
9784
});
9885
});
9986
}
87+
88+
INSTANTIATE_TEST_CASE_P(
89+
OnCudaPlatform, CudaInteropGetNativeTests,
90+
::testing::ValuesIn(pi::getPlatformsWithName("CUDA BACKEND")), );

sycl/unittests/pi/cuda/test_primary_context.cpp

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,32 @@
1010

1111
#include <cuda.h>
1212

13-
#include "TestGetPlugin.hpp"
13+
#include "TestGetPlatforms.hpp"
1414
#include <CL/sycl.hpp>
1515
#include <CL/sycl/backend/cuda.hpp>
16-
#include <detail/plugin.hpp>
1716
#include <pi_cuda.hpp>
1817

1918
#include <iostream>
2019

2120
using namespace cl::sycl;
2221

23-
struct CudaPrimaryContextTests : public ::testing::Test {
22+
struct CudaPrimaryContextTests : public ::testing::TestWithParam<platform> {
2423

2524
protected:
2625
device deviceA_;
2726
device deviceB_;
28-
context context_;
29-
30-
static bool isCudaDevice(const device &dev) {
31-
const platform platform = dev.get_info<info::device::platform>();
32-
const std::string platformVersion =
33-
platform.get_info<info::platform::version>();
34-
// If using PI_CUDA, don't accept a non-CUDA device
35-
return platformVersion.find("CUDA BACKEND") != std::string::npos;
36-
}
37-
38-
class cuda_device_selector : public device_selector {
39-
public:
40-
int operator()(const device &dev) const {
41-
return isCudaDevice(dev) ? 1 : -1;
42-
}
43-
};
44-
45-
class other_cuda_device_selector : public device_selector {
46-
public:
47-
other_cuda_device_selector(const device &dev) : excludeDevice{dev} {}
48-
49-
int operator()(const device &dev) const {
50-
if (!isCudaDevice(dev)) {
51-
return -1;
52-
}
53-
if (dev.get() == excludeDevice.get()) {
54-
// Return only this device if it is the only available
55-
return 0;
56-
}
57-
return 1;
58-
}
59-
60-
private:
61-
const device &excludeDevice;
62-
};
6327

6428
void SetUp() override {
29+
std::vector<device> CudaDevices = GetParam().get_devices();
6530

66-
try {
67-
context context_;
68-
} catch (device_error &e) {
69-
std::cout << "Failed to create device for context" << std::endl;
70-
}
71-
72-
deviceA_ = cuda_device_selector().select_device();
73-
deviceB_ = other_cuda_device_selector(deviceA_).select_device();
74-
75-
ASSERT_TRUE(isCudaDevice(deviceA_));
31+
deviceA_ = CudaDevices[0];
32+
deviceB_ = CudaDevices.size() > 1 ? CudaDevices[1] : deviceA_;
7633
}
7734

7835
void TearDown() override {}
7936
};
8037

81-
TEST_F(CudaPrimaryContextTests, piSingleContext) {
38+
TEST_P(CudaPrimaryContextTests, piSingleContext) {
8239
std::cout << "create single context" << std::endl;
8340
context Context(deviceA_, async_handler{}, /*UsePrimaryContext=*/true);
8441

@@ -93,7 +50,7 @@ TEST_F(CudaPrimaryContextTests, piSingleContext) {
9350
cuDevicePrimaryCtxRelease(CudaDevice);
9451
}
9552

96-
TEST_F(CudaPrimaryContextTests, piMultiContextSingleDevice) {
53+
TEST_P(CudaPrimaryContextTests, piMultiContextSingleDevice) {
9754
std::cout << "create multiple contexts for one device" << std::endl;
9855
context ContextA(deviceA_, async_handler{}, /*UsePrimaryContext=*/true);
9956
context ContextB(deviceA_, async_handler{}, /*UsePrimaryContext=*/true);
@@ -104,18 +61,25 @@ TEST_F(CudaPrimaryContextTests, piMultiContextSingleDevice) {
10461
ASSERT_EQ(CudaContextA, CudaContextB);
10562
}
10663

107-
TEST_F(CudaPrimaryContextTests, piMultiContextMultiDevice) {
64+
TEST_P(CudaPrimaryContextTests, piMultiContextMultiDevice) {
65+
if (deviceA_ == deviceB_)
66+
return;
67+
10868
CUdevice CudaDeviceA = deviceA_.get_native<backend::cuda>();
10969
CUdevice CudaDeviceB = deviceB_.get_native<backend::cuda>();
11070

111-
if (isCudaDevice(deviceB_) && CudaDeviceA != CudaDeviceB) {
112-
std::cout << "create multiple contexts for multiple devices" << std::endl;
113-
context ContextA(deviceA_, async_handler{}, /*UsePrimaryContext=*/true);
114-
context ContextB(deviceB_, async_handler{}, /*UsePrimaryContext=*/true);
71+
ASSERT_NE(CudaDeviceA, CudaDeviceB);
11572

116-
CUcontext CudaContextA = ContextA.get_native<backend::cuda>();
117-
CUcontext CudaContextB = ContextB.get_native<backend::cuda>();
73+
std::cout << "create multiple contexts for multiple devices" << std::endl;
74+
context ContextA(deviceA_, async_handler{}, /*UsePrimaryContext=*/true);
75+
context ContextB(deviceB_, async_handler{}, /*UsePrimaryContext=*/true);
11876

119-
ASSERT_NE(CudaContextA, CudaContextB);
120-
}
77+
CUcontext CudaContextA = ContextA.get_native<backend::cuda>();
78+
CUcontext CudaContextB = ContextB.get_native<backend::cuda>();
79+
80+
ASSERT_NE(CudaContextA, CudaContextB);
12181
}
82+
83+
INSTANTIATE_TEST_CASE_P(
84+
OnCudaPlatform, CudaPrimaryContextTests,
85+
::testing::ValuesIn(pi::getPlatformsWithName("CUDA BACKEND")), );

0 commit comments

Comments
 (0)