Calling CUDA mex functions from functions called via arrayfun

1 view (last 30 days)
Hi everyone,
I have a sizeable function which is used to calculate photon scattering in a liquid. This is about 900 lines of element wise operations, and lookups to arrays stored as uplevel variables, and is called using arrayfun and GPUArrays. Within this function, there are several calls to other smaller matlab functions stored in other m-files. My intention was to experiment with porting these smaller functions to CUDA C and using them as mex files. In creating a small test case for myself to make sure this would work in principle, an error is being thrown when trying to call a mex function in the body of the function that gets invoked using arrayfun. I compiled the mexGPUExample provided in the distcomp toolbox(<MATLAB directory>\toolbox\distcomp\gpu\extern\src\mex), and checked that it worked properly when called directly. When called from within an arrayfun function, i received the error.
"Error using gpuArray/arrayfun
Function passed as first input argument contains unsupported or unknown function mexGPUExample"
In the help for arrayfun ( it states that "FUN must be a handle to a function that is written in the MATLAB language (i.e., not a MEX-function)." This is, in my case, true, unless I take it to mean that all functions called from within FUN must also be written in the MATLAB language. If this is the intended meaning, would I need to port FUN, as well as all functions called within FUN, to CUDA C in order to make any use of CUDA C?
I've given some sample code below to indicate what I'm trying to do, incase the description was unclear.
function []=testArrayMex()
B=mexGPUExample(A) % <-- THIS WORKS
C=arrayfun(@timesTwo,A) % <-- THIS DOES NOT
function [output]=timesTwo(input)

Accepted Answer

Joss Knight
Joss Knight on 9 Jan 2019
GPU arrayfun supports a restricted set of element-wise operations that it knows how to translate into CUDA device IR. It doesn't support user MEX functions. I can see where you're coming from, you think because you wrote a CUDA MEX function it ought to be possible to translate it. However, there are two problems with that - firstly, there's no way of distinguishing between a CUDA MEX function and any other - the only difference is in the build process. Secondly, CUDA MEX functions are just CPU functions that launch kernels, they are not pure device code. A possible route for a future enhancement would be to support calling CUDAKernels from arrayfun, since they are pure device code.

More Answers (0)


Find more on Get Started with GPU Coder in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by