Using reduction variables on the GPU: arrayfun or other options

2 ビュー (過去 30 日間)
D. Plotnick
D. Plotnick 2018 年 9 月 24 日
コメント済み: D. Plotnick 2018 年 12 月 5 日
Hello all,
I am trying to figure out whether/how to use a reduction variable as the output of an arrayfun performed on the GPU. The basic problem is something of the form.
x = zeros(bigNumber,1);
for i = 1:I
x = x + f(i,argin);
end
where bigNumber makes x a large vector, f is some function, and argin are the arguments of that function. Now, using parfor, I can actually run this on the cpu in a manner where x is a reduction variable. However, I want to be able to run this on the GPU. Normally I would do something like:
function x = mainFun()
x = gpuArray.zeros(bigNumber,1);
v = arrayfun(@(i) loopedFun(gpuArray(i),args), 1:N,'UniformOutput',false)
v = sum(cat(2,v{:}),2);
x = x + v
end
function v = loopedFun(i,args)
v = someFunction(i,args);
end
This typically works, but since we are creating a [bigNumber,N] matrix in the intermediate stage this can rapidly fill up memory and become a huge computational burden in concatenation and summation stage. It should be far more efficient if I can initialize x as a reduction variable, and allow arrayfun to write to it in the same OOp independent manner that parfor would. However, I cannot find any examples of this type of operation. SO:
  • Is using reduction variables possible on the gpu, using something like arrayfun,cellfun, etc.?
  • What is the syntax, if it is? Reduction variables in parfor are pretty sensitive to syntax.
Thanks, -Dan

採用された回答

Joss Knight
Joss Knight 2018 年 9 月 25 日
編集済み: Joss Knight 2018 年 12 月 4 日
I suppose it depends on what f is, is it a scalar operation for each element of x? If so you can move your loop over I inside your arrayfun function and, as long as bigNumber is suitably large, the GPU will do a great job.
function a = nMyFunc(a, j)
for i = 1:I
a = a + f(i, j, argin);
end
end
x = gpuArray.zeros(bigNumber,1);
x = arrayfun(@nMyFunc, x, (1:bigNumber)');
j identifies which element of x is being processed.
If f is a vector operation then you need to inspect it to see what you can do to vectorize it.
The only other thing that comes to mind is, if you're willing to write your own CUDA kernel, you could use atomic operations to accumulate into x as a large bigNumber-by-I kernel.
  3 件のコメント
Joss Knight
Joss Knight 2018 年 12 月 4 日
Yes, I was being lax with my code. I'll correct it.
D. Plotnick
D. Plotnick 2018 年 12 月 5 日
Thanks. I thought I might be missing something key for a minute there.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by