Another approach which works under windows, to count cpu cores, would be:

wincpucores=: {{
  assert. 0={.;'kernel32 GetNativeSystemInfo n i' 15!:0 ,<mem=: mema 44
  (memf mem)]_2 ic _4{.memr mem,32 4
}}

Example use:

   wincpucores''
4

It's different, though, under OSX and also different under Linux. And,
given how coy the docs are about the value of _SC_NPROCESSORS_ONLN, I
suspect it might be different on different builds of Linux.  Still,
for linux, I guess you could use +/'processor'E.2!:0'cat
/proc/cpuinfo'

(And, I don't have a working OSX machine currently -- mine died, and
was declared unsalvageable, and I am disinclined to try to replace
it.)

FYI,

-- 
Raul

On Mon, Apr 11, 2022 at 3:59 AM 'Michael Day' via Programming
<programm...@jsoftware.com> wrote:
>
> The mechanism here is presumably common knowledge,  but might be worth
> sharing.
> Here's what I use to allocate half of the laptop's cores for use by J:
>
> NB. Processor Affinity
> coclass 'affin'
> require'dll'
>
> GetCurrentProcess=: >@{.@('kernel32 GetCurrentProcess i'&(15!:0))
> GetProcessAffinityMask=: ('kernel32 GetProcessAffinityMask i i * *'&(15!:0))
> SetProcessAffinityMask=: ('kernel32 SetProcessAffinityMask i i i'&(15!:0))
>
> setproc =: 3 : 0
> 'rc tid pam sam'=.GetProcessAffinityMask (GetCurrentProcess'');(,2-2);(,2-2)
> ppr=. ,I. (8#2)#: sam
> NB. choose random half of cores
> cores =. ((?~ <.@-: ) # ppr )
> SetProcessAffinityMask tid;0{,(8#2)#. 1 cores }8#0
> )
>
> So the number of cores is
>     # #: sam
> and you only need GetProcessAffinityMask and GetCurrentProcess for that
> information.
>
> Cheers,
>
> Mike
>
> On 10/04/2022 23:49, Raul Miller wrote:
> > There's currently no rush for deciding this kind of thing.
> >
> > We need some experience with this mechanism, first. And, before that,
> > we need to support Henry's efforts to sort out the rough edges.
> >
> > But counting cpu cores is a relatively simple task, if slightly
> > obscure. So I thought I should leave a note here about that.
> >
> > Thanks,
> >
> > -- Raul On Sun, Apr 10, 2022 at 6:34 PM Hauke Rehr
> > <hauke.r...@uni-jena.de> wrote:
> >> why not have a command line argument providing a default number?
> >> $USER may know better what they consider a good default
> >>
> >> Am 11.04.22 um 00:31 schrieb Raul Miller:
> >>> It would be nice, I imagine, if we eventually had a mechanism like
> >>>
> >>> https://www.cprogramming.com/snippets/source-code/find-the-number-of-cpu-cores-for-windows-mac-or-linux
> >>>
> >>> to set up a default number of threads.
> >>>
> >> --
> >> ----------------------
> >> mail written using NEO
> >> neo-layout.org
> >> ----------------------------------------------------------------------
> >> For information about J forums seehttp://www.jsoftware.com/forums.htm
> > ----------------------------------------------------------------------
> > For information about J forums seehttp://www.jsoftware.com/forums.htm
>
>
> --
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to