Skip to content

[SYCL] Fix memory leak for sycl::device::get_devices #2256

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
Aug 5, 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
2 changes: 1 addition & 1 deletion sycl/source/detail/device_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ device_impl::device_impl(pi_native_handle InteropDeviceHandle,
}

device_impl::~device_impl() {
if (!MIsRootDevice && !MIsHostDevice) {
if (!MIsHostDevice) {
// TODO catch an exception and put it to list of asynchronous exceptions
const detail::plugin &Plugin = getPlugin();
RT::PiResult Err = Plugin.call_nocheck<PiApiKind::piDeviceRelease>(MDevice);
Expand Down
62 changes: 62 additions & 0 deletions sycl/test/basic_tests/memory-consumption.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
//
// UNSUPPORTED: windows
//
//==-----memory-consumption.cpp - SYCL memory consumption basic test ------==//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test is valuable to check overall memory consumption. But I suggest adding more specific test on the change you are doing, such a verification could be done by setting SYCL_PI_TRACE and checking its output(see sycl/test/scheduler/ReleaseResourcesTest.cpp as an example).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make a follow-up patch for this SYCL_PI_TRACE later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK if additional test will be added in the follow up patch.

//
// This test specifically tests that memory consumption does not change
// when the get_devices() is called repeatedly.
//
// 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
//
//===----------------------------------------------------------------------===//

#include "CL/sycl.hpp"
#include <iostream>
#include <thread>

#ifdef __linux__
#include <strings.h>
#include <sys/resource.h>
#include <sys/time.h>

long get_cpu_mem() {
struct rusage usage;
memset(&usage, 0, sizeof(rusage));
getrusage(RUSAGE_SELF, &usage);
return usage.ru_maxrss;
}
#endif

using namespace cl::sycl;

int main() {
constexpr auto dev_type = info::device_type::gpu;
auto devices = device::get_devices(dev_type);

int startSize = get_cpu_mem();
std::cout << startSize << " kb" << std::endl;

for (int i = 0; i < 1000; i++) {
devices = cl::sycl::device::get_devices(dev_type);
}
int endSize = get_cpu_mem();
std::cout << endSize << " kb" << std::endl;

auto plat = devices[0].get_platform();
std::string plat_name = plat.get_info<info::platform::name>();
std::cout << "Platform: " << plat_name << std::endl;

devices.erase(devices.begin(), devices.end());

if (startSize == endSize) {
std::cout << "Passed" << std::endl;
return 0;
} else {
std::cout << "Failed" << std::endl;
return 1;
}
}