Skip to content

[SYCL][CUDA] Fixes CUDA unit tests that uses SYCL directly #1763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sycl/unittests/pi/TestGetPlatforms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#pragma once

#include <CL/sycl.hpp>
#include <algorithm>
#include <functional>
#include <vector>

namespace pi {
inline std::vector<cl::sycl::platform> getPlatformsWithName(const char *name) {
std::vector<cl::sycl::platform> platforms =
cl::sycl::platform::get_platforms();

// Remove platforms that have no devices or doesn't contain the name
auto end =
std::remove_if(platforms.begin(), platforms.end(),
[=](const cl::sycl::platform &platform) -> bool {
const std::string platformName =
platform.get_info<cl::sycl::info::platform::name>();
return platformName.find(name) == std::string::npos ||
platform.get_devices().size() == 0;
});
platforms.erase(end, platforms.end());

return platforms;
}
} // namespace pi
51 changes: 21 additions & 30 deletions sycl/unittests/pi/cuda/test_interop_get_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,44 @@

#include "gtest/gtest.h"

#include "TestGetPlatforms.hpp"
#include <CL/sycl.hpp>
#include <CL/sycl/backend/cuda.hpp>
#include <cuda.h>
#include <iostream>

using namespace cl::sycl;

struct CudaInteropGetNativeTests : public ::testing::Test {
struct CudaInteropGetNativeTests : public ::testing::TestWithParam<platform> {

protected:
queue syclQueue_;
context syclContext_;
device syclDevice_;

CudaInteropGetNativeTests()
: syclQueue_(cuda_device_selector()),
syclContext_(syclQueue_.get_context()),
syclDevice_(syclQueue_.get_device()) {}

static bool isCudaDevice(const device &dev) {
const platform platform = dev.get_info<info::device::platform>();
const std::string platformVersion =
platform.get_info<info::platform::version>();
const std::string platformName = platform.get_info<info::platform::name>();
// If using PI_CUDA, don't accept a non-CUDA device
return platformVersion.find("CUDA") != std::string::npos &&
platformName.find("NVIDIA CUDA") != std::string::npos;
void SetUp() override {
syclDevice_ = GetParam().get_devices()[0];
syclQueue_ = queue{syclDevice_};
syclContext_ = syclQueue_.get_context();
}

class cuda_device_selector : public device_selector {
public:
int operator()(const device &dev) const {
return isCudaDevice(dev) ? 1000 : -1000;
}
};

void SetUp() override {}

void TearDown() override {}
};

TEST_F(CudaInteropGetNativeTests, getNativeDevice) {
TEST_P(CudaInteropGetNativeTests, getNativeDevice) {
CUdevice cudaDevice = get_native<backend::cuda>(syclDevice_);
char cudaDeviceName[2] = {0, 0};
CUresult result = cuDeviceGetName(cudaDeviceName, 2, cudaDevice);
ASSERT_EQ(result, CUDA_SUCCESS);
ASSERT_NE(cudaDeviceName[0], 0);
}

TEST_F(CudaInteropGetNativeTests, getNativeContext) {
TEST_P(CudaInteropGetNativeTests, getNativeContext) {
CUcontext cudaContext = get_native<backend::cuda>(syclContext_);
ASSERT_NE(cudaContext, nullptr);
}

TEST_F(CudaInteropGetNativeTests, getNativeQueue) {
TEST_P(CudaInteropGetNativeTests, getNativeQueue) {
CUstream cudaStream = get_native<backend::cuda>(syclQueue_);
ASSERT_NE(cudaStream, nullptr);

Expand All @@ -74,21 +57,25 @@ TEST_F(CudaInteropGetNativeTests, getNativeQueue) {
ASSERT_EQ(streamContext, cudaContext);
}

TEST_F(CudaInteropGetNativeTests, interopTaskGetMem) {
TEST_P(CudaInteropGetNativeTests, interopTaskGetMem) {
buffer<int, 1> syclBuffer(range<1>{1});
syclQueue_.submit([&](handler &cgh) {
auto syclAccessor = syclBuffer.get_access<access::mode::read>(cgh);
cgh.interop_task([=](interop_handler ih) {
CUdeviceptr cudaPtr = ih.get_mem<backend::cuda>(syclAccessor);
CUdeviceptr cudaPtrBase;
size_t cudaPtrSize = 0;
cuMemGetAddressRange(&cudaPtrBase, &cudaPtrSize, cudaPtr);
ASSERT_EQ(cudaPtrSize, sizeof(int));
CUcontext cudaContext = get_native<backend::cuda>(syclContext_);
ASSERT_EQ(CUDA_SUCCESS, cuCtxPushCurrent(cudaContext));
ASSERT_EQ(CUDA_SUCCESS,
cuMemGetAddressRange(&cudaPtrBase, &cudaPtrSize, cudaPtr));
ASSERT_EQ(CUDA_SUCCESS, cuCtxPopCurrent(nullptr));
ASSERT_EQ(sizeof(int), cudaPtrSize);
});
});
}

TEST_F(CudaInteropGetNativeTests, interopTaskGetBufferMem) {
TEST_P(CudaInteropGetNativeTests, interopTaskGetBufferMem) {
CUstream cudaStream = get_native<backend::cuda>(syclQueue_);
syclQueue_.submit([&](handler &cgh) {
cgh.interop_task([=](interop_handler ih) {
Expand All @@ -97,3 +84,7 @@ TEST_F(CudaInteropGetNativeTests, interopTaskGetBufferMem) {
});
});
}

INSTANTIATE_TEST_CASE_P(
OnCudaPlatform, CudaInteropGetNativeTests,
::testing::ValuesIn(pi::getPlatformsWithName("CUDA BACKEND")), );
82 changes: 23 additions & 59 deletions sycl/unittests/pi/cuda/test_primary_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,32 @@

#include <cuda.h>

#include "TestGetPlugin.hpp"
#include "TestGetPlatforms.hpp"
#include <CL/sycl.hpp>
#include <CL/sycl/backend/cuda.hpp>
#include <detail/plugin.hpp>
#include <pi_cuda.hpp>

#include <iostream>

using namespace cl::sycl;

struct CudaPrimaryContextTests : public ::testing::Test {
struct CudaPrimaryContextTests : public ::testing::TestWithParam<platform> {

protected:
device deviceA_;
device deviceB_;
context context_;

static bool isCudaDevice(const device &dev) {
const platform platform = dev.get_info<info::device::platform>();
const std::string platformVersion =
platform.get_info<info::platform::version>();
// If using PI_CUDA, don't accept a non-CUDA device
return platformVersion.find("CUDA BACKEND") != std::string::npos;
}

class cuda_device_selector : public device_selector {
public:
int operator()(const device &dev) const {
return isCudaDevice(dev) ? 1 : -1;
}
};

class other_cuda_device_selector : public device_selector {
public:
other_cuda_device_selector(const device &dev) : excludeDevice{dev} {}

int operator()(const device &dev) const {
if (!isCudaDevice(dev)) {
return -1;
}
if (dev.get() == excludeDevice.get()) {
// Return only this device if it is the only available
return 0;
}
return 1;
}

private:
const device &excludeDevice;
};

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

try {
context context_;
} catch (device_error &e) {
std::cout << "Failed to create device for context" << std::endl;
}

deviceA_ = cuda_device_selector().select_device();
deviceB_ = other_cuda_device_selector(deviceA_).select_device();

ASSERT_TRUE(isCudaDevice(deviceA_));
deviceA_ = CudaDevices[0];
deviceB_ = CudaDevices.size() > 1 ? CudaDevices[1] : deviceA_;
}

void TearDown() override {}
};

TEST_F(CudaPrimaryContextTests, piSingleContext) {
TEST_P(CudaPrimaryContextTests, piSingleContext) {
std::cout << "create single context" << std::endl;
context Context(deviceA_, async_handler{}, /*UsePrimaryContext=*/true);

Expand All @@ -93,7 +50,7 @@ TEST_F(CudaPrimaryContextTests, piSingleContext) {
cuDevicePrimaryCtxRelease(CudaDevice);
}

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

TEST_F(CudaPrimaryContextTests, piMultiContextMultiDevice) {
TEST_P(CudaPrimaryContextTests, piMultiContextMultiDevice) {
if (deviceA_ == deviceB_)
return;

CUdevice CudaDeviceA = deviceA_.get_native<backend::cuda>();
CUdevice CudaDeviceB = deviceB_.get_native<backend::cuda>();

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

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

ASSERT_NE(CudaContextA, CudaContextB);
}
CUcontext CudaContextA = ContextA.get_native<backend::cuda>();
CUcontext CudaContextB = ContextB.get_native<backend::cuda>();

ASSERT_NE(CudaContextA, CudaContextB);
}

INSTANTIATE_TEST_CASE_P(
OnCudaPlatform, CudaPrimaryContextTests,
::testing::ValuesIn(pi::getPlatformsWithName("CUDA BACKEND")), );