Efficiently run matrix or vector-valued function in element-wise fashion on GPU?
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Joss Knight
on 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 Comments
Joss Knight
on 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++.
See Also
Categories
Find more on GPU Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!