
    Гh$                     ,   % S r SSKrSSKrSSKrSSKrSSKJr  SSKJrJ	r	  SSK
JrJrJrJrJr  SSKrSSKJr  SSKJrJrJrJrJrJr  \R6                  " S	5      r\\\\4      r\\ S
'   \\\4   r!\\ S'   \ " S S5      5       r"S\SS4S jr#S$S jr$S\4S jr%S\4S jr&S\4S jr'S\SS4S jr(S\4S jr)S\S\S\4S jr*S\RV                  S\4S jr,\	 " S S\5      5       r-S\R\                  S\-S\R\                  4S  jr/S$S! jr0 " S" S#5      r1g)%z-XGBoost collective communication related API.    N)	dataclass)IntEnumunique)AnyDictOptional	TypeAliasUnion   )_T)_LIB_check_call
build_infoc_strmake_jcargspy_strz[xgboost.collective]_ArgVals_Argsc                       \ rS rSr% SrSr\\   \S'   Sr	\\   \S'   Sr
\\   \S'   Sr\\   \S'   Sr\\   \S'   S	\S
\4S jrSrg)Config   aH  User configuration for the communicator context. This is used for easier
integration with distributed frameworks. Users of the collective module can pass the
parameters directly into tracker and the communicator.

.. versionadded:: 3.0

Attributes
----------
retry : See `dmlc_retry` in :py:meth:`init`.

timeout :
    See `dmlc_timeout` in :py:meth:`init`. This is only used for communicators, not
    the tracker. They are different parameters since the timeout for tracker limits
    only the time for starting and finalizing the communication group, whereas the
    timeout for communicators limits the time used for collective operations.

tracker_host_ip : See :py:class:`~xgboost.tracker.RabitTracker`.

tracker_port : See :py:class:`~xgboost.tracker.RabitTracker`.

tracker_timeout : See :py:class:`~xgboost.tracker.RabitTracker`.

Nretrytimeouttracker_host_iptracker_porttracker_timeoutargsreturnc                 v    U R                   b  U R                   US'   U R                  b  U R                  US'   U$ )z*Update the arguments for the communicator.
dmlc_retrydmlc_timeout)r   r   selfr   s     eC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\xgboost/collective.pyget_comm_configConfig.get_comm_config8   s7    ::!!%D<<##'<<D      )__name__
__module____qualname____firstlineno____doc__r   r   int__annotations__r   r   strr   r   r   r%   __static_attributes__r(   r'   r$   r   r      sb    0  E8C=!GXc]!%)OXc])"&L(3-&%)OXc])E e r'   r   r   r   c            	      T    [        [        R                  " [        S0 U D65      5        g)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:
      - 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.

    Use upper case for environment variables, use lower case for runtime configuration.

Nr(   )r   r   XGCommunicatorInitr   r   s    r$   initr5   A   s     D ''(;d(;<=r'   c                  @    [        [        R                  " 5       5        g)zFinalize the communicator.N)r   r   XGCommunicatorFinalizer(   r'   r$   finalizer8   f   s    ++-.r'   c                  0    [         R                  " 5       n U $ )zVGet rank of current process.

Returns
-------
rank : int
    Rank of current process.
)r   XGCommunicatorGetRankrets    r$   get_rankr=   k   s     
$
$
&CJr'   c                  0    [         R                  " 5       n U $ )zPGet total number workers.

Returns
-------
n : int
    Total number of process.
)r   XGCommunicatorGetWorldSizer;   s    r$   get_world_sizer@   w   s     
)
)
+CJr'   c                  0    [         R                  " 5       n U $ )z.If the collective communicator is distributed.)r   XGCommunicatorIsDistributed)is_dists    r$   is_distributedrD      s    ..0GNr'   msgc                    [        U [        5      (       d  [        U 5      n [        R                  " 5       nUS:w  a7  [	        [        R
                  " [        U R                  5       5      5      5        g[        U R                  5       SS9  g)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)	
isinstancer0   r   rB   r   XGCommunicatorPrintr   stripprint)rE   rC   s     r$   communicator_printrL      s]     c3#h..0G!|D,,U399;-?@Aciik&r'   c                      [         R                  " 5       n [        [        R                  " [         R
                  " U 5      5      5        U R                  n[        U5      $ )zTGet the processor name.

Returns
-------
name : str
    the name of processor(host)
)ctypesc_char_pr   r   XGCommunicatorGetProcessorNamebyrefvaluer   )name_strrR   s     r$   get_processor_namerT      s?      H33FLL4JKLNNE%=r'   datarootc           	      l   [        5       n[        R                  " 5       nX:X  a=  U c   S5       e[        R                  " U [        R
                  S9n[        U5      Ul        [        [        R                  " [        R                  " U5      [        R                  " [        R                  5      U5      5        X:w  a  [        R                  UR                  -  " 5       n[        [        R                  " [        R                  " U[        R                  5      UR                  U5      5        [        R                   " UR"                  5      n AU $ [        [        R                  " [        R                  " [        R$                  " W5      [        R                  5      UR                  U5      5        AU $ )a'  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=   rN   c_ulongpickledumpsHIGHEST_PROTOCOLlenrR   r   r   XGCommunicatorBroadcastrQ   sizeofc_charcastc_void_ploadsrawrO   )rU   rV   ranklengthsdptrs         r$   	broadcastri      s2    :D^^F|I!IILL(?(?@1v$$LL &--"?	

 |,/((D&//2FLL$	

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

 Kr'   dtypec                    [         R                  " S5      S[         R                  " S5      S[         R                  " S5      S[         R                  " S5      S[         R                  " S	5      S
[         R                  " S5      S[         R                  " S5      S[         R                  " S5      S[         R                  " S5      S[         R                  " S5      S[         R                  " S5      S0n UR                  [         R                  " S5      S05        X;  a  [        SU  S35      eX   $ ! [         a     N$f = f)Nfloat16r   float32r   float64   int8   int16   int32   int64   uint8   uint16	   uint32
   uint64   float128   z
data type z* is not supported on the current platform.)nprj   update	TypeError)rj   	dtype_maps     r$   
_map_dtyper      s    
Q
Q
Q
!
1
1
1
1
A
B
BI"((:.23 *UG+UVWW  s   6'D5 5
EEc                   0    \ rS rSrSrSrSrSrSrSr	Sr
S	rg
)Op   z#Supported operations for allreduce.r   r   ro   r   rq   rs   r(   N)r)   r*   r+   r,   r-   MAXMINSUMBITWISE_AND
BITWISE_ORBITWISE_XORr1   r(   r'   r$   r   r      s#    -
C
C
CKJKr'   r   opc           
      t   [        U [        R                  5      (       d  [        S5      eU R	                  5       R                  5       n[        [        R                  " UR                  R                  [        R                  5      UR                  [        UR                  5      [        U5      5      5        U$ )zPerform 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)rH   r   ndarrayr   ravelcopyr   r   XGCommunicatorAllreducerN   data_asrb   sizer   rj   r.   )rU   r   bufs      r$   	allreducer      s    & dBJJ''?@@
**,


C$$JJv/HHsyy!G		
 Jr'   c                  @    [        [        R                  " 5       5        g)zKill the process.N)r   r   XGCommunicatorSignalErrorr(   r'   r$   signal_errorr   !  s    ..01r'   c                   J    \ rS rSrSrS\SS4S jrS\4S jrS\	SS4S jr
S	rg)
CommunicatorContexti&  zNA context controlling collective communicator initialization and finalization.r   r   Nc                 r   Xl         SnUR                  US 5      b  g [        5       nUS   (       d  g  SSKJn  UR
                  b*  [        R                  R                  UR
                  5      nOS nU(       a/  [        R                  R                  US5      nX`R                   U'   g g ! [         a     g f = f)Ndmlc_nccl_pathUSE_DLOPEN_NCCLr   )libzlibnccl.so.2)r   getr   nvidia.ncclr   __file__ospathdirnamejoinImportError)r#   r   keybinfor   r   r   s          r$   __init__CommunicatorContext.__init__)  s    	88C*&'	'
 ||')+)Fww||G^<!%		#   		s   A4B) )
B65B6c                     [        S0 U R                  D6  [        5       (       d   e[        R	                  S5        U R                  $ )Nz8-------------- communicator say hello ------------------r(   )r5   r   rD   LOGGERdebug)r#   s    r$   	__enter__CommunicatorContext.__enter__E  s7    tyyOPyyr'   c                 B    [        5         [        R                  S5        g )Nz7--------------- communicator say bye ------------------)r8   r   r   r"   s     r$   __exit__CommunicatorContext.__exit__K  s    
NOr'   r4   )r)   r*   r+   r,   r-   r   r   r   r   r   r   r1   r(   r'   r$   r   r   &  s:    Xx D 85 Pc Pd Pr'   r   )r   N)2r-   rN   loggingr   rZ   dataclassesr   enumr   r   typingr   r   r   r	   r
   numpyr   _typingr   corer   r   r   r   r   r   	getLoggerr   r.   r0   r   r/   r   r   r5   r8   r=   r@   rD   rL   rT   ri   rj   r   r   r   r   r   r   r(   r'   r$   <module>r      s`   3   	  !   8 8   K K			1	2 uS#X/) /X&y & & & &R"> ">d ">J/
	# 		 	 'C 'D '(C ,B ,c ,b ,`bhh 3 2   BJJ B 2:: B2
'P 'Pr'   