My GPU coder has wrong output.

1 view (last 30 days)
lim daehee
lim daehee on 4 Dec 2019
Answered: Walter Roberson on 4 Dec 2019
I'm studying GPU coder, and I have a problem with GPU coder.
I generated my code into MEX file stated below.
function [B_mat,R_mat] = fcn_Get_B(a) %#codegen
coder.gpu.kernelfun();
n = length(a);
B_mat = coder.nullcopy(ones(n));
R_mat = coder.nullcopy(ones(n));
coder.gpu.kernel;
for i=1:n
for j=1:n
R_mat(i,j)=B_mat(i,j)*3;
end
end
I intended that the result of B_mat is axa matrices and its elements are all 1 and R_mat is axa matrices and its elements are all 3. In CPU, the function worked well and the value B_mat and R_mat is as I thought. However, when I generated MEX file with this function, all elements of B_mat and R_mat became 0.
I wonder why this problem happened.
Does anybody who knows the way to solve my problem?

Answers (1)

Walter Roberson
Walter Roberson on 4 Dec 2019
X = coder.nullcopy(A) copies type, size, and complexity of A to X, but does not copy element values. The function preallocates memory for X without incurring the overhead of initializing memory. In code generation, the coder.nullcopy function declares uninitialized variables.
So your source matrix B_mat is uninitialized. It could contain anything . Containing 0 is as valid as anything else. It could contain 0xDEADBEEF

Categories

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

Community Treasure Hunt

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

Start Hunting!