Calling CUDA mex functions from functions called via arrayfun

조회 수: 4 (최근 30일)
Aaron Meldrum
Aaron Meldrum 2019년 1월 8일
답변: Joss Knight 2019년 1월 9일
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

채택된 답변

Joss Knight
Joss Knight 2019년 1월 9일
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개)

카테고리

Help CenterFile Exchange에서 GPU Computing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by