Home | History | Annotate | Download | only in kernels
      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 #define EIGEN_USE_THREADS
     17 
     18 #include "tensorflow/core/kernels/dense_update_functor.h"
     19 
     20 #include "tensorflow/core/framework/register_types.h"
     21 #include "tensorflow/core/lib/core/errors.h"
     22 #include "tensorflow/core/platform/mutex.h"
     23 #include "tensorflow/core/platform/types.h"
     24 
     25 namespace tensorflow {
     26 
     27 typedef Eigen::ThreadPoolDevice CPUDevice;
     28 typedef Eigen::GpuDevice GPUDevice;
     29 
     30 namespace functor {
     31 
     32 template <>
     33 struct DenseUpdate<CPUDevice, string, ASSIGN> {
     34   void operator()(const CPUDevice& d, typename TTypes<string>::Flat params,
     35                   typename TTypes<string>::ConstFlat update) {
     36     if (params.dimension(0) == 1) {
     37       params.data()->resize(update.data()->size());
     38       auto work = [&params, &update](int64 start, int64 end) {
     39         memmove(const_cast<char*>(params.data()->data()) + start,
     40                 update.data()->data() + start, end - start);
     41       };
     42       d.parallelFor(update.data()->size(),
     43                     Eigen::TensorOpCost(.1,  // chosen to force large chunks
     44                                         .1, 0),
     45                     work);
     46     } else {
     47       auto work = [&params, &update](int64 start, int64 end) {
     48         for (int i = start; i < end; ++i) {
     49           params.data()[i].resize(update.data()[i].size());
     50           memmove(const_cast<char*>(params.data()[i].data()),
     51                   update.data()[i].data(), update.data()[i].size());
     52         }
     53       };
     54       int64 estimated_string_size;
     55       if (update.size() > 0) {
     56         // first element of the tensor seems as good a guess as any of the sizes
     57         // of the strings contained within...
     58         estimated_string_size =
     59             std::max(update.data()[0].size(), sizeof(string));
     60       } else {
     61         estimated_string_size = sizeof(string);
     62       }
     63       d.parallelFor(
     64           params.dimension(0),
     65           Eigen::TensorOpCost(estimated_string_size, estimated_string_size, 0),
     66           work);
     67     }
     68   }
     69 };
     70 
     71 }  // namespace functor
     72 
     73 }  // namespace tensorflow
     74