Better parallelization than parfor?
古いコメントを表示
Hello,
I have a function fun(vec(1:n),Nmax) with two outputs: (i) a matrix g(1:n,1:Nmax) and (ii) a vector tau(1:Nmax), as Nmax eigenfunctions and eigenvalues of a matrix constructed within fun. Now I want to get this output for all values withing a vector vec2(1:n). The simplest way is a for-loop
for i=1:n
[g(1:n,1:Nmax,1:n),tau(1:n,1:Nmax)] = fun(vec(1:n),Nmax,vec2(i))
end
This is however slow. Replacing it with a parfor-loop
parfor i=1:n
[g(:,:,i),tau(i,:)] = fun(vec(:),Nmax,vec2(i))
end
is quite a bit faster. I was wondering if there is a way to accelerate this even further by threading over the vector vec2? Somehow I cannot find the correct way without rewriting the function fun in several ways. Maybe the problem is that g is already a matrix, therefore fun(vec,Nmax,vec2') does not evaluate things in the correct dimension?
3 件のコメント
Matt J
2020 年 1 月 27 日
I was wondering if there is a way to accelerate this even further by threading over the vector vec2?
Not sure what you mean. The code as you've presented it already does divide vec2 into parallel pieces. Nothing in what you've shown makes inefficient use of parfor that I can see. Anything slow would be in the details of how fun is implemented.
If i write sin(vec+vec'), the output is a nxn matrix, where n is the length of vec. However within this notation I am limited to automatic threading over only two dimensions.
The extension would be
sin(vec+vec.'+ reshape(vec,1,1,[]));
採用された回答
その他の回答 (1 件)
Philippe Lebel
2020 年 1 月 27 日
0 投票
maybe take a look at arrayfun().
カテゴリ
ヘルプ センター および 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!