Repository navigation
CMake: add support for dispatched CPU optimization - #7462
Conversation
|
@alalek, is it ready for review? could you fix the "orange" builder "Linux x64 Debug"? |
|
@vpisarev Yes, patch is ready to review. You can start from branch with samples and Wiki page. |
|
@alalek, I think this patch is very useful and interesting. On the other hand, I'd like to ask about the position of the "Universal Intrinsic"
Universal intrinsic is kind of a "horizontal" approach, when your patch is a proposal of a "vertical" approach. |
|
@tomoaki0705, I have not analyzed the patch yet, but we dicussed the idea long time ago, and it's great that Alex has finally implemented it. The idea is to enable AVX1/2, SSE4.x (on x86/x64 platforms) and ARMv8 (ARM platforms) in regular OpenCV builds. Before we could only enable those instructions/intrinsics at compile time, and that made OpenCV binaries incompatible with older hardware, which is no-go for many applications. Now we can check at runtime whether AVX etc. is supported and call the respective optimized kernels placed in separate files and compiled with special flags. In theory it should not break universal intrinsics concept. Universal intrinsics is compile-time thing, and therefore they should be compatible with 99% of target hardware. It means SSE2 (maybe SSE3 and SSSE3) in the case of x86/x64, ARMv7 NEON in the case of 32-bit ARM, ARMv8 NEON in the case of 64-bit ARM. If you want absolute compatibility and do not want to mess with platform-specific intrinsics, you can use universal intrinsics and expect decent acceleration. If for some selected kernel, e.g. some deep learning kernel where AVX and fma may give serious performance boost, you know you can do even better by using modern instructions, you can place this optimization in a separate file and possibly invoke this branch at runtime. Now if the question is whether we want to extend universal intrinsics to enable, say, 256-bit or 512-bit vectors (abstracting AVX/AVX2 and AVX512, respectively) or alternative implementations of certain universal intrinsics using modern instructions, so that those platform-specific kernels can still be implemented using universal intrinsics, the answer is - well, not yet. We'd rather advance step by step. Universal intrinsics was a step forward, allowing people to write SIMD code once and run it everywhere. This is another step forward, where we make modern instructions more practical, we show how to use them without worrying about backward compatibility. The next step is somewhere ahead :) |
|
@vpisarev , thank you for your comment. For instance, @alalek explained how to use this patch in https://github.com/alalek/opencv/wiki/CPU-optimizations-build-options |
|
IMHO, Universal intrinsic code (probably under some namespace) can migrate to corresponding header file ("cvfunction_simd.hpp"), which will be included into |
| */ | ||
| CV_INLINE int cvFloor( double value ) | ||
| { | ||
| #if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) |
There was a problem hiding this comment.
why is this part changed? cvFloor() and cvCeil() (together with cvRound) are some of the most performance-critical functions in the library; we want to keep them maximally efficient. Can you please revert the changes?
There was a problem hiding this comment.
- Modern compilers is able to apply more effective construction instead of proposed intrinsic code (
comiss+set<cond>instead ofmovmskps). - In general, intrinsic code may block some compiler optimizations including loop unrolling and auto-vectorizer.
- There is bug in some version of MSVC compiler with AVX code generation mode: it omits
_mm_set_*statement ("value" is already stored as XMM register), so higher elements are not cleared to zero. As a result, "movemask" returns values in range 0-15, that is wrong (0-1 is expected). Related to this code.
There are MSVS2015 times for "floor" processing of 1M float vector on i5-6300U:
Current: (samples = 100, mean = 1.46, median = 1.38, stddev = 0.20 (13.8%))
Proposed: (samples = 100, mean = 0.78, median = 0.79, stddev = 0.12 (15.2%))
BTW, There are similar results for SSE4.2 and AVX2 compilation flags
GCC on i5-6600:
Current: (samples = 10, mean = 5.82, median = 5.75, stddev = 0.15 (2.5%))
Proposed: (samples = 100, mean = 0.38, median = 0.37, stddev = 0.05 (13.8%))
| */ | ||
| CV_INLINE int cvCeil( double value ) | ||
| { | ||
| #if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__&& !defined __APPLE__)) && !defined(__CUDACC__) |
|
@alalek, general question: in order to keep things simple, can we simplify dispatching and file naming scheme a bit, i.e. combine sse4.1 and sse4.2 into sse4, avx and avx2 into avx2. That is, if CPU supports both SSE 4.1 and SSE 4.2, we use code from _sse4.cpp files. If CPU supports both AVX and AVX2, we use code from _avx2.cpp. SSE3 and SSSE3 should probably be assumed as available by default |
And If CPU supports AVX and doesn't support AVX2, we use code from _avx.cpp. Dispatch levels can be configured via CMake, so configuration with SSE3 baseline + AVX2 dispatching mode (which includes SSSE3, SSE4.1, SSE4.2, AVX and AVX2 code - only one is added, other are dropped) is possible. |
|
ok, the tests and the rationale all sound very reasonable! 👍 |
|
From this change, the CPU_DISPATCH value is not correctly set for Windows / VS12 |
Related issue: #6990
Wiki page: https://github.com/alalek/opencv/wiki/CPU-optimizations-build-options
Samples branch: master...alalek:cpu_multi_target_test
Related problems: