使用 matlab 的 spmd 计算简单的三重积分给了我不正确的解决方案,对我做错了什么有什么想法吗?
close all; clear all; clc;
% Create a parallel pool if none exists
if isempty(gcp())
parpool();
end
nworkers = gcp().NumWorkers;
% Define the function
f = @(x,y,z) z
% Discretize the interval on the client
x = linspace(0,4,nworkers+1)
y = linspace(0,4,nworkers+1)
z = linspace(0,4,nworkers+1)
% On the workers
spmd
ainit = x(labindex())
bfin = x(labindex()+1)
cinit = y(labindex())
dfin = y(labindex()+1)
einit = z(labindex())
ffin = z(labindex()+1)
% locint = integral3(f,ainit,bfin,cinit,dfin,einit,ffin) % subinterval integration
locint = integral3(f,ainit,bfin,ainit,bfin,ainit,bfin) % subinterval integration
totalint = gplus(locint) % Add all values.
end
% Send the value back the client
totalvalue = totalint{1}
fun = @(x,y,z) z
q = integral3(fun,0,4,0,4,0,4)
q 是正确的解决方案。