Arrayfun related GPU processing issues. MATLAB

I am having problems using arrayfun in MATLAB using GPU processing. I have simplified the situation below.

I have 4 large matrices (video data as (x, y, t)). I am using random case for this example.

A = gpuArray(rand(10,10,100));
B = gpuArray(rand(10,10,100));
C = gpuArray(rand(10,10,100));
D = gpuArray(rand(10,10,100));

      

I want to take every pixel of every matrix, (1,1,1) then (2,1,1), etc .; and perform a least squares calculation (values ​​are examples)

X = [10 10 ; 20 20 ; 30 30 ; 40 40]\[A;B;C;D];

      

Doing this as a for loop is taking too long for my data. Since I want to execute the function on a per element basis, I thought using a GPU would be the way to go.

For this, I created a function

function [x] = GPUTestFun (A,B,C,D)
X = [10 10 ; 20 20 ; 30 30 ; 40 40]\[A;B;C;D];
end

      

What do I then call with arrayfun (I don't think Matlab has GPU support for least squares?)

[x] = arrayfun(@GPUTestFun,[A;B;C;D]);

      

My understanding is that this should take each element of the 4 matrices individually and do the calculations.

The error I am getting: Error when using gpuArray / arrayfun Array concatenation is not supported. error at line: 4.

Line 4:

X = [10 10 ; 20 20 ; 30 30 ; 40 40]\[A;B;C;D];

      

Obviously the problem is that I am concatenating a matrix inside arrayfun. I thought about my options and can't see a solution. I figured it was matrix concatenation before I call the function, however, massive fun would be trying to execute the function on every element that doesn't line up now. I figured maybe solving the least squares by hand rather than using \, however I hesitate to try this without checking if I missed the easier solution first.

I also understand that the X output probably needs to be tweaked since one calculation produces 2 outputs, so it will probably need to be split up, so my outputs are the same size as my inputs. However, this is not an actual problem.

Any help would be greatly appreciated.

Thank you Jordan

EDIT: Working processor code:

A = (rand(10,10,100));
B = (rand(10,10,100));
C = (rand(10,10,100));
D = (rand(10,10,100));

[X1,X2] = arrayfun(@GPUTestFun,A,B,C,D);

      

Functions:

function [X1,X2] = GPUTestFun (A,B,C,D)
    [X] = [10 11 ; 20 8 ; 30 30 ; 40 30]\[A;B;C;D];
    X1 = X(1);
    X2 = X(2);
end

      

+3


source to share


1 answer


In this case, you can simply rewrite the whole problem as a call with multiple rights to \

, for example:

%# with A,B,C,D defined as per question
AA = [10 11 ; 20 8 ; 30 30 ; 40 30];
x  = [A(:)'; B(:)'; C(:)'; D(:)'];
x1x2 = AA \ x;
X1 = reshape(x1x2(1,:), size(A));
X2 = reshape(x1x2(2,:), size(A));

      



This will work on the GPU.

+2


source







All Articles