1 ## Copyright (C) 2009 Jaroslav Hajek <highegg@gmail.com>
2 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
3 ## Copyright (C) 2009 Travis Collier <travcollier@gmail.com>
5 ## This program is free software; you can redistribute it and/or modify it under
6 ## the terms of the GNU General Public License as published by the Free Software
7 ## Foundation; either version 3 of the License, or (at your option) any later
10 ## This program is distributed in the hope that it will be useful, but WITHOUT
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 ## You should have received a copy of the GNU General Public License along with
16 ## this program; if not, see <http://www.gnu.org/licenses/>.
19 ## @deftypefn{Function File} {[@var{o1}, @var{o2}, @dots{}] =} pararrayfun (@var{nproc}, @var{fun}, @var{a1}, @var{a2}, @dots{})
20 ## @deftypefnx{Function File} {} pararrayfun (nproc, fun, @dots{}, "UniformOutput", @var{val})
21 ## @deftypefnx{Function File} {} pararrayfun (nproc, fun, @dots{}, "ErrorHandler", @var{errfunc})
22 ## Evaluates a function for corresponding elements of an array.
23 ## Argument and options handling is analogical to @code{parcellfun}, except that
24 ## arguments are arrays rather than cells. If cells occur as arguments, they are treated
25 ## as arrays of singleton cells.
26 ## Arrayfun supports one extra option compared to parcellfun: "Vectorized".
27 ## This option must be given together with "ChunksPerProc" and it indicates
28 ## that @var{fun} is able to operate on vectors rather than just scalars, and returns
29 ## a vector. The same must be true for @var{errfunc}, if given.
30 ## In this case, the array is split into chunks which are then directly served to @var{func}
31 ## for evaluation, and the results are concatenated to output arrays.
32 ## @seealso{parcellfun, arrayfun}
35 function varargout = pararrayfun (nproc, func, varargin)
41 [nargs, uniform_output, error_handler, ...
42 verbose_level, chunks_per_proc, vectorized] = parcellfun_opts (varargin);
44 args = varargin(1:nargs);
45 opts = varargin(nargs+1:end);
49 [err, args{:}] = common_size (args{:});
51 error ("pararrayfun: arguments size must match");
55 njobs = numel (args{1});
57 if (vectorized && chunks_per_proc > 0 && chunks_per_proc < njobs / nproc)
58 ## If "Vectorized" is on, we apply the function directly on chunks of
60 [varargout{1:nargout}] = chunk_parcellfun (nproc, chunks_per_proc, ...
61 func, error_handler, verbose_level, args{:});
63 args = cellfun (@num2cell, args, "UniformOutput", false,
64 "ErrorHandler", @arg_class_error);
66 [varargout{1:nargout}] = parcellfun (nproc, func, args{:}, opts{:});
71 function arg_class_error (S, X)
72 error ("arrayfun: invalid argument of class %s", class (X))