Parallel computing for integrals [closed]
I want to reduce the calculation time for a time-consuming integral by splitting the integration range. I'm using C++, Windows, and a quad-core Intel i7 CPU.
How can I split it into 4 parallel computations?
What is your integration algorithm?
Mathematically, integral over range is sum of integrals over parts, so parallelization seems trivial.
Use OpenMP. gcc supports it. Visual C++ supports it.
As others have said, and I'm sure you would know integration is summation, parellization should be easy I know you are using C++, but is using go a possibility? It is very easy to use goroutines to do this type of job. But I understand if this code is going to a customer you would be reluctant to use go as it is still untested in the wild. If it is a personal project go for it (no pun intended).
Tackle this in 2 parts:
1) What approach to parallelisation are you going to take: MPI, OpenMP, threads, what-have-you ? and
2) How to modify the algorithm.
On a quad-core Windows machine, I'd suggest OpenMP as my answer to the first part of this. The answer to the second part depends on what algorithm you are using. If, for example, you are using the Monte Carlo technique, parallelisation is trivial. If you're using numerical quadrature of some sort, it's a lot less trivial to ensure that none of the corner cases that others have identified become serious problems. However, if you can exclude pathological integrals it shouldn't be prohibitively difficult
I suggest you look at intel's thread building blocks.
http://www.threadingbuildingblocks.org/
and especially parallel_reduce
.
The problem you'll have is boundary conditions.
It's easy to think that numerical quadrature can be broken up into several domains, but you don't know what the internal boundary conditions are.
Breaking an integral up into subdomains starts sounding like a finite element, finite difference, or boundary element solution. You've taken a complex domain, broken into to pieces, turned it into a matrix, and solved it given the boundary conditions you know.
I don't think this is as simple as breaking the domain into subdomains and assigning each one to a processor, but I could be wrong.
You can calculate the integral using the Trapezoidal rule (or any other of the Newton-Cotes formulas). To get a better result, you can segment the interval and use the Trapezoidal rule for each segment. The formula can be found in the linked article.
Since each calculation is independent of the other, you can execute them in parallel. The easiest way to do this is OpenMP (as mentioned above).
精彩评论