Sorry if the question is very basic, that's because I am newbie in the Mathematica.

Assume I have a module that is supposed to do a very very long calculation

result = Table[0, {i, 100000000}]; f[x0_] := Module[{x = x0},

For[i =0, i < 100000000, i++, result[[i]] = i^2; ]; ]

Now I want to take the advantage of my multicore CPU. I there any way to break the work into pieces and assign every piece to a core to do that in parallel?

Possibly it is not a simple ParallelEvaluate command. Let me be more specific:

Assume this module is taking a very long For loop like For[i=0,i<100000000,.... . My question is if I have 8 cores on my CPU, can I break the 100 million loop job into 8x12.5 million cycles and assign each one of these 12.5 million cycles to one core?

  • $\begingroup$ You are not supplying enough information to get a reasonable answer. $\endgroup$
    – Igor Rivin
    Sep 29, 2014 at 23:31
  • $\begingroup$ Perhaps ParallelDo or ParallelTable, or other parallel commands. But not ParallelEvaluate. Which is more appropriate is hard to say without a clearer formulation of your question. Right now details are missing that leave the question a little vague. $\endgroup$
    – Michael E2
    Sep 29, 2014 at 23:31
  • $\begingroup$ @IgorRivin The main module is huge and I thought it is not appropriate to enter that. It consists of 8 For loops that do around 100 million integrations. It takes 2 days to do each evaluation and I want to reduce this time. $\endgroup$
    – Aug
    Sep 29, 2014 at 23:34
  • $\begingroup$ The question is what the dependencies between these evaluations are... $\endgroup$
    – Igor Rivin
    Sep 29, 2014 at 23:37
  • $\begingroup$ @IgorRivin Actually they are not related. each cycle calculates an integral and stores the result in a matrix for further use. $\endgroup$
    – Aug
    Sep 29, 2014 at 23:39

1 Answer 1


If your computation in each cycle takes little time there is no reason for paralleling this cycle. Prove:

ParallelTable[i, {i, 1, 1000000}]; // AbsoluteTiming (*8 cores*)
(*{0.449026, Null}*)
Table[i, {i, 1, 1000000}]; // AbsoluteTiming
(*{0.011001, Null}*)

This is that becouse tranfering data to parallel kernels take more time than computation on it.

Post your real code please


Replace construction like

For[i = 1, i <= n, i++, 
some code

with constructions like

some code

DistributedContexts->All is not recomended becouse it is distribe all contexts to all parallel kernels.

  • $\begingroup$ Thanks but the real code is so huge ( 8xFor loops, each cycles does an Integration . Total: 100 million). Each cycle takes almost long time. The whole process takes 2 days $\endgroup$
    – Aug
    Sep 30, 2014 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.