Numerical simulations using multiple cores
By Jose at Fri, Sep 24 2010 4:02pm |
Is it possible to run numerical calculations that can take advantage of the multiple cores in a Mac?
I've got an Quad-core Intel i7 iMac with Snow Leopard. I have a fortran code that I compiled with gfortran 4.6 that solves a system of conservation equations. Running this code can take several hours. I use istat menus to check on the cpu usage and distribution of processes on the different cores. When I run the fortran code, the iMac only uses one of the four cores. It switches from one to another but it always uses 100% of only once core at a time.
Does anybody know how can I get my machine running this code on two or three cores?




The easiest way to do this
The easiest way to do this is to use OpenMP. Basically this a set of compiler directives that allow to split the work on different cores. These are supported by most (if not all) Fortran compilers (including recent versions of gfortran). You can find good tutorials on the web.
I second OpenMP. I actually
I second OpenMP. I actually learned about it here at MacResearch (http://www.macresearch.org/screencast_introduction_to_openmp). It's a relatively easy technology to implement in your code.
OpenMP doesn't seem too
OpenMP doesn't seem too complicated but I guess that it'll take a lot of testing to make sure that it does what it's supposed to do. Thanks for the advice.
Do you know whether it is possible to use other technology such as Grand Central Dispatch (GCD) or OpenCL along with fortran or C++ codes?
I don't think GCD works with
I don't think GCD works with Fortran. It works well with C/Objective C; I have not encountered problems with Objective C++, but plain C++ is unworkable at present (Snow Leopard/gcc 4.2): although GCD itself works ok, blocks are not supported in the slightest in C++ code (if you are very lucky the code will compile, but it will often not generate correct code, making it total russian roulette to program with).
That proviso aside, I think it is an excellent technology. Once you understand how it is meant to work I find it very quick and easy to write concurrent code using that architecture. It does however require a good understanding of the subtleties and pitfalls of multithreaded programming, issues that I think OpenMP generally does a pretty good job of shielding you from.
Although I don't have direct experience of OpenCL, having worked with similar technologies I would caution against it in most cases. If you can use existing libraries to offload work onto the GPU then great, but as soon as you start writing your own code you are creating some serious work for yourself. The whole paradigm is completely different to C/Fortran type programming, and a very clear understanding of memory management issues, pipelining and other low-level concepts are absolutely essential to be able to write code that is even close to a single CPU thread in performance. With enough investment of time and expertise you can go better than that, and write screaming fast code (for certain problem types at least...), but this is definitely not an easy route to take. I would suggest it is very rarely worth investing time in this if you are writing code just for yourself/your research group. The time invested is only worth it for a major commercial software project. Just buy more machines instead of paying yourself for months of development time!
You might want to check out
You might want to check out the "HPC on Mac OS X" website at http://hpc.sourceforge.net/.
For a commercial Fortran, definitely check out "Absoft Pro Fortran for Intel Mac OS X" at: http://www.absoft.com/Absoft_MacIntel_Compiler.htm. This system has auto-parallelization and auto-vectorization, as well as OpenMP and MPI support. It generates fast, multi-core, and multi-threaded 32bit and 64bit applications. The IDE, Debugger, and Technical Support are also excellent. (No affiliation, just a very happy Absoft customer.)