import torch


def get_device(gpu_list=None):
    """Get a Pytorch device corresponding to one of the GPU indices listed
    in gpu_list.  If gpu_list is empty, get the device corresponding
    to the CPU instead.  If gpu_list is None (the default), enumerate
    the available GPU indices and pick one as though the list had been
    passed directly, except that in the case of there being no GPUs,
    an IndexError will be thrown.

    The return value is a pair of the device and a boolean that is
    true if the returned device is a GPU device.

    Note that we currently return the first listed device.

    """
    if gpu_list is None:
        gpu_list = list(range(torch.cuda.device_count()))
    elif not gpu_list:
        return torch.device('cpu'), False
    return torch.device('cuda:{}'.format(gpu_list[0])), True