Skip to content

CMake: add support for dispatched CPU optimization - #7462

Merged
opencv-pushbot merged 1 commit into
opencv:masterfrom
alalek:cpu_multi_target
Mar 21, 2017
Merged

opencv-pushbot merged 1 commit into
opencv:masterfrom
alalek:cpu_multi_target

Conversation

@vpisarev

Copy link
Copy Markdown
Contributor

@alalek, is it ready for review? could you fix the "orange" builder "Linux x64 Debug"?

@alalek

alalek commented Feb 16, 2017

Copy link
Copy Markdown
Member Author

@vpisarev Yes, patch is ready to review. You can start from branch with samples and Wiki page.
Orange debug build should be ignored - is related to build system: CMake test options should be changed. But it can be done right now, because other builds will fail due unknown options introduced by this patch. Merge and buildbot update should be done in the same time.

@tomoaki0705

Copy link
Copy Markdown
Contributor

@alalek, I think this patch is very useful and interesting.
I was feeling that it's a waste of time to check the support of SSE2 every line of the image on modern CPU.
This patch is great and smart.

On the other hand, I'd like to ask about the position of the "Universal Intrinsic"
Your patch seems not to include the modification for universal intrinsic.
May I ask your opinion about this point ?

  1. Universal intrinsic will be deprecated. All optimization should be written in raw intrinsic
  2. Universal intrinsic will be called inside ifdef
  3. Universal intrinsic will be kept, too. Something like CV_CPU_CALL_SIMD128 will be added later
  4. Something else

Universal intrinsic is kind of a "horizontal" approach, when your patch is a proposal of a "vertical" approach.
It just conflict each other, so I'd like to know your opinion, if possible.

@vpisarev

vpisarev commented Feb 23, 2017 •

Copy link
Copy Markdown
Contributor

@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 :)

@tomoaki0705

Copy link
Copy Markdown
Contributor

@vpisarev , thank you for your comment.
My point is NOT to extend the universal intrinsic to 256bit. It's much more practical question.

For instance, @alalek explained how to use this patch in https://github.com/alalek/opencv/wiki/CPU-optimizations-build-options
In there, it's explained that the optimized function should be written in cvfunction_opt_avx.cpp.
Now, where should the universal intrinsic should go ?
Should I write the universal intrinsic version in cvfunction_opt_universalintrinsic.cpp or in other file ?

@alalek

alalek commented Feb 24, 2017

Copy link
Copy Markdown
Member Author

IMHO, Universal intrinsic code (probably under some namespace) can migrate to corresponding header file ("cvfunction_simd.hpp"), which will be included into cvfunction_opt_<>.cpp files. .cpp files will have minimal set of code.
In the future we will try wrap this code into macro and generate these files automatically via CMake (if these files are based on universal intrinsic code).

*/
CV_INLINE int cvFloor( double value )
{
#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Modern compilers is able to apply more effective construction instead of proposed intrinsic code (comiss+set<cond> instead of movmskps).
  2. In general, intrinsic code may block some compiler optimizations including loop unrolling and auto-vectorizer.
  3. 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__)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@vpisarev

Copy link
Copy Markdown
Contributor

@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

@alalek

alalek commented Mar 20, 2017

Copy link
Copy Markdown
Member Author

If CPU supports both AVX and AVX2, we use code from _avx2.cpp.

And If CPU supports AVX and doesn't support AVX2, we use code from _avx.cpp.
What is wrong with current implementation?

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.
if you want dispatched both AVX and AVX2 modes, then both files _avx.cpp and avx2.cpp are added to compilation list.

@vpisarev

Copy link
Copy Markdown
Contributor

ok, the tests and the rationale all sound very reasonable! 👍

@arkeon7

arkeon7 commented Apr 21, 2017

Copy link
Copy Markdown

From this change, the CPU_DISPATCH value is not correctly set for Windows / VS12
This should be a list but the cmake gui only show a combo box with one choice selection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants