I am trying to simulate a CIR process using Matlab and my GPU for effeciency. At the moment i run into some implementation problems due to the recursive nature of the discretization.

The sheme I currently use is the simple Euler

$$V_t \approx \max \{V_{t-1} + \kappa_V(\bar{V}-V_{t-1})\Delta h + \sigma_V\sqrt{V_{t-1}}\sqrt{\Delta h}\mathcal{N}(0,1),0\}. $$

To proper simulate on the GPU I need some help function, like discribed in this Matlab guide

function [ V ] = simulation_fun( V, W, N, h, kappa_V, V_bar, sigma_V)        
while t < N    % a path has N steps 
     V(:,t+1)=max(V(:,t)+kappa_V*(V_bar-V(:,t))*h + sigma_V*sqrt(V(:,t))*sqrt(h)*W(:,t),0);

Than I define the model parameters

M=1000;  % Number of MC simulations

After that I call the simulation via

V = arrayfun(@simulation_fun,V, W, N, h, kappa_V, V_bar, sigma_V);

and get the follwing error

Error using gpuArray/arrayfun
Indexing is not supported. error at line: 3  .

However, if I use

V = simulation_fun(V, W, N, h, kappa_V, V_bar, sigma_V);

it works, but the simulation is roughly 2.5 times slower than only using the CPU.

Does anyone know how to correctly implement the simulation?


2 Answers 2


You should write some kernel functions in CUDA (Nvidia language) for your matlab code. Arrayfun is quite restrictive and not appropriate. Look at this link http://fr.mathworks.com/help/distcomp/run-cuda-or-ptx-code-on-gpu.html for more details about matlab and parallel computing.


There are some restrictions to using arrayfun. You can read the restrictions here.

Judging from the error, you cannot use indexes the way you are.

You probably have to create separate GPU arrays for $ V_{t+1} $ and $V_t$.

I suggest that you find similar examples in Matlab's website and try to replicate its functionality. Here is an article with information on how to program the second order wave equation on a GPU with Matlab.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.