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. What is the best way to break (return) from OpenMP loop?

What is the best way to break (return) from OpenMP loop?

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    Is there a common way to handle such situation?

    SomeObject* objects;
    omp parallel for
    for (i = 0; i < N; i++) {
    int res = RunFunction(objects[i]);
    if (res)
    return res; //return from open mp loop is forbidden
    }

    Once RunFunction failed stop processing and notify application

    Чесноков

    M C L 3 Replies Last reply
    0
    • C Chesnokov Yuriy

      Is there a common way to handle such situation?

      SomeObject* objects;
      omp parallel for
      for (i = 0; i < N; i++) {
      int res = RunFunction(objects[i]);
      if (res)
      return res; //return from open mp loop is forbidden
      }

      Once RunFunction failed stop processing and notify application

      Чесноков

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      I'm no expert on openMP stuff... can't you just break ? or simply change the loop value so that next iteration will not continue ?

      int res;
      SomeObject* objects;
      omp parallel for
      for (i = 0; i < N; i++) {
      res = RunFunction(objects[i]);
      if (res)
      i = N;
      }

      return res;

      Max.

      Watched code never compiles.

      C 1 Reply Last reply
      0
      • M Maximilien

        I'm no expert on openMP stuff... can't you just break ? or simply change the loop value so that next iteration will not continue ?

        int res;
        SomeObject* objects;
        omp parallel for
        for (i = 0; i < N; i++) {
        res = RunFunction(objects[i]);
        if (res)
        i = N;
        }

        return res;

        Max.

        Watched code never compiles.

        C Offline
        C Offline
        Chesnokov Yuriy
        wrote on last edited by
        #3

        break and return are no allowed. I'm looking for the common practices and patterns in that scenario to not to envent something uncommon.

        Чесноков

        1 Reply Last reply
        0
        • C Chesnokov Yuriy

          Is there a common way to handle such situation?

          SomeObject* objects;
          omp parallel for
          for (i = 0; i < N; i++) {
          int res = RunFunction(objects[i]);
          if (res)
          return res; //return from open mp loop is forbidden
          }

          Once RunFunction failed stop processing and notify application

          Чесноков

          C Offline
          C Offline
          cp9876
          wrote on last edited by
          #4

          I'm no OpenMP expert, but isn't this an example of an algorithm that can't be paralleled as it is written. Imagine two threads, so it runs (0,1), then (2,3), but what if the return value of RunFunction(object[2]) says that the loop should stop, we have probably already executed RunFunction(object[3]). It is not easy to come up with a way that stops a parallel loop with the same result as stopping a sequential loop.

          Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

          C 1 Reply Last reply
          0
          • C Chesnokov Yuriy

            Is there a common way to handle such situation?

            SomeObject* objects;
            omp parallel for
            for (i = 0; i < N; i++) {
            int res = RunFunction(objects[i]);
            if (res)
            return res; //return from open mp loop is forbidden
            }

            Once RunFunction failed stop processing and notify application

            Чесноков

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            the return value of your code corresponds to the logical OR of all those RunFunction invocations. If RunFunction() has no side-effects, you could use a global flag, and even abort all threads once a true value is encountered. If there are side-effects, your code is strictly sequential and cannot be parallellized; at best you could split RunFunction() in a first half without side-effects (that part can be run in parallel), and a second half that produces the side-effects (that part needs to be executed in strict sequence). :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            1 Reply Last reply
            0
            • C cp9876

              I'm no OpenMP expert, but isn't this an example of an algorithm that can't be paralleled as it is written. Imagine two threads, so it runs (0,1), then (2,3), but what if the return value of RunFunction(object[2]) says that the loop should stop, we have probably already executed RunFunction(object[3]). It is not easy to come up with a way that stops a parallel loop with the same result as stopping a sequential loop.

              Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

              C Offline
              C Offline
              Chesnokov Yuriy
              wrote on last edited by
              #6

              it needs to prevent further processing in case of the single error even the next some other parallel returned correctly

              Чесноков

              C 1 Reply Last reply
              0
              • C Chesnokov Yuriy

                it needs to prevent further processing in case of the single error even the next some other parallel returned correctly

                Чесноков

                C Offline
                C Offline
                cp9876
                wrote on last edited by
                #7

                Chesnokov Yuriy wrote:

                it needs to prevent further processing in case of the single error even the next some other parallel returned correctly

                That's why you want to do it, now you have to find a way to tell the compiler how to do it in one of the standard ways that it knows how to parallelise. Currently you are telling it that if RunFunction(object[n]) is false then it must not execute RunFunction(object[n+1]), and I don't think any compiler will know how to parallelise that.

                Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                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