如何在所有GPU线程上运行相同的MATLAB代码

因此,我正在编写用于ODE的Runge-Kutta解算器,该解算器具有初始条件并返回2个数组。我想在各种各样的随机初始条件下运行它,但是运行时间永远耗费时间,因此我尝试设置一个parfor循环,如下所示:

tic
parfor i = 1:10
    [y,py] = RK(rand,rand,rand);
end
toc

我看到速度提高了约80%。现在,我该如何做,但我选择GPU线程而不是CPU线程?

这是整个代码:

function [y,py] = RK(x1,y1,px1,py1)
%setting time
Time = 100;
dt = 0.01;
N = floor(Time/dt);

%Equations
f1 = @(x,y,px,py) px;
f2 = @(x,py) py;
f3 = @(x,py) -x -2*x*y;
f4 = @(x,py) -y -x^2 + y^2;

%Initializing the matrix that will store the values of the weights
c = zeros(4,4);
x = zeros(1,N);
y = zeros(1,N);
px = zeros(1,N);
py = zeros(1,N);

x(1) = x1; y(1) = y1; px(1) = px1; py(1) = py1;

for i = 1:N-1
    c(1,1) = dt*f1(x(i),y(i),px(i),py(i));
    c(2,1) = dt*f2(x(i),py(i));
    c(3,1) = dt*f3(x(i),py(i));
    c(4,1) = dt*f4(x(i),py(i));

    c(1,2) = dt*f1(x(i) + 0.5*c(1,1),y(i) + 0.5*c(2,px(i) + 0.5*c(3,py(i) + 0.5*c(4,1));
    c(2,2) = dt*f2(x(i) + 0.5*c(1,1));
    c(3,2) = dt*f3(x(i) + 0.5*c(1,1));
    c(4,2) = dt*f4(x(i) + 0.5*c(1,1));

    c(1,3) = dt*f1(x(i) + 0.5*c(1,2),2));
    c(2,3) = dt*f2(x(i) + 0.5*c(1,2));
    c(3,3) = dt*f3(x(i) + 0.5*c(1,2));
    c(4,3) = dt*f4(x(i) + 0.5*c(1,2));

    c(1,4) = dt*f1(x(i) + c(1,3),y(i) + c(2,px(i) + c(3,py(i) + c(4,3));
    c(2,4) = dt*f2(x(i) + c(1,3));
    c(3,4) = dt*f3(x(i) + c(1,3));
    c(4,4) = dt*f4(x(i) + c(1,3));

    x(i+1) = x(i) + (1/6)*(c(1,1) + 2*c(1,2) + 2*c(1,3) +c(1,4));
    y(i+1) = y(i) + (1/6)*(c(2,1) + 2*c(2,2) + 2*c(2,3) +c(2,4));
    px(i+1) = px(i) + (1/6)*(c(3,1) + 2*c(3,2) + 2*c(3,3) +c(3,4));
    py(i+1) = py(i) + (1/6)*(c(4,1) + 2*c(4,2) + 2*c(4,3) +c(4,4));
end

zer = find(x<0.00001 & x>-0.00001);
y = y(zer);
py = py(zer);
end
zzlq55 回答:如何在所有GPU线程上运行相同的MATLAB代码

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2954050.html

大家都在问