code: def _get_cuda_device(requested_device_id: int):
      """
      Find an EGL device with a given CUDA device ID.
  
      Args:
          requested_device_id: The desired CUDA device ID, e.g. "1" for "cuda:1".
  
      Returns:
          EGL device with the desired CUDA ID.
      """
      num_devices = egl.EGLint()
      if (
          # pyre-ignore Undefined attribute [16]
          not egl.eglQueryDevicesEXT(
              0, None, ctypes.pointer(num_devices)
          )
          or num_devices.value < 1
      ):
          raise RuntimeError(
              "EGL requires a system that supports at least one device."
          )
      devices = (
          egl.EGLDeviceEXT * num_devices.value
      )()  # array of size num_devices
      if (
          # pyre-ignore Undefined attribute [16]
          not egl.eglQueryDevicesEXT(
              num_devices.value, devices, ctypes.pointer(num_devices)
          )
          or num_devices.value < 1
      ):
          raise RuntimeError("EGL sees no available devices.")
      if len(devices) < requested_device_id + 1:
          raise ValueError(
              f"Device {requested_device_id} not available. Found only {len(devices)} devices."
          )
  
      # Iterate over all the EGL devices, and check if their CUDA ID matches the request.
      for device in devices:
          available_device_id = egl.EGLAttrib(ctypes.c_int(-1))
          # pyre-ignore Undefined attribute [16]
          egl.eglQueryDeviceAttribEXT(
              device, EGL_CUDA_DEVICE_NV, available_device_id
          )
          if available_device_id.contents.value == requested_device_id:
              return device
      raise ValueError(
          f"Found {len(devices)} CUDA devices, but none with CUDA id {requested_device_id}."
      )
  
imports: import contextlib
  import ctypes
  import os
  import threading
  from typing import Any, Dict
  import OpenGL.EGL
  import pycuda.driver
  from OpenGL._opaque import opaque_pointer_cls
  from OpenGL.raw.EGL._errors import EGLError
label_desc: Use image node_id 3 for calling a global function with example usage: _get_cuda_device(requested_device_id) and returns: device