"""Example 0 — Discover the platforms and the devices (OpenCL model).

Concepts: platform (a vendor's driver), device, hardware characteristics,
choice of the device.

    python examples/00_discovery.py
"""

import common  # noqa: F401  (makes unigpu importable without installing it, e.g. on iPad)

import unigpu as gpu

for platform in gpu.get_platforms():
    print(f"Platform {platform.backend}:{platform.index}: {platform.name} "
          f"({platform.vendor}, {platform.version})")
    for device in platform.get_devices():
        info = device.info
        print(f"  - {info.name} [{info.device_type.label}]")
        print(f"      compute units: {info.compute_units}, threads per block: "
              f"{info.max_threads_per_block}, shared memory: "
              f"{info.shared_memory_per_block // 1024} KiB, float64: "
              f"{'yes' if info.supports_float64 else 'no'}")

# GPUs only (like clGetDeviceIDs(..., CL_DEVICE_TYPE_GPU, ...))
print("Detected GPUs:", [d.name for d in gpu.list_devices(gpu.DeviceType.GPU)] or "none")

# Complete report, in the manner of numba.cuda.detect()
gpu.detect()

# Choose a device: by object, by rank, by type or by name
gpu.select_device(gpu.DeviceType.SIMULATOR)
print("Current device:", gpu.get_current_device())
with gpu.gpus[0]:  # temporarily, like "with numba.cuda.gpus[0]:"
    print("Inside the with block:", gpu.get_current_device())
