Efficiently run matrix or vector-valued function in element-wise fashion on GPU?

1 Ansicht (letzte 30 Tage)
I have a function like
function [a b] = fun(x)
that takes in a scalar x and outputs two Tx1 vectors, a and b.
I'd like to call this function on every element of a large MxN matrix X, and get two TxMxN multi-dimensional arrays A and B, such that [A(:,m,n), B(:,m,n)] = fun(X(m,n)), for m=1,...,M and n=1,...,N.
arrayfun won't work here since this is not a scalar function (and UniformOutput=false doesn't work on GPU).
What are my options? I can also rewrite fun to return a single vector [a;b] or a Tx2 matrix [a b] instead, then rejigger the results myself, if that makes the problem any easier. Anyway the basic problem here is achieving something like arrayfun on GPU for a matrix-valued function (that takes scalar inputs). If there's no MATLAB functionality for this, I'd appreciate advice on how to manually code this in CUDA.

Antworten (1)

Joss Knight
Joss Knight am 7 Nov. 2018
It's difficult to say what the best solution is without seeing what fun does. Typically you can address it using a series of calls to pagefun. If T is a manageable size, you can write out the vector operations in scalar mathematics, and create a series of T outputs of size M-by-N. For instance.
function [a1, a2, a3] = polyvals(x)
a1 = x;
a2 = x*x;
a3 = a2*x;
end
[A1, A2, A3] = arrayfun(@polyvals, X);
A = cat(3, A1, A2, A3);
Then you could permute the result if you really wanted it to be 3-by-M-by-N rather than M-by-N-by-3. However, this may not be feasible for your problem.
  4 Kommentare
Yibo Yang
Yibo Yang am 8 Nov. 2018
Thanks again for your insight Joss!
Now I understand better the restrictions when using gpuArray with arrayfun. Since you likely have more experience with CUDA than me, do you think writing a custom CUDA kernel and calling it from MATLAB would work? It'll be a pain to write a different function for each T (it'd be great if MATLAB has something like C++ templates and can generate/compile a different gaussjac for each T).
Otherwise, as you suggested, I probably should look into vectorization or a different algorithm.
Joss Knight
Joss Knight am 8 Nov. 2018
Anything you can do in MATLAB, you can do in C++. The question is of course, can you do it easily? On the face of it your gaussjac function doesn't look too hard to convert into C++ because it's mostly loops and scalar arithmetic, but you'd probably have to give it a try to assess that for sure. And only you know what your level is for CUDA C++.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu GPU Computing 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