Why is ParallelDo slower than Do? Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Internal`Bag inside CompileWhy won't Parallelize speed up my code?ParallelTable 70 times slower on 16 cores than Table on single coreWhy is ParallelMap way slower than MapParallelTable slower than Table in the presence of an Association?How to make the ProgressIndicator for ParallelDo more efficientParallel calculation is 5 times slower than the non-parallel oneParallelTable much slower than Table on RandomReal with arbitrary precisionParallelDo with doubly-indexed iteratorsClearAttributes in ParallelDoWriting to file with ParallelDoParallelDo gives different solution to Eigensystem

ListPlot join points by nearest neighbor rather than order

How to tell that you are a giant?

Single word antonym of "flightless"

How to deal with a team lead who never gives me credit?

How to answer "Have you ever been terminated?"

String `!23` is replaced with `docker` in command line

How come Sam didn't become Lord of Horn Hill?

Error "illegal generic type for instanceof" when using local classes

Okay to merge included columns on otherwise identical indexes?

How much time will it take to get my passport back if I am applying for multiple Schengen visa countries?

Sci-Fi book where patients in a coma ward all live in a subconscious world linked together

Apollo command module space walk?

What is the logic behind the Maharil's explanation of why we don't say שעשה ניסים on Pesach?

Storing hydrofluoric acid before the invention of plastics

Is it fair for a professor to grade us on the possession of past papers?

What's the meaning of 間時肆拾貳 at a car parking sign

Why did the rest of the Eastern Bloc not invade Yugoslavia?

How can I make names more distinctive without making them longer?

51k Euros annually for a family of 4 in Berlin: Is it enough?

Is the Standard Deduction better than Itemized when both are the same amount?

What does the "x" in "x86" represent?

Why didn't this character "real die" when they blew their stack out in Altered Carbon?

Understanding Ceva's Theorem

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?



Why is ParallelDo slower than Do?



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Internal`Bag inside CompileWhy won't Parallelize speed up my code?ParallelTable 70 times slower on 16 cores than Table on single coreWhy is ParallelMap way slower than MapParallelTable slower than Table in the presence of an Association?How to make the ProgressIndicator for ParallelDo more efficientParallel calculation is 5 times slower than the non-parallel oneParallelTable much slower than Table on RandomReal with arbitrary precisionParallelDo with doubly-indexed iteratorsClearAttributes in ParallelDoWriting to file with ParallelDoParallelDo gives different solution to Eigensystem










5












$begingroup$


I have problems to write parallel code in mathematica.
Why is



candidates = ;
SetSharedVariable[candidates];

Do[
ParallelDo[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


slower than the non parallel version



candidates = ;
Do[
Do[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


?










share|improve this question









New contributor




Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    I reverted your post to before the edit because it looks like a different question (like Henrik said in his comment). Note, however, that if you ask it in precisely such form it will be likely closed due to not enough info: you need to provide the minimal working example, not through some undefined functions into a piece of code that no one will be able to run and test.
    $endgroup$
    – corey979
    Apr 11 at 13:54






  • 1




    $begingroup$
    See here mathematica.stackexchange.com/a/48296/12 I suggest you don't use SetSharedVariable until you get quite fluent in using the parallel tools. It effectively "unparallelizes" your code.
    $endgroup$
    – Szabolcs
    Apr 11 at 14:09
















5












$begingroup$


I have problems to write parallel code in mathematica.
Why is



candidates = ;
SetSharedVariable[candidates];

Do[
ParallelDo[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


slower than the non parallel version



candidates = ;
Do[
Do[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


?










share|improve this question









New contributor




Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    I reverted your post to before the edit because it looks like a different question (like Henrik said in his comment). Note, however, that if you ask it in precisely such form it will be likely closed due to not enough info: you need to provide the minimal working example, not through some undefined functions into a piece of code that no one will be able to run and test.
    $endgroup$
    – corey979
    Apr 11 at 13:54






  • 1




    $begingroup$
    See here mathematica.stackexchange.com/a/48296/12 I suggest you don't use SetSharedVariable until you get quite fluent in using the parallel tools. It effectively "unparallelizes" your code.
    $endgroup$
    – Szabolcs
    Apr 11 at 14:09














5












5








5


3



$begingroup$


I have problems to write parallel code in mathematica.
Why is



candidates = ;
SetSharedVariable[candidates];

Do[
ParallelDo[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


slower than the non parallel version



candidates = ;
Do[
Do[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


?










share|improve this question









New contributor




Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I have problems to write parallel code in mathematica.
Why is



candidates = ;
SetSharedVariable[candidates];

Do[
ParallelDo[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


slower than the non parallel version



candidates = ;
Do[
Do[

eq = RandomReal[] + RandomReal[];
AppendTo[candidates, eq]

, j, 1, 1000]
, i, 1, 10]


?







parallelization






share|improve this question









New contributor




Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Apr 11 at 13:52









corey979

20.9k64382




20.9k64382






New contributor




Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 11 at 12:25









Matthias HellerMatthias Heller

283




283




New contributor




Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Matthias Heller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • $begingroup$
    I reverted your post to before the edit because it looks like a different question (like Henrik said in his comment). Note, however, that if you ask it in precisely such form it will be likely closed due to not enough info: you need to provide the minimal working example, not through some undefined functions into a piece of code that no one will be able to run and test.
    $endgroup$
    – corey979
    Apr 11 at 13:54






  • 1




    $begingroup$
    See here mathematica.stackexchange.com/a/48296/12 I suggest you don't use SetSharedVariable until you get quite fluent in using the parallel tools. It effectively "unparallelizes" your code.
    $endgroup$
    – Szabolcs
    Apr 11 at 14:09

















  • $begingroup$
    I reverted your post to before the edit because it looks like a different question (like Henrik said in his comment). Note, however, that if you ask it in precisely such form it will be likely closed due to not enough info: you need to provide the minimal working example, not through some undefined functions into a piece of code that no one will be able to run and test.
    $endgroup$
    – corey979
    Apr 11 at 13:54






  • 1




    $begingroup$
    See here mathematica.stackexchange.com/a/48296/12 I suggest you don't use SetSharedVariable until you get quite fluent in using the parallel tools. It effectively "unparallelizes" your code.
    $endgroup$
    – Szabolcs
    Apr 11 at 14:09
















$begingroup$
I reverted your post to before the edit because it looks like a different question (like Henrik said in his comment). Note, however, that if you ask it in precisely such form it will be likely closed due to not enough info: you need to provide the minimal working example, not through some undefined functions into a piece of code that no one will be able to run and test.
$endgroup$
– corey979
Apr 11 at 13:54




$begingroup$
I reverted your post to before the edit because it looks like a different question (like Henrik said in his comment). Note, however, that if you ask it in precisely such form it will be likely closed due to not enough info: you need to provide the minimal working example, not through some undefined functions into a piece of code that no one will be able to run and test.
$endgroup$
– corey979
Apr 11 at 13:54




1




1




$begingroup$
See here mathematica.stackexchange.com/a/48296/12 I suggest you don't use SetSharedVariable until you get quite fluent in using the parallel tools. It effectively "unparallelizes" your code.
$endgroup$
– Szabolcs
Apr 11 at 14:09





$begingroup$
See here mathematica.stackexchange.com/a/48296/12 I suggest you don't use SetSharedVariable until you get quite fluent in using the parallel tools. It effectively "unparallelizes" your code.
$endgroup$
– Szabolcs
Apr 11 at 14:09











1 Answer
1






active

oldest

votes


















5












$begingroup$

Because managing write access to shared memory is expensive: Subprocesses have to wait until they are granted write access (because another process uses that ressource).



Moreover, it is in general more efficient to use Parallel only upon the most outer loop construct.



By the way: Using Append and AppendTo are the worst methods to build a list, because they involve a copy of the full list each time another element is appended. Instead of complexity $O(n)$ for a list of $n$ elements, you get an implementation of complexity $O(n^2)$. Better use Table or, if you don't know how long the list is about to get, use Sow and Reap. Internal`Bag is a further option, and it is even compilable.






share|improve this answer











$endgroup$












  • $begingroup$
    Thanks, that actually helped a lot. I just dont understand how to use Sow and Reap to avoid Append To be more specific: instead of ParallelDo I use now ParallelTable: eq = ParallelTable[ FNumeric[ SetPrecision[N[monlistnumeric[[i]] + monlistnumeric[[j]], 20], 10]] , j, jj]; FNumeric is a function, that returns either 0 or a value I want to store. I then do eq = DeleteCases[eq, 0]; candidates = Join[candidates, eq]; Is there a more efficient way to do this?
    $endgroup$
    – Matthias Heller
    Apr 11 at 12:55







  • 2




    $begingroup$
    @MatthiasHeller, you're welcome. How is this new code related to your post? You should consider a new post with your real problem and all relevant data. I may have a look. In general, depending on the details, there are various ways to perform the computation efficiently; these way might not use Parallel at all, but rather Compiled code.
    $endgroup$
    – Henrik Schumacher
    Apr 11 at 13:13











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Matthias Heller is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f195006%2fwhy-is-paralleldo-slower-than-do%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5












$begingroup$

Because managing write access to shared memory is expensive: Subprocesses have to wait until they are granted write access (because another process uses that ressource).



Moreover, it is in general more efficient to use Parallel only upon the most outer loop construct.



By the way: Using Append and AppendTo are the worst methods to build a list, because they involve a copy of the full list each time another element is appended. Instead of complexity $O(n)$ for a list of $n$ elements, you get an implementation of complexity $O(n^2)$. Better use Table or, if you don't know how long the list is about to get, use Sow and Reap. Internal`Bag is a further option, and it is even compilable.






share|improve this answer











$endgroup$












  • $begingroup$
    Thanks, that actually helped a lot. I just dont understand how to use Sow and Reap to avoid Append To be more specific: instead of ParallelDo I use now ParallelTable: eq = ParallelTable[ FNumeric[ SetPrecision[N[monlistnumeric[[i]] + monlistnumeric[[j]], 20], 10]] , j, jj]; FNumeric is a function, that returns either 0 or a value I want to store. I then do eq = DeleteCases[eq, 0]; candidates = Join[candidates, eq]; Is there a more efficient way to do this?
    $endgroup$
    – Matthias Heller
    Apr 11 at 12:55







  • 2




    $begingroup$
    @MatthiasHeller, you're welcome. How is this new code related to your post? You should consider a new post with your real problem and all relevant data. I may have a look. In general, depending on the details, there are various ways to perform the computation efficiently; these way might not use Parallel at all, but rather Compiled code.
    $endgroup$
    – Henrik Schumacher
    Apr 11 at 13:13















5












$begingroup$

Because managing write access to shared memory is expensive: Subprocesses have to wait until they are granted write access (because another process uses that ressource).



Moreover, it is in general more efficient to use Parallel only upon the most outer loop construct.



By the way: Using Append and AppendTo are the worst methods to build a list, because they involve a copy of the full list each time another element is appended. Instead of complexity $O(n)$ for a list of $n$ elements, you get an implementation of complexity $O(n^2)$. Better use Table or, if you don't know how long the list is about to get, use Sow and Reap. Internal`Bag is a further option, and it is even compilable.






share|improve this answer











$endgroup$












  • $begingroup$
    Thanks, that actually helped a lot. I just dont understand how to use Sow and Reap to avoid Append To be more specific: instead of ParallelDo I use now ParallelTable: eq = ParallelTable[ FNumeric[ SetPrecision[N[monlistnumeric[[i]] + monlistnumeric[[j]], 20], 10]] , j, jj]; FNumeric is a function, that returns either 0 or a value I want to store. I then do eq = DeleteCases[eq, 0]; candidates = Join[candidates, eq]; Is there a more efficient way to do this?
    $endgroup$
    – Matthias Heller
    Apr 11 at 12:55







  • 2




    $begingroup$
    @MatthiasHeller, you're welcome. How is this new code related to your post? You should consider a new post with your real problem and all relevant data. I may have a look. In general, depending on the details, there are various ways to perform the computation efficiently; these way might not use Parallel at all, but rather Compiled code.
    $endgroup$
    – Henrik Schumacher
    Apr 11 at 13:13













5












5








5





$begingroup$

Because managing write access to shared memory is expensive: Subprocesses have to wait until they are granted write access (because another process uses that ressource).



Moreover, it is in general more efficient to use Parallel only upon the most outer loop construct.



By the way: Using Append and AppendTo are the worst methods to build a list, because they involve a copy of the full list each time another element is appended. Instead of complexity $O(n)$ for a list of $n$ elements, you get an implementation of complexity $O(n^2)$. Better use Table or, if you don't know how long the list is about to get, use Sow and Reap. Internal`Bag is a further option, and it is even compilable.






share|improve this answer











$endgroup$



Because managing write access to shared memory is expensive: Subprocesses have to wait until they are granted write access (because another process uses that ressource).



Moreover, it is in general more efficient to use Parallel only upon the most outer loop construct.



By the way: Using Append and AppendTo are the worst methods to build a list, because they involve a copy of the full list each time another element is appended. Instead of complexity $O(n)$ for a list of $n$ elements, you get an implementation of complexity $O(n^2)$. Better use Table or, if you don't know how long the list is about to get, use Sow and Reap. Internal`Bag is a further option, and it is even compilable.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 11 at 13:48

























answered Apr 11 at 12:29









Henrik SchumacherHenrik Schumacher

60.3k583169




60.3k583169











  • $begingroup$
    Thanks, that actually helped a lot. I just dont understand how to use Sow and Reap to avoid Append To be more specific: instead of ParallelDo I use now ParallelTable: eq = ParallelTable[ FNumeric[ SetPrecision[N[monlistnumeric[[i]] + monlistnumeric[[j]], 20], 10]] , j, jj]; FNumeric is a function, that returns either 0 or a value I want to store. I then do eq = DeleteCases[eq, 0]; candidates = Join[candidates, eq]; Is there a more efficient way to do this?
    $endgroup$
    – Matthias Heller
    Apr 11 at 12:55







  • 2




    $begingroup$
    @MatthiasHeller, you're welcome. How is this new code related to your post? You should consider a new post with your real problem and all relevant data. I may have a look. In general, depending on the details, there are various ways to perform the computation efficiently; these way might not use Parallel at all, but rather Compiled code.
    $endgroup$
    – Henrik Schumacher
    Apr 11 at 13:13
















  • $begingroup$
    Thanks, that actually helped a lot. I just dont understand how to use Sow and Reap to avoid Append To be more specific: instead of ParallelDo I use now ParallelTable: eq = ParallelTable[ FNumeric[ SetPrecision[N[monlistnumeric[[i]] + monlistnumeric[[j]], 20], 10]] , j, jj]; FNumeric is a function, that returns either 0 or a value I want to store. I then do eq = DeleteCases[eq, 0]; candidates = Join[candidates, eq]; Is there a more efficient way to do this?
    $endgroup$
    – Matthias Heller
    Apr 11 at 12:55







  • 2




    $begingroup$
    @MatthiasHeller, you're welcome. How is this new code related to your post? You should consider a new post with your real problem and all relevant data. I may have a look. In general, depending on the details, there are various ways to perform the computation efficiently; these way might not use Parallel at all, but rather Compiled code.
    $endgroup$
    – Henrik Schumacher
    Apr 11 at 13:13















$begingroup$
Thanks, that actually helped a lot. I just dont understand how to use Sow and Reap to avoid Append To be more specific: instead of ParallelDo I use now ParallelTable: eq = ParallelTable[ FNumeric[ SetPrecision[N[monlistnumeric[[i]] + monlistnumeric[[j]], 20], 10]] , j, jj]; FNumeric is a function, that returns either 0 or a value I want to store. I then do eq = DeleteCases[eq, 0]; candidates = Join[candidates, eq]; Is there a more efficient way to do this?
$endgroup$
– Matthias Heller
Apr 11 at 12:55





$begingroup$
Thanks, that actually helped a lot. I just dont understand how to use Sow and Reap to avoid Append To be more specific: instead of ParallelDo I use now ParallelTable: eq = ParallelTable[ FNumeric[ SetPrecision[N[monlistnumeric[[i]] + monlistnumeric[[j]], 20], 10]] , j, jj]; FNumeric is a function, that returns either 0 or a value I want to store. I then do eq = DeleteCases[eq, 0]; candidates = Join[candidates, eq]; Is there a more efficient way to do this?
$endgroup$
– Matthias Heller
Apr 11 at 12:55





2




2




$begingroup$
@MatthiasHeller, you're welcome. How is this new code related to your post? You should consider a new post with your real problem and all relevant data. I may have a look. In general, depending on the details, there are various ways to perform the computation efficiently; these way might not use Parallel at all, but rather Compiled code.
$endgroup$
– Henrik Schumacher
Apr 11 at 13:13




$begingroup$
@MatthiasHeller, you're welcome. How is this new code related to your post? You should consider a new post with your real problem and all relevant data. I may have a look. In general, depending on the details, there are various ways to perform the computation efficiently; these way might not use Parallel at all, but rather Compiled code.
$endgroup$
– Henrik Schumacher
Apr 11 at 13:13










Matthias Heller is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Matthias Heller is a new contributor. Be nice, and check out our Code of Conduct.












Matthias Heller is a new contributor. Be nice, and check out our Code of Conduct.











Matthias Heller is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f195006%2fwhy-is-paralleldo-slower-than-do%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company