SPMD Error when trying to use the data outside of the SPMD loop

7 views (last 30 days)
if true
% code
tic;
arraySize = 24;
tArray = [1:arraySize];
spmd
startIdx = (labindex-1)*(arraySize/numlabs) + 1;
endIdx = startIdx + (arraySize/numlabs) - 1;
for index = startIdx:endIdx
for throw_1 = 1:1:6
for throw_2 = 1:1:6
RollTotal = throw_1 + throw_2;
NumbersThrownCount(RollTotal) = NumbersThrownCount(RollTotal) + 1;
Probability = NumbersThrownCount(Throw) / sum(NumbersThrownCount) * 100;
end
end
end
end
toc
Throw = input('For which throw would you like the probability?');
if Throw > 24 fprintf("Sorry but the throw you want the probability for is invalid please try again\n"); Throw = input('For which throw would you like the probability?'); end
fprintf("The probability of throwing a %d is %f %%\n", Throw, Probability);
Error using SPMD (line 6) Error detected on workers 2 3.
Caused by: Error using SPMD (line 6) An UndefinedFunction error was thrown on the workers for 'NumbersThrownCount'. This may be because the file containing 'NumbersThrownCount' is not accessible on the workers. Specify the required files for this parallel pool using the command: addAttachedFiles(pool, ...). See the documentation for parpool for more details. Unrecognized function or variable 'NumbersThrownCount'.
What I am trying to do is get the data from the SPMD loop so that I can then ask the user what the probability is rolling 'x' value on the dice. The error I get is shown below any help with this would be great as I am really stuck with this. Thanks in advance

Answers (1)

Raymond Norris
Raymond Norris on 22 May 2021
I would suggest reformatting your post so that all your code is formatted well. Code is not correctly indented and some of the code is text [e.g., Throw = input('For which throw would you like the probability?'); ]. This will make it easier to read.
On line 13, you reference NumbersThrownCount on the right hand side of the equation. Is this a function or a variable? Since you also index into it on the left hand side, I'll assume a variable. Given that, where in your code are you initializing NumbersThrownCount? In the end, that's what the error is stating.
  2 Comments
Dean Morris
Dean Morris on 22 May 2021
Hi Raymond thanks for your reply, you are correct that it is a variable. The initializing is what i was having trouble with as I was unsure how to do this. Where would I initialize NumbersThrownCount so that it can be used in the spmd loop and also be used to calculate and display the probability outside the loop. Thanks in advance.
Raymond Norris
Raymond Norris on 22 May 2021
You'll need to define NumberThrownCount outside spmd (as presumably it's already define).

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!