0

Consider the following simple class

classdef A < handle

    properties
        M;
    end


    methods
        function obj= A(m)
               obj.M=m;
        end

        function foo(obj)
            Array = linspace(0,obj.M,100);
            arrayfun(@obj.bar,Array);
        end

         function foo2(obj)
            Array = gpuArray.linspace(0,obj.M,100);
            arrayfun(@obj.bar,Array);
         end

        function y = bar(obj,x)
            y = x^2/obj.M;
        end
    end

end

Now a run

>> a=A(1);

>> a.foo();

>> a.foo2();

Error using gpuArray/arrayfun Function passed as first input argument contains unsupported 'MCOS' language feature 'CLASSDEF'. For more information see Tips and Restrictions.

Error in A/foo2 (line 20) arrayfun(@obj.bar,Array);

Notice that foo() and foo2() are the same function with the only exception, foo2() is supposed to run the GPU version of arrayfun.

Is there any workaround or trick to make foo2() above working, i.e. class method run on GPU? Consider bar() cannot be static or so as it supposed to use class properties.

1 Answers1

0

You cannot use classdef objects in code running on the GPU. You will have to create a function that takes each one of the class properties it uses as an input argument. Something like this should work (not tested!):

classdef A < handle

    properties
        M;
    end

    methods
        function obj = A(m)
            obj.M = m;
        end

        function foo2(obj)
            Array = gpuArray.linspace(0,obj.M,100);
            arrayfun(@(x)bar(obj.M,x),Array);
        end
    end

end

function y = bar(M,x)
    y = x^2/M;
end

If your function bar could return as outputs any new values for object properties, which your function foo2 would write to those properties.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • @MichaelMedvinsky: But there is no `classdef` involved in that call! Did you clear your previous definition of the class? (`clear classes`). – Cris Luengo Jun 05 '19 at 00:32