Reply
Multi-threading and CPU affinity
Old 02-28-2008, 03:02 AM Multi-threading and CPU affinity
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
I have software that's pretty fast, but I'd like to make it a little quicker. I've done as much to optimize the algorithm and data structures as can be done. I'm using a single thread, so there are a number of serial operations that could be run in parallel.

I'm thinking about spawning one thread per cpu core on the host machine, then setting their affinity so that each thread can only run on one core, and each core gets one thread. When a machine is somewhat idle, this should almost double the speed on my laptop, and do better on servers. There's a little bit of overhead managing the threads, and locking data, but the idea is probably a good one.

Unless some errant process monopolizes a particular core. Work seems to be pretty evenly distributed across both in my laptop, but ... I'm wondering if I'd be better off just letting Windows schedule the threads and hoping they really do run concurrently?
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
When You Register, These Ads Go Away!
Old 02-29-2008, 07:15 AM Re: Multi-threading and CPU affinity
Junior Talker

Posts: 4
Name: Victor
"There is no limit to improvements..."
The question is - what are you actually trying to achieve? Is it a practical goal or playing around on a specific computer, specific operating system, specific software development language, etc, etc...
If you want to make it running as fast as possible, why don't you run this 'thing' on a Japaneese weather forecast supercomputer?

PS. -Just to help maintaining this topic. It's late Friday in Melbourne, business week is over.
__________________
- MyOffice24x7.com -
Business Solutions for Small and Medium Companies

Last edited by vicatmyoffice : 02-29-2008 at 07:16 AM. Reason: mistyping
vicatmyoffice is offline
Reply With Quote
View Public Profile
 
Old 02-29-2008, 01:59 PM Re: Multi-threading and CPU affinity
willcode4beer's Avatar
Webmaster Talker

Posts: 695
Name: Paul Davis
Location: San Francisco
Quote:
Originally Posted by ForrestCroce View Post
I have software that's pretty fast, but I'd like to make it a little quicker. I've done as much to optimize the algorithm and data structures as can be done. I'm using a single thread, so there are a number of serial operations that could be run in parallel.

I'm thinking about spawning one thread per cpu core on the host machine, then setting their affinity so that each thread can only run on one core, and each core gets one thread. When a machine is somewhat idle, this should almost double the speed on my laptop, and do better on servers. There's a little bit of overhead managing the threads, and locking data, but the idea is probably a good one.

Unless some errant process monopolizes a particular core. Work seems to be pretty evenly distributed across both in my laptop, but ... I'm wondering if I'd be better off just letting Windows schedule the threads and hoping they really do run concurrently?
Yes, it is probably best to let the operating system handle thread scheduling. A major reason being that the OS is already handling threads for processes other than your own. So, it should be able to balance well (even if it is windows).

A major thing to keep track of are variables that are shared by instances of objects that run in different threads. Ideally you don't want them to change but, if they must make sure they are volatile.

The reason for this is, the values may be loaded in the cache of each CPU. If the value changes, then it may only be visible in the cache that changed it and main memory. But, the other CPU's cache may not see the change. So, ideally, don't change those types of values. Making the variables volatile, will ensure they always check against main memory, it'll also be slower as a result since, the cache has the same clock speed as the CPU and the main bus a fraction of that.

HTH
__________________
Paul Davis
willCode4Beer.com (coding for all the right reasons)
willcode4beer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Multi-threading and CPU affinity
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13956 seconds with 12 queries