import torch


def get_backend(args):
    """Checks for available GPU and chooses the hardware device.

    :param args: object, arugments parsed by ArgumentParser.

    :return device: object, torch.device to use.

    """

    if args.verbose:
        print("Initializing hardware devices...")
    if torch.cuda.is_available() and not args.no_cuda:
        device = torch.device("cuda:0")
        args.cuda = True
    else:
        device = torch.device("cpu")
        args.cuda = False
    args.device = device
    if args.verbose:
        print(f"Found GPUs: {torch.cuda.device_count()} ")
        print(f"Running on {device}\n")

    return device