Taking advantage of both cores using C++

Discussion in 'Mac Programming' started by vgoklani, Nov 2, 2006.

  1. vgoklani macrumors regular

    vgoklani

    Joined:
    Jul 2, 2004
    #1
    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)
     
  2. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #2
    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.
     
  3. rtharper macrumors regular

    rtharper

    Joined:
    Sep 6, 2006
    Location:
    Oxford, UK
    #3
    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.
     
  4. vgoklani thread starter macrumors regular

    vgoklani

    Joined:
    Jul 2, 2004
    #4
    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.
     
  5. rtharper macrumors regular

    rtharper

    Joined:
    Sep 6, 2006
    Location:
    Oxford, UK
    #5
    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.
     
  6. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #6
    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).
     
  7. geomes macrumors newbie

    Joined:
    Sep 10, 2006
    #7
    C++ and threads

    The first step to writing threads in C++ is using a library that supports threaded programming. Boost has one called boost.threads.
    http://www.boost.org/
     
  8. jalagl macrumors 6502a

    jalagl

    Joined:
    Jun 5, 2003
    Location:
    Costa Rica
    #8
    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
     

Share This Page