MATLAB Answers

I need to do this without for loop(For Gpu computing)

9 views (last 30 days)
I have these two problems in gpu computing in matlab
Suppose X=600*103*36 array
for n=1:36
I want to access all the pages at once for gpu computing. Is there a way to do ?
Problem 2:
From the above result,
suppose w is 600*36 matrix
for n=1:36
R1(:,:,n)=transpose(Q(:,:,n) )* w(600,n);
I wanted to make this loop without for loop for gpu computing.
Arrays here such as w, X are gpu arrays.


Srinidhi Ganeshan
Srinidhi Ganeshan on 31 Mar 2018
Mr. Walter Roberson, ThankYou for your reply :). But, in the link provided, I have to access Q values page by Page. But I wanted to access it in a stretch without for-loop for GPU Computing. I tried using reshape, pagefun, permute but till now I could not find a solution to do that without for loop.

Sign in to comment.

Accepted Answer

Joss Knight
Joss Knight on 1 Apr 2018
You can't do this, but I will take it as a further request for pagefun to support qr which will be coming in a future version of MATLAB.
The closest you can get is to form the block-diagonal matrix with your pages of X and solve qr for that. The results will be the equivalent block-diagonal elements of the output. Because your matrices are non-square, you would have to pad the columns with zeros.
function [Q,R] = pagefunQR(X)
m = size(X,1);
n = size(X,2);
p = size(X,3);
maxmn = max(m,n);
A = zeros(maxmn*p,'like',X);
for i = 1:p
strt = (i-1)*maxmn+1;
A(strt:strt+m-1,strt:strt+n-1) = X(:,:,i);
[Q, R] = qr(A);
for i = 1:p
strt = (i-1)*maxmn+1;
Q(1:maxmn,strt:strt+maxmn-1) = Q(strt:strt+maxmn-1,strt:strt+maxmn-1);
R(1:maxmn,strt:strt+maxmn-1) = R(strt:strt+maxmn-1,strt:strt+maxmn-1);
Q = reshape(Q(1:maxmn,:), maxmn, maxmn, p);
R = reshape(R(1:maxmn,:), maxmn, maxmn, p);
R = R(1:m,1:n,:);
Sadly, this approach is slower than just looping like you are doing. Plus it creates a huge pressure on memory.

  1 Comment

Srinidhi Ganeshan
Srinidhi Ganeshan on 2 Apr 2018
Thank you for your answer. But as you said, this is slower approach.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by