parallel mathematica

the mathematica website has documentation for parallelization, but I could not find good examples for trivially parallelizable mathematica loops

Suppose I have a code that is "trivially parallelizable". That is, each loop is independent of the other loops. As an example,

In matlab syntax,

for indx=1:8

a(indx) = indx*indx +3;

end

while in mathematica syntax,

For[indx=1,indx<=8, a[[indx]]=indx indx +3; indx++]

To parallelize this in mathematica, create a function that is the body of the loop which is only dependent on the index. In this case,

fncn[n_]:= n n +3

Using this function in a serial for loop would look like

For[indx=1,indx<=8, a[[indx]]=indx indx +3; indx++]

To parallelize, I assume I have as many kernals (processes) as loop steps. In this case, I have 8 CPUs available

CloseKernels[];

LaunchKernels[8];

a = Table[0, {i, $KernelCount}];

DistributeDefinitions[fncn] (* define the function on each kernel *)

a = ParallelMap[fncn, Range[1,8]]

First we clean up any existing kernals, then launch the desired number of local kernels. Create an array to hold the results. The "parallel map" function simply sprays the "fncn" across all kernels, passing one index in "Range[]" to each kernel.

To demonstrate that the wall clock time saved by parallelization, time the process. I added some pauses so that each step of the "loop" takes some time.

Serial:

result = AbsoluteTiming[For[j = 1, j <= uprlimt, Pause[1]; k[[j]] = fncn[j]; j++]];

result[[1]]

k = result[[2]];

k // MatrixForm

Parallel:

CloseKernels[];

LaunchKernels[uprlimt];

k = Table[0, {i, $KernelCount}];

DistributeDefinitions[fncn]

result = AbsoluteTiming[ParallelMap[Pause[1]; fncn, Range[1, uprlimt]]];

result[[1]]

k = result[[2]];

k // MatrixForm