Hash :
897a5654
Author :
Date :
2024-10-16T10:32:16
CL: Rename isSet/isNotSet to intersects/excludes In particular, `isSet` was checking if _any_ bit was set instead of if _all_ bits were set. This was a point of confusion. Bug: angleproject:42266936 Change-Id: I76211a423f304fb4641ceb9cbfbb0bea4ff33ccf Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5937411 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
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
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CLPlatform.cpp: Implements the cl::Platform class.
#include "libANGLE/CLPlatform.h"
#include "libANGLE/CLContext.h"
#include "libANGLE/CLDevice.h"
#include "libANGLE/cl_utils.h"
#include <cstring>
namespace cl
{
namespace
{
bool IsDeviceTypeMatch(DeviceType select, DeviceType type)
{
// The type 'DeviceType' is a bitfield, so it matches if any selected bit is set.
// A custom device is an exception, which only matches if it was explicitely selected, see:
// https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#clGetDeviceIDs
return type == CL_DEVICE_TYPE_CUSTOM ? select == CL_DEVICE_TYPE_CUSTOM
: type.intersects(select);
}
Context::PropArray ParseContextProperties(const cl_context_properties *properties,
Platform *&platform,
bool &userSync)
{
Context::PropArray propArray;
if (properties != nullptr)
{
const cl_context_properties *propIt = properties;
while (*propIt != 0)
{
switch (*propIt++)
{
case CL_CONTEXT_PLATFORM:
platform = &reinterpret_cast<cl_platform_id>(*propIt++)->cast<Platform>();
break;
case CL_CONTEXT_INTEROP_USER_SYNC:
userSync = *propIt++ != CL_FALSE;
break;
}
}
// Include the trailing zero
++propIt;
propArray.reserve(propIt - properties);
propArray.insert(propArray.cend(), properties, propIt);
}
return propArray;
}
} // namespace
void Platform::Initialize(const cl_icd_dispatch &dispatch,
rx::CLPlatformImpl::CreateFuncs &&createFuncs)
{
PlatformPtrs &platforms = GetPointers();
ASSERT(_cl_platform_id::sDispatch == nullptr && platforms.empty());
if (_cl_platform_id::sDispatch != nullptr || !platforms.empty())
{
ERR() << "Already initialized";
return;
}
Dispatch::sDispatch = &dispatch;
platforms.reserve(createFuncs.size());
while (!createFuncs.empty())
{
platforms.emplace_back(new Platform(createFuncs.front()));
// Release initialization reference, lifetime controlled by RefPointer.
platforms.back()->release();
// Remove platform on any errors
if (!platforms.back()->mInfo.isValid() || platforms.back()->mDevices.empty())
{
platforms.pop_back();
}
createFuncs.pop_front();
}
}
angle::Result Platform::GetPlatformIDs(cl_uint numEntries,
cl_platform_id *platforms,
cl_uint *numPlatforms)
{
const PlatformPtrs &availPlatforms = GetPlatforms();
if (numPlatforms != nullptr)
{
*numPlatforms = static_cast<cl_uint>(availPlatforms.size());
}
if (platforms != nullptr)
{
cl_uint entry = 0u;
auto platformIt = availPlatforms.cbegin();
while (entry < numEntries && platformIt != availPlatforms.cend())
{
platforms[entry++] = (*platformIt++).get();
}
}
return angle::Result::Continue;
}
angle::Result Platform::getInfo(PlatformInfo name,
size_t valueSize,
void *value,
size_t *valueSizeRet) const
{
const void *copyValue = nullptr;
size_t copySize = 0u;
switch (name)
{
case PlatformInfo::Profile:
copyValue = mInfo.profile.c_str();
copySize = mInfo.profile.length() + 1u;
break;
case PlatformInfo::Version:
copyValue = mInfo.versionStr.c_str();
copySize = mInfo.versionStr.length() + 1u;
break;
case PlatformInfo::NumericVersion:
copyValue = &mInfo.version;
copySize = sizeof(mInfo.version);
break;
case PlatformInfo::Name:
copyValue = mInfo.name.c_str();
copySize = mInfo.name.length() + 1u;
break;
case PlatformInfo::Vendor:
copyValue = kVendor;
copySize = sizeof(kVendor);
break;
case PlatformInfo::Extensions:
copyValue = mInfo.extensions.c_str();
copySize = mInfo.extensions.length() + 1u;
break;
case PlatformInfo::ExtensionsWithVersion:
copyValue = mInfo.extensionsWithVersion.data();
copySize = mInfo.extensionsWithVersion.size() *
sizeof(decltype(mInfo.extensionsWithVersion)::value_type);
break;
case PlatformInfo::HostTimerResolution:
copyValue = &mInfo.hostTimerRes;
copySize = sizeof(mInfo.hostTimerRes);
break;
case PlatformInfo::IcdSuffix:
copyValue = kIcdSuffix;
copySize = sizeof(kIcdSuffix);
break;
default:
ASSERT(false);
ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
}
if (value != nullptr)
{
// CL_INVALID_VALUE if size in bytes specified by param_value_size is < size of return type
// as specified in the OpenCL Platform Queries table, and param_value is not a NULL value.
if (valueSize < copySize)
{
ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
}
if (copyValue != nullptr)
{
std::memcpy(value, copyValue, copySize);
}
}
if (valueSizeRet != nullptr)
{
*valueSizeRet = copySize;
}
return angle::Result::Continue;
}
angle::Result Platform::getDeviceIDs(DeviceType deviceType,
cl_uint numEntries,
cl_device_id *devices,
cl_uint *numDevices) const
{
cl_uint found = 0u;
for (const DevicePtr &device : mDevices)
{
if (IsDeviceTypeMatch(deviceType, device->getInfo().type))
{
if (devices != nullptr && found < numEntries)
{
devices[found] = device.get();
}
++found;
}
}
if (numDevices != nullptr)
{
*numDevices = found;
}
// CL_DEVICE_NOT_FOUND if no OpenCL devices that matched device_type were found.
if (found == 0u)
{
ANGLE_CL_RETURN_ERROR(CL_DEVICE_NOT_FOUND);
}
return angle::Result::Continue;
}
bool Platform::hasDeviceType(DeviceType deviceType) const
{
for (const DevicePtr &device : mDevices)
{
if (IsDeviceTypeMatch(deviceType, device->getInfo().type))
{
return true;
}
}
return false;
}
cl_context Platform::CreateContext(const cl_context_properties *properties,
cl_uint numDevices,
const cl_device_id *devices,
ContextErrorCB notify,
void *userData)
{
DevicePtrs devs;
devs.reserve(numDevices);
while (numDevices-- != 0u)
{
devs.emplace_back(&(*devices++)->cast<Device>());
}
Platform *platform = nullptr;
bool userSync = false;
Context::PropArray propArray = ParseContextProperties(properties, platform, userSync);
if (platform == nullptr)
{
// All devices in the list have already been validated at this point to contain the same
// platform - just use/select the first device's platform.
platform = &devs.front()->getPlatform();
}
return Object::Create<Context>(*platform, std::move(propArray), std::move(devs), notify,
userData, userSync);
}
cl_context Platform::CreateContextFromType(const cl_context_properties *properties,
DeviceType deviceType,
ContextErrorCB notify,
void *userData)
{
Platform *platform = nullptr;
bool userSync = false;
Context::PropArray propArray = ParseContextProperties(properties, platform, userSync);
// Choose default platform if user does not specify in the context properties field
if (platform == nullptr)
{
platform = Platform::GetDefault();
}
return Object::Create<Context>(*platform, std::move(propArray), deviceType, notify, userData,
userSync);
}
angle::Result Platform::unloadCompiler()
{
return mImpl->unloadCompiler();
}
Platform::~Platform() = default;
Platform::Platform(const rx::CLPlatformImpl::CreateFunc &createFunc)
: mImpl(createFunc(*this)),
mInfo(mImpl ? mImpl->createInfo() : rx::CLPlatformImpl::Info{}),
mDevices(mImpl ? createDevices(mImpl->createDevices()) : DevicePtrs{}),
mMultiThreadPool(mImpl ? angle::WorkerThreadPool::Create(0, ANGLEPlatformCurrent()) : nullptr)
{}
DevicePtrs Platform::createDevices(rx::CLDeviceImpl::CreateDatas &&createDatas)
{
DevicePtrs devices;
devices.reserve(createDatas.size());
while (!createDatas.empty())
{
devices.emplace_back(
new Device(*this, nullptr, createDatas.front().first, createDatas.front().second));
// Release initialization reference, lifetime controlled by RefPointer.
devices.back()->release();
if (!devices.back()->mInfo.isValid())
{
devices.pop_back();
}
createDatas.pop_front();
}
return devices;
}
constexpr char Platform::kVendor[];
constexpr char Platform::kIcdSuffix[];
} // namespace cl