Use this skill for MATLAB code involving gpuArray, arrayfun on GPU, vectorization, value function iteration on GPU, and performance-sensitive numerical code.
Use this skill when the user asks for MATLAB code or advice involving:
gpuArrayarrayfun on GPUgpuArray.arrayfun as elementwisegpuArray.arrayfun computes one scalar output element from corresponding scalar input elements.
Implications:
max, sum, or policy search inside the arrayfun kernel.arrayfun if that helper relies on logical masks, slicing, or indexed assignment.arrayfunInside a function called by gpuArray.arrayfun:
arrayfunPreferred pattern:
(ap, a, z)u(ap,a,z) inside the kernelndgrid, repmat, or similar only to feed arrayfunarrayfun accepts them, since explicit replicated tensors add avoidable GPU memory trafficarrayfun and rely on implicit expansion instead of creating zero-padded or explicitly broadcasted copiesarrayfunAvoid:
arrayfun(@(ap,a,z) util(ap,a,z,w,r,gamma), ap_in, a_in, z_in)
Prefer:
V = arrayfun(@util_gpu, ap_in, a_in, z_in, w, r, gamma);
Where util_gpu is a separate function that receives all needed scalar inputs explicitly.
arrayfun based on the operationBefore using arrayfun, check whether the logic is clearer or more efficient with GPU-enabled built-ins and implicit expansion.
Guidance:
gpuArray.arrayfun for scalar custom logic, branching, or pointwise utility evaluation when that is the cleaner GPU formulationgpuArray.arrayfun whenever possiblearrayfun formulation existsFor Bellman or VFI code on GPU:
max(..., [], dim) outside arrayfunDo not present arrayfun as the place to perform the full Bellman maximization.
On GPU, both vectorized built-ins and arrayfun can work well, but full tensor expansion can exceed device memory.
Guidance:
V, policy objects, grids, and transition objects on the GPU once moved theregpuArray(...) and gather(...) calls inside iteration loopsarrayfunarrayfun, or a hybrid approach.arrayfun, state clearly that it is elementwise and not a reduction kernel.