You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab中fmincon+parfor场景下,调度空闲Worker加速慢迭代的方案问询

问题:Parfor与Fmincon并行协作优化不动点迭代效率

问题背景

在Matlab中使用fmincon进行不动点迭代,通过parfor实现多线程并行。但parfor循环中第n次fmincon迭代因数据特性运行极慢,导致其余Worker全部进入空闲状态等待其完成,整体耗时过长。

当前代码逻辑

error = 10;
maxIter = 5;
tol = 1e-6;
iter = 0;
solly = [];

options = optimoptions('fmincon','Algorithm','sqp', 'TolConSQP', 1e-8, ...
'EnableFeasibilityMode', true, 'UseParallel',false); 
    
while error > tol & t < maxIter
    t= t+1;
    error = error/10;

    parfor j = 1:n
        [sol, fval, exitflag, output] = fmincon(obj, x0, [], [], [], [], lb, ub, nonlcon, options);
        solly(j).sol = sol;
    end

    solstacked.solly(t) = solly;
end

核心需求

调整parfor逻辑:当其他Worker空闲时,让它们协作处理第n次迭代——将fmincon的UseParallel设为true(parfor中原本需设为false)。本质上,parfor调用并行fmincon实例时,fmincon迭代需串行执行,多数Worker空闲时会形成瓶颈。

注意:不动点迭代必须等待第t轮全部完成才能进入t+1轮,否则无法收敛到正确解;同时存在第n次迭代启动时,仍有Worker未完成前序任务的情况。

设想的实现逻辑(未成功)

尝试实现如下逻辑,但因Matlab监控Worker的限制,以及无法将fmincon当前迭代次数传递到外层循环(除非用persistent机制,但不确定可行性),尝试后未成功:

if j == n && numidleworkers == numtotalworkers - 1 
    if [当前正在运行j==n的任务,且该任务启动时numidleworkers == numtotalworkers -1不成立,同时当前fmincon迭代次数 < fminconiterThreshold]
        % 中断当前任务,回到外层j==n的判断逻辑,重新启动第n次迭代
        break;
    else
        options2 = optimoptions('fmincon','Algorithm','sqp', 'TolConSQP', 1e-8, ...
        'UseParallel',true); 
        [sol, fval, exitflag, output] = fmincon(obj, x0, [], [], [], [], lb, ub, nonlcon, options2);
    end
else
    [sol, fval, exitflag, output] = fmincon(obj, x0, [], [], [], [], lb, ub, nonlcon, options);
end
% 后续parfor/不动点迭代逻辑

请问该逻辑是否可行?若可行,如何解决Worker监控和fmincon迭代次数传递的问题?

内容的提问来源于stack exchange,提问作者ZZ Top

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:45:55