Home | History | Annotate | Download | only in framework
      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 #include "tensorflow/core/framework/op_segment.h"
     17 
     18 #include "tensorflow/core/framework/op_kernel.h"
     19 #include "tensorflow/core/lib/core/errors.h"
     20 #include "tensorflow/core/lib/gtl/map_util.h"
     21 #include "tensorflow/core/platform/logging.h"
     22 #include "tensorflow/core/platform/mutex.h"
     23 #include "tensorflow/core/platform/types.h"
     24 
     25 namespace tensorflow {
     26 
     27 OpSegment::Item::~Item() {
     28   for (auto kv : name_kernel) delete kv.second;
     29 }
     30 
     31 OpSegment::OpSegment() {}
     32 
     33 OpSegment::~OpSegment() {
     34   for (auto kv : sessions_) delete kv.second;
     35 }
     36 
     37 Status OpSegment::FindOrCreate(const string& session_handle,
     38                                const string& node_name, OpKernel** kernel,
     39                                CreateKernelFn create_fn) {
     40   {
     41     mutex_lock l(mu_);
     42     auto item = gtl::FindPtrOrNull(sessions_, session_handle);
     43     if (item == nullptr) {
     44       return errors::NotFound("Session ", session_handle, " is not found.");
     45     }
     46     *kernel = gtl::FindPtrOrNull(item->name_kernel, node_name);
     47     if (*kernel != nullptr) {
     48       return Status::OK();
     49     }
     50   }
     51   Status s = create_fn(kernel);
     52   if (!s.ok()) {
     53     LOG(ERROR) << "Create kernel failed: " << s;
     54     return s;
     55   }
     56   {
     57     mutex_lock l(mu_);
     58     auto item = gtl::FindPtrOrNull(sessions_, session_handle);
     59     if (item == nullptr) {
     60       return errors::NotFound("Session ", session_handle, " is not found.");
     61     }
     62     OpKernel** p_kernel = &(item->name_kernel[node_name]);
     63     if (*p_kernel == nullptr) {
     64       *p_kernel = *kernel;  // Inserts 'kernel' in the map.
     65     } else {
     66       delete *kernel;
     67       *kernel = *p_kernel;
     68     }
     69   }
     70   return Status::OK();
     71 }
     72 
     73 void OpSegment::AddHold(const string& session_handle) {
     74   mutex_lock l(mu_);
     75   Item** item = &sessions_[session_handle];
     76   if (*item == nullptr) {
     77     *item = new Item;  // num_holds == 1
     78   } else {
     79     ++((*item)->num_holds);
     80   }
     81 }
     82 
     83 void OpSegment::RemoveHold(const string& session_handle) {
     84   Item* item = nullptr;
     85   {
     86     mutex_lock l(mu_);
     87     auto siter = sessions_.find(session_handle);
     88     if (siter == sessions_.end()) {
     89       VLOG(1) << "Session " << session_handle << " is not found.";
     90       return;
     91     }
     92     item = siter->second;
     93     if (--(item->num_holds) > 0) {
     94       return;
     95     } else {
     96       sessions_.erase(siter);
     97     }
     98   }
     99   delete item;
    100 }
    101 
    102 }  // end namespace tensorflow
    103