workaround segfault in compiler on macos-clang-intel
[LibreOffice.git] / opencl / source / openclwrapper.cxx
blob9588d742668048367c905b02d1ab08bff0cbfb51
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <config_folders.h>
12 #include <opencl_device.hxx>
13 #include <opencl_device_selection.h>
15 #include <opencl/openclconfig.hxx>
16 #include <opencl/openclwrapper.hxx>
17 #include <opencl/platforminfo.hxx>
18 #include <osl/file.hxx>
19 #include <rtl/bootstrap.hxx>
20 #include <rtl/digest.h>
21 #include <rtl/strbuf.hxx>
22 #include <rtl/ustring.hxx>
23 #include <sal/config.h>
24 #include <sal/log.hxx>
25 #include <opencl/OpenCLZone.hxx>
27 #include <memory>
28 #include <string_view>
30 #include <stdlib.h>
32 #include <officecfg/Office/Common.hxx>
34 #ifdef _WIN32
35 #include <prewin.h>
36 #include <postwin.h>
37 #define OPENCL_DLL_NAME "OpenCL.dll"
38 #elif defined(MACOSX)
39 #define OPENCL_DLL_NAME nullptr
40 #else
41 #define OPENCL_DLL_NAME "libOpenCL.so.1"
42 #endif
44 #ifdef _WIN32_WINNT_WINBLUE
45 #include <VersionHelpers.h>
46 #endif
48 #define DEVICE_NAME_LENGTH 1024
49 #define DRIVER_VERSION_LENGTH 1024
50 #define PLATFORM_VERSION_LENGTH 1024
52 #define CHECK_OPENCL(status,name) \
53 if( status != CL_SUCCESS ) \
54 { \
55 SAL_WARN( "opencl", "OpenCL error code " << status << " at " SAL_DETAIL_WHERE "from " name ); \
56 return false; \
59 namespace {
61 bool bIsInited = false;
65 namespace openclwrapper {
67 GPUEnv gpuEnv;
68 sal_uInt64 kernelFailures = 0;
70 namespace
73 OString generateMD5(const void* pData, size_t length)
75 sal_uInt8 pBuffer[RTL_DIGEST_LENGTH_MD5];
76 rtlDigestError aError = rtl_digest_MD5(pData, length,
77 pBuffer, RTL_DIGEST_LENGTH_MD5);
78 SAL_WARN_IF(aError != rtl_Digest_E_None, "opencl", "md5 generation failed");
80 OStringBuffer aBuffer(length * 2);
81 const char* const pString = "0123456789ABCDEF";
82 for(sal_uInt8 val : pBuffer)
84 aBuffer.append(OStringChar(pString[val/16]) + OStringChar(pString[val%16]));
86 return aBuffer.makeStringAndClear();
89 // workaround segfault with XCode 14
90 OString get_CacheFolder_impl()
92 OUString url(u"${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/cache/"_ustr);
93 rtl::Bootstrap::expandMacros(url);
95 osl::Directory::create(url);
97 return OUStringToOString(url, RTL_TEXTENCODING_UTF8);
100 OString const & getCacheFolder()
102 static OString const aCacheFolder = get_CacheFolder_impl();
103 return aCacheFolder;
108 static bool initializeCommandQueue(GPUEnv& aGpuEnv)
110 OpenCLZone zone;
112 cl_int nState;
113 cl_command_queue command_queue[OPENCL_CMDQUEUE_SIZE];
115 for (int i = 0; i < OPENCL_CMDQUEUE_SIZE; ++i)
117 command_queue[i] = clCreateCommandQueue(aGpuEnv.mpContext, aGpuEnv.mpDevID, 0, &nState);
118 if (nState != CL_SUCCESS)
119 SAL_WARN("opencl", "clCreateCommandQueue failed: " << errorString(nState));
121 if (command_queue[i] == nullptr || nState != CL_SUCCESS)
123 // Release all command queues created so far.
124 for (int j = 0; j <= i; ++j)
126 if (command_queue[j])
128 clReleaseCommandQueue(command_queue[j]);
129 command_queue[j] = nullptr;
133 clReleaseContext(aGpuEnv.mpContext);
134 SAL_WARN("opencl", "failed to set/switch opencl device");
135 return false;
138 SAL_INFO("opencl", "Created command queue " << command_queue[i] << " for context " << aGpuEnv.mpContext);
141 for (int i = 0; i < OPENCL_CMDQUEUE_SIZE; ++i)
143 aGpuEnv.mpCmdQueue[i] = command_queue[i];
145 aGpuEnv.mbCommandQueueInitialized = true;
146 return true;
149 void setKernelEnv( KernelEnv *envInfo )
151 if (!gpuEnv.mbCommandQueueInitialized)
153 initializeCommandQueue(gpuEnv);
156 envInfo->mpkContext = gpuEnv.mpContext;
157 envInfo->mpkProgram = gpuEnv.mpArryPrograms[0];
159 assert(gpuEnv.mnCmdQueuePos < OPENCL_CMDQUEUE_SIZE);
160 envInfo->mpkCmdQueue = gpuEnv.mpCmdQueue[gpuEnv.mnCmdQueuePos];
163 namespace {
165 OString createFileName(cl_device_id deviceId, const char* clFileName)
167 OString fileName(clFileName);
168 sal_Int32 nIndex = fileName.lastIndexOf(".cl");
169 if(nIndex > 0)
170 fileName = fileName.copy(0, nIndex);
172 char deviceName[DEVICE_NAME_LENGTH] = {0};
173 clGetDeviceInfo(deviceId, CL_DEVICE_NAME,
174 sizeof(deviceName), deviceName, nullptr);
176 char driverVersion[DRIVER_VERSION_LENGTH] = {0};
177 clGetDeviceInfo(deviceId, CL_DRIVER_VERSION,
178 sizeof(driverVersion), driverVersion, nullptr);
180 cl_platform_id platformId;
181 clGetDeviceInfo(deviceId, CL_DEVICE_PLATFORM,
182 sizeof(platformId), &platformId, nullptr);
184 char platformVersion[PLATFORM_VERSION_LENGTH] = {0};
185 clGetPlatformInfo(platformId, CL_PLATFORM_VERSION, sizeof(platformVersion),
186 platformVersion, nullptr);
188 // create hash for deviceName + driver version + platform version
189 OString aString = OString::Concat(deviceName) + driverVersion + platformVersion;
190 OString aHash = generateMD5(aString.getStr(), aString.getLength());
192 return getCacheFolder() + fileName + "-" + aHash + ".bin";
195 std::vector<std::shared_ptr<osl::File> > binaryGenerated( const char * clFileName, cl_context context )
197 size_t numDevices=0;
199 std::vector<std::shared_ptr<osl::File> > aGeneratedFiles;
200 cl_int clStatus = clGetContextInfo( context, CL_CONTEXT_DEVICES,
201 0, nullptr, &numDevices );
202 numDevices /= sizeof(numDevices);
204 if(clStatus != CL_SUCCESS)
205 return aGeneratedFiles;
207 assert(numDevices == 1);
209 // grab the handle to the device in the context.
210 cl_device_id pDevID;
211 clStatus = clGetContextInfo( context, CL_CONTEXT_DEVICES,
212 sizeof( cl_device_id ), &pDevID, nullptr );
214 if(clStatus != CL_SUCCESS)
215 return aGeneratedFiles;
217 assert(pDevID == gpuEnv.mpDevID);
219 OString fileName = createFileName(gpuEnv.mpDevID, clFileName);
220 auto pNewFile = std::make_shared<osl::File>(OStringToOUString(fileName, RTL_TEXTENCODING_UTF8));
221 if(pNewFile->open(osl_File_OpenFlag_Read) == osl::FileBase::E_None)
223 aGeneratedFiles.push_back(pNewFile);
224 SAL_INFO("opencl.file", "Opening binary file '" << fileName << "' for reading: success");
226 else
228 SAL_INFO("opencl.file", "Opening binary file '" << fileName << "' for reading: FAIL");
231 return aGeneratedFiles;
234 bool writeBinaryToFile( std::string_view rFileName, const char* binary, size_t numBytes )
236 osl::File file(OStringToOUString(rFileName, RTL_TEXTENCODING_UTF8));
237 osl::FileBase::RC status = file.open(
238 osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
240 if(status != osl::FileBase::E_None)
241 return false;
243 sal_uInt64 nBytesWritten = 0;
244 file.write( binary, numBytes, nBytesWritten );
246 assert(numBytes == nBytesWritten);
248 return true;
253 bool generatBinFromKernelSource( cl_program program, const char * clFileName )
255 cl_uint numDevices;
257 cl_int clStatus = clGetProgramInfo( program, CL_PROGRAM_NUM_DEVICES,
258 sizeof(numDevices), &numDevices, nullptr );
259 CHECK_OPENCL( clStatus, "clGetProgramInfo" );
261 assert(numDevices == 1);
263 cl_device_id pDevID;
264 /* grab the handle to the device in the program. */
265 clStatus = clGetProgramInfo( program, CL_PROGRAM_DEVICES,
266 sizeof(cl_device_id), &pDevID, nullptr );
267 CHECK_OPENCL( clStatus, "clGetProgramInfo" );
269 /* figure out the size of the binary. */
270 size_t binarySize;
272 clStatus = clGetProgramInfo( program, CL_PROGRAM_BINARY_SIZES,
273 sizeof(size_t), &binarySize, nullptr );
274 CHECK_OPENCL( clStatus, "clGetProgramInfo" );
276 /* copy over the generated binary. */
277 if ( binarySize != 0 )
279 std::unique_ptr<char[]> binary(new char[binarySize]);
280 clStatus = clGetProgramInfo( program, CL_PROGRAM_BINARIES,
281 sizeof(char *), &binary, nullptr );
282 CHECK_OPENCL(clStatus,"clGetProgramInfo");
284 OString fileName = createFileName(pDevID, clFileName);
285 if ( !writeBinaryToFile( fileName,
286 binary.get(), binarySize ) )
287 SAL_INFO("opencl.file", "Writing binary file '" << fileName << "': FAIL");
288 else
289 SAL_INFO("opencl.file", "Writing binary file '" << fileName << "': success");
291 return true;
294 namespace {
296 struct OpenCLEnv
298 cl_platform_id mpOclPlatformID;
299 cl_context mpOclContext;
300 cl_device_id mpOclDevsID;
303 bool initOpenCLAttr( OpenCLEnv * env )
305 if ( gpuEnv.mnIsUserCreated )
306 return true;
308 gpuEnv.mpContext = env->mpOclContext;
309 gpuEnv.mpPlatformID = env->mpOclPlatformID;
310 gpuEnv.mpDevID = env->mpOclDevsID;
312 gpuEnv.mnIsUserCreated = 1;
314 gpuEnv.mbCommandQueueInitialized = false;
316 gpuEnv.mnCmdQueuePos = 0; // default to 0.
318 return false;
321 bool buildProgram(const char* buildOption, GPUEnv* gpuInfo, int idx)
323 cl_int clStatus;
324 //char options[512];
325 // create a cl program executable for all the devices specified
326 clStatus = clBuildProgram(gpuInfo->mpArryPrograms[idx], 1, &gpuInfo->mpDevID,
327 buildOption, nullptr, nullptr);
329 if ( clStatus != CL_SUCCESS )
331 size_t length;
332 clStatus = clGetProgramBuildInfo( gpuInfo->mpArryPrograms[idx], gpuInfo->mpDevID,
333 CL_PROGRAM_BUILD_LOG, 0, nullptr, &length);
334 if ( clStatus != CL_SUCCESS )
336 return false;
339 std::unique_ptr<char[]> buildLog(new char[length]);
340 clStatus = clGetProgramBuildInfo( gpuInfo->mpArryPrograms[idx], gpuInfo->mpDevID,
341 CL_PROGRAM_BUILD_LOG, length, buildLog.get(), &length );
342 if ( clStatus != CL_SUCCESS )
344 return false;
347 OString aBuildLogFileURL = getCacheFolder() + "kernel-build.log";
348 osl::File aBuildLogFile(OStringToOUString(aBuildLogFileURL, RTL_TEXTENCODING_UTF8));
349 osl::FileBase::RC status = aBuildLogFile.open(
350 osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
352 if(status != osl::FileBase::E_None)
353 return false;
355 sal_uInt64 nBytesWritten = 0;
356 aBuildLogFile.write( buildLog.get(), length, nBytesWritten );
358 return false;
361 return true;
366 bool buildProgramFromBinary(const char* buildOption, GPUEnv* gpuInfo, const char* filename, int idx)
368 size_t numDevices;
369 cl_int clStatus = clGetContextInfo( gpuInfo->mpContext, CL_CONTEXT_DEVICES,
370 0, nullptr, &numDevices );
371 numDevices /= sizeof(numDevices);
372 CHECK_OPENCL( clStatus, "clGetContextInfo" );
374 std::vector<std::shared_ptr<osl::File> > aGeneratedFiles = binaryGenerated(
375 filename, gpuInfo->mpContext );
377 if (aGeneratedFiles.size() == numDevices)
379 std::unique_ptr<size_t[]> length(new size_t[numDevices]);
380 std::unique_ptr<unsigned char*[]> pBinary(new unsigned char*[numDevices]);
381 for(size_t i = 0; i < numDevices; ++i)
383 sal_uInt64 nSize;
384 aGeneratedFiles[i]->getSize(nSize);
385 unsigned char* binary = new unsigned char[nSize];
386 sal_uInt64 nBytesRead;
387 aGeneratedFiles[i]->read(binary, nSize, nBytesRead);
388 if(nSize != nBytesRead)
389 assert(false);
391 length[i] = nBytesRead;
393 pBinary[i] = binary;
396 // grab the handles to all of the devices in the context.
397 std::unique_ptr<cl_device_id[]> pArryDevsID(new cl_device_id[numDevices]);
398 clStatus = clGetContextInfo( gpuInfo->mpContext, CL_CONTEXT_DEVICES,
399 sizeof( cl_device_id ) * numDevices, pArryDevsID.get(), nullptr );
401 if(clStatus != CL_SUCCESS)
403 for(size_t i = 0; i < numDevices; ++i)
405 delete[] pBinary[i];
407 return false;
410 cl_int binary_status;
412 gpuInfo->mpArryPrograms[idx] = clCreateProgramWithBinary( gpuInfo->mpContext,numDevices,
413 pArryDevsID.get(), length.get(), const_cast<const unsigned char**>(pBinary.get()),
414 &binary_status, &clStatus );
415 if(clStatus != CL_SUCCESS)
417 // something went wrong, fall back to compiling from source
418 return false;
420 SAL_INFO("opencl", "Created program " << gpuInfo->mpArryPrograms[idx] << " from binary");
421 for(size_t i = 0; i < numDevices; ++i)
423 delete[] pBinary[i];
427 if ( !gpuInfo->mpArryPrograms[idx] )
429 return false;
431 return buildProgram(buildOption, gpuInfo, idx);
434 namespace {
436 void checkDeviceForDoubleSupport(cl_device_id deviceId, bool& bKhrFp64, bool& bAmdFp64)
438 OpenCLZone zone;
440 bKhrFp64 = false;
441 bAmdFp64 = false;
443 // Check device extensions for double type
444 size_t aDevExtInfoSize = 0;
446 cl_uint clStatus = clGetDeviceInfo( deviceId, CL_DEVICE_EXTENSIONS, 0, nullptr, &aDevExtInfoSize );
447 if( clStatus != CL_SUCCESS )
448 return;
450 std::unique_ptr<char[]> pExtInfo(new char[aDevExtInfoSize]);
452 clStatus = clGetDeviceInfo( deviceId, CL_DEVICE_EXTENSIONS,
453 sizeof(char) * aDevExtInfoSize, pExtInfo.get(), nullptr);
455 if( clStatus != CL_SUCCESS )
456 return;
458 if ( strstr( pExtInfo.get(), "cl_khr_fp64" ) )
460 bKhrFp64 = true;
462 else
464 // Check if cl_amd_fp64 extension is supported
465 if ( strstr( pExtInfo.get(), "cl_amd_fp64" ) )
466 bAmdFp64 = true;
470 bool initOpenCLRunEnv( GPUEnv *gpuInfo )
472 OpenCLZone zone;
473 cl_uint nPreferredVectorWidthFloat;
474 char pName[64];
476 bool bKhrFp64 = false;
477 bool bAmdFp64 = false;
479 checkDeviceForDoubleSupport(gpuInfo->mpDevID, bKhrFp64, bAmdFp64);
481 gpuInfo->mnKhrFp64Flag = bKhrFp64;
482 gpuInfo->mnAmdFp64Flag = bAmdFp64;
484 gpuInfo->mbNeedsTDRAvoidance = false;
486 clGetDeviceInfo(gpuInfo->mpDevID, CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT, sizeof(cl_uint),
487 &nPreferredVectorWidthFloat, nullptr);
488 SAL_INFO("opencl", "CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT=" << nPreferredVectorWidthFloat);
490 clGetPlatformInfo(gpuInfo->mpPlatformID, CL_PLATFORM_NAME, 64,
491 pName, nullptr);
493 #if defined (_WIN32)
494 // the Win32 SDK 8.1 deprecates GetVersionEx()
495 # ifdef _WIN32_WINNT_WINBLUE
496 const bool bIsNotWinOrIsWin8OrGreater = IsWindows8OrGreater();
497 # else
498 bool bIsNotWinOrIsWin8OrGreater = true;
499 OSVERSIONINFOW aVersionInfo = {};
500 aVersionInfo.dwOSVersionInfoSize = sizeof( aVersionInfo );
501 if (GetVersionExW( &aVersionInfo ))
503 // Windows 7 or lower?
504 if (aVersionInfo.dwMajorVersion < 6 ||
505 (aVersionInfo.dwMajorVersion == 6 && aVersionInfo.dwMinorVersion < 2))
506 bIsNotWinOrIsWin8OrGreater = false;
508 # endif
509 #else
510 const bool bIsNotWinOrIsWin8OrGreater = true;
511 #endif
513 // Heuristic: Certain old low-end OpenCL implementations don't
514 // work for us with too large group lengths. Looking at the preferred
515 // float vector width seems to be a way to detect these devices, except
516 // the non-working NVIDIA cards on Windows older than version 8.
517 gpuInfo->mbNeedsTDRAvoidance = ( nPreferredVectorWidthFloat == 4 ) ||
518 ( !bIsNotWinOrIsWin8OrGreater &&
519 OUString::createFromAscii(pName).indexOf("NVIDIA") > -1 );
521 size_t nMaxParameterSize;
522 clGetDeviceInfo(gpuInfo->mpDevID, CL_DEVICE_MAX_PARAMETER_SIZE, sizeof(size_t),
523 &nMaxParameterSize, nullptr);
524 SAL_INFO("opencl", "CL_DEVICE_MAX_PARAMETER_SIZE=" << nMaxParameterSize);
526 return false;
529 bool initOpenCLRunEnv( int argc )
531 if ( ( argc > MAX_CLFILE_NUM ) || ( argc < 0 ) )
532 return true;
534 if ( !bIsInited )
536 if ( !gpuEnv.mnIsUserCreated )
537 memset( &gpuEnv, 0, sizeof(gpuEnv) );
539 //initialize devices, context, command_queue
540 bool status = initOpenCLRunEnv( &gpuEnv );
541 if ( status )
543 return true;
545 //initialize program, kernelName, kernelCount
546 if( getenv( "SC_FLOAT" ) )
548 gpuEnv.mnKhrFp64Flag = false;
549 gpuEnv.mnAmdFp64Flag = false;
551 if( gpuEnv.mnKhrFp64Flag )
553 SAL_INFO("opencl", "Use Khr double");
555 else if( gpuEnv.mnAmdFp64Flag )
557 SAL_INFO("opencl", "Use AMD double type");
559 else
561 SAL_INFO("opencl", "USE float type");
563 bIsInited = true;
565 return false;
568 // based on crashes and hanging during kernel compilation
569 void createDeviceInfo(cl_device_id aDeviceId, OpenCLPlatformInfo& rPlatformInfo)
571 OpenCLDeviceInfo aDeviceInfo;
572 aDeviceInfo.device = aDeviceId;
574 char pName[DEVICE_NAME_LENGTH];
575 cl_int nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_NAME, DEVICE_NAME_LENGTH, pName, nullptr);
576 if(nState != CL_SUCCESS)
577 return;
579 aDeviceInfo.maName = OUString::createFromAscii(pName);
581 char pVendor[DEVICE_NAME_LENGTH];
582 nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_VENDOR, DEVICE_NAME_LENGTH, pVendor, nullptr);
583 if(nState != CL_SUCCESS)
584 return;
586 aDeviceInfo.maVendor = OUString::createFromAscii(pVendor);
588 cl_ulong nMemSize;
589 nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(nMemSize), &nMemSize, nullptr);
590 if(nState != CL_SUCCESS)
591 return;
593 aDeviceInfo.mnMemory = nMemSize;
595 cl_uint nClockFrequency;
596 nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(nClockFrequency), &nClockFrequency, nullptr);
597 if(nState != CL_SUCCESS)
598 return;
600 aDeviceInfo.mnFrequency = nClockFrequency;
602 cl_uint nComputeUnits;
603 nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(nComputeUnits), &nComputeUnits, nullptr);
604 if(nState != CL_SUCCESS)
605 return;
607 char pDriver[DEVICE_NAME_LENGTH];
608 nState = clGetDeviceInfo(aDeviceId, CL_DRIVER_VERSION, DEVICE_NAME_LENGTH, pDriver, nullptr);
610 if(nState != CL_SUCCESS)
611 return;
613 aDeviceInfo.maDriver = OUString::createFromAscii(pDriver);
615 bool bKhrFp64 = false;
616 bool bAmdFp64 = false;
617 checkDeviceForDoubleSupport(aDeviceId, bKhrFp64, bAmdFp64);
619 // only list devices that support double
620 if(!bKhrFp64 && !bAmdFp64)
621 return;
623 aDeviceInfo.mnComputeUnits = nComputeUnits;
625 if(!OpenCLConfig::get().checkImplementation(rPlatformInfo, aDeviceInfo))
626 rPlatformInfo.maDevices.push_back(aDeviceInfo);
629 bool createPlatformInfo(cl_platform_id nPlatformId, OpenCLPlatformInfo& rPlatformInfo)
631 rPlatformInfo.platform = nPlatformId;
632 char pName[64];
633 cl_int nState = clGetPlatformInfo(nPlatformId, CL_PLATFORM_NAME, 64,
634 pName, nullptr);
635 if(nState != CL_SUCCESS)
636 return false;
637 rPlatformInfo.maName = OUString::createFromAscii(pName);
639 char pVendor[64];
640 nState = clGetPlatformInfo(nPlatformId, CL_PLATFORM_VENDOR, 64,
641 pVendor, nullptr);
642 if(nState != CL_SUCCESS)
643 return false;
645 rPlatformInfo.maVendor = OUString::createFromAscii(pVendor);
647 cl_uint nDevices;
648 nState = clGetDeviceIDs(nPlatformId, CL_DEVICE_TYPE_ALL, 0, nullptr, &nDevices);
649 if(nState != CL_SUCCESS)
650 return false;
652 // memory leak that does not matter
653 // memory is stored in static variable that lives through the whole program
654 cl_device_id* pDevices = new cl_device_id[nDevices];
655 nState = clGetDeviceIDs(nPlatformId, CL_DEVICE_TYPE_ALL, nDevices, pDevices, nullptr);
656 if(nState != CL_SUCCESS)
657 return false;
659 for(size_t i = 0; i < nDevices; ++i)
661 createDeviceInfo(pDevices[i], rPlatformInfo);
664 return true;
669 const std::vector<OpenCLPlatformInfo>& fillOpenCLInfo()
671 static std::vector<OpenCLPlatformInfo> aPlatforms;
673 // return early if we already initialized or can't use OpenCL
674 if (!aPlatforms.empty() || !canUseOpenCL())
675 return aPlatforms;
677 int status = clewInit(OPENCL_DLL_NAME);
678 if (status < 0)
679 return aPlatforms;
681 cl_uint nPlatforms;
682 cl_int nState = clGetPlatformIDs(0, nullptr, &nPlatforms);
684 if(nState != CL_SUCCESS)
685 return aPlatforms;
687 // memory leak that does not matter,
688 // memory is stored in static instance aPlatforms
689 cl_platform_id* pPlatforms = new cl_platform_id[nPlatforms];
690 nState = clGetPlatformIDs(nPlatforms, pPlatforms, nullptr);
692 if(nState != CL_SUCCESS)
693 return aPlatforms;
695 for(size_t i = 0; i < nPlatforms; ++i)
697 OpenCLPlatformInfo aPlatformInfo;
698 if(createPlatformInfo(pPlatforms[i], aPlatformInfo))
699 aPlatforms.push_back(aPlatformInfo);
702 return aPlatforms;
705 namespace {
707 cl_device_id findDeviceIdByDeviceString(std::u16string_view rString, const std::vector<OpenCLPlatformInfo>& rPlatforms)
709 for (const OpenCLPlatformInfo& rPlatform : rPlatforms)
711 for (const OpenCLDeviceInfo& rDeviceInfo : rPlatform.maDevices)
713 OUString aDeviceId = rDeviceInfo.maVendor + " " + rDeviceInfo.maName;
714 if (rString == aDeviceId)
716 return rDeviceInfo.device;
721 return nullptr;
724 void findDeviceInfoFromDeviceId(cl_device_id aDeviceId, size_t& rDeviceId, size_t& rPlatformId)
726 cl_platform_id platformId;
727 cl_int nState = clGetDeviceInfo(aDeviceId, CL_DEVICE_PLATFORM,
728 sizeof(platformId), &platformId, nullptr);
730 if(nState != CL_SUCCESS)
731 return;
733 const std::vector<OpenCLPlatformInfo>& rPlatforms = fillOpenCLInfo();
734 for(size_t i = 0; i < rPlatforms.size(); ++i)
736 cl_platform_id platId = rPlatforms[i].platform;
737 if(platId != platformId)
738 continue;
740 for(size_t j = 0; j < rPlatforms[i].maDevices.size(); ++j)
742 cl_device_id id = rPlatforms[i].maDevices[j].device;
743 if(id == aDeviceId)
745 rDeviceId = j;
746 rPlatformId = i;
747 return;
755 bool canUseOpenCL()
757 if( const char* env = getenv( "SC_FORCE_CALCULATION" ))
759 if( strcmp( env, "opencl" ) == 0 )
760 return true;
762 return !getenv("SAL_DISABLE_OPENCL") && officecfg::Office::Common::Misc::UseOpenCL::get();
765 bool switchOpenCLDevice(std::u16string_view aDevice, bool bAutoSelect, bool bForceEvaluation, OUString& rOutSelectedDeviceVersionIDString)
767 if (!canUseOpenCL() || fillOpenCLInfo().empty())
768 return false;
770 cl_device_id pDeviceId = findDeviceIdByDeviceString(aDevice, fillOpenCLInfo());
772 if(!pDeviceId || bAutoSelect)
774 int status = clewInit(OPENCL_DLL_NAME);
775 if (status < 0)
776 return false;
778 OUString url(OStringToOUString(getCacheFolder(), RTL_TEXTENCODING_UTF8));
779 OUString path;
780 osl::FileBase::getSystemPathFromFileURL(url,path);
781 ds_device aSelectedDevice = getDeviceSelection(path, bForceEvaluation);
782 if ( aSelectedDevice.eType != DeviceType::OpenCLDevice)
783 return false;
784 pDeviceId = aSelectedDevice.aDeviceID;
787 if(gpuEnv.mpDevID == pDeviceId)
789 // we don't need to change anything
790 // still the same device
791 return pDeviceId != nullptr;
794 cl_context context;
795 cl_platform_id platformId;
798 OpenCLZone zone;
799 cl_int nState = clGetDeviceInfo(pDeviceId, CL_DEVICE_PLATFORM,
800 sizeof(platformId), &platformId, nullptr);
802 cl_context_properties cps[3];
803 cps[0] = CL_CONTEXT_PLATFORM;
804 cps[1] = reinterpret_cast<cl_context_properties>(platformId);
805 cps[2] = 0;
806 context = clCreateContext( cps, 1, &pDeviceId, nullptr, nullptr, &nState );
807 if (nState != CL_SUCCESS)
808 SAL_WARN("opencl", "clCreateContext failed: " << errorString(nState));
810 if(nState != CL_SUCCESS || context == nullptr)
812 if(context != nullptr)
813 clReleaseContext(context);
815 SAL_WARN("opencl", "failed to set/switch opencl device");
816 return false;
818 SAL_INFO("opencl", "Created context " << context << " for platform " << platformId << ", device " << pDeviceId);
820 OString sDeviceID = getDeviceInfoString(pDeviceId, CL_DEVICE_VENDOR) + " " + getDeviceInfoString(pDeviceId, CL_DRIVER_VERSION);
821 rOutSelectedDeviceVersionIDString = OStringToOUString(sDeviceID, RTL_TEXTENCODING_UTF8);
824 setOpenCLCmdQueuePosition(0); // Call this just to avoid the method being deleted from unused function deleter.
826 releaseOpenCLEnv(&gpuEnv);
828 OpenCLEnv env;
829 env.mpOclPlatformID = platformId;
830 env.mpOclContext = context;
831 env.mpOclDevsID = pDeviceId;
833 initOpenCLAttr(&env);
835 return !initOpenCLRunEnv(0);
838 void getOpenCLDeviceInfo(size_t& rDeviceId, size_t& rPlatformId)
840 if (!canUseOpenCL())
841 return;
843 int status = clewInit(OPENCL_DLL_NAME);
844 if (status < 0)
845 return;
847 cl_device_id id = gpuEnv.mpDevID;
848 findDeviceInfoFromDeviceId(id, rDeviceId, rPlatformId);
851 void getOpenCLDeviceName(OUString& rDeviceName, OUString& rPlatformName)
853 if (!canUseOpenCL())
854 return;
856 int status = clewInit(OPENCL_DLL_NAME);
857 if (status < 0)
858 return;
860 cl_device_id deviceId = gpuEnv.mpDevID;
861 cl_platform_id platformId;
862 if( clGetDeviceInfo(deviceId, CL_DEVICE_PLATFORM, sizeof(platformId), &platformId, nullptr) != CL_SUCCESS )
863 return;
865 char deviceName[DEVICE_NAME_LENGTH] = {0};
866 if( clGetDeviceInfo(deviceId, CL_DEVICE_NAME, sizeof(deviceName), deviceName, nullptr) != CL_SUCCESS )
867 return;
868 char platformName[64];
869 if( clGetPlatformInfo(platformId, CL_PLATFORM_NAME, 64, platformName, nullptr) != CL_SUCCESS )
870 return;
871 rDeviceName = OUString::createFromAscii(deviceName);
872 rPlatformName = OUString::createFromAscii(platformName);
875 void setOpenCLCmdQueuePosition( int nPos )
877 if (nPos < 0 || nPos >= OPENCL_CMDQUEUE_SIZE)
878 // Out of range. Ignore this.
879 return;
881 gpuEnv.mnCmdQueuePos = nPos;
884 const char* errorString(cl_int nError)
886 #define CASE(val) case CL_##val: return #val
887 switch (nError)
889 CASE(SUCCESS);
890 CASE(DEVICE_NOT_FOUND);
891 CASE(DEVICE_NOT_AVAILABLE);
892 CASE(COMPILER_NOT_AVAILABLE);
893 CASE(MEM_OBJECT_ALLOCATION_FAILURE);
894 CASE(OUT_OF_RESOURCES);
895 CASE(OUT_OF_HOST_MEMORY);
896 CASE(PROFILING_INFO_NOT_AVAILABLE);
897 CASE(MEM_COPY_OVERLAP);
898 CASE(IMAGE_FORMAT_MISMATCH);
899 CASE(IMAGE_FORMAT_NOT_SUPPORTED);
900 CASE(BUILD_PROGRAM_FAILURE);
901 CASE(MAP_FAILURE);
902 CASE(INVALID_VALUE);
903 CASE(INVALID_DEVICE_TYPE);
904 CASE(INVALID_PLATFORM);
905 CASE(INVALID_DEVICE);
906 CASE(INVALID_CONTEXT);
907 CASE(INVALID_QUEUE_PROPERTIES);
908 CASE(INVALID_COMMAND_QUEUE);
909 CASE(INVALID_HOST_PTR);
910 CASE(INVALID_MEM_OBJECT);
911 CASE(INVALID_IMAGE_FORMAT_DESCRIPTOR);
912 CASE(INVALID_IMAGE_SIZE);
913 CASE(INVALID_SAMPLER);
914 CASE(INVALID_BINARY);
915 CASE(INVALID_BUILD_OPTIONS);
916 CASE(INVALID_PROGRAM);
917 CASE(INVALID_PROGRAM_EXECUTABLE);
918 CASE(INVALID_KERNEL_NAME);
919 CASE(INVALID_KERNEL_DEFINITION);
920 CASE(INVALID_KERNEL);
921 CASE(INVALID_ARG_INDEX);
922 CASE(INVALID_ARG_VALUE);
923 CASE(INVALID_ARG_SIZE);
924 CASE(INVALID_KERNEL_ARGS);
925 CASE(INVALID_WORK_DIMENSION);
926 CASE(INVALID_WORK_GROUP_SIZE);
927 CASE(INVALID_WORK_ITEM_SIZE);
928 CASE(INVALID_GLOBAL_OFFSET);
929 CASE(INVALID_EVENT_WAIT_LIST);
930 CASE(INVALID_EVENT);
931 CASE(INVALID_OPERATION);
932 CASE(INVALID_GL_OBJECT);
933 CASE(INVALID_BUFFER_SIZE);
934 CASE(INVALID_MIP_LEVEL);
935 CASE(INVALID_GLOBAL_WORK_SIZE);
936 default:
937 return "Unknown OpenCL error code";
939 #undef CASE
942 bool GPUEnv::isOpenCLEnabled()
944 return gpuEnv.mpDevID && gpuEnv.mpContext;
949 void releaseOpenCLEnv( openclwrapper::GPUEnv *gpuInfo )
951 OpenCLZone zone;
953 if ( !bIsInited )
955 return;
958 for (_cl_command_queue* & i : openclwrapper::gpuEnv.mpCmdQueue)
960 if (i)
962 clReleaseCommandQueue(i);
963 i = nullptr;
966 openclwrapper::gpuEnv.mnCmdQueuePos = 0;
968 if ( openclwrapper::gpuEnv.mpContext )
970 clReleaseContext( openclwrapper::gpuEnv.mpContext );
971 openclwrapper::gpuEnv.mpContext = nullptr;
973 bIsInited = false;
974 gpuInfo->mnIsUserCreated = 0;
977 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */