Using matlab's spmd to compute simple triple integral is giving me incorrect solution, any thoughts on what I am doing wrong?
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 is the correct solution.
The problem here is that your spmd
block is dividing the 3-dimensional region to be integrated in each dimension, rather than just a single dimension. You need to pick a dimension in which to divide the integral, and vary the limits in only that dimension. For example, you could correct things by replacing your integral3
call inside spmd
with this:
spmd
...
locint = integral3(f,0,4,0,4,einit,ffin)
...
end