nproc.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2012 Iain Murray
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "defun-dld.h"
00028 #include "nproc.h"
00029 
00030 DEFUN_DLD (nproc, args, nargout,
00031    "-*- texinfo -*-\n\
00032 @deftypefn  {Loadable Function} {} nproc ()\n\
00033 @deftypefnx {Loadable Function} {} nproc (@var{query})\n\
00034 Return the current number of available processors.\n\
00035 \n\
00036 If called with the optional argument @var{query}, modify how processors\n\
00037 are counted as follows:\n\
00038 @table @code\n\
00039 @item all\n\
00040 total number of processors.\n\
00041 \n\
00042 @item current\n\
00043 processors available to the current process.\n\
00044 \n\
00045 @item overridable\n\
00046 likewise, but overridable through the @w{@env{OMP_NUM_THREADS}} environment\n\
00047 variable.\n\
00048 @end table\n\
00049 @end deftypefn")
00050 {
00051   octave_value retval;
00052 
00053   int nargin = args.length ();
00054 
00055   if ((nargin != 0 && nargin != 1) || (nargout != 0 && nargout != 1))
00056     {
00057       print_usage ();
00058       return retval;
00059     }
00060 
00061   nproc_query query = NPROC_CURRENT;
00062   if (nargin == 1)
00063     {
00064       std::string arg = args(0).string_value ();
00065 
00066       std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
00067 
00068       if (arg == "all")
00069         query = NPROC_ALL;
00070       else if (arg == "current")
00071         query = NPROC_CURRENT;
00072       else if (arg == "overridable")
00073         query = NPROC_CURRENT_OVERRIDABLE;
00074       else
00075         {
00076           error ("nproc: invalid value for QUERY");
00077           return retval;
00078         }
00079     }
00080 
00081   retval = num_processors (query);
00082 
00083   return retval;
00084 }
00085 
00086 /*
00087 
00088 %% Must always report at least 1 cpu available
00089 %!assert (nproc () >= 1);
00090 
00091 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines