CPU vs GPU - Is it reasonable?

23 ビュー (過去 30 日間)
Mehmet OZTURK
Mehmet OZTURK 2014 年 3 月 19 日
回答済み: Sparsh Mittal 2015 年 6 月 6 日
x1GPU=gpuArray.randn(3,10000);
x2GPU=gpuArray.randn(3,5000);
tic,for i=1:10000, bsxfun(@minus, x1GPU(:,i), x2GPU); end, toc
Elapsed time is 3.068520 seconds.
x1CPU=randn(3,10000);
x2CPU=randn(3,5000);
tic,for i=1:10000, bsxfun(@minus, x1CPU(:,i), x2cPU); end, toc
Elapsed time is 1.520823 seconds.
Computer Specs:
Intel Core i7 processor with 6GB of RAM
NVIDIA GT550M Graphics Card with 2GB of dedicated VRAM

採用された回答

Jill Reese
Jill Reese 2014 年 3 月 19 日
編集済み: Jill Reese 2014 年 3 月 19 日
While I can't comment exactly on the CPU/GPU comparison for your specific setup, I can say that the general rule of thumb in GPU computing is that operating on as much data as possible in a single call by vectorizing your code will provide the best performance. With that said, since you are already using the bsxfun function I would go a step further and use the following code to avoid the for loop and operate on all of your data in a single call.
Also, the timeit/gputimeit functions are the best choice for comparing CPU and GPU execution as they each provide an average time over multiple runs. Furthermore, gputimeit takes into account the fact that GPU operations perform asynchronously, while tic/toc does not.
x1GPU=gpuArray.randn(3,1,10000);
x2GPU=gpuArray.randn(3,5000);
gputimeit(@()bsxfun(@minus, x1GPU, x2GPU))
x1CPU=randn(3,1,10000);
x2CPU=randn(3,5000);
timeit(@()bsxfun(@minus, x1CPU, x2CPU))
I can confirm that when I run the code you provided, it takes about 4 seconds on the GPU and 1.5 seconds on the CPU. For the code that I have provided, gputimeit reports an average time of 0.0855 seconds on the GPU while timeit reports (again) an average of 1.5 seconds on the CPU.
The bottom line is that CPU for loops combined with GPU computing in the loop body generally does not provide the best performance. You should always try to replace code like this with a vectorized version if possible.
  1 件のコメント
Mehmet OZTURK
Mehmet OZTURK 2014 年 3 月 19 日
I think I got the point. Thank you for your clean explanation and coding.

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

その他の回答 (1 件)

Sparsh Mittal
Sparsh Mittal 2015 年 6 月 6 日
Please see this paper accepted in ACM Computing Surveys 2015, which clarifies the "CPU vs GPU" debate and also makes a case for "CPU-GPU collaborative computing". This paper also clarifies where CPUs are good and GPUs are good.

カテゴリ

Help Center および File ExchangeGPU Computing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by