Is cellfun multithreaded or does it do the operation one cell at a time?
古いコメントを表示
Does the cellfun function perform the function on all the cells simultaneously, using multiple cores if available, or does it just do one cell at a time? It seems like the process takes much longer than it should, and I don't think it's using multiple cores.
Is there some way to make cellfun multithreaded? I have the Parallel computing toolbox, although I'm not using it in this code. I'd hate to have to write everything in parfor loops just to get parallel processing.
4 件のコメント
Jan
2011 年 2 月 17 日
You hate to parallelize the program? Why? It makes programs faster. I like it.
Walter Roberson
2011 年 2 月 18 日
Switching to parfor() is, I've read, slower even if there is no pool open.
Sean de Wolski
2011 年 2 月 18 日
That is true Walter.
Shinya
2020 年 11 月 19 日
Using parfor just has a larger overhead. If the benefit of parallelization can overcome it, it will still be useful. Please see my answer below. In that implementation, my function that uses parfor is faster if the function spends roughly 100ms.
回答 (3 件)
Walter Roberson
2011 年 2 月 16 日
cellfun(@disp,num2cell(1:10))
gives a perfect 1 through 10 listing for me. Mind you I don't have a pool opened. It's a simple enough test to make though.
I would not expect cellfun to be multithreaded at the current time.
2 件のコメント
Kenneth Eaton
2011 年 2 月 18 日
Although, the documentation for CELLFUN does say this: "The order in which cellfun computes elements of A is not specified and should not be relied upon." So I guess the behavior you see is not guaranteed.
Matt Tearle
2011 年 2 月 18 日
IANA developer, so this is wild speculation, but I think Walter's observation is guaranteed, because nothing in disp could change any elements of the cell. I suspect that cellfun may take the cell elements in some non-monotonic order behind the scenes, but then compile the results (again, behind the scenes). With something like parfor, OTOH, you see things in the order they actually happened.
Matt Tearle
2011 年 2 月 16 日
3 投票
Currently cellfun is not multithreaded.
1 件のコメント
Royi Avital
2011 年 2 月 18 日
Moreover it is slower than a loop along the array.
As others noted, 'cellfun' is single threaded, but you can write a parallel version of 'cellfun' relatively easily.
function result = cellfunp(func, c, varargin)
% Parallel version of cellfun that uses parfor inside
p = inputParser;
addParameter(p, 'UniformOutput', 1, @isscalar);
parse(p, varargin{:});
result = cell(size(c));
parfor i = 1:numel(c)
result{i} = func(c{i});
end
if p.Results.UniformOutput % uniform
result = cell2mat(result);
end
This method has relatively large overhead (~0.1s on my computer), but will be useful if 'func' is time consuming and the benefit overcomes this overhead. Make sure that func is a function handle.
4 件のコメント
Walter Roberson
2020 年 11 月 19 日
You will, however, have a problem if the function handle does not return any value.
c = arrayfun(@(varargin) randi([-9 9]), 1:20, 'uniform', 0); %just SOME cell
result = cellfunp(@(X) X.^2 - X.^3, c)
function result = cellfunp(func, c, varargin)
% Parallel version of cellfun that uses parfor inside
p = inputParser;
addParameter(p, 'UniformOutput', 1, @isscalar);
parse(p, varargin{:});
result = cell(size(c));
parfor i = 1:numel(c)
result{i} = func(c{i});
end
if p.Results.UniformOutput % uniform
result = cell2mat(result);
end
end
Andrea
2023 年 3 月 1 日
Thanks! Really appreciate the help.
カテゴリ
ヘルプ センター および File Exchange で Parallel for-Loops (parfor) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!