GNU Octave  3.8.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-2013 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 likewise, but overridable through the @w{@env{OMP_NUM_THREADS}} environment\n\
48 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 */