简体   繁体   English

使用GPU加速的arrayfun中的匿名函数(Matlab)

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

I am new to the Parallel toolbox from Matlab R2012b and was wondering what the best way is to overcome the following problem. 我是Matlab R2012b的Parallel工具箱的新手,想知道克服以下问题的最佳方法是什么。

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 (eg size of the analyze window). 其中x是dummy-var,图像是包含图像亮度值的2D矩阵,const1(和其他)是函数常数(例如分析窗口的大小)。 m and n are the size of the dimensions of the image. m和n是图像尺寸的大小。

However, I get this error 但是,我收到此错误

Error using gpuArray/arrayfun Use of functional workspace is not supported. 使用gpuArray / arrayfun时出错不支持使用功能工作区。

Any ideas? 有任何想法吗?

Cheers, Ruben 干杯,鲁本

Unfortunately, this is not supported by Parallel Computing Toolbox in R2012b. 不幸的是,R2012b中的Parallel Computing Toolbox不支持这一点。 The gpuArray version of arrayfun currently does not support binding in the constant data to an anonymous function handle. arrayfun的gpuArray版本目前不支持将常量数据绑定到匿名函数句柄。 Arrayfun arguments must be passed directly, and must all either be scalar or the same size. Arrayfun参数必须直接传递,并且必须全部为标量或相同大小。

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. 也许您可以使用支持的例程(如CONV2或FILTER2)构建算法。

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. 如果将arrayfun调用放在函数中,则可以将analyze函数实现为可以访问常量数组的嵌套函数。 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, eg in a for loop over the array. 但是,这可能需要花费大量精力来重写代码,因为在嵌套analyze函数中,您无法将任何完整数组传递给任何其他函数,这意味着您必须以仅使用单个索引数组条目的方式重写所有内容。常数数组,例如在数组中的for循环中。 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). 因此, size等函数的所有调用都不起作用,应该移出analyze之外(至少Matlab2015b就是这种情况,我正在使用)。 Here is an example of how it can be done (not mine): 这是一个如何完成它的例子(不是我的):

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

Best, 最好,

Hans-Martin 汉斯 - 马丁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM