cjolivier01 commented on a change in pull request #8972: Profiling 
enhancements, python API, vtune and chrome tracing objects, etc.
URL: https://github.com/apache/incubator-mxnet/pull/8972#discussion_r164227180
 
 

 ##########
 File path: src/c_api/c_api_profile.cc
 ##########
 @@ -0,0 +1,586 @@
+//
+// Created by coolivie on 11/25/17.
+//
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+/*!
+ *  Copyright (c) 2017 by Contributors
+ * \file c_api_profile.cc
+ * \brief C API of mxnet profiler and support functions
+ */
+#include <dmlc/base.h>
+#include <dmlc/logging.h>
+#include <dmlc/thread_group.h>
+#include <stack>
+#include "./c_api_common.h"
+#include "../profiler/profiler.h"
+
+namespace mxnet {
+
+// #define PROFILE_API_INCLUDE_AS_EVENT
+
+#if MXNET_USE_PROFILER
+static profiler::ProfileDomain api_domain("MXNET_C_API");
+static profiler::ProfileCounter api_call_counter("MXNet C API Calls", 
&api_domain);
+static profiler::ProfileCounter api_concurrency_counter("MXNet C API 
Concurrency",
+                                                        &api_domain);
+
+/*! \brief Per-API-call timing data */
+struct APICallTimingData {
+  const char *name_;
+  profiler::ProfileTask *task_;
+#ifdef PROFILE_API_INCLUDE_AS_EVENT
+  profiler::ProfileEvent *event_;
+#endif  // PROFILE_API_INCLUDE_AS_EVENT
+};
+
+template<typename T, typename... Args>
+inline std::unique_ptr<T> make_unique(Args&&... args) {
+  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+/*!
+ * \brief Per-thread profiling data
+ */
+class ProfilingThreadData {
+ public:
+  /*!
+   * \brief Constructor, nothrow
+   */
+  inline ProfilingThreadData() noexcept {}
+
+  /*!
+   * \brief Retreive ProfileTask object of the given name, or create if it 
doesn't exist
+   * \param name Name of the task
+   * \param domain Domain of the task
+   * \return Pointer to the stored or created ProfileTask object
+   */
+  profiler::ProfileTask *profile_task(const char *name, 
profiler::ProfileDomain *domain) {
+    // Per-thread so no lock necessary
+    auto iter = tasks_.find(name);
+    if (iter == tasks_.end()) {
+      iter = tasks_.emplace(std::make_pair(
+        name, make_unique<profiler::ProfileTask>(name, domain))).first;
+    }
+    return iter->second.get();
+  }
+
+#ifdef PROFILE_API_INCLUDE_AS_EVENT
+  /*!
+   * \brief Retreive ProfileEvent object of the given name, or create if it 
doesn't exist
+   * \param name Name of the event
+   * \return Pointer to the stored or created ProfileEvent object
+   */
+  profiler::ProfileEvent *profile_event(const char *name) {
+    // Per-thread so no lock necessary
+    auto iter = events_.find(name);
+    if (iter == events_.end()) {
+      iter = events_.emplace(std::make_pair(name, 
make_unique<profiler::ProfileEvent>(name))).first;
+    }
+    return iter->second.get();
+  }
+#endif  // PROFILE_API_INCLUDE_AS_EVENT
+
+  /*! \brief nestable call stack */
+  std::stack<APICallTimingData> calls_;
+  /*! \brief Whether profiling actions should be ignored/excluded */
+  volatile bool ignore_call_ = false;  // same-thread only, so not atomic
+
+ private:
+  /*! \brief tasks */
+  std::unordered_map<std::string, std::unique_ptr<profiler::ProfileTask>> 
tasks_;
+#ifdef PROFILE_API_INCLUDE_AS_EVENT
+  /*! \brief events */
+  std::unordered_map<std::string, std::unique_ptr<profiler::ProfileEvent>> 
events_;
+#endif  // PROFILE_API_INCLUDE_AS_EVENT
+};
+
+static thread_local ProfilingThreadData thread_profiling_data;
+#endif  // MXNET_USE_PROFILER
+
+extern void on_enter_api(const char *function) {
 
 Review comment:
   Do you realize how much junk would then need to be pulled into 
c_api_common.h?
   Right now it just has:
   ```cpp
   #include <dmlc/base.h>
   #include <dmlc/logging.h>
   #include <dmlc/thread_local.h>
   #include <mxnet/c_api.h>
   #include <mxnet/base.h>
   #include <nnvm/graph.h>
   #include <vector>
   #include <string>
   ```
   
   In the grand scheme of things with the whole python<->C transition, I don't 
feel like this little function call introduces any measurable overhead.
   
   Remember: 
   "premature optimization is the root of all evil (or at least most of it) in 
programming. "
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to