Skip to content

Commit 32bb71d

Browse files
committed
Merge pull request opencv#9603 from alalek:ocl_device_extensions
2 parents 822d33d + 9e381d0 commit 32bb71d

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

modules/core/include/opencv2/core/ocl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class CV_EXPORTS Device
9191

9292
String name() const;
9393
String extensions() const;
94+
bool isExtensionSupported(const String& extensionName) const;
9495
String version() const;
9596
String vendorName() const;
9697
String OpenCL_C_Version() const;
@@ -160,6 +161,7 @@ class CV_EXPORTS Device
160161
uint imagePitchAlignment() const;
161162
uint imageBaseAddressAlignment() const;
162163

164+
/// deprecated, use isExtensionSupported() method (probably with "cl_khr_subgroups" value)
163165
bool intelSubgroupsSupport() const;
164166

165167
size_t image2DMaxWidth() const;

modules/core/src/ocl.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <list>
4444
#include <map>
4545
#include <deque>
46+
#include <set>
4647
#include <string>
4748
#include <sstream>
4849
#include <iostream> // std::cerr
@@ -518,6 +519,7 @@ struct Device::Impl
518519

519520
name_ = getStrProp(CL_DEVICE_NAME);
520521
version_ = getStrProp(CL_DEVICE_VERSION);
522+
extensions_ = getStrProp(CL_DEVICE_EXTENSIONS);
521523
doubleFPConfig_ = getProp<cl_device_fp_config, int>(CL_DEVICE_DOUBLE_FP_CONFIG);
522524
hostUnifiedMemory_ = getBoolProp(CL_DEVICE_HOST_UNIFIED_MEMORY);
523525
maxComputeUnits_ = getProp<cl_uint, int>(CL_DEVICE_MAX_COMPUTE_UNITS);
@@ -528,6 +530,20 @@ struct Device::Impl
528530
String deviceVersion_ = getStrProp(CL_DEVICE_VERSION);
529531
parseDeviceVersion(deviceVersion_, deviceVersionMajor_, deviceVersionMinor_);
530532

533+
size_t pos = 0;
534+
while (pos < extensions_.size())
535+
{
536+
size_t pos2 = extensions_.find(' ', pos);
537+
if (pos2 == String::npos)
538+
pos2 = extensions_.size();
539+
if (pos2 > pos)
540+
{
541+
std::string extensionName = extensions_.substr(pos, pos2 - pos);
542+
extensions_set_.insert(extensionName);
543+
}
544+
pos = pos2 + 1;
545+
}
546+
531547
intelSubgroupsSupport_ = isExtensionSupported("cl_intel_subgroups");
532548

533549
vendorName_ = getStrProp(CL_DEVICE_VENDOR);
@@ -569,23 +585,19 @@ struct Device::Impl
569585
sz < sizeof(buf) ? String(buf) : String();
570586
}
571587

572-
bool isExtensionSupported(const String& extensionName) const
588+
bool isExtensionSupported(const std::string& extensionName) const
573589
{
574-
bool ret = false;
575-
size_t pos = getStrProp(CL_DEVICE_EXTENSIONS).find(extensionName);
576-
if (pos != String::npos)
577-
{
578-
ret = true;
579-
}
580-
return ret;
590+
return extensions_set_.count(extensionName) > 0;
581591
}
582592

583593

584594
IMPLEMENT_REFCOUNTABLE();
595+
585596
cl_device_id handle;
586597

587598
String name_;
588599
String version_;
600+
std::string extensions_;
589601
int doubleFPConfig_;
590602
bool hostUnifiedMemory_;
591603
int maxComputeUnits_;
@@ -597,6 +609,8 @@ struct Device::Impl
597609
String vendorName_;
598610
int vendorID_;
599611
bool intelSubgroupsSupport_;
612+
613+
std::set<std::string> extensions_set_;
600614
};
601615

602616

@@ -651,7 +665,10 @@ String Device::name() const
651665
{ return p ? p->name_ : String(); }
652666

653667
String Device::extensions() const
654-
{ return p ? p->getStrProp(CL_DEVICE_EXTENSIONS) : String(); }
668+
{ return p ? String(p->extensions_) : String(); }
669+
670+
bool Device::isExtensionSupported(const String& extensionName) const
671+
{ return p ? p->isExtensionSupported(extensionName) : false; }
655672

656673
String Device::version() const
657674
{ return p ? p->version_ : String(); }
@@ -744,16 +761,7 @@ bool Device::imageSupport() const
744761

745762
bool Device::imageFromBufferSupport() const
746763
{
747-
bool ret = false;
748-
if (p)
749-
{
750-
size_t pos = p->getStrProp(CL_DEVICE_EXTENSIONS).find("cl_khr_image2d_from_buffer");
751-
if (pos != String::npos)
752-
{
753-
ret = true;
754-
}
755-
}
756-
return ret;
764+
return p ? p->isExtensionSupported("cl_khr_image2d_from_buffer") : false;
757765
}
758766

759767
uint Device::imagePitchAlignment() const

modules/ts/src/ocl_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ void dumpOpenCLDevice()
181181
DUMP_MESSAGE_STDOUT(" Host unified memory = "<< isUnifiedMemoryStr);
182182
DUMP_PROPERTY_XML("cv_ocl_current_hostUnifiedMemory", device.hostUnifiedMemory());
183183

184+
DUMP_MESSAGE_STDOUT(" Device extensions:");
185+
String extensionsStr = device.extensions();
186+
size_t pos = 0;
187+
while (pos < extensionsStr.size())
188+
{
189+
size_t pos2 = extensionsStr.find(' ', pos);
190+
if (pos2 == String::npos)
191+
pos2 = extensionsStr.size();
192+
if (pos2 > pos)
193+
{
194+
String extensionName = extensionsStr.substr(pos, pos2 - pos);
195+
DUMP_MESSAGE_STDOUT(" " << extensionName);
196+
}
197+
pos = pos2 + 1;
198+
}
199+
DUMP_PROPERTY_XML("cv_ocl_current_extensions", extensionsStr.c_str());
200+
184201
const char* haveAmdBlasStr = haveAmdBlas() ? "Yes" : "No";
185202
DUMP_MESSAGE_STDOUT(" Has AMD Blas = "<< haveAmdBlasStr);
186203
DUMP_PROPERTY_XML("cv_ocl_current_AmdBlas", haveAmdBlas());

0 commit comments

Comments
 (0)