Skip to content

Commit 66532d8

Browse files
committed
[CUDA] Implement urKernelSuggestMaxCooperativeGroupCountExp for Cuda
1 parent 25157af commit 66532d8

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

source/adapters/cuda/device.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
5757
return ReturnValue(4318u);
5858
}
5959
case UR_DEVICE_INFO_MAX_COMPUTE_UNITS: {
60-
int ComputeUnits = 0;
61-
UR_CHECK_ERROR(cuDeviceGetAttribute(
62-
&ComputeUnits, CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT,
63-
hDevice->get()));
64-
detail::ur::assertion(ComputeUnits >= 0);
65-
return ReturnValue(static_cast<uint32_t>(ComputeUnits));
60+
return ReturnValue(hDevice->getNumComputeUnits());
6661
}
6762
case UR_DEVICE_INFO_MAX_WORK_ITEM_DIMENSIONS: {
6863
return ReturnValue(MaxWorkItemDimensions);

source/adapters/cuda/device.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct ur_device_handle_t_ {
3232
int MaxCapacityLocalMem{0};
3333
int MaxChosenLocalMem{0};
3434
bool MaxLocalMemSizeChosen{false};
35+
uint32_t NumComputeUnits{0};
3536

3637
public:
3738
ur_device_handle_t_(native_type cuDevice, CUcontext cuContext, CUevent evBase,
@@ -54,6 +55,10 @@ struct ur_device_handle_t_ {
5455
sizeof(MaxWorkGroupSize), &MaxWorkGroupSize,
5556
nullptr));
5657

58+
UR_CHECK_ERROR(cuDeviceGetAttribute(
59+
reinterpret_cast<int *>(&NumComputeUnits),
60+
CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT, cuDevice));
61+
5762
// Set local mem max size if env var is present
5863
static const char *LocalMemSizePtrUR =
5964
std::getenv("UR_CUDA_MAX_LOCAL_MEM_SIZE");
@@ -107,6 +112,8 @@ struct ur_device_handle_t_ {
107112
int getMaxChosenLocalMem() const noexcept { return MaxChosenLocalMem; };
108113

109114
bool maxLocalMemSizeChosen() { return MaxLocalMemSizeChosen; };
115+
116+
uint32_t getNumComputeUnits() const noexcept { return NumComputeUnits; };
110117
};
111118

112119
int getAttribute(ur_device_handle_t Device, CUdevice_attribute Attribute);

source/adapters/cuda/kernel.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "memory.hpp"
1414
#include "queue.hpp"
1515
#include "sampler.hpp"
16+
#include "ur_api.h"
1617

1718
UR_APIEXPORT ur_result_t UR_APICALL
1819
urKernelCreate(ur_program_handle_t hProgram, const char *pKernelName,
@@ -167,10 +168,25 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelGetNativeHandle(
167168
UR_APIEXPORT ur_result_t UR_APICALL urKernelSuggestMaxCooperativeGroupCountExp(
168169
ur_kernel_handle_t hKernel, size_t localWorkSize,
169170
size_t dynamicSharedMemorySize, uint32_t *pGroupCountRet) {
170-
(void)hKernel;
171-
(void)localWorkSize;
172-
(void)dynamicSharedMemorySize;
173-
*pGroupCountRet = 1;
171+
UR_ASSERT(hKernel, UR_RESULT_ERROR_INVALID_KERNEL);
172+
173+
// We need to set the active current device for this kernel explicitly here,
174+
// because the occupancy querying API does not take device parameter.
175+
ur_device_handle_t Device = hKernel->getProgram()->getDevice();
176+
ScopedContext Active(Device);
177+
try {
178+
int MaxNumActiveGroupsPerCU{0};
179+
UR_CHECK_ERROR(cuOccupancyMaxActiveBlocksPerMultiprocessor(
180+
&MaxNumActiveGroupsPerCU, hKernel->get(), localWorkSize,
181+
dynamicSharedMemorySize));
182+
detail::ur::assertion(MaxNumActiveGroupsPerCU >= 0);
183+
184+
// Multiply by the number of SMs (CUs = compute units) on the device in
185+
// order to retreive the total number of groups/blocks that can be launched.
186+
*pGroupCountRet = Device->getNumComputeUnits() * MaxNumActiveGroupsPerCU;
187+
} catch (ur_result_t Err) {
188+
return Err;
189+
}
174190
return UR_RESULT_SUCCESS;
175191
}
176192

0 commit comments

Comments
 (0)