Skip to content

core: Remove raw intrinsics from arithmetic operations - #12064

Merged
opencv-pushbot merged 1 commit into
opencv:3.4from
seiko2plus:coreUnvintrinArithm2
Nov 8, 2018
Merged

opencv-pushbot merged 1 commit into
opencv:3.4from
seiko2plus:coreUnvintrinArithm2

Conversation

@seiko2plus

@seiko2plus seiko2plus commented Jul 25, 2018 •

Copy link
Copy Markdown
Contributor
force_builders=Custom,armv7,armv8,linux,Linux AVX2
docker_image:Custom=powerpc64le
pw_compilers=gcc-4.9, gcc-5, gcc-6
disable_ipp=1

@vpisarev

Copy link
Copy Markdown
Contributor

@seiko2plus, big thanks for the patch! May I ask you to make it a bit less cryptic? :) Frankly speaking, the proposed implementation is almost impossible to read, therefore its long term support is very difficult.

@seiko2plus

Copy link
Copy Markdown
Contributor Author

@vpisarev, My apologies :), I'm going to rebase this patch into multiple commits and also improve readability.

@vpisarev

vpisarev commented Jul 26, 2018 •

Copy link
Copy Markdown
Contributor

@seiko2plus, thank you! To be more specific, I would like to ask you to avoid macros as much as possible. I know, we use them sometimes, e.g. for universal intrinsic implementation (which is difficult to avoid, because we map many different instructions to overloaded variants of the same inline function), but overall in the code we should try to use templates mostly for a generic code. For example, see the recently modified split.cpp and merge.cpp where I replaced platform specific intrinsics with wide universal intrinsics

/** @brief Saturating absolute difference

Returns \f$ saturate(|a - b|) \f$ .
For 8-, 16-bit signed integer source types. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you please add tests for new common functions (test_intrin.cpp)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done but I had to remove absdiff[s](signed int) and reuse absdiff(signed int) by recasting it to handle overflow which gives the same result as removed code for all archs.

//=======================================

#if (ARITHM_USE_IPP == 1)
static inline void fixSteps(int width, int height, size_t elemSize, size_t& step1, size_t& step2, size_t& step)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This massive code migration to another file "arithm.dispatch.cpp" is not required.
"dispatch.cpp" file is not required to enable "dispatched" optimizations.

Please move this part of code back (with added changes).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done, thanks

@tompollok

Copy link
Copy Markdown
Contributor

Im not very familiar with raw intrinsics/universal intrinsics, but does the switch improve the overall performance or whats the benefits?

@seiko2plus
seiko2plus force-pushed the coreUnvintrinArithm2 branch 8 times, most recently from 79d6152 to aec52ec Compare July 29, 2018 12:31
@seiko2plus

seiko2plus commented Jul 29, 2018 •

Copy link
Copy Markdown
Contributor Author

@vpisarev, please help me out, I don't know any other way to avoid using the current macros. I know macros are considered by many as pure evil but I think in my case in here It's a necessary evil or maybe It just possess me :).

Please take the following into consideration :

  • Using macros SIMD_DEF_* allow us to easily forward, define and dispatch the optimization with multiple types in one step and that allow us to massively reduce the amount of written code.

  • Generate predefined template functions allow us to easily define template functions depend on variant needs, especially and I still have to convert the following operations for all types to wide/unv at my next patches:
    { Comparisons, Multiplication, Division, Reciprocial, Add-weighted, In-range}.

Thanks !

@seiko2plus

Copy link
Copy Markdown
Contributor Author

@tompollok, Sorry for the delayed response,

Im not very familiar with raw intrinsics/universal intrinsics, but does the switch improve the overall performance or whats the benefits?

I'm not sure yet I still have to run performance tests over several archs but definitely this patch going to increase performance for Power8/VSX since VSX intrinsics wasn't implemented before only X86/IPP/SSE/AVX, ARM/NEON.
There are also additional benefits like supporting CPU dispatching and replace raw intrinsics [SSE/AVX, NEON] with universal intrinsics which make SIMD code hopefully more robust and clean.

@vpisarev

vpisarev commented Aug 1, 2018 •

Copy link
Copy Markdown
Contributor

@seiko2plus, I've just noticed your question; let me think a bit. Using macros to define the set of very similar dispatcher functions is fine, I think. The main problem is arithm_helper.hpp, where I cannot just follow the logic. I'm sure, in a few months from now you will have hard time following it too.

Since we now have universal intrinsics that use overloaded functions and operators, I suggest to start with defining template classes/functions that just do the whole thing in one loop without using any macros, something like:

template<typename T, typename VecT> vecadd(const T* src1, const T* src2, T* dst, int n)
{
      int i = 0;
#if CV_SIMD
      const int VECSZ = VecT::nlanes; 
      for( ; i <= n - VECSZ; i += VECSZ ) {
            VecT a = vx_load(src1 + i), b = vx_load(src2 + i);
            v_store(dst + i, a + b);
      }
#endif
      for( ; i < n; i++ ) dst[i] = saturate_cast<T>(src1[i] + src2[i]);
}

(well, perhaps, those kernels should be made 2-dimensional to avoid the overhead on calling the 1D functions repeatedly for each single row).
yes, it means some code duplication, but it looks clean and is easy to maintain.
In the end, we do not have that many arithmetical functions do define:

add, subtract, multiply, divide, absdiff, min, max, and, or, xor, not

Also, a very big advantage of such a simple approach is that if you need some custom implementation of a kernel for a particular datatype, you can always create a template specialization.

Then you can define higher-level dispatcher functions using macros.

@seiko2plus
seiko2plus force-pushed the coreUnvintrinArithm2 branch 12 times, most recently from 7f36e88 to 0e3dddf Compare August 6, 2018 18:44
@seiko2plus

Copy link
Copy Markdown
Contributor Author

@vpisarev, I'm sorry for delayed response :), I'm almost done from this patch currently I'm going to final step which is analyses compiled objects and running performance tests.

Maybe I didn't literally follow your suggestions, I'm not sure if I did the right thing but I just tried to keep it simple without duplicating the same code as much as possible and without using macros only at "higher-level dispatcher functions " just like you said.

Please could you give me your final review? I have free time and I'm ready to accept any radical changes even I have to rewrite this patch from scratch!.
Thank you!

@seiko2plus

Copy link
Copy Markdown
Contributor Author

@vpisarev, please take into consideration that #12516 is required to merge first in order to successfully build this patch, also I combined the two patches at this repo /seiko2plus/opencv/coreUnvintrinArithm2_test for test purposes.

@seiko2plus
seiko2plus force-pushed the coreUnvintrinArithm2 branch 8 times, most recently from 6170c3e to 12fb937 Compare October 30, 2018 09:53
…to wide universal intrinsics

  - initialize arithmetic dispatcher
  - add new universal intrinsic v_absdiffs
  - add new universal intrinsic v_pack_b
  - add accumulate version of universal intrinsic v_round
  - fix sse/avx2:uint8 multiplication overflow
  - reimplement arithmetic, logic and comparison operations into wide universal intrinsics
    with full support for all types
  - reimplement IPP arithmetic, logic and comparison operations in a sperate file arithm_ipp.hpp
  - avoid scalar multiplication if scaling factor eq 1 and use integer multiplication
  - move C arithmetic operations to precomp.hpp and delete [arithm_simd|arithm_core].hpp
  - add compatibility with new opencv4 divide policy
@seiko2plus
seiko2plus force-pushed the coreUnvintrinArithm2 branch from 12fb937 to 93ffebc Compare October 30, 2018 11:19
@opencv-pushbot
opencv-pushbot merged commit 93ffebc into opencv:3.4 Nov 8, 2018
@alalek alalek mentioned this pull request Nov 10, 2018
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants