Home / Class/ parallel_reduce Class — pytorch Architecture

parallel_reduce Class — pytorch Architecture

Architecture documentation for the parallel_reduce class in Parallel-inl.h from the pytorch codebase.

Entity Profile

Source Code

aten/src/ATen/Parallel-inl.h lines 45–91

template <class scalar_t, class F, class SF>
inline scalar_t parallel_reduce(
    const int64_t begin,
    const int64_t end,
    const int64_t grain_size,
    const scalar_t ident,
    const F& f,
    const SF& sf) {
  TORCH_CHECK(grain_size >= 0);
  if (begin >= end) {
    return ident;
  }

#ifdef INTRA_OP_PARALLEL
  at::internal::lazy_init_num_threads();
  const auto max_threads = at::get_num_threads();
  const bool use_parallel =
      ((end - begin) > grain_size && !at::in_parallel_region() &&
       max_threads > 1);
  if (!use_parallel) {
    internal::ThreadIdGuard tid_guard(0);
    c10::ParallelGuard guard(true);
    return f(begin, end, ident);
  }

  c10::SmallVector<scalar_t, 64> results(max_threads, ident);
  internal::invoke_parallel(
      begin,
      end,
      grain_size,
      [&](const int64_t my_begin, const int64_t my_end) {
        const auto tid = at::get_thread_num();
        c10::ParallelGuard guard(true);
        results[tid] = f(my_begin, my_end, ident);
      });

  scalar_t result = ident;
  for (auto partial_result : results) {
    result = sf(result, partial_result);
  }
  return result;
#else
  internal::ThreadIdGuard tid_guard(0);
  c10::ParallelGuard guard(true);
  return f(begin, end, ident);
#endif
}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free