1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 // Algorithms and data structures for partition assignment. 17 18 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_PARTITION_ASSIGNMENT_H_ 19 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_PARTITION_ASSIGNMENT_H_ 20 21 #include <iosfwd> 22 #include <map> 23 #include <memory> 24 25 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 26 #include "tensorflow/compiler/xla/service/hlo_module.h" 27 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 28 #include "tensorflow/core/platform/types.h" 29 30 namespace xla { 31 namespace gpu { 32 33 // Encapsulates the launch dimensions of a kernel, e.g., the block count and the 34 // number of threads per block. 35 class LaunchDimensions { 36 public: 37 // The default constructor creates a launch dimension that indicate 38 // single-threaded execution. LaunchDimensions()39 LaunchDimensions() : block_count_(1), threads_per_block_(1) {} 40 LaunchDimensions(int64 block_count,int64 threads_per_block)41 LaunchDimensions(int64 block_count, int64 threads_per_block) 42 : block_count_(block_count), threads_per_block_(threads_per_block) {} 43 IsSinglethreaded()44 bool IsSinglethreaded() const { 45 return block_count_ == 1 && threads_per_block_ == 1; 46 } 47 block_count()48 int64 block_count() const { return block_count_; } threads_per_block()49 int64 threads_per_block() const { return threads_per_block_; } launch_bound()50 int64 launch_bound() const { return block_count() * threads_per_block(); } 51 52 private: 53 int64 block_count_; 54 int64 threads_per_block_; 55 }; 56 57 std::ostream& operator<<(std::ostream& out, 58 const LaunchDimensions& launch_dims); 59 60 // Returns the maximum number of threads per block allowed by the device. 61 int64 ThreadsPerBlockLimit(const se::DeviceDescription& device_desc); 62 63 LaunchDimensions CalculateLaunchDimensions( 64 const Shape& shape, const se::DeviceDescription& device_desc, 65 int unroll_factor = 1); 66 67 } // namespace gpu 68 } // namespace xla 69 70 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_PARTITION_ASSIGNMENT_H_ 71