%   following guidelines.
%     * Your implementation should return a one-based index. It is 
%       acceptable to return an empty array ([]) if no device is available 
%       or no device should be used.
%     * Your implementation should not actually create any GPU data or
%       reserve a GPU device.
%     * Consider using parallel.gpu.GPUDevice.isAvailable to determine
%       whether a device is available without selecting it.
%     * The implementation we provide does not perform collective 
%       operations that communicate between workers. Collective 
%       operations would require that each worker call this function at
%       the same time, which would cause a synchronization point.
%     * Before we call this function, we verify that at least one device
%       exists and is in either default compute mode or exclusive compute
%       mode.
%     * In your implementation, if multiple workers try to use the same
%       exclusive-mode GPU device, only one of them will be able to use it.
%       The implementation we provide allows only one worker to try to use
%       each exclusive-mode device.
%
%   Example:
%     function deviceIndex = selectGPU(nodeInfo)
%         % This function assigns each worker to use one of the GPU devices
%         % that are in 'default' compute mode in a round-robin fashion,
%         % that is, worker 1 uses device 1, worker 2 device 2, and so on.
%         % Devices in 'exclusive' compute mode are ignored.
%         deviceIndex = [];
%         defaultDevices = nodeInfo.DefaultModeDevices;
%         myWorkerIndex = nodeInfo.MyWorkerIndex;
%
%         nD = numel(defaultDevices);
%         if nD > 0
%             % Index into the list of default mode devices using
%             % my worker index. If there are more workers than devices,
%             % multiple workers share devices. 
%             deviceIndex = defaultDevices(mod(myWorkerIndex-1, nD) + 1);
%         end
%     end
%
%   See also parallel.gpu.NodeInfo, parallel.gpu.GPUDevice.isAvailable.

% Copyright 2012-2013 The MathWorks, Inc.

exclusiveDevices = nodeInfo.ExclusiveModeDevices;
defaultDevices = nodeInfo.DefaultModeDevices;
myWorkerIndex = nodeInfo.MyWorkerIndex;
numWorkers = nodeInfo.NumNodeWorkers;

nE = numel(exclusiveDevices);
nD = numel(defaultDevices);

if myWorkerIndex <= nE
    % In this implementation, one and only one worker will try to access a
    % given exclusive-mode device: the first nE workers will each try one
    % exclusive-mode device. This minimizes the possibility of MATLAB
    % causing race conditions. If any of the exclusive mode devices is not
    % available, use a default mode device instead, or error if there are
    % none.
    
    if numWorkers == 1
        % If I'm the only worker on the node, no race conditions are 
        % possible: try all the devices.
        idx = 0;
        deviceFound = false;
        while ~deviceFound && (idx < nE)
            idx = idx+1;
            deviceIndex = exclusiveDevices(idx);
            deviceFound = parallel.gpu.GPUDevice.isAvailabl