1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/*
* Copyright (C) 2017-2020 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "opencl/source/mem_obj/mem_obj.h"
#include "shared/source/command_stream/command_stream_receiver.h"
#include "shared/source/gmm_helper/gmm.h"
#include "shared/source/gmm_helper/resource_info.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/bit_helpers.h"
#include "shared/source/helpers/get_info.h"
#include "shared/source/memory_manager/deferred_deleter.h"
#include "shared/source/memory_manager/internal_allocation_storage.h"
#include "shared/source/memory_manager/memory_manager.h"
#include "shared/source/os_interface/os_context.h"
#include "opencl/source/cl_device/cl_device.h"
#include "opencl/source/command_queue/command_queue.h"
#include "opencl/source/context/context.h"
#include "opencl/source/helpers/get_info_status_mapper.h"
#include <algorithm>
namespace NEO {
MemObj::MemObj(Context *context,
cl_mem_object_type memObjectType,
const MemoryProperties &memoryProperties,
cl_mem_flags flags,
cl_mem_flags_intel flagsIntel,
size_t size,
void *memoryStorage,
void *hostPtr,
MultiGraphicsAllocation multiGraphicsAllocation,
bool zeroCopy,
bool isHostPtrSVM,
bool isObjectRedescribed)
: context(context), memObjectType(memObjectType), memoryProperties(memoryProperties), flags(flags), flagsIntel(flagsIntel), size(size),
memoryStorage(memoryStorage), hostPtr(hostPtr),
isZeroCopy(zeroCopy), isHostPtrSVM(isHostPtrSVM), isObjectRedescribed(isObjectRedescribed),
multiGraphicsAllocation(std::move(multiGraphicsAllocation)),
mapAllocations(static_cast<uint32_t>(this->multiGraphicsAllocation.getGraphicsAllocations().size() - 1)) {
if (context) {
context->incRefInternal();
memoryManager = context->getMemoryManager();
auto device = context->getDevice(0);
executionEnvironment = device->getExecutionEnvironment();
}
}
MemObj::~MemObj() {
if (!context) {
return;
}
bool needWait = false;
if (allocatedMapPtr != nullptr) {
needWait = true;
}
if (mapOperationsHandler.size() > 0 && !getCpuAddressForMapping()) {
needWait = true;
}
if (!destructorCallbacks.empty()) {
needWait = true;
}
if (!isObjectRedescribed) {
if (peekSharingHandler()) {
peekSharingHandler()->releaseReusedGraphicsAllocation();
}
for (auto graphicsAllocation : multiGraphicsAllocation.getGraphicsAllocations()) {
auto rootDeviceIndex = graphicsAllocation ? graphicsAllocation->getRootDeviceIndex() : 0;
bool doAsyncDestructions = DebugManager.flags.EnableAsyncDestroyAllocations.get();
if (graphicsAllocation && !associatedMemObject && !isHostPtrSVM && graphicsAllocation->peekReuseCount() == 0) {
memoryManager->removeAllocationFromHostPtrManager(graphicsAllocation);
if (!doAsyncDestructions) {
needWait = true;
}
if (needWait && graphicsAllocation->isUsed()) {
memoryManager->waitForEnginesCompletion(*graphicsAllocation);
}
destroyGraphicsAllocation(graphicsAllocation, doAsyncDestructions);
graphicsAllocation = nullptr;
}
if (!associatedMemObject) {
releaseMapAllocation(rootDeviceIndex, doAsyncDestructions);
}
if (mcsAllocation) {
destroyGraphicsAllocation(mcsAllocation, false);
}
if (graphicsAllocation && associatedMemObject) {
if (associatedMemObject->getGraphicsAllocation(graphicsAllocation->getRootDeviceIndex()) != graphicsAllocation) {
destroyGraphicsAllocation(graphicsAllocation, false);
}
associatedMemObject->decRefInternal();
}
}
if (!associatedMemObject) {
releaseAllocatedMapPtr();
}
}
for (auto callback : destructorCallbacks) {
callback->invoke(this);
delete callback;
}
context->decRefInternal();
}
cl_int MemObj::getMemObjectInfo(cl_mem_info paramName,
size_t paramValueSize,
void *paramValue,
size_t *paramValueSizeRet) {
cl_int retVal;
size_t srcParamSize = GetInfo::invalidSourceSize;
void *srcParam = nullptr;
cl_bool usesSVMPointer;
cl_uint refCnt = 0;
cl_uint mapCount = 0;
cl_mem clAssociatedMemObject = static_cast<cl_mem>(this->associatedMemObject);
cl_context ctx = nullptr;
uint64_t internalHandle = 0llu;
auto allocation = getMultiGraphicsAllocation().getDefaultGraphicsAllocation();
cl_bool usesCompression = allocation->getAllocationType() == GraphicsAllocation::AllocationType::BUFFER_COMPRESSED;
switch (paramName) {
case CL_MEM_TYPE:
srcParamSize = sizeof(memObjectType);
srcParam = &memObjectType;
break;
case CL_MEM_FLAGS:
srcParamSize = sizeof(flags);
srcParam = &flags;
break;
case CL_MEM_SIZE:
srcParamSize = sizeof(size);
srcParam = &size;
break;
case CL_MEM_HOST_PTR:
srcParamSize = sizeof(hostPtr);
srcParam = &hostPtr;
break;
case CL_MEM_CONTEXT:
srcParamSize = sizeof(context);
ctx = context;
srcParam = &ctx;
break;
case CL_MEM_USES_SVM_POINTER:
usesSVMPointer = isHostPtrSVM && isValueSet(flags, CL_MEM_USE_HOST_PTR);
srcParamSize = sizeof(cl_bool);
srcParam = &usesSVMPointer;
break;
case CL_MEM_OFFSET:
srcParamSize = sizeof(offset);
srcParam = &offset;
break;
case CL_MEM_ASSOCIATED_MEMOBJECT:
srcParamSize = sizeof(clAssociatedMemObject);
srcParam = &clAssociatedMemObject;
break;
case CL_MEM_MAP_COUNT:
srcParamSize = sizeof(mapCount);
mapCount = static_cast<cl_uint>(mapOperationsHandler.size());
srcParam = &mapCount;
break;
case CL_MEM_REFERENCE_COUNT:
refCnt = static_cast<cl_uint>(this->getReference());
srcParamSize = sizeof(refCnt);
srcParam = &refCnt;
break;
case CL_MEM_ALLOCATION_HANDLE_INTEL:
internalHandle = multiGraphicsAllocation.getDefaultGraphicsAllocation()->peekInternalHandle(this->memoryManager);
srcParamSize = sizeof(internalHandle);
srcParam = &internalHandle;
break;
case CL_MEM_USES_COMPRESSION_INTEL:
srcParam = &usesCompression;
srcParamSize = sizeof(cl_bool);
break;
case CL_MEM_PROPERTIES:
srcParamSize = propertiesVector.size() * sizeof(cl_mem_properties);
srcParam = propertiesVector.data();
break;
default:
getOsSpecificMemObjectInfo(paramName, &srcParamSize, &srcParam);
break;
}
auto getInfoStatus = GetInfo::getInfo(paramValue, paramValueSize, srcParam, srcParamSize);
retVal = changeGetInfoStatusToCLResultType(getInfoStatus);
GetInfo::setParamValueReturnSize(paramValueSizeRet, srcParamSize, getInfoStatus);
return retVal;
}
cl_int MemObj::setDestructorCallback(void(CL_CALLBACK *funcNotify)(cl_mem, void *),
void *userData) {
auto cb = new MemObjDestructorCallback(funcNotify, userData);
std::unique_lock<std::mutex> theLock(mtx);
destructorCallbacks.push_front(cb);
return CL_SUCCESS;
}
void *MemObj::getCpuAddress() const {
return memoryStorage;
}
void *MemObj::getHostPtr() const {
return hostPtr;
}
size_t MemObj::getSize() const {
return size;
}
void MemObj::setAllocatedMapPtr(void *allocatedMapPtr) {
this->allocatedMapPtr = allocatedMapPtr;
}
bool MemObj::isMemObjZeroCopy() const {
return isZeroCopy;
}
bool MemObj::isMemObjWithHostPtrSVM() const {
return isHostPtrSVM;
}
bool MemObj::isMemObjUncacheable() const {
return isValueSet(flagsIntel, CL_MEM_LOCALLY_UNCACHED_RESOURCE);
}
bool MemObj::isMemObjUncacheableForSurfaceState() const {
return isAnyBitSet(flagsIntel, CL_MEM_LOCALLY_UNCACHED_SURFACE_STATE_RESOURCE | CL_MEM_LOCALLY_UNCACHED_RESOURCE);
}
GraphicsAllocation *MemObj::getGraphicsAllocation(uint32_t rootDeviceIndex) const {
return multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex);
}
void MemObj::checkUsageAndReleaseOldAllocation(uint32_t rootDeviceIndex) {
auto graphicsAllocation = getGraphicsAllocation(rootDeviceIndex);
if (graphicsAllocation != nullptr && (peekSharingHandler() == nullptr || graphicsAllocation->peekReuseCount() == 0)) {
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(graphicsAllocation);
}
}
void MemObj::resetGraphicsAllocation(GraphicsAllocation *newGraphicsAllocation) {
TakeOwnershipWrapper<MemObj> lock(*this);
checkUsageAndReleaseOldAllocation(newGraphicsAllocation->getRootDeviceIndex());
multiGraphicsAllocation.addAllocation(newGraphicsAllocation);
}
void MemObj::removeGraphicsAllocation(uint32_t rootDeviceIndex) {
TakeOwnershipWrapper<MemObj> lock(*this);
checkUsageAndReleaseOldAllocation(rootDeviceIndex);
multiGraphicsAllocation.removeAllocation(rootDeviceIndex);
}
bool MemObj::readMemObjFlagsInvalid() {
return isValueSet(flags, CL_MEM_HOST_WRITE_ONLY) || isValueSet(flags, CL_MEM_HOST_NO_ACCESS);
}
bool MemObj::writeMemObjFlagsInvalid() {
return isValueSet(flags, CL_MEM_HOST_READ_ONLY) || isValueSet(flags, CL_MEM_HOST_NO_ACCESS);
}
bool MemObj::mapMemObjFlagsInvalid(cl_map_flags mapFlags) {
return (writeMemObjFlagsInvalid() && (mapFlags & CL_MAP_WRITE)) ||
(readMemObjFlagsInvalid() && (mapFlags & CL_MAP_READ));
}
void MemObj::setHostPtrMinSize(size_t size) {
hostPtrMinSize = size;
}
void *MemObj::getCpuAddressForMapping() {
void *ptrToReturn = nullptr;
if (isValueSet(flags, CL_MEM_USE_HOST_PTR)) {
ptrToReturn = this->hostPtr;
} else {
ptrToReturn = this->memoryStorage;
}
return ptrToReturn;
}
void *MemObj::getCpuAddressForMemoryTransfer() {
void *ptrToReturn = nullptr;
if (isValueSet(flags, CL_MEM_USE_HOST_PTR) && this->isMemObjZeroCopy()) {
ptrToReturn = this->hostPtr;
} else {
ptrToReturn = this->memoryStorage;
}
return ptrToReturn;
}
void MemObj::releaseAllocatedMapPtr() {
if (allocatedMapPtr) {
DEBUG_BREAK_IF(isValueSet(flags, CL_MEM_USE_HOST_PTR));
memoryManager->freeSystemMemory(allocatedMapPtr);
}
allocatedMapPtr = nullptr;
}
void MemObj::releaseMapAllocation(uint32_t rootDeviceIndex, bool asyncDestroy) {
auto mapAllocation = mapAllocations.getGraphicsAllocation(rootDeviceIndex);
if (mapAllocation && !isHostPtrSVM) {
if (asyncDestroy && !isValueSet(flags, CL_MEM_USE_HOST_PTR)) {
destroyGraphicsAllocation(mapAllocation, true);
} else {
if (mapAllocation->isUsed()) {
memoryManager->waitForEnginesCompletion(*mapAllocation);
}
destroyGraphicsAllocation(mapAllocation, false);
}
}
}
void MemObj::destroyGraphicsAllocation(GraphicsAllocation *allocation, bool asyncDestroy) {
if (asyncDestroy) {
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(allocation);
} else {
memoryManager->freeGraphicsMemory(allocation);
}
}
bool MemObj::checkIfMemoryTransferIsRequired(size_t offsetInMemObject, size_t offsetInHostPtr, const void *hostPtr, cl_command_type cmdType) {
auto bufferStorage = ptrOffset(this->getCpuAddressForMemoryTransfer(), offsetInMemObject);
auto hostStorage = ptrOffset(hostPtr, offsetInHostPtr);
auto isMemTransferNeeded = !((bufferStorage == hostStorage) &&
(cmdType == CL_COMMAND_WRITE_BUFFER || cmdType == CL_COMMAND_READ_BUFFER ||
cmdType == CL_COMMAND_WRITE_BUFFER_RECT || cmdType == CL_COMMAND_READ_BUFFER_RECT ||
cmdType == CL_COMMAND_WRITE_IMAGE || cmdType == CL_COMMAND_READ_IMAGE));
return isMemTransferNeeded;
}
void *MemObj::getBasePtrForMap(uint32_t rootDeviceIndex) {
if (associatedMemObject) {
return associatedMemObject->getBasePtrForMap(rootDeviceIndex);
}
if (getFlags() & CL_MEM_USE_HOST_PTR) {
return getHostPtr();
} else {
TakeOwnershipWrapper<MemObj> memObjOwnership(*this);
if (getMapAllocation(rootDeviceIndex)) {
return getMapAllocation(rootDeviceIndex)->getUnderlyingBuffer();
} else {
auto memory = getAllocatedMapPtr();
if (!memory) {
memory = memoryManager->allocateSystemMemory(getSize(), MemoryConstants::pageSize);
setAllocatedMapPtr(memory);
}
AllocationProperties properties{rootDeviceIndex,
false, // allocateMemory
getSize(), GraphicsAllocation::AllocationType::MAP_ALLOCATION,
false, //isMultiStorageAllocation
context->getDeviceBitfieldForAllocation(rootDeviceIndex)};
auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, memory);
setMapAllocation(allocation);
return getAllocatedMapPtr();
}
}
}
bool MemObj::addMappedPtr(void *ptr, size_t ptrLength, cl_map_flags &mapFlags,
MemObjSizeArray &size, MemObjOffsetArray &offset,
uint32_t mipLevel) {
return mapOperationsHandler.add(ptr, ptrLength, mapFlags, size, offset,
mipLevel);
}
bool MemObj::isTiledAllocation() const {
auto graphicsAllocation = multiGraphicsAllocation.getDefaultGraphicsAllocation();
auto gmm = graphicsAllocation->getDefaultGmm();
return gmm && (gmm->gmmResourceInfo->getTileModeSurfaceState() != 0);
}
bool MemObj::mappingOnCpuAllowed() const {
auto graphicsAllocation = multiGraphicsAllocation.getDefaultGraphicsAllocation();
return !isTiledAllocation() && !peekSharingHandler() && !isMipMapped(this) && !DebugManager.flags.DisableZeroCopyForBuffers.get() &&
!(graphicsAllocation->getDefaultGmm() && graphicsAllocation->getDefaultGmm()->isRenderCompressed) &&
MemoryPool::isSystemMemoryPool(graphicsAllocation->getMemoryPool());
}
void MemObj::storeProperties(const cl_mem_properties *properties) {
if (properties) {
for (size_t i = 0; properties[i] != 0; i += 2) {
propertiesVector.push_back(properties[i]);
propertiesVector.push_back(properties[i + 1]);
}
propertiesVector.push_back(0);
}
}
} // namespace NEO
|