How to formulate problem so Matlab can use GPU functionality?

Often when I try to solve a problem and want to use the GPU I strugle to formulate the problem so Matlab can use GPU functionality.
Below is very simple example. I have a function with an array as input and I want to evaluate it with a large number of different parameters, in this example it's a filter that I want to test with different filter coefficients. It is possible to use "parfor" to parallelize it on the CPU. But how do I use the GPU to solve it?
.
X=sin((0:0.1:5)'); % Unfiltered signal
C = linspace(0,1,1e6)'; % Large number of filter coefficients
Y = zeros(length(X),length(C)); % Pre allocate storage for filtered signals
for k=1:length(C)
Y(:,k) = MyFilter(X,C(k)); % I want to parallelize this for-loop on the GPU
end

 Accepted Answer

Matt J
Matt J on 22 Jun 2013
Edited: Matt J on 22 Jun 2013
GPUs aren't general things that can parallelize any arbitrary for-loop the way parfor can. They work well for accelerating operations on arrays, where different regions of the array get modified by lots of small parallel computations, each consisting of a handful of adds and multiplies.
MATLAB has provided a data type called gpuArray for manipulating arrays on the GPU along with built-in GPU-accelerated functions that operate on this data type. You might be able to re-implement MyFilter() on the GPU by making X a gpuArray and applying its filter() method,
It really all depends what exactly is going on inside MyFilter()...

2 Comments

To keep it simple, MyFilter could be:
function x = MyFilter(x,c)
for k=2:length(x)
x(k) = c*x(k-1) + (1-c)*x(k);
end
If I only change all variables to gpuArray and run it get much slower comperaed to CPU. So maybe this kind of problems isn't suitable for the GPU or maybe it's possible to rewrite it to a more GPU friendly way.
If I only change all variables to gpuArray
No, that wouldn't be enough. You would also have to use the filter() command like I said. The filter() command is what has all the relevant GPU code in it.
Because your filter is only 1st-order however, it's pretty easy to vectorize this whole thing, getting rid of the for-loop completely
X=X(:); %ensure column
C=C(:).'; %ensure row
xmat=[x(1:end-1), x(2:end)]
cmat=[C;1-C];
Y=xmat*cmat;
These linear algebra operations might run even faster on gpuArrays.

Sign in to comment.

More Answers (0)

Asked:

on 22 Jun 2013

Community Treasure Hunt

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

Start Hunting!