Usage of anonymous functions in arrayfun with GPU acceleration (Matlab)

懵懂的女人 提交于 2019-12-13 14:23:26

问题


I am new to the Parallel toolbox from Matlab R2012b and was wondering what the best way is to overcome the following problem.

I am analyzing the neighbourhood of every pixel in an image. Which is extremely good case for parallelization. However, I can't seem to get it working.

The main catch in the problem is that some "constant" arguments should be passed to the function. So the function should be called for every pixel, however, it also needs to access the surrounding pixels. (Preferable by passing the image as some sort of constant parameter and the coordinates of the pixel to be analyzed).

The output is one value per pixel.

At the moment I have this:

z2 = arrayfun(@(x) analyze(x, image, const1, ...), gpuArray(1:m*n));

Where x is the dummy-var, image a 2D matrix containing the luminance values of the image, const1 (and others) are function-constants (e.g. size of the analyze window). m and n are the size of the dimensions of the image.

However, I get this error

Error using gpuArray/arrayfun Use of functional workspace is not supported.

Any ideas?

Cheers, Ruben


回答1:


Unfortunately, this is not supported by Parallel Computing Toolbox in R2012b. The gpuArray version of arrayfun currently does not support binding in the constant data to an anonymous function handle. Arrayfun arguments must be passed directly, and must all either be scalar or the same size.

If you could bind in the constant arguments, you would next discover that you cannot currently index into them (or perform any non-scalar operations on them).

Perhaps you might be able to build up your algorithm using supported routines such as CONV2 or FILTER2.




回答2:


this is a very old post, but since I was struggeling with a similar issue, I wanted to share what I found out about this:

If you put your call of arrayfun within a function, you might be able to implement the analyze function as a nested function that has access to your constant arrays. However, this might require quite some effort in rewriting your code, because within the nested analyze function you cannot pass any full array to any other function, which means you have to rewrite everything in a way that you use only single indexed array entries of your constant arrays, e.g. in a for loop over the array. Accordingly all calls of functions like size etc. will not work and should be moved outside of analyze (at least this is the case for Matlab2015b, which I am using). Here is an example of how it can be done (not mine):

https://devblogs.nvidia.com/high-performance-matlab-gpu-acceleration/

Best,

Hans-Martin



来源:https://stackoverflow.com/questions/13236509/usage-of-anonymous-functions-in-arrayfun-with-gpu-acceleration-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!