# error with arrayfun and GPU computing part 2.

1 Ansicht (letzte 30 Tage)
Mikhail am 31 Okt. 2014
Kommentiert: Mikhail am 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.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Geoff Hayes am 31 Okt. 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.)

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Edric Ellis am 31 Okt. 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);
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Mikhail am 3 Nov. 2014
Ok, got it, thanks

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by