1

I'd like to be able to run two threads that run forever (one thread computing some complex calculations, and the other counting the time and producing a timeout). As soon as the timeout thread reaches the required time, I'd like to stop both threads. I have tried doing this a couple of ways: first by running the first thread as a task and exiting the thread and then by using sections (by attempting to run #pragma omp cancel sections). This is what I have so far:

    #pragma omp parallel num_threads(8) shared(break_out)
        {
            while (!break_out){
                #pragma omp sections 
                {
                    #pragma omp section
                        complex_calc();

                    #pragma omp section
                    {
                        //Timekeeping
                        while(true){
                            current_time = std::chrono::high_resolution_clock::now();
                            wall_clock = std::chrono::duration_cast<std::chrono::duration<double>> (current_time - start_time);
                            if (wall_clock.count() > 0.99 * TIME_LIMIT){
                                break_out = true;
                                #pragma omp cancel sections
                                break;
                            }
                        }
                    }
                }
            }
        }
**Point X**

The code is able to break out of the second section when the timeout occurs, but the first section still runs. Is there any way to return to 'Point X' in the program?

StaticCrazee
  • 167
  • 1
  • 11
  • 1
    OpenMP isn't really the right paradigm for this kind of manual thread management. You will just end up doing a whole lot of thread management / communication on your own bypassing OpenMP. See also https://stackoverflow.com/q/43496870/620382 – Zulan Apr 02 '19 at 22:13
  • Thought as much :/ I'll try your suggestions in the link you sent. – StaticCrazee Apr 03 '19 at 13:35

1 Answers1

1

The cancel construct in the second sections construct will correctly terminate the second sections construct. But it will not affect the first one, unless you add a cancellation point construct in the first sections construct as well.

OpenMP always requires a combination of the cancel construct to trigger the cancellation of an OpenMP region and cancellation point constructs in the other threads/tasks to react and also cancel their regions.

Michael Klemm
  • 2,658
  • 1
  • 12
  • 15
  • 1
    The thing is, once you have to manually insert cancellation points, there is little benefit from using a watchdog thread and cancellation over just checking the time in the compute thread. – Zulan Apr 03 '19 at 07:19
  • Yes, @Zulan is right. The reason I've got a second section is to asyncrously count while the other section runs. If I check for the timeout in the other thread, it would not be accurate: the function may run just BEFORE the timeout but finish AFTER the timeout since the check was done before the function. – StaticCrazee Apr 03 '19 at 13:38