Improving OTTD Performance on a multicore processor
Posted: 16 Oct 2009 15:09
Hi all,
I've seen so many times on these forums people asking for multi-core support or complaining of performance drops when using some of the more CPU intensive patches, well, as discussed elsewhere, and for good reason, multi-core support is not coming any time soon, if at all
In the meantime however there is a very useful method for getting the most out of OTTD and your multi-core CPU, for me (it may not apply in all cases) I've seen better performance running a single dedicated core for OTTD than allowing it to occupy the Primary Core with all the other windows processes.
You will need a program called imagecfg.exe , which can be found as part of the Windows 2000 or Windows XP Server Development Kit, or here:
Clicky 1
EDIT: ???ABOVE DEAD LINK??? --- NEW ONE ---> Clicky 2
Now, once decompressed from the ZIP file, you need to place the imagecfg.exe file in your windows\system32\ , windows\system32\dllcache\ and windows\system\ folders
Back-up your OpenTTD.exe as you will modify this with ImageCFG
What we are going to do is permanently set OpenTTD to use one core of your multicore processor, this is also known as setting the processor affinity, for performance it's best to use the penultimate core, or 2nd core in dual-core cpu's...
So for example in a dual core CPU you would set the second CPU (CPU ID 1), in a 3-core you would set the 2nd CPU (CPU ID 1), in a 4-core you would set the third CPU (CPU ID 2).
The reason for setting the penultimate core is that, in some multicore cpu's, empty cycles are passed down through the cores so the last core will perform slightly worse than the ones that precede it. The reason for not using the primary core is that windows and most processes and apps will default to this one (though newer versions of the windows kernel are improving how work is distributed between cores). So the penultimate core is generally the best to use as it will have the lowest load and won't suffer as much as the last core if free cycles are passed down
OK, let's begin,
Open notepad and create the following text:
Note that the second, non "rem'd", 0xn must appear with a number not an "n"
"n" is the core number to use; with a 4-core system this is what you would have to choose from
1 = CPU0 (Primary Core)
2 = CPU1 (Secondary Core)
4 = CPU2 (Third Core)
8 = CPU3 (Fourth Core)
So as we want to use the third core you need to change the above code in the notepad file to this:
Note that the second 0xn is now 0x4
Now save the notepad file as a batch file, for example
"openttdcoreset.bat" (Without the "s)
make sure you save it ending .bat
NOT
.bat.txt
(You may have to set "Save as type:" to "All types (*.*)" to get it to save as just a .bat file)
Once you have saved once, copy the .bat file to your OpenTTD core directory, where OpenTTD.exe resides
Open Windows Explorer and navigate to that folder, double click on the openttdcoreset.bat file to run it, you will see it set the processor affinity and pause, once complete you can close the dialogue
You can also use the "Start->Run" method to get the .bat file running too
OpenTTD is now set to use the third core and the third core only resulting in best use of the core, with other cores used to carry the weight of most apps
You need to run the .bat file EVERYTIME you change OpenTTD.exe, this can be updating to the latest version, or compiling a new patch. Fortunately once the .bat file is created you don't have to edit it unless you want to use a different core
comments please
Jacob
2013 Additions:
If you know your HEX then you can also set OTTD to use a range of cores by adding all the "0xn" mask values of each core up and inputting the total as a new "0xn" value for the batch file listed on the thread...
I've not tested this yet... So haven't a clue if it makes any difference to users with 8,12 or 16 core PC's...
For example:
Therefore you would use, to set OTTD, for the first four cores only in an 8-Core system:
= 15 = CPU0, CPU1, CPU2, and CPU3 (1+2+4+8 = 15)
OR MORE SENSIBLY TO USE THE LAST FOUR CORES OF AN 8-CORE SYSTEM
= 96 = CPU4, CPU5, CPU6, and CPU7 (10+20+40+80 = 96)
EDIT1: If this is useful to anyone is it worthy of a sticky? As I'm sick of "Multicore Support Please" type questions
EDIT2: Added an example batch file
EDIT3: April 2013, Amended Dead Link
EDIT4: April 2013, want to spread it accross more than one tertiary core; then you need a scientific calculator for the hex values explained here: Multi-Core Spread Clicky
I've seen so many times on these forums people asking for multi-core support or complaining of performance drops when using some of the more CPU intensive patches, well, as discussed elsewhere, and for good reason, multi-core support is not coming any time soon, if at all
In the meantime however there is a very useful method for getting the most out of OTTD and your multi-core CPU, for me (it may not apply in all cases) I've seen better performance running a single dedicated core for OTTD than allowing it to occupy the Primary Core with all the other windows processes.
You will need a program called imagecfg.exe , which can be found as part of the Windows 2000 or Windows XP Server Development Kit, or here:
Clicky 1
EDIT: ???ABOVE DEAD LINK??? --- NEW ONE ---> Clicky 2
Now, once decompressed from the ZIP file, you need to place the imagecfg.exe file in your windows\system32\ , windows\system32\dllcache\ and windows\system\ folders
Back-up your OpenTTD.exe as you will modify this with ImageCFG
What we are going to do is permanently set OpenTTD to use one core of your multicore processor, this is also known as setting the processor affinity, for performance it's best to use the penultimate core, or 2nd core in dual-core cpu's...
So for example in a dual core CPU you would set the second CPU (CPU ID 1), in a 3-core you would set the 2nd CPU (CPU ID 1), in a 4-core you would set the third CPU (CPU ID 2).
The reason for setting the penultimate core is that, in some multicore cpu's, empty cycles are passed down through the cores so the last core will perform slightly worse than the ones that precede it. The reason for not using the primary core is that windows and most processes and apps will default to this one (though newer versions of the windows kernel are improving how work is distributed between cores). So the penultimate core is generally the best to use as it will have the lowest load and won't suffer as much as the last core if free cycles are passed down
OK, let's begin,
Open notepad and create the following text:
Code: Select all
rem Place ImageCFG.exe in System32 and System32\dllcache
rem CPU number is 0xn , where n is...
rem 1 = CPU0
rem 2 = CPU1
rem 4 = CPU2
rem 8 = CPU3
imagecfg -a 0xn OpenTTD.exe
pause
"n" is the core number to use; with a 4-core system this is what you would have to choose from
1 = CPU0 (Primary Core)
2 = CPU1 (Secondary Core)
4 = CPU2 (Third Core)
8 = CPU3 (Fourth Core)
So as we want to use the third core you need to change the above code in the notepad file to this:
Code: Select all
rem Place ImageCFG.exe in System32 and System32\dllcache
rem CPU number is 0xn , where n is...
rem 1 = CPU0
rem 2 = CPU1
rem 4 = CPU2
rem 8 = CPU3
imagecfg -a 0x4 OpenTTD.exe
pause
Now save the notepad file as a batch file, for example
"openttdcoreset.bat" (Without the "s)
make sure you save it ending .bat
NOT
.bat.txt
(You may have to set "Save as type:" to "All types (*.*)" to get it to save as just a .bat file)
Once you have saved once, copy the .bat file to your OpenTTD core directory, where OpenTTD.exe resides
Open Windows Explorer and navigate to that folder, double click on the openttdcoreset.bat file to run it, you will see it set the processor affinity and pause, once complete you can close the dialogue
You can also use the "Start->Run" method to get the .bat file running too
OpenTTD is now set to use the third core and the third core only resulting in best use of the core, with other cores used to carry the weight of most apps

You need to run the .bat file EVERYTIME you change OpenTTD.exe, this can be updating to the latest version, or compiling a new patch. Fortunately once the .bat file is created you don't have to edit it unless you want to use a different core
comments please

Jacob
2013 Additions:
If you know your HEX then you can also set OTTD to use a range of cores by adding all the "0xn" mask values of each core up and inputting the total as a new "0xn" value for the batch file listed on the thread...
I've not tested this yet... So haven't a clue if it makes any difference to users with 8,12 or 16 core PC's...
For example:
Code: Select all
0x01 = 1 = CPU0
0x02 = 2 = CPU1
0x04 = 4 = CPU2
0x08 = 8 = CPU3
0x10 = 16 = CPU4
0x20 = 32 = CPU5
0x40 = 64 = CPU6
0x80 = 128 = CPU7
Code: Select all
0x0f
OR MORE SENSIBLY TO USE THE LAST FOUR CORES OF AN 8-CORE SYSTEM
Code: Select all
0x96
EDIT1: If this is useful to anyone is it worthy of a sticky? As I'm sick of "Multicore Support Please" type questions

EDIT2: Added an example batch file
EDIT3: April 2013, Amended Dead Link
EDIT4: April 2013, want to spread it accross more than one tertiary core; then you need a scientific calculator for the hex values explained here: Multi-Core Spread Clicky