Skip to content

Fix slower CV_PAUSE on SkyLake and above.#22966

Merged
asmorkalov merged 1 commit intoopencv:3.4from
vrabaud:mm_pause_fix
Dec 16, 2022
Merged

Fix slower CV_PAUSE on SkyLake and above.#22966
asmorkalov merged 1 commit intoopencv:3.4from
vrabaud:mm_pause_fix

Conversation

@vrabaud
Copy link
Copy Markdown
Contributor

@vrabaud vrabaud commented Dec 15, 2022

This is fixing #22852

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work

@asmorkalov
Copy link
Copy Markdown
Contributor

/home/ci/opencv/modules/core/src/parallel_impl.cpp:419:85: error: '__rdtsc' was not declared in this scope
                     CV_PAUSE(16);
                                                                                     ^
/home/ci/opencv/modules/core/src/parallel_impl.cpp: In member function 'void cv::ThreadPool::run(const cv::Range&, const cv::ParallelLoopBody&, double)':
/home/ci/opencv/modules/core/src/parallel_impl.cpp:701:97: error: '__rdtsc' was not declared in this scope
                                 CV_PAUSE(16);
                                                                                                 ^
make[2]: *** [modules/core/CMakeFiles/opencv_core.dir/src/parallel_impl.cpp.o] Error 1

@asmorkalov asmorkalov self-requested a review December 16, 2022 09:19
@asmorkalov asmorkalov merged commit 6b50410 into opencv:3.4 Dec 16, 2022
@asmorkalov asmorkalov added this to the 3.4.19 milestone Dec 16, 2022
@vrabaud vrabaud deleted the mm_pause_fix branch December 16, 2022 12:52
# define CV_PAUSE(v) do { for (int __delay = (v); __delay > 0; --__delay) { _mm_pause(); } } while (0)
// 5 * v is meants for backward compatibility: with pre-Skylake CPUs, _mm_pause took 4 or 5 cycles.
// With post-Skylake CPUs, _mm_pause takes 140 cycles.
# define CV_PAUSE(v) do { const uint64_t __delay = 5 * v; uint64_t __init = __rdtsc(); do { _mm_pause(); } while ((__rdtsc() - __init) < __delay); } while (0)
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.

RDTSC latency is about 25-35 cycles.

@vrabaud Alternative is ignoring of v parameter and run _mm_pause() once. If it works for you, then we could do that.

BTW, TBB code doesn't suggest anything new (still uses euristic constants): https://github.com/search?q=repo%3Aoneapi-src%2FoneTBB%20machine_pause&type=code

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.

We could indeed use _mm_pause once, that would save some cycles (I thought RDTSC was 18 cycles) BUT that would not be compatible with older CPUs where _mm_pause is only 4/5 cycles.
Do we have a way to check for SkyLake and above CPUs ? I'll gladly make a patch for that.

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.

Skylake is here for 7+ years: https://en.wikipedia.org/wiki/Skylake_(microarchitecture)

There is OPENCV_THREAD_POOL_ACTIVE_WAIT_PAUSE_LIMIT runtime parameter which could control total number of pause operations for fine tuning.

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.

Indeed, so what do you suggest: that I make a new pull request that only does one _mm_pause where defined ?
And for older CPUs, OPENCV_THREAD_POOL_ACTIVE_WAIT_PAUSE_LIMIT should be used ? I am totally fine with that.

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.

Right

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 in #22988

@alalek alalek mentioned this pull request Dec 18, 2022
@alalek alalek mentioned this pull request Jan 8, 2023
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.

3 participants