Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all
I parallelized a part of my application. From my point of view the problem is predestined to parallelize it: Simply N independent tasks.

Not parallelized I observed, that on a quad core my process will consume 25% cpu. My naïve assumption was that if I run e.g. three threads the quad core should consume something around 75%.

But it is unfortunately not like this, my application (after parallelization) still consumes only about 25% of cpu...and more... it takes much more time to execute all N jobs. I also checked the three threads and found that each thread processed about 1/3 of the jobs.

Do I need to create real parallel processes and not only threads?

Environment: Borland C++ Builder V6 ( :( ) .
Note: Jobs use a lot of STL containers for job's data. Maybe the problem?


Any ideas?

Thank you very much in advance.
Regards, Idle63
Posted
Updated 6-Oct-13 5:00am
v4

1 solution

Its a common misconception that creating multiple threads will automatically improve the performance of a long running task. As long as you let the OS determine processor affinity there is no guarantee that the threads will run in parallel.

I'm sure that you know that a single core even running multiple threads only runs one thread at a time. Add on top of that the overhead of task switching, stack management, and memory space for the threads, they will actually take more time than just doing the task in a single thread.

A computer can only run as many threads in parallel as there are processor cores, and as long as you set the affinity for each thread to use a different core. The OS chooses the core based on which one is least loaded (I think), so if core 1 is the least busy, it may get all 4 cores threads without specifying affinity.
 
Share this answer
 
v2
Comments
[no name] 5-Oct-13 11:16am    
Ron, thank you very much for your answer

a.) As long as you let the OS determine processor affinity ...
Can I influence this? If yes how? N.B. My main application is not assigned to a specific cpu, means it can use any cpu (this information I got from task manager)

b.) Single core
I'm aware of this

c.) Affinity
Again, how can I influence this?

Thanks, regards, Bruno
Ron Beyer 5-Oct-13 11:23am    
You can set the affinity using SetProcessorAffinityMask in Windows. You may have to dig around for a good reference on it and the arguments.
[no name] 5-Oct-13 11:51am    
I already do this with Affinity. I mean all cpu's are available for the application...but my app do not use them...maybe also a thing that bcb6 is meanwhile antiquated :(
Thanks again for your time. Regards, Bruno
[no name] 7-Oct-13 11:06am    
Once more I was searching the lazy way. After hours of debugging I had to recognize that the problem is -as usual- 40cm in front of the screen…it was me…now everything works fine and 7 threads against single thread is five time faster.

Thanks for your help, regards, idle63

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900