code: def set_cpu_affinity():
      """
      Setup CPU affinity.
      Each GPU would be bound to a specific set of CPU cores for optimal and stable performance.
      This function would obtain GPU-CPU affinity via "nvidia-smi topo -m", then equally distribute
      CPU cores to each GPU.
      """
  
      (
          gpu_cpu_affinity_map,
          cpu_socket_gpus_list,
          cpu_core_groups,
      ) = _get_gpu_affinity_table()
  
      node_num = paddle.distributed.fleet.node_num()
      gpu_per_node = paddle.distributed.get_world_size() // node_num
      local_rank = paddle.distributed.get_rank() % gpu_per_node
  
      # gpu_cpu_affinity_map (dict): Key is GPU ID and value is cpu_affinity string.
      # cpu_socket_gpus_list (dict): Key is cpu_affinity string and value is  a list
      #                              collected all GPU IDs that affinity to this cpu socket.
      # cpu_core_groups (dict):      Key is cpu_affinity string and value is cpu core groups.
      #                              cpu core groups contains #GPUs groups, each group have,
      #                              nearly eaual amount of cpu cores.
      # Example:
      # $nvidia-smi topo -m
      #        GPU0    GPU1    GPU2    GPU3    CPU Affinity    NUMA Affinity
      # GPU0     X     SYS     SYS     SYS      0-9,20-29           0
      # GPU1   SYS       X     SYS     SYS      0-9,20-29           0
      # GPU2   SYS      SYS      X     SYS      10-19,30-39         1
      # GPU3   SYS      SYS    SYS       X      10-19,30-39         1
      #
      # gpu_cpu_affinity_map =
      #     { 0: '0-9,20-29',
      #       1: '0-9,20-29',
      #       2: '10-19,30-39',
      #       3: '10-19,30-39' }
      # cpu_socket_gpus_list =
      #     { '0-9,20-29': [0, 1],
      #       '10-19,30-39': [2, 3] }
      # cpu_core_groups =
      #     {'0-9,20-29': [
      #                     [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]],
      #                     [[20, 22, 24, 26, 28], [21, 23, 25, 27, 29]]
      #                    ],
      #       '10-19,30-39': [
      #                        [[10, 12, 14, 16, 18], [11, 13, 15, 17, 19]],
      #                        [[30, 32, 34, 36, 38], [31, 33, 35, 37, 39]]
      #                       ]}
      #
      # for rank-0, it belong to '0-9,20-29' cpu_affinity_key,
      # and it locate in index-0 of cpu_socket_gpus_list['0-9,20-29'],
      # therefore, affinity_mask would be a collection of all cpu cores
      # in index-0 of cpu_core_groups['0-9,20-29'], that is [0, 2, 4, 6, 8]
      # and [20, 22, 24, 26, 28].
      # affinity_mask = [0, 2, 4, 6, 8, 20, 22, 24, 26, 28]
      affinity_mask = list()
      cpu_affinity_key = gpu_cpu_affinity_map[local_rank]
      cpu_core_idx = cpu_socket_gpus_list[cpu_affinity_key].index(
          local_rank
      )
      for cpu_core_group in cpu_core_groups[cpu_affinity_key]:
          affinity_mask.extend(cpu_core_group[cpu_core_idx])
  
      pid = os.getpid()
      os.sched_setaffinity(pid, affinity_mask)
      logging.info(
          "Set CPU affinity of rank-%d (Process %d) " "to %s.",
          local_rank,
          pid,
          str(os.sched_getaffinity(pid)),
      )
  
imports: import os
  import logging
  import paddle
label_desc: Use image node_id 4 for calling a global function with example usage: set_cpu_affinity() without return types