Quadratic programming for GPU computing
6 次查看(过去 30 天)
显示 更早的评论
Is anybody aware of script that solves a quadratic programming or nonlinear problem written for GPUs? It would be great to have sth similar to fsolve, lsqnonlin or fmin(un)con.
0 个评论
回答(1 个)
Matt J
2019-6-23
编辑:Matt J
2019-6-23
You are free to use gpuArray variables within your objective function, and in constraint functions in the case of fmincon. Unfortunately, you are required to ensure that inputs and outputs of these functions are CPU variables, and so you must do CPU/GPU transfers at the beginning and end of each call, killing a lot of the potential speed-up of the GPU. I have pointed out this weakness to Matlab staff and was told they would look into an enhancement.
Still, you can keep any large fixed data like matrices on the GPU throughout the optimization, and that will reduce the overhead. In the example below, I was able to get a 30% acceleration on the GPU using this approach, for sufficiently large problem size, N. Note that I would never normally recommend that you use the nonlinear constraint function in fmincon to implement linear constraints. In this case, though, it is a necessary workaround to get them implemented on the GPU.
function test
N=10000; %problem size
H=eye(N);
Aeq0=ones(1,N); %CPU copy
Aeq=Aeq0;
beq=1;
options=optimoptions(@fmincon,'Display','off','SpecifyObjectiveGradient',true,...
'SpecifyConstraintGradient',true);
x0=5*rand(N,1)+10;
%% Run on CPU
objfun=@(x) objective(x,H);
confun=@(x) linconFAKE(x,Aeq0,Aeq,beq);
tic;
fmincon(objfun,x0,[],[],[],[],[],[],confun,options);
toc
%Elapsed time is 18.771326 seconds.
%% Run on GPU
[H,Aeq,beq]=deal(gpuArray(H), gpuArray(Aeq), gpuArray(beq));
objfun=@(x) objective(x,H);
confun=@(x) linconFAKE(x,Aeq0,Aeq,beq);
tic;
fmincon(objfun,x0,[],[],[],[],[],[],confun,options);
toc
%Elapsed time is 12.546122 seconds.
end
function [f,g]=objective(x,H)
g=H*x(:);
f=(x.'*g)/2;
f=gather(f);
if nargout>1
g=gather(g);
end
end
function [c,ceq,g,geq]=linconFAKE(x,Aeq0,Aeq,beq)
c=[];
ceq=gather(Aeq*x-beq);
if nargout>2
g=[];
geq=Aeq0(:); %Use CPU copy
end
end
2 个评论
Matt J
2019-6-23
rokP's reply moved here:
Thanks for the info and sharing your problem. I am using scalars within the objective function, so would not gain anything by the suggested setup. I tried to modify the function LMFsolve from file exchange to work on GPU, but there are bunch of funcs (feval, diag, any,...) that are not supported for GpGPU computing. I hope Mathworks fix it. I'll go and try it on py and openCL.
Joss Knight
2019-7-6
I'm reading your comment out of context, but I can respond that DIAG and ANY do support gpuArray inputs. So does feval of course, depending on what function you're evaluating.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 GPU Computing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!