Skip to Main Content Skip to Search
Product Documentation

arrayfun - Apply function to each element of array on GPU

Syntax

A = arrayfun(FUN, B)
A = arrayfun(FUN, B, C, ...)
[A, B, ...] = arrayfun(FUN, C, ...)

Description

This method of a GPUArray object is very similar in behavior to the MATLAB function arrayfun, except that the actual evaluation of the function happens on the GPU, not on the CPU. Thus, any required data not already on the GPU is moved to GPU memory, the MATLAB function passed in for evaluation is compiled for the GPU, and then executed on the GPU. All the output arguments return as GPUArray objects, whose data you can retrieve with the gather method.

A = arrayfun(FUN, B) applies the function specified by FUN to each element of the GPUArray B, and returns the results in GPUArray A. A is the same size as B, and A(i,j,...) is equal to FUN(B(i,j,...)). FUN is a function handle to a function that takes one input argument and returns a scalar value. FUN must return values of the same class each time it is called. The input data must be a arrays of one of the following types: single, double, int32, uint32, logical, or GPUArray. The order in which arrayfun computes elements of A is not specified and should not be relied on.

FUN must be a handle to a function that is written in the MATLAB language (i.e., not a built-in function or a mex function); it must not be a nested, anonymous, or sub-function; and the MATLAB file that defines the function must contain exactly one function definition.

The subset of the MATLAB language that is currently supported for execution on the GPU can be found in Execute MATLAB Code on a GPU.

A = arrayfun(FUN, B, C, ...) evaluates FUN using elements of arrays B, C, ... as input arguments. The resulting GPUArray element A(i,j,...) is equal to FUN(B(i,j,...), C(i,j,...), ...). The inputs B, C, ... must all have the same size or be scalar. Any scalar inputs are scalar expanded before being input to the function FUN.

One or more of the inputs B, C, ... must be a GPUArray; any of the others can reside in CPU memory. Each array that is held in CPU memory is converted to a GPUArray before calling the function on the GPU. If you plan to use an array in several different arrayfun calls, it is more efficient to convert that array to a GPUArray before making the series of calls to arrayfun.

[A, B, ...] = arrayfun(FUN, C, ...), where FUN is a function handle to a function that returns multiple outputs, returns GPUArrays A, B, ..., each corresponding to one of the output arguments of FUN. arrayfun calls FUN each time with as many outputs as there are in the call to arrayfun. FUN can return output arguments having different classes, but the class of each output must be the same each time FUN is called. This means that all elements of A must be the same class; B can be a different class from A, but all elements of B must be of the same class, etc.

Although the MATLAB arrayfun function allows you to specify optional parameter name/value pairs, the GPUArray arrayfun method does not support these options.

Examples

If you define a MATLAB function as follows:

function [o1, o2] = aGpuFunction(a, b, c)
o1 = a + b;
o2 = o1 .* c + 2;

You can evaluate this on the GPU.

s1 = gpuArray(rand(400));
s2 = gpuArray(rand(400));
s3 = gpuArray(rand(400));
[o1, o2] = arrayfun(@aGpuFunction, s1, s2, s3);
whos
  Name        Size         Bytes  Class

  o1        400x400          108  parallel.gpu.GPUArray
  o2        400x400          108  parallel.gpu.GPUArray
  s1        400x400          108  parallel.gpu.GPUArray
  s2        400x400          108  parallel.gpu.GPUArray
  s3        400x400          108  parallel.gpu.GPUArray

Use gather to retrieve the data from the GPU to the MATLAB workspace.

d = gather(o2);

See Also

bsxfun | gather | gpuArray

  


Free Parallel Computing Interactive Kit

See how to solve large problems with minimal effort and reduce simulation time.

Get free kit

Trials Available

Try the latest versions of parallel computing products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS