o
    i(                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ejdkr2dZd	Znejd
kr<dZdZndZdZdd Zdd ZdddZdd Zdd ZdddZdd ZdS )a2  Cudatoolkit libraries lookup utilities.

Cudatoolkit libraries can be available via either:

- the `cudatoolkit` conda package,
- a user supplied location from CUDA_HOME,
- a system wide location,
- package-specific locations (e.g. the Debian NVIDIA packages),
- or can be discovered by the system loader.
    N)find_lib)get_cuda_paths)locate_driver_and_loaderload_driver)CudaSupportErrorwin32z%s.dllz%s.libdarwinzlib%s.dylibzlib%s.azlib%s.soc                  C   s   t  } | d j}|S )N	libdevice)r   info)dpaths r   f/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/numba/cuda/cudadrv/libs.pyget_libdevice!   s   
r   c                  C   s8   t t d} |  W  d    S 1 sw   Y  d S )Nrb)openr   read)Zbcfiler   r   r   open_libdevice'   s   $r   Fc                 C   sb   | dkrt  d jptd S |rdnd}t  | j}t| ||d}|r%tnt}|r-t|S ||  S )a  
    Find the path of a CUDA library based on a search of known locations. If
    the search fails, return a generic filename for the library (e.g.
    'libnvvm.so' for 'nvvm') so that we may attempt to load it using the system
    loader's search mechanism.
    nvvmstatic_cudalib_dircudalib_dirstatic)r   r
   _dllnamepatternr   _staticnamepatternmax)libr   dir_typelibdir
candidatesZnamepatternr   r   r   get_cudalib,   s   r    c                 C   s   t | }t|S )N)r    ctypesCDLL)r   pathr   r   r   open_cudalib>   s   
r$   c                 C   s   t j| st|  dd S )Nz
 not found)osr#   isfileFileNotFoundError)r#   r   r   r   check_static_libC   s   r(   c                 C   s@   | dkr
t  d jS | dkrt  d jS |rdnd}t  | jS )Nr   r	   r   r   )r   Zby)r   r   r   r   r   r   _get_source_variableH   s   r)   c                  C   s  d} z6t  \}}td |D ]	}td|  qtd|  tddd t||\}}td td	|  W n tyS } ztd
|  d} W Y d}~nd}~ww tjdkr| st }tj	tjj
d| d}zt|}	|	 }
W d   n1 sw   Y  W n ty   td| d Y nw tdd |
 D }td |D ]	}td|  qd }|D ]A}t|}td|t| td| ztddd t| td W q ty } ztd||f  d} W Y d}~qd}~ww d}t|dd}td|t|dd td| ztddd t| td W n ty@ } ztd||f  d} W Y d}~nd}~ww td}td |  t }td| ztddd t| td W |  S  ty } ztd||f  d} W Y d}~|  S d}~ww )!z:Test library lookup.  Path info is printed to stdout.
    FzFinding driver from candidates:	zUsing loader z	Trying to load driverz...)endz	okz		Loaded from z	ERROR: failed to open driver: TNlinuxprocmapsz	ERROR: Could not open z) to determine absolute path to libcuda.soc                 s   s    | ]	}d |v r|V  qdS )z
libcuda.soNr   ).0sr   r   r   	<genexpr>~   s    ztest.<locals>.<genexpr>z	Mapped libcuda.so paths:z		znvvm nvrtc cudartzFinding {} from {}z	Located atz	Trying to open libraryz	ERROR: failed to open %s:
%sZ	cudadevrtr   z	Checking libraryz	ERROR: failed to find %s:
%sr	   zFinding libdevice from )r   printr   r   sysplatformr%   getpidr#   joinsepr   r   OSErrorsetsplitr    formatr)   r$   r(   r'   r   )failedZdlloaderr   locationZdllr#   epidZmapsfilefr.   	locationsZlibsr   wherer   r   r   testR   s   



	




rC   )F)__doc__r%   r3   r!   Znumba.misc.findlibr   Znumba.cuda.cuda_pathsr   Znumba.cuda.cudadrv.driverr   r   Znumba.cuda.cudadrv.errorr   r4   r   r   r   r   r    r$   r(   r)   rC   r   r   r   r   <module>   s.    




