How to make commands in Mathematica 8 use all cores?

From the Parallelize doumentation, under Examples > Possible Issues:

Expressions that cannot be parallelized are evaluated normally:

Parallelize[Integrate[1/(x - 1), x]]

enter image description here


As mentioned in the other questions and comments, things like Integrate and Simplify would be really difficult to parallelize, so Mathematica returns the message Parallelize::nopar1 and proceeds "with sequential evaluation."

(Although on reflection, maybe FullSimplify could be parallelized, since it basically works by trying lots of different rules and doing leafcounts on them...)

If you have many integrals or simplifications to do, then you could use ParallelTable or ParallelMap etc...

As a trivial example, if you have the integrands

In[1]:= ints = Table[x^n, {n, 1, 10}]
Out[1]= {x, x^2, x^3, x^4, x^5, x^6, x^7, x^8, x^9, x^10}

You can use ParallelTable

In[2]:= ParallelTable[Integrate[int, x], {int, ints}]
Out[2]= {x^2/2, x^3/3, x^4/4, x^5/5, x^6/6, x^7/7, x^8/8,\ 
         x^9/9, x^10/10, x^11/11}

or ParallelMap

In[3]:= ParallelMap[Integrate[#, x] &, ints]
Out[3]= {x^2/2, x^3/3, x^4/4, x^5/5, x^6/6, x^7/7, x^8/8,\  
         x^9/9, x^10/10, x^11/11}

Obviously for small lists of integrals like the above, the parallelization overhead is probably larger than benefit. But if you have really large lists and complex integrals, then it's probably worth it.


Edit in response to comments

Given the really messy integrand that the OP is interested in (note: you should really simplify your results as you go!), here's some code that breaks the integral into a sum of monomials and performs the integrals using ParallelDo.

First we import the integral from pastebin

In[1]:= import = Import["http://pastebin.com/raw.php?i=JZ0CXewJ", "Text"];

extract the integration domain

In[2]:= intLimits = Rest@(2 Pi^5 ToExpression[StringReplace[import, "Integrate" -> "List"]])
        vars = intLimits[[All, 1]];

Out[2]= {{\[Theta]3, 0, 2*Pi}, {\[Theta]2, 0, 2*Pi}, 
         {\[Theta]1, 0, 2*Pi}, {\[CurlyPhi]2, 0, Pi/2}, {\[CurlyPhi]1, 0, Pi/2}}

and the integrand, which comes as the sum of 21 monsterous terms

In[4]:= integrand = First@(2 Pi^5 ToExpression[StringReplace[import, "Integrate" -> "Hold"]]);
        Length[integrand]
        LeafCount[integrand]

Out[5]= 21
Out[6]= 48111

We need to break the horrible mess down into bite sized chunks. First we extract all of the different functions from the integral

In[7]:= (fns=Union[vars, Cases[integrand, (Cos|Sin|Tan|Sec|Csc|Cot)[x_]/;!FreeQ[x,Alternatives@@vars],Infinity]])//Timing
Out[7]= {0.1,{\[Theta]1, <snip> ,Tan[\[CurlyPhi]2]}}

We find the (13849 nonvanishing) coefficients of monomials constructed from fns

In[8]:= coef = CoefficientRules[integrand, fns]; // Timing
         Length@coef

Out[8]= {35.63, Null}
Out[9]= 13849

Check that all of the coefficients are free of any integration variables

In[10]:= FreeQ[coef[[All, 2]], Alternatives@@vars]
Out[10]= True

Note that we can actually clean up the coefficients using Factor or Simplify and decrease the ByteSize by about 5 times... But since the integrals of most of the monomials are zero, we might as well leave simplifications until the very end.

This is how you reconstruct a monomial, integrate it and recombine with its coefficient, for example, the 40th monomial gives a nonvanishing integral:

In[11]:= monomialNum=40;
         Times@@(fns^coef[[monomialNum,1]])
         Integrate[%, Sequence@@intLimits]
         coef[[monomialNum,2]] %//Factor
Out[12]= \[Theta]1 Cos[\[Theta]1]^2 Cos[\[CurlyPhi]1]^4 Cos[4 \[CurlyPhi]1] Cos[\[CurlyPhi]2]^4 Cos[2 \[CurlyPhi]2] Sin[\[Theta]1]^2
Out[13]= \[Pi]^6/256
Out[14]= -((k1^2 (k1-k2) (k1+k2) (-2+p) p^3 \[Pi]^6 \[Sigma]^4)/(131072 \[Omega]1))

For now I'll reduce the number of terms, since it would take forever to do all of the integrals on my dual-core laptop. Delete or comment out the following line when you want to evaluate the whole set of integrals

In[15]:= coef = RandomChoice[coef, 100];  (* Delete me!! *)

OK, initialize an empty list for the monomial integration results

In[16]:= SetSharedVariable[ints]
         ints = ConstantArray[Null, Length@coef];

As we perform the integrals, we Print out num: {timing, result} for each monomial integrated. The CellLabel of each printed cell tells you which core did the integral. The printing can get annoying - if it does annoy you, then replace Print with PrintTempory or ##&. You could also monitor the calculation using a Dynamic variable of some sort: e.g. a progress bar.

ParallelDo[Print[c, ": ", Timing[
            ints[[c]] = Integrate[Times@@(fns^coef[[c,1]]), Sequence@@intLimits]]], 
           {c, Length@coef}]

Combine with their coefficients

1/(2 Pi^5) Simplify[ints.coef[[All, 2]]]

And (hopefully) that's that!