Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 824675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:08:02+00:00 2026-05-15T03:08:02+00:00

Here’s sequential code: do i = 1, n do j = i+1, n if

  • 0

Here’s sequential code:

do i = 1, n
   do j = i+1, n
      if ("some_condition(i,j)") then
         result = "here's result"
         return
      end if
   end do
end do

Is there a cleaner way to execute iterations of the outer loop concurrently other than:

  !$OMP PARALLEL private(i,j)
  !$OMP DO 
  do i = 1, n     
     !$OMP FLUSH(found)
     if (found) goto 10
     do j = i+1, n        
        if ("some_condition(i,j)") then
           !$OMP CRITICAL
           !$OMP FLUSH(found)
           if (.not.found) then           
              found = .true.
              result = "here's result"
           end if
           !$OMP FLUSH(found)
           !$OMP END CRITICAL
           goto 10
        end if
     end do
10   continue
  end do
  !$OMP END DO NOWAIT
  !$OMP END PARALLEL

The order of iterations over i-loop may be arbitrary as long as some result is found (it doesn’t matter if it changes from run to run as long as it satisfies "some_condition").

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:08:03+00:00Added an answer on May 15, 2026 at 3:08 am

    It seems $OMP DO doesn’t allow break out of the loop earlier. An alternative might be to implement it by hand.

    Give each thread fixed continuous range of indices to process

    Following Guide into OpenMP: Easy multithreading programming for C++:

      results = "invalid_value"
    
      !$OMP PARALLEL private(i,j,thread_num,num_threads,start,end)
    
      thread_num = OMP_GET_THREAD_NUM()
      num_threads = OMP_GET_NUM_THREADS()
      start = thread_num * n / num_threads + 1
      end = (thread_num + 1) * n / num_threads
    
      outer: do i = start, end
         !$OMP FLUSH(found)             
         if (found) exit outer
         do j = i+1, n
            if ("some_condition") then
               found = .true.
               !$OMP FLUSH(found)
               results(thread_num+1) = "here's result"
               exit outer
            end if
         end do
      end do outer
    
      !$OMP END PARALLEL
    
      ! extract `result` from `results` if any
      do i = 1, size(results)
         if (results(i).ne."invalid_value") result = results(i)
      end do
    

    UPDATE: replaced goto by exit, introduced results array based on @M. S. B.’s answer.

    If solution exists this approach is faster then $OMP DO due to earlier exit.

    Give each thread one iteration at a time to process

    Using task directive (suggested by @High Performance Mark):

      !$OMP PARALLEL
      !$OMP SINGLE
      !$OMP TASK UNTIED
              ! "untied" allows other threads to generate tasks
      do i = 1, n ! i is private
         !$OMP TASK ! implied "flush"
         task:     do j = i+1, n ! i is firstprivate, j is private       
            if (found) exit task
            if ("some_condition(i,j)") then
               !$OMP CRITICAL
               result = "here's result" ! result is shared              
               found = .true.           ! found is shared
               !$OMP END CRITICAL ! implied "flush"
               exit task
            end if
         end do task
         !$OMP END TASK 
      end do 
      !$OMP END TASK
      !$OMP END SINGLE
      !$OMP END PARALLEL
    

    This variant is 2 times faster on my tests than the version with the outer-loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 426k
  • Answers 426k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I strongly suggest using CHESS, a free tool from Microsoft… May 15, 2026 at 12:36 pm
  • Editorial Team
    Editorial Team added an answer Okie, I found that it cannot be (should not be)… May 15, 2026 at 12:35 pm
  • Editorial Team
    Editorial Team added an answer Remove As Range from Dim c As Range and that… May 15, 2026 at 12:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.