Error: Code generation does not support global variables that are sparse matrices or contain sparse matrices.
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
When I use GPU Coder APP to convert my mfile to CUDA, I see the below error.
"Code generation does not support global variables that are sparse matrices or contain sparse matrices."
There is an sparse Matrix in a function being called to the main function.
Is there a syntax I could use for sparse Matrices to fix the problem. MATLAB compacts sparse matrices which is not true for C/C++ which should be the cause of the problem.
Please advise,
Thanks
4 commentaires
Jan
le 10 Déc 2022
Modifié(e) : Jan
le 10 Déc 2022
It is easy to avoid global variables.
% Bad code design with global variables:
global a
fcn();
disp(a);
function fcn()
global a
a = 17;
end
% =============================================
% Without global variables:
fcn();
a = Storage('a');
disp(a);
function fcn()
a = 17;
Storage('a', a);
end
function value = Storage(key, value)
persistent S
if nargin == 1
try
value = S.(key);
catch ME
throw(ME);
end
elseif nargin == 2
S.(key) = value;
end
end
Now the "global" values are stored in a persistent variable instead of polluting the base workspace, where they can (and will!) collide with other Matlab applications.
You see, that the code is leaner with globals, but the debugging is a pure horror, if you have to find out, which part of the code is responsible for the last change of a global variable. With the Storage() function, you can set a conditional brekpoint to control this easily.
Using global variables is a shot in your knee. I do not know, what a "multi function program" is, but I would not use a code for productive work, which contains global variables.
Of course, the shown Storage() function is an ugly hack also, which should be used as first-aid solution in brownfield projects only. A clean and stable project would look like this:
a = fcn();
disp(a);
function a = fcn()
a = 17;
end
Does storing the variable persistently instead of globally work with GPU Coder APP?
Ram Kokku
le 11 Déc 2022
@Jan - thank for the response.
@Fatemeh Kalantari - As Jan recommend, you may consider using persinsten variable to work around this problem. Though GPU Coder supports persistent and global variables, using global and persistent variables may result in additional copies between GPU and CPU if used in GPU. so, minimize the use of persistent and global variables for better performance.
Réponses (0)
Voir également
Catégories
En savoir plus sur Kernel Creation from MATLAB Code dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!