Detect the CPU Core Count From Silverlight

Nov 12, 2009

If you're are writing an application that is heavy on multi-threaded computations (e.g. full screen blur, game, or scientific data processing), you will want to know how many threads to run optimally.

Edit: just fixed a bug reported where the initial assesment woult be 0 msec, thanks Morten for reporting it!

The answer is easy: run as many threads as the CPU cores. For example, on Dual Core, you should run 2 threads and on Quad Core 4 threads.

Download Source Code

This is how to find the number of cores:

  1. Create a simple computing function (e.g. that adds +1 to a number continuously) and run it with 1, 2, 4, 8, and 16 threads
  2. Measure the time it takes for the function to complete for each set of threads.

Once you hit the "core limit" of the client system, the time will significantly increase. Here's an example from my box:

  • 1 thread: 132 msec
  • 2 threads: 111 msec
  • 4 threads: 140 msec
  • 8 threads: 822 msec

If you look at the above data, you can easily tell I'm running on a quad-core system, because jumping from 4 to 8 threads significanly increases the computational time needed (more than 1.8 times).

This is how to use the source code:

int coreCount = PerformanceMeasure.GetCoreCount();

You can call the above function from the UI thread.

There are also two tweaks in the code that allows it to run roughly at the same speed on all machines and run faster on single-core machines too:

  • Once the core limit is hit, the algorithm stops. E.g. if you find that 4 threads take more than 1.8x the time as compared to 2 threads, this means you have 2 cores and there is no need to test with 8 threads
  • Before the main algorithm (above) starts, there is an estimation step, which calculates how many operations can be executed for 100 msec on 1 thread. This ensures that the assesment will run fast even on slow machines.

Please comment! I would be interesting to know how well the algorithm works and if it detected your cores as you expected!

  

Comments

11/12/2009 8:35:03 AM #

Isak

Nice! Worked perfectly for me.

1 thread: 112.01
2 thread: 116.01
4 thread: 127.01
8 thread: 309.02
Detected cores: 4

(Ran on 2.2ghz quad core xeon)

Isak | Reply

11/12/2009 5:06:40 PM #

nokola

Thanks! Great! Smile

nokola | Reply

8/19/2010 9:56:00 PM #

Send Free SMS to any mobile in India

Bilaroo.com: Register & collect Bilaroo Coins & Win Lowest and Unique bid with our daily auctions, Free Online mobile recharge & Send Free SMS to any mobile in India.

Send Free SMS to any mobile in India United States | Reply

11/12/2009 6:15:57 PM #

Morten

The first time I ran it, it detected one core, second time it correctly detected two.
On the first run, the first thread ran for 0ms, so there's some type of bug there I think.

Morten | Reply

11/12/2009 6:25:01 PM #

Morten

After running the test over and over again, it most of the time reports only 1 core.
1 thread: 87.01
2 thread: 309.02
Detected cores: 2


...funny how the last digits is always .01, except the last run which is .02.

Morten | Reply

11/12/2009 7:10:52 PM #

nokola

Hi Morten, just fixed the bug you reported! Thanks! Source & sample is updated (you may have to clear browser cache though)

nokola | Reply

11/12/2009 7:14:35 PM #

nokola

btw, does your CPU have multiple cores or does it use HyperThreading?
If it's using HyperThreading then the correct number of threads to run would be 1 (depends on the CPU), in any case going from 87 to 309ms means that there is no point in running more than 1 thread.

nokola | Reply

11/12/2009 10:51:28 PM #

Morten

I'm not using HyperThreading (T7400 Core2Duo). It seems to be returning 2 cores more consistently now, however once in a while it's still "fails" and returns 1 core. I only saw the 0ms the very first time I loaded the page and ran the test. Never saw it again after that.

Morten | Reply

11/13/2009 1:14:21 AM #

nokola

Interesting...perhaps I have to increase the 100 ms time for running the test to 150-200ms or so. It used to be 250 ms before, which had more "stable" timings/results, but slower.

nokola | Reply

11/13/2009 7:21:32 AM #

nokola

fixed it! Smile Morten, could you try now? There are few extra checks to guard against edge cases (if the measurement is below 100ms it is ignored) and the timing is now 150ms.

Please let me know if it works for you! Thanks a lot for trying!

nokola | Reply

11/13/2009 8:45:07 PM #

Sl.ayer

Works pretty well on my core duo, but it did detect 1 core once out of about 30 tries. You might want to change 1.8 multiplier to a bigger one. On my computer going from 2 to 4 threades results in execution time growing by factor of 2.5 or greater.

Sl.ayer | Reply

11/13/2009 9:18:32 PM #

Mark

Nice - thought not altogether consistant, but I imagine this can be largely affected by what else is running on each core. Am running an intel i7 860 @ 2.8 Ghz and it alternates at between 4 and 8 cores. Seems like a great idea for optimizing apps though despite that.

Mark | Reply

11/13/2009 10:38:15 PM #

nokola

Thanks Everyone! The factor is now 1.9 and I'll leave the algorithm as-is. As Mark mentioned, it is close enough and will definitely give your application an edge in almost all cases Smile. To get the several cases where the cores may be incorrect, you can add an "advanced option" to your app ui that allows manual override. I'll improve this algorithm more in the future (unless someone finds a super bad bug Smile), and for now will focus on other things (like actually using those threads for something usefulSmile. Please continue to post results - I appreciate it!

nokola | Reply

11/14/2009 9:34:02 PM #

alex

2 cores

1 -150
2 -208
4 -653

But I'd like to add some comments.
If my 2nd cpu is under pressure because of antivirus or smth other or if it's VM with 2 virtual cores, I think you will not detect.


alex | Reply

11/16/2009 6:21:38 AM #

nokola

Thanks for the info and comment Alex!
yes - you're right. Anyway, if you use this for optimizations, you probably care about the optimal number of threads that you're supposed to run (not number of cores even though it should match in most cases). In such case this code would work fine I think. But in case you want to get the number of cores very reliably, this code will not always return it right.

Thanks again for posting your data.

nokola | Reply

1/24/2010 9:31:55 PM #

LLH

Very nice. But I guess you were not aware of System.Environment.ProcessorCount?

LLH | Reply

1/24/2010 11:22:05 PM #

nokola

Hi LLH,

To be frank, I wasn't aware until you mentioned it! Smile
System.Environment.ProcessorCount is a security critical member, which means that application code using it will throw an exception if you try to use if from your Silverlight code.

nokola | Reply

1/25/2010 9:57:59 AM #

LLH

OK, I see - I've only ever used it from standard .Net application code so I never gave that issue any thought.

BTW: When running your algorithm on my dual core laptop, I can see that the laptop shuts down one processor core when running on battery Smile

LLH | Reply

1/29/2010 9:48:55 PM #

nokola

Interesting Smile (about the core shutting down)
I raised a question with the Silverlight runtime team today regarding why is ProcessorCount marked as security critical.

nokola | Reply

2/28/2010 10:44:37 AM #

convert mp4

Nice post can i know still more about silver light.like how it its work

convert mp4 United States | Reply

2/28/2010 5:59:31 PM #

nokola

Thanks - yes! a good way to learn about Silverlight is to visit http://silverlight.net/GetStarted
When I first started with Visual Studio, the information was a bit overwhelming at first, but quickly came into place.
Thi is a good "Getting Started with Silverlght" video: silverlight.net/.../

nokola United States | Reply

3/1/2010 1:19:40 PM #

Rok Mejak

For me too not working and most time showing error
1 thread: 87.01

Rok Mejak India | Reply

3/1/2010 7:45:47 PM #

nokola

How many cores do you have?

nokola | Reply

4/2/2010 8:39:16 AM #

Pizza Recipe

I have gathered some better information from this

Pizza Recipe South Africa | Reply

4/2/2010 9:33:04 AM #

dubai apartments

really its very interesting and informative stuff thanks for sharing it, I have shared this with my friends too..

dubai apartments U.A.E. | Reply

5/28/2010 7:12:58 AM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Detect the CPU Core Count From Silverlight
        [nokola.com]
        on Topsy.com

topsy.com | Reply

6/23/2010 9:07:53 AM #

Tech News

How can multiple CPUs be identified in a Silverlight application..?

Tech News India | Reply

8/19/2010 2:20:22 PM #

Dubai apartments

second time it correctly detected two.
On the first run, the first thread ran for 0ms, so there's some type of bug there I think.

Dubai apartments United States | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



nokola.com | Terms | Log in

Recent

About the author

Happy & enjoying life. Software enthusiast.
The opinions I express here and on nokola.com are mine and not my employeer's (Microsoft).
This is the official blog of nokola.com. You can find Silverlight samples, coding stuff, and hopefully other interesting things here.