Home | History | Annotate | Download | only in distributed_runtime
      1 /* Copyright 2016 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 #include "tensorflow/core/distributed_runtime/partial_run_mgr.h"
     17 
     18 namespace tensorflow {
     19 
     20 namespace {
     21 // TODO(suharshs): Move this to a common location to allow other part of the
     22 // repo to use it.
     23 template <typename T, typename... Args>
     24 std::unique_ptr<T> MakeUnique(Args&&... args) {
     25   return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
     26 }
     27 }  // namespace
     28 
     29 bool PartialRunMgr::FindOrCreate(int step_id,
     30                                  CancellationManager** cancellation_manager) {
     31   mutex_lock l(mu_);
     32   auto it = step_id_to_partial_run_.find(step_id);
     33   if (it != step_id_to_partial_run_.end()) {
     34     *cancellation_manager = it->second->cancellation_manager.get();
     35     return false;
     36   }
     37 
     38   std::unique_ptr<PartialRunState> partial_run = MakeUnique<PartialRunState>();
     39   partial_run->cancellation_manager = MakeUnique<CancellationManager>();
     40   *cancellation_manager = partial_run->cancellation_manager.get();
     41   step_id_to_partial_run_[step_id] = std::move(partial_run);
     42   return true;
     43 }
     44 
     45 void PartialRunMgr::ExecutorDone(int step_id, const Status& executor_status) {
     46   StatusCallback done;
     47   Status callback_status;
     48   {
     49     mutex_lock l(mu_);
     50     auto run_it = step_id_to_partial_run_.find(step_id);
     51     if (run_it == step_id_to_partial_run_.end()) {
     52       return;
     53     }
     54     // If we found the partial_run, we call the final callback, if it
     55     // exists.
     56     // It is guaranteed that run_it->second->final_callback is left empty
     57     // after the std::move call.
     58     done = std::move(run_it->second->final_callback);
     59     if (!executor_status.ok()) {
     60       run_it->second->final_status = executor_status;
     61     }
     62     callback_status = run_it->second->final_status;
     63     run_it->second->executor_done = true;
     64   }
     65   if (done != nullptr) {
     66     done(callback_status);
     67     mutex_lock l(mu_);
     68     step_id_to_partial_run_.erase(step_id);
     69   }
     70 }
     71 
     72 void PartialRunMgr::PartialRunDone(int step_id, StatusCallback done,
     73                                    const Status& status) {
     74   Status callback_status;
     75   {
     76     mutex_lock l(mu_);
     77     auto run_it = step_id_to_partial_run_.find(step_id);
     78     if (run_it == step_id_to_partial_run_.end()) {
     79       return;
     80     }
     81     run_it->second->final_status.Update(status);
     82     if (!run_it->second->executor_done) {
     83       // If we found the partial_run, we set the final callback to call only
     84       // when the executor is completely done.
     85       run_it->second->final_callback = std::move(done);
     86       return;
     87     }
     88     callback_status = run_it->second->final_status;
     89   }
     90   // Otherwise we call the callback immediately.
     91   done(callback_status);
     92   mutex_lock l(mu_);
     93   step_id_to_partial_run_.erase(step_id);
     94 }
     95 
     96 }  // namespace tensorflow
     97