Skip to content

Commit c645944

Browse files
committed
Document OpenCL acceleration with UMat
1 parent a390f59 commit c645944

7 files changed

Lines changed: 153 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ PS1> Install-WindowsFeature Server-Media-Foundation
8686

8787
**OpenCvSharp does not support CUDA.** If you want to use CUDA features, you need to customize the native bindings yourself.
8888

89+
OpenCV's OpenCL Transparent API can be used through `UMat`. See [OpenCL Acceleration with UMat](docs/docfx/articles/guides/opencl-and-umat.md) for runtime diagnostics and benchmarking guidance.
90+
8991
## Installation
9092

9193
### Windows x64

docs/docfx/articles/guides/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ These guides cover the parts of using OpenCV that are specific to OpenCvSharp, C
88
- [Resource Management](resource-management.md) explains deterministic disposal and ownership transfer for native resources.
99
- [InputArray, OutputArray, and In-place Processing](input-output-arrays-and-in-place.md) explains temporary proxies, destination arguments, and safe in-place operations.
1010
- [Copies, Native Memory, and Performance](memory-copy-and-performance.md) distinguishes headers, views, clones, managed copies, and reusable buffers.
11+
- [OpenCL Acceleration with UMat](opencl-and-umat.md) explains runtime detection, device diagnostics, transfer boundaries, and synchronized benchmarking.
1112
- [Pixel Access](pixel-access.md) compares indexed, row-based, span-based, and pointer-based access.
1213

1314
## Application integration

docs/docfx/articles/guides/memory-copy-and-performance.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ When using BenchmarkDotNet or another harness:
217217

218218
Managed allocation counters do not include most `Mat` pixel storage. Use process memory, native profilers, or a controlled working-set test when native memory is the concern.
219219

220+
OpenCL operations using `UMat` may be queued asynchronously. Follow [OpenCL Acceleration with UMat](opencl-and-umat.md) to include device execution and synchronization in benchmark results.
221+
220222
## Choose an ownership operation
221223

222224
| Requirement | Preferred operation |
@@ -234,6 +236,7 @@ Managed allocation counters do not include most `Mat` pixel storage. Use process
234236
- [Pixel Access](pixel-access.md)
235237
- [InputArray, OutputArray, and In-place Processing](input-output-arrays-and-in-place.md)
236238
- [Resource Management](resource-management.md)
239+
- [OpenCL Acceleration with UMat](opencl-and-umat.md)
237240

238241
## Official OpenCV references
239242

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# OpenCL Acceleration with UMat
2+
3+
OpenCvSharp can use OpenCV's Transparent API (T-API) through `UMat`. When OpenCL is available and an operation has an OpenCL implementation, passing `UMat` inputs and outputs allows OpenCV to select that implementation without changing the `Cv2` method being called.
4+
5+
OpenCL acceleration is not the same as CUDA support. OpenCvSharp does not expose OpenCV's CUDA modules, and `UMat` uses OpenCL rather than CUDA.
6+
7+
## Treat acceleration as an execution choice, not a guarantee
8+
9+
Using `UMat` does not guarantee that an operation runs on a GPU or that it is faster than `Mat`:
10+
11+
- The operating system must provide a working OpenCL runtime and device driver.
12+
- OpenCV must have an OpenCL implementation for the operation, image type, and parameters.
13+
- OpenCV may reject an OpenCL path through its runtime performance checks and fall back to the CPU.
14+
- Kernel compilation, host-device transfers, and synchronization can outweigh the benefit for small images or short pipelines.
15+
- Performance varies by operation, device, and driver. An optimization that performs well on one vendor's device may behave differently on another.
16+
17+
Measure the complete production-shaped pipeline instead of assuming that replacing `Mat` with `UMat` will improve it.
18+
19+
## Check availability and the current state
20+
21+
Use `Cv2.Ocl` to inspect and control OpenCL use:
22+
23+
```csharp
24+
Console.WriteLine($"OpenCL available: {Cv2.Ocl.HaveOpenCL()}");
25+
Console.WriteLine($"OpenCL enabled: {Cv2.Ocl.UseOpenCL()}");
26+
27+
Cv2.Ocl.SetUseOpenCL(true);
28+
Console.WriteLine($"OpenCL enabled: {Cv2.Ocl.UseOpenCL()}");
29+
```
30+
31+
These methods answer different questions:
32+
33+
- `HaveOpenCL()` reports whether OpenCV can find an OpenCL runtime with at least one platform.
34+
- `UseOpenCL()` reports whether OpenCL is currently enabled for the calling thread.
35+
- `SetUseOpenCL(bool)` enables or disables OpenCL use for the calling thread.
36+
37+
An available and enabled runtime still does not prove that a particular operation used OpenCL. OpenCV can fall back to another implementation for that call.
38+
39+
## Inspect platforms and devices
40+
41+
`GetPlatformsInfo()` returns read-only snapshots of the OpenCL platforms and devices visible to OpenCV. It returns an empty list when no runtime is available.
42+
43+
```csharp
44+
foreach (var platform in Cv2.Ocl.GetPlatformsInfo())
45+
{
46+
Console.WriteLine(
47+
$"Platform: {platform.Name} ({platform.Vendor}, {platform.Version})");
48+
49+
foreach (var device in platform.Devices)
50+
{
51+
Console.WriteLine($" Device: {device.Name}");
52+
Console.WriteLine($" Type: {device.Type}");
53+
Console.WriteLine($" OpenCL: {device.OpenCLVersion}");
54+
Console.WriteLine($" Driver: {device.DriverVersion}");
55+
Console.WriteLine($" Memory: {device.GlobalMemorySize} bytes");
56+
}
57+
}
58+
```
59+
60+
The returned objects contain diagnostic information. They do not expose or own native OpenCL platform, device, context, or queue handles, and they cannot be used to select a device.
61+
62+
## Keep a pipeline in UMat
63+
64+
OpenCL is most useful when several supported operations can run while the data remains in `UMat` storage:
65+
66+
```csharp
67+
using var source = new UMat(1080, 1920, MatType.CV_8UC3);
68+
using var blurred = new UMat();
69+
using var hsv = new UMat();
70+
71+
Cv2.GaussianBlur(source, blurred, new Size(5, 5), 0);
72+
Cv2.CvtColor(blurred, hsv, ColorConversionCodes.BGR2HSV);
73+
```
74+
75+
Converting between `Mat` and `UMat`, accessing pixels on the CPU, or calling `UMat.GetMat()` can introduce synchronization and host-device transfers. Frequent transitions can cost more than the accelerated operations save.
76+
77+
Use `GetMat(AccessFlag.READ)` when the CPU genuinely needs the result, not merely to make an asynchronous benchmark wait.
78+
79+
## Benchmark queued work correctly
80+
81+
OpenCL commands may be queued asynchronously. Measuring only the calls that enqueue work can produce unrealistically short results. Warm up the operation to exclude one-time kernel compilation, then call `Finish()` at the measurement boundaries:
82+
83+
```csharp
84+
static TimeSpan MeasureOpenCL(Action operation, int iterations)
85+
{
86+
operation();
87+
Cv2.Ocl.Finish(); // Complete warm-up and kernel compilation.
88+
89+
var stopwatch = Stopwatch.StartNew();
90+
for (var i = 0; i < iterations; i++)
91+
{
92+
operation();
93+
}
94+
95+
Cv2.Ocl.Finish(); // Include completion of all queued operations.
96+
stopwatch.Stop();
97+
return stopwatch.Elapsed;
98+
}
99+
```
100+
101+
Run the operation and `Finish()` on the same thread because OpenCL enablement and the default execution context are thread-specific.
102+
103+
Do not call `Finish()` after every operation in a normal pipeline. It blocks the calling thread and prevents OpenCV from overlapping or batching queued work. It is primarily useful for benchmarks and genuine synchronization boundaries.
104+
105+
Compare equivalent `Mat` and `UMat` pipelines with the same source data, output consumption, dimensions, types, warm-up, and build configuration. Use Release builds, and report both latency and throughput.
106+
107+
## Interpret OpenCL build information
108+
109+
`Cv2.GetBuildInformation()` reports whether OpenCV was built with OpenCL support. An include path containing a version such as `opencl/1.2` identifies the OpenCL headers used to compile OpenCV; it does not cap the version reported by the installed runtime or device.
110+
111+
OpenCV loads the OpenCL runtime at execution time. A device can therefore report OpenCL 3.0 through `GetPlatformsInfo()` even when the build information mentions 1.2 headers. Rebuilding OpenCvSharp with newer headers is not, by itself, expected to make an image-processing operation faster.
112+
113+
## Diagnose unexpected performance
114+
115+
When reporting an OpenCL performance problem, include:
116+
117+
- OpenCvSharp managed and runtime package versions.
118+
- Operating system and architecture.
119+
- Release or Debug build configuration.
120+
- `HaveOpenCL()` and `UseOpenCL()` results.
121+
- Platform, device, OpenCL, and driver values from `GetPlatformsInfo()`.
122+
- The OpenCL section of `Cv2.GetBuildInformation()`.
123+
- Equivalent, warmed-up `Mat` and synchronized `UMat` measurements.
124+
- Image dimensions, `MatType`, parameters, and iteration count.
125+
126+
OpenCvSharp forwards operations such as `Cv2.GaussianBlur` to OpenCV. Device-specific OpenCL kernels, performance guards, and CPU fallbacks are implemented by upstream OpenCV. Once synchronization and transfer costs have been accounted for, an operation-specific regression will usually need to be reproduced and investigated upstream.
127+
128+
## Related guides
129+
130+
- [Copies, Native Memory, and Performance](memory-copy-and-performance.md)
131+
- [InputArray, OutputArray, and In-place Processing](input-output-arrays-and-in-place.md)
132+
- [Resource Management](resource-management.md)
133+
134+
## Official OpenCV reference
135+
136+
- [OpenCV configuration options: OpenCL support](https://docs.opencv.org/5.0/tutorials/introduction/config_reference/config_reference.html#opencl-support)
137+
138+
## Related OpenCvSharp API
139+
140+
- [UMat](xref:OpenCvSharp.UMat)
141+
- [Cv2.Ocl](xref:OpenCvSharp.Cv2.Ocl)
142+
- [OclPlatformInfo](xref:OpenCvSharp.OclPlatformInfo)
143+
- [OclDeviceInfo](xref:OpenCvSharp.OclDeviceInfo)

docs/docfx/articles/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Already familiar with OpenCV C++ or Python (`cv2`)? Start with the [API comparis
2222
- [Manage native resources](guides/resource-management.md)
2323
- [Use InputArray, OutputArray, and in-place processing safely](guides/input-output-arrays-and-in-place.md)
2424
- [Control copies, native memory, and hot-loop allocations](guides/memory-copy-and-performance.md)
25+
- [Use and benchmark OpenCL acceleration with UMat](guides/opencl-and-umat.md)
2526
- [Access and modify pixels](guides/pixel-access.md)
2627
- [Encode images and convert UI image types](guides/image-conversion.md)
2728
- [Store matrices and parameters in YAML, XML, or JSON](guides/file-storage.md)

docs/docfx/articles/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
href: guides/input-output-arrays-and-in-place.md
2222
- name: Copies, Native Memory, and Performance
2323
href: guides/memory-copy-and-performance.md
24+
- name: OpenCL Acceleration with UMat
25+
href: guides/opencl-and-umat.md
2426
- name: Pixel Access
2527
href: guides/pixel-access.md
2628
- name: Image Encoding and Conversion

docs/docfx/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ OpenCvSharp is a cross-platform .NET wrapper for OpenCV. This site contains the
1515
- [Manage native resources](articles/guides/resource-management.md)
1616
- [Understand InputArray, OutputArray, and in-place processing](articles/guides/input-output-arrays-and-in-place.md)
1717
- [Control copies, native memory, and performance](articles/guides/memory-copy-and-performance.md)
18+
- [Use and benchmark OpenCL acceleration with UMat](articles/guides/opencl-and-umat.md)
1819
- [Diagnose common errors](articles/troubleshooting/common-errors.md)
1920
- [Troubleshoot native library loading](articles/troubleshooting/native-library-loading.md)
2021
- [Migrate from OpenCvSharp4 to OpenCvSharp5](https://github.com/shimat/opencvsharp/blob/main/docs/migration-4-to-5.md)

0 commit comments

Comments
 (0)