MATLAB Answers

0

Calling CUDA mex functions from functions called via arrayfun

Asked by Aaron Meldrum on 8 Jan 2019
Latest activity Answered by Joss Knight
on 9 Jan 2019
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 (https://www.mathworks.com/help/distcomp/arrayfun.html) 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.
Thanks,
Aaron
function []=testArrayMex()
A=gpuArray(1:10000);
B=mexGPUExample(A) % <-- THIS WORKS
C=arrayfun(@timesTwo,A) % <-- THIS DOES NOT
end
function [output]=timesTwo(input)
output=mexGPUExample(input)
end

  0 Comments

Sign in to comment.

Products


Release

R2018b

1 Answer

Answer by Joss Knight
on 9 Jan 2019
 Accepted Answer

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.

  0 Comments

Sign in to comment.