Skip to content

Commit

Permalink
Merge pull request #911 from CHIP-SPV/cpu-usm
Browse files Browse the repository at this point in the history
Properly annotate Intel USM kernels
  • Loading branch information
pvelesko authored Aug 17, 2024
2 parents 4813847 + 14c74fd commit ab44488
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
34 changes: 26 additions & 8 deletions src/backend/OpenCL/CHIPBackendOpenCL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ annotateIndirectPointers(const CHIPContextOpenCL &Ctx,
AnnotationList.size() * sizeof(void *),
AnnotationList.data());
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetKernelExecInfo);

if (Ctx.getAllocStrategy() == AllocationStrategy::IntelUSM) {
cl_bool param = CL_TRUE;

Check warning on line 286 in src/backend/OpenCL/CHIPBackendOpenCL.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.cc:286:15 [readability-identifier-naming]

invalid case style for local variable 'param'
if (Ctx.MemManager_.isHostAllocUsed()) {
clStatus = clSetKernelExecInfo(
KernelAPIHandle, CL_KERNEL_EXEC_INFO_INDIRECT_HOST_ACCESS_INTEL,
sizeof(cl_bool), &param);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetKernelExecInfo);
}
if (Ctx.MemManager_.isDeviceAllocUsed()) {
clStatus = clSetKernelExecInfo(
KernelAPIHandle, CL_KERNEL_EXEC_INFO_INDIRECT_DEVICE_ACCESS_INTEL,
sizeof(cl_bool), &param);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetKernelExecInfo);
}
if (Ctx.MemManager_.isSharedAllocUsed()) {
clStatus = clSetKernelExecInfo(
KernelAPIHandle, CL_KERNEL_EXEC_INFO_INDIRECT_SHARED_ACCESS_INTEL,
sizeof(cl_bool), &param);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetKernelExecInfo);
}
}
}

return AllocKeepAlives;
Expand Down Expand Up @@ -1259,13 +1281,8 @@ CHIPQueueOpenCL::launchImpl(chipstar::ExecItem *ExecItem) {
LOCK(Backend->DubiousLockOpenCL);
#endif

std::unique_ptr<std::vector<std::shared_ptr<void>>> AllocationsToKeepAlive;
// Disable annotation for CPU device. Runtime errror.
// https://community.intel.com/t5/OpenCL-for-CPU/OpenCL-Using-USM-on-Intel-CPU-Runtime-along-with/m-p/1621136#M7350
if (ChipEnvVars.getDevice().getType() != DeviceType::CPU) {
AllocationsToKeepAlive = annotateIndirectPointers(
*OclContext, Kernel->getModule()->getInfo(), KernelHandle);
}
auto AllocationsToKeepAlive = annotateIndirectPointers(
*OclContext, Kernel->getModule()->getInfo(), KernelHandle);

auto [EventsToWait, EventLocks] = getSyncQueuesLastEvents(LaunchEvent, false);
std::vector<cl_event> SyncQueuesEventHandles = getOpenCLHandles(EventsToWait);
Expand Down Expand Up @@ -1891,7 +1908,8 @@ void CHIPBackendOpenCL::initializeImpl() {
std::vector<cl::Device> SupportedDevices;
std::vector<cl::Device> Dev;
clStatus = SelectedPlatform.getDevices(SelectedDevType, &Dev);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clGetPlatformIDs);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(
clGetDeviceIDs); // C equivalent of getDevices

for (auto D : Dev) {

Expand Down
10 changes: 10 additions & 0 deletions src/backend/OpenCL/CHIPBackendOpenCL.hh
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ private:
/// The allocation strategy to use.
AllocationStrategy AllocStrategy_;

// Add these three bools
bool hostAllocUsed;

Check warning on line 192 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:192:8 [readability-identifier-naming]

invalid case style for private member 'hostAllocUsed'
bool deviceAllocUsed;

Check warning on line 193 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:193:8 [readability-identifier-naming]

invalid case style for private member 'deviceAllocUsed'
bool sharedAllocUsed;

Check warning on line 194 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:194:8 [readability-identifier-naming]

invalid case style for private member 'sharedAllocUsed'

public:
void init(CHIPContextOpenCL *ChipCtxCl);
MemoryManager &operator=(MemoryManager &&Rhs);
Expand All @@ -212,6 +217,11 @@ public:
return AllocStrategy_;
}

// Add getter methods for the new bools
bool isHostAllocUsed() const { return hostAllocUsed; }

Check warning on line 221 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:221:3 [modernize-use-nodiscard]

function 'isHostAllocUsed' should be marked [[nodiscard]]

Check warning on line 221 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:221:8 [modernize-use-trailing-return-type]

use a trailing return type for this function
bool isDeviceAllocUsed() const { return deviceAllocUsed; }

Check warning on line 222 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:222:3 [modernize-use-nodiscard]

function 'isDeviceAllocUsed' should be marked [[nodiscard]]

Check warning on line 222 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:222:8 [modernize-use-trailing-return-type]

use a trailing return type for this function
bool isSharedAllocUsed() const { return sharedAllocUsed; }

Check warning on line 223 in src/backend/OpenCL/CHIPBackendOpenCL.hh

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/CHIPBackendOpenCL.hh:223:3 [modernize-use-nodiscard]

function 'isSharedAllocUsed' should be marked [[nodiscard]]

std::pair<cl_mem, size_t> translateDevPtrToBuffer(const void *DevPtr) const;

private:
Expand Down
26 changes: 25 additions & 1 deletion src/backend/OpenCL/MemoryManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ void MemoryManager::init(CHIPContextOpenCL *ChipCtxCl_) {
} else {
std::memset(&USM_, 0, sizeof(USM_));
}

// Initialize the allocation usage bools
hostAllocUsed = false;
deviceAllocUsed = false;
sharedAllocUsed = false;
}

MemoryManager &MemoryManager::operator=(MemoryManager &&Rhs) {
Expand Down Expand Up @@ -241,6 +246,25 @@ void *MemoryManager::allocate(size_t Size, size_t Alignment,
logTrace("Memory allocated: {} / {}\n", Ptr.get(), Size);
assert(Allocations_.find(Ptr) == Allocations_.end());
Allocations_.emplace(Ptr, Size);

// Set the appropriate bool based on the MemType
switch (MemType) {
case hipMemoryTypeHost:
hostAllocUsed = true;
break;
case hipMemoryTypeDevice:
deviceAllocUsed = true;
break;
case hipMemoryTypeManaged:
case hipMemoryTypeUnified:
sharedAllocUsed = true;
break;
default:
// Handle unexpected memory type
assert(!"Unexpected memory type!");
break;
}

return Ptr.get();
}

Expand Down Expand Up @@ -320,4 +344,4 @@ MemoryManager::translateDevPtrToBuffer(const void *DevPtr) const {
}

return {DevPtrToBuffer_.at(BasePtr), Offset};
}
}
8 changes: 2 additions & 6 deletions src/backend/OpenCL/clHipErrorConversion.hh
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ const std::unordered_map<void *, cl_hip_error_map_t> CL_HIP_ERROR_MAPS = {
{CL_OUT_OF_RESOURCES, hipErrorOutOfMemory},
{CL_MEM_OBJECT_ALLOCATION_FAILURE, hipErrorOutOfMemory},
{CL_INVALID_EVENT_WAIT_LIST, hipErrorInvalidResourceHandle},
{CL_OUT_OF_HOST_MEMORY, hipErrorOutOfMemory}}},
{CL_OUT_OF_HOST_MEMORY, hipErrorOutOfMemory},
{CL_INVALID_OPERATION, hipErrorInvalidValue}}},

{(void *)&clEnqueueReadBuffer,
{{CL_SUCCESS, hipSuccess},
Expand Down Expand Up @@ -447,11 +448,6 @@ const std::unordered_map<void *, cl_hip_error_map_t> CL_HIP_ERROR_MAPS = {
{CL_INVALID_EVENT_WAIT_LIST, hipErrorInvalidResourceHandle},
{CL_OUT_OF_HOST_MEMORY, hipErrorOutOfMemory}}},

{(void *)&clGetPlatformIDs,
{{CL_SUCCESS, hipSuccess},
{CL_INVALID_VALUE, hipErrorInvalidValue},
{CL_OUT_OF_HOST_MEMORY, hipErrorOutOfMemory}}},

{(void *)&clCreateProgramWithIL,
{{CL_SUCCESS, hipSuccess},
{CL_INVALID_CONTEXT, hipErrorInvalidResourceHandle},
Expand Down

0 comments on commit ab44488

Please sign in to comment.