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
|
/* Copyright (c) 2008 - 2021 Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#include "platform/context.hpp"
#include "platform/interop_gl.hpp"
#include "vdi_common.hpp"
#include "platform/commandqueue.hpp"
#include <algorithm>
#include <functional>
#ifdef _WIN32
#include <d3d10_1.h>
#include <dxgi.h>
#include "CL/cl_d3d10.h"
#include "CL/cl_d3d11.h"
#include "CL/cl_dx9_media_sharing.h"
#endif //_WIN32
namespace amd {
Context::Context(const std::vector<Device*>& devices, const Info& info)
: devices_(devices),
info_(info),
properties_(NULL),
glenv_(NULL),
customHostAllocDevice_(NULL) {
for (const auto& device : devices) {
device->retain();
if (customHostAllocDevice_ == NULL && device->customHostAllocator()) {
customHostAllocDevice_ = device;
}
if (device->svmSupport()) {
svmAllocDevice_.push_back(device);
}
}
if (svmAllocDevice_.size() > 1) {
uint isFirstDeviceFGSEnabled = svmAllocDevice_.front()->isFineGrainedSystem(true);
for (auto& dev : svmAllocDevice_) {
// allocation on fine - grained system incapable device first
if (isFirstDeviceFGSEnabled && !dev->isFineGrainedSystem(true)) {
std::swap(svmAllocDevice_.front(), dev);
break;
}
}
}
}
Context::~Context() {
static const bool VALIDATE_ONLY = false;
// Loop through all devices
for (const auto& it : devices_) {
// Dissociate OCL context with any external device
if (info_.flags_ & (GLDeviceKhr | D3D10DeviceKhr | D3D11DeviceKhr)) {
it->unbindExternalDevice(info_.flags_, info_.hDev_, info_.hCtx_, VALIDATE_ONLY);
}
// Notify device about context destroy
it->ContextDestroy();
// Release device
it->release();
}
delete[] properties_;
delete glenv_;
}
int Context::checkProperties(const cl_context_properties* properties, Context::Info* info) {
cl_platform_id pfmId = 0;
uint count = 0;
const struct Element {
intptr_t name;
void* ptr;
}* p = reinterpret_cast<const Element*>(properties);
// Clear the context infor structure
::memset(info, 0, sizeof(Context::Info));
if (properties == NULL) {
return CL_SUCCESS;
}
// Process all properties
while (p->name != 0) {
switch (p->name) {
case CL_CONTEXT_INTEROP_USER_SYNC:
if (p->ptr == reinterpret_cast<void*>(true)) {
info->flags_ |= InteropUserSync;
}
break;
#ifdef _WIN32
case CL_CONTEXT_D3D10_DEVICE_KHR:
if (p->ptr == NULL) {
return CL_INVALID_VALUE;
}
info->hDev_[D3D10DeviceKhrIdx] = p->ptr;
info->flags_ |= D3D10DeviceKhr;
break;
case CL_CONTEXT_D3D11_DEVICE_KHR:
if (p->ptr == NULL) {
return CL_INVALID_VALUE;
}
info->hDev_[D3D11DeviceKhrIdx] = p->ptr;
info->flags_ |= D3D11DeviceKhr;
break;
case CL_CONTEXT_ADAPTER_D3D9_KHR:
if (p->ptr == NULL) { // not supported for xp
return CL_INVALID_VALUE;
}
info->hDev_[D3D9DeviceKhrIdx] = p->ptr;
info->flags_ |= D3D9DeviceKhr;
break;
case CL_CONTEXT_ADAPTER_D3D9EX_KHR:
if (p->ptr == NULL) {
return CL_INVALID_VALUE;
}
info->hDev_[D3D9DeviceEXKhrIdx] = p->ptr;
info->flags_ |= D3D9DeviceEXKhr;
break;
case CL_CONTEXT_ADAPTER_DXVA_KHR:
if (p->ptr == NULL) {
return CL_INVALID_VALUE;
}
info->hDev_[D3D9DeviceVAKhrIdx] = p->ptr;
info->flags_ |= D3D9DeviceVAKhr;
break;
#endif //_WIN32
case CL_EGL_DISPLAY_KHR:
info->flags_ |= EGLDeviceKhr;
#ifdef _WIN32
case CL_WGL_HDC_KHR:
#endif //_WIN32
#if defined(__linux__)
case CL_GLX_DISPLAY_KHR:
#endif // linux
if (p->ptr == NULL) {
return CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR;
}
case ROCCLR_HIP_GLX_DISPLAY_KHR:
case ROCCLR_HIP_WGL_HDC_KHR:
info->hDev_[GLDeviceKhrIdx] = p->ptr;
break;
#if defined(__APPLE__) || defined(__MACOSX)
case CL_CGL_SHAREGROUP_KHR:
Unimplemented();
break;
#endif //__APPLE__ || MACOS
case CL_GL_CONTEXT_KHR:
if (p->ptr == NULL) {
return CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR;
}
//skip the null case in the case of hip-gl, it will be initialized in create
case ROCCLR_HIP_GL_CONTEXT_KHR:
info->hCtx_ = p->ptr;
info->flags_ |= GLDeviceKhr;
break;
case CL_CONTEXT_PLATFORM:
pfmId = reinterpret_cast<cl_platform_id>(p->ptr);
if ((NULL != pfmId) && (AMD_PLATFORM != pfmId)) {
return CL_INVALID_VALUE;
}
break;
case CL_CONTEXT_OFFLINE_DEVICES_AMD:
if (p->ptr != reinterpret_cast<void*>(1)) {
return CL_INVALID_VALUE;
}
// Set the offline device flag
info->flags_ |= OfflineDevices;
break;
default:
return CL_INVALID_VALUE;
}
p++;
count++;
}
info->propertiesSize_ = count * sizeof(Element) + sizeof(intptr_t);
return CL_SUCCESS;
}
int Context::create(const intptr_t* properties) {
static const bool VALIDATE_ONLY = false;
int result = CL_SUCCESS;
if (properties != NULL) {
properties_ = new cl_context_properties[info().propertiesSize_ / sizeof(cl_context_properties)];
if (properties_ == NULL) {
return CL_OUT_OF_HOST_MEMORY;
}
::memcpy(properties_, properties, info().propertiesSize_);
}
// if the context passed in is null, it's the GL interop case and we need to get the current context
if (info_.hCtx_ == nullptr) {
if (info_.flags_ & GLDeviceKhr) {
// Init context for GL interop
if (glenv_ == NULL) {
HMODULE h = (HMODULE)Os::loadLibrary(
#ifdef _WIN32
"OpenGL32.dll"
#else //!_WIN32
"libGL.so.1"
#endif //!_WIN32
);
if (h && (glenv_ = new GLFunctions(h, (info_.flags_ & Flags::EGLDeviceKhr) != 0))) {
#ifdef _WIN32
info_.hCtx_ = (void*)glenv_->wglGetCurrentContext_();
info_.hDev_[GLDeviceKhrIdx] = (void*)glenv_->wglGetCurrentDC_();
#else
info_.hCtx_ = (void*)glenv_->glXGetCurrentContext_();
info_.hDev_[GLDeviceKhrIdx] = (void*)glenv_->glXGetCurrentDisplay_();
#endif
}
}
struct Element {
intptr_t name;
void* ptr;
}* p = reinterpret_cast<Element*>(properties_);
while (p->name != 0) {
switch (p->name) {
case ROCCLR_HIP_GLX_DISPLAY_KHR:
case ROCCLR_HIP_WGL_HDC_KHR:
p->ptr = info_.hDev_[GLDeviceKhrIdx];
break;
case ROCCLR_HIP_GL_CONTEXT_KHR:
p->ptr = info_.hCtx_;
break;
}
p++;
}
}
}
// Check if OCL context can be associated with any external device
if (info_.flags_ & (D3D10DeviceKhr | D3D11DeviceKhr | GLDeviceKhr | D3D9DeviceKhr |
D3D9DeviceEXKhr | D3D9DeviceVAKhr)) {
// Loop through all devices
for (const auto& it : devices_) {
if (!it->bindExternalDevice(info_.flags_, info_.hDev_, info_.hCtx_, VALIDATE_ONLY)) {
result = CL_INVALID_VALUE;
}
}
}
// Check if the device binding wasn't successful
if (result != CL_SUCCESS) {
if (info_.flags_ & GLDeviceKhr) {
result = CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR;
} else if (info_.flags_ & D3D10DeviceKhr) {
// return CL_INVALID_VALUE; // FIXME_odintsov: CL_INVALID_D3D_INTEROP;
} else if (info_.flags_ & D3D11DeviceKhr) {
// return CL_INVALID_VALUE; // FIXME_odintsov: CL_INVALID_D3D_INTEROP;
} else if (info_.flags_ & (D3D9DeviceKhr | D3D9DeviceEXKhr | D3D9DeviceVAKhr)) {
// return CL_INVALID_DX9_MEDIA_ADAPTER_KHR;
}
} else {
if (info_.flags_ & GLDeviceKhr) {
if (glenv_ == NULL) {
HMODULE h = (HMODULE)Os::loadLibrary(
#ifdef _WIN32
"OpenGL32.dll"
#else //!_WIN32
"libGL.so.1"
#endif //!_WIN32
);
if (!h || !(glenv_ = new GLFunctions(h, (info_.flags_ & Flags::EGLDeviceKhr) != 0))) {
return CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR;
}
}
if (!glenv_->init(reinterpret_cast<intptr_t>(info_.hDev_[GLDeviceKhrIdx]),
reinterpret_cast<intptr_t>(info_.hCtx_))) {
delete glenv_;
glenv_ = NULL;
result = CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR;
}
}
}
return result;
}
void* Context::hostAlloc(size_t size, size_t alignment, bool atomics) const {
if (customHostAllocDevice_ != NULL) {
return customHostAllocDevice_->hostAlloc(size, alignment, atomics
? Device::MemorySegment::kAtomics
: Device::MemorySegment::kNoAtomics);
}
return AlignedMemory::allocate(size, alignment);
}
void Context::hostFree(void* ptr) const {
if (customHostAllocDevice_ != NULL) {
customHostAllocDevice_->hostFree(ptr);
return;
}
AlignedMemory::deallocate(ptr);
}
void* Context::svmAlloc(size_t size, size_t alignment, cl_svm_mem_flags flags,
const amd::Device* curDev) {
unsigned int numSVMDev = svmAllocDevice_.size();
if (numSVMDev < 1) {
return nullptr;
}
void* svmPtrAlloced = nullptr;
amd::ScopedLock lock(&ctxLock_);
if (curDev != nullptr) {
if (!(flags & CL_MEM_SVM_ATOMICS) ||
(curDev->info().svmCapabilities_ & CL_DEVICE_SVM_ATOMICS)) {
svmPtrAlloced = curDev->svmAlloc(*this, size, alignment, flags, svmPtrAlloced);
if (svmPtrAlloced == nullptr) {
return nullptr;
}
}
}
for (const auto& dev : svmAllocDevice_) {
if (dev == curDev) {
continue;
}
// check if the device support svm platform atomics,
// skipped allocation for platform atomics if not supported by this device
if ((flags & CL_MEM_SVM_ATOMICS) &&
!(dev->info().svmCapabilities_ & CL_DEVICE_SVM_ATOMICS)) {
continue;
}
svmPtrAlloced = dev->svmAlloc(*this, size, alignment, flags, svmPtrAlloced);
if (svmPtrAlloced == nullptr) {
return nullptr;
}
}
return svmPtrAlloced;
}
void Context::svmFree(void* ptr) const {
amd::ScopedLock lock(&ctxLock_);
for (const auto& dev : svmAllocDevice_) {
dev->svmFree(ptr);
}
return;
}
bool Context::containsDevice(const Device* device) const {
for (const auto& it : devices_) {
if (device == it) {
return true;
}
}
return false;
}
DeviceQueue* Context::defDeviceQueue(const Device& dev) const {
const auto it = deviceQueues_.find(&dev);
if (it != deviceQueues_.cend()) {
return it->second.defDeviceQueue_;
} else {
return NULL;
}
}
bool Context::isDevQueuePossible(const Device& dev) {
return (deviceQueues_[&dev].deviceQueueCnt_ < dev.info().maxOnDeviceQueues_) ? true : false;
}
void Context::addDeviceQueue(const Device& dev, DeviceQueue* queue, bool defDevQueue) {
DeviceQueueInfo& info = deviceQueues_[&dev];
info.deviceQueueCnt_++;
if (defDevQueue) {
info.defDeviceQueue_ = queue;
}
}
void Context::removeDeviceQueue(const Device& dev, DeviceQueue* queue) {
DeviceQueueInfo& info = deviceQueues_[&dev];
assert((info.deviceQueueCnt_ != 0) && "The device queue map is empty!");
info.deviceQueueCnt_--;
if (info.defDeviceQueue_ == queue) {
info.defDeviceQueue_ = NULL;
}
}
} // namespace amd
|