Recent

Author Topic: Auto-detect number of CPUs  (Read 13776 times)

crorden

  • New Member
  • *
  • Posts: 36
Auto-detect number of CPUs
« on: August 04, 2007, 11:57:27 pm »
Hello-

First of all, thanks Tombo for the rapid progress on the Carbon widgetset. This is really first rate work.

I have written an open source program for brain imaging. The statistics use multiple threads to greatly accelerate the analysis:
 http://www.sph.sc.edu/comd/rorden/npm/

I would like to automatically detect the number of CPUs available to optimize performance. Does anyone know how to do this with MacOSX or Linux? The Windows-specific solution is shown below:

function GetLogicalCpuCount: Integer;
var
  SystemInfo: _SYSTEM_INFO;
begin
  GetSystemInfo(SystemInfo);
  Result := SystemInfo.dwNumberOfProcessors;
end;

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
RE: Auto-detect number of CPUs
« Reply #1 on: August 05, 2007, 11:59:05 am »
In linux, the simplest way is to read /proc/cpuinfo. Each CPU is listed separated by empty line, so if you count empty lines you get #CPU - 1 result.

In Mac OS X you can use the sysctl unit from RTL and perform a sysctl. The value is in "hw.ncpu"

crorden

  • New Member
  • *
  • Posts: 36
Auto-detect
« Reply #2 on: August 05, 2007, 07:56:20 pm »
Hello-

I can run 'sysctl -a' from the command line and see the correct hw.ncpu, but I am not sure how to do this from the Lazarus compiler. Can you give me a Pascal example of reading this value? Thanks very much. I searched Google and the solution is not obvious.

Thanks

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
RE: Auto-detect
« Reply #3 on: August 05, 2007, 08:49:05 pm »

crorden

  • New Member
  • *
  • Posts: 36
Auto-detect number of CPUs
« Reply #4 on: August 06, 2007, 10:00:30 pm »
OK, Thanks for your help. For futrure users, here is my solution...

function CPUcount: integer;
//returns number of CPUs for MacOSX computer
//example - will return 4 if the computer has two dual core CPUs
//requires Process in Uses Clause
//see http://wiki.lazarus.freepascal.org/Executing_External_Programs
var
   lProcess: TProcess;
   lLen,lPos: integer;
   lStr: string;
   lStringList: TStringList;
 begin
   Result := 1;
   lProcess := TProcess.Create(nil);
   lStringList := TStringList.Create;
   lProcess.CommandLine := 'sysctl hw.ncpu';
   lProcess.Options := lProcess.Options + [poWaitOnExit, poUsePipes];
   lProcess.Execute;
   lStringList.LoadFromStream(lProcess.Output);
   lLen := length(lStringList.Text);
   if lLen > 0 then begin
      lStr := '';
      for lPos := 1 to lLen do
             if lStringList.Text[lPos] in ['0'..'9'] then
                lStr := lStr + lStringList.Text[lPos];
      if length(lStr) > 0 then
         result := strtoint(lStr);
   end;//if at least one character returned
   lStringList.Free;
   lProcess.Free;
end;

Phil

  • Hero Member
  • *****
  • Posts: 2737
Auto-detect number of CPUs
« Reply #5 on: August 06, 2007, 11:22:21 pm »
Quote from: "crorden"
OK, Thanks for your help. For futrure users, here is my solution...



With 10.4 (Tiger) I believe you can use the Carbon gestalt function with the gestaltCountOfCPUs selector to retrieve this information.

Thanks.

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
Auto-detect number of CPUs
« Reply #6 on: August 07, 2007, 10:36:22 am »
uses
  SysCtl;

fpSysctl() ??

I think that's a pretty clean solution...

 

TinyPortal © 2005-2018