Conflicting behaviour of arrayfun() with gpu: example that works and example of error

2 vues (au cours des 30 derniers jours)
I often use arrayfun() to evaluate a function on a multidimensional grid on gpu. I have created an example that shows the idea and works. I then include an example in which I do the same inside a function and it gives the error "Variable number of inputs is not supported. For more information see Tips". This seems to be conflicting behaviour of Matlab.
The attached file "ExampleOfError.m" gives the working example and the one that throws an error (the one that throws an error additionally calls the other three attached scripts).
Essentially, I have a function that takes the first three inputs as grids, and then a number of scalar inputs. I use arrayfun() to evaluate this function (the grids are on gpu). Because in general I do not know the number of scalar inputs that the function will take I do a trick of setting up the scalar parameters into a cell so that they can be passed into arrayfun() (in my main codes, the function is inputted by the user, and while the three grid inputs is fixed, they can use as many scalar inputs as they want).
The first 26 lines of "ExampleOfError.m" are a stripped back working example. There is a function f(), three gpu grids (x,y,z), params which is a cell containing the scalar parameters, and then it calls arrayfun() which works. There is a call to 'whos' which shows:
Name Size Bytes Class Attributes
params 1x11 1232 cell
x 21x1 4 gpuArray
y 1x301 4 gpuArray
z 1x1x301 4 gpuArray
From lines 27 to end (line 67) of "ExampleOfError.m" is an example that essentially does the same but gives the error "Variable number of inputs is not supported. For more information see Tips" when it calls arrayfun().
It first creates the grids, then Params (which is a structure of the parameters), line 56 defines the function we want to evaluate (which is a script). Lines 59 to 65 figure out the names of the scalar inputs, and then gets these out of Params and creates a vector of them. Then on line 67 a function is called (I have stripped back the contents to make it easy to follow and see the error). In that function it turns the vector of scalar inputs into a cell. It then calls arrayfun() to evaluate on the grids and the cell of scalars, exactly as I did in the working example, but now it throws the error "Variable number of inputs is not supported. For more information see Tips". I added a 'whos' command on the line before arrayfun() and you can see that it gives the exact same about the inputs to arrayfun() as was the case in the example that works.
Is this some issue with how Matlab is attempting to compile? That it is getting confused about the call of arrayfun() in the example that errors? Admittedly the use of the cell is probably not the intended approach, but it works just fine most of the time (I use this trick a huge amount, it almost always works but on rare occasions throws this error, I wrote these examples to clearly show exactly how it happens).
[I wrote the example generating this error in R2021a, but have had it in many different versions of Matlab.]

Réponse acceptée

Joss Knight
Joss Knight le 14 Nov 2022
The function normcdf isn't supported by GPU arrayfun because it accepts varargin. For a list of supported functions see the documentation.
Unfortunately your use of an anonymous function obfuscated the error. If you just pass @KulishKentSmith2010_ReturnFn then you get a bit more info:
Error using CreateReturnFnMatrix_Case1_Disc_noz_Par2_showerror
Variable number of inputs is not supported.
For more information see Tips.
Error in 'normcdf' (line: 1)
Error in ExampleOfError (line 68)
ReturnMatrix=CreateReturnFnMatrix_Case1_Disc_noz_Par2_showerror(ReturnFn, n_d, n_a, d_grid,
a_grid, ReturnFnParamsVec);
  1 commentaire
Robert Kirkby
Robert Kirkby le 14 Nov 2022
Thanks!
The error message had thrown me off.
In case anyone is interested I fixed it by just replacing normcdf() with
(1/2)*(1+erf((x-mu)/(sqrt(2)*sigma)))
and this solved things as erf() is supported by arrayfun() on the gpu.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur GPU Computing in MATLAB dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by