C/C++ How can I parallelize my C++ code to utilize multiple cores?

  • Thread starter Thread starter Niles
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary
The discussion centers around the challenge of parallelizing a C++ simulation program that currently runs on a single core. The original poster seeks guidance on whether the process of parallelization will be extensive, given their lack of experience with parallel programming. Responses highlight that the difficulty of parallelizing code varies based on the algorithm; for instance, fractal generation is easier to parallelize compared to heat flow, which requires synchronization between computations. It is noted that on Windows, utilizing multiple cores involves creating threads, which introduces some overhead for thread management and synchronization. An example of a multi-threaded program is shared, demonstrating file copying with thread communication through a linked list and synchronization mechanisms like mutexes and semaphores. Additionally, a Wikipedia article on parallelization is recommended for further reading.
Niles
Messages
1,834
Reaction score
0
Hi

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.


Niles.
 
Technology news on Phys.org
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:

http://en.wikipedia.org/wiki/Parallelization
 
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:

http://rcgldr.net/misc/mtcopy.zip
 
Last edited:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
7
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 39 ·
2
Replies
39
Views
7K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K