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 333789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:59:38+00:00 2026-05-12T09:59:38+00:00

in the following snippet, if you replace Do by ParallelDo, it will evaluate by

  • 0

in the following snippet, if you replace Do by ParallelDo, it will evaluate by a factor of 3 SLOWER, because now the loop will be broken in only ONE of the two kernels.

ParallelEvaluate[NN = 10070];
SetSharedVariable[res]
Module[{a, b, c},
  Do[
   c = NN - a - b;  
   If[a a + b b == c c, res = a b c; Break[]]
   , {a, 1, NN}, {b, a + 1, NN}
   ];
  res
  ] // AbsoluteTiming

Calling ParallelAbort would solve the issue, but it’s forbidden. What else is there?

  • 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-12T09:59:39+00:00Added an answer on May 12, 2026 at 9:59 am

    You need to have a way for each iteration to tell
    all other iterations that the answer has been found.
    I modelled this with a “quit” flag, intially set
    to false, that is set to true when any iteration
    decides to finish. Each iteration likewise has
    to check the exit condition.

    My Mathematica is 15 years rusty, and I haven’t
    seen the Parallelxxx forms before, but a good guess
    at how the loop should change is the following
    variation on your code:

    ParallelEvaluate[NN = 10070];
    SetSharedVariable[res,quit]
    Module[{a, b, c},
        quit=false;
       Do[ c = NN - a - b;  
           If[quit, Break[]];
           If[ a a + b b == c c, quit=true; res = a b c; Break[]],
           {a, 1, NN}, {b, a + 1, NN}
         ];
         res
       ] // AbsoluteTiming
    

    The extra If slows down the loop somewhat, but thats the price of
    synchronization.

    I suspect that the amount
    of work you are doing in each iteration is already pretty small
    compared to the cost of executing each iteration in parallel,
    so this loop is probably inefficient and you may not get
    any real value from the Do Parallel.
    If you dont, then you can make each Do iteration operate on several values
    of a and b (e.g., use {a, 1, NN, 10} and similarly for b for each
    iteration and handle the 10-wide subrange as a subloop inside
    each parallel iteration).to keep the quit-test exit overhead small in comparison
    to the work done in each loop body.
    Recode exercise left for the reader.

    Your code has another problem: there’s a race condition in setting
    res. Under ceratin circumstances, two iterations could both decide to set res.
    If you don’t care which answer is produced, and the store to res is “atomic”,
    this is fine. If res were a more complicated data structure, and updating
    it took multiple Mathematica statements, you’d surely have a data race
    and your loop would produce bad results once in a great while and it
    would be very hard to debug. You ideally need some kind of atomic
    test to protect the exit condition. I don’t know what that is in MMa,
    so you’ll have to look it up, but I imagine an “atomic[…]” form
    that insists its body is executged by only one of the many parallel threads.
    (Perhaps MMa has a semaphore that you can use to implement atomic].
    If so, your code should then look like this:

    ParallelEvaluate[NN = 10070];
    SetSharedVariable[res,quit]
    Module[{a, b, c},
        quit=false;
       Do[ c = NN - a - b;  
           If[quit, Break[]];
           If[ a a + b b == c c, 
               atomic[If[not[quit]; quit=true; res = a b c;]; Break[]],
           {a, 1, NN}, {b, a + 1, NN}
         ];
         res
       ] // AbsoluteTiming
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • 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 Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

I have the following snippet in one of my html pages : <div class=inputboximage>
The following code snippet illustrates a memory leak when opening XPS files. If you
I am trying to run a simple SQLITE application on Windows Mobile developed with
I was messing around with LinqToSQL and LINQPad and I noticed that SingleOrDefault() doesn't
I'm lead dev for Bitfighter , and we're using Lua as a scripting language

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.