|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +#include <opencv2/core.hpp> |
| 8 | +#include <opencv2/core/ocl.hpp> |
| 9 | + |
| 10 | +#ifndef DUMP_CONFIG_PROPERTY |
| 11 | +#define DUMP_CONFIG_PROPERTY(...) |
| 12 | +#endif |
| 13 | + |
| 14 | +#ifndef DUMP_MESSAGE_STDOUT |
| 15 | +#define DUMP_MESSAGE_STDOUT(...) do { std::cout << __VA_ARGS__ << std::endl; } while (false) |
| 16 | +#endif |
| 17 | + |
| 18 | +namespace cv { |
| 19 | + |
| 20 | +namespace { |
| 21 | +static std::string bytesToStringRepr(size_t value) |
| 22 | +{ |
| 23 | + size_t b = value % 1024; |
| 24 | + value /= 1024; |
| 25 | + |
| 26 | + size_t kb = value % 1024; |
| 27 | + value /= 1024; |
| 28 | + |
| 29 | + size_t mb = value % 1024; |
| 30 | + value /= 1024; |
| 31 | + |
| 32 | + size_t gb = value; |
| 33 | + |
| 34 | + std::ostringstream stream; |
| 35 | + |
| 36 | + if (gb > 0) |
| 37 | + stream << gb << " GB "; |
| 38 | + if (mb > 0) |
| 39 | + stream << mb << " MB "; |
| 40 | + if (kb > 0) |
| 41 | + stream << kb << " KB "; |
| 42 | + if (b > 0) |
| 43 | + stream << b << " B"; |
| 44 | + |
| 45 | + std::string s = stream.str(); |
| 46 | + if (s[s.size() - 1] == ' ') |
| 47 | + s = s.substr(0, s.size() - 1); |
| 48 | + return s; |
| 49 | +} |
| 50 | +} // namespace |
| 51 | + |
| 52 | +static void dumpOpenCLInformation() |
| 53 | +{ |
| 54 | + using namespace cv::ocl; |
| 55 | + |
| 56 | + try |
| 57 | + { |
| 58 | + if (!haveOpenCL() || !useOpenCL()) |
| 59 | + { |
| 60 | + DUMP_MESSAGE_STDOUT("OpenCL is disabled"); |
| 61 | + DUMP_CONFIG_PROPERTY("cv_ocl", "disabled"); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + std::vector<PlatformInfo> platforms; |
| 66 | + cv::ocl::getPlatfomsInfo(platforms); |
| 67 | + if (platforms.size() > 0) |
| 68 | + { |
| 69 | + DUMP_MESSAGE_STDOUT("OpenCL Platforms: "); |
| 70 | + for (size_t i = 0; i < platforms.size(); i++) |
| 71 | + { |
| 72 | + const PlatformInfo* platform = &platforms[i]; |
| 73 | + DUMP_MESSAGE_STDOUT(" " << platform->name().c_str()); |
| 74 | + Device current_device; |
| 75 | + for (int j = 0; j < platform->deviceNumber(); j++) |
| 76 | + { |
| 77 | + platform->getDevice(current_device, j); |
| 78 | + const char* deviceTypeStr = current_device.type() == Device::TYPE_CPU |
| 79 | + ? ("CPU") : (current_device.type() == Device::TYPE_GPU ? current_device.hostUnifiedMemory() ? "iGPU" : "dGPU" : "unknown"); |
| 80 | + DUMP_MESSAGE_STDOUT( " " << deviceTypeStr << ": " << current_device.name().c_str() << " (" << current_device.version().c_str() << ")"); |
| 81 | + DUMP_CONFIG_PROPERTY( cv::format("cv_ocl_platform_%d_device_%d", (int)i, (int)j ), |
| 82 | + cv::format("(Platform=%s)(Type=%s)(Name=%s)(Version=%s)", |
| 83 | + platform->name().c_str(), deviceTypeStr, current_device.name().c_str(), current_device.version().c_str()) ); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + DUMP_MESSAGE_STDOUT("OpenCL is not available"); |
| 90 | + DUMP_CONFIG_PROPERTY("cv_ocl", "not available"); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const Device& device = Device::getDefault(); |
| 95 | + if (!device.available()) |
| 96 | + CV_ErrorNoReturn(Error::OpenCLInitError, "OpenCL device is not available"); |
| 97 | + |
| 98 | + DUMP_MESSAGE_STDOUT("Current OpenCL device: "); |
| 99 | + |
| 100 | +#if 0 |
| 101 | + DUMP_MESSAGE_STDOUT(" Platform = " << device.getPlatform().name()); |
| 102 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_platformName", device.getPlatform().name()); |
| 103 | +#endif |
| 104 | + |
| 105 | + const char* deviceTypeStr = device.type() == Device::TYPE_CPU |
| 106 | + ? ("CPU") : (device.type() == Device::TYPE_GPU ? device.hostUnifiedMemory() ? "iGPU" : "dGPU" : "unknown"); |
| 107 | + DUMP_MESSAGE_STDOUT(" Type = " << deviceTypeStr); |
| 108 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_deviceType", deviceTypeStr); |
| 109 | + |
| 110 | + DUMP_MESSAGE_STDOUT(" Name = " << device.name()); |
| 111 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_deviceName", device.name()); |
| 112 | + |
| 113 | + DUMP_MESSAGE_STDOUT(" Version = " << device.version()); |
| 114 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_deviceVersion", device.version()); |
| 115 | + |
| 116 | + DUMP_MESSAGE_STDOUT(" Driver version = " << device.driverVersion()); |
| 117 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_driverVersion", device.driverVersion()); |
| 118 | + |
| 119 | + DUMP_MESSAGE_STDOUT(" Address bits = " << device.addressBits()); |
| 120 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_addressBits", device.addressBits()); |
| 121 | + |
| 122 | + DUMP_MESSAGE_STDOUT(" Compute units = " << device.maxComputeUnits()); |
| 123 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_maxComputeUnits", device.maxComputeUnits()); |
| 124 | + |
| 125 | + DUMP_MESSAGE_STDOUT(" Max work group size = " << device.maxWorkGroupSize()); |
| 126 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_maxWorkGroupSize", device.maxWorkGroupSize()); |
| 127 | + |
| 128 | + std::string localMemorySizeStr = bytesToStringRepr(device.localMemSize()); |
| 129 | + DUMP_MESSAGE_STDOUT(" Local memory size = " << localMemorySizeStr); |
| 130 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_localMemSize", device.localMemSize()); |
| 131 | + |
| 132 | + std::string maxMemAllocSizeStr = bytesToStringRepr(device.maxMemAllocSize()); |
| 133 | + DUMP_MESSAGE_STDOUT(" Max memory allocation size = " << maxMemAllocSizeStr); |
| 134 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_maxMemAllocSize", device.maxMemAllocSize()); |
| 135 | + |
| 136 | + const char* doubleSupportStr = device.doubleFPConfig() > 0 ? "Yes" : "No"; |
| 137 | + DUMP_MESSAGE_STDOUT(" Double support = " << doubleSupportStr); |
| 138 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_haveDoubleSupport", device.doubleFPConfig() > 0); |
| 139 | + |
| 140 | + const char* isUnifiedMemoryStr = device.hostUnifiedMemory() ? "Yes" : "No"; |
| 141 | + DUMP_MESSAGE_STDOUT(" Host unified memory = " << isUnifiedMemoryStr); |
| 142 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_hostUnifiedMemory", device.hostUnifiedMemory()); |
| 143 | + |
| 144 | + DUMP_MESSAGE_STDOUT(" Device extensions:"); |
| 145 | + String extensionsStr = device.extensions(); |
| 146 | + size_t pos = 0; |
| 147 | + while (pos < extensionsStr.size()) |
| 148 | + { |
| 149 | + size_t pos2 = extensionsStr.find(' ', pos); |
| 150 | + if (pos2 == String::npos) |
| 151 | + pos2 = extensionsStr.size(); |
| 152 | + if (pos2 > pos) |
| 153 | + { |
| 154 | + String extensionName = extensionsStr.substr(pos, pos2 - pos); |
| 155 | + DUMP_MESSAGE_STDOUT(" " << extensionName); |
| 156 | + } |
| 157 | + pos = pos2 + 1; |
| 158 | + } |
| 159 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_extensions", extensionsStr.c_str()); |
| 160 | + |
| 161 | + const char* haveAmdBlasStr = haveAmdBlas() ? "Yes" : "No"; |
| 162 | + DUMP_MESSAGE_STDOUT(" Has AMD Blas = " << haveAmdBlasStr); |
| 163 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_AmdBlas", haveAmdBlas()); |
| 164 | + |
| 165 | + const char* haveAmdFftStr = haveAmdFft() ? "Yes" : "No"; |
| 166 | + DUMP_MESSAGE_STDOUT(" Has AMD Fft = " << haveAmdFftStr); |
| 167 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_AmdFft", haveAmdFft()); |
| 168 | + |
| 169 | + |
| 170 | + DUMP_MESSAGE_STDOUT(" Preferred vector width char = " << device.preferredVectorWidthChar()); |
| 171 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_preferredVectorWidthChar", device.preferredVectorWidthChar()); |
| 172 | + |
| 173 | + DUMP_MESSAGE_STDOUT(" Preferred vector width short = " << device.preferredVectorWidthShort()); |
| 174 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_preferredVectorWidthShort", device.preferredVectorWidthShort()); |
| 175 | + |
| 176 | + DUMP_MESSAGE_STDOUT(" Preferred vector width int = " << device.preferredVectorWidthInt()); |
| 177 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_preferredVectorWidthInt", device.preferredVectorWidthInt()); |
| 178 | + |
| 179 | + DUMP_MESSAGE_STDOUT(" Preferred vector width long = " << device.preferredVectorWidthLong()); |
| 180 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_preferredVectorWidthLong", device.preferredVectorWidthLong()); |
| 181 | + |
| 182 | + DUMP_MESSAGE_STDOUT(" Preferred vector width float = " << device.preferredVectorWidthFloat()); |
| 183 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_preferredVectorWidthFloat", device.preferredVectorWidthFloat()); |
| 184 | + |
| 185 | + DUMP_MESSAGE_STDOUT(" Preferred vector width double = " << device.preferredVectorWidthDouble()); |
| 186 | + DUMP_CONFIG_PROPERTY("cv_ocl_current_preferredVectorWidthDouble", device.preferredVectorWidthDouble()); |
| 187 | + } |
| 188 | + catch (...) |
| 189 | + { |
| 190 | + DUMP_MESSAGE_STDOUT("Exception. Can't dump OpenCL info"); |
| 191 | + DUMP_MESSAGE_STDOUT("OpenCL device not available"); |
| 192 | + DUMP_CONFIG_PROPERTY("cv_ocl", "not available"); |
| 193 | + } |
| 194 | +} |
| 195 | +#undef DUMP_MESSAGE_STDOUT |
| 196 | +#undef DUMP_CONFIG_PROPERTY |
| 197 | + |
| 198 | +} // namespace |
0 commit comments