Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vgoklani

macrumors regular
Original poster
Jul 2, 2004
186
0
Hi,

I wrote a small simulation in C++, and upon execution, I noticed that only one of the two cores was doing the actual processing (courtesy of menumeters). Is there a way to take advantage of both cores, ie: is there some sort of compiler flag that I am supposed to use?

This is how I compiled my code:

g++ simulated_annealing.cpp

some background info: I am running everything from within terminal, and I have the latest version of XCode installed (hence I am using gcc 4.01). I am running all this on my Macbook Pro.

(btw, I aplogize if you see this post twice - I originally posted it in the wrong forum)
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
If you don't use multiple threads or subprocesses, you'll be on a single core.

This isn't just you, there are tons of well-used programs that are stuck in that rut.
 

rtharper

macrumors regular
Sep 6, 2006
201
0
Oxford, UK
Hi,

I wrote a small simulation in C++, and upon execution, I noticed that only one of the two cores was doing the actual processing (courtesy of menumeters). Is there a way to take advantage of both cores, ie: is there some sort of compiler flag that I am supposed to use?

This is how I compiled my code:

g++ simulated_annealing.cpp

some background info: I am running everything from within terminal, and I have the latest version of XCode installed (hence I am using gcc 4.01). I am running all this on my Macbook Pro.

(btw, I aplogize if you see this post twice - I originally posted it in the wrong forum)

This is totally a normal issue for simulated annealing. To be honest, though it is difficult to parallelize simulated annealing and often times the overhead of communication outweighs the potential benefits. Most parallelised versions of simulated annealing involving running sequential simulated annealing multiple times to find the best solution, since simulated annealing solutions are stochastic.

If you are running multiple simulated annealing runs at the same time, putting them into separate threads is trivial.
 

vgoklani

macrumors regular
Original poster
Jul 2, 2004
186
0
parallelizing simulated annlealing would make it a replica exchange simulation. I was under the impression that the OS would find a way to force both cores to do the work, but apparently not. I guess this is a job for MPI.
 

rtharper

macrumors regular
Sep 6, 2006
201
0
Oxford, UK
parallelizing simulated annlealing would make it a replica exchange simulation. I was under the impression that the OS would find a way to force both cores to do the work, but apparently not. I guess this is a job for MPI.

The only way that any system with multiple processors, whether it be distributed, SMP, or multi-core, can distribute a task is if there are multiple threads of execution (even if they aren't necessarily represented as threads). The onus is on the programmer to split this stuff up. The thread scheduler can't magically parallelize them.

You don't have to use MPI, using shared storage is also acceptable on a multi-core system.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Yeah, for now you are mostly on your own to force the application to do more than one thing at once, but the situation is slowly improving. Intel Fortran (also, their C++ product) can now do some automatic parallelization. A future release of GCC will likely have some support for this too, details here (PDF).
 

jalagl

macrumors 6502a
Jun 5, 2003
802
1
Costa Rica
OpenMP

You can also try out OpenMP. I've used it a lot on other platforms, and it produces great results with minimal effort. You still need to be careful with shared variables, critical regions, and other details of threading, but hey, threading has never been easy.

In OpenMP, for example, if you want to parallelize a for loop, you need to write something like this:

Code:
#pragma omp parallel for
for (int i=0;i<10;i++){
  //Iterations are assigned to threads

  //This creates a critical region
  #pragma omp critical
  {
    //Critical region
  }
}

I think that would be a good place to start with threading. You can later move on to specific threading libraries. I've used it with the Intel Compiler for both Windows and Linux (on EM64T & Itanium). I'm not sure, though, if the gcc version that currently ships with OSX supports OpenMP - I know version 4.2 does, and I found GOMP by quickly googling for information.

You can read more about openmp on the official site
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.