GPU Coder in MATLAB for heavily object oriented code

206 Views Asked by At

I am working with a heavily object oriented code in MATLAb. I want to use GPU coder to accelerated the code. However GPU coder only works for functions and the argument of the function should be explicitly specified. The input argument can be anything except objects. This actually makes my job very difficult if not impossible. Almost all the functions inside the class take object and return the same object. Any idea how I can isolate this behavior for at least some of the computationally intensive functions. I have a vague idea that maybe a wrapper can do the trick but not really sure. Thank you Thanks.

@rlivings39, after you comments, I created this simple class and after conversion I only see a place holder for the calling function. Can you please take a look and let me know if this is correct. Thanks again.

classdef MyClass
   properties
      Prop
   end
   methods
      function obj = MyClass(val)
         if nargin > 0
            obj.Prop = 10^val;
         end
      end
   end
end


function sampleClass
a = MyClass(2);
disp(a.Prop)
end


//
// sampleClass.cpp
//
// Code generation for function 'sampleClass'
//
// Include files
#include "sampleClass.h"

// Function Definitions
void sampleClass()
{
}

// End of code generation (sampleClass.cpp)
1

There are 1 best solutions below

1
Ryan Livingston On

MATLAB Coder and GPU Coder support value classes as top-level function I/O. Handle classes are not. The piece to consider here is that only the top-level functions cannot take handle objects as I/O. If you have internal functions called from your entry-points, those can use handle object I/O.

The wrapper idea is the right approach. You can write wrapper functions to serve as your entry-point functions for Coder. Internally those can create, modify, and use your objects as other functions would. They should then take and return types supported as top-level I/O by the Coders.

If the idea is to accelerate your algorithm, try to minimize the number of calls to the generated MEX file as well since there is overhead to those calls.