Adding just one line in my code slows the GPU.

hello, my first code was
for i = 1:1000
r=0.8*r+0.2*tanh(A*r+W_in*xc(i+T0)+(0.3)*bias);
end
In the code above,the gpu is faster than cpu but I have to stack 'r' which is (nx1) matrix, so I just adding 'R' on previous code makes the gpu slower than cpu.
R=zeros(n,1000);
for i = 1:1000
r=0.8*r+0.2*tanh(A*r+W_in*xc(i+T0)+(0.3)*bias);
R(:,i)=r;
end
I want to make my code runs fast on gpu than cpu. What should I do??

回答 (1 件)

Matt J
Matt J 2020 年 2 月 18 日
編集済み: Matt J 2020 年 2 月 18 日

0 投票

Pre-allocate on the GPU. Also, pre-compute things on the GPU that are easily vectorized and don't depend on r.
R=gpuArray.zeros(n,1000);
r=gpuArray(r);
A-gpuArray(A);
increments = W_in*gpuArray( xc((1:1000)+T0) ) + 0.3*bias;
for i = 1:1000
r=0.8*r+0.2*tanh(A*r+ increments(i));
R(:,i)=r;
end

3 件のコメント

TAEYOON KIM
TAEYOON KIM 2020 年 2 月 19 日
編集済み: TAEYOON KIM 2020 年 2 月 19 日
Sorry, I didn't write about pre-allocate. I pre-allocate all elements already. My prblem is when R(:,i)=r part makes for loop slow.
Matt J
Matt J 2020 年 2 月 19 日
Still, my other recommendations should help...
TAEYOON KIM
TAEYOON KIM 2020 年 2 月 19 日
Ok thanks a lot!!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeParallel Computing についてさらに検索

質問済み:

2020 年 2 月 18 日

コメント済み:

2020 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by