Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-09

detect CPU cores

13 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

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

Reply by email

Dmitry Boyarintsev
to Xiangrong Fang

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

Xiangrong Fang wrote 14 lines
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 _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Xiangrong Fang
to Dmitry Boyarintsev

I found this code on the net:

http://code.google.com/p/fpos/source/browse/kernel/cpuid.pas?r=c387b381d7a05f9328693cdcf59b0b4f633294e4

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 :

Show quoted text 30 lines
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 On Fri, Sep 12, 2014 at 10:07 AM, Xiangrong Fang wrote: > 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 > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Sven Barth
to Xiangrong Fang: Is there a platform independent (specifically Windows and Linux) way to detect cores and…
"Xiangrong Fang" wrote
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.

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

Reply by email

Tomas Hajny
to Xiangrong Fang: I found this code on the net: http://code.google.com/p/fpos/source/browse/kernel/cpuid.pas…
Xiangrong Fang wrote 13 lines
I found this code on the net: http://code.google.com/p/fpos/source/browse/kernel/cpuid.pas?r=c387b381d7a05f9328693cdcf59b0b4f633294e4 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.

.
.

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

Reply by email

Marco van de Voort
to Sven Barth: 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.

Reply by email

Dmitry Boyarintsev
to Xiangrong Fang: 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

Xiangrong Fang wrote 64 lines
I found this code on the net: http://code.google.com/p/fpos/source/browse/kernel/cpuid.pas?r=c387b381d7a05f9328693cdcf59b0b4f633294e4 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 : > 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 > > On Fri, Sep 12, 2014 at 10:07 AM, Xiangrong Fang > wrote: > >> 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 >> >> >> _______________________________________________ >> fpc-pascal maillist - >> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal >> > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Sven Barth
to Marco van de Voort: In our previous episode, Sven Barth said: I ran it to test if it made a difference…
"Marco van de Voort" wrote
In our previous episode, Sven Barth said: > FPC 2.7.1 has TThread.ProcessorCount (it's a class property, so no

instance

Show quoted text 5 lines
> 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.
Considering that the implementation is currently "Result := 1" for any
target that is no surprise ;)

Regards,
Sven

Reply by email

Sven Barth
to Sven Barth
Sven Barth wrote 15 lines
Am 12.09.2014 17:31 schrieb "Marco van de Voort" < >: > > 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. Considering that the implementation is currently "Result := 1" for any target that is no surprise ;)

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

Reply by email

Marco van de Voort
to Sven Barth

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!

Reply by email

Sven Barth
to Marco van de Voort
Marco van de Voort wrote 12 lines
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?

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

Reply by email

Xiangrong Fang
to Dmitry Boyarintsev: 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?

Reply by email

Dmitry Boyarintsev
to Xiangrong Fang

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

Xiangrong Fang wrote 20 lines
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? _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email