MATLAB Answers

0

Declare gpuArray while executing on gpu

Asked by cedric W on 4 Jan 2019
Latest activity Commented on by Joss Knight
on 7 Jan 2019
I'm trying to modify my code working well for a CPU setup to a GPU setup. It appears to be more complicated than what I had expected.
I'm using arrayfun to send the computation to GPU for my monte carlo simulations:
MC_Paths = arrayfun(@Heston_One_Simulation, Param1, Param2, and so on)
with the beginning of the function Heston_One_Simulation defined as
function [S] = Heston_One_Simulation(Param1,Params2, so on...)
HistoCorrelatedPaths1=zeros(N,NbAssets,BatchSize,'gpuArray');
...
end
And I get the following error message:
Function passed as first input argument contains unsupported or unknown function 'zeros'. For more information see Tips and Restrictions.
Is it impossible to declare new variables while running on a GPU ?
I'm sure I'll have more problems once this one is solved but let's start with that one.

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Joss Knight
on 5 Jan 2019

GPU arrayfun functions can only do scalar operations. You can declare new scalar variables but you can't create new arrays.

  4 Comments

Show 1 older comment
You should read up on GPU arrayfun by following the link in that error. You cannot have an output that is a different size from the input for gpuArray inputs (apart from within the rules of singleton dimension expansion).
This is what I read about the output size hence my second question.
My underlying question was to know what could be a workaround I should think about before starting implementing things that won't suit in the end. Do you have any possible solutions ?
GPU is suited for Monte Carlo simulations. But MC Sim are generally used to get the full path of the stock prices (N above is the number of paths per simulation), otherwise the product is too complex to price. So I guess that there is a way to land on my feet.
Most problems are solved using a combination of arrayfun, pagefun, and standard vectorisation. You need to look for opportunity to batch up all your work as much as possible. Arrayfun can be used wherever you have a sequence of element-wise maths, but isn't usually necessary because MATLAB is clever enough to group all those operations into a single kernel for you where possible.
Essentially you are looking to remove all the loops in your code and replace them with masks, vector algebra and arrayfun/pagefun calls.
This is a good blog post to get you on the right track:

Sign in to comment.