Effacer les filtres
Effacer les filtres

error with arrayfun and GPU computing part 2.

2 vues (au cours des 30 derniers jours)
Mikhail
Mikhail le 31 Oct 2014
Commenté : Mikhail le 3 Nov 2014
I am tring to compute simple example on GPU:
function C=myf(A,B,N)
for i=1:N
C(:,:,i)=(A(:,:,i)*B)^10;
end
end
With CPU it works:
M = 100;
K = 100;
N = 1000;
A=rand(M,K,N);
B=rand(K,M);
C=myf(A,B,N);
With GPU:
if true
Aongpu=gpuArray(A);
Bongpu=gpuArray(B);
C=arrayfun(@myf,Aongpu,Bongpu,N);
end
It doesn't:
Error using parallel.gpu.GPUArray/arrayfun Matrix dimensions must agree.
Why?? Thanks in advance
  1 commentaire
Geoff Hayes
Geoff Hayes le 31 Oct 2014
Mikhail - I'm not sure if using arrayfun in this manner is appropriate/correct. The documentation indicates
Nonsingleton dimensions of input arrays must match each other. In other words, the corresponding dimensions of arguments B, C, etc., must be equal to each other, or equal to one. Whenever a dimension of an input array is singleton (equal to 1), arrayfun uses singleton expansion to virtually replicate the array along that dimension to match the largest of the other arrays in that dimension…
Given the examples (from the above link), this seems to suggest that your B and N would be expanded to MxKxN arrays, and your myf function would be then applied to each element within these identically sized inputs. (This is similar to the non-GPU version of arrayfun where each array input must be of the same dimension.)

Connectez-vous pour commenter.

Réponse acceptée

Edric Ellis
Edric Ellis le 31 Oct 2014
When working with gpuArray data, the function that you pass to the arrayfun will be called with scalar values. In other words, your myf needs to accept scalars and return scalars, like so:
M = 100;
K = 100;
N = 1000;
A=rand(M,K,N);
B=rand(K,M);
myf = @(a, b, n) a + b + n % or something more appropriate
arrayfun(myf, gpuArray(A), gpuArray(B), N);

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by