6
3
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?
6
3
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?
3
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 – 2011-07-28T13:20:03.860
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 – 2011-07-28T14:01:59.480Problem 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. http://www.maplesoft.com/products/maple/new_features/index.aspx#parallel
– derdack – 2011-07-28T15:39:12.763Sorry, 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 – 2011-07-28T17:28:14.340
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 – 2011-07-29T01:37:41.197
Of course not, I have f(x,y,z) under the three-integral which is so large on 20 pages in combination with constants, but limits of integrals are numeric values. Can I use all cores in this case and how? – derdack – 2011-07-29T10:58:50.190
Well... if your f(x,y,z) is so big, then maybe you can break it into pieces that you can treat separately and on different cores. It all depends on the structure of the integrand. Try using Apart
and Expand
and TrigExpand
, then turn hit the sum of terms with List@@
. If there are poles in the expanded function then you'll have to be more careful. But eventually, you might have to face the fact that most integrals can not be solved in closed form. – Simon – 2011-07-29T11:29:09.240
No, this integral has a closed form because it is combination of trigonometric function and my limits of integrals are Pi or Pi/2. So, I suppose that can be solved. But function is too large when I will use expand or apart, this will spend lot of time also on one core, ok if it will not spend much time for this it will be function on 50 pages? And what to to do after with the list? – derdack – 2011-07-29T12:07:36.833
I was thinking something like terms = List@@Apart[1/(x^2 - 1)]
Total@ParallelTable[Integrate[t, x], {t, terms}]
. Chuck the integrand in InputForm into pastebin (or the notebook onto a file server somewhere) and I'll have a quick look.
http://pastebin.com/JZ0CXewJ Also you got it on a mail. – derdack – 2011-07-29T13:10:38.333
@derdack: See edit – Simon – 2011-07-31T05:26:12.403
4
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 – 2011-07-26T16:36:18.630
Because I got this message on Win7: ParallelCombine::nopar: No parallel kernels available; proceeding with sequential evaluation. >> – derdack – 2011-07-26T16:40:11.967
1A "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 – 2011-07-26T16:48:44.703
4My 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 – 2011-07-26T16:05:21.073