Using matlab's spmd to compute simple triple integral is giving me incorrect solution, any thoughts on what I am doing wrong?

2 views (last 30 days)
close all; clear all; clc;
% I want to do this triple integral using SPMD
fun = @(x,y,z) z
totalval_s = integral3(fun,0,4,0,4,0,4)
% So I know the answer is 128, and integral3 works well
%
% Now I want to do the same triple integral but using SPMD
% 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()) %left point of subinterval
bfin = x(labindex()+1) %right point of subinterval
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
totalint = gplus(locint) % Add all values.
end
% Send the value back the client
totalvalue_spmd = totalint{1}
% However, the answer, totalvalue_spmd = 32, is incorrect.
% I am novice at using SPMD, need help troubleshooting what I did wrong
I want to break the three intervals into two subintervals each, and use integral3 in spmd to integrate in each dimension.
  2 Comments
Austin Taylor
Austin Taylor on 20 Oct 2020
Hi Raymond, thank you for the reply.
I want to do this triple integral:
fun = @(x,y,z) z
q = integral3(fun,0,4,0,4,0,4)
in MATLAB's spmd, using two cores, one doing subinterval [0,2] and the other doing [2,4] for each integrand of the triple integral.
The last two lines of my script shows the expected answer, 128.
I appreciate your time,
Austin

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Oct 2020
You integrate the first section of x with the first section of y and the first section of z, and the second section of x with the second section of y and the second section of z, and so on.
You never integrate the first section of x with the third section of y and the second section of z.
  2 Comments
Austin Taylor
Austin Taylor on 20 Oct 2020
Edited: Austin Taylor on 20 Oct 2020
Hi Robert, thank you for your reply.
May you please exand on your reply? I am new to using MATLAB's spmd, so the last to lines of my script give me the expected answer of 128. How do I make sure to integrate in correct orders?
I initially was going to write this line that I commented out:
% locint = integral3(f,ainit,bfin,cinit,dfin,einit,ffin) % subinterval integration
However, I was getting the wrong solution, 32.
This following script works, but I want to be able to use MATLAB's spmd, using two cores, one doing subinterval [0,2] and the other doing [2,4] for each of the three integrands that make up the triple integral, not just one like I am using here (but this script gives correct 128 solution):
close all; clear all; clc;
syms f
% 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
z = linspace(0,4,nworkers+1)
% double(max([x,y]))
% 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,0,4,0,4,einit,ffin)
% 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)
I appreciate your time,
Austin
Walter Roberson
Walter Roberson on 20 Oct 2020
I want to be able to use MATLAB's spmd, using two cores, one doing subinterval [0,2] and the other doing [2,4] for each of the three integrands that make up the triple integral, not just one like I am using here
Then you would need 8 cores. Each core should decode (labindex minus one) to binary, add one to each digit result (getting 1 or 2), and use the first value to select the half-interval for x, the second value to select the half-interval for y, and the third value to select the half-interval for z.
More generally, for N sub-intervals, you need N^3 cores and each lab should decode (labindex-1) to a base N number, add one to each digit, and use that as an index to determine the N'th portion of the interval for x, y, z.
selectors = dec2base(labindex-1, N, 3) - '0' + 1;
ainit = x(selectors(1));
bfin = x(selectors(1)+1);
cinit = y(selectors(2));
dfin = y(selectors(2)+1);
einit = z(selectors(3));
ffin = z(selectors(3)+1);

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!