Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Terminating OpenMP loop early

Terminating OpenMP loop early

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomdata-structuresperformance
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Damir Valiulin
    wrote on last edited by
    #1

    Another poster already asked similar question already (http://www.codeproject.com/Messages/3644795/What-is-the-best-way-to-break-return-from-OpenMP-l.aspx[^]), but I'll ask anyway. I have a function that checks if a point belongs to an object in array. The loop looks like this:

    for (int i=0; i<N; i++)
    {
    if (point_is_in_object(obj[i], pt){
    return true;
    }
    }
    return false;

    I parallelized it with OpenMP by adding shared boolean flag found.

    bool found = false;
    #pragma omp parallel for
    for (int i=0; i<N; i++)
    {
    if (!found)
    {
    if (point_is_in_object(obj[i], pt){
    found = true;
    #pragma omp flush (found)
    }
    }
    }

    return found;

    Is this a good way to do this? Could this cause access violation when one thread is writing and the other thread is reading memory contents of found flag? I'm new to OpenMP and don't know much how it operates under the hood. With standard Win32 I would isolate access to found with critical section. Should I do the same here as well and add critical clause? Thanks

    L 1 Reply Last reply
    0
    • D Damir Valiulin

      Another poster already asked similar question already (http://www.codeproject.com/Messages/3644795/What-is-the-best-way-to-break-return-from-OpenMP-l.aspx[^]), but I'll ask anyway. I have a function that checks if a point belongs to an object in array. The loop looks like this:

      for (int i=0; i<N; i++)
      {
      if (point_is_in_object(obj[i], pt){
      return true;
      }
      }
      return false;

      I parallelized it with OpenMP by adding shared boolean flag found.

      bool found = false;
      #pragma omp parallel for
      for (int i=0; i<N; i++)
      {
      if (!found)
      {
      if (point_is_in_object(obj[i], pt){
      found = true;
      #pragma omp flush (found)
      }
      }
      }

      return found;

      Is this a good way to do this? Could this cause access violation when one thread is writing and the other thread is reading memory contents of found flag? I'm new to OpenMP and don't know much how it operates under the hood. With standard Win32 I would isolate access to found with critical section. Should I do the same here as well and add critical clause? Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Damir Valiulin wrote:

      Could this cause access violation when one thread is writing and the other thread is reading memory contents of found flag?

      You are safe the memory barrier MFENCE[^] instruction will be generated by the flush directive. The only change I would suggest is declaring your bool as volatile. This will hint the compiler to generate instructions differently and not bother to keep your bool in a register. Best Wishes, -David Delaune

      D 1 Reply Last reply
      0
      • L Lost User

        Damir Valiulin wrote:

        Could this cause access violation when one thread is writing and the other thread is reading memory contents of found flag?

        You are safe the memory barrier MFENCE[^] instruction will be generated by the flush directive. The only change I would suggest is declaring your bool as volatile. This will hint the compiler to generate instructions differently and not bother to keep your bool in a register. Best Wishes, -David Delaune

        D Offline
        D Offline
        Damir Valiulin
        wrote on last edited by
        #3

        David, thanks for the quick answer. This was helpful.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups