arrayfun
Apply function to each element of array on GPU
Description
Note
This function behaves similarly to the MATLAB® function arrayfun
, except that the evaluation of the
function happens on the GPU, not on the CPU. Any required data not already on the GPU is
moved to GPU memory. The MATLAB function passed in for evaluation is compiled and then executed on the
GPU. All output arguments are returned as gpuArray
objects.
applies a function B
= arrayfun(func
,A
)func
to each element of a gpuArray
A
and then concatenates the outputs from func
into
output gpuArray
B
. B
is the same size as A
and
B(i,j,...) = func(A(i,j,...))
. The input argument
func
is a function handle to a MATLAB function that takes one input argument and returns a scalar.
func
is called as many times as there are elements of
A
.
applies B
= arrayfun(func
,A1,...,An)func
to the elements of the arrays A1,...,An
,
so that B(i,j,...) = func(A1(i,j,...),...,An(i,j,...))
. The function
func
must take n
input arguments and return a
scalar. The sizes of A1,...,An
must match or be compatible.
Examples
Input Arguments
Output Arguments
Limitations
The sizes of
A1,...,An
must match or be compatible. The size of output arrayB
depends on the sizes ofA1,...,An
. For more information, see Compatible Array Sizes for Basic Operations.Because the operations supported by
arrayfun
are strictly element-wise, and each computation of each element is performed independently of the others, certain restrictions are imposed:Input and output arrays cannot change shape or size.
Array-creation functions such as
rand
do not support size specifications. Arrays of random numbers have independent streams for each element.
You cannot specify the order in which
arrayfun
calculates the elements of output arrayB
or rely on them being done in any particular order.Like
arrayfun
in MATLAB, matrix exponential power, multiplication, and division (^
,*
,/
,\
) perform element-wise calculations only.Operations that change the size or shape of the input or output arrays (
cat
,reshape
, and so on) are not supported.Read-only indexing (
subsref
) and access to variables of the parent (outer) function workspace from within nested functions is supported. You can index variables that exist in the function before the evaluation on the GPU. Assignment orsubsasgn
indexing of these variables from within the nested function is not supported. For an example of the supported usage, see Stencil Operations on a GPU.Anonymous functions do not have access to their parent function workspace.
Overloading the supported functions is not allowed.
The code cannot call scripts.
There is no
ans
variable to hold unassigned computation results. Make sure to explicitly assign to variables the results of all calculations.The following language features are not supported: persistent or global variables,
parfor
,spmd
, andtry
/catch
.Calls to
arrayfun
inside P-code files or usingarrayfun
to evaluate functions obfuscated as a P-code files are not supported in standalone functions compiled using MATLAB Compiler™.
Tips
The first time you call
arrayfun
to run a particular function on the GPU, there is some overhead time to set up the function for GPU execution. Subsequent calls ofarrayfun
with the same function can run faster.