code: def build_swap_map(
      matrix, swap_map, swap_ids, used_stripes, verbosity
  ):
      improvements = None
  
      # if we have a GPU and built kernels, pre-compute the needed values
      do_gpu = use_gpu()
      if do_gpu:
          if len(swap_map) == 0:
              used_stripes = [
                  s for s in range(int(matrix.shape[1] / 4))
              ]
          improvements = compute_swap_map(matrix, used_stripes)
  
      idx = 0
      updates = 0
      for src in range(matrix.shape[1] - 1):  # parallelize these loops
          for dst in range(src + 1, matrix.shape[1]):
              # swapping within a stripe does nothing
              if int(src / 4) == int(dst / 4):
                  continue
  
              # if we touched this stripe last time, update it
              if (
                  (int(src / 4) in used_stripes)
                  or (int(dst / 4) in used_stripes)
                  or len(swap_map) <= idx
              ):
                  tmp_improvement = 0.0
  
                  # use the pre-computed values from the GPU if possible, otherwise compute on the CPU
                  if do_gpu:
                      tmp_improvement = improvements[(src, dst)]
                  else:
                      tmp_mag, tmp_improvement = try_swap(
                          matrix, src, dst
                      )
                  updates += 1
  
                  if len(swap_map) <= idx:
                      swap_map.append(tmp_improvement)
                      swap_ids.append((src, dst))
                  else:
                      swap_map[idx] = tmp_improvement
                      swap_ids[idx] = (src, dst)
  
              idx += 1
  
      if verbosity > 15:
          print(f"\tupdated {updates} map entries")
      return swap_map, swap_ids
  
file_path: apex/apex/contrib/sparsity/permutation_search_kernels/channel_swap.py
imports: from .permutation_utilities import *
label_desc: Use image node_id 6 for calling a global function with example usage: build_swap_map(matrix, swap_map, swap_ids, used_stripes, verbosity) and returns: swap_map,  swap_ids