0

I am using 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 ?

2
  • You need to convert SharedMatrix to gpuArray as well. Commented Nov 29, 2012 at 20:18
  • Even if I did, It process it element by element not as a whole matrix. And it needs to be with the same dimensionality as gpuElements matrix Commented Dec 1, 2012 at 9:17

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.