parfor loop throws error for bigger matrices : Index must not exceed 0
2 次查看(过去 30 天)
显示 更早的评论
Hello, I am a newbie in parallel computing.
I am doing some calculation to genereate a 4D data. It worked fined while calculating a 4D matrix of small size(for e.g 2 × 3 × 4 × 100). But it then threw an error when the matrix sized was increased to 5 × 101 × 1001 × 101.
Below is my minimal code script.
'A' is the 4D matrix to be generated. The calculations necessary are done by my function 'bem4DParallel'.
So, at the innermost loop, a 1D array A_L is calculated. It is then stored in the 2D matrix A_K. This is done parallely for different values of k.
Then it is stored in 3D matix A_J and then finally in the 4D matrix A.
Computations for input_3 are parallelized. I didn't parallelize the innemost loop because to there is a dependency of the variable. That is, calculation of A_L(l) in the innermonst loop required the value of A_L(l-1). That's why I am passing the whole A_L array in the function while calculating.
% script name: main4DParallel.m
% inputs
input_1 = 0:5:20;
input_2 = 0:100;
input_3 = 500:1500;
input_4 = 0:0.01:2;
% 4D matrix to be calculated
A = zeros( length(input_1), length(input_2), length(input_3), length(input_4) );
for i = 1:length(input_1)
arg_1 = input_1(i);
A_J = zeros(length(input_2), length(input_3), length(input_4));
for j = 1:length(input_2)
arg_2 = input_2(j);
A_K = zeros(length(input_3), length(input_4));
% parallel computing
parfor k = 1:length(input_3)
arg_3 = input_3(k);
A_L = zeros(1,length(input_4));
for l = 1:length(input_4)
a = bem4DParallel(arg_1, arg_2, arg_3, input_4(l), A_L, l);
A_L(l) = a;
end
A_K(k,:) = A_L;
end
A_J(j,:,:) = A_K;
end
A(i,:,:,:) = A_J;
end
The script runs for some time(about 12 hours) and then gives the following error:
'main4DParallel' is the name of this script. And line 103 corresponds to the line where we have parfor k = 1:length(input_3)
After stopping, the values of the looping variables in the workspace are:
i = 1, j = 3. And there exists no k and l values.
Matrices:
A_L does not exist
2D matrix 'A_K' exist. And also data are present upto a row of 24. (Is it something to do with number of matlab workers?)
3D Matrix 'A_J' exist and contains value upto
A_J(2,:,:)
Make sense, because after stopping, j is at a value of 3
4D matrix A is completely empty.
Please let me know what I am doing wrong.
Thanks in advance
4 个评论
Harald
2023-7-28
Hi,
the next problem is that l does not exist in the function doSum. Let's assume that you want to use loopVar, so
m = Matrix(loopVar-1);
The next problem is that the loop over l starts at 1, meaning that you are falsely going to extract the 0-Element of Matrix. So, let me drop the "-1". Then, the provided example runs fine for me.
If that does not resolve the problem, I am afraid that it will be in bem4DParallel, and I recommend the following:
- Before returning the output, verify that it has the expected data type and dimension (double and scalar, I suppose).
- Wrap a try/catch - statement around the body of the function. In the catch portion, save the workspace of the function to a file. This will let you debug what the inputs were that led to the erroneous output, and thus fix the problem.
Best wishes,
Harald
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!