Skip to content

Remove unnecessary and confusing unique_ptr usage #17144

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
Feb 26, 2025
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
4 changes: 0 additions & 4 deletions unified-runtime/source/adapters/cuda/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ void assertion(bool Condition, const char *Message = nullptr);

namespace umf {

using cuda_params_unique_handle_t = std::unique_ptr<
umf_cuda_memory_provider_params_t,
std::function<umf_result_t(umf_cuda_memory_provider_params_handle_t)>>;

inline umf_result_t setCUMemoryProviderParams(
umf_cuda_memory_provider_params_handle_t CUMemoryProviderParams,
int cuDevice, void *cuContext, umf_usm_memory_type_t memType) {
Expand Down
17 changes: 7 additions & 10 deletions unified-runtime/source/adapters/cuda/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,25 @@ static ur_result_t
CreateHostMemoryProviderPool(ur_device_handle_t_ *DeviceHandle,
umf_memory_provider_handle_t *MemoryProviderHost,
umf_memory_pool_handle_t *MemoryPoolHost) {
umf_cuda_memory_provider_params_handle_t CUMemoryProviderParams = nullptr;

*MemoryProviderHost = nullptr;
CUcontext context = DeviceHandle->getNativeContext();

umf_cuda_memory_provider_params_handle_t CUMemoryProviderParams = nullptr;
umf_result_t UmfResult =
umfCUDAMemoryProviderParamsCreate(&CUMemoryProviderParams);
UMF_RETURN_UR_ERROR(UmfResult);
OnScopeExit Cleanup(
[=]() { umfCUDAMemoryProviderParamsDestroy(CUMemoryProviderParams); });

umf::cuda_params_unique_handle_t CUMemoryProviderParamsUnique(
CUMemoryProviderParams, umfCUDAMemoryProviderParamsDestroy);

UmfResult = umf::setCUMemoryProviderParams(CUMemoryProviderParamsUnique.get(),
0 /* cuDevice */, context,
UMF_MEMORY_TYPE_HOST);
UmfResult = umf::setCUMemoryProviderParams(
CUMemoryProviderParams, 0 /* cuDevice */, context, UMF_MEMORY_TYPE_HOST);
UMF_RETURN_UR_ERROR(UmfResult);

// create UMF CUDA memory provider and pool for the host memory
// (UMF_MEMORY_TYPE_HOST)
UmfResult = umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
CUMemoryProviderParamsUnique.get(),
MemoryProviderHost);
UmfResult = umfMemoryProviderCreate(
umfCUDAMemoryProviderOps(), CUMemoryProviderParams, MemoryProviderHost);
UMF_RETURN_UR_ERROR(UmfResult);

UmfResult = umfPoolCreate(umfProxyPoolOps(), *MemoryProviderHost, nullptr, 0,
Expand Down
18 changes: 8 additions & 10 deletions unified-runtime/source/adapters/cuda/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ CreateDeviceMemoryProvidersPools(ur_platform_handle_t_ *Platform) {
umfCUDAMemoryProviderParamsCreate(&CUMemoryProviderParams);
UMF_RETURN_UR_ERROR(UmfResult);

umf::cuda_params_unique_handle_t CUMemoryProviderParamsUnique(
CUMemoryProviderParams, umfCUDAMemoryProviderParamsDestroy);
OnScopeExit Cleanup(
[=]() { umfCUDAMemoryProviderParamsDestroy(CUMemoryProviderParams); });

for (auto &Device : Platform->Devices) {
ur_device_handle_t_ *device_handle = Device.get();
Expand All @@ -37,25 +37,23 @@ CreateDeviceMemoryProvidersPools(ur_platform_handle_t_ *Platform) {

// create UMF CUDA memory provider for the device memory
// (UMF_MEMORY_TYPE_DEVICE)
UmfResult =
umf::setCUMemoryProviderParams(CUMemoryProviderParamsUnique.get(),
device, context, UMF_MEMORY_TYPE_DEVICE);
UmfResult = umf::setCUMemoryProviderParams(CUMemoryProviderParams, device,
context, UMF_MEMORY_TYPE_DEVICE);
UMF_RETURN_UR_ERROR(UmfResult);

UmfResult = umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
CUMemoryProviderParamsUnique.get(),
CUMemoryProviderParams,
&device_handle->MemoryProviderDevice);
UMF_RETURN_UR_ERROR(UmfResult);

// create UMF CUDA memory provider for the shared memory
// (UMF_MEMORY_TYPE_SHARED)
UmfResult =
umf::setCUMemoryProviderParams(CUMemoryProviderParamsUnique.get(),
device, context, UMF_MEMORY_TYPE_SHARED);
UmfResult = umf::setCUMemoryProviderParams(CUMemoryProviderParams, device,
context, UMF_MEMORY_TYPE_SHARED);
UMF_RETURN_UR_ERROR(UmfResult);

UmfResult = umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
CUMemoryProviderParamsUnique.get(),
CUMemoryProviderParams,
&device_handle->MemoryProviderShared);
UMF_RETURN_UR_ERROR(UmfResult);

Expand Down
22 changes: 22 additions & 0 deletions unified-runtime/source/common/ur_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <sstream>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>

int ur_getpid(void);
Expand Down Expand Up @@ -484,6 +485,27 @@ template <typename T> class AtomicSingleton {
}
};

// A type that will call your function on exit of the current scope. Useful for
// avoiding leaks with C APIs.
// e.g.
//
// {
// OnScopeExit _([]() { puts("out of scope"); };
// }
//
// Decent compilers (and MSVC) will optimize the above to a simple call to puts
template <class Callable> struct OnScopeExit {
const Callable Fn;
OnScopeExit(Callable Fn) : Fn(Fn) {}
// We don't want copies because this should only ever call the user function
// once and we don't know where we might end up if we allow copies
OnScopeExit(OnScopeExit &) = delete;
OnScopeExit(OnScopeExit &&) = delete;
~OnScopeExit() { Fn(); }
};
// Silence a warning about unintended deduction
template <class Callable> OnScopeExit(Callable) -> OnScopeExit<Callable>;

template <typename Numeric>
static inline std::string groupDigits(Numeric numeric) {
auto number = std::to_string(numeric);
Expand Down
Loading