1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
28 #include <string_view>
32 #include <officecfg/Office/Common.hxx>
37 #define OPENCL_DLL_NAME "OpenCL.dll"
39 #define OPENCL_DLL_NAME nullptr
41 #define OPENCL_DLL_NAME "libOpenCL.so.1"
44 #ifdef _WIN32_WINNT_WINBLUE
45 #include <VersionHelpers.h>
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 ) \
55 SAL_WARN( "opencl", "OpenCL error code " << status << " at " SAL_DETAIL_WHERE "from " name ); \
61 bool bIsInited
= false;
65 namespace openclwrapper
{
68 sal_uInt64 kernelFailures
= 0;
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 OString
const & getCacheFolder()
91 static OString
const aCacheFolder
= []()
93 OUString
url("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/cache/");
94 rtl::Bootstrap::expandMacros(url
);
96 osl::Directory::create(url
);
98 return OUStringToOString(url
, RTL_TEXTENCODING_UTF8
);
105 static bool initializeCommandQueue(GPUEnv
& aGpuEnv
)
110 cl_command_queue command_queue
[OPENCL_CMDQUEUE_SIZE
];
112 for (int i
= 0; i
< OPENCL_CMDQUEUE_SIZE
; ++i
)
114 command_queue
[i
] = clCreateCommandQueue(aGpuEnv
.mpContext
, aGpuEnv
.mpDevID
, 0, &nState
);
115 if (nState
!= CL_SUCCESS
)
116 SAL_WARN("opencl", "clCreateCommandQueue failed: " << errorString(nState
));
118 if (command_queue
[i
] == nullptr || nState
!= CL_SUCCESS
)
120 // Release all command queues created so far.
121 for (int j
= 0; j
<= i
; ++j
)
123 if (command_queue
[j
])
125 clReleaseCommandQueue(command_queue
[j
]);
126 command_queue
[j
] = nullptr;
130 clReleaseContext(aGpuEnv
.mpContext
);
131 SAL_WARN("opencl", "failed to set/switch opencl device");
135 SAL_INFO("opencl", "Created command queue " << command_queue
[i
] << " for context " << aGpuEnv
.mpContext
);
138 for (int i
= 0; i
< OPENCL_CMDQUEUE_SIZE
; ++i
)
140 aGpuEnv
.mpCmdQueue
[i
] = command_queue
[i
];
142 aGpuEnv
.mbCommandQueueInitialized
= true;
146 void setKernelEnv( KernelEnv
*envInfo
)
148 if (!gpuEnv
.mbCommandQueueInitialized
)
150 initializeCommandQueue(gpuEnv
);
153 envInfo
->mpkContext
= gpuEnv
.mpContext
;
154 envInfo
->mpkProgram
= gpuEnv
.mpArryPrograms
[0];
156 assert(gpuEnv
.mnCmdQueuePos
< OPENCL_CMDQUEUE_SIZE
);
157 envInfo
->mpkCmdQueue
= gpuEnv
.mpCmdQueue
[gpuEnv
.mnCmdQueuePos
];
162 OString
createFileName(cl_device_id deviceId
, const char* clFileName
)
164 OString
fileName(clFileName
);
165 sal_Int32 nIndex
= fileName
.lastIndexOf(".cl");
167 fileName
= fileName
.copy(0, nIndex
);
169 char deviceName
[DEVICE_NAME_LENGTH
] = {0};
170 clGetDeviceInfo(deviceId
, CL_DEVICE_NAME
,
171 sizeof(deviceName
), deviceName
, nullptr);
173 char driverVersion
[DRIVER_VERSION_LENGTH
] = {0};
174 clGetDeviceInfo(deviceId
, CL_DRIVER_VERSION
,
175 sizeof(driverVersion
), driverVersion
, nullptr);
177 cl_platform_id platformId
;
178 clGetDeviceInfo(deviceId
, CL_DEVICE_PLATFORM
,
179 sizeof(platformId
), &platformId
, nullptr);
181 char platformVersion
[PLATFORM_VERSION_LENGTH
] = {0};
182 clGetPlatformInfo(platformId
, CL_PLATFORM_VERSION
, sizeof(platformVersion
),
183 platformVersion
, nullptr);
185 // create hash for deviceName + driver version + platform version
186 OString aString
= OString::Concat(deviceName
) + driverVersion
+ platformVersion
;
187 OString aHash
= generateMD5(aString
.getStr(), aString
.getLength());
189 return getCacheFolder() + fileName
+ "-" + aHash
+ ".bin";
192 std::vector
<std::shared_ptr
<osl::File
> > binaryGenerated( const char * clFileName
, cl_context context
)
196 std::vector
<std::shared_ptr
<osl::File
> > aGeneratedFiles
;
197 cl_int clStatus
= clGetContextInfo( context
, CL_CONTEXT_DEVICES
,
198 0, nullptr, &numDevices
);
199 numDevices
/= sizeof(numDevices
);
201 if(clStatus
!= CL_SUCCESS
)
202 return aGeneratedFiles
;
204 assert(numDevices
== 1);
206 // grab the handle to the device in the context.
208 clStatus
= clGetContextInfo( context
, CL_CONTEXT_DEVICES
,
209 sizeof( cl_device_id
), &pDevID
, nullptr );
211 if(clStatus
!= CL_SUCCESS
)
212 return aGeneratedFiles
;
214 assert(pDevID
== gpuEnv
.mpDevID
);
216 OString fileName
= createFileName(gpuEnv
.mpDevID
, clFileName
);
217 auto pNewFile
= std::make_shared
<osl::File
>(OStringToOUString(fileName
, RTL_TEXTENCODING_UTF8
));
218 if(pNewFile
->open(osl_File_OpenFlag_Read
) == osl::FileBase::E_None
)
220 aGeneratedFiles
.push_back(pNewFile
);
221 SAL_INFO("opencl.file", "Opening binary file '" << fileName
<< "' for reading: success");
225 SAL_INFO("opencl.file", "Opening binary file '" << fileName
<< "' for reading: FAIL");
228 return aGeneratedFiles
;
231 bool writeBinaryToFile( std::string_view rFileName
, const char* binary
, size_t numBytes
)
233 osl::File
file(OStringToOUString(rFileName
, RTL_TEXTENCODING_UTF8
));
234 osl::FileBase::RC status
= file
.open(
235 osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
);
237 if(status
!= osl::FileBase::E_None
)
240 sal_uInt64 nBytesWritten
= 0;
241 file
.write( binary
, numBytes
, nBytesWritten
);
243 assert(numBytes
== nBytesWritten
);
250 bool generatBinFromKernelSource( cl_program program
, const char * clFileName
)
254 cl_int clStatus
= clGetProgramInfo( program
, CL_PROGRAM_NUM_DEVICES
,
255 sizeof(numDevices
), &numDevices
, nullptr );
256 CHECK_OPENCL( clStatus
, "clGetProgramInfo" );
258 assert(numDevices
== 1);
261 /* grab the handle to the device in the program. */
262 clStatus
= clGetProgramInfo( program
, CL_PROGRAM_DEVICES
,
263 sizeof(cl_device_id
), &pDevID
, nullptr );
264 CHECK_OPENCL( clStatus
, "clGetProgramInfo" );
266 /* figure out the size of the binary. */
269 clStatus
= clGetProgramInfo( program
, CL_PROGRAM_BINARY_SIZES
,
270 sizeof(size_t), &binarySize
, nullptr );
271 CHECK_OPENCL( clStatus
, "clGetProgramInfo" );
273 /* copy over the generated binary. */
274 if ( binarySize
!= 0 )
276 std::unique_ptr
<char[]> binary(new char[binarySize
]);
277 clStatus
= clGetProgramInfo( program
, CL_PROGRAM_BINARIES
,
278 sizeof(char *), &binary
, nullptr );
279 CHECK_OPENCL(clStatus
,"clGetProgramInfo");
281 OString fileName
= createFileName(pDevID
, clFileName
);
282 if ( !writeBinaryToFile( fileName
,
283 binary
.get(), binarySize
) )
284 SAL_INFO("opencl.file", "Writing binary file '" << fileName
<< "': FAIL");
286 SAL_INFO("opencl.file", "Writing binary file '" << fileName
<< "': success");
295 cl_platform_id mpOclPlatformID
;
296 cl_context mpOclContext
;
297 cl_device_id mpOclDevsID
;
300 bool initOpenCLAttr( OpenCLEnv
* env
)
302 if ( gpuEnv
.mnIsUserCreated
)
305 gpuEnv
.mpContext
= env
->mpOclContext
;
306 gpuEnv
.mpPlatformID
= env
->mpOclPlatformID
;
307 gpuEnv
.mpDevID
= env
->mpOclDevsID
;
309 gpuEnv
.mnIsUserCreated
= 1;
311 gpuEnv
.mbCommandQueueInitialized
= false;
313 gpuEnv
.mnCmdQueuePos
= 0; // default to 0.
318 bool buildProgram(const char* buildOption
, GPUEnv
* gpuInfo
, int idx
)
322 // create a cl program executable for all the devices specified
323 clStatus
= clBuildProgram(gpuInfo
->mpArryPrograms
[idx
], 1, &gpuInfo
->mpDevID
,
324 buildOption
, nullptr, nullptr);
326 if ( clStatus
!= CL_SUCCESS
)
329 clStatus
= clGetProgramBuildInfo( gpuInfo
->mpArryPrograms
[idx
], gpuInfo
->mpDevID
,
330 CL_PROGRAM_BUILD_LOG
, 0, nullptr, &length
);
331 if ( clStatus
!= CL_SUCCESS
)
336 std::unique_ptr
<char[]> buildLog(new char[length
]);
337 clStatus
= clGetProgramBuildInfo( gpuInfo
->mpArryPrograms
[idx
], gpuInfo
->mpDevID
,
338 CL_PROGRAM_BUILD_LOG
, length
, buildLog
.get(), &length
);
339 if ( clStatus
!= CL_SUCCESS
)
344 OString aBuildLogFileURL
= getCacheFolder() + "kernel-build.log";
345 osl::File
aBuildLogFile(OStringToOUString(aBuildLogFileURL
, RTL_TEXTENCODING_UTF8
));
346 osl::FileBase::RC status
= aBuildLogFile
.open(
347 osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
);
349 if(status
!= osl::FileBase::E_None
)
352 sal_uInt64 nBytesWritten
= 0;
353 aBuildLogFile
.write( buildLog
.get(), length
, nBytesWritten
);
363 bool buildProgramFromBinary(const char* buildOption
, GPUEnv
* gpuInfo
, const char* filename
, int idx
)
366 cl_int clStatus
= clGetContextInfo( gpuInfo
->mpContext
, CL_CONTEXT_DEVICES
,
367 0, nullptr, &numDevices
);
368 numDevices
/= sizeof(numDevices
);
369 CHECK_OPENCL( clStatus
, "clGetContextInfo" );
371 std::vector
<std::shared_ptr
<osl::File
> > aGeneratedFiles
= binaryGenerated(
372 filename
, gpuInfo
->mpContext
);
374 if (aGeneratedFiles
.size() == numDevices
)
376 std::unique_ptr
<size_t[]> length(new size_t[numDevices
]);
377 std::unique_ptr
<unsigned char*[]> pBinary(new unsigned char*[numDevices
]);
378 for(size_t i
= 0; i
< numDevices
; ++i
)
381 aGeneratedFiles
[i
]->getSize(nSize
);
382 unsigned char* binary
= new unsigned char[nSize
];
383 sal_uInt64 nBytesRead
;
384 aGeneratedFiles
[i
]->read(binary
, nSize
, nBytesRead
);
385 if(nSize
!= nBytesRead
)
388 length
[i
] = nBytesRead
;
393 // grab the handles to all of the devices in the context.
394 std::unique_ptr
<cl_device_id
[]> pArryDevsID(new cl_device_id
[numDevices
]);
395 clStatus
= clGetContextInfo( gpuInfo
->mpContext
, CL_CONTEXT_DEVICES
,
396 sizeof( cl_device_id
) * numDevices
, pArryDevsID
.get(), nullptr );
398 if(clStatus
!= CL_SUCCESS
)
400 for(size_t i
= 0; i
< numDevices
; ++i
)
407 cl_int binary_status
;
409 gpuInfo
->mpArryPrograms
[idx
] = clCreateProgramWithBinary( gpuInfo
->mpContext
,numDevices
,
410 pArryDevsID
.get(), length
.get(), const_cast<const unsigned char**>(pBinary
.get()),
411 &binary_status
, &clStatus
);
412 if(clStatus
!= CL_SUCCESS
)
414 // something went wrong, fall back to compiling from source
417 SAL_INFO("opencl", "Created program " << gpuInfo
->mpArryPrograms
[idx
] << " from binary");
418 for(size_t i
= 0; i
< numDevices
; ++i
)
424 if ( !gpuInfo
->mpArryPrograms
[idx
] )
428 return buildProgram(buildOption
, gpuInfo
, idx
);
433 void checkDeviceForDoubleSupport(cl_device_id deviceId
, bool& bKhrFp64
, bool& bAmdFp64
)
440 // Check device extensions for double type
441 size_t aDevExtInfoSize
= 0;
443 cl_uint clStatus
= clGetDeviceInfo( deviceId
, CL_DEVICE_EXTENSIONS
, 0, nullptr, &aDevExtInfoSize
);
444 if( clStatus
!= CL_SUCCESS
)
447 std::unique_ptr
<char[]> pExtInfo(new char[aDevExtInfoSize
]);
449 clStatus
= clGetDeviceInfo( deviceId
, CL_DEVICE_EXTENSIONS
,
450 sizeof(char) * aDevExtInfoSize
, pExtInfo
.get(), nullptr);
452 if( clStatus
!= CL_SUCCESS
)
455 if ( strstr( pExtInfo
.get(), "cl_khr_fp64" ) )
461 // Check if cl_amd_fp64 extension is supported
462 if ( strstr( pExtInfo
.get(), "cl_amd_fp64" ) )
467 bool initOpenCLRunEnv( GPUEnv
*gpuInfo
)
470 cl_uint nPreferredVectorWidthFloat
;
473 bool bKhrFp64
= false;
474 bool bAmdFp64
= false;
476 checkDeviceForDoubleSupport(gpuInfo
->mpDevID
, bKhrFp64
, bAmdFp64
);
478 gpuInfo
->mnKhrFp64Flag
= bKhrFp64
;
479 gpuInfo
->mnAmdFp64Flag
= bAmdFp64
;
481 gpuInfo
->mbNeedsTDRAvoidance
= false;
483 clGetDeviceInfo(gpuInfo
->mpDevID
, CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT
, sizeof(cl_uint
),
484 &nPreferredVectorWidthFloat
, nullptr);
485 SAL_INFO("opencl", "CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT=" << nPreferredVectorWidthFloat
);
487 clGetPlatformInfo(gpuInfo
->mpPlatformID
, CL_PLATFORM_NAME
, 64,
491 // the Win32 SDK 8.1 deprecates GetVersionEx()
492 # ifdef _WIN32_WINNT_WINBLUE
493 const bool bIsNotWinOrIsWin8OrGreater
= IsWindows8OrGreater();
495 bool bIsNotWinOrIsWin8OrGreater
= true;
496 OSVERSIONINFOW aVersionInfo
= {};
497 aVersionInfo
.dwOSVersionInfoSize
= sizeof( aVersionInfo
);
498 if (GetVersionExW( &aVersionInfo
))
500 // Windows 7 or lower?
501 if (aVersionInfo
.dwMajorVersion
< 6 ||
502 (aVersionInfo
.dwMajorVersion
== 6 && aVersionInfo
.dwMinorVersion
< 2))
503 bIsNotWinOrIsWin8OrGreater
= false;
507 const bool bIsNotWinOrIsWin8OrGreater
= true;
510 // Heuristic: Certain old low-end OpenCL implementations don't
511 // work for us with too large group lengths. Looking at the preferred
512 // float vector width seems to be a way to detect these devices, except
513 // the non-working NVIDIA cards on Windows older than version 8.
514 gpuInfo
->mbNeedsTDRAvoidance
= ( nPreferredVectorWidthFloat
== 4 ) ||
515 ( !bIsNotWinOrIsWin8OrGreater
&&
516 OUString::createFromAscii(pName
).indexOf("NVIDIA") > -1 );
518 size_t nMaxParameterSize
;
519 clGetDeviceInfo(gpuInfo
->mpDevID
, CL_DEVICE_MAX_PARAMETER_SIZE
, sizeof(size_t),
520 &nMaxParameterSize
, nullptr);
521 SAL_INFO("opencl", "CL_DEVICE_MAX_PARAMETER_SIZE=" << nMaxParameterSize
);
526 bool initOpenCLRunEnv( int argc
)
528 if ( ( argc
> MAX_CLFILE_NUM
) || ( argc
< 0 ) )
533 if ( !gpuEnv
.mnIsUserCreated
)
534 memset( &gpuEnv
, 0, sizeof(gpuEnv
) );
536 //initialize devices, context, command_queue
537 bool status
= initOpenCLRunEnv( &gpuEnv
);
542 //initialize program, kernelName, kernelCount
543 if( getenv( "SC_FLOAT" ) )
545 gpuEnv
.mnKhrFp64Flag
= false;
546 gpuEnv
.mnAmdFp64Flag
= false;
548 if( gpuEnv
.mnKhrFp64Flag
)
550 SAL_INFO("opencl", "Use Khr double");
552 else if( gpuEnv
.mnAmdFp64Flag
)
554 SAL_INFO("opencl", "Use AMD double type");
558 SAL_INFO("opencl", "USE float type");
565 // based on crashes and hanging during kernel compilation
566 void createDeviceInfo(cl_device_id aDeviceId
, OpenCLPlatformInfo
& rPlatformInfo
)
568 OpenCLDeviceInfo aDeviceInfo
;
569 aDeviceInfo
.device
= aDeviceId
;
571 char pName
[DEVICE_NAME_LENGTH
];
572 cl_int nState
= clGetDeviceInfo(aDeviceId
, CL_DEVICE_NAME
, DEVICE_NAME_LENGTH
, pName
, nullptr);
573 if(nState
!= CL_SUCCESS
)
576 aDeviceInfo
.maName
= OUString::createFromAscii(pName
);
578 char pVendor
[DEVICE_NAME_LENGTH
];
579 nState
= clGetDeviceInfo(aDeviceId
, CL_DEVICE_VENDOR
, DEVICE_NAME_LENGTH
, pVendor
, nullptr);
580 if(nState
!= CL_SUCCESS
)
583 aDeviceInfo
.maVendor
= OUString::createFromAscii(pVendor
);
586 nState
= clGetDeviceInfo(aDeviceId
, CL_DEVICE_GLOBAL_MEM_SIZE
, sizeof(nMemSize
), &nMemSize
, nullptr);
587 if(nState
!= CL_SUCCESS
)
590 aDeviceInfo
.mnMemory
= nMemSize
;
592 cl_uint nClockFrequency
;
593 nState
= clGetDeviceInfo(aDeviceId
, CL_DEVICE_MAX_CLOCK_FREQUENCY
, sizeof(nClockFrequency
), &nClockFrequency
, nullptr);
594 if(nState
!= CL_SUCCESS
)
597 aDeviceInfo
.mnFrequency
= nClockFrequency
;
599 cl_uint nComputeUnits
;
600 nState
= clGetDeviceInfo(aDeviceId
, CL_DEVICE_MAX_COMPUTE_UNITS
, sizeof(nComputeUnits
), &nComputeUnits
, nullptr);
601 if(nState
!= CL_SUCCESS
)
604 char pDriver
[DEVICE_NAME_LENGTH
];
605 nState
= clGetDeviceInfo(aDeviceId
, CL_DRIVER_VERSION
, DEVICE_NAME_LENGTH
, pDriver
, nullptr);
607 if(nState
!= CL_SUCCESS
)
610 aDeviceInfo
.maDriver
= OUString::createFromAscii(pDriver
);
612 bool bKhrFp64
= false;
613 bool bAmdFp64
= false;
614 checkDeviceForDoubleSupport(aDeviceId
, bKhrFp64
, bAmdFp64
);
616 // only list devices that support double
617 if(!bKhrFp64
&& !bAmdFp64
)
620 aDeviceInfo
.mnComputeUnits
= nComputeUnits
;
622 if(!OpenCLConfig::get().checkImplementation(rPlatformInfo
, aDeviceInfo
))
623 rPlatformInfo
.maDevices
.push_back(aDeviceInfo
);
626 bool createPlatformInfo(cl_platform_id nPlatformId
, OpenCLPlatformInfo
& rPlatformInfo
)
628 rPlatformInfo
.platform
= nPlatformId
;
630 cl_int nState
= clGetPlatformInfo(nPlatformId
, CL_PLATFORM_NAME
, 64,
632 if(nState
!= CL_SUCCESS
)
634 rPlatformInfo
.maName
= OUString::createFromAscii(pName
);
637 nState
= clGetPlatformInfo(nPlatformId
, CL_PLATFORM_VENDOR
, 64,
639 if(nState
!= CL_SUCCESS
)
642 rPlatformInfo
.maVendor
= OUString::createFromAscii(pVendor
);
645 nState
= clGetDeviceIDs(nPlatformId
, CL_DEVICE_TYPE_ALL
, 0, nullptr, &nDevices
);
646 if(nState
!= CL_SUCCESS
)
649 // memory leak that does not matter
650 // memory is stored in static variable that lives through the whole program
651 cl_device_id
* pDevices
= new cl_device_id
[nDevices
];
652 nState
= clGetDeviceIDs(nPlatformId
, CL_DEVICE_TYPE_ALL
, nDevices
, pDevices
, nullptr);
653 if(nState
!= CL_SUCCESS
)
656 for(size_t i
= 0; i
< nDevices
; ++i
)
658 createDeviceInfo(pDevices
[i
], rPlatformInfo
);
666 const std::vector
<OpenCLPlatformInfo
>& fillOpenCLInfo()
668 static std::vector
<OpenCLPlatformInfo
> aPlatforms
;
670 // return early if we already initialized or can't use OpenCL
671 if (!aPlatforms
.empty() || !canUseOpenCL())
674 int status
= clewInit(OPENCL_DLL_NAME
);
679 cl_int nState
= clGetPlatformIDs(0, nullptr, &nPlatforms
);
681 if(nState
!= CL_SUCCESS
)
684 // memory leak that does not matter,
685 // memory is stored in static instance aPlatforms
686 cl_platform_id
* pPlatforms
= new cl_platform_id
[nPlatforms
];
687 nState
= clGetPlatformIDs(nPlatforms
, pPlatforms
, nullptr);
689 if(nState
!= CL_SUCCESS
)
692 for(size_t i
= 0; i
< nPlatforms
; ++i
)
694 OpenCLPlatformInfo aPlatformInfo
;
695 if(createPlatformInfo(pPlatforms
[i
], aPlatformInfo
))
696 aPlatforms
.push_back(aPlatformInfo
);
704 cl_device_id
findDeviceIdByDeviceString(std::u16string_view rString
, const std::vector
<OpenCLPlatformInfo
>& rPlatforms
)
706 for (const OpenCLPlatformInfo
& rPlatform
: rPlatforms
)
708 for (const OpenCLDeviceInfo
& rDeviceInfo
: rPlatform
.maDevices
)
710 OUString aDeviceId
= rDeviceInfo
.maVendor
+ " " + rDeviceInfo
.maName
;
711 if (rString
== aDeviceId
)
713 return rDeviceInfo
.device
;
721 void findDeviceInfoFromDeviceId(cl_device_id aDeviceId
, size_t& rDeviceId
, size_t& rPlatformId
)
723 cl_platform_id platformId
;
724 cl_int nState
= clGetDeviceInfo(aDeviceId
, CL_DEVICE_PLATFORM
,
725 sizeof(platformId
), &platformId
, nullptr);
727 if(nState
!= CL_SUCCESS
)
730 const std::vector
<OpenCLPlatformInfo
>& rPlatforms
= fillOpenCLInfo();
731 for(size_t i
= 0; i
< rPlatforms
.size(); ++i
)
733 cl_platform_id platId
= rPlatforms
[i
].platform
;
734 if(platId
!= platformId
)
737 for(size_t j
= 0; j
< rPlatforms
[i
].maDevices
.size(); ++j
)
739 cl_device_id id
= rPlatforms
[i
].maDevices
[j
].device
;
754 if( const char* env
= getenv( "SC_FORCE_CALCULATION" ))
756 if( strcmp( env
, "opencl" ) == 0 )
759 return !getenv("SAL_DISABLE_OPENCL") && officecfg::Office::Common::Misc::UseOpenCL::get();
762 bool switchOpenCLDevice(std::u16string_view aDevice
, bool bAutoSelect
, bool bForceEvaluation
, OUString
& rOutSelectedDeviceVersionIDString
)
764 if (!canUseOpenCL() || fillOpenCLInfo().empty())
767 cl_device_id pDeviceId
= findDeviceIdByDeviceString(aDevice
, fillOpenCLInfo());
769 if(!pDeviceId
|| bAutoSelect
)
771 int status
= clewInit(OPENCL_DLL_NAME
);
775 OUString
url(OStringToOUString(getCacheFolder(), RTL_TEXTENCODING_UTF8
));
777 osl::FileBase::getSystemPathFromFileURL(url
,path
);
778 ds_device aSelectedDevice
= getDeviceSelection(path
, bForceEvaluation
);
779 if ( aSelectedDevice
.eType
!= DeviceType::OpenCLDevice
)
781 pDeviceId
= aSelectedDevice
.aDeviceID
;
784 if(gpuEnv
.mpDevID
== pDeviceId
)
786 // we don't need to change anything
787 // still the same device
788 return pDeviceId
!= nullptr;
792 cl_platform_id platformId
;
796 cl_int nState
= clGetDeviceInfo(pDeviceId
, CL_DEVICE_PLATFORM
,
797 sizeof(platformId
), &platformId
, nullptr);
799 cl_context_properties cps
[3];
800 cps
[0] = CL_CONTEXT_PLATFORM
;
801 cps
[1] = reinterpret_cast<cl_context_properties
>(platformId
);
803 context
= clCreateContext( cps
, 1, &pDeviceId
, nullptr, nullptr, &nState
);
804 if (nState
!= CL_SUCCESS
)
805 SAL_WARN("opencl", "clCreateContext failed: " << errorString(nState
));
807 if(nState
!= CL_SUCCESS
|| context
== nullptr)
809 if(context
!= nullptr)
810 clReleaseContext(context
);
812 SAL_WARN("opencl", "failed to set/switch opencl device");
815 SAL_INFO("opencl", "Created context " << context
<< " for platform " << platformId
<< ", device " << pDeviceId
);
817 OString sDeviceID
= getDeviceInfoString(pDeviceId
, CL_DEVICE_VENDOR
) + " " + getDeviceInfoString(pDeviceId
, CL_DRIVER_VERSION
);
818 rOutSelectedDeviceVersionIDString
= OStringToOUString(sDeviceID
, RTL_TEXTENCODING_UTF8
);
821 setOpenCLCmdQueuePosition(0); // Call this just to avoid the method being deleted from unused function deleter.
823 releaseOpenCLEnv(&gpuEnv
);
826 env
.mpOclPlatformID
= platformId
;
827 env
.mpOclContext
= context
;
828 env
.mpOclDevsID
= pDeviceId
;
830 initOpenCLAttr(&env
);
832 return !initOpenCLRunEnv(0);
835 void getOpenCLDeviceInfo(size_t& rDeviceId
, size_t& rPlatformId
)
840 int status
= clewInit(OPENCL_DLL_NAME
);
844 cl_device_id id
= gpuEnv
.mpDevID
;
845 findDeviceInfoFromDeviceId(id
, rDeviceId
, rPlatformId
);
848 void getOpenCLDeviceName(OUString
& rDeviceName
, OUString
& rPlatformName
)
853 int status
= clewInit(OPENCL_DLL_NAME
);
857 cl_device_id deviceId
= gpuEnv
.mpDevID
;
858 cl_platform_id platformId
;
859 if( clGetDeviceInfo(deviceId
, CL_DEVICE_PLATFORM
, sizeof(platformId
), &platformId
, nullptr) != CL_SUCCESS
)
862 char deviceName
[DEVICE_NAME_LENGTH
] = {0};
863 if( clGetDeviceInfo(deviceId
, CL_DEVICE_NAME
, sizeof(deviceName
), deviceName
, nullptr) != CL_SUCCESS
)
865 char platformName
[64];
866 if( clGetPlatformInfo(platformId
, CL_PLATFORM_NAME
, 64, platformName
, nullptr) != CL_SUCCESS
)
868 rDeviceName
= OUString::createFromAscii(deviceName
);
869 rPlatformName
= OUString::createFromAscii(platformName
);
872 void setOpenCLCmdQueuePosition( int nPos
)
874 if (nPos
< 0 || nPos
>= OPENCL_CMDQUEUE_SIZE
)
875 // Out of range. Ignore this.
878 gpuEnv
.mnCmdQueuePos
= nPos
;
881 const char* errorString(cl_int nError
)
883 #define CASE(val) case CL_##val: return #val
887 CASE(DEVICE_NOT_FOUND
);
888 CASE(DEVICE_NOT_AVAILABLE
);
889 CASE(COMPILER_NOT_AVAILABLE
);
890 CASE(MEM_OBJECT_ALLOCATION_FAILURE
);
891 CASE(OUT_OF_RESOURCES
);
892 CASE(OUT_OF_HOST_MEMORY
);
893 CASE(PROFILING_INFO_NOT_AVAILABLE
);
894 CASE(MEM_COPY_OVERLAP
);
895 CASE(IMAGE_FORMAT_MISMATCH
);
896 CASE(IMAGE_FORMAT_NOT_SUPPORTED
);
897 CASE(BUILD_PROGRAM_FAILURE
);
900 CASE(INVALID_DEVICE_TYPE
);
901 CASE(INVALID_PLATFORM
);
902 CASE(INVALID_DEVICE
);
903 CASE(INVALID_CONTEXT
);
904 CASE(INVALID_QUEUE_PROPERTIES
);
905 CASE(INVALID_COMMAND_QUEUE
);
906 CASE(INVALID_HOST_PTR
);
907 CASE(INVALID_MEM_OBJECT
);
908 CASE(INVALID_IMAGE_FORMAT_DESCRIPTOR
);
909 CASE(INVALID_IMAGE_SIZE
);
910 CASE(INVALID_SAMPLER
);
911 CASE(INVALID_BINARY
);
912 CASE(INVALID_BUILD_OPTIONS
);
913 CASE(INVALID_PROGRAM
);
914 CASE(INVALID_PROGRAM_EXECUTABLE
);
915 CASE(INVALID_KERNEL_NAME
);
916 CASE(INVALID_KERNEL_DEFINITION
);
917 CASE(INVALID_KERNEL
);
918 CASE(INVALID_ARG_INDEX
);
919 CASE(INVALID_ARG_VALUE
);
920 CASE(INVALID_ARG_SIZE
);
921 CASE(INVALID_KERNEL_ARGS
);
922 CASE(INVALID_WORK_DIMENSION
);
923 CASE(INVALID_WORK_GROUP_SIZE
);
924 CASE(INVALID_WORK_ITEM_SIZE
);
925 CASE(INVALID_GLOBAL_OFFSET
);
926 CASE(INVALID_EVENT_WAIT_LIST
);
928 CASE(INVALID_OPERATION
);
929 CASE(INVALID_GL_OBJECT
);
930 CASE(INVALID_BUFFER_SIZE
);
931 CASE(INVALID_MIP_LEVEL
);
932 CASE(INVALID_GLOBAL_WORK_SIZE
);
934 return "Unknown OpenCL error code";
939 bool GPUEnv::isOpenCLEnabled()
941 return gpuEnv
.mpDevID
&& gpuEnv
.mpContext
;
946 void releaseOpenCLEnv( openclwrapper::GPUEnv
*gpuInfo
)
955 for (_cl_command_queue
* & i
: openclwrapper::gpuEnv
.mpCmdQueue
)
959 clReleaseCommandQueue(i
);
963 openclwrapper::gpuEnv
.mnCmdQueuePos
= 0;
965 if ( openclwrapper::gpuEnv
.mpContext
)
967 clReleaseContext( openclwrapper::gpuEnv
.mpContext
);
968 openclwrapper::gpuEnv
.mpContext
= nullptr;
971 gpuInfo
->mnIsUserCreated
= 0;
974 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */