Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Parallelizing my C++ code

  1. Mar 27, 2013 #1

    I have written an extensive simulation in C++ and when I run it on my quad-core machine, only a single core is used (on Windows). This is because my code is not parallelized.

    I'm not familiar in any way with code running in parallel, except in Matlab where is happens almost automatically. Will it be an extensive procedure to parallelize my existing program?

    If anyone has any references/book titles/etc.. that they can recommend, I would be grateful.

  2. jcsd
  3. Mar 27, 2013 #2


    Staff: Mentor

    parallelizing code can be easy or difficult. It depends on the algorithm you're developing.

    As an example, fractal generation can be easily parallelized because its a localized computation on one point that doesn't depend on nearby point values. But something like a heat flow is more difficult because now you must use nearby cells to determine the value in the current. This means one cpu has to wait until another has completed a calculation for a neighboring cells.

    Anyway wikipedia has an article that talks about it briefly:

  4. Mar 27, 2013 #3


    User Avatar
    Homework Helper

    Assuming this is windows, you need to create threads to utilize multiple cores. There's some overhead in creating the threads and also to create the synchronization variables (mutex and/or semaphore).

    I created an example multi-threaded dos console program that copies a file, using the starting thread to read a file and a created thread to write the file. I created a linked list fifo messaging system for thread communication and synchronization that uses mutex and semaphores. You probably won't need a messaging interface, but that part of the code is not complex. Link to zip of example code:

    Last edited: Mar 27, 2013
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook