Strange gpu arrayfun behavior

조회 수: 2 (최근 30일)
nealm
nealm 2018년 7월 20일
댓글: Joss Knight 2018년 7월 26일
I have existing code that works but I'd like to accelerate using the GPU version of arrayfun. However the behavior of GPU arrayfun is difficult to understand. According to the documentation the code within the helper function performs scalar operations on the input gpuArray values. However if I try to write my own scalar values the result is unpredictable. To demonstrate, if I have a helper function that look like:
function outArray = simpleTest(inArray)
outArray = inArray * 3.14159;
end
x = gpuArray(1:4);
y = arrayfun(@simpleTest, x);
y = gather(y)
y =
Columns 1 through 2
0.0000 + 0.0000i 0.0000 + 0.0000i
Columns 3 through 4
0.0000 + 0.0000i 0.0000 + 0.0000i
If I pass the scalar value to the helper function, as shown in the second helper function, it does seem to work:
function outArray = simpleTest2(inArray, scalarValue)
outArray = inArray * scalarValue;
end
y = arrayfun(@simpleTest2, x, 3.14159);
y = gather(y)
y =
3.1416 6.2832 9.4248 12.5664
Reading the documentation, I know that the scalar values in the calling function are expanded (similar to bsxfun) however I have been unable to get that function, or any other function, to work within the helper function. I know I'm missing something here, I would appreciate the help.
I have Matlab R2018a with Parallel Computing Toolbox and Cuda version 9.0 installed.
  댓글 수: 5
nealm
nealm 2018년 7월 24일
편집: nealm 2018년 7월 24일
>> which simpleTest
'simpleTest' not found.
>> which simpleTest2
'simpleTest2' not found.
>>
I placed the simpleTest functions in a script. I also tried calling them from their own files, no difference.
Joss Knight
Joss Knight 2018년 7월 24일
Okay, so the next thing to do is to check and upgrade your driver. What is your device? What is your driver version?
gpuDevice
parallel.internal.gpu.CUDADriverVersion

댓글을 달려면 로그인하십시오.

채택된 답변

nealm
nealm 2018년 7월 25일
Joss/OCDER,
I found the source of the problem. I use my own "startup.m" file (which only has "addpath" commands) that is executed at the beginning of a Matlab start. The directories that have been added during the startup process appear to interfere with the way gpu arrayfun executes.
If I don't use the startup.m file gpu arrayfun works fine. Curiously, if I manually run the contents of startup.m AFTER starting Matlab - gpu arrayfun also works fine.
  댓글 수: 6
nealm
nealm 2018년 7월 26일
편집: nealm 2018년 7월 26일
Assuming the call to mtree wasn't normal, I checked the files in my path and didn't find any file that uses classdef mtree directly. Then after some work I think I found the culprit; In my path I have a class folder "@char" where I've redefined the method str2double to that from a mex file from the file exchange:
I had relabeled the function from str2doubleq to str2double for convenience and have used it for several years without issue. After removing this user version of "str2double" from the @char folder, GPU arrayfun behaves normally in all test cases.
Not sure if this is a bug in Matlab or just poor coding form by me, but I'll stop relabeling matlab functions to my own user functions.
Joss Knight
Joss Knight 2018년 7월 26일
Thanks for the investigation! I'll have a look at why str2double is involved and whether I can reproduce your issue. MTree is used to do static analysis of your arrayfun function for the GPU, and presumably str2double is being used to convert the text of your literal value. It may well be a bug that it's possible to break arrayfun by shadowing this function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by