Apply function to each page of array on GPU
A = pagefun(FUN,B)
A = pagefun(FUN,B,C,...)
[A,B,...] = pagefun(FUN,C,...)
pagefun iterates over the pages of a gpuArray, applying the same function to each page.
A = pagefun(FUN,B) applies the function specified by FUN to each page of the gpuArray B, and returns the results in gpuArray A, such that A(:,:,I,J,...) = FUN(B(:,:,I,J,...)). FUN is a handle to a function that takes a two-dimensional input argument.
You can use gather to retrieve the array from the GPU back to the MATLAB workspace.
A = pagefun(FUN,B,C,...) evaluates FUN using pages of the arrays B, C, etc., as input arguments with scalar expansion enabled. Any of the input page dimensions that are scalar are virtually replicated to match the size of the other arrays in that dimension so that A(:,:,I,J,...) = FUN(B(:,:,I,J,...), C(:,:,I,J,...),...). At least one of the inputs B, C, etc. must be a gpuArray. Any other inputs held in CPU memory are converted to a gpuArray before calling the function on the GPU. If an array is to be used in several different pagefun calls, it is more efficient to convert that array to a gpuArray before your series of pagefun calls. The input pages B(:,:,I, J, ...), C(:,:,I, J, ...), etc., must satisfy all of the input and output requirements of FUN.
[A,B,...] = pagefun(FUN,C,...), where FUN is a handle to a function that returns multiple outputs, returns gpuArrays A, B, etc., each corresponding to one of the output arguments of FUN. pagefun invokes FUN with as many outputs as there are in the call to pagefun. 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.
FUN must return values of the same class each time it is called. The order in which pagefun computes pages 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).
Currently the supported values for FUN are:
Most element-wise gpuArray functions, listed in Run Built-In Functions on a GPU , and the following:
@mldivide for square matrices of sizes up to 32-by-32
M = 3; % output number of rows K = 6; % matrix multiply inner dimension N = 2; % output number of columns P1 = 10; % size of first page dimension P2 = 17; % size of second page dimension P3 = 4; % size of third page dimension P4 = 12; % size of fourth page dimension A = rand(M,K,P1,1,P3,'gpuArray'); B = rand(K,N,1,P2,P3,P4,'gpuArray'); C = pagefun(@mtimes,A,B); s = size(C) % M-by-N-by-P1-by-P2-by-P3-by-P4
s = 3 2 10 17 4 12
M = 300; % output number of rows K = 500; % matrix multiply inner dimension N = 1000; % output number of columns P = 200; % number of pages A = rand(M,K,'gpuArray'); B = rand(K,N,P,'gpuArray'); C = pagefun(@mtimes,A,B); s = size(C) % returns M-by-N-by-P
s = 300 1000 200