code: def init_affinity_at_program_beginning():
      if TUTEL_CUDA_SANDBOX:
          return
      try:
          numa_type = int(os.environ.get("NUMA_TYPE", "1"))
          if numa_type <= 0:
              return
          group_rank = int(os.environ.get("LOCAL_RANK", "0"))
          nodes = sorted(
              [
                  int(x[4:])
                  for x in os.listdir("/sys/devices/system/node")
                  if re.match("node[0-9]+", x)
              ]
          )
          cpus = [
              sorted(
                  [
                      int(x[3:])
                      for x in os.listdir(
                          "/sys/devices/system/node/node%d" % node_id
                      )
                      if re.match("cpu[0-9]+", x)
                  ]
              )
              for node_id in nodes
          ]
          sel_node = (group_rank // numa_type) % len(nodes)
          os.sched_setaffinity(0, cpus[sel_node])
          logging.info(
              "LOCAL_RANK %d is to set NUMA node: %d (total NUMA nodes = %d)"
              % (group_rank, sel_node, len(nodes))
          )
      except Exception as ex:
          if group_rank == 0:
              logging.warning("Failed to set NUMA status: %s" % ex)
  
file_path: tutel/tutel/system.py
imports: import os, sys
  import re
  import logging
label_desc: Use image node_id 1 for calling a global function with example usage: init_affinity_at_program_beginning() without return types