Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
1.38/5 (4 votes)
See more:
Hi all!

I'm trying to write a program that automatically creates as many threads as the computer can handle (not malware!).

How do you create an instance of PerformanceCounter that gets the usage across all CPUs/Cores?
All I've seen are examples that work on an individual core.


Thank you very much!
Posted
Updated 21-Sep-13 7:58am
v2
Comments
Sergey Alexandrovich Kryukov 21-Sep-13 21:51pm    
Not malware? Why then?
—SA
Homero Rivera 22-Sep-13 13:50pm    
Hah! Just trying to make computations as fast as possible using all CPU that's available.
Sergey Alexandrovich Kryukov 22-Sep-13 14:13pm    
Okay, then you are talking of the way how to make performance as low as possible. Isn't it obvious?
—SA

In general it is a bad idea to drive a system at it's limits.
But what's the problem with having those per core counters? Aggregate them, and you get an overview.
I suggest you check both approaches here: http://allen-conway-dotnet.blogspot.hu/2013/07/get-cpu-usage-across-all-cores-in-c.html[^]

Still, I would consider leaving at least one core below 100%, since your system would become unresponsive, and you would have no actual means to control it. Even more, I would set the affinity[^] of those threads to omit one specific core (0).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Sep-13 21:52pm    
Sure, a 5.
—SA
Homero Rivera 22-Sep-13 13:49pm    
Thank you Zoltan! Im just waiting till I get to work tomorrow and validate this in an 8-core PC. If we're good, I'll accept as an answer (my lousy computers at home are 1-core only).
Sergey Alexandrovich Kryukov 22-Sep-13 14:17pm    
Please see OP's clarification in the comments to the question added later, and also my answer.
It looks like OP is lost quite well, probably more than we thought.
—SA
Homero Rivera 24-Sep-13 12:55pm    
Zoltan, this was the right answer from the beginning. I am going to select this as the right answer. For the good of everybody in the forums, can you please consider editing your solution in a way that includes both code and reference?
In case my comments to the question are still unclear: as soon as you have more threads then the CPU cores (which is usually much, much less then the number of threads the system can possibly handle), adding each thread only degrades performance.

Threads are used mostly to implement scenarios where you need logically separate and almost independent scenarios of computing, not for improving performance.

[EDIT]

If you really need to measure some throughput, instead of performance counting, use the class System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

The accuracy of this thing is very high. To get an idea, read the static properties IsHighResolution (it should return true on Windows) and Frequency:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency.aspx[^].

This is the most accurate method you can get.

—SA
 
Share this answer
 
v3
Comments
Homero Rivera 22-Sep-13 14:29pm    
This information is very useful and appreciated! But please consider this scenario; Lets say I have a program that just computes and computes (e.g. something mathematical in series). I've seen my program working in different computers, some of them 1-core, some of them 4-core, some of them 8-core. When looking at the Task Manager, the 1-cores PCs are 100% busy, and the 8-cores seem quite relaxed (only 1 core at 50% or so, and the rest barely doing anything). Even in this scenario, would you asure that adding threads to the 8 core computer will not output more work in less time? Thank you very much for your help, Sergey!
Sergey Alexandrovich Kryukov 22-Sep-13 20:52pm    
In most cases, the single-thread solution could be fastest. Or use Parallel, then the actual number of threads can be optimized during runtime, depending on the number of available cores.
—SA
Homero Rivera 24-Sep-13 13:06pm    
I did an experiment in the 8-core computer. I created a console program that would get all prime numbers from a number to another (5 to 500000 was used for the experiment). I measured the time it took to compute for a single window. From 1 to 3 windows running at the same time I got 100 seconds readings, meaning I can get 3 times the amount of work with 3 threads but with the same 100 seconds. At 4 and 5 windows (threads) at the same time I got readings of 125 and 153 seconds respectively which is still more efficient than a single thread. Thus, creating more threads can result in more work done with the same time. However, it is obvious more and more time is required, but we do get more work done per time unit.
Sergey Alexandrovich Kryukov 24-Sep-13 13:22pm    
Of course. But isn't it obvious: at some point, adding another thread makes the total throughput worse?
Did you try Parallel instead of threads? Parallel is based on threads, but number of threads could be optimized during run time, which can give an apparent gain.
—SA
Homero Rivera 25-Sep-13 12:14pm    
Agree, after a certain number of threads the performance will be detrimental. The best performance I saw was at no more than 85%-90% CPU usage (so I guess the best is to stop creating threads once you reach 85-90 usage). I haven't try Parallel yet, I won't have time to study that untill the weekend but I will. thank you very much Sergey!
1. To use multiple cores you have to break your computation into multiple threads. The OS can't do that for you. Many problems cannot be broken down so you are left with single threaded, single core computation.

2. If you are able to rewrite your computation so that it can be broken into multiple parallel operations these can then be allocated separate threads. See OpenMP or Parallel.

http://www.greatlakesconsortium.org/events/scaling/files/openmp09.pdf[^]

OpenMP style multi-threading in C#[^]

3. The OS will then share these threads over multiple cores.

4. Even then because of overheads not all problems will see a gain in computational speed. Some may actually slow down.
 
Share this answer
 
v5
Comments
Homero Rivera 25-Sep-13 13:57pm    
Good answer!
[no name] 25-Sep-13 19:04pm    
Thanks

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