Title : Cross-platform CpuCount()
Author : Brazilian Joe
Posted : 1+ years ago
Description : USAGE:
Save both code snippets as cpucount.macos.c and cpucount.bmx, respectively, in your project folder.
Add the line:
Import "cpucount.bmx"
Ans you will have the function CpuCount() available, which returns an integer number (number of cpus).
EDITED THE CODE TO PROVIDE THE BUGFIX DISCUSSED BELOW. [/i]
Code : ----------DONT SAVE THIS LINE. save the contents as cpucount.macos.c----------
#include <stdio.h>
#include <sys/param.h>
#include <sys/sysctl.h>
int CpuCount () {
int mib[2];
size_t len;
int cpun = 1;
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
len = sizeof(cpun);
if (sysctl(mib, 2, &cpun, &len, NULL, NULL == -1)) {
cpun=1;
}
return cpun;
}
----------EOF cpucount.macos.c----------
----------DONT SAVE THIS LINE. save the contents as cpucount.bmx------------
?macos
Import "cpucount.macos.c"
?
?win32
Type SYSTEM_INFO
Field wProcessorArchitecture:Short
Field wReserved:Short
Field dwPageSize:Int
Field lpMinimumApplicationAddress:Byte Ptr
Field lpMaximumApplicationAddress:Byte Ptr
Field dwActiveProcessorMask:Int
Field dwNumberOfProcessors:Int
Field dwProcessorType:Int
Field dwAllocationGranularity:Int
Field wProcessorLevel:Short
Field wProcessorRevision:Short
End Type
Extern "win32"
Function GetSystemInfo (si:Byte Ptr)
End Extern
Function CpuCount:Int()
Local info:SYSTEM_INFO=New SYSTEM_INFO
GetSystemInfo(info)
Return info.dwNumberOfProcessors
End Function
?linux
Function CpuCount:Int()
Local file:TStream=ReadFile("/sys/devices/system/cpu/present")
If Not file RuntimeError "could not open file openfile.bmx"
Local cpus_str$ = ReadLine(file)
Local cpus_a$[] = cpus_str.Split("-")
If cpus_a.length = 1 Then
cpus_n = 1
Else
Local cpus_n:Int = Int(cpus_a[1])+1
End If
Return cpus_n
End Function
?macos
Extern
Function CpuCount()
End Extern
?
'uncomment this one to test functionality standalone
'Print CpuCount()
------------EOF cpucount.bmx----------
Comments :
ImaginaryHuman(Posted 1+ years ago)
This is cool buh how about also Linux support to make it truly cross-platform?
jkrankie(Posted 1+ years ago)
it does support linux!CheersCharlie
degac(Posted 1+ years ago)
To add to standard BRL modules!
SebHoll(Posted 1+ years ago)
The second occurence of this line in Linux's CPUCount() implementation, shouldn't be there, right?Local cpus_n:Int = Int(cpus_a[1]) + 1
markcw(Posted 1+ years ago)
Yeah it gives a duplicate identifier compile error. Fixed and tested on a dual-core cpu with ubuntu.
' ID: 2526
' Author: Brazilian Joe
' Date: 2009-07-11 12:06:16
' Title: Cross-platform CpuCount()
' Description: Adding the Function CpuCount which returns the, er, number of cpus on the system. Useful For multi-threading.
Rem
----------DONT SAVE THIS LINE. save the contents as cpucount.macos.c----------
#Include <stdio.h>
#Include <sys/param.h>
#Include <sys/sysctl.h>
Int CpuCount () {
Int mib[2];
size_t Len;
Int cpun = 1;
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
Len = SizeOf(cpun);
If (sysctl(mib, 2, &cpun, &Len, Null, Null == -1)) {
cpun=1;
}
Return cpun;
}
----------Eof cpucount.macos.c----------
----------DONT SAVE THIS LINE. save the contents as cpucount.bmx------------
EndRem
'SuperStrict
?macos
Import "cpucount.macos.c"
?
?win32
Type SYSTEM_INFO
Field wProcessorArchitecture:Short
Field wReserved:Short
Field dwPageSize:Int
Field lpMinimumApplicationAddress:Byte Ptr
Field lpMaximumApplicationAddress:Byte Ptr
Field dwActiveProcessorMask:Int
Field dwNumberOfProcessors:Int
Field dwProcessorType:Int
Field dwAllocationGranularity:Int
Field wProcessorLevel:Short
Field wProcessorRevision:Short
End Type
Extern "win32"
Function GetSystemInfo (si:Byte Ptr)
End Extern
Function CpuCount:Int()
Local info:SYSTEM_INFO=New SYSTEM_INFO
GetSystemInfo(info)
Return info.dwNumberOfProcessors
End Function
?linux
Function CpuCount:Int()
Local file:TStream=ReadFile("/sys/devices/system/cpu/present")
If Not file RuntimeError "could not open file openfile.bmx"
Local cpus_str$ = ReadLine(file)
Local cpus_a$[] = cpus_str.Split("-")
Local cpus_n:Int = 1
If cpus_a.length = 1 Then
cpus_n = 1
Else
cpus_n = Int(cpus_a[1])+1
End If
Return cpus_n
End Function
?macos
Extern
Function CpuCount()
End Extern
?
'uncomment this one to test functionality standalone
'Print CpuCount()
'------------Eof cpucount.bmx----------
beanage(Posted 1+ years ago)
Thank you -
Brazilian Joe(Posted 1+ years ago)
Edited the top post to implement the fix. I forgot to remove that line after putting it inside the IF

[/i]