ukoethe / vigra

a generic C++ library for image analysis
http://ukoethe.github.io/vigra/
Other
407 stars 191 forks source link

SIMD support #483

Open kfjahnke opened 3 years ago

kfjahnke commented 3 years ago

Dear group!

Working on my b-spline library vspline, I have used SIMD data types together with vigra. I have investigated several approches to SIMD programming by using/defining several SIMD data types, following the pattern set by M. Kretz' SIMD library Vc, and described in his thesis, using both Vc's own SIMD data types, data types introduced by it's successor std::simd, and a fallback implementation which relies on loop vectorization with the compiler's optimizer, as coded here. I found that I could introduce these SIMD data types into the vigra type system by defining the appropriate traits, which enabled me to do stuff like vectorized vigra::quaternions.

My library depends heavily on SIMD arithmetic for maximum speed, and I found that using the techniques laid out by M. Kretz - and his data types and collateral code in Vc - produce a noticeable performance boost. SIMD programming is - sadly - slow to take off, even though modern processors now contain powerful SIMD units. The majority of SIMD code is probably machine code produced by the compiler's optimizer, and such automatic vectorization of scalar code is oftentimes suboptimal, because good SIMD code has to be restructured and uses a few specific features like masks and gather and scatter operations, which are hard for the optimizer to synthesize from scalar code. M. Kretz' approach is to use specific SIMD data types (which map to SIMD registers in the CPU) and to use intrinsics internally, but to hide the complexity in an easy-to-use portable type. So to help SIMD programming to become more common, my proposition is to add support for such SIMD data types to vigra. My traits code would be a step towards such an integration, but I feel it's a bit rough and ready, so I'd welcome cooperation to make it fit to be contributed to vigra, if that seems like a good idea at all - I'd like to see the matter discussed; it may turn out that SIMD support is best left outside the library.

Kay