why the caculation results is different for parfor-loop and for-loop?

2 ビュー (過去 30 日間)
Menghui Chen
Menghui Chen 2021 年 9 月 23 日
編集済み: Matt J 2021 年 9 月 27 日
load('matlab.mat')
for i =1:1
b_OLS(i) = X_*y;
end
parfor i =1:1
b_OLS_(i) = X_*y;
end
error = b_OLS_-b_OLS
The results is:
error =
1.8190e-11
why the caculation results is different for parfor-loop and for-loop?
  1 件のコメント
Stephen23
Stephen23 2021 年 9 月 27 日
Because the associative laws of algebra do not generally hold for floating-point numbers.

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2021 年 9 月 23 日
編集済み: Matt J 2021 年 9 月 27 日
Probably because, with a parpool active, the matrix multiplication code cannot multithread the operation in precisely the same way. The vectors are split into parallel blocks of one size if a parpool is open and another size if not.
  1 件のコメント
Edric Ellis
Edric Ellis 2021 年 9 月 27 日
In particular, by default for process-based parpool, the workers run in single-computational-thread mode. This can definitely result in slightly different results compared to multithreaded mode. You can use maxNumCompThreads(1) to put the client in single-computational-thread mode to check. Like this:
A = rand(1,10000);
B = rand(10000,1);
maxNumCompThreads(4);
c1 = A*B;
maxNumCompThreads(1);
c2 = A*B;
c1-c2
ans = -4.5475e-13

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by