Faster DP_fiber with GPU processing... Run test\gpu_cpu_comparison.m to see difference on your setup

This commit is contained in:
silas (home)
2026-02-01 13:53:20 +01:00
parent 67689bb70f
commit fba7cbdba2
15 changed files with 1281 additions and 217 deletions

View File

@@ -1,41 +1,72 @@
function [opt_out_x,opt_out_y,state] = CNLSE_plain(opt_in_x,opt_in_y,state)
function [opt_out_x,opt_out_y,state] = CNLSE_plain(opt_in_x,opt_in_y,state,useGPU,useSingle)
% GPU auto-detection if not specified
if nargin < 4 || isempty(useGPU)
useGPU = canUseGPU();
end
if nargin < 5 || isempty(useSingle)
useSingle = false;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pre calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pre calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
state.common_beta=struct('X',0,'Y',0);
state.common_beta=struct('X',0,'Y',0);
for n=1:2
% get current polarization name and contrary one
curPol = state.polNames{n};
for n=1:2
% get current polarization name and contrary one
curPol = state.polNames{n};
% extend linear transfer function depending on beta values for the current polarization
% Was ist der Sinn dieser komischen beta notation? zB. state.beta.X = [0.3142 0 -9.1105e-28 5.1068e-41]
for n_beta = 1:length(state.beta.(curPol))
state.common_beta.(curPol) = state.common_beta.(curPol) + state.beta.(curPol)(n_beta) * (state.omega).^(n_beta-1) / factorial(n_beta-1);
end
%opt_out_struct.(curPol)=opt_in_struct.(curPol).envelope;
% extend linear transfer function depending on beta values for the current polarization
% Was ist der Sinn dieser komischen beta notation? zB. state.beta.X = [0.3142 0 -9.1105e-28 5.1068e-41]
for n_beta = 1:length(state.beta.(curPol))
state.common_beta.(curPol) = state.common_beta.(curPol) + state.beta.(curPol)(n_beta) * (state.omega).^(n_beta-1) / factorial(n_beta-1);
end
beta_const = state.beta.('X')(1);
beta_1 = state.beta.('X')(2);
beta_2 = state.beta.('X')(3);
beta_3 = state.beta.('X')(4);
deltaomega = state.omega;
beta_x = beta_const + beta_1 * deltaomega + 1/2 * beta_2 * deltaomega.^2 + 1/6 *beta_3 * deltaomega.^3;
% opt_in_x = gpuArray(opt_in_x);
% opt_in_y = gpuArray(opt_in_y);
%opt_out_struct.(curPol)=opt_in_struct.(curPol).envelope;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Split Step Method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
beta_const = state.beta.('X')(1);
beta_1 = state.beta.('X')(2);
beta_2 = state.beta.('X')(3);
beta_3 = state.beta.('X')(4);
deltaomega = state.omega;
beta_x = beta_const + beta_1 * deltaomega + 1/2 * beta_2 * deltaomega.^2 + 1/6 *beta_3 * deltaomega.^3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GPU Transfer (if enabled)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if useGPU
% Convert to single precision if requested (faster on most GPUs)
if useSingle
opt_in_x = gpuArray(single(opt_in_x));
opt_in_y = gpuArray(single(opt_in_y));
state.common_beta.X = gpuArray(single(state.common_beta.X));
state.common_beta.Y = gpuArray(single(state.common_beta.Y));
state.brf.db1 = gpuArray(single(state.brf.db1));
state.brf.db0 = gpuArray(single(state.brf.db0));
for k = 1:numel(state.brf.matR)
state.brf.matR{k} = gpuArray(single(state.brf.matR{k}));
end
else
opt_in_x = gpuArray(opt_in_x);
opt_in_y = gpuArray(opt_in_y);
state.common_beta.X = gpuArray(state.common_beta.X);
state.common_beta.Y = gpuArray(state.common_beta.Y);
state.brf.db1 = gpuArray(state.brf.db1);
state.brf.db0 = gpuArray(state.brf.db0);
for k = 1:numel(state.brf.matR)
state.brf.matR{k} = gpuArray(state.brf.matR{k});
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Split Step Method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [opt_out_x,opt_out_y] = split_step_loop(state.L,opt_in_x,opt_in_y,state.gamma,state.SS_dzmin,state.SS_dzmax,state.SS_dphimax,state.alpha_lin,...
% state.lin_z_test,state.corr_length,state.n_plates_done,state.missing_dz,state.brf,state.common_beta,....
@@ -44,35 +75,33 @@ function [opt_out_x,opt_out_y,state] = CNLSE_plain(opt_in_x,opt_in_y,state)
% [opt_out_x,opt_out_y] = split_step_loop_mex(state.L,opt_in_x,opt_in_y,state.gamma,state.SS_dzmin,state.SS_dzmax,state.SS_dphimax,state.alpha_lin,...
% state.lin_z_test,state.corr_length,state.n_plates_done,state.missing_dz,state.brf,state.common_beta,....
% state.chi,state.manakov,state.beat_len);
% get nonlinear step size
[state.dz] = getNLstepsize(opt_in_x,opt_in_y,state.gamma,state.SS_dzmin,state.SS_dzmax,state.SS_dphimax,state.alpha_lin);
%[state.dz] = getNLstepsize_original(state,opt_out_struct);
state.n_step = 0;
state.z_prop = 0;
state.test_dz = [];
state.powers = [];
tic
% get nonlinear step size
[state.dz] = getNLstepsize(opt_in_x,opt_in_y,state.gamma,state.SS_dzmin,state.SS_dzmax,state.SS_dphimax,state.alpha_lin);
%[state.dz] = getNLstepsize_original(state,opt_out_struct);
while state.z_prop < state.L
state.n_step = 0;
state.z_prop = 0;
state.test_dz = [];
state.powers = [];
% reduce step length (dz) if we are to overshoot the fiber length
% (L) in the next step
if state.z_prop + state.dz > state.L
state.dz = state.L - state.z_prop;
end
% update step number (n)
state.n_step=state.n_step+1;
while state.z_prop < state.L
% append current step length to logbook (dzs)
state.dzs(state.n_step)=state.dz;
% reduce step length (dz) if we are to overshoot the fiber length
% (L) in the next step
if state.z_prop + state.dz > state.L
state.dz = state.L - state.z_prop;
end
% half linear step
[opt_in_x,opt_in_y,state.z_prop,state.lin_z_test,...
% update step number (n)
state.n_step=state.n_step+1;
% append current step length to logbook (dzs)
state.dzs(state.n_step)=state.dz;
% half linear step
[opt_in_x,opt_in_y,state.z_prop,state.lin_z_test,...
state.corr_length,state.n_plates_done,state.missing_dz,state.n_step,...
state.test_plates,state.test_plate_numbers,state.brf,state.common_beta.X,...
state.common_beta.Y,state.alpha_lin.X,state.alpha_lin.X]...
@@ -82,12 +111,12 @@ function [opt_out_x,opt_out_y,state] = CNLSE_plain(opt_in_x,opt_in_y,state)
state.test_plates,state.test_plate_numbers,state.brf,state.common_beta.X,...
state.common_beta.Y,state.alpha_lin.X,state.alpha_lin.X);
% complete nonlinear step
[opt_in_x,opt_in_y] = nl_step(opt_in_x,opt_in_y, state.dz, state.gamma, state.chi, state.manakov, state.beat_len ,state.alpha_lin.X, state.alpha_lin.Y);
% half linear step
[opt_in_x,opt_in_y,state.z_prop,state.lin_z_test,...
% complete nonlinear step
[opt_in_x,opt_in_y] = nl_step(opt_in_x,opt_in_y, state.dz, state.gamma, state.chi, state.manakov, state.beat_len ,state.alpha_lin.X, state.alpha_lin.Y);
% half linear step
[opt_in_x,opt_in_y,state.z_prop,state.lin_z_test,...
state.corr_length,state.n_plates_done,state.missing_dz,state.n_step,...
state.test_plates,state.test_plate_numbers,state.brf,state.common_beta.X,...
state.common_beta.Y,state.alpha_lin.X,state.alpha_lin.X]...
@@ -97,24 +126,34 @@ function [opt_out_x,opt_out_y,state] = CNLSE_plain(opt_in_x,opt_in_y,state)
state.test_plates,state.test_plate_numbers,state.brf,state.common_beta.X,...
state.common_beta.Y,state.alpha_lin.X,state.alpha_lin.X);
% get nonlinear step size
[state.dz] = getNLstepsize(opt_in_x,opt_in_y,state.gamma,state.SS_dzmin,state.SS_dzmax,state.SS_dphimax,state.alpha_lin);
%[state.dz] = getNLstepsize_original(state,opt_out_struct);
% get nonlinear step size
[state.dz] = getNLstepsize(opt_in_x,opt_in_y,state.gamma,state.SS_dzmin,state.SS_dzmax,state.SS_dphimax,state.alpha_lin);
%[state.dz] = getNLstepsize_original(state,opt_out_struct);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GPU Gather (if enabled)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if useGPU
opt_out_x = gather(opt_in_x);
opt_out_y = gather(opt_in_y);
% Gather state arrays back to CPU
state.common_beta.X = gather(state.common_beta.X);
state.common_beta.Y = gather(state.common_beta.Y);
state.brf.db1 = gather(state.brf.db1);
state.brf.db0 = gather(state.brf.db0);
for k = 1:numel(state.brf.matR)
state.brf.matR{k} = gather(state.brf.matR{k});
end
toc
else
opt_out_x = opt_in_x;
opt_out_y = opt_in_y;
end
opt_out_x = (opt_in_x);
opt_out_y = (opt_in_y);
% opt_out_x = gather(opt_in_x);
% opt_out_y = gather(opt_in_y);
end