Remove unused env variable utility (#7140)
author오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Wed, 4 Sep 2019 11:05:11 +0000 (20:05 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 4 Sep 2019 11:05:11 +0000 (20:05 +0900)
Unify environment variable utility
Remove unused environment variable utility functions

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/contrib/xtrace/src/benchmark_runner.cc
runtimes/libs/misc/CMakeLists.txt
runtimes/libs/misc/include/misc/EnvVar.h
runtimes/libs/misc/include/misc/environment.h [deleted file]
runtimes/libs/misc/src/environment.cpp [deleted file]
runtimes/libs/tflite/src/Diff.cpp
tests/tools/tflite_benchmark/src/tflite_benchmark.cc
tests/tools/tflite_run/src/tflite_run.cc

index 1567599..87ef156 100644 (file)
@@ -23,7 +23,6 @@
 #include <tflite/Assert.h>
 #include <tflite/NNAPISession.h>
 
-#include <misc/environment.h>
 #include <misc/benchmark.h>
 
 #include <cpp14/memory.h>
index bfdb303..537c544 100644 (file)
@@ -1,6 +1,5 @@
 # Library `nnfw_lib_misc`
-set(NNFW_UTILITY_SRCS src/environment.cpp)
-list(APPEND NNFW_UTILITY_SRCS src/tensor/Shape.cpp)
+set(NNFW_UTILITY_SRCS src/tensor/Shape.cpp)
 list(APPEND NNFW_UTILITY_SRCS src/tensor/NonIncreasingStride.cpp)
 list(APPEND NNFW_UTILITY_SRCS src/tensor/IndexFormatter.cpp)
 list(APPEND NNFW_UTILITY_SRCS src/tensor/Comparator.cpp)
index 47206d4..db28a3c 100644 (file)
@@ -97,6 +97,19 @@ public:
     return std::stoi(_value);
   }
 
+  /**
+   * @brief Get environment variable of float type
+   * @param[in] def   Default value of environment variable
+   * @return Defaut value passed as a parameter when there is no environment variable,
+   *         otherwise the value of environment variable passed into constructor
+   */
+  float asFloat(float def) const
+  {
+    if (_value.empty())
+      return def;
+    return std::stof(_value);
+  }
+
 private:
   std::string _value;
 };
diff --git a/runtimes/libs/misc/include/misc/environment.h b/runtimes/libs/misc/include/misc/environment.h
deleted file mode 100644 (file)
index 8e6bd00..0000000
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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.
- */
-
-/**
- * @file environment.h
- * @ingroup COM_AI_RUNTIME
- * @brief This file contains utility functions and classes to access environment variables
- */
-
-#ifndef __UTIL_ENVIRONMENT_H__
-#define __UTIL_ENVIRONMENT_H__
-
-namespace nnfw
-{
-namespace misc
-{
-
-/**
- * @brief Get the environment variable of int type
- * @param[in] name            Name of the environment variable
- * @param[in] defaultValue    Default value when the value of environment variable does not exist
- * @return  The int value of the environment variable
- */
-int get_env_int(const char *name, int defaultValue = 0);
-
-/**
- * @brief Get the environment variable of bool type
- * @param[in] name            Name of the environment variable
- * @param[in] defaultValue    Default value when the value of environment variable does not exist
- * @return  @c 0 if the value of the environment variable is @c "0", @c 1 in case of other number
- */
-bool get_env_bool(const char *name, bool defaultValue = false);
-}
-}
-
-#include <string>
-
-namespace nnfw
-{
-namespace misc
-{
-namespace env
-{
-/**
- * @brief Parent struct of @ref IntAccessor and @ref FloatAccessor
- * @tparam T Type of the value of environment variable
- */
-template <typename T> struct Accessor
-{
-  /**
-   * @brief Destroy the Accessor object
-   */
-  virtual ~Accessor() = default;
-  /**
-   * @brief Read the value of environment variable
-   * @param[out] out    The value of environment variable
-   * @return     @c true if accessing environment variable is successful,
-   *             @c false if there is exist no such environment variable
-   */
-  virtual bool access(T &out) const = 0;
-};
-
-/**
- * @brief Class to read int environment variable
- */
-class IntAccessor : public Accessor<int>
-{
-public:
-  /**
-   * @brief Construct a new IntAccessor object
-   * @param[in] tag     Name of environment variable
-   */
-  IntAccessor(const std::string &tag);
-
-public:
-  /**
-   * @brief Read the value of environment variable
-   * @param[out] out    The value of environment variable
-   * @return     @c true if accessing environment variable is successful,
-   *             @c false if there is exist no such environment variable
-   */
-  bool access(int &out) const override;
-
-private:
-  std::string _tag;
-};
-
-/**
- * @brief Class to read float environment variable
- */
-class FloatAccessor : public Accessor<float>
-{
-public:
-  /**
-   * @brief Construct a new FloatAccessor object
-   * @param[in] tag     Name of environment variable
-   */
-  FloatAccessor(const std::string &tag);
-
-public:
-  /**
-   * @brief Read the value of environment variable
-   * @param[out] out    The value of environment variable
-   * @return     @c true if accessing environment variable is successful,
-   *             @c false if there is exist no such environment variable
-   */
-  bool access(float &out) const override;
-
-private:
-  std::string _tag;
-};
-
-} // namespace env
-} // namespace misc
-} // namespace nnfw
-
-#endif // __UTIL_ENVIRONMENT_H__
diff --git a/runtimes/libs/misc/src/environment.cpp b/runtimes/libs/misc/src/environment.cpp
deleted file mode 100644 (file)
index e39f18d..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 <string.h>
-#include <cstdlib>
-#include <string>
-
-#include "misc/environment.h"
-
-namespace nnfw
-{
-namespace misc
-{
-
-int get_env_int(const char *name, int defaultValue)
-{
-  const char *value = std::getenv(name);
-  if (value != nullptr)
-    return std::stoi(value);
-  return defaultValue;
-}
-
-bool get_env_bool(const char *name, bool defaultValue)
-{
-  const char *value = std::getenv(name);
-  if (value != nullptr)
-  {
-    return std::stoi(value) != 0;
-  }
-
-  return defaultValue;
-}
-
-} // namespace misc
-} // namespace nnfw
-
-namespace nnfw
-{
-namespace misc
-{
-namespace env
-{
-
-IntAccessor::IntAccessor(const std::string &tag) : _tag{tag}
-{
-  // DO NOTHING
-}
-
-bool IntAccessor::access(int &out) const
-{
-  auto value = std::getenv(_tag.c_str());
-
-  if (value == nullptr)
-  {
-    return false;
-  }
-
-  out = std::stoi(value);
-  return true;
-}
-
-FloatAccessor::FloatAccessor(const std::string &tag) : _tag{tag}
-{
-  // DO NOTHING
-}
-
-bool FloatAccessor::access(float &out) const
-{
-  auto value = std::getenv(_tag.c_str());
-
-  if (value == nullptr)
-  {
-    return false;
-  }
-
-  out = std::stof(value);
-  return true;
-}
-
-} // namespace env
-} // namespace misc
-} // namespace nnfw
index 45ef061..414aef2 100644 (file)
@@ -24,7 +24,7 @@
 #include "misc/tensor/Zipper.h"
 #include "misc/tensor/Comparator.h"
 
-#include "misc/environment.h"
+#include "misc/EnvVar.h"
 
 #include <iostream>
 #include <cassert>
@@ -588,11 +588,9 @@ RandomTestRunner RandomTestRunner::make(int seed)
 {
   RandomTestParam param;
 
-  param.verbose = 0;
-  param.tolerance = 1;
-
-  nnfw::misc::env::IntAccessor("VERBOSE").access(param.verbose);
-  nnfw::misc::env::IntAccessor("TOLERANCE").access(param.tolerance);
+  param.verbose = nnfw::misc::EnvVar("VERBOSE").asInt(0);
+  param.tolerance = nnfw::misc::EnvVar("TOLERANCE").asInt(1);
+  ;
 
   return RandomTestRunner{seed, param};
 }
index 54c97c4..634b149 100644 (file)
@@ -34,7 +34,7 @@
 #include <iostream>
 #include <thread>
 
-#include "misc/environment.h"
+#include "misc/EnvVar.h"
 #include "misc/benchmark.h"
 
 using namespace tflite;
@@ -70,9 +70,9 @@ int main(const int argc, char **argv)
 
   const auto filename = argv[1];
 
-  const bool use_nnapi = nnfw::misc::get_env_bool("USE_NNAPI");
-  const auto thread_count = nnfw::misc::get_env_int("THREAD", -1);
-  const auto pause = nnfw::misc::get_env_int("PAUSE", 0);
+  const bool use_nnapi = nnfw::misc::EnvVar("USE_NNAPI").asBool(false);
+  const auto thread_count = nnfw::misc::EnvVar("THREAD").asInt(-1);
+  const auto pause = nnfw::misc::EnvVar("PAUSE").asInt(0);
 
   std::cout << "Num threads: " << thread_count << std::endl;
   if (use_nnapi)
@@ -215,7 +215,7 @@ int main(const int argc, char **argv)
   //
   // Measure
   //
-  const auto cnt = nnfw::misc::get_env_int("COUNT", 1);
+  const auto cnt = nnfw::misc::EnvVar("COUNT").asInt(1);
 
   using namespace boost::accumulators;
 
index c067f8b..deed128 100644 (file)
@@ -21,7 +21,7 @@
 #include "tensor_dumper.h"
 #include "tensor_loader.h"
 #include "misc/benchmark.h"
-#include "misc/environment.h"
+#include "misc/EnvVar.h"
 #include "misc/fp32.h"
 #include "tflite/Diff.h"
 #include "tflite/Assert.h"
@@ -73,7 +73,7 @@ int main(const int argc, char **argv)
 
       TFLITE_ENSURE(builder(&interpreter))
 
-      interpreter->SetNumThreads(nnfw::misc::get_env_int("THREAD", -1));
+      interpreter->SetNumThreads(nnfw::misc::EnvVar("THREAD").asInt(-1));
     };
   }
   catch (const std::exception &e)
@@ -266,8 +266,7 @@ int main(const int argc, char **argv)
 
     // TODO Code duplication (copied from RandomTestRunner)
 
-    int tolerance = 1;
-    nnfw::misc::env::IntAccessor("TOLERANCE").access(tolerance);
+    int tolerance = nnfw::misc::EnvVar("TOLERANCE").asInt(1);
 
     auto equals = [tolerance](float lhs, float rhs) {
       // NOTE Hybrid approach