]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/private/chunk_parcellfun.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / private / chunk_parcellfun.m
1 ## Copyright (C) 2010 VZLU Prague, a.s., Czech Republic
2 ## Copyright (C) 2010 Jean-Benoist Leger <jben@jben.info>
3 ##
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
7 ## version.
8 ##
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
12 ## details.
13 ##
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/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn{Function File} {} chunk_parcellfun (@dots{:})
19 ## Undocumented internal function.
20 ## @end deftypefn
21
22 function varargout = chunk_parcellfun (nproc, chunks_per_proc, func,
23   error_handler, verbose_level, varargin)
24
25   args = varargin;
26   
27   nchunks = chunks_per_proc * nproc;
28
29   ## Compute optimal chunk sizes.
30   N = numel (args{1});
31   len_chunk = ceil (N/nchunks);
32   chunk_sizes = len_chunk (ones(1, nchunks));
33   chunk_sizes(1:nchunks*len_chunk - N) -= 1;
34
35   ## Split argument arrays into chunks (thus making arrays of arrays).
36   chunked_args = cellfun (@(arg) mat2cell (arg(:), chunk_sizes), args, ...
37   "UniformOutput", false);
38
39   ## Attach error handler if present.
40   if (! isempty (error_handler))
41     chunked_args = [chunked_args, {"ErrorHandler", error_handler}];
42   endif
43
44   ## Main call.
45   [out_brut{1:nargout}] = parcellfun (nproc, func, chunked_args{:}, ...
46   "UniformOutput", false, "VerboseLevel", verbose_level);
47
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);
51
52 endfunction
53