How to make commands in Mathematica 8 use all cores?

Many commands in Mathematica 8 (`Integrate`, `Simplify`, etc.) seem to only be using a single core on my system. Is there any way I can change the affinity so that it utilizes all cores for computations?

-
My guess is that those commands can not be easily parallelized and therefore won't deliver better performance when run on different cores at the same time. That's why Wolfram didn't bother to implement it for parallel computing. I'm no expert in theoretical computer science and mathematics though, so I wouldn't know how to implement an integration algorithm. –  slhck Jul 26 '11 at 16:05

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.

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!

-
I have integral by five variables, ok for beginning I can integrate one by one, but the problem is how to use all cores for this one integral. Of course FullSimplify command also can not be parallelized. –  derdack Jul 28 '11 at 13:20
An iterated/multidimensional integral? If it's numeric, then you could parallelize a Monte Carlo evaluation. If it's an analytic result you want, you might be in trouble. What I meant by the FullSimplify comment is that the algorithm probably has room for some parallelization - not that you could get Mma to parallelize it in its current form. –  Simon Jul 28 '11 at 14:01
Problem is that I want analytic result. For a beginning I can decouple my multiple integral. But Analytic calculation for just one can be one month or I don't know how long, because I have computer with 16 cores and I can use just one for calculating. "Maple 15" wrote that they have parallel computation in this version, but I tested and for integrating also couldn't work on many cores in same time. maplesoft.com/products/maple/new_features/index.aspx#parallel –  derdack Jul 28 '11 at 15:39
Sorry, I mean my limits of integrals are numerical values. I have for example: Integrate [0..5] Integrate [0..8] Integrate [0..2](large function) dx dy dz. Large function is in the form: ax+by+c*z, where a,b,c are analytical constants in form of a, b, c. Can use parallelize in this case? –  derdack Jul 28 '11 at 17:28
So you have an integral of the form int (a x + b y + c z) dx dy dz where a, b and c are independent of x, y, and z? Isn't that trivial - or am I missing something? –  Simon Jul 29 '11 at 1:37

From the Parallelize doumentation, under Examples > Possible Issues:

Expressions that cannot be parallelized are evaluated normally:

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

-
Do you think that it is the same problem under the Linux? –  derdack Jul 26 '11 at 16:36
Because I got this message on Win7: ParallelCombine::nopar: No parallel kernels available; proceeding with sequential evaluation. >> –  derdack Jul 26 '11 at 16:40
A "kernel" in computation is, simply speaking, only the part of an algorithm that is applied on the data over and over again. If there is no parallel kernel for a task, it can't be run in parallel. This is the same for every operating system and solely dependent on the software, i.e. Mathematica. –  slhck Jul 26 '11 at 16:48