does arrayfun support named functions on GPU?
1 次查看(过去 30 天)
显示 更早的评论
I'd like to implement some patch based image analysis (like nl-means) using arrayfun and a GPU.
I have a 2D matrix of image patches that is Npixels by Nblocks in size and would like to compare each patch to every other patch and save some statistic. I started with this:
weights = arrayfun(@(k)
sum( imgBlocks(:,floor((k-1)/Nblocks)+1)-imgBlocks(:,mod((k-1),Nblocks)+1))/Npix, 1:Nblocks*Nblocks);
The above works fine on CPU but fails on the GPU because sum() is not supported, so I changed from a lambda to a named function:
weights = arrayfun(@(k)
gpuLoop(imgBlocks(:,floor((k-1)/Nblocks)+1),imgBlocks(:,mod((k-1),Nblocks)+1)), 1:Nblocks*Nblocks);
where gpuLoop is:
function weight = gpuLoop(p1,p2)
[Npix foo] = size(p1);
weight = sum(p1-p2)/Npix;
Again, this runs fine on CPU, but fails on GPU with:
Error using parallel.gpu.GPUArray/arrayfun
Error: Unexpected MATLAB operator.
Error in toy_gpu (line 55)
weightsGPU = arrayfun(@(k)gpuLoop(imgBlocksGPU(:,floor((k-1)/Nblocks)+1), ...
Here's my GPU version:
imgBlocksGPU = gpuArray(imgBlocks);
weightsGPU = gpuArray(zeros(1,Nblocks*Nblocks));
blockCtrGPU = gpuArray(1:Nblocks*Nblocks);
weightsGPU = arrayfun(@(k)gpuLoop(imgBlocksGPU(:,floor((k-1)/Nblocks)+1), ...
imgBlocksGPU(:,mod((k-1),Nblocks)+1)), ...
blockCtrGPU);
I'm guessing that something about arrayfun with named functions isn't supported on GPU yet, since this same code ran fine on the CPU. Is that correct or is something else wrong?
Is there any way to run this kind of thing on the GPU with standard Matlab code?
0 个评论
回答(2 个)
Wayne King
2012-9-13
I can't test this at the moment, but I'm guessing you're right. You can mix MATLAB workspace variables and gpuArray objects, but that embedding a function call here is the problem.
Is it really necessary to use function? sum() is supported on the GPU, since you're just summing the two arrays, can't you just do that outside the call to arrayfun()?
Andy
2012-9-17
hello Rob,
The issue does not have to do with named vs. anonymous functions. In fact the error message that should be returned should be about indexing. This has been fixed and shipped in R2012b. Other issues with the code are the use of SUM and the uplevel variables in the anonymous function.
Sorry for the confusing error message. Hope this clears up any confusion.
thanks,
Andy
3 个评论
Jill Reese
2012-9-18
The error message "Indexing is not supported." means that you cannot use any indexing operations within a call to arrayfun executed on the GPU in R2012b. Not all MATLAB functionality is supported by arrayfun on the GPU, so you may need to rewrite your code.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 GPU Computing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!