Hi All,
Is there a platform independent (specifically Windows and Linux) way to detect cores and hyper-threads of the CPU? I am writing a calculation intensive app and would like to fully utilize SMP capability of the CPU.
Thanks!
Xiangrong

Mailing lists / fpc-pascal / 2014-09
13 messages · 5 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi All,
Is there a platform independent (specifically Windows and Linux) way to detect cores and hyper-threads of the CPU? I am writing a calculation intensive app and would like to fully utilize SMP capability of the CPU.
Thanks!
Xiangrong
If you're talking bout x88_64, i386 platforms, you should be able to do it using CPUID instruction
http://en.wikipedia.org/wiki/CPUID
thanks,
Dmitry
I found this code on the net:
It's part of the "FreePascal Operationg System" project. I suppose it is compatible with FreePascal of course. But while compiled, I got lots of errors, such as:
cpuid.pas(28,3) Error: Unrecognized opcode pushfd
cpuid.pas(29,10) Error: Unknown identifier "EAX"
cpuid.pas(30,13) Error: Unknown identifier "EDX"Even I added {$mode objfpc}{$H+} does not help.
I tried to compile this code on this platform:
CPU: i3 M 390
OS: Ubuntu x64
FPC: 2.6.4
How to use assembly in fpc anyway?
Thanks.
2014-09-12 22:28 GMT+08:00 Dmitry Boyarintsev :
Is there a platform independent (specifically Windows and Linux) way to detect cores and…
"Xiangrong Fang" wroteHi All, Is there a platform independent (specifically Windows and Linux) way to
detect cores and hyper-threads of the CPU? I am writing a calculation intensive app and would like to fully utilize SMP capability of the CPU.
FPC 2.7.1 has TThread.ProcessorCount (it's a class property, so no instance needed) or System.CpuCount (ProcessorCount uses this). It's not yet implemented though for any system :/
Regards,
Sven
I found this code on the net: http://code.google.com/p/fpos/source/browse/kernel/cpuid.pas…
.
.
This code uses Intel syntax of assembler routines. You need to add
{$ASMMODE INTEL} before the assembler block, or use one of compilation
modes using this assembler mode by default (such a {$MODE DELPHI} or
{$MODE TP}).Tomas
detect cores and hyper-threads of the CPU? I am writing a calculation intensive app and…
In our previous episode, Sven Barth said:
FPC 2.7.1 has TThread.ProcessorCount (it's a class property, so no instance needed) or System.CpuCount (ProcessorCount uses this). It's not yet implemented though for any system :/
I ran it to test if it made a difference between cores and hyperthreading, but didn't even get that far, on Windows it seems to always return 1.
I found this code on the net: http://code.google.com/p/fpos/source/browse/kernel/cpuid.pas…
You're compiling for x64, You need to replace "eax" and "edx" with "rax" and "rdx", since e?x are available for i386 only.
thanks,
Dmitry
In our previous episode, Sven Barth said: I ran it to test if it made a difference…
"Marco van de Voort" wroteIn our previous episode, Sven Barth said: > FPC 2.7.1 has TThread.ProcessorCount (it's a class property, so no
instance
Considering that the implementation is currently "Result := 1" for any
target that is no surprise ;)Regards,
Sven
I've now implemented GetCPUCount for all Windows targets. For the BSD's I'd use sysctl, which would mean that I'd need to duplicate sysctl() and the necessary constants/types...
Regards,
Sven
In our previous episode, Sven Barth said:
> Considering that the implementation is currently "Result := 1" for any > target that is no surprise ;) I've now implemented GetCPUCount for all Windows targets.
.. updating...
For the BSD's I'd use sysctl, which would mean that I'd need to duplicate sysctl()
Why not simply use fpsysctl in unit sysctl?
and the necessary constants/types...
Yes. The FreeBSD ones are probably 5.x. Bonus points if you can haul those constants through h2pas!
Because GetCPUCount is in System unit. So I'll need to duplicate the fpsysctl only to implement that function (nothing else in System requires sysctl yet) :(
Regards,
Sven
You're compiling for x64, You need to replace "eax" and "edx" with "rax" and "rdx", since…
2014-09-13 0:28 GMT+08:00 Dmitry Boyarintsev :
You're compiling for x64, You need to replace "eax" and "edx" with "rax" and "rdx", since e?x are available for i386 only.
My situation is, I program and test on Ubuntu x64 on a i3-M390. Then compile it to Windows 32bit on a VirtualBox on this machine, then run it on a Windows 32bit running on a rather old pentium dual core (T-something, I don't know if it is 32bit or 64bit processor).
regarding e?x vs. r?x, is it only related to the CPU, or also has something to do with the OS (32bit or 64bit)? How to do "conditional compiling" for assembly code?
Please disregard my previous statement abour renaming register names. I was completely wrong. The problem most likely was with a wrong asm mode. Try to specify the asm mode explicitly by adding
{$ASMMODE INTEL}
at the begging of the unit.
You might try an alternative version here: http://pastebin.com/ujyBLjYZ
Register names and more important ABIs are CPU dependent 32 vs 64 bit. Has little to do with OS, though knowledgable apporach is necessary.
If you need to have the code working for both 32 and 64, you might need to use compiler directive $IFDEF.with the CPU target you need.
The good news, if you compile win32 bit application it will run just fine on win64 and will provide you with the most accurate results possible.So you can use the existing 32-bit code.
IRC, some extra efforts would be needed to run 32-bit binary on 64bit Linux.
thanks,
Dmitry