Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



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

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
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?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
 
Register now for full access!
Old 02-29-2008, 08:15 AM Re: Multi-threading and CPU affinity
Junior Talker

Posts: 4
Name: Victor
Trades: 0
"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 -

Please login or register to view this content. Registration is FREE

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

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
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
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Multi-threading and CPU affinity
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.12581 seconds with 12 queries