Skip to content

Commit 39e7773

Browse files
authored
[SYCL] Fix memory leak for sycl::device::get_devices (#2256)
Plugins create pi_device objects for root devices, but it does not call piDeviceRelease and causes memory leaks. This slows the performance of SYCL apps. Signed-off-by: Byoungro So <byoungro.so@intel.com>
1 parent 414c1e5 commit 39e7773

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

sycl/source/detail/device_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ device_impl::device_impl(pi_native_handle InteropDeviceHandle,
7777
}
7878

7979
device_impl::~device_impl() {
80-
if (!MIsRootDevice && !MIsHostDevice) {
80+
if (!MIsHostDevice) {
8181
// TODO catch an exception and put it to list of asynchronous exceptions
8282
const detail::plugin &Plugin = getPlugin();
8383
RT::PiResult Err = Plugin.call_nocheck<PiApiKind::piDeviceRelease>(MDevice);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
3+
//
4+
// UNSUPPORTED: windows
5+
//
6+
//==-----memory-consumption.cpp - SYCL memory consumption basic test ------==//
7+
//
8+
// This test specifically tests that memory consumption does not change
9+
// when the get_devices() is called repeatedly.
10+
//
11+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
12+
// See https://llvm.org/LICENSE.txt for license information.
13+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "CL/sycl.hpp"
18+
#include <iostream>
19+
#include <thread>
20+
21+
#ifdef __linux__
22+
#include <strings.h>
23+
#include <sys/resource.h>
24+
#include <sys/time.h>
25+
26+
long get_cpu_mem() {
27+
struct rusage usage;
28+
memset(&usage, 0, sizeof(rusage));
29+
getrusage(RUSAGE_SELF, &usage);
30+
return usage.ru_maxrss;
31+
}
32+
#endif
33+
34+
using namespace cl::sycl;
35+
36+
int main() {
37+
constexpr auto dev_type = info::device_type::gpu;
38+
auto devices = device::get_devices(dev_type);
39+
40+
int startSize = get_cpu_mem();
41+
std::cout << startSize << " kb" << std::endl;
42+
43+
for (int i = 0; i < 1000; i++) {
44+
devices = cl::sycl::device::get_devices(dev_type);
45+
}
46+
int endSize = get_cpu_mem();
47+
std::cout << endSize << " kb" << std::endl;
48+
49+
auto plat = devices[0].get_platform();
50+
std::string plat_name = plat.get_info<info::platform::name>();
51+
std::cout << "Platform: " << plat_name << std::endl;
52+
53+
devices.erase(devices.begin(), devices.end());
54+
55+
if (startSize == endSize) {
56+
std::cout << "Passed" << std::endl;
57+
return 0;
58+
} else {
59+
std::cout << "Failed" << std::endl;
60+
return 1;
61+
}
62+
}

0 commit comments

Comments
 (0)