aboutsummaryrefslogtreecommitdiff
path: root/src/core/CL/OpenCL.cpp
diff options
context:
space:
mode:
authorMarco Antognini <marco.antognini@arm.com>2021-03-23 16:59:08 +0000
committerMichele Di Giorgio <michele.digiorgio@arm.com>2021-04-08 16:29:20 +0000
commit4a5f73d36962f254ad630beb70deb1907ba381b9 (patch)
tree1a89ef8a7f47207f38bfbb4a998d7788f6273105 /src/core/CL/OpenCL.cpp
parentb71322dc037267219f95da406affd42e880a0cc6 (diff)
downloadComputeLibrary-4a5f73d36962f254ad630beb70deb1907ba381b9.tar.gz
Ensure OpenCL runtimes are initialized first
The OpenCL API Specification states: > The behavior of OpenCL API functions called from global constructors > or destructors is therefore implementation-defined. This patch improves compatibility with OpenCL runtimes that use static objects to hold their internal state. Change-Id: I850be378e9c6f0b5aa8db926fe0c62833a936724 Signed-off-by: Marco Antognini <marco.antognini@arm.com> Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/5383 Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com> Comments-Addressed: Sheri Zhang <sheri.zhang@arm.com> Tested-by: Arm Jenkins <bsgcomp@arm.com>
Diffstat (limited to 'src/core/CL/OpenCL.cpp')
-rw-r--r--src/core/CL/OpenCL.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/core/CL/OpenCL.cpp b/src/core/CL/OpenCL.cpp
index aff6285697..dd9960a7b3 100644
--- a/src/core/CL/OpenCL.cpp
+++ b/src/core/CL/OpenCL.cpp
@@ -152,6 +152,23 @@ bool CLSymbols::load(const std::string &library)
bool opencl_is_available()
{
CLSymbols::get().load_default();
+
+ // Using static objects that rely on OpenCL in their constructor or
+ // destructor is implementation defined according to the OpenCL API
+ // Specification. These objects include CLScheduler.
+ //
+ // For compatibility with OpenCL runtimes that also use static objects to
+ // hold their state, we call a harmless OpenCL function (clGetPlatformIDs
+ // with invalid parameters must result in CL_INVALID_VALUE) to ensure the
+ // runtimes have a chance to initialize their static objects first. Thanks
+ // to C++11 rules about normal program termination (cf [basic.start]), this
+ // ensures their static objects are destroyed last, i.e. after the
+ // singleton CLScheduler is destroyed.
+ //
+ // When OpenCL is not available, this call results in CL_OUT_OF_RESOURCES,
+ // which is equally harmless.
+ (void)clGetPlatformIDs(0, nullptr, nullptr);
+
return CLSymbols::get().clBuildProgram_ptr != nullptr;
}
} // namespace arm_compute