1 /* Copyright 2015 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 #ifndef TENSORFLOW_CORE_FRAMEWORK_RUN_HANDLER_UTIL_H_ 17 #define TENSORFLOW_CORE_FRAMEWORK_RUN_HANDLER_UTIL_H_ 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 23 namespace tensorflow { 24 25 // Assign thread ranges to requests. 26 // Requests are numbered 0...num_active_requests-1, and 27 // threads are numbered 0...num_threads-1. 28 // On return, the range [start_vec->at(i), end_vec->at(i)) 29 // indicates the subrange of the threads available to request i. 30 // The ranges given to different requests may overlap. 31 // Lower numbered requests will tend to be assigned more threads. 32 // Thus, a client might associate older requests with lower 33 // array indices so they receive access to more threads. 34 // However, the routine ensures that each request is given access 35 // to at least min(min_threads_per_request, num_threads) threads. 36 // Every thread will be assigned to at least one request range, 37 // assuming there is at least one request. 38 void ComputeInterOpSchedulingRanges(int num_active_requests, int num_threads, 39 int min_threads_per_request, 40 std::vector<std::uint_fast32_t>* start_vec, 41 std::vector<std::uint_fast32_t>* end_vec); 42 43 // Assign thread steal ranges to threads.Threads are numbered 0...num_threads-1. 44 // On return, the range [start_vec->at(i), end_vec->at(i)) indicates the steal 45 // range of the thread i. The ranges given to different threads may overlap. 46 void ComputeInterOpStealingRanges(int num_threads, int min_threads_per_domain, 47 std::vector<std::uint_fast32_t>* start_vec, 48 std::vector<std::uint_fast32_t>* end_vec); 49 50 // For each of the num_threads determine the index of the active_request whose 51 // work queue should be attempted first by that the thread. Return a vector of 52 // size num_threads which represents how threads should be distributed across 53 // requests. 54 std::vector<int> ChooseRequestsWithExponentialDistribution( 55 int num_active_requests, int num_threads); 56 57 // Look up environment variable named 'var_name' and return the value if it 58 // exist and can be parsed. Return 'default_value' otherwise. 59 double ParamFromEnvWithDefault(const char* var_name, double default_value); 60 61 // Look up environment variable named 'var_name' and return the value if it 62 // exist and can be parsed. The value must be in format val1,val2... Return 63 // 'default_value' otherwise. 64 std::vector<double> ParamFromEnvWithDefault(const char* var_name, 65 std::vector<double> default_value); 66 67 // Look up environment variable named 'var_name' and return the value if it 68 // exist and can be parsed. The value must be in format val1,val2... Return 69 // 'default_value' otherwise. 70 std::vector<int> ParamFromEnvWithDefault(const char* var_name, 71 std::vector<int> default_value); 72 73 // Look up environment variable named 'var_name' and return the value if it 74 // exist and can be parsed. Return 'default_value' otherwise. 75 bool ParamFromEnvBoolWithDefault(const char* var_name, bool default_value); 76 77 } // end namespace tensorflow 78 #endif // TENSORFLOW_CORE_FRAMEWORK_RUN_HANDLER_UTIL_H_ 79