Simple Approach: 'gpuDevice(IDX)'
To use a different Graphics Processiong Unit (GPU) in each concurrent MATLAB session, use the 'gpuDevice(IDX)' form of the 'gpuDevice' function to explicitly select a different GPUDevice in each session. For example:
MATLAB Session A: >> g = gpuDevice(1);
>> x = rand(100, 'gpuArray');
MATLAB Session B:
>> g = gpuDevice(2);
>> x = ones(100, 'gpuArray');
It is important to keep in mind that 'gpuDevice', called without an index, chooses the _default_ GPU, and does not automatically select the GPU with the most available memory or the lowest computation load. For more information about 'gpuDevice', refer to the following link:
https://www.mathworks.com/help/distcomp/gpudevice.html
Alternative Approach: 'parpool'
Another alternative is to use a parallel pool of workers and an 'spmd' block to execute the different MATLAB programs on separate MATLAB workers. When workers share a single machine with multiple GPUs, MATLAB automatically assigns each worker in a parallel pool to use a different GPU by default.
Refer to the following link for more information:
Out-of-memory Errors
Out-of-memory errors can occur when multiple MATLAB programs are using the same GPU to perform calculations, unaware that they do not have exclusive access to the GPU's total amount of memory. The error might look like this:
Out of memory on device. To view more detail about available memory on the GPU, use 'gpuDevice()'. If the problem persists, reset the GPU by calling 'gpuDevice(2)'.
Best Answer