Does GPU coder have only one output?

I'm changing a function into mex file with GPU coder, and I have a problem while I using the GPU coder.
I want to get two matrices; D, N as the output of fcn_DH.
When I executed GPU coder with a code below, Running test file with MEX is not terminated, thus I cannot make MEX file.
However, When I changed the first line of the function into 'D = fcn_PRM(node,PosMap)', which has only one output, the GPU coder worked fine.
I'm wondering if I can apply the GPU coder to a function and only get one variable as a result.
Otherwise, if GPU coder can multiple outputs, I want to know which part of my code is wrong.
function [D,N] = fcn_PRM(node,PosMap)
n = length(node);
D = coder.nullcopy(zeros(n,n,2));
N = coder.nullcopy(zeros(n,n));
len=(1:n)';
pos_mat=PosMap(node(len),:);
coder.gpu.kernel;
for i=1:n-1
for j=i+1:n
dist=sqrt((pos_mat(i,1)-pos_mat(j,1))^2+(pos_mat(i,2)-pos_mat(j,2))^2+(pos_mat(i,3)-pos_mat(j,3))^2);
B = collisionCheck_SP5(PosMap(node(i),:), PosMap(node(j),:));
D(i,j,1) = dist;
N(i,j) = B;
end
end

6 Comments

Hello Lim,
It would be helpful if you provide the test file? You can consider the sample data set for 'node', 'PosMap'.
Hi Pravin,
I attach the sample data set for 'node' and 'PosMap'.
I hope this file helps solve my problem..
Hello Lim,
I am not able to reproduce the error because the 'collisionCheck_SP5' variable which you used in the 'fcn_PRM' function is not recognized.
Also, provide the caller MATALB file which you used for generating the MEX which will help in debugging the issue.
~Pravin
Hello Parvin,
First of all, thank you for you reply and I'm sorry for my insufficient information.
Here is the code collisionCheck_SP5 and I attach the map file dlite_small.mat.
function B = collisionCheck_SP5(p1, p2)
P1=[round(p1(1)/0.3+1) round(p1(2)/0.3+1) round(p1(3)/0.3+1)];
P2=[round(p2(1)/0.3+1) round(p2(2)/0.3+1) round(p2(3)/0.3+1)];
chk_p=abs(P1-P2);
if (chk_p(1)>=chk_p(2)) && (chk_p(1)>=chk_p(3))
if P1(1)<P2(1)
X=(P1(1):P2(1))';
else
X=(P2(1):P1(1))';
end
elseif (chk_p(2)>chk_p(1)) && (chk_p(2)>=chk_p(3))
if P1(2)<P2(2)
X=(P1(2):P2(2))';
else
X=(P2(2):P1(2))';
end
else
if P1(3)<P2(3)
X=(P1(3):P2(3))';
else
X=(P2(3):P1(3))';
end
end
% B=1;
B=size(X,1);
Also, I don't know what the caller MATLAB file means.. so I attach the main code which calls fcn_PRM.
clc
clear
close all
%% Map Load
load dlite_small.mat
%% Parameter Setting
NodeNum = 1000;
DrawCHK =1;
Vec{1} = unique(PosMap(:,1),'sorted');
Vec{2} = unique(PosMap(:,2),'sorted');
Vec{3} = unique(PosMap(:,3),'sorted');
%% Initialize condition
% WP Sett
num_goals=1:size(WP,1); % Waypoint Num
com_goals=combnk(num_goals,2); % Combination of route Num
WP_N = size(WP,1);
Obs_mat=make_obsmap(Map_Obs);
%% PRM
tic
% Initialization
idx_FS = find(Map_Obs == 1);
idx_temp=randperm(NodeNum)';
node_PRM = idx_FS(idx_temp);
[D,N] = fcn_PRM(node_PRM,PosMap);
MapGenTime=toc
Hello Lim,
Refer to following documentation to understand GPU coder workflow:
For code generation, we need to define 'primary' function which is 'fcn_PRM' in your case and 'entry-point function' which is used for the defining the input variables.
Still earlier issue persists since you are loading the data from ''dlite_small.mat'' which is not accessible to me. Also, please verify the code and provide all the necessary inputs for reproducing the error by executing the code.
Hello Parvin,
Thank you for your continuous help.
In my case, the entry-point function is the main code, which starts with loading the dlite_small.mat file. I followed all procedures of GPU coder app on documentation you send to me. However, GPU coder app does not complete at a certain stage on attached figure.
Also, I don't understand why are you cannot accessible to "dlite_small.mat"file. I just confirmed that all files and codes are work well on my computer now. So I attach dlite_small.mat and all codes.
Thanks for your help.
Sincerely.

Sign in to comment.

 Accepted Answer

Hello Lim,
After analyzing the code, It is clear that you are not able to generate ‘MEX’ because the ‘%#codegendirective is missing in the function definition at beginning. To generate the ‘MEX’ please add the ‘%#codegen’ directive as shown below at the starting of the Entry-Point function:
function [D,N] = fcn_PRM(node_PRM,PosMap) %#codegen
%…
%…
end
Also, make sure that the same function is getting called in the ‘Define Input Type’ stage.
Kind Regards
~Pravin

3 Comments

lim daehee
lim daehee on 25 Nov 2019
Edited: lim daehee on 25 Nov 2019
Hello Parvin,
I added the'%#codegen' directive at the starting of the Entry-point function as you mentioned. After that, I confirmed that MEX file is successfully generated. I'm very appreciate that. Thank you for your help.
However, in the running test file with MEX preocedure, the elapsed time is resulted about 235 seconds while the elapsed time is resulted only 0.13seconds when the output of fcn_PRM is only D.
I wonder why does the difference of elapsed time between one output condition and multiple output condition is so big.
Can you explain why this difference is happened? I attach two cases as figures;single output and multiple output.
I'm very sorry to bother you with my incompetence.
Sincerely.
Hello Lim,
One of the possible reasons can be the data copy from Device to Host which can add extra overhead and the size of matrix is also considerable in your case. There can be other possible reasons as well.
~Pravin
lim daehee
lim daehee on 27 Nov 2019
Edited: lim daehee on 27 Nov 2019
Hello Parvin,
I always thanks to you for your help and I have a question about your reply.
Is the matrix size can cause the increase of calculation time?
The map data which I gave you is smaller one.. actual map size is 1701*1701*11 double and size of 'node' is 1900.
If my problem is caused by the size of the map data matrix, I want to know the maximum size of matrix that GPU coder can work properly.
Also, I know that GPU coder don't create kernel for indexing of matrix, is that right?
Plus, I realized that my problem is caused by data size of array N. The data size of N is 576kb. I wonder why the size of array cause the increase of calculation time.
Also, I wonder both maximum size of array and maximum data size of array that GPU coder can work properly.
Sincerely.
Lim.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with GPU Coder in Help Center and File Exchange

Products

Release

R2019b

Asked:

on 22 Nov 2019

Edited:

on 27 Nov 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!