code: def get_host_assignments(hosts, min_num_proc, max_num_proc=None):
      """Assign hosts with process capacities (slots) to ranks in the Horovod process.
  
      This function will try to allocate as many as possible processes on the same host to leverage
      local network.
  
      :param hosts: list of HostInfo objects describing host and slot capacity
      :type hosts: list[HostInfo]
      :param min_num_proc: minimum number of processes to be allocated
      :type min_num_proc: int
      :param max_num_proc: (optional) maximum number of processes to be allocated
      :type max_num_proc: int
      :return: a list of the allocation of process on hosts in a `SlotInfo` object.
      :rtype: list[SlotInfo]
      """
      host_ranks = []
      cross_ranks = collections.defaultdict(dict)
      rank = 0
      for host_info in hosts:
          ranks = []
          for local_rank in range(host_info.slots):
              if rank == max_num_proc:
                  break
  
              ranks.append(rank)
              rank += 1
  
              cross_ranks_at_local = cross_ranks[local_rank]
              cross_ranks_at_local[host_info.hostname] = len(
                  cross_ranks_at_local
              )
  
          host_ranks.append((host_info, ranks))
  
      world_size = rank
      if world_size < min_num_proc:
          raise ValueError(
              "Requested more processes ({}) than there are available slots ({})".format(
                  min_num_proc, world_size
              )
          )
  
      alloc_list = []
      for host_info, ranks in host_ranks:
          local_size = len(ranks)
          for local_rank, rank in enumerate(ranks):
              cross_ranks_at_local = cross_ranks[local_rank]
              cross_rank = cross_ranks_at_local[host_info.hostname]
              cross_size = len(cross_ranks_at_local)
  
              alloc_list.append(
                  SlotInfo(
                      hostname=host_info.hostname,
                      rank=rank,
                      local_rank=local_rank,
                      cross_rank=cross_rank,
                      size=world_size,
                      local_size=local_size,
                      cross_size=cross_size,
                  )
              )
  
      return alloc_list
  
file_path: horovod/horovod/runner/common/util/hosts.py
imports: import collections
  import re
  from dataclasses import dataclass
label_desc: Use image node_id 4 for calling a global function with example usage: get_host_assignments(hosts, min_num_proc, max_num_proc) and returns: alloc_list