GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
nproc.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "defun.h"
28 #include "nproc.h"
29 
30 DEFUN (nproc, args, nargout,
31  "-*- texinfo -*-\n\
32 @deftypefn {Built-in Function} {} nproc ()\n\
33 @deftypefnx {Built-in Function} {} nproc (@var{query})\n\
34 Return the current number of available processors.\n\
35 \n\
36 If called with the optional argument @var{query}, modify how processors\n\
37 are counted as follows:\n\
38 \n\
39 @table @code\n\
40 @item all\n\
41 total number of processors.\n\
42 \n\
43 @item current\n\
44 processors available to the current process.\n\
45 \n\
46 @item overridable\n\
47 same as @code{current}, but overridable through the @w{@env{OMP_NUM_THREADS}}\n\
48 environment variable.\n\
49 @end table\n\
50 @end deftypefn")
51 {
52  octave_value retval;
53 
54  int nargin = args.length ();
55 
56  if ((nargin != 0 && nargin != 1) || (nargout != 0 && nargout != 1))
57  {
58  print_usage ();
59  return retval;
60  }
61 
62  nproc_query query = NPROC_CURRENT;
63  if (nargin == 1)
64  {
65  std::string arg = args(0).string_value ();
66 
67  std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
68 
69  if (arg == "all")
70  query = NPROC_ALL;
71  else if (arg == "current")
72  query = NPROC_CURRENT;
73  else if (arg == "overridable")
74  query = NPROC_CURRENT_OVERRIDABLE;
75  else
76  {
77  error ("nproc: invalid value for QUERY");
78  return retval;
79  }
80  }
81 
82  retval = num_processors (query);
83 
84  return retval;
85 }
86 
87 /*
88 ## Must always report at least 1 cpu available
89 %!assert (nproc () >= 1);
90 %!assert (nproc ("all") >= 1);
91 %!assert (nproc ("current") >= 1);
92 
93 %!test
94 %! c = nproc ("current");
95 %! unwind_protect
96 %! old_val = getenv ("OMP_NUM_THREADS");
97 %! new_val = c + 1;
98 %! setenv ("OMP_NUM_THREADS", num2str (new_val));
99 %! assert (nproc ("overridable"), new_val);
100 %! unwind_protect_cleanup
101 %! if (! isempty (old_val))
102 %! setenv ("OMP_NUM_THREADS", old_val);
103 %! else
104 %! unsetenv ("OMP_NUM_THREADS");
105 %! endif
106 %! end_unwind_protect
107 
108 %!error nproc ("no_valid_option");
109 */
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
octave_idx_type length(void) const
Definition: ov.cc:1525
double arg(double x)
Definition: lo-mappers.h:37
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5259