GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
nproc.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "nproc-wrapper.h"
31 
32 #include "defun.h"
33 #include "error.h"
34 
36 
37 DEFUN (nproc, args, ,
38  doc: /* -*- texinfo -*-
39 @deftypefn {} {@var{n} =} nproc ()
40 @deftypefnx {} {@var{n} =} nproc (@var{query})
41 Return the current number of available (logical) processors.
42 
43 This returns the number of logical processors. For processors with
44 hyperthreading, this is larger than the number of physical cores.
45 
46 If called with the optional argument @var{query}, modify how processors
47 are counted as follows:
48 
49 @table @code
50 @item all
51 total number of processors.
52 
53 @item current
54 processors available to the current process.
55 
56 @item overridable
57 same as @code{current}, but overridable through the
58 @w{@env{OMP_NUM_THREADS}}@ environment variable.
59 @end table
60 @end deftypefn */)
61 {
62  int nargin = args.length ();
63 
64  if (nargin > 1)
65  print_usage ();
66 
68 
69  if (nargin == 1)
70  {
71  std::string arg = args(0).string_value ();
72 
73  std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
74 
75  if (arg == "all")
76  query = OCTAVE_NPROC_ALL;
77  else if (arg == "current")
78  query = OCTAVE_NPROC_CURRENT;
79  else if (arg == "overridable")
81  else
82  error ("nproc: invalid value for QUERY");
83  }
84 
85  return ovl (octave_num_processors_wrapper (query));
86 }
87 
88 /*
89 ## Must always report at least 1 cpu available
90 %!assert (nproc () >= 1)
91 %!assert (nproc ("all") >= 1)
92 %!assert (nproc ("current") >= 1)
93 
94 ## Test that "overridable" is the default
95 %!assert (nproc ("overridable"), nproc ())
96 
97 %!test
98 %! c = nproc ("current");
99 %! unwind_protect
100 %! old_val = getenv ("OMP_NUM_THREADS");
101 %! new_val = c + 1;
102 %! setenv ("OMP_NUM_THREADS", num2str (new_val));
103 %! assert (nproc ("overridable"), new_val);
104 %! unwind_protect_cleanup
105 %! if (! isempty (old_val))
106 %! setenv ("OMP_NUM_THREADS", old_val);
107 %! else
108 %! unsetenv ("OMP_NUM_THREADS");
109 %! endif
110 %! end_unwind_protect
111 
112 %!error nproc ("no_valid_option")
113 */
114 
115 OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void() error(const char *fmt,...)
Definition: error.cc:988
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5468
unsigned long int octave_num_processors_wrapper(enum octave_nproc_query octave_query)
Definition: nproc-wrapper.c:40
octave_nproc_query
Definition: nproc-wrapper.h:34
@ OCTAVE_NPROC_CURRENT
Definition: nproc-wrapper.h:36
@ OCTAVE_NPROC_ALL
Definition: nproc-wrapper.h:35
@ OCTAVE_NPROC_CURRENT_OVERRIDABLE
Definition: nproc-wrapper.h:37
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219