# How to use multiple core in evaluation?

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?

• You are not supplying enough information to get a reasonable answer. – Igor Rivin Sep 29 '14 at 23:31
• 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. – Michael E2 Sep 29 '14 at 23:31
• @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. – Aug Sep 29 '14 at 23:34
• The question is what the dependencies between these evaluations are... – Igor Rivin Sep 29 '14 at 23:37
• @IgorRivin Actually they are not related. each cycle calculates an integral and stores the result in a matrix for further use. – Aug Sep 29 '14 at 23:39

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.

EDIT

Replace construction like

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


with constructions like

ParallelDo[
some code
,{i,1,n},DistributedContexts->All]


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

• 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 – Aug Sep 30 '14 at 14:31