o
    iu                     @   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 )a  CUDA Toolkit libraries lookup utilities.

CUDA Toolkit libraries can be available via either:

- the `cuda-nvcc` and `cuda-nvrtc` conda packages for CUDA 12,
- the `cudatoolkit` conda package for CUDA 11,
- 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   T/home/ubuntu/veenaModal/venv/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)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
candidates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_libD   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   by)r   r   r   r   r   r   _get_source_variableI   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:
%s	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   )faileddlloaderr    locationdllr%   epidmapsfilefr1   	locationslibsr   wherer   r   r   testS   s   



	




rK   )F)__doc__r'   r7   r#   numba.misc.findlibr   numba.cuda.cuda_pathsr   numba.cuda.cudadrv.driverr   r   numba.cuda.cudadrv.errorr   r8   r   r   r   r   r"   r&   r*   r,   rK   r   r   r   r   <module>   s.    




