1 ## Copyright (C) 2010 VZLU Prague, a.s., Czech Republic
2 ## Copyright (C) 2010 Jean-Benoist Leger <jben@jben.info>
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
18 ## @deftypefn{Function File} {} chunk_parcellfun (@dots{:})
19 ## Undocumented internal function.
22 function varargout = chunk_parcellfun (nproc, chunks_per_proc, func,
23 error_handler, verbose_level, varargin)
27 nchunks = chunks_per_proc * nproc;
29 ## Compute optimal chunk sizes.
31 len_chunk = ceil (N/nchunks);
32 chunk_sizes = len_chunk (ones(1, nchunks));
33 chunk_sizes(1:nchunks*len_chunk - N) -= 1;
35 ## Split argument arrays into chunks (thus making arrays of arrays).
36 chunked_args = cellfun (@(arg) mat2cell (arg(:), chunk_sizes), args, ...
37 "UniformOutput", false);
39 ## Attach error handler if present.
40 if (! isempty (error_handler))
41 chunked_args = [chunked_args, {"ErrorHandler", error_handler}];
45 [out_brut{1:nargout}] = parcellfun (nproc, func, chunked_args{:}, ...
46 "UniformOutput", false, "VerboseLevel", verbose_level);
48 ## Concatenate output args and reshape them to correct size.
49 true_size = size (args{1});
50 varargout = cellfun (@(arg) reshape (vertcat (arg{:}), true_size), out_brut, "UniformOutput", false);