Skip to content

Commit

Permalink
[l0][HIP][CUDA] Prefer values over pointers-to
Browse files Browse the repository at this point in the history
Flags are 32bit here and none of these implementations functions need
modify their argument. Passing by reference is opaque, slower, and
superfluous in this case, so let's just pass by value.
  • Loading branch information
ldrumm committed Mar 20, 2024
1 parent ed1f8bf commit b78261c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 47 deletions.
24 changes: 12 additions & 12 deletions source/adapters/cuda/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,
UR_RESULT_ERROR_INVALID_VALUE);

if (!hPool) {
return USMHostAllocImpl(ppMem, hContext, nullptr, size, alignment);
return USMHostAllocImpl(ppMem, hContext, /* flags */ 0, size, alignment);
}

auto UMFPool = hPool->HostMemPool.get();
Expand All @@ -57,7 +57,7 @@ urUSMDeviceAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
UR_RESULT_ERROR_INVALID_VALUE);

if (!hPool) {
return USMDeviceAllocImpl(ppMem, hContext, hDevice, nullptr, size,
return USMDeviceAllocImpl(ppMem, hContext, hDevice, /* flags */ 0, size,
alignment);
}

Expand All @@ -82,8 +82,8 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
UR_RESULT_ERROR_INVALID_VALUE);

if (!hPool) {
return USMSharedAllocImpl(ppMem, hContext, hDevice, nullptr, nullptr, size,
alignment);
return USMSharedAllocImpl(ppMem, hContext, hDevice, /*host flags*/ 0,
/*device flags*/ 0, size, alignment);
}

auto UMFPool = hPool->SharedMemPool.get();
Expand Down Expand Up @@ -132,7 +132,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,
}

ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_device_handle_t, ur_usm_device_mem_flags_t *,
ur_device_handle_t, ur_usm_device_mem_flags_t,
size_t Size, uint32_t Alignment) {
try {
ScopedContext Active(Context);
Expand All @@ -151,8 +151,8 @@ ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t Context,
}

ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_device_handle_t, ur_usm_host_mem_flags_t *,
ur_usm_device_mem_flags_t *, size_t Size,
ur_device_handle_t, ur_usm_host_mem_flags_t,
ur_usm_device_mem_flags_t, size_t Size,
uint32_t Alignment) {
try {
ScopedContext Active(Context);
Expand All @@ -172,7 +172,7 @@ ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t Context,
}

ur_result_t USMHostAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_usm_host_mem_flags_t *, size_t Size,
ur_usm_host_mem_flags_t, size_t Size,
uint32_t Alignment) {
try {
ScopedContext Active(Context);
Expand Down Expand Up @@ -358,19 +358,19 @@ umf_result_t USMMemoryProvider::get_min_page_size(void *Ptr, size_t *PageSize) {

ur_result_t USMSharedMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMSharedAllocImpl(ResultPtr, Context, Device, nullptr, nullptr, Size,
Alignment);
return USMSharedAllocImpl(ResultPtr, Context, Device, /*host flags*/ 0,
/*device flags*/ 0, Size, Alignment);
}

ur_result_t USMDeviceMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMDeviceAllocImpl(ResultPtr, Context, Device, nullptr, Size,
return USMDeviceAllocImpl(ResultPtr, Context, Device, /* flags */ 0, Size,
Alignment);
}

ur_result_t USMHostMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMHostAllocImpl(ResultPtr, Context, nullptr, Size, Alignment);
return USMHostAllocImpl(ResultPtr, Context, /* flags */ 0, Size, Alignment);
}

ur_usm_pool_handle_t_::ur_usm_pool_handle_t_(ur_context_handle_t Context,
Expand Down
8 changes: 4 additions & 4 deletions source/adapters/cuda/usm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ class USMHostMemoryProvider final : public USMMemoryProvider {

ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_device_handle_t Device,
ur_usm_device_mem_flags_t *Flags, size_t Size,
ur_usm_device_mem_flags_t Flags, size_t Size,
uint32_t Alignment);

ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_device_handle_t Device,
ur_usm_host_mem_flags_t *,
ur_usm_device_mem_flags_t *, size_t Size,
ur_usm_host_mem_flags_t,
ur_usm_device_mem_flags_t, size_t Size,
uint32_t Alignment);

ur_result_t USMHostAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_usm_host_mem_flags_t *Flags, size_t Size,
ur_usm_host_mem_flags_t Flags, size_t Size,
uint32_t Alignment);
24 changes: 12 additions & 12 deletions source/adapters/hip/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,
UR_RESULT_ERROR_INVALID_VALUE);

if (!hPool) {
return USMHostAllocImpl(ppMem, hContext, nullptr, size, alignment);
return USMHostAllocImpl(ppMem, hContext, /* flags */ 0, size, alignment);
}

return umfPoolMallocHelper(hPool, ppMem, size, alignment);
Expand All @@ -43,7 +43,7 @@ urUSMDeviceAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
UR_RESULT_ERROR_INVALID_VALUE);

if (!hPool) {
return USMDeviceAllocImpl(ppMem, hContext, hDevice, nullptr, size,
return USMDeviceAllocImpl(ppMem, hContext, hDevice, /* flags */ 0, size,
alignment);
}

Expand All @@ -60,8 +60,8 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
UR_RESULT_ERROR_INVALID_VALUE);

if (!hPool) {
return USMSharedAllocImpl(ppMem, hContext, hDevice, nullptr, nullptr, size,
alignment);
return USMSharedAllocImpl(ppMem, hContext, hDevice, /*host flags*/ 0,
/*device flags*/ 0, size, alignment);
}

return umfPoolMallocHelper(hPool, ppMem, size, alignment);
Expand Down Expand Up @@ -105,7 +105,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,

ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t,
ur_device_handle_t Device,
ur_usm_device_mem_flags_t *, size_t Size,
ur_usm_device_mem_flags_t, size_t Size,
[[maybe_unused]] uint32_t Alignment) {
try {
ScopedContext Active(Device);
Expand All @@ -120,8 +120,8 @@ ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t,

ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t,
ur_device_handle_t Device,
ur_usm_host_mem_flags_t *,
ur_usm_device_mem_flags_t *, size_t Size,
ur_usm_host_mem_flags_t,
ur_usm_device_mem_flags_t, size_t Size,
[[maybe_unused]] uint32_t Alignment) {
try {
ScopedContext Active(Device);
Expand All @@ -136,7 +136,7 @@ ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t,

ur_result_t USMHostAllocImpl(void **ResultPtr,
[[maybe_unused]] ur_context_handle_t Context,
ur_usm_host_mem_flags_t *, size_t Size,
ur_usm_host_mem_flags_t, size_t Size,
[[maybe_unused]] uint32_t Alignment) {
try {
UR_CHECK_ERROR(hipHostMalloc(ResultPtr, Size));
Expand Down Expand Up @@ -302,19 +302,19 @@ umf_result_t USMMemoryProvider::get_min_page_size(void *Ptr, size_t *PageSize) {

ur_result_t USMSharedMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMSharedAllocImpl(ResultPtr, Context, Device, nullptr, nullptr, Size,
Alignment);
return USMSharedAllocImpl(ResultPtr, Context, Device, /*host flags*/ 0,
/*device flags*/ 0, Size, Alignment);
}

ur_result_t USMDeviceMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMDeviceAllocImpl(ResultPtr, Context, Device, nullptr, Size,
return USMDeviceAllocImpl(ResultPtr, Context, Device, /* flags */ 0, Size,
Alignment);
}

ur_result_t USMHostMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMHostAllocImpl(ResultPtr, Context, nullptr, Size, Alignment);
return USMHostAllocImpl(ResultPtr, Context, /* flags */ 0, Size, Alignment);
}

ur_usm_pool_handle_t_::ur_usm_pool_handle_t_(ur_context_handle_t Context,
Expand Down
8 changes: 4 additions & 4 deletions source/adapters/hip/usm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ class USMHostMemoryProvider final : public USMMemoryProvider {

ur_result_t USMDeviceAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_device_handle_t Device,
ur_usm_device_mem_flags_t *Flags, size_t Size,
ur_usm_device_mem_flags_t Flags, size_t Size,
uint32_t Alignment);

ur_result_t USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_device_handle_t Device,
ur_usm_host_mem_flags_t *,
ur_usm_device_mem_flags_t *, size_t Size,
ur_usm_host_mem_flags_t,
ur_usm_device_mem_flags_t, size_t Size,
uint32_t Alignment);

ur_result_t USMHostAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_usm_host_mem_flags_t *Flags, size_t Size,
ur_usm_host_mem_flags_t Flags, size_t Size,
uint32_t Alignment);

bool checkUSMAlignment(uint32_t &alignment, const ur_usm_desc_t *pUSMDesc);
Expand Down
27 changes: 12 additions & 15 deletions source/adapters/level_zero/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ static ur_result_t USMAllocationMakeResident(
static ur_result_t USMDeviceAllocImpl(void **ResultPtr,
ur_context_handle_t Context,
ur_device_handle_t Device,
ur_usm_device_mem_flags_t *Flags,
ur_usm_device_mem_flags_t Flags,
size_t Size, uint32_t Alignment) {
std::ignore = Flags;
// TODO: translate PI properties to Level Zero flags
Expand Down Expand Up @@ -212,12 +212,10 @@ static ur_result_t USMDeviceAllocImpl(void **ResultPtr,
return UR_RESULT_SUCCESS;
}

static ur_result_t USMSharedAllocImpl(void **ResultPtr,
ur_context_handle_t Context,
ur_device_handle_t Device,
ur_usm_host_mem_flags_t *,
ur_usm_device_mem_flags_t *, size_t Size,
uint32_t Alignment) {
static ur_result_t
USMSharedAllocImpl(void **ResultPtr, ur_context_handle_t Context,
ur_device_handle_t Device, ur_usm_host_mem_flags_t,
ur_usm_device_mem_flags_t, size_t Size, uint32_t Alignment) {

// TODO: translate PI properties to Level Zero flags
ZeStruct<ze_host_mem_alloc_desc_t> ZeHostDesc;
Expand Down Expand Up @@ -262,7 +260,7 @@ static ur_result_t USMSharedAllocImpl(void **ResultPtr,

static ur_result_t USMHostAllocImpl(void **ResultPtr,
ur_context_handle_t Context,
ur_usm_host_mem_flags_t *Flags, size_t Size,
ur_usm_host_mem_flags_t Flags, size_t Size,
uint32_t Alignment) {
std::ignore = Flags;
// TODO: translate PI properties to Level Zero flags
Expand Down Expand Up @@ -775,29 +773,28 @@ umf_result_t L0MemoryProvider::get_min_page_size(void *Ptr, size_t *PageSize) {

ur_result_t L0SharedMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMSharedAllocImpl(ResultPtr, Context, Device, nullptr, nullptr, Size,
Alignment);
return USMSharedAllocImpl(ResultPtr, Context, Device, /*host flags*/ 0,
/*device flags*/ 0, Size, Alignment);
}

ur_result_t L0SharedReadOnlyMemoryProvider::allocateImpl(void **ResultPtr,
size_t Size,
uint32_t Alignment) {
ur_usm_device_desc_t UsmDeviceDesc{};
UsmDeviceDesc.flags = UR_USM_DEVICE_MEM_FLAG_DEVICE_READ_ONLY;
ur_usm_host_desc_t UsmHostDesc{};
return USMSharedAllocImpl(ResultPtr, Context, Device, &UsmDeviceDesc.flags,
&UsmHostDesc.flags, Size, Alignment);
return USMSharedAllocImpl(ResultPtr, Context, Device, UsmDeviceDesc.flags,
/*host flags*/ 0, Size, Alignment);
}

ur_result_t L0DeviceMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMDeviceAllocImpl(ResultPtr, Context, Device, nullptr, Size,
return USMDeviceAllocImpl(ResultPtr, Context, Device, /* flags */ 0, Size,
Alignment);
}

ur_result_t L0HostMemoryProvider::allocateImpl(void **ResultPtr, size_t Size,
uint32_t Alignment) {
return USMHostAllocImpl(ResultPtr, Context, nullptr, Size, Alignment);
return USMHostAllocImpl(ResultPtr, Context, /* flags */ 0, Size, Alignment);
}

ur_usm_pool_handle_t_::ur_usm_pool_handle_t_(ur_context_handle_t Context,
Expand Down

0 comments on commit b78261c

Please sign in to comment.