Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parallel_for_ flagNestedParallelFor cause multi task in difference thread may not parallel #25556

Open
4 tasks done
DaydreamCoding opened this issue May 7, 2024 · 3 comments

Comments

@DaydreamCoding
Copy link

System Information

opencv 4.9

Detailed description

    static std::atomic<bool> flagNestedParallelFor(false);
    if (isNotNestedRegion)
            isNotNestedRegion = !flagNestedParallelFor.exchange(true); // multi threads in this line, may cause isNotNestedRegion to be false
    if (isNotNestedRegion)
    {
        try
        {
            parallel_for_impl(range, body, nstripes); // before done, flagNestedParallelFor always true, and another job could not fall into parallel_for_impl
            flagNestedParallelFor = false;
        }
        catch (...)
        {
            flagNestedParallelFor = false;
            throw;
        }
    }
    else // nested parallel_for_() calls are not parallelized
    {
        CV_UNUSED(nstripes);
        body(range);
    }

When multiple threads are using cv::resize, the cv::resize of some threads cannot be fall into parallel for

Steps to reproduce

auto t1 = std::thread([]() {
    cv::resize(); // this fall into parallel_for_impl
});

auto t2 = std::thread([]() {
    cv::resize(); // can not fall into parallel_for_impl
});

t1.join();
t2.join();

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)
@asmorkalov
Copy link
Contributor

@opencv-alalek could you take a look?

@fengyuentau
Copy link
Member

fengyuentau commented May 8, 2024

It is not a bug. Parallel for is designed to work in this way. Nested parallel for does not lead to better performance. See #25260 (comment).

@DaydreamCoding
Copy link
Author

DaydreamCoding commented May 9, 2024

It is not a bug. Parallel for is designed to work in this way. Nested parallel for does not lead to better performance. See #25260 (comment).

This design is flawed, resulting in external non-nested calls that probabilistically cannot be concurrent. And for multi-threaded calls to a function that uses parral_for, this scheme may be invalidated

auto t1 = std::thread([]() {
    cv::resize(); // this fall into parallel_for_impl
});

auto t2 = std::thread([]() {
    cv::resize(); // can not fall into parallel_for_impl
});

t1.join();
t2.join();

@opencv-alalek

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

No branches or pull requests

3 participants