On Sat, 29 Nov 2008 19:14:39 +0100 (CET)
mar...@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
{ System depending code for light weight threads.

  This file is part of the Free Pascal run time library.

  Copyright (C) 2008 Mattias Gaertner matt...@freepascal.org

  See the file COPYING.FPC, included in this distribution,
  for details about the copyright.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit MTPCPU;

{$mode objfpc}{$H+}

interface

{$IF defined(windows)}
uses Windows;
{$ELSEIF defined(freebsd) or defined(darwin)}
uses ctypes, sysctl;
{$ELSEIF defined(linux)}
{$linklib c}
uses ctypes;
{$ENDIF}

function GetSystemThreadCount: integer;

implementation

{$IFDEF Linux}
const _SC_NPROCESSORS_ONLN = 83;
function sysconf(i: cint): clong; cdecl; external name 'sysconf';
{$ENDIF}

function GetSystemThreadCount: integer;
// returns a good default for the number of threads on this system
{$IF defined(windows)}
//returns total number of processors available to system including logical hyperthreaded processors
var
  i: Integer;
  ProcessAffinityMask, SystemAffinityMask: DWORD;
  Mask: DWORD;
  SystemInfo: SYSTEM_INFO;
begin
  if GetProcessAffinityMask(GetCurrentProcess, ProcessAffinityMask, SystemAffinityMask)
  then begin
    Result := 0;
    for i := 0 to 31 do begin
      Mask := 1 shl i;
      if (ProcessAffinityMask and Mask)<>0 then
        inc(Result);
    end;
  end else begin
    //can't get the affinity mask so we just report the total number of processors
    GetSystemInfo(SystemInfo);
    Result := SystemInfo.dwNumberOfProcessors;
  end;
end;
{$ELSEIF defined(UNTESTEDsolaris)}
  begin
    t = sysconf(_SC_NPROC_ONLN);
  end;
{$ELSEIF defined(freebsd) or defined(darwin)}
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);
  Result:=t;
end;
{$ELSEIF defined(linux)}
  begin
    Result:=sysconf(_SC_NPROCESSORS_ONLN);
  end;
{$ELSE}
  begin
    Result:=1;
  end;
{$ENDIF}

end.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to