November 28, 2020, 02:11:33 PM

Author Topic: [bmx] Cross-platform CpuCount() by Brazilian Joe [ 1+ years ago ]  (Read 952 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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:
Code: [Select]
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 :
Code: BlitzMax
  1. ----------DONT SAVE THIS LINE. save the contents as cpucount.macos.c----------
  2. #include <stdio.h>
  3. #include <sys/param.h>
  4. #include <sys/sysctl.h>
  5.  
  6. int CpuCount () {
  7.         int mib[2];
  8.         size_t len;
  9.         int cpun = 1;
  10.         mib[0] = CTL_HW;
  11.         mib[1] = HW_NCPU;
  12.         len = sizeof(cpun);
  13.         if (sysctl(mib, 2, &cpun, &len, NULL, NULL == -1)) {
  14.                 cpun=1;
  15.         }
  16.     return cpun;
  17. }
  18. ----------EOF cpucount.macos.c----------
  19. ----------DONT SAVE THIS LINE. save the contents as cpucount.bmx------------
  20. ?macos
  21. Import "cpucount.macos.c"
  22. ?
  23. ?win32
  24. Type SYSTEM_INFO
  25.         Field wProcessorArchitecture:Short
  26.         Field wReserved:Short
  27.         Field dwPageSize:Int
  28.         Field lpMinimumApplicationAddress:Byte Ptr
  29.         Field lpMaximumApplicationAddress:Byte Ptr
  30.         Field dwActiveProcessorMask:Int
  31.         Field dwNumberOfProcessors:Int
  32.         Field dwProcessorType:Int
  33.         Field dwAllocationGranularity:Int
  34.         Field wProcessorLevel:Short
  35.         Field wProcessorRevision:Short
  36. End Type
  37.  
  38. Extern "win32"
  39.         Function GetSystemInfo (si:Byte Ptr)
  40. End Extern
  41.  
  42. Function CpuCount:Int()
  43.         Local info:SYSTEM_INFO=New SYSTEM_INFO
  44.         GetSystemInfo(info)
  45.         Return info.dwNumberOfProcessors
  46. End Function
  47. ?linux
  48. Function CpuCount:Int()
  49.         Local file:TStream=ReadFile("/sys/devices/system/cpu/present")
  50.         If Not file RuntimeError "could not open file openfile.bmx"
  51.         Local cpus_str$ =  ReadLine(file)
  52.         Local cpus_a$[] = cpus_str.Split("-")
  53.         If cpus_a.length = 1 Then
  54.                 cpus_n = 1
  55.         Else
  56.                 Local cpus_n:Int = Int(cpus_a[1])+1
  57.         End If
  58.         Return cpus_n
  59. End Function
  60. ?macos
  61. Extern
  62.         Function CpuCount()
  63. End Extern
  64. ?
  65. 'uncomment this one to test functionality standalone
  66. 'Print CpuCount()
  67. ------------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?
Code: [Select]
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.
Code: [Select]
' 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]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal