Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Set Program Affinity (Set CPU core) - usefull for Dual/Multi CPU systems

Author
Message
Manson_NS
20
Years of Service
User Offline
Joined: 4th May 2004
Location: Denmark
Posted: 6th Aug 2007 11:19
This piece of code allows you to set which CPU or CPU core your program should run on - should work on both XP and Vista.

Basic use: set_affinity(2) - will set your program to run on core no. 2.

Attachments

Login to view attachments
Dazzag
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Cyprus
Posted: 6th Aug 2007 15:47
Interesting. Does it show how many you can choose from? What would also be nice is if you can detect the amount of idle time for each processor (so you can nicely choose the most under used one). And can you then change CPU mid runtime? Would be nice to monitor each CPU every few minutes or so, then change CPU to the one that is least stressed.

Cheers

I am 99% probably lying in bed right now... so don't blame me for crappy typing
Current fave quote : "She was like a candle in the wind.... unreliable...."
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th Aug 2007 16:05 Edited at: 6th Aug 2007 16:06
There's some interesting information about using multiple processors here http://msdn2.microsoft.com/en-us/library/ms684251.aspx.

Quote: "What would also be nice is if you can detect the amount of idle time for each processor (so you can nicely choose the most under used one)."

I think this happens anyway.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
gamebird
17
Years of Service
User Offline
Joined: 13th Jun 2007
Location:
Posted: 1st Sep 2007 02:36
Quote: "Quote: "What would also be nice is if you can detect the amount of idle time for each processor (so you can nicely choose the most under used one)."
I think this happens anyway."


Only on dual cores. I read somewhere that because quad cores (right now) are 2 dual cores stuck together you can't tell which is idle. That is why it can't save power by turning cores off- it would require some modification to do so and it would be easier in the end to just make a real quad core processor.

Anyway, I think he means could you program it.
Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 1st Sep 2007 04:55
It didn't work for me, still eats up half of both CPUs. When I change the affinity to 0 in task manager however CPU 0 maxes out and CPU 1 is virtually idle.

I'd like to see how this works when successful. Would letting it execute on only 1 CPU be increase FPS because it doesn't have to constantly switch? I'm not a hardware expert so I'm not sure...

Ian T
22
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 5th Oct 2007 02:50
Interesting. If only this could be plugged into other applications smoehow - I've got a number of games that I can't play on Vista unless I either disable multithreading in the BIOS (which I dislike doing since it's obviously useful for more modern games) or set single core affinity in the task manager to run at all. Thanks
TinTin
18
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 9th Oct 2007 17:11
I'd like to be able to set the affinity of sections of my application, e.g. Main Loop to CPU 1, Loading to CPU 2 etc..

This would allow for the creation of multiple threaded applications.

Great stuff..

Go Create...

Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 9th Oct 2007 17:46
Quote: "I've got a number of games that I can't play on Vista unless I either disable multithreading in the BIOS (which I dislike doing since it's obviously useful for more modern games) or set single core affinity in the task manager to run at all."

How come?

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
The admiral
22
Years of Service
User Offline
Joined: 29th Aug 2002
Location:
Posted: 10th Oct 2007 05:00
Yeah setting parts of code to run on cetain cores would be good.

The admiral

Login to post a reply

Server time is: 2024-11-22 13:17:11
Your offset time is: 2024-11-22 13:17:11