does arrayfun support named functions on GPU?

3 views (last 30 days)
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?

Answers (2)

Wayne King
Wayne King on 13 Sep 2012
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()?
  1 Comment
Laboratory for Computational Vision, HHMI / NYU
Sorry, after thinking about the problem a little more I can see that I'm not very clear with my original question. Let me try again.
My goal is not to parallelize the sum operation, but to run my function gpuLoop on each GPU core with a different combination of image patches. I basically have a small collection of operations that are independent and can be done in each iteration of a for loop. What I would like to do is to distribute each of these iterations to a GPU core.
In reading the documentation at mathworks they seem to say that they do not support for loops on GPU's and that one should use arrayfun instead. Is this not correct? It seems like arrayfun is not very well supported on GPU yet though. Is this correct or am I implementing something incorrectly?

Sign in to comment.


Andy
Andy on 17 Sep 2012
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 Comments
Jill Reese
Jill Reese on 18 Sep 2012
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.
Laboratory for Computational Vision, HHMI / NYU
OK, I don't see a way to rewrite this code that will run on the GPU. I have independent for loop iterations that work fine in a parfor loop, but I want to run them on a GPU. The documentation I read said that for loops are not supported on GPU's and that one should use arrayfun. I wrote the above code to do this, but indexing is still not supported. Is there another way to write this code that would make it work on a GPU?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!