Mercurial > hg > octave-nkf
annotate libinterp/corefcn/nproc.cc @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | 4f45eaf83908 |
children |
rev | line source |
---|---|
12510
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
1 /* |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
2 |
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19360
diff
changeset
|
3 Copyright (C) 2012-2015 Iain Murray |
12510
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
4 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
5 This file is part of Octave. |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
6 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
7 Octave is free software; you can redistribute it and/or modify it |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
10 option) any later version. |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
11 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
15 for more details. |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
16 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
18 along with Octave; see the file COPYING. If not, see |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
20 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
21 */ |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
22 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
24 #include <config.h> |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
25 #endif |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
26 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
27 #include "defun.h" |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
28 #include "nproc.h" |
12510
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
29 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
30 DEFUN (nproc, args, nargout, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
31 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
32 @deftypefn {Built-in Function} {} nproc ()\n\ |
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
33 @deftypefnx {Built-in Function} {} nproc (@var{query})\n\ |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
34 Return the current number of available processors.\n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
35 \n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
36 If called with the optional argument @var{query}, modify how processors\n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
37 are counted as follows:\n\ |
14366
b76f0740940e
doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
38 \n\ |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
39 @table @code\n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
40 @item all\n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
41 total number of processors.\n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
42 \n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
43 @item current\n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
44 processors available to the current process.\n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
45 \n\ |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
46 @item overridable\n\ |
20382
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
47 same as @code{current}, but overridable through the @w{@env{OMP_NUM_THREADS}}\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
48 environment variable.\n\ |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
49 @end table\n\ |
12510
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
50 @end deftypefn") |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
51 { |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
52 octave_value retval; |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
53 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
54 int nargin = args.length (); |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
55 |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
56 if ((nargin != 0 && nargin != 1) || (nargout != 0 && nargout != 1)) |
12510
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
57 { |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
58 print_usage (); |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
59 return retval; |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
60 } |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
61 |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
62 nproc_query query = NPROC_CURRENT; |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
63 if (nargin == 1) |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
64 { |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
65 std::string arg = args(0).string_value (); |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
66 |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
67 std::transform (arg.begin (), arg.end (), arg.begin (), tolower); |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
68 |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
69 if (arg == "all") |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
70 query = NPROC_ALL; |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
71 else if (arg == "current") |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
72 query = NPROC_CURRENT; |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
73 else if (arg == "overridable") |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
74 query = NPROC_CURRENT_OVERRIDABLE; |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
75 else |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
76 { |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
77 error ("nproc: invalid value for QUERY"); |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
78 return retval; |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
79 } |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
80 } |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
81 |
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
82 retval = num_processors (query); |
12510
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
83 |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
84 return retval; |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
85 } |
a1b2da4967ac
Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff
changeset
|
86 |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
87 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14366
diff
changeset
|
88 ## Must always report at least 1 cpu available |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
89 %!assert (nproc () >= 1); |
19328
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
90 %!assert (nproc ("all") >= 1); |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
91 %!assert (nproc ("current") >= 1); |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
92 |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
93 %!test |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
94 %! c = nproc ("current"); |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
95 %! unwind_protect |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
96 %! old_val = getenv ("OMP_NUM_THREADS"); |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
97 %! new_val = c + 1; |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
98 %! setenv ("OMP_NUM_THREADS", num2str (new_val)); |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
99 %! assert (nproc ("overridable"), new_val); |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
100 %! unwind_protect_cleanup |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
101 %! if (! isempty (old_val)) |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
102 %! setenv ("OMP_NUM_THREADS", old_val); |
19337
b5c03bcc640a
nproc: Fix unwind_protect_cleanup block in %!test
Mike Miller <mtmiller@ieee.org>
parents:
19328
diff
changeset
|
103 %! else |
19360
9163a6e9b096
Use unsetenv to fix libgomp error created by nproc BIST testing.
Rik <rik@octave.org>
parents:
19337
diff
changeset
|
104 %! unsetenv ("OMP_NUM_THREADS"); |
19328
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
105 %! endif |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
106 %! end_unwind_protect |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
107 |
10c57204fd72
nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
17787
diff
changeset
|
108 %!error nproc ("no_valid_option"); |
12512
77b14e634166
Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents:
12510
diff
changeset
|
109 */ |