Matlab GPU arrayfun shared variable

 ̄綄美尐妖づ 提交于 2019-12-12 03:30:43

问题


I am using matlab GPU computing with function arrayfun and a gpuArray object to do element-wise function on elements of the gpuArray variable on my function:

[ output ] = MyFunc( element, SharedMatrix )
//
// Process element with Shared Matrix
//
end

and my code is like so:

SharedMatrix = magic(5000); %Large Memory Object
SharedMatrix = gpuArray(SharedMatrix);
elements = magic(5);
gpuElements = gpuArray(elements );
//Error on next line, SharedMatrix object must be a scaler.
result = arrayfun(@MyFunc,gpuElements,SharedMatrix); 

I've heard that global variables can't be used in GPU computing.

Is there a way to do so with arrayfun ?


回答1:


Using recent versions of Parallel Computing Toolbox, this can be done for example by using a nested function in conjunction with arrayfun, like so:

function result = gpueg()

largeArray = gpuArray.rand(5000);

smallArray = magic(5);

    function out = myNestedFcn(in)
    % nested function accesses 'smallArray'    
        element = ceil(in * 25);
        out = smallArray(element);
    end

result = arrayfun(@myNestedFcn, largeArray);

end



回答2:


arrayfun currently require all inputs to be compatible sizes (or scalars), and the processing is done in an elementwise manner.

Also, Parallel Computing Toolbox in Matlab don't support Global Variables, So it can't be done using the Parallel Computing Toolbox.




回答3:


Possibly you can use a handle class:

classdef VarByRefContainer < handle
    properties
        val = [];
    end
end

handle = VarByRefContainer;
handle.val = SharedMatrix;
cellfun(@myfun, {handle, handle, handle});

See also this question.



来源:https://stackoverflow.com/questions/13607558/matlab-gpu-arrayfun-shared-variable

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