diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2018-03-09 10:39:50 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-03-09 10:44:15 -0800 |
commit | 0ebfee36ed65f3540c216f10b8ec326b7b52db3a (patch) | |
tree | 097a26bbeec3370fefc6f0964f1db98f6d92a366 /tensorflow/contrib/lite/kernels/eigen_support.cc | |
parent | 87dab2d8289750c9d34f26d7d5fb18475dff985b (diff) |
Make SetNumThreads apply to the eigen threads. (This creates a dependency on eigen!)
PiperOrigin-RevId: 188504172
Diffstat (limited to 'tensorflow/contrib/lite/kernels/eigen_support.cc')
-rw-r--r-- | tensorflow/contrib/lite/kernels/eigen_support.cc | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tensorflow/contrib/lite/kernels/eigen_support.cc b/tensorflow/contrib/lite/kernels/eigen_support.cc new file mode 100644 index 0000000000..1435a45672 --- /dev/null +++ b/tensorflow/contrib/lite/kernels/eigen_support.cc @@ -0,0 +1,52 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#include "tensorflow/contrib/lite/kernels/eigen_support.h" + +#include "third_party/eigen3/Eigen/Core" +#include "tensorflow/contrib/lite/kernels/op_macros.h" + +namespace tflite { +namespace eigen_support { + +struct RefCountedEigenContext { + int num_references = 0; +}; + +void IncrementUsageCounter(TfLiteContext* context) { + auto* ptr = reinterpret_cast<RefCountedEigenContext*>(context->eigen_context); + if (ptr == nullptr) { + Eigen::setNbThreads(context->recommended_num_threads); + + ptr = new RefCountedEigenContext; + ptr->num_references = 0; + context->eigen_context = ptr; + } + ptr->num_references++; +} + +void DecrementUsageCounter(TfLiteContext* context) { + auto* ptr = reinterpret_cast<RefCountedEigenContext*>(context->eigen_context); + if (ptr == nullptr) { + TF_LITE_FATAL( + "Call to DecrementUsageCounter() not preceded by " + "IncrementUsageCounter()"); + } + if (--ptr->num_references == 0) { + delete ptr; + } +} + +} // namespace eigen_support +} // namespace tflite |