MATLAB: Is it possible to combine computation on a GPU with computation on several CPUs

Parallel Computing Toolbox

I have a good graphics card and a multi-core CPU. In order to increase the performance I would like to combine GPU computation with computation on all my cores.

Best Answer

It might be possible to combine computation on a GPU with computation on several CPUs , but it is likely to be difficult to get the load-balancing right to get additional benefit. Here is one outline of how one might approach this:
performComputationOnGPU = @()norm(gpuArray.rand(1000)*gpuArray.rand(1000), Inf)
performComputationOnCPU = @()norm(rand(1000)*rand(1000), Inf)
p = gcp;
numGpuTasks = 0:p.NumWorkers;
N = 4 * p.NumWorkers;
for nt = 1:numel(numGpuTasks)
tic
parfor idx = 1:N
t = getCurrentTask();
if t.ID <= numGpuTasks(nt)
output(idx) = performComputationOnGPU();
wait(gpuDevice)
else
output(idx) = performComputationOnCPU();
end
end
mixedParallelTime(nt) = toc;
end
mixedParallelTime
tic
for idx = 1:N
performComputationOnGPU();
end
wait(gpuDevice)
hostTimeGPU = toc
tic
for idx = 1:N
performComputationOnCPU();
end
hostTimeCPU = toc
On one machine used for testing the code the computing times are as follows:
mixedParallelTime =
0.7326 0.5400 0.5603 0.5583 1.0859 1.0802 1.6064 1.5975 2.1307
hostTimeGPU =
0.2659
hostTimeCPU =
1.4987
In other words, there is some benefit to running in mixed-parallel mode - but actually the PARFOR overhead and missed load-balancing (since there are relatively few relatively quick tasks) means that simply running the
host-GPU version is quicker in this case. It is worth pointing out that the "purely-GPU" time can actually also be affected by the CPU performance.
Related Question