Repository: google/gemma.cpp
File: util/threading.h (C/C++)

Code before:
    // Regardless of topology, ignore LPs disabled via OS, taskset, or numactl.
    LPS enabled_lps;
    if (HWY_UNLIKELY(!GetThreadAffinity(enabled_lps))) {
      const size_t num_lps = hwy::TotalLogicalProcessors();
      fprintf(
          stderr,
          "Warning, unknown OS affinity, considering all %zu LPs enabled\n.",
          num_lps);
      for (size_t lp = 0; lp < num_lps; ++lp) {
        enabled_lps.Set(lp);
      }
    }

    // Without threading support, only keep the first enabled LP; it might still
    // make sense to pin the main thread.
    if (HWY_UNLIKELY(!hwy::HaveThreadingSupport())) {
      HWY_ASSERT(enabled_lps.Any());
      const size_t lp = enabled_lps.First();
      enabled_lps = LPS();
      enabled_lps.Set(lp);
    }

#if !GEMMA_DISABLE_TOPOLOGY
    if (HWY_LIKELY(!topology_.packages.empty())) {
      InitFromTopology(enabled_lps, package_slice, cluster_slice);
    }

    // Topology unknown or no packages with enabled LPs: create a single
    // package with one cluster, and one node.
    if (HWY_UNLIKELY(NumPackages() == 0)) {
      InitFromSlice(enabled_lps, lp_slice);
    }
#else
    // Topology disabled: create a single package with one cluster, and one
    // node.
    InitFromSlice(enabled_lps, lp_slice);
#endif

    HWY_ASSERT(NumPackages() != 0 && NumClusters(0) != 0 && NumNodes() != 0);
  }

  size_t NumPackages() const { return packages_.size(); }
  const char* TopologyString() const { return topology_string_; }
  size_t NumNodes() const { return nodes_.Count(); }

  class Cluster {
   public:
    // Topology is unknown, rely on OS affinity and user-specified slice.
    Cluster(const LPS& enabled_lps, BoundedSlice lp_slice) {
      // Interpret `lp_slice` as a slice of the 1-bits of `enabled_lps`, so
      // we honor both the OS affinity and the user-specified slice. Note that

Review comment (suggestion):
I think we could just put an `#endif` here - without InitFromTopology, we will not have filled packages_, which is what NumPackages checks, so we can use the same fallback.

Code after:
    // Regardless of topology, ignore LPs disabled via OS, taskset, or numactl.
    LPS enabled_lps;
    if (HWY_UNLIKELY(!GetThreadAffinity(enabled_lps))) {
      const size_t num_lps = hwy::TotalLogicalProcessors();
      fprintf(
          stderr,
          "Warning, unknown OS affinity, considering all %zu LPs enabled\n.",
          num_lps);
      for (size_t lp = 0; lp < num_lps; ++lp) {
        enabled_lps.Set(lp);
      }
    }

    // Without threading support, only keep the first enabled LP; it might still
    // make sense to pin the main thread.
    if (HWY_UNLIKELY(!hwy::HaveThreadingSupport())) {
      HWY_ASSERT(enabled_lps.Any());
      const size_t lp = enabled_lps.First();
      enabled_lps = LPS();
      enabled_lps.Set(lp);
    }

#if !GEMMA_DISABLE_TOPOLOGY
    if (HWY_LIKELY(!topology_.packages.empty())) {
      InitFromTopology(enabled_lps, package_slice, cluster_slice);
    }
#endif

    // Topology unknown, disabled or no packages with enabled LPs: create a
    // single package with one cluster, and one node.
    if (HWY_UNLIKELY(NumPackages() == 0)) {
      InitFromSlice(enabled_lps, lp_slice);
    }

    HWY_ASSERT(NumPackages() != 0 && NumClusters(0) != 0 && NumNodes() != 0);
  }

  size_t NumPackages() const { return packages_.size(); }
  const char* TopologyString() const { return topology_string_; }
  size_t NumNodes() const { return nodes_.Count(); }

  class Cluster {
   public:
    // Topology is unknown, rely on OS affinity and user-specified slice.
    Cluster(const LPS& enabled_lps, BoundedSlice lp_slice) {
      // Interpret `lp_slice` as a slice of the 1-bits of `enabled_lps`, so
      // we honor both the OS affinity and the user-specified slice. Note that
      // this can be used to exclude hyperthreads because Linux groups LPs by
      // sibling index. For example, the first `num_cores` are not siblings.
      const size_t detected = enabled_lps.Count();
      size_t enabled_idx = 0;