Skip to content
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
8 changes: 8 additions & 0 deletions sycl/unittests/context_device/DeviceRefCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <detail/global_handler.hpp>
#include <gtest/gtest.h>
#include <helpers/UrMock.hpp>
#include <sycl/sycl.hpp>
Expand All @@ -31,6 +32,7 @@ static ur_result_t redefinedDeviceReleaseAfter(void *) {
TEST(DevRefCounter, DevRefCounter) {
{
sycl::unittest::UrMock<> Mock;
EXPECT_EQ(DevRefCounter, 0);

mock::getCallbacks().set_after_callback("urDeviceGet",
&redefinedDevicesGetAfter);
Expand All @@ -41,6 +43,12 @@ TEST(DevRefCounter, DevRefCounter) {
sycl::platform Plt = sycl::platform();

Plt.get_devices();
EXPECT_NE(DevRefCounter, 0);
// This is the behavior that SYCL performs at shutdown, but there
// are timing differences Lin/Win and shared/static that make
// it not map correctly into our mock.
// So for this test, we just do it.
sycl::detail::GlobalHandler::instance().getPlatformCache().clear();
}
EXPECT_EQ(DevRefCounter, 0);
}
20 changes: 12 additions & 8 deletions sycl/unittests/helpers/UrMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,16 @@ template <sycl::backend Backend = backend::opencl> class UrMock {

sycl::detail::ur::initializeUr(UrLoaderConfig);
urLoaderConfigRelease(UrLoaderConfig);

// We clear platform cache for each test run, so that tests can have a
// different backend. This forces platforms to be reconstructed (and thus
// queries about UR backend info to be called again) This also erases each
// platform's devices (normally done in the library shutdown) so that
// platforms/devices' lifetimes could work in unittests scenario. In SYCL,
// this is normally done at shutdown, but between Win/Lin and static/shared
// differences, there is no correct parallel in the ~UrMock destructor.
// Instead we do it here. Simple and clean.
detail::GlobalHandler::instance().getPlatformCache().clear();
}

UrMock(UrMock<Backend> &&Other) = delete;
Expand All @@ -606,14 +616,8 @@ template <sycl::backend Backend = backend::opencl> class UrMock {
// these between tests
detail::GlobalHandler::instance().prepareSchedulerToRelease(true);
detail::GlobalHandler::instance().releaseDefaultContexts();
// clear platform cache in case subsequent tests want a different backend,
// this forces platforms to be reconstructed (and thus queries about UR
// backend info to be called again)
//
// This also erases each platform's devices (normally done in the library
// shutdown) so that platforms/devices' lifetimes could work in unittests
// scenario.
detail::GlobalHandler::instance().getPlatformCache().clear();
// the platform cache is cleared at the BEGINING of the mock.

mock::getCallbacks().resetCallbacks();
}

Expand Down
Loading