MATLAB: Two GPU computing simultaneously

gpu parallel computingParallel Computing Toolbox

I have two physical Nvidia GTX 1080 graphic cards and was wondering how to run two GPU computing simultaneously. Read some articles saying the combination of parfor and gpuArray would work but never found any example. Wrote some code in the following but does not work. Please help.
clear all
tic
test=ones(2048,2048,800,'uint16');
parfor i=1:2
gpuDevice(i);
gtest{i}=gpuArray(test(:,:,(400*(i-1)+1:400*i)));
end
test1=gather(gtest{1});
test2=gather(gtest{2});

Best Answer

As Edric commented, I don't see why your code would cause an error, but the parfor loop will be inefficient as written because "test" will be treated as a broadcast variable and cloned on each parallel worker.
To send smaller chunks to different parallel workers, you should divide it into cells using, for example MAT2TILES (download here).
testCell=mat2tiles(test,[inf,inf,400]);
parfor i=1:2
gpuDevice(i);
gtest{i}=gpuArray(testCell{i});
end
Form of Indexing. Within the list of indices for a sliced variable, one of these indices is of the form i, i+k, i-k, k+i, or k-i. The index i is the loop variable and k is a constant or a simple (nonindexed) broadcast variable. Every other index is a scalar constant, a simple broadcast variable, a nested for-loop index, colon, or end.