o
    viO                     @   s   d dl Z d dlmZ d dlmZ d dlmZ G dd deZG dd deZg d	Z	g d
Z
e defddZe defddZe defddZejfdedeeef fddZejfdedeeef fddZejfdedeeef fddZdS )    N)Enum)	lru_cache)Dictc                   @      e Zd ZdZdZdZdS )ClusterTypelocalZngcZslurmN)__name__
__module____qualname__LOCALZNGCZSLURM r   r   U/data/cameron/vidgen/cosmos-policy/cosmos_policy/_src/imaginaire/utils/cluster_env.pyr          r   c                   @   r   )ClusterEnvInfobasicZdetailedallN)r   r	   r
   BASICDETAILEDALLr   r   r   r   r      r   r   )Z
NGC_JOB_IDNGC_ARRAY_SIZEZNGC_GPUS_PER_NODE)ZSLURM_JOB_USERZSLURM_JOB_PARTITIONZSLURM_LOG_DIRZSLURM_JOBIDZSLURM_NNODESZSLURM_JOB_NAMEZSLURM_JOB_NODELISTZSLURMD_NODENAMEreturnc                   C   s   t   ot  S )z:
    Check if the code is running on a local machine.
    )is_ngcis_slurmr   r   r   r   is_local4   s   r   c                   C   
   dt jv S )z.
    Check if the code is running on NGC.
    r   osenvironr   r   r   r   r   <      
r   c                   C   r   )z0
    Check if the code is running on SLURM.
    SLURM_JOB_IDr   r   r   r   r   r   D   r   r   levelc                 C   sZ   | t jkrdd tD S | t jkrdd tjD S | t jkr&dd tjD S td|  )a`  
    Retrieves NVIDIA GPU Cloud (NGC) environment variables based on the specified detail level.
    The function filters environment variables to include only those relevant to NGC,
    differentiated by the detail level specified.

    Parameters:
        level (ClusterInfoLevel): The level of detail for the information returned.
                                  Defaults to ClusterInfoLevel.BASIC.

    Returns:
        dict: A dictionary containing the environment variables. If the level is BASIC,
              it includes only predefined key variables that are considered basic.
              If the level is DETAILED, it includes all environment variables that start
              with "NGC_".

    Raises:
        ValueError: If an unknown level is specified, an exception is raised indicating that the
                    level is not recognized.
    c                 S   "   i | ]}|t jv r|t j| qS r   r   .0kr   r   r   
<dictcomp>a      " zget_ngc_env.<locals>.<dictcomp>c                 S   "   i | ]}| d r|tj| qS )ZNGC_
startswithr   r   r"   r   r   r   r%   c   r&   c                 S      i | ]\}}||qS r   r   r#   r$   vr   r   r   r%   e       Unknown level )r   r   NGC_ENV_BASIC_VARSr   r   r   r   
ValueErrorr    r   r   r   get_ngc_envL   s   


r2   c                 C   s^   | t jkrdd tD S | t jkrdd tjD S | t jkr(dd tj D S td|  )a'  
    Retrieves SLURM environment variables based on the specified detail level.
    This function filters the environment variables related to the SLURM job scheduler
    environment based on the provided detail level of the cluster information.

    Parameters:
        level (ClusterEnvInfo): The detail level of the environment variables to retrieve.
                                This can be BASIC, DETAILED, or ALL. Defaults to BASIC.

    Returns:
        Dict[str, str]: A dictionary containing the SLURM environment variables. The contents of
                        the dictionary vary based on the level:
                        - BASIC: Returns predefined key variables important for basic SLURM variables.
                        - DETAILED: Includes all variables that start with "SLURM_".
                        - ALL: Returns all environment variables available in the current session.

    Raises:
        ValueError: If an unknown level is specified, it raises an exception indicating
                    that the level is not recognized.
    c                 S   r!   r   r   r"   r   r   r   r%      r&   z!get_slurm_env.<locals>.<dictcomp>c                 S   r'   )ZSLURM_r(   r"   r   r   r   r%      r&   c                 S   r*   r   r   r+   r   r   r   r%      r-   r.   )	r   r   SLURM_ENV_BASIC_VARSr   r   r   r   itemsr0   r1   r   r   r   get_slurm_envj   s   


r5   c                 C   sF   dt  i}| tjkr|t j |S |t|  |t|  |S )an  
    Retrieves a combination of environment variables from the cluster, merging information from
    both NVIDIA GPU Cloud (NGC) and SLURM environments based on the specified detail level.
    This function provides a unified dictionary of environment settings that are crucial for
    applications running in clustered computing environments.

    Parameters:
        level (ClusterEnvInfo): The level of detail for the environment variables to be retrieved.
                                The level can be BASIC, DETAILED, or ALL. Defaults to BASIC.
                                - BASIC: Gathers basic environment variables from both NGC and SLURM.
                                - DETAILED: Includes more detailed information from both NGC and SLURM.
                                - ALL: Combines all available environment variables from the system
                                       with NGC and SLURM specific ones.

    Returns:
        Dict[str, str]: A dictionary containing key-value pairs of environment variables.
                        Initially includes the current working directory under the key 'PWD'.
    ZPWD)r   getcwdr   r   updater   r2   r5   )r    Zenv_infor   r   r   get_cluster_env   s   
r8   )r   enumr   	functoolsr   typingr   r   r   r/   r3   boolr   r   r   r   strr2   r5   r8   r   r   r   r   <module>   s"     $