Skip to content

[SYCL][CUDA] Fix context clearing in PiCuda tests #4483

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 2 commits into from
Sep 16, 2021
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
20 changes: 20 additions & 0 deletions sycl/unittests/pi/cuda/CudaUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 <cuda.h>

namespace pi {

// utility function to clear the CUDA context stack
inline void clearCudaContext() {
CUcontext ctxt = nullptr;
do {
cuCtxSetCurrent(nullptr);
cuCtxGetCurrent(&ctxt);
} while (ctxt != nullptr);
}

} // namespace pi
3 changes: 2 additions & 1 deletion sycl/unittests/pi/cuda/test_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <cuda.h>

#include "CudaUtils.hpp"
#include "TestGetPlugin.hpp"
#include <CL/sycl.hpp>
#include <CL/sycl/detail/pi.hpp>
Expand All @@ -34,7 +35,7 @@ struct CudaCommandsTest : public ::testing::Test {
GTEST_SKIP();
}

cuCtxSetCurrent(nullptr);
pi::clearCudaContext();
pi_uint32 numPlatforms = 0;
ASSERT_EQ(plugin->getBackend(), backend::cuda);

Expand Down
5 changes: 3 additions & 2 deletions sycl/unittests/pi/cuda/test_contexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <cuda.h>

#include "CudaUtils.hpp"
#include "TestGetPlugin.hpp"
#include <CL/sycl.hpp>
#include <CL/sycl/detail/pi.hpp>
Expand Down Expand Up @@ -63,7 +64,7 @@ struct CudaContextsTest : public ::testing::Test {

TEST_F(CudaContextsTest, ContextLifetime) {
// start with no active context
cuCtxSetCurrent(nullptr);
pi::clearCudaContext();

// create a context
pi_context context;
Expand Down Expand Up @@ -149,7 +150,7 @@ TEST_F(CudaContextsTest, ContextLifetimeExisting) {
// still able to work correctly in that thread.
TEST_F(CudaContextsTest, ContextThread) {
// start with no active context
cuCtxSetCurrent(nullptr);
pi::clearCudaContext();

// create two PI contexts
pi_context context1;
Expand Down
3 changes: 2 additions & 1 deletion sycl/unittests/pi/cuda/test_mem_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <cuda.h>

#include "CudaUtils.hpp"
#include "TestGetPlugin.hpp"
#include <CL/sycl.hpp>
#include <CL/sycl/detail/cuda_definitions.hpp>
Expand All @@ -34,7 +35,7 @@ struct CudaTestMemObj : public ::testing::Test {
GTEST_SKIP();
}

cuCtxSetCurrent(nullptr);
pi::clearCudaContext();
pi_uint32 numPlatforms = 0;
ASSERT_EQ(plugin->getBackend(), backend::cuda);

Expand Down