GNU Octave  4.4.1
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-2018 Iain Murray
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "defun.h"
28 #include "nproc-wrapper.h"
29 
30 DEFUN (nproc, args, ,
31  doc: /* -*- texinfo -*-
32 @deftypefn {} {} nproc ()
33 @deftypefnx {} {} nproc (@var{query})
34 Return the current number of available processors.
35 
36 If called with the optional argument @var{query}, modify how processors
37 are counted as follows:
38 
39 @table @code
40 @item all
41 total number of processors.
42 
43 @item current
44 processors available to the current process.
45 
46 @item overridable
47 same as @code{current}, but overridable through the
48 @w{@env{OMP_NUM_THREADS}} environment variable.
49 @end table
50 @end deftypefn */)
51 {
52  int nargin = args.length ();
53 
54  if (nargin > 1)
55  print_usage ();
56 
58 
59  if (nargin == 1)
60  {
61  std::string arg = args(0).string_value ();
62 
63  std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
64 
65  if (arg == "all")
66  query = OCTAVE_NPROC_ALL;
67  else if (arg == "current")
68  query = OCTAVE_NPROC_CURRENT;
69  else if (arg == "overridable")
71  else
72  error ("nproc: invalid value for QUERY");
73  }
74 
75  return ovl (octave_num_processors_wrapper (query));
76 }
77 
78 /*
79 ## Must always report at least 1 cpu available
80 %!assert (nproc () >= 1)
81 %!assert (nproc ("all") >= 1)
82 %!assert (nproc ("current") >= 1)
83 
84 %!test
85 %! c = nproc ("current");
86 %! unwind_protect
87 %! old_val = getenv ("OMP_NUM_THREADS");
88 %! new_val = c + 1;
89 %! setenv ("OMP_NUM_THREADS", num2str (new_val));
90 %! assert (nproc ("overridable"), new_val);
91 %! unwind_protect_cleanup
92 %! if (! isempty (old_val))
93 %! setenv ("OMP_NUM_THREADS", old_val);
94 %! else
95 %! unsetenv ("OMP_NUM_THREADS");
96 %! endif
97 %! end_unwind_protect
98 
99 %!error nproc ("no_valid_option")
100 */
std::string string_value(bool force=false) const
Definition: ov.h:955
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
octave_value arg
Definition: pr-output.cc:3244
unsigned long int octave_num_processors_wrapper(enum octave_nproc_query octave_query)
Definition: nproc-wrapper.c:37
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
args.length() nargin
Definition: file-io.cc:589
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5410
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
octave_nproc_query
Definition: nproc-wrapper.h:30