caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Cross-platform cpu count
@ 2011-09-02  0:04 Daniel Bünzli
  2011-09-02  0:38 ` malc
  2011-09-02  7:51 ` Richard W.M. Jones
  0 siblings, 2 replies; 11+ messages in thread
From: Daniel Bünzli @ 2011-09-02  0:04 UTC (permalink / raw)
  To: caml-list

Hello,

Can anybody confirm me that the following code works on cygwin :

let cpu_count () =
  try match Sys.os_type with
  | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
  | _ ->
      let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
      let close () = ignore (Unix.close_process_in i) in
      try Scanf.fscanf i "%d" (fun n -> close (); n) with e -> close (); raise e
  with
  | Not_found | Sys_error _ | Failure _ | Scanf.Scan_failure _
  | End_of_file | Unix.Unix_error (_, _, _) -> 1

Thanks,

Daniel

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Cross-platform cpu count
  2011-09-02  0:04 [Caml-list] Cross-platform cpu count Daniel Bünzli
@ 2011-09-02  0:38 ` malc
  2011-09-02  0:47   ` Daniel Bünzli
  2011-09-02  7:39   ` David Allsopp
  2011-09-02  7:51 ` Richard W.M. Jones
  1 sibling, 2 replies; 11+ messages in thread
From: malc @ 2011-09-02  0:38 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list

On Fri, 2 Sep 2011, Daniel B?nzli wrote:

> Hello,
> 
> Can anybody confirm me that the following code works on cygwin :
> 
> let cpu_count () =
>   try match Sys.os_type with
>   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
>   | _ ->
>       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
>       let close () = ignore (Unix.close_process_in i) in
>       try Scanf.fscanf i "%d" (fun n -> close (); n) with e -> close (); raise e
>   with
>   | Not_found | Sys_error _ | Failure _ | Scanf.Scan_failure _
>   | End_of_file | Unix.Unix_error (_, _, _) -> 1
> 
> Thanks,
> 

http://repo.or.cz/w/apc.git/blob/55de75ccb853f5e4443fd484e5eb95e1342e72bd:/ml_apc.c

has code to get number of cpus for linux/windows/solaris and osx

-- 
mailto:av1474@comtv.ru

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Cross-platform cpu count
  2011-09-02  0:38 ` malc
@ 2011-09-02  0:47   ` Daniel Bünzli
  2011-09-02  7:39   ` David Allsopp
  1 sibling, 0 replies; 11+ messages in thread
From: Daniel Bünzli @ 2011-09-02  0:47 UTC (permalink / raw)
  To: malc; +Cc: caml-list

Thanks, but I'd like to avoid binding to C at the moment.

Daniel

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [Caml-list] Cross-platform cpu count
  2011-09-02  0:38 ` malc
  2011-09-02  0:47   ` Daniel Bünzli
@ 2011-09-02  7:39   ` David Allsopp
  2011-09-02 10:03     ` Daniel Bünzli
  2011-09-02 12:51     ` malc
  1 sibling, 2 replies; 11+ messages in thread
From: David Allsopp @ 2011-09-02  7:39 UTC (permalink / raw)
  To: caml-list

malc wrote:
> On Fri, 2 Sep 2011, Daniel Bünzli wrote:
> 
> > Hello,
> >
> > Can anybody confirm me that the following code works on cygwin :

It won't - Sys.os_type returns "Cygwin" and getconf isn't in Cygwin either - http://cygwin.com/ml/cygwin/2010-12/msg00435.html (actually, it may be now - I haven't upgraded my Cygwin in a while - but it'll be a recent addition)

> >
> > let cpu_count () =
> >   try match Sys.os_type with
> >   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
> >   | _ ->
> >       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
> >       let close () = ignore (Unix.close_process_in i) in
> >       try Scanf.fscanf i "%d" (fun n -> close (); n) with e -> close ();
> raise e
> >   with
> >   | Not_found | Sys_error _ | Failure _ | Scanf.Scan_failure _
> >   | End_of_file | Unix.Unix_error (_, _, _) -> 1
> >
> > Thanks,
> >
> 
> http://repo.or.cz/w/apc.git/blob/55de75ccb853f5e4443fd484e5eb95e1342e72bd:
> /ml_apc.c

The C code here uses a deprecated API call (probably for Windows NT 4 compatibility). If you do end up using a C stub, use GetSystemInfo (http://msdn.microsoft.com/en-us/library/ms724381(v=vs.85).aspx) - it's easier to call. It's used in the OCaml runtime - see byterun/win32.c. Personally, even for something where it's unimportant, I'd be nervous relying on an environment variable (which can be edited...)


David 



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Cross-platform cpu count
  2011-09-02  0:04 [Caml-list] Cross-platform cpu count Daniel Bünzli
  2011-09-02  0:38 ` malc
@ 2011-09-02  7:51 ` Richard W.M. Jones
  2011-09-02  8:19   ` David Allsopp
  1 sibling, 1 reply; 11+ messages in thread
From: Richard W.M. Jones @ 2011-09-02  7:51 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list

On Fri, Sep 02, 2011 at 02:04:51AM +0200, Daniel Bünzli wrote:
> Hello,
> 
> Can anybody confirm me that the following code works on cygwin :
> 
> let cpu_count () =
>   try match Sys.os_type with
>   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
>   | _ ->
>       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in

Don't know about Cygwin, but it won't work on MinGW.

Rich.

-- 
Richard Jones
Red Hat

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [Caml-list] Cross-platform cpu count
  2011-09-02  7:51 ` Richard W.M. Jones
@ 2011-09-02  8:19   ` David Allsopp
  2011-09-02 14:58     ` Richard W.M. Jones
  0 siblings, 1 reply; 11+ messages in thread
From: David Allsopp @ 2011-09-02  8:19 UTC (permalink / raw)
  To: caml-list

Richard W.M. Jones wrote:
> On Fri, Sep 02, 2011 at 02:04:51AM +0200, Daniel Bünzli wrote:
> > Hello,
> >
> > Can anybody confirm me that the following code works on cygwin :
> >
> > let cpu_count () =
> >   try match Sys.os_type with
> >   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
> >   | _ ->
> >       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
> 
> Don't know about Cygwin, but it won't work on MinGW.

What's your reason? (given that it does work)


David


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Cross-platform cpu count
  2011-09-02  7:39   ` David Allsopp
@ 2011-09-02 10:03     ` Daniel Bünzli
  2011-09-02 12:51     ` malc
  1 sibling, 0 replies; 11+ messages in thread
From: Daniel Bünzli @ 2011-09-02 10:03 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

> It won't - Sys.os_type returns "Cygwin" and getconf isn't in Cygwin either - http://cygwin.com/ml/cygwin/2010-12/msg00435.html (actually, it may be now - I haven't upgraded my Cygwin in a while - but it'll be a recent addition)

Ok thanks. Apparently it may eventually [1].

> Personally, even for something where it's unimportant, I'd be nervous relying on an environment variable (which can be edited...)

I'm even more nervous about calling command line tools. The right way
would be to use sysconf(3) or systcl(3), but Sys or Unix doesn't
provide them.

Best,

Daniel

[1] http://www.mail-archive.com/cygwin-patches@cygwin.com/msg05220.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [Caml-list] Cross-platform cpu count
  2011-09-02  7:39   ` David Allsopp
  2011-09-02 10:03     ` Daniel Bünzli
@ 2011-09-02 12:51     ` malc
  2011-09-02 13:05       ` David Allsopp
  1 sibling, 1 reply; 11+ messages in thread
From: malc @ 2011-09-02 12:51 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

On Fri, 2 Sep 2011, David Allsopp wrote:

> malc wrote:
> > On Fri, 2 Sep 2011, Daniel B?nzli wrote:
> > 
> > > Hello,
> > >
> > > Can anybody confirm me that the following code works on cygwin :
> 
> It won't - Sys.os_type returns "Cygwin" and getconf isn't in Cygwin either - http://cygwin.com/ml/cygwin/2010-12/msg00435.html (actually, it may be now - I haven't upgraded my Cygwin in a while - but it'll be a recent addition)
> 
> > >
> > > let cpu_count () =
> > >   try match Sys.os_type with
> > >   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
> > >   | _ ->
> > >       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
> > >       let close () = ignore (Unix.close_process_in i) in
> > >       try Scanf.fscanf i "%d" (fun n -> close (); n) with e -> close ();
> > raise e
> > >   with
> > >   | Not_found | Sys_error _ | Failure _ | Scanf.Scan_failure _
> > >   | End_of_file | Unix.Unix_error (_, _, _) -> 1
> > >
> > > Thanks,
> > >
> > 
> > http://repo.or.cz/w/apc.git/blob/55de75ccb853f5e4443fd484e5eb95e1342e72bd:
> > /ml_apc.c
> 
> The C code here uses a deprecated API call (probably for Windows NT 4 
> compatibility). If you do end up using a C stub, use GetSystemInfo 
> (http://msdn.microsoft.com/en-us/library/ms724381(v=vs.85).aspx) - it's 
> easier to call. It's used in the OCaml runtime - see byterun/win32.c. 
> Personally, even for something where it's unimportant, I'd be nervous 
> relying on an environment variable (which can be edited...)

Uhm, no, it uses native API which was never publically documented to begin
with, and the reason for that that it provides other information which is
unavailable via other means.

-- 
mailto:av1474@comtv.ru

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [Caml-list] Cross-platform cpu count
  2011-09-02 12:51     ` malc
@ 2011-09-02 13:05       ` David Allsopp
  2011-09-02 13:42         ` malc
  0 siblings, 1 reply; 11+ messages in thread
From: David Allsopp @ 2011-09-02 13:05 UTC (permalink / raw)
  To: malc; +Cc: caml-list

malc wrote:
> On Fri, 2 Sep 2011, David Allsopp wrote:
> 
> > malc wrote:
> > > On Fri, 2 Sep 2011, Daniel B?nzli wrote:
> > >
> > > > Hello,
> > > >
> > > > Can anybody confirm me that the following code works on cygwin :
> >
> > It won't - Sys.os_type returns "Cygwin" and getconf isn't in Cygwin
> > either - http://cygwin.com/ml/cygwin/2010-12/msg00435.html (actually,
> > it may be now - I haven't upgraded my Cygwin in a while - but it'll be
> > a recent addition)
> >
> > > >
> > > > let cpu_count () =
> > > >   try match Sys.os_type with
> > > >   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
> > > >   | _ ->
> > > >       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
> > > >       let close () = ignore (Unix.close_process_in i) in
> > > >       try Scanf.fscanf i "%d" (fun n -> close (); n) with e ->
> > > > close ();
> > > raise e
> > > >   with
> > > >   | Not_found | Sys_error _ | Failure _ | Scanf.Scan_failure _
> > > >   | End_of_file | Unix.Unix_error (_, _, _) -> 1
> > > >
> > > > Thanks,
> > > >
> > >
> > >
> http://repo.or.cz/w/apc.git/blob/55de75ccb853f5e4443fd484e5eb95e1342e72bd
> :
> > > /ml_apc.c
> >
> > The C code here uses a deprecated API call (probably for Windows NT 4
> > compatibility). If you do end up using a C stub, use GetSystemInfo
> > (http://msdn.microsoft.com/en-us/library/ms724381(v=vs.85).aspx) -
> > it's easier to call. It's used in the OCaml runtime - see
> byterun/win32.c.
> > Personally, even for something where it's unimportant, I'd be nervous
> > relying on an environment variable (which can be edited...)
> 
> Uhm, no, it uses native API which was never publically documented to
> begin with, and the reason for that that it provides other information
> which is unavailable via other means.

It is documented and it is deprecated for this usage (see http://msdn.microsoft.com/en-us/library/ms725506(v=VS.85).aspx) making it a poor example for the specific question of querying logical processor count.


David


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [Caml-list] Cross-platform cpu count
  2011-09-02 13:05       ` David Allsopp
@ 2011-09-02 13:42         ` malc
  0 siblings, 0 replies; 11+ messages in thread
From: malc @ 2011-09-02 13:42 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

On Fri, 2 Sep 2011, David Allsopp wrote:

> malc wrote:
> > On Fri, 2 Sep 2011, David Allsopp wrote:
> > 
> > > malc wrote:
> > > > On Fri, 2 Sep 2011, Daniel B?nzli wrote:
> > > >
> > > > > Hello,
> > > > >
> > > > > Can anybody confirm me that the following code works on cygwin :
> > >
> > > It won't - Sys.os_type returns "Cygwin" and getconf isn't in Cygwin
> > > either - http://cygwin.com/ml/cygwin/2010-12/msg00435.html (actually,
> > > it may be now - I haven't upgraded my Cygwin in a while - but it'll be
> > > a recent addition)
> > >
> > > > >
> > > > > let cpu_count () =
> > > > >   try match Sys.os_type with
> > > > >   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
> > > > >   | _ ->
> > > > >       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
> > > > >       let close () = ignore (Unix.close_process_in i) in
> > > > >       try Scanf.fscanf i "%d" (fun n -> close (); n) with e ->
> > > > > close ();
> > > > raise e
> > > > >   with
> > > > >   | Not_found | Sys_error _ | Failure _ | Scanf.Scan_failure _
> > > > >   | End_of_file | Unix.Unix_error (_, _, _) -> 1
> > > > >
> > > > > Thanks,
> > > > >
> > > >
> > > >
> > http://repo.or.cz/w/apc.git/blob/55de75ccb853f5e4443fd484e5eb95e1342e72bd
> > :
> > > > /ml_apc.c
> > >
> > > The C code here uses a deprecated API call (probably for Windows NT 4
> > > compatibility). If you do end up using a C stub, use GetSystemInfo
> > > (http://msdn.microsoft.com/en-us/library/ms724381(v=vs.85).aspx) -
> > > it's easier to call. It's used in the OCaml runtime - see
> > byterun/win32.c.
> > > Personally, even for something where it's unimportant, I'd be nervous
> > > relying on an environment variable (which can be edited...)
> > 
> > Uhm, no, it uses native API which was never publically documented to
> > begin with, and the reason for that that it provides other information
> > which is unavailable via other means.
> 
> It is documented and it is deprecated for this usage (see 
> http://msdn.microsoft.com/en-us/library/ms725506(v=VS.85).aspx) making 
> it a poor example for the specific question of querying logical 
> processor count.

Well, okay, not however that the substitute for
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION is only available starting with 
XP SP1, in any case the times provided by the kernel are wrong anyway.

All that said, Get[Native]SystemInfo (possibly with 
GetLogicalProcessorInfomration[Ex]) is better in this particular case.

-- 
mailto:av1474@comtv.ru

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Cross-platform cpu count
  2011-09-02  8:19   ` David Allsopp
@ 2011-09-02 14:58     ` Richard W.M. Jones
  0 siblings, 0 replies; 11+ messages in thread
From: Richard W.M. Jones @ 2011-09-02 14:58 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

On Fri, Sep 02, 2011 at 08:19:00AM +0000, David Allsopp wrote:
> Richard W.M. Jones wrote:
> > On Fri, Sep 02, 2011 at 02:04:51AM +0200, Daniel Bünzli wrote:
> > > Hello,
> > >
> > > Can anybody confirm me that the following code works on cygwin :
> > >
> > > let cpu_count () =
> > >   try match Sys.os_type with
> > >   | "Win32" -> int_of_string (Sys.getenv "NUMBER_OF_PROCESSORS")
> > >   | _ ->
> > >       let i = Unix.open_process_in "getconf _NPROCESSORS_ONLN" in
> > 
> > Don't know about Cygwin, but it won't work on MinGW.
> 
> What's your reason? (given that it does work)

Yes, indeed it does.  The lesson is I should try it before
pontificating!

Rich.

-- 
Richard Jones
Red Hat

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2011-09-02 14:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-02  0:04 [Caml-list] Cross-platform cpu count Daniel Bünzli
2011-09-02  0:38 ` malc
2011-09-02  0:47   ` Daniel Bünzli
2011-09-02  7:39   ` David Allsopp
2011-09-02 10:03     ` Daniel Bünzli
2011-09-02 12:51     ` malc
2011-09-02 13:05       ` David Allsopp
2011-09-02 13:42         ` malc
2011-09-02  7:51 ` Richard W.M. Jones
2011-09-02  8:19   ` David Allsopp
2011-09-02 14:58     ` Richard W.M. Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).