Unfortunately, this is not supported by Parallel Computing Toolbox in R2012b. The gpuArray version of arrayfun currently does not support binding in the constant data to an anonymous function handle. Arrayfun arguments must be passed directly, and must all either be scalar or the same size.

If you could bind in the constant arguments, you would next discover that you cannot currently index into them (or perform any non-scalar operations on them).

Perhaps you might be able to build up your algorithm using supported routines such as CONV2 or FILTER2.

Answer from Edric on Stack Overflow
🌐
MathWorks
mathworks.com › parallel computing toolbox › gpu computing › gpu computing in matlab
arrayfun - Apply function to each element of array on GPU - MATLAB
... You can now use the intmin, intmax, realmin, and realmax functions and specify a prototype array using the like syntax in functions you apply using arrayfun. For example, this function uses intmin and intmax to determine whether elements of integer gpuArray x are saturated.
🌐
MathWorks
mathworks.com › parallel computing toolbox › gpu computing › gpu computing in matlab
Using GPU arrayfun for Monte-Carlo Simulations - MATLAB & Simulink Example
The payout is the difference between the average price and the strike price. Use the asianCallOption, provided at the end of this example, to simulate an Asian call option. Set the strike price for the option to $95. ... Time 100,000 simulations on the CPU and on the GPU using arrayfun and show the results.
🌐
MathWorks
mathworks.com › parallel computing toolbox › gpu computing › gpu computing in matlab
gpuArray - Array stored on GPU - MATLAB
To precompile and run purely element-wise code on gpuArray objects, use the arrayfun function. To run C++ code containing CUDA® device code or library calls, use a MEX function. For more information, see Run MEX Functions Containing CUDA Code. To run existing GPU kernels written in CUDA C++, use the MATLAB CUDAKernel interface.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 232855-arrayfun-on-gpu-with-each-call-working-from-common-block-of-data
arrayfun on GPU with each call working from common block of data - MATLAB Answers - MATLAB Central
August 8, 2015 - https://www.mathworks.com/matlabcentral/answers/232855-arrayfun-on-gpu-with-each-call-working-from-common-block-of-data#answer_189051 ... arrayfun on the GPU cannot access the parent workspace of anonymous functions, but it can access the parent workspace for nested function handles. There's a detailed example in the documentation.
Top answer
1 of 2
2

Unfortunately, this is not supported by Parallel Computing Toolbox in R2012b. The gpuArray version of arrayfun currently does not support binding in the constant data to an anonymous function handle. Arrayfun arguments must be passed directly, and must all either be scalar or the same size.

If you could bind in the constant arguments, you would next discover that you cannot currently index into them (or perform any non-scalar operations on them).

Perhaps you might be able to build up your algorithm using supported routines such as CONV2 or FILTER2.

2 of 2
0

this is a very old post, but since I was struggeling with a similar issue, I wanted to share what I found out about this:

If you put your call of arrayfun within a function, you might be able to implement the analyze function as a nested function that has access to your constant arrays. However, this might require quite some effort in rewriting your code, because within the nested analyze function you cannot pass any full array to any other function, which means you have to rewrite everything in a way that you use only single indexed array entries of your constant arrays, e.g. in a for loop over the array. Accordingly all calls of functions like size etc. will not work and should be moved outside of analyze (at least this is the case for Matlab2015b, which I am using). Here is an example of how it can be done (not mine):

https://devblogs.nvidia.com/high-performance-matlab-gpu-acceleration/

Best,

Hans-Martin

🌐
NVIDIA Developer
developer.nvidia.com › blog › high-performance-matlab-gpu-acceleration
High-Performance MATLAB with GPU Acceleration | NVIDIA Technical Blog
August 21, 2022 - Vectorization is a key technique for maximizing the performance of GPU-accelerated MATLAB code by taking advantage of matrix algebra and removing loops. The bsxfun function allows for element-wise operations on arrays with different dimensions, while pagefun enables batch matrix operations, such as matrix multiplication and transpose, on multiple small matrices. Using arrayfun, custom kernels can be written in MATLAB to further optimize performance by minimizing kernel launch overhead and supporting scalar operations and standard MATLAB syntax.
🌐
GitHub
github.com › NVIDIA-developer-blog › code-samples › blob › master › MATLAB_arrayfun › ArrayfunArticle.m
code-samples/MATLAB_arrayfun/ArrayfunArticle.m at master · NVIDIA-developer-blog/code-samples
% * <http://devblogs.nvidia.com/parallelforall/calling-cuda-accelerated-libraries-matlab-computer-vision-example/ · % ParallelForAll Blog article on GPU MEX functions> · % * <ArrayfunArticle.zip All the code> needed to reproduce the · % examples and plots in this article.
Author   NVIDIA-developer-blog
🌐
MathWorks
mathworks.com › matlabcentral › answers › 282191-how-do-i-use-arrayfun-on-gpu-when-the-size-of-the-output-array-doesn-t-equal-the-size-of-some-inpu
How do I use arrayfun on GPU when the size of the output array does...
May 4, 2016 - Yes, arrayfun requires input matrices to be the same size. It seems pagefun will do the job. You can use functions like shiftdim to manage multiple dimensions of inputs. Please note I don't have much experience with GPU computing in MATLAB, so I may be wrong about this.
Find elsewhere
🌐
MathWorks
mathworks.com › parallel computing toolbox › gpu computing
GPU Computing in MATLAB - MATLAB & Simulink
Improve Performance Using a GPU and Vectorized Calculations This example shows you how to improve performance by running a function on the GPU instead of the CPU, and by vectorizing the calculations. Improve Performance of Element-Wise MATLAB Functions on the GPU Using arrayfun This example shows how to improve the performance of your code by running MATLAB® functions on the GPU using arrayfun.
🌐
MathWorks
mathworks.com › parallel computing toolbox › gpu computing
Measure and Improve GPU Performance - MATLAB & Simulink
The Stencil Operations on a GPU example shows arrayfun used to call a nested function that accesses variables declared in a parent function.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 403497-indexing-arrays-for-loops-in-a-gpuarray-arrayfun-called-function
Indexing Arrays for Loops in a gpuArray/arrayfun-called Function - MATLAB Answers - MATLAB Central
June 1, 2018 - So, I suspect you need to structure your code so that either you can operate in a completely element-wise manner on A and B - and then use arrayfun, or else you fully vectorise your code so that the whole of A and B can be passed in. Sign in to comment. Sign in to answer this question. Parallel Computing Parallel Computing Toolbox GPU Computing GPU Computing in MATLAB
🌐
University of Oxford
people.maths.ox.ac.uk › gilesm › codes › matlab_gpu
My MATLAB GPU blog
Another built-in demo function is called "paralleldemo_gpu_optionpricing" which does lots of Monte Carlo path calculations for option pricing. This is a particularly good example of the use of "arrayfun" to create vectorised code -- I think I'll come back to this in the future to construct ...
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › structures
arrayfun - Apply function to each element of array - MATLAB
If the execution of func causes ... from MATLAB results. If the input array to accumarray is empty, then the code generator can use zero-valued inputs to predetermine output types. func must not error when its inputs are zero, or the generated code can produce unexpected errors. Refer to the usage notes and limitations in the C/C++ Code Generation section. The same usage notes and limitations apply to GPU code generation. The arrayfun function fully ...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 169596-slice-into-gpuarray-and-perform-functions-on-the-gpu-with-arrayfun
Slice into gpuArray and perform functions on the GPU with arrayfun - MATLAB Answers - MATLAB Central
January 11, 2015 - https://www.mathworks.com/matlabcentral/answers/169596-slice-into-gpuarray-and-perform-functions-on-the-gpu-with-arrayfun#comment_267889 ... arrayfun can take a user-defined function, as long as that function carries out scalar operations. You can also index into arrays in that function as long as the array is passed in as an upvalue - see for instance here, the Mandelbrot example on this page and the Monte Carlo example here.