Skip to content

Commit 2deb9fc

Browse files
authored
Merge pull request #2111 from shimat/feature/ocl-finish
Expose basic OpenCL runtime controls
2 parents 8b87809 + b624ff1 commit 2deb9fc

5 files changed

Lines changed: 153 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
3+
4+
#pragma warning disable 1591
5+
#pragma warning disable CA1401 // P/Invokes should not be visible
6+
#pragma warning disable IDE1006 // Naming style
7+
8+
namespace OpenCvSharp.Internal;
9+
10+
static partial class NativeMethods
11+
{
12+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
13+
public static partial ExceptionStatus core_ocl_haveOpenCL(out int returnValue);
14+
15+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
16+
public static partial ExceptionStatus core_ocl_useOpenCL(out int returnValue);
17+
18+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
19+
public static partial ExceptionStatus core_ocl_setUseOpenCL(int flag);
20+
21+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
22+
public static partial ExceptionStatus core_ocl_finish();
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using OpenCvSharp.Internal;
2+
3+
namespace OpenCvSharp;
4+
5+
public static partial class Cv2
6+
{
7+
/// <summary>
8+
/// cv::ocl functions.
9+
/// </summary>
10+
public static partial class Ocl
11+
{
12+
/// <summary>
13+
/// Returns whether an OpenCL runtime with at least one platform is available.
14+
/// </summary>
15+
public static bool HaveOpenCL()
16+
{
17+
NativeMethods.HandleException(
18+
NativeMethods.core_ocl_haveOpenCL(out var returnValue));
19+
return returnValue != 0;
20+
}
21+
22+
/// <summary>
23+
/// Returns whether OpenCL is currently enabled for the calling thread.
24+
/// </summary>
25+
public static bool UseOpenCL()
26+
{
27+
NativeMethods.HandleException(
28+
NativeMethods.core_ocl_useOpenCL(out var returnValue));
29+
return returnValue != 0;
30+
}
31+
32+
/// <summary>
33+
/// Enables or disables OpenCL use for the calling thread.
34+
/// </summary>
35+
/// <param name="flag">True to enable OpenCL when a suitable runtime and device are available; otherwise, false.</param>
36+
public static void SetUseOpenCL(bool flag)
37+
{
38+
NativeMethods.HandleException(
39+
NativeMethods.core_ocl_setUseOpenCL(flag ? 1 : 0));
40+
}
41+
42+
/// <summary>
43+
/// Waits for all queued OpenCL operations in the current default queue to complete.
44+
/// </summary>
45+
/// <remarks>
46+
/// This method blocks the calling thread and is primarily useful at synchronization boundaries and when measuring OpenCL execution time.
47+
/// Calling it after every operation can reduce performance by preventing asynchronous execution.
48+
/// </remarks>
49+
public static void Finish()
50+
{
51+
NativeMethods.HandleException(
52+
NativeMethods.core_ocl_finish());
53+
}
54+
}
55+
}

src/OpenCvSharpExtern/core.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#include "core_Mat.h"
88
#include "core_UMat.h"
99
#include "core_MatExpr.h"
10+
#include "core_ocl.h"
1011
#include "core_OutputArray.h"
1112
#include "core_PCA.h"
1213
#include "core_SparseMat.h"
1314
#include "core_SVD.h"
14-
#include "core_LDA.h"
15+
#include "core_LDA.h"

src/OpenCvSharpExtern/core_ocl.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include "include_opencv.h"
4+
#include <opencv2/core/ocl.hpp>
5+
6+
// ReSharper disable CppInconsistentNaming
7+
// ReSharper disable CppNonInlineFunctionDefinitionInHeaderFile
8+
9+
CVAPI(ExceptionStatus) core_ocl_haveOpenCL(int* returnValue)
10+
{
11+
return cvTry([&] {
12+
*returnValue = cv::ocl::haveOpenCL() ? 1 : 0;
13+
});
14+
}
15+
16+
CVAPI(ExceptionStatus) core_ocl_useOpenCL(int* returnValue)
17+
{
18+
return cvTry([&] {
19+
*returnValue = cv::ocl::useOpenCL() ? 1 : 0;
20+
});
21+
}
22+
23+
CVAPI(ExceptionStatus) core_ocl_setUseOpenCL(int flag)
24+
{
25+
return cvTry([&] {
26+
cv::ocl::setUseOpenCL(flag != 0);
27+
});
28+
}
29+
30+
CVAPI(ExceptionStatus) core_ocl_finish()
31+
{
32+
return cvTry([] {
33+
cv::ocl::finish();
34+
});
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Xunit;
2+
3+
namespace OpenCvSharp.Tests.Core;
4+
5+
public class OclTest : TestBase
6+
{
7+
[Fact]
8+
public void RuntimeStatusCanBeQueriedAndRestored()
9+
{
10+
var initiallyEnabled = Cv2.Ocl.UseOpenCL();
11+
Assert.False(initiallyEnabled && !Cv2.Ocl.HaveOpenCL());
12+
13+
try
14+
{
15+
Cv2.Ocl.SetUseOpenCL(false);
16+
Assert.False(Cv2.Ocl.UseOpenCL());
17+
}
18+
finally
19+
{
20+
Cv2.Ocl.SetUseOpenCL(initiallyEnabled);
21+
}
22+
23+
Assert.Equal(initiallyEnabled, Cv2.Ocl.UseOpenCL());
24+
}
25+
26+
[Fact]
27+
public void FinishAfterUMatOperation()
28+
{
29+
using var src = new UMat(32, 32, MatType.CV_8UC1, Scalar.All(1));
30+
using var dst = new UMat();
31+
32+
Cv2.GaussianBlur(src, dst, new Size(5, 5), 0);
33+
Cv2.Ocl.Finish();
34+
35+
using var result = dst.GetMat(AccessFlag.READ);
36+
Assert.Equal(1, result.At<byte>(0, 0));
37+
}
38+
}

0 commit comments

Comments
 (0)