File: chunk_parcellfun.m

package info (click to toggle)
octave-parallel 4.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,648 kB
  • sloc: ansic: 24,011; cpp: 6,848; sh: 4,476; makefile: 297; perl: 170; xml: 22
file content (63 lines) | stat: -rw-r--r-- 2,367 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
## Copyright (C) 2010 VZLU Prague, a.s., Czech Republic
## Copyright (C) 2010 Jean-Benoist Leger <jben@jben.info>
##
## 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 <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn{Function File} {} chunk_parcellfun (@dots{:})
## Undocumented internal function.
## @end deftypefn

function varargout = chunk_parcellfun (nproc, chunks_per_proc, func,
                                       error_handler, verbose_level,
                                       cumfunc, uniform_output,
                                       varargin)

  args = varargin;
  
  nchunks = chunks_per_proc * nproc;

  ## Compute optimal chunk sizes.
  N = numel (args{1});
  len_chunk = ceil (N/nchunks);
  chunk_sizes = len_chunk (ones(1, nchunks));
  chunk_sizes(1:nchunks*len_chunk - N) -= 1;

  ## Split argument arrays into chunks (thus making arrays of arrays).
  chunked_args = cellfun (@(arg) mat2cell (arg(:), chunk_sizes), args, ...
  "UniformOutput", false);

  ## Attach error handler if present.
  if (! isempty (error_handler))
    chunked_args = [chunked_args, {"ErrorHandler", error_handler}];
  endif

  ## Main call.
  [out_brut{1:nargout}] = parcellfun (nproc, func, chunked_args{:},
                                      "UniformOutput", false,
                                      "VerboseLevel", verbose_level,
                                      "CumFunc", cumfunc);

  ## Concatenate output args and reshape them to correct size.
  if (isempty (cumfunc))
    true_size = size (args{1});
    varargout = cellfun (@(arg) reshape (vertcat (arg{:}), true_size), out_brut, "UniformOutput", false);
  elseif (uniform_output)
    varargout = cellfun (@(arg) arg{:}, out_brut, "UniformOutput", false);
  else
    varargout = out_brut;
  endif

endfunction