Manipulating arrays within GPU arrayfun
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Charles Lee
le 27 Avr 2016
Réponse apportée : Joss Knight
le 28 Avr 2016
I am trying to use GPU arrayfun to perform computations on various subsets of a large array. However it appears that manipulating arrays is not supported by GPU arrayfun. The following minimal example gives the error "Function passed as first input argument contains unsupported or unknown function 'colon'".
function test()
bigarray = rand(5,5);
param = [1 2 3 4];
param = gpuArray(param); %CPU version gives no error
function res=func(p)
subarray = bigarray(p:p+1,1:5);
res = sum(sum(subarray));
end
arrayfun(@func,param)
end
Is there a workaround to the problem?
0 commentaires
Réponse acceptée
Joss Knight
le 28 Avr 2016
The general rule is that you cannot manipulate matrices or vectors inside a gpuArray arrayfun function, only scalar operations are supported - that rules out colon, and sum I'm afraid. The point of gpuArray arrayfun is to represent an element-wise kernel, so the function represents the operations one GPU thread is carrying out on one element of your input. You can do indexing, on upvalue variables, but only to index a single element. In this sense it is very different from the CPU version - the CPU version is just a syntactical convenience function that avoids having to write loops.
Usually there is a vectorised version of whatever it is you're trying to do that avoids the need for complex operations inside arrayfun. In your case, you're trying to get hold of the sum of the data in pairwise columns. Reductions are not good candidates for arrayfun. You want something like:
sumRows = sum(bigarray);
res = sumRows(1:end-1) + sumRows(2:end);
If you really really need to do the CPU-like thing on a gpuArray, then just write out the nested for-loops - it will perform just the same.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur GPU Computing in MATLAB dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!