X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fgeneral-1.3.1%2Fpararrayfun.m;fp=octave_packages%2Fgeneral-1.3.1%2Fpararrayfun.m;h=cf61eb1a2912e1406131a629b97fd50c14f7556e;hp=0000000000000000000000000000000000000000;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/general-1.3.1/pararrayfun.m b/octave_packages/general-1.3.1/pararrayfun.m new file mode 100644 index 0000000..cf61eb1 --- /dev/null +++ b/octave_packages/general-1.3.1/pararrayfun.m @@ -0,0 +1,73 @@ +## Copyright (C) 2009 Jaroslav Hajek +## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic +## Copyright (C) 2009 Travis Collier +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn{Function File} {[@var{o1}, @var{o2}, @dots{}] =} pararrayfun (@var{nproc}, @var{fun}, @var{a1}, @var{a2}, @dots{}) +## @deftypefnx{Function File} {} pararrayfun (nproc, fun, @dots{}, "UniformOutput", @var{val}) +## @deftypefnx{Function File} {} pararrayfun (nproc, fun, @dots{}, "ErrorHandler", @var{errfunc}) +## Evaluates a function for corresponding elements of an array. +## Argument and options handling is analogical to @code{parcellfun}, except that +## arguments are arrays rather than cells. If cells occur as arguments, they are treated +## as arrays of singleton cells. +## Arrayfun supports one extra option compared to parcellfun: "Vectorized". +## This option must be given together with "ChunksPerProc" and it indicates +## that @var{fun} is able to operate on vectors rather than just scalars, and returns +## a vector. The same must be true for @var{errfunc}, if given. +## In this case, the array is split into chunks which are then directly served to @var{func} +## for evaluation, and the results are concatenated to output arrays. +## @seealso{parcellfun, arrayfun} +## @end deftypefn + +function varargout = pararrayfun (nproc, func, varargin) + + if (nargin < 3) + print_usage (); + endif + + [nargs, uniform_output, error_handler, ... + verbose_level, chunks_per_proc, vectorized] = parcellfun_opts (varargin); + + args = varargin(1:nargs); + opts = varargin(nargs+1:end); + if (nargs == 0) + print_usage (); + elseif (nargs > 1) + [err, args{:}] = common_size (args{:}); + if (err) + error ("pararrayfun: arguments size must match"); + endif + endif + + njobs = numel (args{1}); + + if (vectorized && chunks_per_proc > 0 && chunks_per_proc < njobs / nproc) + ## If "Vectorized" is on, we apply the function directly on chunks of + ## arrays. + [varargout{1:nargout}] = chunk_parcellfun (nproc, chunks_per_proc, ... + func, error_handler, verbose_level, args{:}); + else + args = cellfun (@num2cell, args, "UniformOutput", false, + "ErrorHandler", @arg_class_error); + + [varargout{1:nargout}] = parcellfun (nproc, func, args{:}, opts{:}); + endif + +endfunction + +function arg_class_error (S, X) + error ("arrayfun: invalid argument of class %s", class (X)) +endfunction