code: def get_cuda_context() -> CudaContext:
      """Check whether the current process already has a CUDA context created."""
  
      _init()
      if _init_pid is None:
          return CudaContext(has_context=False)
  
      for index in range(_get_all_device_count()):
          handle = get_handle_by_index(index)
          try:
              mig_current_mode, mig_pending_mode = get_mig_mode(handle)
          except NVMLAPIError as e:
              if e.errno == NVML_ERROR_NOT_SUPPORTED:
                  mig_current_mode = NVML_DEVICE_MIG_DISABLE
              else:
                  raise
          if mig_current_mode == NVML_DEVICE_MIG_ENABLE:
              for mig_index in range(get_max_mig_device_count(handle)):
                  try:
                      mig_handle = get_mig_device_handle_by_index(
                          handle, mig_index
                      )
                  except NVMLAPIError as e:
                      if e.errno == NVML_ERROR_NOT_FOUND:
                          # No MIG device with that index
                          continue
                      else:
                          raise
                  if _running_process_matches(mig_handle):
                      return CudaContext(
                          has_context=True,
                          device_info=CudaDeviceInfo(
                              uuid=get_uuid(handle),
                              device_index=index,
                              mig_index=mig_index,
                          ),
                      )
          else:
              if _running_process_matches(handle):
                  return CudaContext(
                      has_context=True,
                      device_info=CudaDeviceInfo(
                          uuid=get_uuid(handle), device_index=index
                      ),
                  )
  
      return CudaContext(has_context=False)
  
imports: import dataclasses
  import logging
  import os
  import subprocess
  import sys
  import uuid
  from collections import namedtuple
  from ctypes import c_char, c_char_p, c_int, c_uint, c_ulonglong, byref, create_string_buffer, Structure, POINTER, CDLL
  from typing import List, Tuple, Optional, Union
  from ..utils import parse_readable_size
label_desc: Use image node_id 24 for calling a global function with example usage: get_cuda_context() and returns: CudaContext, CudaContext, CudaContext, CudaContext