Undefined function ' ' for input arguments of type 'gpuArray'. - Why ?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
So Im using Paralell Computing Toolbox v. 23.2, on R2023b and having this problem when passing an array from one function to another, and can't seem to understand what is causing this.
My nested loop looks like this, everything is initiated correctly in its running, except for when I add the nu_t term.
for t_n = 1:t-1
for i = 2:length(X) - 1
for j = 2:length(Y) - 1
nu_t = addTurbulentViscotiy(U_temp, C_s, Delta)
end
end
U_temp = applyNoSlipBoundary(U_temp, t_n);
P_temp = applyNeumannBoundary(P_temp, t_n);
end
And the function that doesnt work looks like this:
function [nu_t] = addTurbulentViscotiy(U, C_s, Delta)
% Computing the strain rate tensor components for S
S_11 = (U(t_n, i+1, j, 1) - U(t_n, i-1, j, 1)) / (2 * Delta);
S_22 = (U(t_n, i, j+1, 2) - U(t_n, i, j-1, 2)) / (2 * Delta);
S_12 = 0.5 * ((U(t_n, i, j+1, 1) - U(t_n, i, j-1, 1)) / (2 * Delta) ...
+ (U(t_n, i+1, j, 2) - U(t_n, i-1, j, 2)) / (2 * Delta));
S_21 = S_12;
% Calculating the magnitude of S
S_mag = sqrt(S_11.^2 + S_22.^2 + 2* S12.^2);
% Calculating the turbulent Viscocity
nu_t = (C_s*Delta).^2*S_mag
end
However, these function work. Its the same values being passed:
function [U_bound] = applyNoSlipBoundary(U_field, t_n)
U_field(t_n + 1, 1, :, :) = 0; % Top boundary
U_field(t_n + 1, end, :, :) = 0; % Bottom boundary
U_field(t_n + 1, :, 1, :) = 0; % Left boundary
U_field(t_n + 1, :, end, :) = 0; % Right boundary
U_bound = U_field;
end
% Apply Neumann boundary conditions (zero gradient)
function [P_bound] = applyNeumannBoundary(P_temp, t_n)
P_temp(t_n + 1, 1, :) = P_temp(t_n + 1, 2, :); % Top boundary
P_temp(t_n + 1, end, :) = P_temp(t_n + 1, end-1, :); % Bottom boundary
P_temp(t_n + 1, :, 1) = P_temp(t_n + 1, :, 2); % Left boundary
P_temp(t_n + 1, :, end) = P_temp(t_n + 1, :, end-1); % Right boundary
P_bound = P_temp;
end
The error gotten is
Undefined function 'NSsolverTest2' for input arguments of type 'gpuArray'.
The functions are in the same file, altough not nested.
Why do they work in one function but not the other? What is causing this I looked through the documentations but can't find anything there. The only difference is where in the loop theyre being called. Anybody that could help me figure this out?
Thanks in advance!
1 Kommentar
Joss Knight
am 18 Mai 2024
Are you sure this function is spelled correctly where you are calling it? Try selecting the function and hitting Ctrl-D.
Antworten (1)
Nipun
am 13 Jun. 2024
Hi Elias,
I understand that you are having trouble with the "Parallel Computing Toolbox" in MATLAB when passing arrays between functions, particularly when adding the "nu_t" term in your nested loop. The error "Undefined function 'NSsolverTest2' for input arguments of type 'gpuArray'" indicates that the function "addTurbulentViscosity" may not be compatible with GPU arrays. Here's how you can address this issue:
- Ensure GPU Compatibility: Ensure that the functions and operations within "addTurbulentViscosity" are compatible with GPU arrays.
- Debugging with CPU Arrays: Test the code with CPU arrays to identify if the issue is specifically related to GPU arrays.
- Check Function Scope: Make sure the function "addTurbulentViscosity" is accessible within the scope where it is called.
Here's a modified version of your code with added checks and changes for GPU compatibility:
% Main loop with added checks
for t_n = 1:t-1
for i = 2:length(X) - 1
for j = 2:length(Y) - 1
nu_t = addTurbulentViscosity(U_temp, C_s, Delta, t_n, i, j);
end
end
U_temp = applyNoSlipBoundary(U_temp, t_n);
P_temp = applyNeumannBoundary(P_temp, t_n);
end
% Updated function with additional arguments for indices
function [nu_t] = addTurbulentViscosity(U, C_s, Delta, t_n, i, j)
% Ensure U is on the CPU if it's a GPU array
if isa(U, 'gpuArray')
U = gather(U);
end
% Computing the strain rate tensor components for S
S_11 = (U(t_n, i+1, j, 1) - U(t_n, i-1, j, 1)) / (2 * Delta);
S_22 = (U(t_n, i, j+1, 2) - U(t_n, i, j-1, 2)) / (2 * Delta);
S_12 = 0.5 * ((U(t_n, i, j+1, 1) - U(t_n, i, j-1, 1)) / (2 * Delta) ...
+ (U(t_n, i+1, j, 2) - U(t_n, i-1, j, 2)) / (2 * Delta));
S_21 = S_12;
% Calculating the magnitude of S
S_mag = sqrt(S_11.^2 + S_22.^2 + 2 * S_12.^2);
% Calculating the turbulent Viscocity
nu_t = (C_s * Delta).^2 * S_mag;
end
% Ensure other functions are also compatible with CPU arrays
function [U_bound] = applyNoSlipBoundary(U_field, t_n)
if isa(U_field, 'gpuArray')
U_field = gather(U_field);
end
U_field(t_n + 1, 1, :, :) = 0; % Top boundary
U_field(t_n + 1, end, :, :) = 0; % Bottom boundary
U_field(t_n + 1, :, 1, :) = 0; % Left boundary
U_field(t_n + 1, :, end, :) = 0; % Right boundary
U_bound = U_field;
end
function [P_bound] = applyNeumannBoundary(P_temp, t_n)
if isa(P_temp, 'gpuArray')
P_temp = gather(P_temp);
end
P_temp(t_n + 1, 1, :) = P_temp(t_n + 1, 2, :); % Top boundary
P_temp(t_n + 1, end, :) = P_temp(t_n + 1, end-1, :); % Bottom boundary
P_temp(t_n + 1, :, 1) = P_temp(t_n + 1, :, 2); % Left boundary
P_temp(t_n + 1, :, end) = P_temp(t_n + 1, :, end-1); % Right boundary
P_bound = P_temp;
end
For more information on gpuArray, refer to the following MathWorks documentation: https://www.mathworks.com/help/parallel-computing/gpuarray.html
Hope this helps.
Regards,
Nipun
0 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with GPU Coder finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!