JRAD
JRAD

Reputation: 11

GPU MATLAB gives different elapsed time between first and second execution

When i execute my code using Matlab parallel toolbox it gives me two different time execution between first and second time.

In fact the first time is very slow (more than CPU version) however the second time is faster and logical, and subsequent runs are the same as the second time. Why does this happen?

Upvotes: 0

Views: 58

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

That is correct, and expected.

When you call it for the first time, it needs to initialize the GPU ("turn it on" in some sense), set up the CUDA contexts, etc etc. The second time you run it the GPU is ready to take anything you throw at it.

On top of that, depends on how you wrote the code, maybe the first time it requires to move some data to the GPU, and perhaps in the second time the memory is already there.

Often doing gpuDevice(1) will initialize the context enough, but otherwise just throw a small matrix multiplication to it to initialize.

All this is somehow true for other parallel computing paradigms in MATLAB, e.g. if you want to use parfor you need to initialize the parallel pool or it will take very long the first time.

Upvotes: 3

Related Questions