Question about Parfor-loop
3 次查看(过去 30 天)
显示 更早的评论
I do nonlinear fitting within a nested for loop as shown below:
for i=1:200
for j=1:170
for k=1:50
%do nonlinear fitting
[p,res] = lsqnonlin()
params(i,j,k,1:5)=[p,fix_par(i,j,k),res];
end
end
end
The above loop is extremely slow and I am trying to find ways to speed it up. I've read about the parlour-loop (which I am not familiar at all - I'm new to programming). I tried to use it but I got the following error "The variable params in a parfor cannot be classified." I tried something as below:
parfor i=1:200
parfor j=1:170
parfor k=1:50
%do nonlinear fitting
[p,res] = lsqnonlin()
params(i,j,k,1:5)=[p,fix_par(i,j,k),res];
end
end
end
3 个评论
Steven Lord
2018-4-11
Why do you need to perform 1.7 million individual fits? Perhaps there's a way to achieve your ultimate goal without calling lsqnonlin almost 2 million times. If you describe what you're trying to do we may be able to offer suggestions about a faster alternative approach.
回答(1 个)
Abhishek
2025-6-27
编辑:Abhishek
2025-6-28
You can definitely speed up your nonlinear fitting using 'parfor', and it’s great that you’re exploring parallel computing.
In MATLAB, ‘parfor’ can only be used on a single loop level (typically the outermost one), but you can still parallelize all elements by flattening your 3D loop into a single index using ‘ind2sub’.
The below code will help to accomplish the workaround:
N = [200, 170, 50]; N1 = 7;
M = prod(N);
params = zeros(M, N1);
parfor idx = 1:M
[i, j, k] = ind2sub(N, idx);
[p, res] = lsqnonlin(@(x) ModelFunctions(x, i, j, k), x0, lb, ub, options);
params(idx, :) = [p, fix_par(i, j, k), res];
end
params = reshape(params, [N,N1]);
This way, the workload is nicely distributed across workers, and you'll likely see a significant speed-up depending on your system resources. Just ensure that ‘fix_par’ is accessible in the correct format, ideally sliced by indices inside the loop.
Also make sure ‘ModelFunction’ is written to accept ‘(i, j, k)’ as context if needed. Moreover, avoid using shared/global variables; pass all required data explicitly.
This workaround efficiently distributes the fitting tasks across workers, often resulting in substantial runtime reductions for large data sets.
You can find additional information about ‘ind2’sub’ in the MATLAB’s official documentation: https://www.mathworks.com/help/releases/R2024b/matlab/ref/ind2sub.html
2 个评论
Walter Roberson
2025-6-27
params(i, j, k, :) = [p, fix_par(i, j, k), res];
That code will not work. Output variables must have one position indexed by a simple expression involving the parfor index, and all other indices to the output variable must be constants or the : operator. Furthermore, the output variable can be assigned to only once.
You can do something like
N = [200, 170, 50]; N1 = 7;
M = prod(N);
params = zeros(M, N1);
parfor idx = 1:M
[i, j, k] = ind2sub(N, idx);
[p, res] = lsqnonlin(@(x) ModelFunctions(x, i, j, k), x0, lb, ub, options);
params(idx, :) = [p, fix_par(i, j, k), res];
end
params = reshape(params, [N,N1]);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!