
    :[g                        d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZ ddlZddlmZ ddlmZmZmZmZmZmZ  ej0                  d      Zd	e	d
dfdZddZd
efdZd
efdZd
efdZde	d
dfdZ d
e!fdZ"deded
efdZ#dejH                  d
efdZ%e G d de             Z&dejN                  de&d
ejN                  fdZ(ddZ) G d d      Z*y) z-XGBoost collective communication related API.    N)IntEnumunique)AnyDictListOptional   )_T)_LIB_check_call
build_infoc_strmake_jcargspy_strz[xgboost.collective]argsreturnc            	      R    t        t        j                  t        di |              y)a  Initialize the collective library with arguments.

    Parameters
    ----------
    args :
        Keyword arguments representing the parameters and their values.

        Accepted parameters:
          - dmlc_communicator: The type of the communicator.
            * rabit: Use Rabit. This is the default if the type is unspecified.
            * federated: Use the gRPC interface for Federated Learning.

        Only applicable to the Rabit communicator:
          - dmlc_tracker_uri: Hostname of the tracker.
          - dmlc_tracker_port: Port number of the tracker.
          - dmlc_task_id: ID of the current task, can be used to obtain deterministic
          - dmlc_retry: The number of retry when handling network errors.
          - dmlc_timeout: Timeout in seconds.
          - dmlc_nccl_path: Path to load (dlopen) nccl for GPU-based communication.

        Only applicable to the Federated communicator (use upper case for environment
        variables, use lower case for runtime configuration):

          - federated_server_address: Address of the federated server.
          - federated_world_size: Number of federated workers.
          - federated_rank: Rank of the current worker.
          - federated_server_cert: Server certificate file path. Only needed for the SSL
            mode.
          - federated_client_key: Client key file path. Only needed for the SSL mode.
          - federated_client_cert: Client certificate file path. Only needed for the SSL
            mode.
    N )r   r   XGCommunicatorInitr   )r   s    M/var/www/html/bid-api/venv/lib/python3.12/site-packages/xgboost/collective.pyinitr      s     B ''(;d(;<=    c                  >    t        t        j                                y)zFinalize the communicator.N)r   r   XGCommunicatorFinalizer   r   r   finalizer   6   s    ++-.r   c                  .    t        j                         } | S )zjGet rank of current process.

    Returns
    -------
    rank : int
        Rank of current process.
    )r   XGCommunicatorGetRankrets    r   get_rankr    ;   s     
$
$
&CJr   c                  .    t        j                         } | S )zdGet total number workers.

    Returns
    -------
    n : int
        Total number of process.
    )r   XGCommunicatorGetWorldSizer   s    r   get_world_sizer#   G   s     
)
)
+CJr   c                  .    t        j                         } | S )z.If the collective communicator is distributed.)r   XGCommunicatorIsDistributed)is_dists    r   is_distributedr'   S   s    ..0GNr   msgc                    t        | t              st        |       } t        j                         }|dk7  r6t	        t        j
                  t        | j                                            yt        | j                         d       y)zPrint message to the communicator.

    This function can be used to communicate the information of
    the progress to the communicator.

    Parameters
    ----------
    msg : str
        The message to be printed to the communicator.
    r   T)flushN)	
isinstancestrr   r%   r   XGCommunicatorPrintr   stripprint)r(   r&   s     r   communicator_printr0   Y   sZ     c3#h..0G!|D,,U399;-?@Aciik&r   c                      t        j                         } t        t        j                  t        j
                  |                    | j                  }|sJ t        |      S )zhGet the processor name.

    Returns
    -------
    name : str
        the name of processor(host)
    )ctypesc_char_pr   r   XGCommunicatorGetProcessorNamebyrefvaluer   )name_strr6   s     r   get_processor_namer8   m   sF      H33FLL4JKLNNEL5%=r   datarootc           	      X   t               }t        j                         }||k(  r>| J d       t        j                  | t        j
                        }t        |      |_        t        t        j                  t        j                  |      t        j                  t        j                        |             ||k7  rt        j                  |j                  z         }t        t        j                  t        j                  |t        j                        |j                  |             t        j                   |j"                        } ~| S t        t        j                  t        j                  t        j$                        t        j                        |j                  |             ~| S )aS  Broadcast object from one node to all other nodes.

    Parameters
    ----------
    data : any type that can be pickled
        Input data, if current rank does not equal root, this can be None
    root : int
        Rank of the node to broadcast data from.

    Returns
    -------
    object : int
        the result of broadcast.
    z&need to pass in data when broadcasting)protocol)r    r2   c_ulongpickledumpsHIGHEST_PROTOCOLlenr6   r   r   XGCommunicatorBroadcastr5   sizeofc_charcastc_void_ploadsrawr3   )r9   r:   ranklengthsdptrs         r   	broadcastrM   |   s6    :D^^Ft|I!IILL(?(?@1v$$LL &--"?	

 t|,/((D&//2FLL$	

 ||DHH% K 	((FOOA.@&,,PT	

 Kr   dtypec                 p   t        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      dt        j                  d	      d
t        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      dt        j                  d      di}	 |j                  t        j                  d      di       | |vrt        d|  d      ||    S # t        $ r Y #w xY w)Nfloat16r   float32r	   float64   int8   int16   int32   int64   uint8   uint16	   uint32
   uint64   float128   z
data type z* is not supported on the current platform.)nprN   update	TypeError)rN   	dtype_maps     r   
_map_dtyperj      s   
Q
Q
Q
!
1
1
1
1
A
B
BI"((:.23 I*UG+UVWWU  s   +&D) )	D54D5c                   (    e Zd ZdZdZdZdZdZdZdZ	y)	Opz#Supported operations for allreduce.r   r	   rS   re   rU   rW   N)
__name__
__module____qualname____doc__MAXMINSUMBITWISE_AND
BITWISE_ORBITWISE_XORr   r   r   rl   rl      s#    -
C
C
CKJKr   rl   opc           
      h   t        | t        j                        st        d      | j	                         j                         }t        t        j                  |j                  j                  t        j                        |j                  t        |j                        t        |                   |S )a'  Perform allreduce, return the result.

    Parameters
    ----------
    data :
        Input data.
    op :
        Reduction operator.

    Returns
    -------
    result :
        The result of allreduce, have same shape as data

    Notes
    -----
    This function is not thread-safe.
    z%allreduce only takes in numpy.ndarray)r+   rf   ndarrayrh   ravelcopyr   r   XGCommunicatorAllreducer2   data_asrF   sizerj   rN   int)r9   rw   bufs      r   	allreducer      s    & dBJJ'?@@
**,


C$$JJv/HHsyy!G		
 Jr   c                  >    t        t        j                                y)zKill the process.N)r   r   XGCommunicatorSignalErrorr   r   r   signal_errorr      s    ..01r   c                   F    e Zd ZdZdeddfdZdeeef   fdZde	ddfdZ
y)CommunicatorContextzNA context controlling collective communicator initialization and finalization.r   r   Nc                 \   || _         d}|j                  |d       y t               }|d   sy 	 ddlm} |j
                  *t        j                  j                  |j
                        }nd }|r0t        j                  j                  |d      }|| j                   |<   y y # t        $ r Y y w xY w)Ndmlc_nccl_pathUSE_DLOPEN_NCCLr   )libzlibnccl.so.2)r   getr   nvidia.ncclr   __file__ospathdirnamejoinImportError)selfr   keybinfor   r   r   s          r   __init__zCommunicatorContext.__init__   s    	88C*&'	'
 ||')+)Fww||G^<!%		#   		s   A/B 	B+*B+c                     t        di | j                   t               sJ t        j	                  d       | j                  S )Nz8-------------- communicator say hello ------------------r   )r   r   r'   LOGGERdebug)r   s    r   	__enter__zCommunicatorContext.__enter__  s4    tyyOPyyr   c                 B    t                t        j                  d       y )Nz7--------------- communicator say bye ------------------)r   r   r   )r   r   s     r   __exit__zCommunicatorContext.__exit__  s    
NOr   )rm   rn   ro   rp   r   r   r   r,   r   r   r   r   r   r   r   r      sB    Xs t 84S> Pd Pt Pr   r   )r   N)+rp   r2   loggingr   r>   enumr   r   typingr   r   r   r   numpyrf   _typingr
   corer   r   r   r   r   r   	getLoggerr   r   r   r   r    r#   r'   r0   r,   r8   rM   rN   rj   rl   ry   r   r   r   r   r   r   <module>r      s   3   	    , ,   K K			1	2!> !> !>H/
	# 		 	 'C 'D '(C ,B ,c ,b ,`bhh 3 2   BJJ B 2:: B2
'P 'Pr   