arrayfun with a function that has 5 inputs of different dimension
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone,
I have written a code to perform a gradient descent function on each point of a big set of values (~100k-2m points). It performs as expected but handles only one point at a time and I would like to speed it up. It seems that a good way to do it would be to perform the function on 384 points at the same time on the 384 cores of my gpu, using arrayfun.
However, after days of searches and tries, I cannot make it work using arrayfun. Maybe this comes from the fact that my function has 5 inputs with different dimensions:
function [Theta[150x1], J_history[20x1]] = GradientDescentLinear(X[1500x150], ...
Y[1500x 1], ...
Theta[150x1], ...
Alpha[1x 1], ...
Num_iters[1x1])
I have tried several things, for instance adding a 3rd [x384] dimension to all the inputs with the right values.but when I run :
[A,B] = arrayfun(GradientDescentLinear, ...
gX_TrainingSetCell, ...
gY_TrainingSetCell, ...
gTheta, ...
gAlpha, ...
gNum_iters);
I alaways get the same error:
Not enough input arguments.
Error in GradientDescentLinear(line 5)
m = (length(Y)); % number of training examples
I have also tried to turn the 5 inputs into 5 [384x1] cells containing the matrices, vector or values. But it does not work (altough it would haven't been possible to then send the cells to the GPU).
Can anyone help me with this? If you know an other way to do this I am also very interested. I can provide more informations if necesseray.
I would like to thank you for your help and your time,
Guilhem
2 Kommentare
dpb
am 1 Jun. 2016
From
help arrayfun
...
A = arrayfun(FUN, B, C, ...) evaluates FUN using elements of arrays B,
C, ... as input arguments. The (I,J,...)th element of A is equal to
FUN(B(I,J,...), C(I,J,...), ...). B, C, ... must all have the same size.
The purpose of arrayfun is to apply a given function to each and every element of the corresponding array elements of each array. Hence, your case of having differing array sizes is just the wrong tool.
bsxfun handles singleton expansion for two-element functions saving an explicit repmat, but that doesn't match your case, either.
You'll have to figure out ways to tile the data to be symmetric in order to be able to factor this; otomh w/o knowing more than we do, don't know we've got the ability to judge whether that's feasible or even possible.
Antworten (1)
Walter Roberson
am 1 Jun. 2016
[A,B] = arrayfun(@GradientDescentLinear, ... %notice @
gX_TrainingSetCell, ...
gY_TrainingSetCell, ...
gTheta, ...
gAlpha, ...
gNum_iters);
2 Kommentare
Joss Knight
am 8 Jun. 2016
Bearbeitet: Joss Knight
am 8 Jun. 2016
You can't do matrix operations inside GPU arrayfun. Use pagefun instead. Make A 10x5x384 and B 5x1x384, then you can call
C = pagefun(@mtimes, A, B);
C will now be 10x1x384.
It seems like you've picked 384 because it's the reported number of cores on your GPU. GPU parallelism doesn't work that way, so forget about that number. Just make the 3rd dimension as large as you can and the GPU will take care of scheduling the work.
To use arrayfun to do 384 complex vector operations per thread, you have to:
- Write out all the matrix algebra in your arrayfun function manually using scalar arithmetic and loops.
- Send your arrays into the function as up-level variables, i.e. variables declared outside the scope of a nested function that can be accessed from inside.
- Pass a single vector, e.g. 1:384, as an argument to your arrayfun function in order to instruct it to launch 384 threads.
- Index into your complex arrays inside the function one value at a time.
- Return an output that is the same size as your input - i.e. your function can only return one value per thread.
In other words, don't do it! That's what all the optimized matrix and vector routines that MATLAB provides for you are designed to do.
Siehe auch
Kategorien
Mehr zu Matrix Indexing 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!