Hello,
I am doing some experiments with gpuArrays with a simple problem: for every pair of distinct rows of a random matrix I want to find the difference of the sum of the elements in the rows. (I am aware that this formulation of the problem is linear, however let'assume that the function I am applying to the couple of rows is not linear)
n = 100;
gpu = true;
if gpu
returns = rand(n, 'gpuArray');
ansmat = zeros(n*(n-1)/2, 3,'gpuArray');
else
returns = rand(n);
ansmat = zeros(n*(n-1)/2, 3);
end
itermat = 1;
for v1 = 1:n
for v2 = 1:v1-1
ansmat(itermat,1:2) = [v1,v2];
itermat = itermat + 1;
end
for v2 = v1+1:n
ansmat(itermat,1:2) = [v1,v2];
itermat = itermat + 1;
end
end
tic
for i=1:size(ansmat,1)
sum1 = sum(returns(ansmat(i,1),:),2);
sum2 = sum(returns(ansmat(i,2),:),2);
ansmat(i,3) = sum1-sum2;
end
toc
When I set
I get
>> ZZZ
Elapsed time is 3.120591 seconds.
When I set
I get
>> ZZZ
Elapsed time is 0.016989 seconds.
I think I am missing a few fundamentals. For example, when I tried to rewrite my code in terms of arrayfun with
myfun = @(x, y) sum(returns(ansmat(x,1),:),2) - sum(returns(ansmat(y,2),:),2);
tic
ansmat(:,3) = arrayfun(@(x,y) myfun(x,y), ansmat(:,1), ansmat(:,2));
toc
it works without the GPU, however using the GPU there is a problem:
>> ZZZ
Error using ZZZ (line 33)
Use of functional workspace is not supported.
For more information see Tips and Restrictions.
But I could not find those tips.
What is the correct way to gain some speed in solving my problem using the GPU?