X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fgeneral-1.3.1%2Fprivate%2Fchunk_parcellfun.m;fp=octave_packages%2Fgeneral-1.3.1%2Fprivate%2Fchunk_parcellfun.m;h=ef4922f821e4f87796c8b762aaeee4e6c9815d71;hp=0000000000000000000000000000000000000000;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/general-1.3.1/private/chunk_parcellfun.m b/octave_packages/general-1.3.1/private/chunk_parcellfun.m new file mode 100644 index 0000000..ef4922f --- /dev/null +++ b/octave_packages/general-1.3.1/private/chunk_parcellfun.m @@ -0,0 +1,53 @@ +## Copyright (C) 2010 VZLU Prague, a.s., Czech Republic +## Copyright (C) 2010 Jean-Benoist Leger +## +## 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} {} chunk_parcellfun (@dots{:}) +## Undocumented internal function. +## @end deftypefn + +function varargout = chunk_parcellfun (nproc, chunks_per_proc, func, + error_handler, verbose_level, 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); + + ## Concatenate output args and reshape them to correct size. + true_size = size (args{1}); + varargout = cellfun (@(arg) reshape (vertcat (arg{:}), true_size), out_brut, "UniformOutput", false); + +endfunction +