annotate libinterp/corefcn/nproc.cc @ 31183:932e43d948a6

maint: Merge stable to default.
author Markus Mützel <markus.muetzel@gmx.de>
date Thu, 11 Aug 2022 09:38:09 +0200
parents 32d2b6604a9f e217b44187a2
children e88a07dec498
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
3 // Copyright (C) 2012-2022 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
12510
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21581
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
27 # include "config.h"
12510
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
28 #endif
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
29
27130
396d17dcfb9f don't include error.h in ov-base.h
John W. Eaton <jwe@octave.org>
parents: 27032
diff changeset
30 #include "nproc-wrapper.h"
396d17dcfb9f don't include error.h in ov-base.h
John W. Eaton <jwe@octave.org>
parents: 27032
diff changeset
31
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
32 #include "defun.h"
27130
396d17dcfb9f don't include error.h in ov-base.h
John W. Eaton <jwe@octave.org>
parents: 27032
diff changeset
33 #include "error.h"
12510
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
34
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
35 OCTAVE_NAMESPACE_BEGIN
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
36
20915
a3359fe50966 remove unused nargout variables
John W. Eaton <jwe@octave.org>
parents: 20909
diff changeset
37 DEFUN (nproc, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
38 doc: /* -*- texinfo -*-
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30564
diff changeset
39 @deftypefn {} {@var{n} =} nproc ()
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30564
diff changeset
40 @deftypefnx {} {@var{n} =} nproc (@var{query})
31182
e217b44187a2 doc: Clarify number of processors returned by "nproc" with hyperthreading.
Markus Mützel <markus.muetzel@gmx.de>
parents: 30564
diff changeset
41 Return the current number of available (logical) processors.
e217b44187a2 doc: Clarify number of processors returned by "nproc" with hyperthreading.
Markus Mützel <markus.muetzel@gmx.de>
parents: 30564
diff changeset
42
e217b44187a2 doc: Clarify number of processors returned by "nproc" with hyperthreading.
Markus Mützel <markus.muetzel@gmx.de>
parents: 30564
diff changeset
43 This returns the number of logical processors. For processors with
e217b44187a2 doc: Clarify number of processors returned by "nproc" with hyperthreading.
Markus Mützel <markus.muetzel@gmx.de>
parents: 30564
diff changeset
44 hyperthreading, this is larger than the number of physical cores.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
45
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
46 If called with the optional argument @var{query}, modify how processors
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
47 are counted as follows:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
48
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
49 @table @code
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
50 @item all
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
51 total number of processors.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
52
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
53 @item current
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
54 processors available to the current process.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
55
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
56 @item overridable
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
57 same as @code{current}, but overridable through the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
58 @w{@env{OMP_NUM_THREADS}} environment variable.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
59 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21893
diff changeset
60 @end deftypefn */)
12510
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
61 {
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
62 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
63
20909
03e4ddd49396 omit unnecessary nargout checks
John W. Eaton <jwe@octave.org>
parents: 20853
diff changeset
64 if (nargin > 1)
20801
a542a9bf177e eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
65 print_usage ();
12510
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
66
27032
0ee0bb5eb381 make nproc default to "overridable" to agree with nproc shell utility
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
67 octave_nproc_query query = OCTAVE_NPROC_CURRENT_OVERRIDABLE;
21893
0a6f387d18d3 * nproc.cc, oct-fftw.cc: Use octave_num_processors_wrapper.
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
68
12512
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
69 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
70 {
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
71 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
72
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
73 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
74
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
75 if (arg == "all")
21893
0a6f387d18d3 * nproc.cc, oct-fftw.cc: Use octave_num_processors_wrapper.
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
76 query = OCTAVE_NPROC_ALL;
12512
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
77 else if (arg == "current")
21893
0a6f387d18d3 * nproc.cc, oct-fftw.cc: Use octave_num_processors_wrapper.
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
78 query = OCTAVE_NPROC_CURRENT;
12512
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
79 else if (arg == "overridable")
21893
0a6f387d18d3 * nproc.cc, oct-fftw.cc: Use octave_num_processors_wrapper.
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
80 query = OCTAVE_NPROC_CURRENT_OVERRIDABLE;
12512
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
81 else
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20801
diff changeset
82 error ("nproc: invalid value for QUERY");
12512
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
83 }
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
84
21893
0a6f387d18d3 * nproc.cc, oct-fftw.cc: Use octave_num_processors_wrapper.
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
85 return ovl (octave_num_processors_wrapper (query));
12510
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
86 }
a1b2da4967ac Add nproc, nproc_conf utility functions for determining number of available processors.
Iain Murray <iain@iainmurray.net>
parents:
diff changeset
87
12512
77b14e634166 Replace nprocs with nproc function. Use gnulib module for portability across platforms.
Iain Murray <iain@iainmurray.net>
parents: 12510
diff changeset
88 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14366
diff changeset
89 ## Must always report at least 1 cpu available
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
90 %!assert (nproc () >= 1)
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
91 %!assert (nproc ("all") >= 1)
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 21301
diff changeset
92 %!assert (nproc ("current") >= 1)
19136
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
93
27032
0ee0bb5eb381 make nproc default to "overridable" to agree with nproc shell utility
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
94 ## Test that "overridable" is the default
0ee0bb5eb381 make nproc default to "overridable" to agree with nproc shell utility
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
95 %!assert (nproc ("overridable"), nproc ())
0ee0bb5eb381 make nproc default to "overridable" to agree with nproc shell utility
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
96
19136
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
97 %!test
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
98 %! c = nproc ("current");
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
99 %! unwind_protect
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
100 %! old_val = getenv ("OMP_NUM_THREADS");
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
101 %! new_val = c + 1;
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
102 %! setenv ("OMP_NUM_THREADS", num2str (new_val));
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
103 %! assert (nproc ("overridable"), new_val);
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
104 %! unwind_protect_cleanup
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
105 %! if (! isempty (old_val))
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
106 %! setenv ("OMP_NUM_THREADS", old_val);
19145
b5c03bcc640a nproc: Fix unwind_protect_cleanup block in %!test
Mike Miller <mtmiller@ieee.org>
parents: 19136
diff changeset
107 %! else
19168
9163a6e9b096 Use unsetenv to fix libgomp error created by nproc BIST testing.
Rik <rik@octave.org>
parents: 19145
diff changeset
108 %! unsetenv ("OMP_NUM_THREADS");
19136
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
109 %! endif
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
110 %! end_unwind_protect
10c57204fd72 nproc.cc: codesprint: adding tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 17787
diff changeset
111
21581
6fab85c1538f maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents: 21547
diff changeset
112 %!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
113 */
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
114
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
115 OCTAVE_NAMESPACE_END