CPU Affinity Made Easy

Discussion in 'Guides and Tutorials' started by KiD0M4N, Feb 19, 2006.

  1. KiD0M4N
    Offline

    KiD0M4N Upgraded User

    Source: Messa :)

    Hi guys,

    This guide should help you easily setup automatic CPU affinity assignments. Even if you dont own a dual core CPU, this is a must read as it uses some of the most basic commands available to all Windows users to achieve this.

    Presenting to you a TE and Kidoman original :hap2: (atleast I did this myself, dunno if was published earlier):

    1. Reference screen shots below for any silly doubts......
    2. Now, create a shortcut to the program you want to assign CPU affinities to.
    3. After the shortcut has been created, open the Properties page.
    4. You can see the full path of the executable in the Target box.
    5. Add "C:\WINDOWS\system32\cmd.exe " (the path to your command prompt interpretor) before the target path (see attached images for clarifications.)
    6. The command interpretor has a inbuilt subcommand known as "start" It allows the end users to set various options to the started executables by passing command line arguments. A short synopsis of the options is:

      [​IMG]

      Use Start > Run > cmd > start /? to see a exhaustive list of options.
    7. Now add a "/C start " after the previously added command. This instructs the command interpretor to consider everything after the /C as a command. The start keyword causes it to invoke the "start" sub command.
    8. Then add whatever options you want to pass to "start" In our case, it will be "/AFFINITY 1 " to set the affinity of the started process to Core 0 (remember 1 corresponds to core 0, 2 to core 1, and A (hex) to core 9 (if that were the case))
    9. Save the short-cut and Apply/Ok the settings. Run the app and fire up taskmanager to see that the desired effect is being achieved.

    I have made exhaustive screenshots so as to remove any doubts. However, as always, the inquisitiveness of the human mind shall prevail and thus doubts will arise. So fire any questions you want. Also, I am in dire needs of Rep points, so those would be welcome too :cool2:

    Regards,

    Karan

    PS: Mods I tried putting the images in the GAR, but instead of the GAR wrapping the images around inside it, it tiled them horizontally and most of them went outside the screen. So i have posted the images in the post directly. Hope thats okay.

    Zip File containing all the images for easier download and viewing.

    [​IMG][​IMG][​IMG][​IMG][​IMG]

    [​IMG][​IMG][​IMG][​IMG][​IMG]

    [​IMG][​IMG][​IMG][​IMG][​IMG]

    [​IMG][​IMG][​IMG][​IMG][​IMG]

    [​IMG][​IMG]
    3 people like this.
  2. 4mer
    Offline

    4mer Upgraded User

    nice post karan:D but images not working:bleh:
  3. KiD0M4N
    Offline

    KiD0M4N Upgraded User

    eh? i can see all the images right in front of me...... whts up?
  4. Aditya
    Offline

    Aditya Upgraded User

    Repped!
    This will help Dual Core users a lot!

    Thought with the /PMTimer it doesnt matter... :D

    Im still gonna use to set Priority...
  5. KiD0M4N
    Offline

    KiD0M4N Upgraded User

    thnx man........
  6. DasFox
    Offline

    DasFox New Member

    I did this, here is my Target:

    C:\WINDOWS\system32\cmd.exe /C start /AFFINITY 1 D:\Games\Oblivion\OblivionLauncher.exe

    When I click the game icon nothing happens, all it looks like it's trying to do is just lauch the cmd prompt.

    What am I doing wrong?

    THANKS
  7. LocoValue
    Offline

    LocoValue New Member

    I'm having the exact same problem for Warcraft III. I've tried without quotes around the game location, with quotes, with quotes around the location and the start commands, everything. All of them have the exact same problem, which you described.

    It might be related to the fact that I do indeed have a dual core processor, but when I go into a cmd prompt and type "start /?", there's one option missing, the /AFFINITY option; it has all of the others that are shown in the screenshot of the cmd prompt in the original post.

    Does anyone have any ideas why /AFFINITY isn't showing up in the list of possible start parameters, and why we're having the problem of the cmd window coming up but then not launching the program?
  8. Sandy
    Offline

    Sandy Privileged Users

    use this much easier ;)
    http://www.geocities.com/edgemeal_software/LnS/Index.htm
  9. Systemic Anamoly
    Offline

    Systemic Anamoly Privileged Users

    nice guide there karan although u people can try this as well

    Force Core
  10. Faile
    Offline

    Faile New Member

    short addition to this guide. the numeric parameter after /affinity is a hexadecimal parameter like stated earlier, but it's not as straightforward as 1,2,3,4,5...

    if you know how ownership modes (chmod) codes work then this will be a breeze:

    1st core = 1
    2nd core = 2
    3rd core = 4
    4th core = 8
    5th core = H ( hexadecimal 16 )
    6th core = 1H ( hexadecimal 32 )

    now if you want something to run on just one core, you pick the right number like this, just multiply by 2 until you get to the core you want.

    if you want something to run on two cores, you ADD their values:

    1st and 2nd core = 1 + 2 = 3
    1st and 3rd core = 1 + 4 = 5
    2nd and 3rd core = 2 + 4 = 6
    2nd and 4th core = 2 + 8 = 10

    Same thing if you want more than two cores, just add each core numerical value up and use the sum as the /affinity parameter

    Tested and retested on a 8 core system.

    HTH
  11. Faile
    Offline

    Faile New Member

    ok, sorry.. im tired, correction:

    1st core = 1
    2nd core = 2
    3rd core = 4
    4th core = 8
    5th core = 10 ( hexadecimal 16 )
    6th core = 20 ( hexadecimal 32 )
  12. jmaster51984
    Offline

    jmaster51984 New Member

    I had been banging my head against a wall with this for some time. I finally got it mostly figured out, here are some tips to make your life easier. :hap2:

    Firstly, if you are having the issue where it looks like it's just attempting to open a cmd prompt. It might be because your either not pointing to the working directory or your working directory requires "quotes".

    To get around this do a search on your system for "cmd". It should be in C:\Windows\System32. Copy the cmd.exe to the directory with the executable you want to run in affinity or priority. You can do it from the GUI or from a command line. In this example I'll use virtual pc 2007.

    copy c:\windows\system32\cmd.exe c:\program files\microsoft virtual pc\

    Second, now that a copy of cmd.exe is in your working directory with your executable, right click on your newly added cmd.exe and create a shotcut.
    You can now move the shortcut to wherever you want (Desktop, Start Menu, wherever.)

    Third, if you fire up your shortcut now you'll notice that it automatically starts you in the directory where your copied cmd is sitting. It would show up like this. (Note: this test is being done on Vista Ultimate 64-bit for reference)

    Microsoft Windows [Version 6.0.6000]
    Copyright (c) 2006 Microsoft Corporation. All rights reserved.

    C:\Program Files\Microsoft Virtual PC>

    Third, now right click on your shortcut and go to properties. Where you see "Target" you should see something like this.

    "C:\Program Files\Microsoft Virtual PC\cmd.exe"

    This is where you add your start command, throwing the /c tells it to execute a command and then terminate. Then you can add your priority switches and affinity switches with the Start command. Add this right into your shortcut target Like so.

    "C:\Program Files\Microsoft Virtual PC\cmd.exe" /c start /realtime /affinity 3 vpc.exe

    The /realtime is the priority I chose for my program, by choosing affinity 3 I'm telling the program to only use processors 0 and 1. This example is being done on a 4 processor system, I would not recommend using /realtime unless you have remaining processors to handle the load, If this system only had 2 processors I would not exceed /high for my priority.

    (Note, the virtual pc 2007 executable is normally named "Virtual PC.exe" I have renamed it "vpc.exe" to avoid having to use "" quotes which causes the command to fail. As best I can tell I think it's because windows doesn't like playing with 2 sets of "" quotes in the same command, just my guess here.)

    Here comes a complicated part, choosing the value for affinity, notice below there are some letters, thats not a typo, the /affinity switch is looking for a Hexadecimal value. This should be fairly accurate, math was never my best subject though.

    1 does processor 0
    2 does processor 1
    3 does processors 0 and 1
    4 does processor 2
    5 does processors 0 and 2
    6 does processors 1 and 2
    7 does processors 0, 1, and 2
    8 does processor 3
    9 does processor 0 and 3
    D does processor 0, 2, and 3
    F does processors 0, 1, 2, and 3
    This Microsoft technet chart may help.
    Microsoft Corporation

    Also note, some programs are setup to force a specific affinity, I have run into some programs using this technique that will not take affinity settings past a certain number of CPU's. So for example, if you set /affinity F and the application is still not using all 4 cpus, this does not necessarily mean you did the command wrong, the program may just be enforcing it's affinity.

    Now that you have your shortcut setup you can change the name, the icon, if your using Vista you can right click on the shortcut, change icon, and browse to the original .exe to use the same icon file.

    This was driving me nuts for some time :mad: I hope this helps somebody out!:clap:
    1 person likes this.
  13. ..:: Free Radical ::..
    Offline

    ..:: Free Radical ::.. Privileged Users

    manually setting affinity was needed in the days of the dual cores.

    C2Ds, quads and modern AM2s don't need that due to recent optimizations and drivers.

    I was also intrigued by affinity settings when I bought my am2. But my queries were answered by some old timers at amd forums.

    Your boot.ini wud have the switche /usepmtimer if you have the optimal dual core drivers.
  14. KingKrool
    Offline

    KingKrool Privileged Users

    You really don't know what you are talking about, do you? Do you even know why there is a need (ever) for CPU affinity? Sorry for being argumentative, but multicore systems happen to be my bread and butter...

    I am willing to accept that the Operating system can handle these issues, but not that the processors can (the current lot can't at any rate).

    Certainly, perhaps with the newer drivers you can get around the bugs that sometimes occur on running some single threaded programs on multicore computers (Thief 3 comes to mind..), but there are many other issues involved. I do think however, that setting an affinity is at least conceptually a bad idea, but at least with Linux, it may some times be required (I haven't performed the same tests on Windows)
  15. ..:: Free Radical ::..
    Offline

    ..:: Free Radical ::.. Privileged Users

    I do understand every word what I am talking about, and frankly can churn out a better automated script to do this on a per PID basis on Windows.

    You can do on the fly affinity settings as soon as a particular application class/process is discovered, but you'd need a script in the background to run continuously to achieve that.

    I am on Windows. Some of you guys think you are the Crème de la Crème, with the right to preassume right.

    btw, i don't need to show my credentials to assert that I am smarter (or pathetic:p).

    On Topic:

    Affinity setting may or may not be beneficial for those on quaddies or greater but its absolutely supefluous if you do it on a dual core with current drivers for windows.

    Sorry for being argumentative:tongue:
  16. KingKrool
    Offline

    KingKrool Privileged Users

    OK. Let me start explaining stuff to you, and you tell me how new drivers and/or processors will help/not help, OK?

    1. Power management. CPU affinity might be useful here, but good drivers and OS should combine to fix that problem. I never said CPU affinity would be useful here.

    2. Some apps don't work on dual core (as I said, Thief 3 flickers like nuts). Driver fixes should fix that. Once again. No argument.

    3. Perf. I don't know your familiarity with cache affinity, because you seem to ignore that entirely. There is an entire class of cache affine scheduling algorithms. Just about every OS scheduler tries to schedule the same thread on the same CPU all the time. Guess what. It doesn't work. And you can see that experimentally on any number of benchmarks. Cache aware scheduling is an inexact science at best (because of the various parameters you have to balance).

    So tell me, how can a dual core with current drivers get around the misses on the L1 cache if a thread switches cores(regardless of whether you have a shared L2 or not)? Drivers don't even come into play at this level, and the only thing the OS can do is to try avoiding switching CPUs on the thread (this is especially critical with non-shared L2 caches on some dual cores, though those are now rare). Not only are these conflict misses, but they result in extra coherence traffic too. Hmmm.

    I contend that CPU scheduling considering the parameters that modern OSes do is NP-hard (it is known to be), and that setting the cache-affinity (while conceptually ugly) IS required under certain circumstances no matter which multicore processor you are using.

    As for Linux vs Windows, I've done both, so all your "Crème de la Crème" seems to go out of the window (no pun intended).

    The fact that you said "threading instructions" is a strong indication you are repeating stuff you heard, without any analysis. Which instructions are these that you speak of? I am quite comfortable with x86 and x86-64 ISAs, neither of them has had recent "threading instruction" enhancements of note. Care to share?
  17. ..:: Free Radical ::..
    Offline

    ..:: Free Radical ::.. Privileged Users

    ^ this thread is supposed to be for manually setting process affinities

    I know of the shortcomings of the current gen proccies.

    I am not taking of mismanaged L1.

    If you know of a better way to manage cache., tell Intel about it. That's why intel hasn't been trying to rush into implementing hyperthreading due to limits set by their current cache management.:rofl:

    With ia-32 smps, you can never rule out L2

    Hence the need for ever more shared L2 cache sizes in the c2ds and c2qs.

    Scheduling code in c2ds have improved but cache algorithms have not.

    Don't you think its best to leave process affinity alone if you are doing it blindly, not knowing whether the process threads are allocated the cache accordingly? It only worsens things up, especially if you have a shared cache, wherein the threads would only displace each other from the cache and repopulate it decreasing performance.

    :no:

    I never said that newer drivers are optimal for cache management.

    The /USEPMTIMER switch was a workaround for the os to use the hrt instead of the rtc (its a given for vista) for the timestamp counter bug.

    btw, dont preassume that cpu design is my bread and butter or my prerogative to post in this thread.

    I merely wanted to say that you'll get no performance boost with manually setting process affinities and with the current batch of processors you better leave things as is, unless of course you are running a buggy program/legacy program which needs to run on a single core.
  18. KingKrool
    Offline

    KingKrool Privileged Users

    OK, now that threading instructions is removed from your post, I am quite happy with the rest of it. :D
    I never said blindly set affinities (as I said, it is conceptually ugly, and I prefer conceptual beauty to actual perf :rofl: )- I said it is necessary in some cases. Obviously, all situations do not require that. There are some high perf cases where you want to run on a single cpu - trust me, if you have a DB doing alpha sort, you MUST set the affinity to get perf - you really can't take the chance that your OS does something weird.
    I am not sure that process affinity is different from threads - the terms can be used interchangeably, though obviously a multithreaded app is screwed if all of its threads are assigned to the same core (in that it won't get any perf scaling). I concede the point that if you can only set process (and not thread) affinities, a lot of this discussion is moot. Though of course if you are running Apache, since each server thread is actually a process unto itself, I think that might actually not matter so much (at least that is how they do it in Linux). Also most OSes do not really differentiate between processes and threads... this is all very confusing.:S
    Yes, Vista uses the HRT, and so does Linux (2.6 at least). I'm guessing that Win 2k3 uses the HRT by default, but I'm be sure.
    Actually, increasing shared L2 cache sizes is bad beyond a certain limit, we have discussed this before in other threads... but since it doesn't show up everywhere and isn't really common knowledge, Intel and friends are ignoring it for now. A year from now, they won't.
  19. ..:: Free Radical ::..
    Offline

    ..:: Free Radical ::.. Privileged Users

    [​IMG]

    :eek:hyeah: eggjactly. Moral of the story? :bleh:
  20. oHPaQue
    Offline

    oHPaQue New Member

    Hope someone finds this useful... I posted this elsewhere on the web...

    This is HEX mask and is read from right to left:

    i.e.
    CPU3 CPU2 CPU1 CPU0 Bin Hex
    ================================
    OFF OFF OFF ON = 0001 = 1
    OFF OFF ON OFF = 0010 = 2
    OFF OFF ON ON = 0011 = 3
    OFF ON OFF OFF = 0100 = 4
    OFF ON OFF ON = 0101 = 5 confirmed via task manager
    OFF ON ON OFF = 0110 = 6
    OFF ON ON ON = 0111 = 7
    ON OFF OFF OFF = 1000 = 8
    ON OFF OFF ON = 1001 = 9
    ON OFF ON OFF = 1010 = A confirmed via task manager
    ON OFF ON ON = 1011 = B
    ON ON OFF OFF = 1100 = C
    ON ON OFF ON = 1101 = D
    ON ON ON OFF = 1110 = E confirmed via task manager
    ON ON ON ON = 1111 = F

    Of course, the CPU on task mananager is listed left to right, so now you have to flip all of the above.

    HEX 30 = Binary 110000 = CPU5 CPU4, but I ONLY have one quad core CPU, so I cannot confirm this works or not. :)

    Then create a .BAT file that contains this command to start the app on CPU1 and CPU3:
    REM MY BATCH FILE
    START/AFFINITY A .\"some executable name.exe"

    Where .\ denotes the current directory, and if there are any spaces between the names, put the whole thing in "".

    If you need to add /D and full path:
    REM MY BATCH FILE
    START/AFFINITY A /D C:\"Program Files"\"some app folder" C:\"Program Files"\"some app folder"\"some app.exe"

    Notice that each folder names are encased in "" betwen the \'s. From my experience I found this following does not work:
    START/AFFINITY A /D "C:\Program Files\some app folder" "C:\Program Files\some app folder\some app.exe"

    You can create a shortcut to the batch file, and right click and select "Run As Administrator" as needed. It's too bad the START command doesn't offer this option...

    Now script/batch till you drop! :)
Similar Threads
Forum Title Date
Guides and Tutorials Guide: Building a 100 inch LCD Projector (DIY-Self Made - Updated with Pics & Video) Dec 7, 2008
Guides and Tutorials OverClocking Made Easy !!!!!!!!!! Jan 21, 2007

Share This Page