[fpc-pascal] number of cpu cores

Robert Wolfe wolfe.robwolfe at gmail.com
Sat Dec 13 18:41:12 CET 2008


On 12/13/2008 11:17 AM, Mattias Gaertner wrote:
> On Sat, 29 Nov 2008 19:14:39 +0100 (CET)
> marcov at stack.nl (Marco van de Voort) wrote:
>
>    
>> In our previous episode, Michalis Kamburelis said:
>> [ Charset ISO-8859-1 unsupported, converting... ]
>>      
>>> Florian Klaempfl wrote:
>>>        
>>>> Mattias Gaertner schrieb:
>>>>          
>>>>> I need a default value of the number of maximum threads.
>>>>> Is there already some RTL function that can be used for that?
>>>>>
>>>>> For example if the program runs on a 2 x quad core it would be
>>>>> nice to get 8.
>>>>> Hyperthreading multipliers should be applied.
>>>>>            
>>>> No, but having some functions returning information about the
>>>> CPUs would be nice.
>>>>          
>>> Blender (http://www.blender.org/) source code has function to do
>>> that (they needed this to set the default number of threads for
>>> renderer). So you could try converting to Pascal the code of
>>> "BLI_system_thread_count" function from:
>>>
>>> https://svn.blender.org/svnroot/bf-blender/trunk/blender/source/blender/blenlib/intern/threads.c
>>>
>>> Doesn't seem that complicated on 1st look. It's basically
>>> appropriate GetSystemInfo call on Windows, sysctl on Mac OS, and
>>> sysconf (from Libc) call on most Unixes.
>>>        
>> Sysconf is not standarized yet. Unit libc is linux/x86 only.
>>
>> On freebsd, the sysctl modeled works, but note the typecast.
>> (probably a K&R void*<->  char* problem)
>>
>> uses ctypes,sysctl;
>>
>> var mib : array[0..1] of cint;
>>     len:cint;
>>      t :cint;
>> begin
>> 	mib[0] := CTL_HW;
>>          mib[1] := HW_NCPU;
>>          len := sizeof(t);
>>          fpsysctl(pchar(@mib), 2, @t, @len, Nil, 0);
>> 	writeln(t); // prints 2 on our dual CPU (Athlon MPs)
>> end.
>>      
>
> Thanks for all the hints.
>
> At the moment I have the attached function.
> It returns only 4 on a 2 x quad core Mac.
> Maybe someone can test under windows?
>
> Mattias
>    
> ------------------------------------------------------------------------
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

I tested this out on my Windows 7-based laptop (running the 64 bit 
edition) and this is what I get:

E:\lazarus>ppcx64 test
Free Pascal Compiler version 2.2.2 [2008/10/05] for x86_64
Copyright (c) 1993-2008 by Florian Klaempfl
Target OS: Win64 for x64
Compiling test.pas
Linking test.exe
7 lines compiled, 0.6 sec , 39984 bytes code, 1648 bytes data

E:\lazarus>test

2 CPUs online.



More information about the fpc-pascal mailing list