Skip to content

Commit

Permalink
proper annonatete
Browse files Browse the repository at this point in the history
  • Loading branch information
pvelesko committed Aug 16, 2024
1 parent fc6d412 commit 04990b1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
42 changes: 34 additions & 8 deletions src/backend/OpenCL/CHIPBackendOpenCL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ annotateIndirectPointers(const CHIPContextOpenCL &Ctx,
return nullptr;

cl_kernel_exec_info PtrListName;
cl_kernel_exec_info AllocType;

switch (Ctx.getAllocStrategy()) {
default:
assert(!"Unexpected allocation strategy!");
Expand Down Expand Up @@ -273,6 +275,21 @@ annotateIndirectPointers(const CHIPContextOpenCL &Ctx,
for (std::shared_ptr<void> Ptr : Ctx.getAllocPointers()) {
AnnotationList.push_back(Ptr.get());
AllocKeepAlives->push_back(Ptr);

switch (Ctx.MemManager_.getPointerMemoryType(Ptr.get())) {
case hipMemoryTypeHost:
AllocType = CL_KERNEL_EXEC_INFO_INDIRECT_HOST_ACCESS_INTEL;
break;
case hipMemoryTypeDevice:
AllocType = CL_KERNEL_EXEC_INFO_INDIRECT_DEVICE_ACCESS_INTEL;
break;
case hipMemoryTypeUnified:
AllocType = CL_KERNEL_EXEC_INFO_INDIRECT_SHARED_ACCESS_INTEL;
break;
default:
assert(!"Unsupported memory type.");
return nullptr;
}
}

// TODO: Optimization. Don't call this function again if we know the
Expand All @@ -281,6 +298,19 @@ 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 303 in src/backend/OpenCL/CHIPBackendOpenCL.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

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

invalid case style for local variable 'param'
clStatus = clSetKernelExecInfo(KernelAPIHandle, CL_KERNEL_EXEC_INFO_INDIRECT_HOST_ACCESS_INTEL,
sizeof(cl_bool), &param);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetKernelExecInfo);
clStatus = clSetKernelExecInfo(KernelAPIHandle, CL_KERNEL_EXEC_INFO_INDIRECT_DEVICE_ACCESS_INTEL,
sizeof(cl_bool), &param);
CHIPERR_CHECK_LOG_AND_THROW_TABLE(clSetKernelExecInfo);
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 +1289,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 +1916,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
13 changes: 9 additions & 4 deletions src/backend/OpenCL/CHIPBackendOpenCL.hh
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ struct CHIPContextUSMExts {
clMemFreeINTEL_fn clMemFreeINTEL;
};

using const_alloc_iterator = ConstMapKeyIterator<
std::map<std::shared_ptr<void>, size_t, PointerCmp<void>>>;
using const_alloc_iterator = ConstMapKeyIterator<std::map<
std::shared_ptr<void>, std::pair<size_t, hipMemoryType>, PointerCmp<void>>>;

/// Used to select a strategy for managing HIP allocations.
enum class AllocationStrategy {
Expand All @@ -173,7 +173,9 @@ enum class AllocationStrategy {
class MemoryManager {
private:
// ContextMutex should be enough
std::map<std::shared_ptr<void>, size_t, PointerCmp<void>> Allocations_;
std::map<std::shared_ptr<void>, std::pair<size_t, hipMemoryType>,
PointerCmp<void>>
Allocations_;
cl::Context Context_;
cl::Device Device_;

Expand All @@ -195,7 +197,8 @@ public:
bool free(void *P);
bool hasPointer(const void *Ptr);
bool pointerSize(void *Ptr, size_t *Size);
bool pointerInfo(void *Ptr, void **Base, size_t *Size);
bool pointerInfo(void *Ptr, void **Base, size_t *Size,

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

use a trailing return type for this function
hipMemoryType *MemType);
int memCopy(void *Dst, const void *Src, size_t Size, cl::CommandQueue &Queue);

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

use a trailing return type for this function
int memFill(void *Dst, size_t Size, const void *Pattern, size_t PatternSize,
cl::CommandQueue &Queue);
Expand All @@ -212,6 +215,8 @@ public:
return AllocStrategy_;
}

hipMemoryType getPointerMemoryType(const void *Ptr) const;

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

use a trailing return type for this function

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

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

View workflow job for this annotation

GitHub Actions / cpp-linter

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

use a trailing return type for this function

private:
Expand Down
51 changes: 30 additions & 21 deletions src/backend/OpenCL/MemoryManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ std::shared_ptr<void> MemoryManager::allocateSVM(size_t Size, size_t Alignment,
return Result;
}

hipMemoryType MemoryManager::getPointerMemoryType(const void *Ptr) const {

Check warning on line 138 in src/backend/OpenCL/MemoryManager.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/MemoryManager.cc:138:30 [modernize-use-trailing-return-type]

use a trailing return type for this function

Check warning on line 138 in src/backend/OpenCL/MemoryManager.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/MemoryManager.cc:138:30 [readability-convert-member-functions-to-static]

method 'getPointerMemoryType' can be made static
auto I = Allocations_.find(

Check warning on line 139 in src/backend/OpenCL/MemoryManager.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/MemoryManager.cc:139:8 [readability-identifier-length]

variable name 'I' is too short, expected at least 3 characters
std::shared_ptr<void>(const_cast<void *>(Ptr), [](void *) {}));
if (I != Allocations_.end()) {
return I->second.second;
}
// Abort the function if the pointer is not found in our allocations
CHIPERR_LOG_AND_ABORT("Pointer not found in allocations");
}

std::shared_ptr<void> MemoryManager::allocateUSM(size_t Size, size_t Alignment,

Check warning on line 148 in src/backend/OpenCL/MemoryManager.cc

View workflow job for this annotation

GitHub Actions / cpp-linter

src/backend/OpenCL/MemoryManager.cc:148:38 [modernize-use-trailing-return-type]

use a trailing return type for this function
hipMemoryType MemType) {
std::shared_ptr<void> Result;
Expand Down Expand Up @@ -240,12 +250,12 @@ 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);
Allocations_.emplace(Ptr, std::make_pair(Size, MemType));
return Ptr.get();
}

bool MemoryManager::free(void *Ptr) {
auto I = Allocations_.find(Ptr);
auto I = Allocations_.find(std::shared_ptr<void>(Ptr, [](void *) {}));
if (I != Allocations_.end())
Allocations_.erase(I);

Expand All @@ -257,33 +267,33 @@ bool MemoryManager::free(void *Ptr) {

bool MemoryManager::hasPointer(const void *Ptr) {
logTrace("hasPointer on: {}\n", Ptr);
return (Allocations_.find((void *)Ptr) != Allocations_.end());
return (Allocations_.find(std::shared_ptr<void>(
const_cast<void *>(Ptr), [](void *) {})) != Allocations_.end());
}

bool MemoryManager::pointerSize(void *Ptr, size_t *Size) {
logTrace("pointerSize on: {}\n", Ptr);
auto I = Allocations_.find(Ptr);
auto I = Allocations_.find(std::shared_ptr<void>(Ptr, [](void *) {}));
if (I != Allocations_.end()) {
*Size = I->second;
*Size = I->second.first;
return true;
} else {
return false;
}
}

bool MemoryManager::pointerInfo(void *Ptr, void **Base, size_t *Size) {
bool MemoryManager::pointerInfo(void *Ptr, void **Base, size_t *Size,
hipMemoryType *MemType) {
logTrace("pointerInfo on: {}\n", Ptr);
for (auto I : Allocations_) {
if ((I.first.get() <= Ptr) &&
(Ptr < ((const char *)I.first.get() + I.second))) {
if (Base)
*Base = I.first.get();
if (Size)
*Size = I.second;
return true;
}
auto I = Allocations_.find(std::shared_ptr<void>(Ptr, [](void *) {}));
if (I != Allocations_.end()) {
*Base = I->first.get();
*Size = I->second.first;
*MemType = I->second.second;
return true;
} else {
return false;
}
return false;
}

void MemoryManager::clear() {
Expand All @@ -302,22 +312,21 @@ std::pair<cl_mem, size_t>
MemoryManager::translateDevPtrToBuffer(const void *DevPtr) const {
assert(getAllocStrategy() == AllocationStrategy::BufferDevAddr);

// Find entry with largest key so that 'key <= DevPtr'.
auto UpperIt = Allocations_.upper_bound(DevPtr);
auto UpperIt = Allocations_.upper_bound(
std::shared_ptr<void>(const_cast<void *>(DevPtr), [](void *) {}));
auto CandIt = UpperIt == Allocations_.begin() ? UpperIt : std::prev(UpperIt);
if (CandIt == Allocations_.end() || DevPtr < CandIt->first.get())
return {nullptr, 0};

const char *BasePtr = static_cast<const char *>(CandIt->first.get());
size_t Offset = 0;

// Handle offseted pointer, check it is within the bounds of the allocation.
if (BasePtr != DevPtr) {
const char *CandPtr = static_cast<const char *>(DevPtr);
if (CandPtr >= (BasePtr + CandIt->second))
if (CandPtr >= (BasePtr + CandIt->second.first))
return {nullptr, 0};
Offset = CandPtr - BasePtr;
}

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 04990b1

Please sign in to comment.