tensorflow/core/platform/threadpool.cc
// This functionality is similar to parallelFor, except that reasoning about
// the number of shards used is significantly easier.
This functionality is similar to parallelFor, except that reasoning about
the number of shards used is significantly easier.
This functionality is similar to parallelFor except that reasoning about the number of shards used is significantly easier .
void ThreadPool::ParallelForFixedBlockSizeScheduling(
    const int64_t total, const int64_t block_size,
    const std::function<void(int64_t, int64_t)>& fn) {
  const int num_shards_used =
      NumShardsUsedByFixedBlockSizeScheduling(total, block_size);
  if (num_shards_used == 1) {
    fn(0, total);
    return;
  }
  BlockingCounter counter(num_shards_used);
  std::function<void(int64_t, int64_t)> handle_range =
      [=, &handle_range, &counter, &fn](int64_t first, int64_t last) {
        while (last - first > block_size) {
          const int64_t mid = first + ((last - first) / 2 + block_size - 1) /
                                          block_size * block_size;
          Schedule([=, &handle_range]() { handle_range(mid, last); });
          last = mid;
        }
        fn(first, last);
        counter.DecrementCount();  
      };
  if (num_shards_used <= NumThreads()) {
    handle_range(0, total);
  } else {
    Schedule([=, &handle_range]() { handle_range(0, total); });
  }
  counter.Wait();
}
void ThreadPool :: ParallelForFixedBlockSizeScheduling ( const int64_t total , const int64_t block_size , const std :: function < void ( int64_t , int64_t ) > & fn ) { const int num_shards_used = NumShardsUsedByFixedBlockSizeScheduling ( total , block_size ) ; if ( num_shards_used == 1 ) { fn ( 0 , total ) ; return ; } BlockingCounter counter ( num_shards_used ) ; std :: function < void ( int64_t , int64_t ) > handle_range = [ = , & handle_range , & counter , & fn ] ( int64_t first , int64_t last ) { while ( last - first > block_size ) { const int64_t mid = first + ( ( last - first ) / 2 + block_size - 1 ) / block_size * block_size ; Schedule ( [ = , & handle_range ] ( ) { handle_range ( mid , last ) ; } ) ; last = mid ; } fn ( first , last ) ; counter . DecrementCount ( ) ; } ; if ( num_shards_used <= NumThreads ( ) ) { handle_range ( 0 , total ) ; } else { Schedule ( [ = , & handle_range ] ( ) { handle_range ( 0 , total ) ; } ) ; } counter . Wait ( ) ; }
This functionality is similar to parallelFor, except that reasoning about
the number of shards used is significantly easier.
This functionality is similar to parallelFor except that reasoning about the number of shards used is significantly easier .
// Adapted from Eigen's parallelFor implementation. // Find something near the midpoint which is a multiple of block size. // Single block or less, execute directly. // The shard is done. // Avoid a thread hop by running the root of the tree and one block on the // main thread. // Execute the root in the thread pool to avoid running work on more than // numThreads() threads.