GNU Octave 7.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-2022 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
35OCTAVE_NAMESPACE_BEGIN
36
37DEFUN (nproc, args, ,
38 doc: /* -*- texinfo -*-
39@deftypefn {} {} nproc ()
40@deftypefnx {} {} nproc (@var{query})
41Return the current number of available processors.
42
43If called with the optional argument @var{query}, modify how processors
44are counted as follows:
45
46@table @code
47@item all
48total number of processors.
49
50@item current
51processors available to the current process.
52
53@item overridable
54same as @code{current}, but overridable through the
55@w{@env{OMP_NUM_THREADS}} environment variable.
56@end table
57@end deftypefn */)
58{
59 int nargin = args.length ();
60
61 if (nargin > 1)
62 print_usage ();
63
65
66 if (nargin == 1)
67 {
68 std::string arg = args(0).string_value ();
69
70 std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
71
72 if (arg == "all")
73 query = OCTAVE_NPROC_ALL;
74 else if (arg == "current")
76 else if (arg == "overridable")
78 else
79 error ("nproc: invalid value for QUERY");
80 }
81
82 return ovl (octave_num_processors_wrapper (query));
83}
84
85/*
86## Must always report at least 1 cpu available
87%!assert (nproc () >= 1)
88%!assert (nproc ("all") >= 1)
89%!assert (nproc ("current") >= 1)
90
91## Test that "overridable" is the default
92%!assert (nproc ("overridable"), nproc ())
93
94%!test
95%! c = nproc ("current");
96%! unwind_protect
97%! old_val = getenv ("OMP_NUM_THREADS");
98%! new_val = c + 1;
99%! setenv ("OMP_NUM_THREADS", num2str (new_val));
100%! assert (nproc ("overridable"), new_val);
101%! unwind_protect_cleanup
102%! if (! isempty (old_val))
103%! setenv ("OMP_NUM_THREADS", old_val);
104%! else
105%! unsetenv ("OMP_NUM_THREADS");
106%! endif
107%! end_unwind_protect
108
109%!error nproc ("no_valid_option")
110*/
111
112OCTAVE_NAMESPACE_END
OCTINTERP_API 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:980
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5861
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:211