You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by jx...@apache.org on 2018/02/23 18:35:14 UTC
[incubator-mxnet] branch master updated: Profiling enhancements,
python API, vtune and chrome tracing objects, etc. (#8972)
This is an automated email from the ASF dual-hosted git repository.
jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push:
new 106f97f Profiling enhancements, python API, vtune and chrome tracing objects, etc. (#8972)
106f97f is described below
commit 106f97f1881e6bb1a00c56a0ae55200e27297733
Author: Chris Olivier <cj...@gmail.com>
AuthorDate: Fri Feb 23 10:35:05 2018 -0800
Profiling enhancements, python API, vtune and chrome tracing objects, etc. (#8972)
* Profiling enhancements
* Set new dmlc-core commit
* Address CR comments
* Update profiler.py
* lint
---
.gitignore | 5 +-
.gitmodules | 6 +-
R-package/R/profiler.R | 19 +-
R-package/src/mxnet.cc | 19 +-
amalgamation/amalgamation.py | 2 +-
amalgamation/mxnet_predict0.cc | 3 +-
dmlc-core | 2 +-
include/mxnet/c_api.h | 143 ++-
perl-package/AI-MXNet/lib/AI/MXNet/Profiler.pm | 29 +-
perl-package/AI-MXNet/lib/AI/MXNet/Types.pm | 1 -
perl-package/AI-MXNetCAPI/mxnet.i | 12 +-
perl-package/AI-MXNetCAPI/mxnet_typemaps.i | 2 +-
python/mxnet/base.py | 1 +
python/mxnet/profiler.py | 373 +++++-
.../src/main/scala/ml/dmlc/mxnet/LibInfo.scala | 4 +-
.../src/main/scala/ml/dmlc/mxnet/Profiler.scala | 12 +-
.../mxnetexamples/profiler/ProfilerMatMul.scala | 6 +-
.../mxnetexamples/profiler/ProfilerNDArray.scala | 8 +-
.../src/main/native/ml_dmlc_mxnet_native_c_api.cc | 52 +-
src/c_api/c_api.cc | 36 -
src/c_api/c_api_common.h | 10 +-
src/c_api/c_api_profile.cc | 595 ++++++++++
src/engine/naive_engine.cc | 44 +-
src/engine/profiler.cc | 227 ----
src/engine/profiler.h | 174 ---
src/engine/thread_pool.h | 56 +-
src/engine/threaded_engine.cc | 10 +-
src/engine/threaded_engine.h | 30 +-
src/engine/threaded_engine_perdevice.cc | 39 +-
src/executor/graph_executor.cc | 6 +-
src/initialize.cc | 12 -
src/ndarray/ndarray_function.cc | 19 +-
src/operator/nn/batch_norm-inl.h | 1 +
src/operator/nn/batch_norm.cu | 4 +
src/operator/tensor/init_op.h | 2 +-
src/profiler/aggregate_stats.cc | 129 ++
src/profiler/aggregate_stats.h | 74 ++
src/profiler/profiler.cc | 314 +++++
src/profiler/profiler.h | 1238 ++++++++++++++++++++
src/profiler/storage_profiler.h | 111 ++
src/profiler/vtune.cc | 31 +
src/profiler/vtune.h | 323 +++++
src/storage/pooled_storage_manager.h | 1 +
src/storage/storage.cc | 18 +-
tests/cpp/include/test_core_op.h | 5 +-
tests/cpp/include/test_legacy_op.h | 5 +-
tests/cpp/include/test_perf.h | 54 +-
tests/cpp/include/test_util.h | 29 -
tests/cpp/misc/memory_test.cc | 2 +-
tests/python/unittest/test_profiler.py | 187 ++-
tools/profile/tune_mnist.sh | 23 +
tools/profile/tune_python.sh | 24 +
52 files changed, 3824 insertions(+), 708 deletions(-)
diff --git a/.gitignore b/.gitignore
index 9d2e894..b3fc89d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -149,11 +149,12 @@ bld
./tmp/*
*.jar
target
-
bin/im2rec
-
model/
+# VTune
+./r0*hs
+
# generated function signature for IDE auto-complete
python/mxnet/symbol/gen_*
python/mxnet/ndarray/gen_*
diff --git a/.gitmodules b/.gitmodules
index 42f0027..cdb8a55 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -13,9 +13,6 @@
[submodule "dlpack"]
path = dlpack
url = https://github.com/dmlc/dlpack
-[submodule "cub"]
- path = 3rdparty/cub
- url = https://github.com/dmlc/cub
[submodule "3rdparty/openmp"]
path = 3rdparty/openmp
url = https://github.com/llvm-mirror/openmp
@@ -26,3 +23,6 @@
path = 3rdparty/mkldnn
url = https://github.com/intel/mkl-dnn.git
branch = master
+[submodule "3rdparty/cub"]
+ path = 3rdparty/cub
+ url = https://github.com/dmlc/cub
diff --git a/R-package/R/profiler.R b/R-package/R/profiler.R
index 3666c35..084e137 100644
--- a/R-package/R/profiler.R
+++ b/R-package/R/profiler.R
@@ -1,19 +1,22 @@
# profiler setting methods
-#
+#
#' @export
-MX.PROF.MODE <- list(SYMBOLIC = 0L, ALL = 1L)
-#' @export
MX.PROF.STATE <- list(STOP = 0L, RUN = 1L)
#' Set up the configuration of profiler.
#'
-#' @param mode Indicting whether to enable the profiler, can be 'MX.PROF.MODE$SYMBOLIC' or 'MX.PROF.MODE$ALL'. Default is `MX.PROF.MODE$SYMBOLIC`.
-#' @param filename The name of output trace file. Default is 'profile.json'
-#'
+#' @param flags list of key/value pair tuples. Indicates configuration parameters
+#' profile_symbolic : boolean, whether to profile symbolic operators
+#' profile_imperative : boolean, whether to profile imperative operators
+#' profile_memory : boolean, whether to profile memory usage
+#' profile_api : boolean, whether to profile the C API
+#' file_name : string, output file for profile data
+#' continuous_dump : boolean, whether to periodically dump profiling data to file
+#' dump_period : float, seconds between profile data dumps
#' @export
-mx.profiler.config <- function(mode = MX.PROF.MODE$SYMBOLIC, filename='profile.json') {
- mx.internal.profiler.config(mode, filename)
+mx.profiler.config <- function(params) {
+ mx.internal.profiler.config(params)
}
#' Set up the profiler state to record operator.
diff --git a/R-package/src/mxnet.cc b/R-package/src/mxnet.cc
index 9f8239b..4249b26 100644
--- a/R-package/src/mxnet.cc
+++ b/R-package/src/mxnet.cc
@@ -3,7 +3,6 @@
* \file mxnet.cc
* \brief The registry of all module functions and objects
*/
-#include <Rcpp.h>
#include <fstream>
#include "./base.h"
#include "./ndarray.h"
@@ -24,8 +23,22 @@ void NotifyShutdown() {
MX_CALL(MXNotifyShutdown());
}
-void ProfilerSetConfig(int mode, const std::string &filename) {
- MX_CALL(MXSetProfilerConfig(mode, filename.c_str()));
+void ProfilerSetConfig(SEXP params) {
+ Rcpp::List kwargs(params);
+ std::vector<std::string> keys = SafeGetListNames(kwargs);
+ std::vector<std::string> str_keys(keys.size());
+ std::vector<std::string> str_vals(keys.size());
+ for (size_t i = 0; i < kwargs.size(); ++i) {
+ RCHECK(keys[i].length() != 0)
+ << "Profiler::SetConfig only accepts key=value style arguments";
+ str_keys[i] = FormatParamKey(keys[i]);
+ str_vals[i] = toPyString(keys[i], kwargs[i]);
+ }
+ std::vector<const char*> c_str_keys = CKeys(str_keys);
+ std::vector<const char*> c_str_vals = CKeys(str_vals);
+
+ MX_CALL(MXSetProfilerConfig(static_cast<mx_uint>(str_keys.size()),
+ dmlc::BeginPtr(c_str_keys), dmlc::BeginPtr(c_str_vals)));
}
void ProfilerSetState(int state) {
diff --git a/amalgamation/amalgamation.py b/amalgamation/amalgamation.py
index 1a82413..4574224 100644
--- a/amalgamation/amalgamation.py
+++ b/amalgamation/amalgamation.py
@@ -31,7 +31,7 @@ blacklist = [
'nvml.h', 'opencv2/opencv.hpp', 'sys/stat.h', 'sys/types.h', 'cuda.h', 'cuda_fp16.h',
'omp.h', 'execinfo.h', 'packet/sse-inl.h', 'emmintrin.h', 'thrust/device_vector.h',
'cusolverDn.h', 'internal/concurrentqueue_internal_debug.h', 'relacy/relacy_std.hpp',
- 'relacy_shims.h'
+ 'relacy_shims.h', 'ittnotify.h', 'shared_mutex'
]
minimum = int(sys.argv[6]) if len(sys.argv) > 5 else 0
diff --git a/amalgamation/mxnet_predict0.cc b/amalgamation/mxnet_predict0.cc
index cfee605..c4653d3 100644
--- a/amalgamation/mxnet_predict0.cc
+++ b/amalgamation/mxnet_predict0.cc
@@ -52,7 +52,8 @@
#include "src/engine/engine.cc"
#include "src/engine/naive_engine.cc"
-#include "src/engine/profiler.cc"
+
+#include "src/profiler/profiler.cc"
#include "src/executor/graph_executor.cc"
#include "src/executor/attach_op_execs_pass.cc"
diff --git a/dmlc-core b/dmlc-core
index 2bdcc6d..a1fd683 160000
--- a/dmlc-core
+++ b/dmlc-core
@@ -1 +1 @@
-Subproject commit 2bdcc6dafcd51041fac5c76f04c3baa5a250e679
+Subproject commit a1fd6834c0cd3fd2cc586deec2dc24194924cada
diff --git a/include/mxnet/c_api.h b/include/mxnet/c_api.h
index d34b194..b1fdbf1 100644
--- a/include/mxnet/c_api.h
+++ b/include/mxnet/c_api.h
@@ -89,6 +89,8 @@ typedef void *RtcHandle;
typedef void *CudaModuleHandle;
/*! \brief handle to rtc cuda kernel*/
typedef void *CudaKernelHandle;
+/*! \brief handle to a Profile object (domain, duration, counter, etc.) */
+typedef void *ProfileHandle;
typedef void (*ExecutorMonitorCallback)(const char*,
NDArrayHandle,
@@ -201,6 +203,7 @@ MXNET_DLL const char *MXGetLastError();
* \return 0 when success, -1 when failure happens.
*/
MXNET_DLL int MXRandomSeed(int seed);
+
/*!
* \brief Notify the engine about a shutdown,
* This can help engine to print less messages into display.
@@ -209,15 +212,16 @@ MXNET_DLL int MXRandomSeed(int seed);
* \return 0 when success, -1 when failure happens.
*/
MXNET_DLL int MXNotifyShutdown();
+
/*!
* \brief Set up configuration of profiler
- * \param mode indicate the working mode of profiler,
- * record anly symbolic operator when mode == 0,
- * record all operator when mode == 1
- * \param filename where to save trace file
+ * \param num_params Number of parameters
+ * \param keys array of parameter keys
+ * \param vals array of parameter values
* \return 0 when success, -1 when failure happens.
*/
-MXNET_DLL int MXSetProfilerConfig(int mode, const char* filename);
+MXNET_DLL int MXSetProfilerConfig(int num_params, const char* const* keys, const char* const* vals);
+
/*!
* \brief Set up state of profiler
* \param state indicate the working state of profiler,
@@ -227,10 +231,133 @@ MXNET_DLL int MXSetProfilerConfig(int mode, const char* filename);
*/
MXNET_DLL int MXSetProfilerState(int state);
-/*! \brief Save profile and stop profiler */
-MXNET_DLL int MXDumpProfile();
+/*!
+ * \brief Save profile and stop profiler
+ * \param finished true if stat output should stop after this point
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXDumpProfile(int finished);
+
+
+/*!
+ * \brief Print aggregate stats to the a string
+ * \param out_str Will receive a pointer to the output string
+ * \param reset Clear the aggregate stats after printing
+ * \return 0 when success, -1 when failure happens.
+ * \note
+ */
+MXNET_DLL int MXAggregateProfileStatsPrint(const char **out_str, int reset);
+
+/*!
+ * \brief Pause profiler tuning collection
+ * \param paused If nonzero, profiling pauses. Otherwise, profiling resumes/continues
+ * \return 0 when success, -1 when failure happens.
+ * \note pausing and resuming is global and not recursive
+ */
+MXNET_DLL int MXProfilePause(int paused);
+
+/*!
+ * \brief Create profiling domain
+ * \param domain String representing the domain name to create
+ * \param out Return domain object
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileCreateDomain(const char *domain, ProfileHandle *out);
+
+/*!
+ * \brief Create profile task
+ * \param name Name of the task
+ * \param domain Domain of the task
+ * \param out Output handle
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileCreateTask(ProfileHandle domain,
+ const char *task_name,
+ ProfileHandle *out);
+
+/*!
+ * \brief Create profile frame
+ * \param name Name of the frame
+ * \param domain Domain of the frame
+ * \param out Output handle
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileCreateFrame(ProfileHandle domain,
+ const char *frame_name,
+ ProfileHandle *out);
+
+/*!
+ * \brief Create profile event
+ * \param name Name of the event
+ * \param out Output handle
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileCreateEvent(const char *event_name, ProfileHandle *out);
+
+/*!
+ * \brief Create profile counter
+ * \param name Name of the counter
+ * \param domain Domain of the counter
+ * \param out Output handle
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileCreateCounter(ProfileHandle domain,
+ const char *counter_name,
+ ProfileHandle *out);
+
+/*!
+ * \brief Destroy a frame
+ * \param frame_handle Handle to frame to destroy
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileDestroyHandle(ProfileHandle frame_handle);
+
+/*!
+ * \brief Start timing the duration of a profile duration object such as an event, task or frame
+ * \param duration_handle handle to the duration object
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileDurationStart(ProfileHandle duration_handle);
+
+/*!
+ * \brief Stop timing the duration of a profile duration object such as an event, task or frame
+ * \param duration_handle handle to the duration object
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileDurationStop(ProfileHandle duration_handle);
-/*! \brief Set the number of OMP threads to use */
+/*!
+ * \brief Set a counter, given its handle
+ * \param counter_handle Handle to counter to set
+ * \param value Value to set the counter to (64-bit unsigned integer)
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileSetCounter(ProfileHandle counter_handle, uint64_t value);
+
+/*!
+ * \brief Adjust a counter by the given amount, given its handle
+ * \param counter_handle Handle to counter to adjust
+ * \param value Value to adjust the counter by (64-bit signed integer)
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileAdjustCounter(ProfileHandle counter_handle, int64_t value);
+
+/*!
+ * \brief Mark a single instant in time
+ * \param domain Domain of the marker
+ * \param instant_marker_name Name of the marker
+ * \param scope Scope of marker ('global', 'process', 'thread', 'task', 'marker')
+ * \return 0 when success, -1 when failure happens.
+ */
+MXNET_DLL int MXProfileSetMarker(ProfileHandle domain,
+ const char *instant_marker_name,
+ const char *scope);
+
+/*!
+ * \brief Set the number of OMP threads to use
+ * \param thread_num Number of OMP threads desired
+ * \return 0 when success, -1 when failure happens.
+ */
MXNET_DLL int MXSetNumOMPThreads(int thread_num);
/*!
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Profiler.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Profiler.pm
index 47d7a0d..4a831b1 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet/Profiler.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/Profiler.pm
@@ -37,18 +37,20 @@ use AI::MXNet::Function::Parameters;
Parameters
----------
- mode : string, optional
- Indicting whether to enable the profiler, can
- be 'symbolic' or 'all'. Default is `symbolic`.
- filename : string, optional
- The name of output trace file. Default is
- 'profile.json'.
+ kwargs : hash ref
+ Indicates configuration parameters with key/value pairs, listed below
+ profile_symbolic : boolean, whether to profile symbolic operators
+ profile_imperative : boolean, whether to profile imperative operators
+ profile_memory : boolean, whether to profile memory usage
+ profile_api : boolean, whether to profile the C API
+ file_name : string, output file for profile data
+ continuous_dump : boolean, whether to periodically dump profiling data to file
+ dump_period : float, seconds between profile data dumps
=cut
-method profiler_set_config(ProfilerMode $mode='symbolic', Str $filename='profile.json')
+method profiler_set_config(HashRef[Str] $kwargs)
{
- my %mode2int = qw/symbolic 0 all 1/;
- check_call(AI::MXNet::SetProfilerConfig($mode2int{ $mode }, $filename));
+ check_call(AI::MXNet::SetProfilerConfig(scalar(keys %{ $kwargs }), $kwargs));
}
=head2 profiler_set_state
@@ -57,15 +59,14 @@ method profiler_set_config(ProfilerMode $mode='symbolic', Str $filename='profile
Parameters
----------
- state : string, optional
+ state : int, optional
Indicting whether to run the profiler, can
- be 'stop' or 'run'. Default is `stop`.
+ be 'stop' - 0 or 'run' - 1. Default is `stop`.
=cut
-method profiler_set_state(ProfilerState $state='stop')
+method profiler_set_state(Int $state)
{
- my %state2int = qw/stop 0 run 1/;
- check_call(AI::MXNet::SetProfilerState($state2int{ $state }));
+ check_call(AI::MXNet::SetProfilerState($state));
}
=head2 dump_profile
diff --git a/perl-package/AI-MXNet/lib/AI/MXNet/Types.pm b/perl-package/AI-MXNet/lib/AI/MXNet/Types.pm
index ea17f19..d48ccdd 100644
--- a/perl-package/AI-MXNet/lib/AI/MXNet/Types.pm
+++ b/perl-package/AI-MXNet/lib/AI/MXNet/Types.pm
@@ -53,7 +53,6 @@ subtype "CudaKernelShape" => as "Shape" => where { @$_ == 3 };
subtype "WholeDim" => as "Str" => where { $_ eq 'X' };
subtype "Slice" => as "ArrayRef[Index]|WholeDim|Index" => where { ref $_ ? @$_ > 0 : 1 };
subtype "Dtype" => as enum([qw[float32 float64 float16 uint8 int32]]);
-subtype "ProfilerMode" => as enum([qw[symbolic all]]);
subtype "GluonClass" => as enum([qw[AI::MXNet::NDArray AI::MXNet::Symbol]]);
subtype "GluonInput" => as "AI::MXNet::NDArray|AI::MXNet::Symbol|ArrayRef[AI::MXNet::NDArray|AI::MXNet::Symbol]";
subtype "ProfilerState" => as enum([qw[stop run]]);
diff --git a/perl-package/AI-MXNetCAPI/mxnet.i b/perl-package/AI-MXNetCAPI/mxnet.i
index 663a0c2..a55ca13 100644
--- a/perl-package/AI-MXNetCAPI/mxnet.i
+++ b/perl-package/AI-MXNetCAPI/mxnet.i
@@ -261,13 +261,13 @@ int MXRandomSeed(int seed);
int MXNotifyShutdown();
/*!
* \brief Set up configuration of profiler
- * \param mode indicate the working mode of profiler,
- * record anly symbolic operator when mode == 0,
- * record all operator when mode == 1
- * \param filename where to save trace file
+ * \param num_params Number of parameters
+ * \param keys array of parameter keys
+ * \param vals array of parameter values
* \return 0 when success, -1 when failure happens.
*/
-int MXSetProfilerConfig(int mode, const char* filename);
+int MXSetProfilerConfig(int num_params, const char* const* keys, const char* const* vals);
+
/*!
* \brief Set up state of profiler
* \param state indicate the working state of profiler,
@@ -278,7 +278,7 @@ int MXSetProfilerConfig(int mode, const char* filename);
int MXSetProfilerState(int state);
/*! \brief Save profile and stop profiler */
-int MXDumpProfile();
+int MXDumpProfile(int finished);
/*! \brief Set the number of OMP threads to use */
int MXSetNumOMPThreads(int thread_num);
diff --git a/perl-package/AI-MXNetCAPI/mxnet_typemaps.i b/perl-package/AI-MXNetCAPI/mxnet_typemaps.i
index fb794de..cfbcede 100644
--- a/perl-package/AI-MXNetCAPI/mxnet_typemaps.i
+++ b/perl-package/AI-MXNetCAPI/mxnet_typemaps.i
@@ -27,7 +27,7 @@
Safefree($1);
}
-%typemap(in) (const char **keys, const char **vals), (char **keys, char **vals)
+%typemap(in) (const char **keys, const char **vals), (char **keys, char **vals), (const char* const* keys, const char* const* vals)
{
HV *temphv;
char *key;
diff --git a/python/mxnet/base.py b/python/mxnet/base.py
index 2b04a68..156ef35 100644
--- a/python/mxnet/base.py
+++ b/python/mxnet/base.py
@@ -130,6 +130,7 @@ RecordIOHandle = ctypes.c_void_p
RtcHandle = ctypes.c_void_p
CudaModuleHandle = ctypes.c_void_p
CudaKernelHandle = ctypes.c_void_p
+ProfileHandle = ctypes.c_void_p
#----------------------------
# helper function definition
#----------------------------
diff --git a/python/mxnet/profiler.py b/python/mxnet/profiler.py
index 7356ed0..3ef262e 100644
--- a/python/mxnet/profiler.py
+++ b/python/mxnet/profiler.py
@@ -20,9 +20,42 @@
# pylint: disable=too-many-branches, too-many-statements
"""Profiler setting methods."""
from __future__ import absolute_import
-
import ctypes
-from .base import _LIB, check_call, c_str
+import warnings
+from .base import _LIB, check_call, c_str, ProfileHandle, c_str_array, py_str
+
+
+def set_config(**kwargs):
+ """Set up the configure of profiler (only accepts keyword arguments).
+
+ Parameters
+ ----------
+ filename : string,
+ output file for profile data
+ profile_all : boolean,
+ all profile types enabled
+ profile_symbolic : boolean,
+ whether to profile symbolic operators
+ profile_imperative : boolean,
+ whether to profile imperative operators
+ profile_memory : boolean,
+ whether to profile memory usage
+ profile_api : boolean,
+ whether to profile the C API
+ contiguous_dump : boolean,
+ whether to periodically dump profiling data to file
+ dump_period : float,
+ seconds between profile data dumps
+ aggregate_stats : boolean,
+ whether to maintain aggregate stats in memory for console
+ dump. Has some negative performance impact.
+ """
+ kk = kwargs.keys()
+ vv = kwargs.values()
+ check_call(_LIB.MXSetProfilerConfig(len(kwargs),
+ c_str_array([key for key in kk]),
+ c_str_array([str(val) for val in vv])))
+
def profiler_set_config(mode='symbolic', filename='profile.json'):
"""Set up the configure of profiler.
@@ -35,13 +68,16 @@ def profiler_set_config(mode='symbolic', filename='profile.json'):
filename : string, optional
The name of output trace file. Defaults to 'profile.json'.
"""
- mode2int = {'symbolic': 0, 'all': 1}
- check_call(_LIB.MXSetProfilerConfig(
- ctypes.c_int(mode2int[mode]),
- c_str(filename)))
+ warnings.warn('profiler.profiler_set_config() is deprecated. ' \
+ 'Please use profiler.set_config() instead')
+ keys = c_str_array([key for key in ["profile_" + mode, "filename"]])
+ values = c_str_array([str(val) for val in [True, filename]])
+ assert len(keys) == len(values)
+ check_call(_LIB.MXSetProfilerConfig(len(keys), keys, values))
+
-def profiler_set_state(state='stop'):
- """Set up the profiler state to record operator.
+def set_state(state='stop'):
+ """Set up the profiler state to 'run' or 'stop'.
Parameters
----------
@@ -52,7 +88,326 @@ def profiler_set_state(state='stop'):
state2int = {'stop': 0, 'run': 1}
check_call(_LIB.MXSetProfilerState(ctypes.c_int(state2int[state])))
+
+def dump(finished=True):
+ """Dump profile and stop profiler. Use this to save profile
+ in advance in case your program cannot exit normally.
+
+ Parameters
+ ----------
+ finished : boolean
+ Indicates whether to stop statistic output (dumping) after this dump.
+ Default is True
+ """
+ fin = 1 if finished is True else False
+ check_call(_LIB.MXDumpProfile(fin))
+
+
def dump_profile():
"""Dump profile and stop profiler. Use this to save profile
in advance in case your program cannot exit normally."""
- check_call(_LIB.MXDumpProfile())
+ warnings.warn('profiler.dump_profile() is deprecated. ' \
+ 'Please use profiler.dump() instead')
+ dump(True)
+
+
+def dumps(reset=False):
+ """Return a printable string of aggregate profile stats.
+
+ Parameters
+ ----------
+ reset: boolean
+ Indicates whether to clean aggeregate statistical data collected up to this point
+ """
+ debug_str = ctypes.c_char_p()
+ do_reset = 1 if reset is True else 0
+ check_call(_LIB.MXAggregateProfileStatsPrint(ctypes.byref(debug_str), int(do_reset)))
+ return py_str(debug_str.value)
+
+
+def pause():
+ """Pause profiling."""
+ check_call(_LIB.MXProfilePause(int(1)))
+
+
+def resume():
+ """Resume paused profiling."""
+ check_call(_LIB.MXProfilePause(int(0)))
+
+
+class Domain(object):
+ """Profiling domain, used to group sub-objects like tasks, counters, etc into categories
+ Serves as part of 'categories' for chrome://tracing
+
+ Note: Domain handles are never destroyed.
+
+ Parameters
+ ----------
+ name : string
+ Name of the domain
+ """
+ def __init__(self, name):
+ self.name = name
+ self.handle = ProfileHandle()
+ check_call(_LIB.MXProfileCreateDomain(c_str(self.name), ctypes.byref(self.handle)))
+
+ def __str__(self):
+ return self.name
+
+ def new_task(self, name):
+ """Create new Task object owned by this domain
+
+ Parameters
+ ----------
+ name : string
+ Name of the task
+ """
+ return Task(self, name)
+
+ def new_frame(self, name):
+ """Create new Frame object owned by this domain
+
+ Parameters
+ ----------
+ name : string
+ Name of the frame
+ """
+ return Frame(self, name)
+
+ def new_counter(self, name, value=None):
+ """Create new Counter object owned by this domain
+
+ Parameters
+ ----------
+ name : string
+ Name of the counter
+ """
+ return Counter(self, name, value)
+
+ def new_marker(self, name):
+ """Create new Marker object owned by this domain
+
+ Parameters
+ ----------
+ name : string
+ Name of the marker
+ """
+ return Marker(self, name)
+
+class Task(object):
+ """Profiling Task class.
+
+ A task is a logical unit of work performed by a particular thread.
+ Tasks can nest; thus, tasks typically correspond to functions, scopes, or a case block
+ in a switch statement.
+ You can use the Task API to assign tasks to threads.
+
+ This is different from Frame in that all profiling statistics for passes
+ through the task's begin and endpoints are accumulated together into a single statistical
+ analysys, rather than a separate analysis for each pass (as with a Frame)
+
+ Parameters
+ ----------
+ domain : Domain object
+ Domain to which this object belongs
+ name : string
+ Name of the task
+ """
+ def __init__(self, domain, name):
+ self.name = name
+ self.handle = ProfileHandle()
+ check_call(_LIB.MXProfileCreateTask(domain.handle,
+ c_str(self.name),
+ ctypes.byref(self.handle)))
+
+ def __del__(self):
+ if self.handle is not None:
+ check_call(_LIB.MXProfileDestroyHandle(self.handle))
+
+ def start(self):
+ """Start timing scope for this object"""
+ check_call(_LIB.MXProfileDurationStart(self.handle))
+
+ def stop(self):
+ """Stop timing scope for this object"""
+ check_call(_LIB.MXProfileDurationStop(self.handle))
+
+ def __str__(self):
+ return self.name
+
+
+class Frame(object):
+ """Profiling Frame class.
+
+ Use the frame API to insert calls to the desired places in your code and analyze
+ performance per frame, where frame is the time period between frame begin and end points.
+ When frames are displayed in Intel VTune Amplifier, they are displayed in a
+ separate track, so they provide a way to visually separate this data from normal task data.
+
+ This is different from Task in that each 'Frame' duration will be a discretely-numbered
+ event in the VTune output, as well as its rate (frame-rate) shown. This is analogous to
+ profiling each frame of some visual output, such as rendering a video game frame.
+
+ Parameters
+ ----------
+ domain : Domain object
+ Domain to which this object belongs
+ name : string
+ Name of the frame
+ """
+ def __init__(self, domain, name):
+ self.name = name
+ self.handle = ProfileHandle()
+ check_call(_LIB.MXProfileCreateFrame(domain.handle,
+ c_str(self.name),
+ ctypes.byref(self.handle)))
+
+ def __del__(self):
+ if self.handle is not None:
+ check_call(_LIB.MXProfileDestroyHandle(self.handle))
+
+ def start(self):
+ """Start timing scope for this object"""
+ check_call(_LIB.MXProfileDurationStart(self.handle))
+
+ def stop(self):
+ """Stop timing scope for this object"""
+ check_call(_LIB.MXProfileDurationStop(self.handle))
+
+ def __str__(self):
+ return self.name
+
+
+class Event(object):
+ """Profiling Event class.
+
+ The event API is used to observe when demarcated events occur in your application, or to
+ identify how long it takes to execute demarcated regions of code. Set annotations in the
+ application to demarcate areas where events of interest occur.
+ After running analysis, you can see the events marked in the Timeline pane.
+ Event API is a per-thread function that works in resumed state.
+ This function does not work in paused state.
+
+ Parameters
+ ----------
+ name : string
+ Name of the event
+ """
+ def __init__(self, name):
+ self.name = name
+ self.handle = ProfileHandle()
+ check_call(_LIB.MXProfileCreateEvent(c_str(self.name), ctypes.byref(self.handle)))
+
+ def __del__(self):
+ if self.handle is not None:
+ check_call(_LIB.MXProfileDestroyHandle(self.handle))
+
+ def start(self):
+ """Start timing scope for this object"""
+ check_call(_LIB.MXProfileDurationStart(self.handle))
+
+ def stop(self):
+ """Stop timing scope for this object"""
+ check_call(_LIB.MXProfileDurationStop(self.handle))
+
+ def __str__(self):
+ return self.name
+
+
+class Counter(object):
+ """Profiling Counter class.
+
+ The counter event can track a value as it changes over time.
+
+ Parameters
+ ----------
+ domain : Domain object
+ Domain to which this object belongs
+ name : string
+ Name of the counter
+ value: integer, optional
+ Initial value of the counter
+ """
+ def __init__(self, domain, name, value=None):
+ self.name = name
+ self.handle = ProfileHandle()
+ check_call(_LIB.MXProfileCreateCounter(domain.handle,
+ c_str(name),
+ ctypes.byref(self.handle)))
+ if value is not None:
+ self.set_value(value)
+
+ def __del__(self):
+ if self.handle is not None:
+ check_call(_LIB.MXProfileDestroyHandle(self.handle))
+
+
+ def set_value(self, value):
+ """Set counter value.
+
+ Parameters
+ ----------
+ value : int
+ Value for the counter
+ """
+ check_call(_LIB.MXProfileSetCounter(self.handle, int(value)))
+
+ def increment(self, delta=1):
+ """Increment counter value.
+
+ Parameters
+ ----------
+ value_change : int
+ Amount by which to add to the counter
+ """
+ check_call(_LIB.MXProfileAdjustCounter(self.handle, int(delta)))
+
+ def decrement(self, delta=1):
+ """Decrement counter value.
+
+ Parameters
+ ----------
+ value_change : int
+ Amount by which to subtract from the counter
+ """
+ check_call(_LIB.MXProfileAdjustCounter(self.handle, -int(delta)))
+
+ def __iadd__(self, delta):
+ self.increment(delta)
+ return self
+
+ def __isub__(self, delta):
+ self.decrement(delta)
+ return self
+
+ def __str__(self):
+ return self.name
+
+
+class Marker(object):
+ """Set marker for an instant in time.
+
+ The marker event marks a particular instant in time across some scope boundaries.
+
+ Parameters
+ ----------
+ domain : Domain object
+ Domain to which this object belongs
+ name : string
+ Name of the marker
+ """
+ def __init__(self, domain, name):
+ self.name = name
+ self.domain = domain
+
+ def mark(self, scope='process'):
+ """Set up the profiler state to record operator.
+
+ Parameters
+ ----------
+ scope : string, optional
+ Indicates what scope the marker should refer to.
+ Can be 'global', 'process', thread', task', and 'marker'
+ Default is `process`.
+ """
+ check_call(_LIB.MXProfileSetMarker(self.domain.handle, c_str(self.name), c_str(scope)))
diff --git a/scala-package/core/src/main/scala/ml/dmlc/mxnet/LibInfo.scala b/scala-package/core/src/main/scala/ml/dmlc/mxnet/LibInfo.scala
index a943e31..38d2579 100644
--- a/scala-package/core/src/main/scala/ml/dmlc/mxnet/LibInfo.scala
+++ b/scala-package/core/src/main/scala/ml/dmlc/mxnet/LibInfo.scala
@@ -315,7 +315,7 @@ private[mxnet] class LibInfo {
@native def mxCustomOpRegister(regName: String, opProp: CustomOpProp): Int
// Profiler
- @native def mxSetProfilerConfig(mode: Int, fileName: String): Int
+ @native def mxSetProfilerConfig(keys: Array[String], vals: Array[String]): Int
@native def mxSetProfilerState(state: Int): Int
- @native def mxDumpProfile(): Int
+ @native def mxDumpProfile(finished: Int): Int
}
diff --git a/scala-package/core/src/main/scala/ml/dmlc/mxnet/Profiler.scala b/scala-package/core/src/main/scala/ml/dmlc/mxnet/Profiler.scala
index ce1103a..e730942 100644
--- a/scala-package/core/src/main/scala/ml/dmlc/mxnet/Profiler.scala
+++ b/scala-package/core/src/main/scala/ml/dmlc/mxnet/Profiler.scala
@@ -21,7 +21,6 @@ import ml.dmlc.mxnet.Base._
object Profiler {
- val mode2Int = Map("symbolic" -> 0, "all" -> 1)
val state2Int = Map("stop" -> 0, "run" -> 1)
/**
@@ -32,9 +31,10 @@ object Profiler {
* @param fileName, optional
* The name of output trace file. Default is "profile.json".
*/
- def profilerSetConfig(mode: String = "symbolic", fileName: String = "profile.json"): Unit = {
- require(mode2Int.contains(mode))
- checkCall(_LIB.mxSetProfilerConfig(mode2Int(mode), fileName))
+ def profilerSetConfig(kwargs: Map[String, String]): Unit = {
+ val keys = kwargs.keys.toArray
+ val vals = kwargs.values.toArray
+ checkCall(_LIB.mxSetProfilerConfig(keys, vals))
}
/**
@@ -52,7 +52,7 @@ object Profiler {
* Dump profile and stop profiler. Use this to save profile
* in advance in case your program cannot exit normally.
*/
- def dumpProfile(): Unit = {
- checkCall(_LIB.mxDumpProfile())
+ def dumpProfile(finished: Int = 1): Unit = {
+ checkCall(_LIB.mxDumpProfile(finished))
}
}
diff --git a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerMatMul.scala b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerMatMul.scala
index 9551e4a..fe62564 100644
--- a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerMatMul.scala
+++ b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerMatMul.scala
@@ -41,7 +41,8 @@ object ProfilerMatMul {
val ctx = if (erul.gpu >= 0) Context.gpu(erul.gpu) else Context.cpu()
val path = s"${erul.outputPath}${File.separator}${erul.profilerName}"
- Profiler.profilerSetConfig(mode = erul.profilerMode, fileName = path)
+ val kwargs = Map("file_name" -> path, "profile_" + erul.profilerMode -> "1")
+ Profiler.profilerSetConfig(kwargs)
logger.info(s"profile file save to $path")
val A = Symbol.Variable("A")
@@ -88,7 +89,8 @@ object ProfilerMatMul {
}
class ProfilerMatMul {
- @Option(name = "--profiler-mode", usage = "the profiler mode, can be \"symbolic\" or \"all\".")
+ @Option(name = "--profiler-mode", usage = "the profiler mode, can be \"symbolic\""
+ + ", \"imperative\", \"api\", \"mem\", etc.")
private val profilerMode: String = "symbolic"
@Option(name = "--output-path", usage = "the profile file output directory.")
private val outputPath: String = "."
diff --git a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerNDArray.scala b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerNDArray.scala
index d16fbb1..9c44f9a 100644
--- a/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerNDArray.scala
+++ b/scala-package/examples/src/main/scala/ml/dmlc/mxnetexamples/profiler/ProfilerNDArray.scala
@@ -216,7 +216,8 @@ object ProfilerNDArray {
parser.parseArgument(args.toList.asJava)
val path = s"${eray.outputPath}${File.separator}${eray.profilerName}"
- Profiler.profilerSetConfig(mode = eray.profilerMode, fileName = path)
+ val kwargs = Map("file_name" -> path, "profile_" + eray.profilerMode -> "1")
+ Profiler.profilerSetConfig(kwargs)
logger.info(s"profile file save to $path")
Profiler.profilerSetState("run")
@@ -241,8 +242,9 @@ object ProfilerNDArray {
}
class ProfilerNDArray {
- @Option(name = "--profiler-mode", usage = "the profiler mode, can be \"symbolic\" or \"all\".")
- private val profilerMode: String = "all"
+ @Option(name = "--profiler-mode", usage = "the profiler mode, can be \"symbolic\""
+ + ", \"imperative\", \"api\", \"mem\", etc.")
+ private val profilerMode: String = "symbolic"
@Option(name = "--output-path", usage = "the profile file output directory.")
private val outputPath: String = "."
@Option(name = "--profile-filename", usage = "the profile file name.")
diff --git a/scala-package/native/src/main/native/ml_dmlc_mxnet_native_c_api.cc b/scala-package/native/src/main/native/ml_dmlc_mxnet_native_c_api.cc
index f4fe93b..10085f0 100644
--- a/scala-package/native/src/main/native/ml_dmlc_mxnet_native_c_api.cc
+++ b/scala-package/native/src/main/native/ml_dmlc_mxnet_native_c_api.cc
@@ -2510,11 +2510,49 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_mxnet_LibInfo_mxCustomOpRegister
return MXCustomOpRegister(regName, creator);
}
-JNIEXPORT jint JNICALL Java_ml_dmlc_mxnet_LibInfo_mxSetProfilerConfig
- (JNIEnv *env, jobject obj, jint jmode, jstring jfilename) {
- const char *fileName = env->GetStringUTFChars(jfilename, 0);
- int ret = MXSetProfilerConfig(jmode, fileName);
- env->ReleaseStringUTFChars(jfilename, fileName);
+struct JNIString {
+ JNIEnv *env_;
+ jstring java_string_;
+ const char *str_;
+ inline JNIString(JNIEnv *env, const jstring& java_string)
+ : env_(env)
+ , java_string_(java_string) {
+ str_ = env_->GetStringUTFChars(java_string_, 0);
+ }
+ inline ~JNIString() {
+ if (str_) {
+ env_->ReleaseStringUTFChars(java_string_, str_);
+ }
+ }
+ inline const char *operator ()() const {
+ return str_;
+ }
+};
+
+struct JNIStringArray {
+ std::vector<std::unique_ptr<JNIString>> jni_strings_;
+ std::vector<const char *> strings_;
+ JNIStringArray(JNIEnv *env, const jobjectArray& stringArray) {
+ const int count = env->GetArrayLength(stringArray);
+ jni_strings_.reserve(count);
+ strings_.reserve(count);
+ for (int i = 0; i < count; ++i) {
+ jstring string = static_cast<jstring>(env->GetObjectArrayElement(stringArray, i));
+ jni_strings_.emplace_back(std::unique_ptr<JNIString>(new JNIString(env, string)));
+ strings_.emplace_back((*jni_strings_.rbegin())->str_);
+ }
+ }
+ const char * const* operator ()() const { return &strings_[0]; }
+};
+
+JNIEXPORT jint JNICALL Java_org_apache_mxnet_LibInfo_mxSetProfilerConfig
+ (JNIEnv *env, jobject obj, jobjectArray keys, jobjectArray vals) {
+ const int stringCount = env->GetArrayLength(keys);
+ CHECK_EQ(stringCount, env->GetArrayLength(vals)) << "Key and value arrays must be the same size";
+
+ JNIStringArray the_keys(env, keys), the_vals(env, vals);
+
+ const int ret = MXSetProfilerConfig(stringCount, the_keys(), the_vals());
return ret;
}
@@ -2524,6 +2562,6 @@ JNIEXPORT jint JNICALL Java_ml_dmlc_mxnet_LibInfo_mxSetProfilerState
}
JNIEXPORT jint JNICALL Java_ml_dmlc_mxnet_LibInfo_mxDumpProfile
- (JNIEnv *env, jobject obj) {
- return MXDumpProfile();
+ (JNIEnv *env, jobject obj, jint finished) {
+ return MXDumpProfile(finished);
}
diff --git a/src/c_api/c_api.cc b/src/c_api/c_api.cc
index 9be8c6e..3ef0043 100644
--- a/src/c_api/c_api.cc
+++ b/src/c_api/c_api.cc
@@ -45,7 +45,6 @@
#include <utility>
#include "./c_api_common.h"
#include "../operator/custom/custom-inl.h"
-#include "../engine/profiler.h"
using namespace mxnet;
@@ -97,41 +96,6 @@ int MXNotifyShutdown() {
API_END();
}
-int MXSetProfilerConfig(int mode, const char* filename) {
- // mode, kOnlySymbolic: 0, kAllOperator: 1
- API_BEGIN();
-#if MXNET_USE_PROFILER
- engine::Profiler::Get()->SetConfig(engine::Profiler::ProfilerMode(mode), std::string(filename));
-#else
- LOG(FATAL) << "Need to compile with USE_PROFILER=1 for MXNet Profiler";
-#endif
- API_END();
-}
-
-int MXDumpProfile() {
- API_BEGIN();
-#if MXNET_USE_PROFILER
- engine::Profiler *profiler = engine::Profiler::Get();
- CHECK(profiler->IsEnableOutput())
- << "Profiler haven't been run. Config and start profiler first";
- engine::Profiler::Get()->DumpProfile();
-#else
- LOG(FATAL) << "Need to compile with USE_PROFILER=1 for MXNet Profiler";
-#endif
- API_END()
-}
-
-int MXSetProfilerState(int state) {
- // state, kNotRunning: 0, kRunning: 1
- API_BEGIN();
-#if MXNET_USE_PROFILER
- engine::Profiler::Get()->SetState(engine::Profiler::ProfilerState(state));
-#else
- LOG(FATAL) << "Need to compile with USE_PROFILER=1 for MXNet Profiler";
-#endif
- API_END();
-}
-
int MXSetNumOMPThreads(int thread_num) {
API_BEGIN();
omp_set_num_threads(thread_num);
diff --git a/src/c_api/c_api_common.h b/src/c_api/c_api_common.h
index 7f86604..079b587 100644
--- a/src/c_api/c_api_common.h
+++ b/src/c_api/c_api_common.h
@@ -35,16 +35,16 @@
#include <string>
/*! \brief macro to guard beginning and end section of all functions */
-#define API_BEGIN() try {
+#define API_BEGIN() try { on_enter_api(__FUNCTION__);
/*! \brief every function starts with API_BEGIN();
and finishes with API_END() or API_END_HANDLE_ERROR */
-#define API_END() } catch(dmlc::Error &_except_) { return MXAPIHandleException(_except_); } return 0; // NOLINT(*)
+#define API_END() } catch(dmlc::Error &_except_) { on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0; // NOLINT(*)
/*!
* \brief every function starts with API_BEGIN();
* and finishes with API_END() or API_END_HANDLE_ERROR
* The finally clause contains procedure to cleanup states when an error happens.
*/
-#define API_END_HANDLE_ERROR(Finalize) } catch(dmlc::Error &_except_) { Finalize; return MXAPIHandleException(_except_); } return 0; // NOLINT(*)
+#define API_END_HANDLE_ERROR(Finalize) } catch(dmlc::Error &_except_) { Finalize; on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0; // NOLINT(*)
/*!
* \brief Set the last error message needed by C API
@@ -137,6 +137,10 @@ inline void CopyAttr(const nnvm::IndexedGraph& idx,
// stores keys that will be converted to __key__
extern const std::vector<std::string> kHiddenKeys;
+
+extern void on_enter_api(const char *function);
+extern void on_exit_api();
+
} // namespace mxnet
#endif // MXNET_C_API_C_API_COMMON_H_
diff --git a/src/c_api/c_api_profile.cc b/src/c_api/c_api_profile.cc
new file mode 100644
index 0000000..16b47ad
--- /dev/null
+++ b/src/c_api/c_api_profile.cc
@@ -0,0 +1,595 @@
+//
+// 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) {
+#if MXNET_USE_PROFILER
+ if (profiler::Profiler::Get()->IsProfiling(profiler::Profiler::kAPI)) {
+ if (!thread_profiling_data.ignore_call_) {
+ ++api_call_counter;
+ ++api_concurrency_counter;
+ APICallTimingData data = {
+ function
+ , thread_profiling_data.profile_task(function, &api_domain)
+#ifdef PROFILE_API_INCLUDE_AS_EVENT
+ , thread_profiling_data.profile_event(function)
+#endif // PROFILE_API_INCLUDE_AS_EVENT
+ };
+ thread_profiling_data.calls_.push(data);
+ data.task_->start();
+#ifdef PROFILE_API_INCLUDE_AS_EVENT
+ data.event_->start();
+#endif // PROFILE_API_INCLUDE_AS_EVENT
+ }
+ }
+#endif // MXNET_USE_PROFILER
+}
+extern void on_exit_api() {
+#if MXNET_USE_PROFILER
+ if (profiler::Profiler::Get()->IsProfiling(profiler::Profiler::kAPI)) {
+ if (!thread_profiling_data.ignore_call_) {
+ CHECK(!thread_profiling_data.calls_.empty());
+ APICallTimingData data = thread_profiling_data.calls_.top();
+#ifdef PROFILE_API_INCLUDE_AS_EVENT
+ data.event_->stop();
+#endif // PROFILE_API_INCLUDE_AS_EVENT
+ data.task_->stop();
+ thread_profiling_data.calls_.pop();
+ --api_concurrency_counter;
+ }
+ }
+#endif // MXNET_USE_PROFILER
+}
+
+/*!
+ * \brief Don't profile calls in this scope using RAII
+ */
+struct IgnoreProfileCallScope {
+ IgnoreProfileCallScope() {
+#if MXNET_USE_PROFILER
+ DCHECK_EQ(thread_profiling_data.ignore_call_, false);
+ thread_profiling_data.ignore_call_ = true;
+#endif // MXNET_USE_PROFILER
+ }
+ ~IgnoreProfileCallScope() {
+#if MXNET_USE_PROFILER
+ DCHECK_EQ(thread_profiling_data.ignore_call_, true);
+ thread_profiling_data.ignore_call_ = false;
+#endif // MXNET_USE_PROFILER
+ }
+};
+
+} // namespace mxnet
+
+/*!
+ * \brief Simple global profile objects created from Python
+ * \note These mutexes will almost never have a collision, so internal futexes will be able
+ * to lock in user mode (good performance)
+ * I would use dmlc::SpinLock, except that I am concerned that if conditions change and
+ * there are frequent collisions (ie multithreaded inference), then the spin locks may
+ * start burning CPU unnoticed
+ */
+struct PythonProfileObjects {
+ // These will almost never collide, so locking will happen in user-space (at least on Linux)
+ // since pthreads uses futexes.
+ std::mutex cs_domains_;
+ std::mutex cs_counters_;
+ std::mutex cs_tasks_;
+ std::mutex cs_frames_;
+ std::mutex cs_events_;
+ std::list<std::shared_ptr<profiler::ProfileDomain>> domains_;
+ std::unordered_map<profiler::ProfileCounter *, std::shared_ptr<profiler::ProfileCounter>>
+ counters_;
+ std::unordered_map<profiler::ProfileDuration *, std::shared_ptr<profiler::ProfileDuration>>
+ tasks_;
+ std::unordered_map<profiler::ProfileDuration *, std::shared_ptr<profiler::ProfileDuration>>
+ frames_;
+ std::unordered_map<profiler::ProfileDuration *, std::shared_ptr<profiler::ProfileDuration>>
+ events_;
+};
+static PythonProfileObjects python_profile_objects;
+
+#if !defined(MXNET_USE_PROFILER) || !MXNET_USE_PROFILER
+static void warn_not_built_with_profiler_enabled() {
+ static volatile bool warned_not_built_with_profiler_enabled = false;
+ if (!warned_not_built_with_profiler_enabled) {
+ warned_not_built_with_profiler_enabled = true;
+ LOG(WARNING) << "Need to compile with USE_PROFILER=1 for MXNet Profiling";
+ }
+}
+#endif // MXNET_USE_PROFILER
+
+struct ProfileConfigParam : public dmlc::Parameter<ProfileConfigParam> {
+ bool profile_all;
+ bool profile_symbolic;
+ bool profile_imperative;
+ bool profile_memory;
+ bool profile_api;
+ std::string filename;
+ bool continuous_dump;
+ float dump_period;
+ bool aggregate_stats;
+ DMLC_DECLARE_PARAMETER(ProfileConfigParam) {
+ DMLC_DECLARE_FIELD(profile_all).set_default(false)
+ .describe("Profile all.");
+ DMLC_DECLARE_FIELD(profile_symbolic).set_default(true)
+ .describe("Profile symbolic operators.");
+ DMLC_DECLARE_FIELD(profile_imperative).set_default(true)
+ .describe("Profile imperative operators.");
+ DMLC_DECLARE_FIELD(profile_memory).set_default(true)
+ .describe("Profile memory.");
+ DMLC_DECLARE_FIELD(profile_api).set_default(true)
+ .describe("Profile C API.");
+ DMLC_DECLARE_FIELD(filename).set_default("profile.json")
+ .describe("File name to write profiling info.");
+ DMLC_DECLARE_FIELD(continuous_dump).set_default(true)
+ .describe("Periodically dump (and append) priofling data to file while running.");
+ DMLC_DECLARE_FIELD(dump_period).set_default(1.0f)
+ .describe("When continuous dump is enabled, the period between subsequent "
+ "profile info dumping.");
+ DMLC_DECLARE_FIELD(aggregate_stats).set_default(false)
+ .describe("Maintain aggregate stats, required for MXDumpAggregateStats. Note that "
+ "this can have anegative performance impact.");
+ }
+};
+
+DMLC_REGISTER_PARAMETER(ProfileConfigParam);
+
+struct ProfileMarkerScopeParam : public dmlc::Parameter<ProfileMarkerScopeParam> {
+ int scope;
+ DMLC_DECLARE_PARAMETER(ProfileMarkerScopeParam) {
+ DMLC_DECLARE_FIELD(scope).set_default(profiler::ProfileMarker::kProcess)
+ .add_enum("global", profiler::ProfileMarker::kGlobal)
+ .add_enum("process", profiler::ProfileMarker::kProcess)
+ .add_enum("thread", profiler::ProfileMarker::kThread)
+ .add_enum("task", profiler::ProfileMarker::kTask)
+ .add_enum("marker", profiler::ProfileMarker::kMarker)
+ .describe("Profile Instant-Marker scope.");
+ }
+};
+
+DMLC_REGISTER_PARAMETER(ProfileMarkerScopeParam);
+
+int MXSetProfilerConfig(int num_params, const char* const* keys, const char* const* vals) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ std::vector<std::pair<std::string, std::string>> kwargs;
+ kwargs.reserve(num_params);
+ for (int i = 0; i < num_params; ++i) {
+ CHECK_NOTNULL(keys[i]);
+ CHECK_NOTNULL(vals[i]);
+ kwargs.emplace_back(std::make_pair(keys[i], vals[i]));
+ }
+ ProfileConfigParam param;
+ param.Init(kwargs);
+ int mode = 0;
+ if (param.profile_api || param.profile_all) { mode |= profiler::Profiler::kAPI; }
+ if (param.profile_symbolic || param.profile_all) { mode |= profiler::Profiler::kSymbolic; }
+ if (param.profile_imperative || param.profile_all) { mode |= profiler::Profiler::kImperative; }
+ if (param.profile_memory || param.profile_all) { mode |= profiler::Profiler::kMemory; }
+ profiler::Profiler::Get()->SetConfig(profiler::Profiler::ProfilerMode(mode),
+ std::string(param.filename),
+ param.continuous_dump,
+ param.dump_period,
+ param.aggregate_stats);
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXAggregateProfileStatsPrint(const char **out_str, int reset) {
+ MXAPIThreadLocalEntry *ret = MXAPIThreadLocalStore::Get();
+ API_BEGIN();
+ CHECK_NOTNULL(out_str);
+#if MXNET_USE_PROFILER
+ profiler::Profiler *profiler = profiler::Profiler::Get();
+ if (profiler->IsEnableOutput()) {
+ // Register stats up until now
+ profiler->DumpProfile(false);
+ }
+ std::shared_ptr<profiler::AggregateStats> stats = profiler->GetAggregateStats();
+ std::ostringstream os;
+ if (stats) {
+ stats->Dump(os, reset != 0);
+ }
+ ret->ret_str = os.str();
+#else
+ warn_not_built_with_profiler_enabled();
+ ret->ret_str.clear();
+#endif
+ *out_str = (ret->ret_str).c_str();
+ API_END();
+}
+
+int MXDumpProfile(int finished) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ profiler::Profiler *profiler = profiler::Profiler::Get();
+ CHECK(profiler->IsEnableOutput())
+ << "Profiler hasn't been run. Config and start profiler first";
+ profiler->DumpProfile(finished != 0);
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END()
+}
+
+int MXSetProfilerState(int state) {
+ mxnet::IgnoreProfileCallScope ignore;
+ // state, kNotRunning: 0, kRunning: 1
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ switch (state) {
+ case profiler::Profiler::kNotRunning:
+ profiler::vtune::vtune_pause();
+ break;
+ case profiler::Profiler::kRunning:
+ profiler::vtune::vtune_resume();
+ break;
+ }
+ profiler::Profiler::Get()->SetState(profiler::Profiler::ProfilerState(state));
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXProfileCreateDomain(const char *domain, ProfileHandle *out) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ auto dom = std::make_shared<profiler::ProfileDomain>(domain);
+ {
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_domains_);
+ python_profile_objects.domains_.push_back(dom);
+ }
+ *out = dom.get();
+#else
+ warn_not_built_with_profiler_enabled();
+ *out = nullptr;
+#endif
+ API_END();
+}
+
+int MXProfileCreateTask(ProfileHandle domain,
+ const char *task_name,
+ ProfileHandle *out) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ auto ctr =
+ std::make_shared<profiler::ProfileTask>(task_name,
+ static_cast<profiler::ProfileDomain *>(domain));
+ {
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_tasks_);
+ python_profile_objects.tasks_.emplace(std::make_pair(ctr.get(), ctr));
+ }
+ *out = ctr.get();
+#else
+ warn_not_built_with_profiler_enabled();
+ *out = nullptr;
+#endif
+ API_END();
+}
+
+int MXProfileCreateFrame(ProfileHandle domain,
+ const char *frame_name,
+ ProfileHandle *out) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ auto ctr =
+ std::make_shared<profiler::ProfileFrame>(frame_name,
+ static_cast<profiler::ProfileDomain *>(domain));
+ {
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_frames_);
+ python_profile_objects.frames_.emplace(std::make_pair(ctr.get(), ctr));
+ }
+ *out = ctr.get();
+#else
+ warn_not_built_with_profiler_enabled();
+ *out = nullptr;
+#endif
+ API_END();
+}
+
+int MXProfileCreateEvent(const char *event_name, ProfileHandle *out) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ auto ctr =
+ std::make_shared<profiler::ProfileEvent>(event_name);
+ {
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_events_);
+ python_profile_objects.events_.emplace(std::make_pair(ctr.get(), ctr));
+ }
+ *out = ctr.get();
+#else
+ warn_not_built_with_profiler_enabled();
+ *out = nullptr;
+#endif
+ API_END();
+}
+
+int MXProfileDestroyHandle(ProfileHandle object_handle) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ CHECK_NE(object_handle, static_cast<ProfileHandle>(nullptr))
+ << "Invalid NULL handle passed to MXProfileDestroyHandle";
+ std::shared_ptr<profiler::ProfileObject> shared_object_ptr(nullptr);
+ {
+ auto object = static_cast<profiler::ProfileObject *>(object_handle);
+ switch (object->type()) {
+ case profiler::kTask: {
+ auto p = static_cast<profiler::ProfileDuration *>(object_handle);
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_tasks_);
+ auto iter = python_profile_objects.tasks_.find(p);
+ if (iter != python_profile_objects.tasks_.end()) {
+ shared_object_ptr = iter->second;
+ python_profile_objects.tasks_.erase(iter);
+ }
+ break;
+ }
+ case profiler::kEvent: {
+ auto p = static_cast<profiler::ProfileDuration *>(object_handle);
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_events_);
+ auto iter = python_profile_objects.events_.find(p);
+ if (iter != python_profile_objects.events_.end()) {
+ shared_object_ptr = iter->second;
+ python_profile_objects.events_.erase(iter);
+ }
+ break;
+ }
+ case profiler::kFrame: {
+ auto p = static_cast<profiler::ProfileDuration *>(object_handle);
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_frames_);
+ auto iter = python_profile_objects.frames_.find(p);
+ if (iter != python_profile_objects.frames_.end()) {
+ shared_object_ptr = iter->second;
+ python_profile_objects.frames_.erase(iter);
+ }
+ break;
+ }
+ case profiler::kCounter: {
+ auto p = static_cast<profiler::ProfileCounter *>(object_handle);
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_counters_);
+ auto iter = python_profile_objects.counters_.find(p);
+ if (iter != python_profile_objects.counters_.end()) {
+ shared_object_ptr = iter->second;
+ python_profile_objects.counters_.erase(iter);
+ }
+ break;
+ }
+ case profiler::kDomain:
+ // Not destroyed
+ break;
+ }
+ }
+ shared_object_ptr.reset(); // Destroy out of lock scope
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXProfileDurationStart(ProfileHandle duration_handle) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ CHECK_NOTNULL(duration_handle);
+ static_cast<profiler::ProfileDuration *>(duration_handle)->start();
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXProfileDurationStop(ProfileHandle duration_handle) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ CHECK_NOTNULL(duration_handle);
+ static_cast<profiler::ProfileDuration *>(duration_handle)->stop();
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXProfilePause(int paused) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ if (paused) {
+ profiler::vtune::vtune_pause();
+ profiler::Profiler::Get()->set_paused(true);
+ } else {
+ profiler::Profiler::Get()->set_paused(false);
+ profiler::vtune::vtune_resume();
+ }
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXProfileCreateCounter(ProfileHandle domain,
+ const char *counter_name,
+ ProfileHandle *out) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ auto ctr =
+ std::make_shared<profiler::ProfileCounter>(counter_name,
+ static_cast<profiler::ProfileDomain *>(domain));
+ {
+ std::unique_lock<std::mutex> lock(python_profile_objects.cs_counters_);
+ python_profile_objects.counters_.emplace(std::make_pair(ctr.get(), ctr));
+ }
+ *out = ctr.get();
+#else
+ warn_not_built_with_profiler_enabled();
+ *out = nullptr;
+#endif
+ API_END();
+}
+
+int MXProfileSetCounter(ProfileHandle counter_handle, uint64_t value) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ static_cast<profiler::ProfileCounter *>(counter_handle)->operator=(value);
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXProfileAdjustCounter(ProfileHandle counter_handle, int64_t by_value) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ static_cast<profiler::ProfileCounter *>(counter_handle)->operator+=(by_value);
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
+
+int MXProfileSetMarker(ProfileHandle domain,
+ const char *instant_marker_name,
+ const char *scope) {
+ mxnet::IgnoreProfileCallScope ignore;
+ API_BEGIN();
+#if MXNET_USE_PROFILER
+ ProfileMarkerScopeParam param;
+ std::vector<std::pair<std::string, std::string>> kwargs = {{ "scope", scope }};
+ param.Init(kwargs);
+ profiler::ProfileMarker marker(instant_marker_name,
+ static_cast<profiler::ProfileDomain *>(domain),
+ static_cast<profiler::ProfileMarker::MarkerScope>(
+ param.scope));
+ marker.mark();
+#else
+ warn_not_built_with_profiler_enabled();
+#endif
+ API_END();
+}
diff --git a/src/engine/naive_engine.cc b/src/engine/naive_engine.cc
index adf14b1..6246b73 100644
--- a/src/engine/naive_engine.cc
+++ b/src/engine/naive_engine.cc
@@ -26,7 +26,7 @@
#include <atomic>
#include <thread>
#include "./engine_impl.h"
-#include "./profiler.h"
+#include "../profiler/profiler.h"
#include "./openmp.h"
namespace mxnet {
@@ -44,7 +44,7 @@ class NaiveEngine final : public Engine {
/*! \brief indicate whether to profile this operator */
bool profiling{false};
/*! \brief operator execution statistics */
- OprExecStat *opr_stat;
+ std::unique_ptr<profiler::ProfileOperator> opr_profile;
};
NaiveEngine() {
@@ -90,23 +90,22 @@ class NaiveEngine final : public Engine {
}
void Push(OprHandle op, Context exec_ctx, int priority = 0, bool profiling = false) override {
- Profiler *profiler = Profiler::Get();
+ profiler::Profiler *profiler = profiler::Profiler::Get();
NaiveOpr *opr = op->Cast<NaiveOpr>();
- opr->profiling = profiling && (profiler->GetMode() == Profiler::kOnlySymbolic);
+ opr->profiling = profiling && profiler->IsProfiling(profiler::Profiler::kSymbolic);
this->PushAsync([&](RunContext ctx, CallbackOnComplete on_complete) {
#if MXNET_USE_PROFILER
if (opr->profiling) {
- opr->opr_stat = Profiler::Get()->AddOprStat(exec_ctx.dev_type, exec_ctx.dev_id);
- uint64_t id = std::hash<std::thread::id>()(std::this_thread::get_id());
- opr->opr_stat->thread_id = id;
- strncpy(opr->opr_stat->opr_name,
- opr->opr_name,
- sizeof(opr->opr_stat->opr_name) - 1);
- SetOprStart(opr->opr_stat);
+ std::unique_ptr<profiler::ProfileOperator::Attributes> attrs;
+ if (profiler->AggregateEnabled()) {
+ attrs.reset(new profiler::ProfileOperator::Attributes());
+ }
+ opr->opr_profile.reset(new profiler::ProfileOperator(opr->opr_name, attrs.release()));
+ opr->opr_profile->start(exec_ctx.dev_type, exec_ctx.dev_id);
}
opr->fn(ctx, on_complete);
if (opr->profiling) {
- SetOprEnd(opr->opr_stat);
+ opr->opr_profile->stop();
}
#else
opr->fn(ctx, on_complete);
@@ -132,22 +131,19 @@ class NaiveEngine final : public Engine {
NaiveEngine::OnComplete, nullptr);
this->req_completed_ = false;
#if MXNET_USE_PROFILER
- Profiler *profiler = Profiler::Get();
+ profiler::Profiler *profiler = profiler::Profiler::Get();
NaiveOpr *opr = nullptr;
- bool profiling = (profiler->GetState() == Profiler::kRunning) &&
- (profiler->GetMode() == Profiler::kAllOperator) &&
- opr_name;
+ const bool profiling = opr_name && profiler->IsProfiling(profiler::Profiler::kImperative);
if (profiling) {
opr = NewOperator(exec_fun, const_vars, mutable_vars,
prop, opr_name)->Cast<NaiveOpr>();
opr->profiling = profiling;
- opr->opr_stat = Profiler::Get()->AddOprStat(exec_ctx.dev_type, exec_ctx.dev_id);
- uint64_t id = std::hash<std::thread::id>()(std::this_thread::get_id());
- opr->opr_stat->thread_id = id;
- strncpy(opr->opr_stat->opr_name,
- opr->opr_name,
- sizeof(opr->opr_stat->opr_name) - 1);
- SetOprStart(opr->opr_stat);
+ std::unique_ptr<profiler::ProfileOperator::Attributes> attrs;
+ if (profiler->AggregateEnabled()) {
+ attrs.reset(new profiler::ProfileOperator::Attributes());
+ }
+ opr->opr_profile.reset(new profiler::ProfileOperator(opr->opr_name, attrs.release()));
+ opr->opr_profile->start(exec_ctx.dev_type, exec_ctx.dev_id);
}
#endif
if (exec_ctx.dev_mask() == gpu::kDevMask) {
@@ -171,7 +167,7 @@ class NaiveEngine final : public Engine {
<< "NaiveEngine only support synchronize Push so far";
#if MXNET_USE_PROFILER
if (profiling) {
- SetOprEnd(opr->opr_stat);
+ opr->opr_profile->stop();
}
#endif
}
diff --git a/src/engine/profiler.cc b/src/engine/profiler.cc
deleted file mode 100644
index 13f8cca..0000000
--- a/src/engine/profiler.cc
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * 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) 2015 by Contributors
- * \file profiler.cc
- * \brief implements profiler
- */
-#include <dmlc/base.h>
-#include <dmlc/logging.h>
-#include <dmlc/omp.h>
-#include <mxnet/base.h>
-#include <fstream>
-#include <thread>
-#include "./profiler.h"
-
-#if MXNET_USE_CUDA
-#include "../common/cuda_utils.h"
-#endif
-
-#if defined(_MSC_VER) && _MSC_VER <= 1800
-#include <Windows.h>
-#endif
-
-namespace mxnet {
-namespace engine {
-
-Profiler::Profiler()
- : state_(kNotRunning), enable_output_(false), filename_("profile.json") {
- this->init_time_ = NowInUsec();
-
- this->cpu_num_ = std::thread::hardware_concurrency();
-#if MXNET_USE_CUDA
- int kMaxNumGpus = 32;
- this->gpu_num_ = kMaxNumGpus;
-#else
- this->gpu_num_ = 0;
-#endif
-
- this->profile_stat = new DevStat[cpu_num_ + gpu_num_ + 1];
- for (unsigned int i = 0; i < cpu_num_; ++i) {
- profile_stat[i].dev_name_ = "cpu/" + std::to_string(i);
- }
- for (unsigned int i = 0; i < gpu_num_; ++i) {
- profile_stat[cpu_num_ + i].dev_name_ = "gpu/" + std::to_string(i);
- }
- profile_stat[cpu_num_ + gpu_num_].dev_name_ = "cpu pinned/";
-
- mode_ = (ProfilerMode)dmlc::GetEnv("MXNET_PROFILER_MODE", static_cast<int>(kOnlySymbolic));
- if (dmlc::GetEnv("MXNET_PROFILER_AUTOSTART", 0)) {
- this->state_ = ProfilerState::kRunning;
- this->enable_output_ = true;
- }
-}
-
-Profiler* Profiler::Get() {
-#if MXNET_USE_PROFILER
- static Profiler inst;
- return &inst;
-#else
- return nullptr;
-#endif
-}
-
-void Profiler::SetState(ProfilerState state) {
- std::lock_guard<std::mutex> lock{this->m_};
- this->state_ = state;
- // once running, output will be enabled.
- if (state == kRunning)
- this->enable_output_ = true;
-}
-
-void Profiler::SetConfig(ProfilerMode mode, std::string output_filename) {
- std::lock_guard<std::mutex> lock{this->m_};
- this->mode_ = mode;
- this->filename_ = output_filename;
-}
-
-OprExecStat *Profiler::AddOprStat(int dev_type, uint32_t dev_id) {
- std::unique_ptr<OprExecStat> opr_stat(new OprExecStat);
- opr_stat->dev_type = dev_type;
- opr_stat->dev_id = dev_id;
- opr_stat->opr_name[sizeof(opr_stat->opr_name)-1] = '\0';
-
- int idx;
- switch (dev_type) {
- case Context::kCPU:
- idx = dev_id;
- break;
- case Context::kGPU:
- idx = cpu_num_ + dev_id;
- break;
- case Context::kCPUPinned:
- idx = cpu_num_ + gpu_num_;
- break;
- default:
- LOG(FATAL) << "Unknown dev_type: " << dev_type;
- return NULL;
- }
-
- DevStat& dev_stat = profile_stat[idx];
- dev_stat.opr_exec_stats_->enqueue(opr_stat.get());
- return opr_stat.release();
-}
-
-void Profiler::EmitPid(std::ostream *os, const std::string& name, uint32_t pid) {
- (*os) << " {\n"
- << " \"ph\": \"M\",\n"
- << " \"args\": {\n"
- << " \"name\": \"" << name << "\"\n"
- << " },\n"
- << " \"pid\": " << pid << ",\n"
- << " \"name\": \"process_name\"\n"
- << " }";
-}
-
-void Profiler::EmitEvent(std::ostream *os, const std::string& name,
- const std::string& category, const std::string& ph,
- uint64_t ts, uint32_t pid, uint32_t tid) {
- (*os) << " {\n"
- << " \"name\": \"" << name << "\",\n"
- << " \"cat\": " << "\"" << category << "\",\n"
- << " \"ph\": \""<< ph << "\",\n"
- << " \"ts\": " << ts << ",\n"
- << " \"pid\": " << pid << ",\n"
- << " \"tid\": " << tid << "\n"
- << " }";
-}
-
-
-void Profiler::DumpProfile() {
- SetState(kNotRunning);
-
- std::lock_guard<std::mutex> lock{this->m_};
- std::ofstream file;
- file.open(filename_);
-
- file << "{" << std::endl;
- file << " \"traceEvents\": [" << std::endl;
-
- uint32_t dev_num = cpu_num_ + gpu_num_ + 1;
-
- for (uint32_t i = 0; i < dev_num; ++i) {
- const DevStat &d = profile_stat[i];
- this->EmitPid(&file, d.dev_name_, i);
- file << ",\n";
- }
-
- bool first_flag = true;
- for (uint32_t i = 0; i < dev_num; ++i) {
- DevStat &d = profile_stat[i];
- OprExecStat *_opr_stat;
- while (d.opr_exec_stats_->try_dequeue(_opr_stat)) {
- CHECK_NOTNULL(_opr_stat);
- std::unique_ptr<OprExecStat> opr_stat(_opr_stat); // manage lifecycle
- uint32_t pid = i;
- uint32_t tid = opr_stat->thread_id;
-
- if (first_flag) {
- first_flag = false;
- } else {
- file << ",";
- }
- file << std::endl;
- this->EmitEvent(&file, opr_stat->opr_name, "category", "B",
- opr_stat->opr_start_rel_micros, pid, tid);
- file << ",\n";
- this->EmitEvent(&file, opr_stat->opr_name, "category", "E",
- opr_stat->opr_end_rel_micros, pid, tid);
- }
- }
-
- file << "\n" << std::endl;
- file << " ]," << std::endl;
- file << " \"displayTimeUnit\": \"ms\"" << std::endl;
- file << "}" << std::endl;
-
- enable_output_ = false;
-}
-
-
-inline uint64_t NowInUsec() {
-#if defined(_MSC_VER) && _MSC_VER <= 1800
- LARGE_INTEGER frequency, counter;
- QueryPerformanceFrequency(&frequency);
- QueryPerformanceCounter(&counter);
- return counter.QuadPart * 1000000 / frequency.QuadPart;
-#else
- return std::chrono::duration_cast<std::chrono::microseconds>(
- std::chrono::high_resolution_clock::now().time_since_epoch()).count();
-#endif
-}
-
-void SetOprStart(OprExecStat* opr_stat) {
- if (!opr_stat) {
- LOG(WARNING) << "SetOpStart: nullptr";
- return;
- }
- opr_stat->opr_start_rel_micros = NowInUsec() - Profiler::Get()->GetInitTime();
-}
-
-void SetOprEnd(OprExecStat* opr_stat) {
- if (!opr_stat) {
- LOG(WARNING) << "SetOpEnd: nullptr";
- return;
- }
- opr_stat->opr_end_rel_micros = NowInUsec() - Profiler::Get()->GetInitTime();
-}
-
-} // namespace engine
-} // namespace mxnet
diff --git a/src/engine/profiler.h b/src/engine/profiler.h
deleted file mode 100644
index ebd9420..0000000
--- a/src/engine/profiler.h
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * 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) 2015 by Contributors
- * \file profiler.h
- * \brief implements profiler
- */
-#ifndef MXNET_ENGINE_PROFILER_H_
-#define MXNET_ENGINE_PROFILER_H_
-
-#include <dmlc/concurrentqueue.h>
-#include <vector>
-#include <string>
-#include <mutex>
-#include <memory>
-
-namespace mxnet {
-namespace engine {
-
-/*!
- * \brief Operation execution statistics
- */
-struct OprExecStat {
- /*! \brief operation name */
- char opr_name[32];
- /*!
- * \brief operation execution start relative timestamp
- * time unit is microsecond (10^-6 s)
- */
- uint64_t opr_start_rel_micros;
- /*!
- * \brief operation execution end relative timestamp
- * time unit is microsecond (10^-6 s)
- */
- uint64_t opr_end_rel_micros;
- /*! \brief id of thread which operation run on */
- uint32_t thread_id;
- /*!
- * \brief device type
- * CPU: 1, GPU: 2, CPUPinned: 3
- */
- uint32_t dev_type;
- /*! \brief device id */
- uint32_t dev_id;
-};
-
-/*!
- * \brief Device statistics
- */
-struct DevStat {
- /*! \brief device name */
- std::string dev_name_;
- /*! \brief operation execution statistics on this device */
- std::shared_ptr<dmlc::moodycamel::ConcurrentQueue<OprExecStat *>> opr_exec_stats_ =
- std::make_shared<dmlc::moodycamel::ConcurrentQueue<OprExecStat *>>();
-
- /*!
- * \brief Destructor, clean up allocated objects
- * TODO(cjolivier01) Investigate queueing unique_ptr<>'s if it won't hurt performance
- */
- ~DevStat() {
- std::shared_ptr<dmlc::moodycamel::ConcurrentQueue<OprExecStat *>> es = opr_exec_stats_;
- if (es) {
- OprExecStat *opr_stat = nullptr;
- while (es->try_dequeue(opr_stat)) {
- delete opr_stat;
- }
- }
- }
-};
-
-
-/*!
- * \brief profiler that records the operation execution information
- * and saves the profile statistics.
- */
-class Profiler {
- public:
- enum ProfilerMode {
- kOnlySymbolic = 0,
- kAllOperator = 1
- };
- enum ProfilerState {
- kNotRunning = 0,
- kRunning = 1
- };
- /*! \brief set state of profiler */
- void SetState(ProfilerState state);
- /*! \return state of profiler */
- inline ProfilerState GetState() const {
- return this->state_;
- }
- /*! \brief set configure of profiler */
- void SetConfig(ProfilerMode mode, std::string output_filename);
- /*! \return mode of profiler */
- inline ProfilerMode GetMode() const {
- return this->mode_;
- }
- /*! \return whether the profiler is enabled to output */
- inline bool IsEnableOutput() const {
- return this->enable_output_;
- }
- /*! \brief dump the profile file */
- void DumpProfile();
- /*! \return the profiler init time, time unit is microsecond (10^-6) s */
- inline uint64_t GetInitTime() const {
- return init_time_;
- }
- /*! \brief add one operation execution record in
- * corresponding device statistics */
- OprExecStat* AddOprStat(int dev_type, uint32_t dev_id);
- /*! \return Profiler singleton */
- static Profiler* Get();
-
- protected:
- /*! \brief make constructor protected. */
- Profiler();
-
- private:
- /*! \brief generate device information following chrome profile file format */
- void EmitPid(std::ostream *os, const std::string& name, uint32_t pid);
- /*! \brief generate event information following chrome profile file format */
- void EmitEvent(std::ostream *os, const std::string& name,
- const std::string& category, const std::string& ph,
- uint64_t ts, uint32_t pid, uint32_t tid);
- /*! \brief Profiler instance */
- static Profiler* instance_;
- /*! \brief internal mutex of the profiler */
- std::mutex m_;
- /*! \brief indicate whether the profiler is running */
- ProfilerState state_;
- /*! \brief once running, enable profiler to output */
- bool enable_output_;
- /*! \brief indicate what operator the profiler will record */
- ProfilerMode mode_;
- /*! \brief filename to output profile file */
- std::string filename_;
- /*! \brief profile statistics consist of multiple device statistics */
- DevStat* profile_stat;
- /*! \brief cpu number on the machine */
- unsigned int cpu_num_;
- /*! \brief gpu number on the machine */
- unsigned int gpu_num_;
- /*! \brief the profiler init time */
- uint64_t init_time_;
-};
-
-/*! \return current clock time, time unit is microsecond (10^-6 s) */
-inline uint64_t NowInUsec();
-/*! \brief set operation execution start timestamp */
-void SetOprStart(OprExecStat* opr_stat);
-/*! \brief set operation execution end timestamp */
-void SetOprEnd(OprExecStat* opr_stat);
-
-} // namespace engine
-} // namespace mxnet
-#endif // MXNET_ENGINE_PROFILER_H_
diff --git a/src/engine/thread_pool.h b/src/engine/thread_pool.h
index d40c51a..a48ac1b 100644
--- a/src/engine/thread_pool.h
+++ b/src/engine/thread_pool.h
@@ -24,6 +24,7 @@
#define MXNET_ENGINE_THREAD_POOL_H_
#include <dmlc/base.h>
+#include <dmlc/thread_group.h>
#include <cstddef>
#include <vector>
#include <list>
@@ -39,40 +40,17 @@ namespace engine {
*/
class ThreadPool {
public:
- /*! \brief Simple manually-signalled event gate which remains open */
- class SimpleEvent {
- public:
- SimpleEvent()
- : signaled_(false) {}
- void wait() {
- std::unique_lock<std::mutex> lock(mutex_);
- if (!signaled_) {
- condition_variable_.wait(lock);
- }
- }
- void signal() {
- signaled_ = true;
- std::unique_lock<std::mutex> lk(mutex_);
- condition_variable_.notify_all();
+ /*! \brief Signal event upon destruction, even for exceptions (RAII) */
+ struct SetReadyOnDestroy {
+ explicit inline SetReadyOnDestroy(const std::shared_ptr<dmlc::ManualEvent>& event)
+ : event_(event) {
}
-
- /*! \brief Signal event upon destruction, even for exceptions (RAII) */
- struct SetReadyOnDestroy {
- explicit inline SetReadyOnDestroy(const std::shared_ptr<SimpleEvent>& event)
- : event_(event) {
+ inline ~SetReadyOnDestroy() {
+ if (event_) {
+ event_->signal();
}
- inline ~SetReadyOnDestroy() {
- if (event_) {
- event_->signal();
- }
- }
- std::shared_ptr<SimpleEvent> event_;
- };
-
- private:
- std::mutex mutex_;
- std::condition_variable condition_variable_;
- std::atomic<bool> signaled_;
+ }
+ std::shared_ptr<dmlc::ManualEvent> event_;
};
/*!
@@ -88,14 +66,14 @@ class ThreadPool {
}
}
explicit ThreadPool(size_t size,
- std::function<void(const std::shared_ptr<SimpleEvent> ready)> func,
+ std::function<void(std::shared_ptr<dmlc::ManualEvent> ready)> func,
const bool wait)
: worker_threads_(size) {
CHECK_GT(size, 0);
- ready_events_.reserve(size);
for (auto& i : worker_threads_) {
- ready_events_.emplace_back(std::make_shared<SimpleEvent>());
- i = std::thread(func, ready_events_.back());
+ std::shared_ptr<dmlc::ManualEvent> ptr = std::make_shared<dmlc::ManualEvent>();
+ ready_events_.emplace_back(ptr);
+ i = std::thread(func, ptr);
}
if (wait) {
WaitForReady();
@@ -112,8 +90,8 @@ class ThreadPool {
* \brief Wait for all started threads to signal that they're ready
*/
void WaitForReady() {
- for (const std::shared_ptr<SimpleEvent>& event : ready_events_) {
- event->wait();
+ for (const std::shared_ptr<dmlc::ManualEvent>& ptr : ready_events_) {
+ ptr->wait();
}
}
@@ -124,7 +102,7 @@ class ThreadPool {
/*!
* \brief Startup synchronization objects
*/
- std::vector<std::shared_ptr<SimpleEvent> > ready_events_;
+ std::list<std::shared_ptr<dmlc::ManualEvent>> ready_events_;
/*!
* \brief Disallow default construction.
*/
diff --git a/src/engine/threaded_engine.cc b/src/engine/threaded_engine.cc
index e166a6d..77b14b4 100644
--- a/src/engine/threaded_engine.cc
+++ b/src/engine/threaded_engine.cc
@@ -313,11 +313,9 @@ void ThreadedEngine::PushAsync(AsyncFn fn, Context exec_ctx,
ThreadedOpr *opr = NewOperator(std::move(fn), const_vars, mutable_vars, prop, opr_name, wait);
opr->temporary = true;
#if MXNET_USE_PROFILER
- Profiler *profiler = Profiler::Get();
- bool profiling = (profiler->GetState() == Profiler::kRunning) &&
- (profiler->GetMode() == Profiler::kAllOperator);
+ const bool profiling = profiler_->IsProfiling(profiler::Profiler::kImperative);
#else
- bool profiling = false;
+ const bool profiling = false;
#endif
Push(opr, exec_ctx, priority, profiling);
}
@@ -447,7 +445,7 @@ inline void ThreadedEngine::OnComplete(ThreadedOpr* threaded_opr) {
finished_cv_.notify_all();
}
- // delte operator if it is temperory
+ // delete operator if it is temperory
if (is_temporary_opr) {
ThreadedOpr::Delete(threaded_opr);
}
@@ -469,7 +467,7 @@ void ThreadedEngine::OnCompleteStatic(
#if MXNET_USE_PROFILER
if (opr_block->profiling && threaded_opr->opr_name) {
// record operator end timestamp
- SetOprEnd(opr_block->opr_stat);
+ opr_block->opr_profile->stop();
}
#endif
static_cast<ThreadedEngine*>(engine)->OnComplete(threaded_opr);
diff --git a/src/engine/threaded_engine.h b/src/engine/threaded_engine.h
index f647074..1b9453f 100644
--- a/src/engine/threaded_engine.h
+++ b/src/engine/threaded_engine.h
@@ -39,7 +39,7 @@
#include <string>
#include <thread>
#include "./engine_impl.h"
-#include "./profiler.h"
+#include "../profiler/profiler.h"
#include "./openmp.h"
#include "../common/object_pool.h"
@@ -77,7 +77,7 @@ struct OprBlock : public common::ObjectPoolAllocatable<OprBlock> {
/*! \brief indicate whether to profile this operator */
bool profiling{false};
/*! \brief operator execution statistics */
- OprExecStat *opr_stat;
+ std::unique_ptr<profiler::ProfileOperator> opr_profile;
// define possible debug information
DEFINE_ENGINE_DEBUG_INFO(OprBlock);
/*!
@@ -305,7 +305,10 @@ class ThreadedEngine : public Engine {
objpool_varblk_ref_ = common::ObjectPool<VersionedVarBlock>::_GetSharedRef();
objpool_var_ref_ = common::ObjectPool<ThreadedVar>::_GetSharedRef();
- /*! \brief Set default OMP threads per kernel worker to default */
+#ifdef MXNET_USE_PROFILER
+ // Get a ref to the profiler so that it doesn't get killed before us
+ profiler::Profiler::Get(&profiler_);
+#endif // MXNET_USE_PROFILER
}
~ThreadedEngine() {
{
@@ -335,15 +338,14 @@ class ThreadedEngine : public Engine {
ThreadedOpr* threaded_opr = opr_block->opr;
#if MXNET_USE_PROFILER
if (opr_block->profiling && threaded_opr->opr_name) {
+ std::unique_ptr<profiler::ProfileOperator::Attributes> attrs;
+ if (profiler_->AggregateEnabled()) {
+ attrs.reset(new profiler::ProfileOperator::Attributes());
+ }
const Context& ctx = opr_block->ctx;
- opr_block->opr_stat =
- Profiler::Get()->AddOprStat(ctx.dev_type, ctx.dev_id);
- uint64_t id = std::hash<std::thread::id>()(std::this_thread::get_id());
- opr_block->opr_stat->thread_id = id;
- strncpy(opr_block->opr_stat->opr_name, threaded_opr->opr_name,
- sizeof(opr_block->opr_stat->opr_name) - 1);
- // record operator start timestamp
- SetOprStart(opr_block->opr_stat);
+ opr_block->opr_profile.reset(new profiler::ProfileOperator(threaded_opr->opr_name,
+ attrs.release()));
+ opr_block->opr_profile->start(ctx.dev_type, ctx.dev_id);
}
#endif
CallbackOnComplete callback =
@@ -536,8 +538,14 @@ class ThreadedEngine : public Engine {
std::shared_ptr<common::ObjectPool<VersionedVarBlock> > objpool_varblk_ref_;
std::shared_ptr<common::ObjectPool<ThreadedVar> > objpool_var_ref_;
+#if MXNET_USE_PROFILER
+ /*! \brief Hold a ref count ot the profiler */
+ std::shared_ptr<profiler::Profiler> profiler_;
+#endif // MXNET_USE_PROFILER
+
/*!
* \brief Disallow copy construction and assignment.
+ * \note This must be last
*/
DISALLOW_COPY_AND_ASSIGN(ThreadedEngine);
}; // class ThreadedEngine
diff --git a/src/engine/threaded_engine_perdevice.cc b/src/engine/threaded_engine_perdevice.cc
index 638062f..3b4c4fc 100644
--- a/src/engine/threaded_engine_perdevice.cc
+++ b/src/engine/threaded_engine_perdevice.cc
@@ -27,6 +27,7 @@
#include <dmlc/logging.h>
#include <dmlc/parameter.h>
#include <dmlc/concurrency.h>
+#include <dmlc/thread_group.h>
#include "./threaded_engine.h"
#include "./thread_pool.h"
#include "../common/lazy_alloc_array.h"
@@ -96,7 +97,7 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
cpu_priority_worker_.reset(new ThreadWorkerBlock<kPriorityQueue>());
cpu_priority_worker_->pool.reset(new ThreadPool(
cpu_priority_nthreads,
- [this](std::shared_ptr<ThreadPool::SimpleEvent> ready_event) {
+ [this](std::shared_ptr<dmlc::ManualEvent> ready_event) {
this->CPUWorker(Context(), cpu_priority_worker_.get(), ready_event);
}, true));
// GPU tasks will be created lazily
@@ -118,11 +119,13 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
if (opr_block->opr->prop == FnProperty::kCPUPrioritized) {
cpu_priority_worker_->task_queue.Push(opr_block, opr_block->priority);
} else {
- const size_t nthreads = cpu_worker_nthreads_;
- auto ptr = cpu_normal_workers_.Get(ctx.dev_id, [this, ctx, nthreads]() {
- auto blk = new ThreadWorkerBlock<kWorkerQueue>();
- blk->pool.reset(new ThreadPool(nthreads,
- [this, ctx, blk](std::shared_ptr<ThreadPool::SimpleEvent> ready_event) {
+ int dev_id = ctx.dev_id;
+ int nthread = cpu_worker_nthreads_;
+ auto ptr =
+ cpu_normal_workers_.Get(dev_id, [this, ctx, nthread]() {
+ auto blk = new ThreadWorkerBlock<kWorkerQueue>();
+ blk->pool.reset(new ThreadPool(nthread,
+ [this, ctx, blk](std::shared_ptr<dmlc::ManualEvent> ready_event) {
this->CPUWorker(ctx, blk, ready_event);
}, true));
return blk;
@@ -147,14 +150,14 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
// Signify to kernel that GPU is being used, so reserve cores as necessary
OpenMP::Get()->set_reserve_cores(GetReserveCoreCount(true));
auto blk = new ThreadWorkerBlock<kCopyQueue>();
- blk->pool.reset(new ThreadPool(
- nthread,
- [this, ctx, is_copy, blk]
- (std::shared_ptr<ThreadPool::SimpleEvent> ready_event) {
- this->GPUWorker(ctx, is_copy, blk, ready_event);
- }, true));
- return blk;
- });
+ blk->pool.reset(new ThreadPool(
+ nthread,
+ [this, ctx, is_copy, blk]
+ (std::shared_ptr<dmlc::ManualEvent> ready_event) {
+ this->GPUWorker(ctx, is_copy, blk, ready_event);
+ }, true));
+ return blk;
+ });
if (ptr) {
if (opr_block->opr->prop == FnProperty::kDeleteVar) {
ptr->task_queue.PushFront(opr_block, opr_block->priority);
@@ -170,7 +173,7 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
blk->pool.reset(new ThreadPool(
nthread,
[this, ctx, is_copy, blk]
- (std::shared_ptr<ThreadPool::SimpleEvent> ready_event) {
+ (std::shared_ptr<dmlc::ManualEvent> ready_event) {
this->GPUWorker(ctx, is_copy, blk, ready_event);
}, true));
return blk;
@@ -225,13 +228,13 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
inline void GPUWorker(Context ctx,
bool is_copy_worker,
ThreadWorkerBlock<type> *block,
- const std::shared_ptr<ThreadPool::SimpleEvent>& ready_event) {
+ const std::shared_ptr<dmlc::ManualEvent>& ready_event) {
this->is_worker_ = true;
#if MXNET_USE_CUDA
CHECK(block != nullptr);
mshadow::Stream<gpu> *stream;
do {
- ThreadPool::SimpleEvent::SetReadyOnDestroy setReady(ready_event);
+ ThreadPool::SetReadyOnDestroy setReady(ready_event);
// allocate stream
mshadow::SetDevice<gpu>(ctx.dev_id);
if (is_copy_worker) {
@@ -260,7 +263,7 @@ class ThreadedEnginePerDevice : public ThreadedEngine {
template<dmlc::ConcurrentQueueType type>
inline void CPUWorker(Context ctx,
ThreadWorkerBlock<type> *block,
- std::shared_ptr<ThreadPool::SimpleEvent> ready_event) {
+ const std::shared_ptr<dmlc::ManualEvent>& ready_event) {
this->is_worker_ = true;
auto* task_queue = &(block->task_queue);
RunContext run_ctx{ctx, nullptr};
diff --git a/src/executor/graph_executor.cc b/src/executor/graph_executor.cc
index f685370..7d31a31 100644
--- a/src/executor/graph_executor.cc
+++ b/src/executor/graph_executor.cc
@@ -30,7 +30,7 @@
#include "./exec_pass.h"
#include "./graph_executor.h"
-#include "../engine/profiler.h"
+#include "../profiler/profiler.h"
#include "../common/utils.h"
namespace mxnet {
@@ -1484,7 +1484,7 @@ void GraphExecutor::RunOps(bool is_train, size_t topo_start, size_t topo_end) {
// Check segments first
if (monitor_callback_ == nullptr && seg_op.opr != nullptr && seg_op.topo_end <= topo_end) {
#if MXNET_USE_PROFILER
- bool profiling = engine::Profiler::Get()->GetState() == engine::Profiler::kRunning;
+ bool profiling = profiler::Profiler::Get()->GetState() == profiler::Profiler::kRunning;
#else
bool profiling = false;
#endif
@@ -1505,7 +1505,7 @@ void GraphExecutor::RunOps(bool is_train, size_t topo_start, size_t topo_end) {
CopyFromTo(opnode.exec->in_array[0], &(opnode.exec->out_array[0]));
} else if (opnode.cached_opr != nullptr) {
#if MXNET_USE_PROFILER
- bool profiling = engine::Profiler::Get()->GetState() == engine::Profiler::kRunning;
+ bool profiling = profiler::Profiler::Get()->GetState() == profiler::Profiler::kRunning;
#else
bool profiling = false;
#endif
diff --git a/src/initialize.cc b/src/initialize.cc
index 2d077f4..69d408d 100644
--- a/src/initialize.cc
+++ b/src/initialize.cc
@@ -25,7 +25,6 @@
#include <signal.h>
#include <dmlc/logging.h>
#include <mxnet/engine.h>
-#include "engine/profiler.h"
namespace mxnet {
#if MXNET_USE_SIGNAL_HANDLER && DMLC_LOG_STACK_TRACE
@@ -43,17 +42,6 @@ class LibraryInitializer {
#if MXNET_USE_SIGNAL_HANDLER && DMLC_LOG_STACK_TRACE
signal(SIGSEGV, SegfaultLogger);
#endif
-#if MXNET_USE_PROFILER
- // ensure profiler's constructor are called before atexit.
- engine::Profiler::Get();
- // DumpProfile will be called before engine's and profiler's destructor.
- std::atexit([](){
- engine::Profiler* profiler = engine::Profiler::Get();
- if (profiler->IsEnableOutput()) {
- profiler->DumpProfile();
- }
- });
-#endif
}
static LibraryInitializer* Get();
diff --git a/src/ndarray/ndarray_function.cc b/src/ndarray/ndarray_function.cc
index 552555a..927c906 100644
--- a/src/ndarray/ndarray_function.cc
+++ b/src/ndarray/ndarray_function.cc
@@ -26,6 +26,7 @@
#include "./ndarray_function.h"
#include "./ndarray_function-inl.h"
#include "../common/utils.h"
+#include "../operator/mxnet_op.h"
namespace mxnet {
namespace ndarray {
@@ -33,17 +34,27 @@ template<>
void Copy<cpu, cpu>(const TBlob &from, TBlob *to,
Context from_ctx, Context to_ctx,
RunContext ctx) {
+ using namespace mxnet::op;
MSHADOW_TYPE_SWITCH(to->type_flag_, DType, {
if (to->type_flag_ == from.type_flag_) {
- mshadow::Copy(to->FlatTo1D<cpu, DType>(),
- from.FlatTo1D<cpu, DType>());
+ TBlob dest = to->FlatTo1D<cpu, DType>();
+ TBlob src = from.FlatTo1D<cpu, DType>();
+ const size_t size = src.Size();
+ if (dest.CheckContiguous() && src.CheckContiguous() && size >= 20000 /* non-trivial size */) {
+ CHECK_EQ(dest.shape_, src.shape_)
+ << "Copy:shape mismatch:" << dest.shape_ << " vs " << src.shape_;
+ mxnet_op::Kernel<mxnet_op::op_with_req<mshadow_op::identity, kWriteTo>, cpu>::Launch(
+ ctx.get_stream<cpu>(), src.Size(), dest.dptr<DType>(), src.dptr<DType>());
+ } else {
+ mshadow::Copy(to->FlatTo1D<cpu, DType>(), from.FlatTo1D<cpu, DType>());
+ }
} else {
MSHADOW_TYPE_SWITCH(from.type_flag_, SrcDType, {
to->FlatTo1D<cpu, DType>() =
- mshadow::expr::tcast<DType>(from.FlatTo1D<cpu, SrcDType>());
+ mshadow::expr::tcast<DType>(from.FlatTo1D<cpu, SrcDType>());
})
}
- })
+ });
}
template<typename DType, typename IType>
diff --git a/src/operator/nn/batch_norm-inl.h b/src/operator/nn/batch_norm-inl.h
index 27e0a84..8418ec3 100644
--- a/src/operator/nn/batch_norm-inl.h
+++ b/src/operator/nn/batch_norm-inl.h
@@ -295,6 +295,7 @@ class BNTensor3 {
, indexOfChannel_(static_cast<size_t>(indexOfChannel < 0
? (static_cast<int>(blob.shape_.ndim()) + indexOfChannel)
: indexOfChannel)) {
+ CHECK_EQ(blob.type_flag_, mshadow::DataType<DType>::kFlag);
shape_[OUTER] = 1;
for (size_t i = 0; i < indexOfChannel_; ++i) {
shape_[OUTER] *= blob.shape_[i];
diff --git a/src/operator/nn/batch_norm.cu b/src/operator/nn/batch_norm.cu
index 80c1597..b8657fc 100644
--- a/src/operator/nn/batch_norm.cu
+++ b/src/operator/nn/batch_norm.cu
@@ -40,6 +40,7 @@
#endif
#include "../../common/cuda_utils.h"
+#include "../../../include/mxnet/tensor_blob.h"
using namespace mxnet;
@@ -453,6 +454,7 @@ struct DeviceTensor {
template<typename DType, int Dim>
static DeviceTensor<DType, Dim> devicetensor(const TBlob &blob) {
+ CHECK_EQ(blob.type_flag_, mshadow::DataType<DType>::kFlag);
DType *data = blob.dptr<DType>();
const int inDim = blob.shape_.ndim();
if (inDim == Dim) {
@@ -544,6 +546,8 @@ static void BatchNormalizationBackward(mshadow::Stream<gpu> *s,
batchnorm::BNTensor3<DType>gradInput = batchnorm::BNTensor3<DType>(
in_grad[batchnorm::kData], param.axis);
+ CHECK_EQ(gradOutput.Size(), gradInput.Size());
+
CUDATensors<DeviceTensor1> tensors;
tensors.gradWeight = devicetensor<AccReal, 1>(in_grad[batchnorm::kGamma]);
diff --git a/src/operator/tensor/init_op.h b/src/operator/tensor/init_op.h
index c6235f4..0b9e6a2 100644
--- a/src/operator/tensor/init_op.h
+++ b/src/operator/tensor/init_op.h
@@ -196,7 +196,7 @@ void Fill(mshadow::Stream<xpu> *s, const TBlob& b, const OpReqType req, ValueTyp
const size_t size = b.Size();
if (val == 0) {
if (req != kAddTo) {
- if (b.dev_mask() == cpu::kDevMask) {
+ if (b.dev_mask() == cpu::kDevMask && size < 50000) {
MSHADOW_TYPE_SWITCH(b.type_flag_, DType, {
memset(b.dptr_, 0, size * sizeof(DType));
});
diff --git a/src/profiler/aggregate_stats.cc b/src/profiler/aggregate_stats.cc
new file mode 100644
index 0000000..66f833e
--- /dev/null
+++ b/src/profiler/aggregate_stats.cc
@@ -0,0 +1,129 @@
+/*
+ * 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) 2015 by Contributors
+ * \file profiler.cc
+ * \brief implements profiler
+ */
+#include <dmlc/base.h>
+#include <dmlc/logging.h>
+#include <mxnet/base.h>
+#include <fstream>
+#include <thread>
+#include <iomanip>
+#include "./profiler.h"
+
+namespace mxnet {
+namespace profiler {
+
+template<typename DType>
+inline float MicroToMilli(const DType micro) {
+ return static_cast<float>(static_cast<double>(micro) / 1000);
+}
+
+void AggregateStats::OnProfileStat(const ProfileStat& stat) {
+ std::unique_lock<std::mutex> lk(m_);
+ stat.SaveAggregate(&stats_[stat.categories_.c_str()][stat.name_.c_str()]);
+}
+
+void AggregateStats::Dump(std::ostream& os, bool clear) {
+ std::ios state(nullptr);
+ state.copyfmt(os);
+ os << std::endl
+ << "Profile Statistics." << std::endl
+ << "\tNote that counter items are counter values and not time units."
+ << std::endl;
+ std::unique_lock<std::mutex> lk(m_);
+ for (auto type_iter = stats_.begin(), type_e_iter = stats_.end();
+ type_iter != type_e_iter; ++type_iter) {
+ const std::string& type = type_iter->first;
+ const std::unordered_map<std::string, StatData>& mm = type_iter->second;
+ if (!mm.empty()) {
+ os << type << std::endl << "=================" << std::endl;
+ os << std::setw(25) << std::left << "Name"
+ << std::setw(16) << std::right << "Total Count"
+ << " "
+ << std::setw(16) << std::right
+ << "Time (ms)"
+ << " "
+ << std::setw(16) << std::right
+ << "Min Time (ms)"
+ << " "
+ << std::setw(16) << std::right
+ << "Max Time (ms)"
+ << " "
+ << std::setw(16) << std::right
+ << "Avg Time (ms)"
+ << std::endl;
+ os << std::setw(25) << std::left << "----"
+ << std::setw(16) << std::right << "-----------"
+ << " "
+ << std::setw(16) << std::right
+ << "---------"
+ << " "
+ << std::setw(16) << std::right
+ << "-------------"
+ << " "
+ << std::setw(16) << std::right
+ << "-------------"
+ << " "
+ << std::setw(16) << std::right
+ << "-------------"
+ << std::endl;
+ for (auto iter = mm.begin(), e_iter = mm.end(); iter != e_iter; ++iter) {
+ const StatData &data = iter->second;
+ if (data.type_ == StatData::kDuration || data.type_ == StatData::kCounter) {
+ const std::string &name = iter->first;
+ os << std::setw(25) << std::left << name
+ << std::setw(16) << std::right << data.total_count_;
+ os << " "
+ << std::fixed << std::setw(16) << std::setprecision(4) << std::right
+ << MicroToMilli(data.total_aggregate_)
+ << " "
+ << std::fixed << std::setw(16) << std::setprecision(4) << std::right
+ << MicroToMilli(data.min_aggregate_)
+ << " "
+ << std::fixed << std::setw(16) << std::setprecision(4) << std::right
+ << MicroToMilli(data.max_aggregate_);
+ if (data.type_ == StatData::kCounter) {
+ os << " "
+ << std::fixed << std::setw(16) << std::setprecision(4) << std::right
+ << (MicroToMilli(data.max_aggregate_ - data.min_aggregate_) / 2);
+ } else {
+ os << " "
+ << std::fixed << std::setw(16) << std::setprecision(4) << std::right
+ << (MicroToMilli(static_cast<double>(data.total_aggregate_)
+ / data.total_count_));
+ }
+ os << std::endl;
+ }
+ }
+ os << std::endl;
+ }
+ }
+ os << std::flush;
+ os.copyfmt(state);
+ if (clear) {
+ stats_.clear();
+ }
+}
+
+} // namespace profiler
+} // namespace mxnet
diff --git a/src/profiler/aggregate_stats.h b/src/profiler/aggregate_stats.h
new file mode 100644
index 0000000..fad8a31
--- /dev/null
+++ b/src/profiler/aggregate_stats.h
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+
+#ifndef MXNET_PROFILER_AGGREGATE_STATS_H_
+#define MXNET_PROFILER_AGGREGATE_STATS_H_
+
+#include <string>
+#include <map>
+#include <cstdint>
+#include <ostream>
+#include <mutex>
+#include "./profiler.h"
+
+namespace mxnet {
+namespace profiler {
+
+struct ProfileStat;
+
+class AggregateStats {
+ public:
+ struct StatData {
+ /*!
+ * \brief Types that the console printer knows how to format
+ */
+ enum StatType {
+ kDuration = 1,
+ kCounter = 2,
+ kOther = 4
+ };
+
+ StatType type_ = kOther;
+ size_t total_count_ = 0;
+ uint64_t total_aggregate_ = 0;
+ uint64_t max_aggregate_ = 0;
+ uint64_t min_aggregate_ = INT_MAX;
+ };
+
+ /*!
+ * \brief Record aggregate profile data
+ * \param stat SIngle profile statistics to add to the accumulates statistics
+ */
+ void OnProfileStat(const ProfileStat& stat);
+ /*!
+ * \brief Print profliing statistics to console
+ * \param clear Delete all of the current statistics after printing
+ */
+ void Dump(std::ostream& os, bool clear);
+
+ private:
+ /*! \brief Should rarely collide, so most locks should occur only in user-space (futex) */
+ std::mutex m_;
+ /* !\brief Stat type -> State name -> Stats */
+ std::map<std::string, std::unordered_map<std::string, StatData>> stats_;
+};
+
+} // namespace profiler
+} // namespace mxnet
+#endif // MXNET_PROFILER_AGGREGATE_STATS_H_
diff --git a/src/profiler/profiler.cc b/src/profiler/profiler.cc
new file mode 100644
index 0000000..82d1744
--- /dev/null
+++ b/src/profiler/profiler.cc
@@ -0,0 +1,314 @@
+/*
+ * 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) 2015 by Contributors
+ * \file profiler.cc
+ * \brief implements profiler
+ */
+#include <dmlc/base.h>
+#include <dmlc/logging.h>
+#include <dmlc/omp.h>
+#include <mxnet/base.h>
+#include <fstream>
+#include <thread>
+#include "./profiler.h"
+
+#if MXNET_USE_CUDA
+#include "../common/cuda_utils.h"
+#endif
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+#include <Windows.h>
+#endif
+
+namespace mxnet {
+namespace profiler {
+
+ProfileDomain ProfileOperator::domain_("operator");
+
+Profiler::Profiler()
+ : state_(kNotRunning)
+ , enable_output_(false)
+ , filename_("profile.json")
+ , profile_dump_count_(0) {
+ this->init_time_ = ProfileStat::NowInMicrosec();
+
+ this->cpu_num_ = 0;
+#ifdef _OPENMP
+ // OMP is going to know better than a generic stl, which might return 0
+ this->cpu_num_ = static_cast<unsigned int>(::omp_get_num_procs());
+#else
+ this->cpu_num_ = std::thread::hardware_concurrency();
+#endif
+ if (!this->cpu_num_) {
+ this->cpu_num_ = 64;
+ }
+#if MXNET_USE_CUDA
+ int kMaxNumGpus = 32;
+ this->gpu_num_ = kMaxNumGpus;
+#else
+ this->gpu_num_ = 0;
+#endif
+
+ this->profile_stat = new DeviceStats[cpu_num_ + gpu_num_ + 2];
+ for (unsigned int i = 0; i < cpu_num_; ++i) {
+ this->profile_stat[i].dev_name_ = "cpu/" + std::to_string(i);
+ }
+ for (unsigned int i = 0; i < gpu_num_; ++i) {
+ this->profile_stat[cpu_num_ + i].dev_name_ = "gpu/" + std::to_string(i);
+ }
+ this->profile_stat[cpu_num_ + gpu_num_].dev_name_ = "cpu pinned/";
+
+ this->profile_stat[cpu_num_ + gpu_num_ + 1].dev_name_ = "cpu shared/";
+
+ this->mode_ = dmlc::GetEnv("MXNET_PROFILER_MODE", this->mode_);
+ if (dmlc::GetEnv("MXNET_PROFILER_AUTOSTART", 0)) {
+ this->state_ = ProfilerState::kRunning;
+ this->enable_output_ = true;
+ // Since we want to avoid interfering with pure-VTune analylisys runs, for not set,
+ // vtune will be recording based upon whether "Start" or "STart Paused" was selected
+ vtune::vtune_resume();
+ }
+}
+
+Profiler::~Profiler() {
+ DumpProfile(true);
+ if (thread_group_) {
+ thread_group_->request_shutdown_all();
+ thread_group_->join_all();
+ thread_group_.reset();
+ }
+}
+
+Profiler* Profiler::Get(std::shared_ptr<Profiler> *sp) {
+#if MXNET_USE_PROFILER
+ static std::mutex mtx;
+ static std::shared_ptr<Profiler> prof = nullptr;
+ if (!prof) {
+ std::unique_lock<std::mutex> lk(mtx);
+ if (!prof) {
+ prof = std::make_shared<Profiler>();
+ }
+ }
+ if (sp) {
+ *sp = prof;
+ }
+ return prof.get();
+#else
+ return nullptr;
+#endif
+}
+
+void Profiler::SetState(ProfilerState state) {
+ std::lock_guard<std::recursive_mutex> lock{this->m_};
+ this->state_ = state;
+ // once running, output will be enabled.
+ if (state == kRunning) {
+ this->enable_output_ = true;
+ set_paused(false);
+ } else {
+ set_paused(true);
+ }
+}
+
+void Profiler::SetConfig(int mode,
+ std::string output_filename,
+ bool continuous_dump,
+ float dump_period,
+ bool aggregate_stats) {
+ CHECK(!continuous_dump || dump_period > 0);
+ std::lock_guard<std::recursive_mutex> lock{this->m_};
+ this->mode_ = mode;
+ this->filename_ = output_filename;
+ // Remove the output file to start
+ if (!this->filename_.empty()) {
+ ::unlink(this->filename_.c_str());
+ }
+ SetContinuousProfileDump(continuous_dump, dump_period);
+ // Adjust whether storing aggregate stats as necessary
+ if (aggregate_stats) {
+ if (!aggregate_stats_) {
+ aggregate_stats_ = std::make_shared<AggregateStats>();
+ }
+ } else if (aggregate_stats_) {
+ aggregate_stats_.reset();
+ }
+}
+
+/*
+ * Docs for tracing format:
+ * https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
+ */
+void Profiler::EmitPid(std::ostream *os, const std::string& name, size_t pid) {
+ (*os) << " {\n"
+ << " \"ph\": \"" << static_cast<char>(ProfileStat::kMetadata) << "\",\n"
+ << " \"args\": {\n"
+ << " \"name\": \"" << name << "\"\n"
+ << " },\n"
+ << " \"pid\": " << pid << ",\n"
+ << " \"name\": \"process_name\"\n"
+ << " }";
+}
+
+void Profiler::DumpProfile(bool peform_cleanup) {
+ std::lock_guard<std::recursive_mutex> lock{this->m_};
+ if (!IsEnableOutput()) {
+ return;
+ }
+ if (peform_cleanup) {
+ SetContinuousProfileDump(false, 1.0f);
+ }
+ std::ofstream file;
+ const bool first_pass = ++profile_dump_count_ == 1;
+ const bool last_pass = peform_cleanup || !continuous_dump_;
+ if (!first_pass && continuous_dump_) {
+ file.open(filename_, std::ios::app|std::ios::out);
+ } else {
+ file.open(filename_, std::ios::trunc|std::ios::out);
+ }
+ if (first_pass || !continuous_dump_) {
+ file << "{" << std::endl;
+ file << " \"traceEvents\": [" << std::endl;
+ }
+
+ const size_t dev_num = DeviceCount();
+
+ if (first_pass) {
+ for (uint32_t pid = 0; pid < dev_num; ++pid) {
+ if (pid) {
+ file << ",\n";
+ }
+ const DeviceStats &d = profile_stat[pid];
+ this->EmitPid(&file, d.dev_name_, pid);
+ process_ids_.emplace(pid);
+ }
+ }
+
+ // Hold ref in case SetConfig() resets aggregate_stats_
+ // If aggregate stats aren't enabled, this won't cause a locked instruction
+ std::shared_ptr<AggregateStats> ptr_aggregate_stats = aggregate_stats_.get()
+ ? aggregate_stats_ : nullptr;
+ bool first_flag = !first_pass && !num_records_emitted_;
+ for (uint32_t i = 0; i < dev_num; ++i) {
+ DeviceStats &d = profile_stat[i];
+ ProfileStat *_opr_stat;
+ while (d.opr_exec_stats_->try_dequeue(_opr_stat)) {
+ CHECK_NOTNULL(_opr_stat);
+ std::unique_ptr<ProfileStat> opr_stat(_opr_stat); // manage lifecycle
+ opr_stat->process_id_ = i; // lie and set process id to be the device number
+ if (first_flag) {
+ first_flag = false;
+ } else {
+ file << ",\n";
+ }
+ file << std::endl;
+ opr_stat->EmitEvents(&file);
+ ++num_records_emitted_;
+ if (ptr_aggregate_stats) {
+ ptr_aggregate_stats->OnProfileStat(*_opr_stat);
+ }
+ }
+ }
+
+ // Now do the non-device items
+ ProfileStat *_profile_stat;
+ while (general_stats_.opr_exec_stats_->try_dequeue(_profile_stat)) {
+ CHECK_NOTNULL(_profile_stat);
+
+ if (first_flag) {
+ first_flag = false;
+ } else {
+ file << ",";
+ }
+
+ std::unique_ptr<ProfileStat> profile_stat(_profile_stat); // manage lifecycle
+ CHECK_NE(profile_stat->categories_.c_str()[0], '\0') << "Category must be set";
+ // Currently, category_to_pid_ is only accessed here, so it is protected by this->m_ above
+ auto iter = category_to_pid_.find(profile_stat->categories_.c_str());
+ if (iter == category_to_pid_.end()) {
+ static std::hash<std::string> hash_fn;
+ const size_t this_pid = hash_fn(profile_stat->categories_.c_str());
+ iter = category_to_pid_.emplace(std::make_pair(profile_stat->categories_.c_str(),
+ this_pid)).first;
+ EmitPid(&file, profile_stat->categories_.c_str(), iter->second);
+ file << ",\n";
+ }
+ profile_stat->process_id_ = iter->second;
+ file << std::endl;
+ profile_stat->EmitEvents(&file);
+ ++num_records_emitted_;
+ if (ptr_aggregate_stats) {
+ ptr_aggregate_stats->OnProfileStat(*profile_stat);
+ }
+ }
+
+ if (last_pass) {
+ file << "\n" << std::endl;
+ file << " ]," << std::endl;
+ file << " \"displayTimeUnit\": \"ms\"" << std::endl;
+ file << "}" << std::endl;
+ }
+ enable_output_ = continuous_dump_ && !last_pass; // If we're appending, then continue.
+ // Otherwise, profiling stops.
+}
+
+static constexpr char TIMER_THREAD_NAME[] = "DumpProfileTimer";
+
+void Profiler::SetContinuousProfileDump(bool continuous_dump, float delay_in_seconds) {
+ std::lock_guard<std::recursive_mutex> lock{this->m_};
+ if (continuous_dump) {
+ this->continuous_dump_ = true; // Continuous doesn't make sense without append mode
+ DumpProfile(false);
+ std::shared_ptr<dmlc::ThreadGroup::Thread> old_thread =
+ thread_group_->thread_by_name(TIMER_THREAD_NAME);
+ if (old_thread && old_thread->is_shutdown_requested()) {
+ // This should never happen unless someone is doing something malicious
+ // At any rate, wait for its shutdown to complete
+ if (old_thread->joinable()) {
+ old_thread->join();
+ } else {
+ do {
+ std::this_thread::sleep_for(std::chrono::milliseconds(10));
+ } while (thread_group_->thread_by_name(TIMER_THREAD_NAME));
+ }
+ old_thread.reset();
+ }
+ if (!old_thread) {
+ dmlc::CreateTimer(
+ TIMER_THREAD_NAME,
+ std::chrono::milliseconds(static_cast<size_t>(delay_in_seconds * 1000.0f)),
+ thread_group_.get(),
+ [this]() -> int {
+ DumpProfile(false);
+ return 0;
+ });
+ }
+ } else {
+ std::shared_ptr<dmlc::ThreadGroup::Thread> old_thread =
+ thread_group_->thread_by_name(TIMER_THREAD_NAME);
+ if (old_thread) {
+ // Signal it to finish asynchronously
+ old_thread->request_shutdown();
+ }
+ }
+}
+
+} // namespace profiler
+} // namespace mxnet
diff --git a/src/profiler/profiler.h b/src/profiler/profiler.h
new file mode 100644
index 0000000..1224231
--- /dev/null
+++ b/src/profiler/profiler.h
@@ -0,0 +1,1238 @@
+/*
+ * 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) 2015 by Contributors
+ * \file profiler.h
+ * \brief implements profiler
+ */
+#ifndef MXNET_PROFILER_PROFILER_H_
+#define MXNET_PROFILER_PROFILER_H_
+
+#include <dmlc/concurrentqueue.h>
+#include <dmlc/thread_group.h>
+#include <vector>
+#include <string>
+#include <cstdint>
+#include <mutex>
+#include <memory>
+#include <array>
+#include "./vtune.h"
+#include "./aggregate_stats.h"
+
+#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <cstdint>
+#endif
+
+namespace mxnet {
+namespace profiler {
+
+#if defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__)
+inline size_t current_process_id() { return ::GetCurrentProcessId(); }
+#else
+inline size_t current_process_id() { return getpid(); }
+#endif
+
+/*!
+ * \brief Constant-sized character array class with simple string API to avoid allocations
+ * \tparam string_size Maximum size of the string (including zero-terminator)
+ */
+template<size_t string_size>
+struct static_string {
+ inline static_string() { string_[0] = '\0'; }
+ inline explicit static_string(const char *s) { set(s); }
+ inline const char *c_str() const { return &string_[0]; }
+ inline void set(const char *s) {
+ strncpy(&string_[0], s, string_size - 1);
+ string_[string_size - 1] = '\0';
+ }
+ inline void append(const char *s) {
+ const size_t l = strlen(&string_[0]);
+ if (l < string_size - 1) {
+ strncpy(&string_[0] + l, s, string_size - l - 1);
+ string_[string_size - 1] = '\0';
+ }
+ }
+ private:
+ /*! \brief The actual character array */
+ std::array<char, string_size> string_;
+};
+
+using profile_stat_string = static_string<128>;
+
+/*!
+ * \brief Base profile statistic structure
+ */
+struct ProfileStat {
+ /*!
+ * \brief Event type as used for chrome://tracing support
+ * \note Tracing formats: https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview NOLINT(*)
+ */
+ enum EventType {
+ kDurationBegin = 'B',
+ kDurationEnd = 'E',
+ kComplete = 'X',
+ kInstant = 'i',
+ kCounter = 'C',
+ kAsyncNestableStart = 'b',
+ kAsyncNestableInstant = 'n',
+ kAsyncNestableEnd = 'e',
+ kFlowStart = 's',
+ kFlowStep = 't',
+ kFlowEnd = 'f',
+ kSample = 'P',
+ kObjectCreated = 'N',
+ kObjectSnapshot = 'O',
+ kObjectDestroyed = 'D',
+ kMetadata = 'M',
+ kMemoryDumpGlobal = 'V',
+ kMemoryDumpProcess = 'v',
+ kMark = 'R',
+ kClockSync = 'c',
+ kContextEnter = '(',
+ kContextLeave = ')'
+ };
+
+ struct SubEvent {
+ /*! \brief whether this sub-event object is enabled */
+ bool enabled_ = false;
+ /*! \brief Type of the sub-event */
+ EventType event_type_;
+ /*! \brief Timestamp of sub-event */
+ uint64_t timestamp_;
+ };
+
+ /*! \brief operation name */
+ profile_stat_string name_;
+
+ /*! \brief operation categories (comma-delimited) */
+ profile_stat_string categories_;
+
+ /* !\brief Process id */
+ size_t process_id_ = current_process_id();
+
+ /*! \brief id of thread which operation run on */
+ std::thread::id thread_id_ = std::this_thread::get_id(); // Not yet seen a
+ // case where this isn't valid
+
+ /*! \brief Sub-events (ie begin, end, etc.) */
+ SubEvent items_[3]; // Don't use vector in order to avoid memory allocation
+
+ /*!
+ * \brief Get current tick count in microseconds
+ * \return Current arbitrary tick count in microseconds
+ */
+ static inline uint64_t NowInMicrosec() {
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+ LARGE_INTEGER frequency, counter;
+ QueryPerformanceFrequency(&frequency);
+ QueryPerformanceCounter(&counter);
+ return counter.QuadPart * 1000000 / frequency.QuadPart;
+#else
+ return std::chrono::duration_cast<std::chrono::microseconds>(
+ std::chrono::high_resolution_clock::now().time_since_epoch()).count();
+#endif
+ }
+
+ /*!
+ * \brief Print event statistics in json format to the supplied output stream
+ * \param os Output stream to write the data
+ * \note Emits all sub-even statistics
+ */
+ void EmitEvents(std::ostream *os) {
+ size_t count = 0;
+ for (size_t i = 0; i < sizeof(items_) / sizeof(items_[0]); ++i) {
+ if (items_[i].enabled_) {
+ if (count) {
+ *os << ",\n";
+ }
+ EmitSubEvent(os, i);
+ ++count;
+ }
+ }
+ }
+
+ /*!
+ * \brief Virtual destructor
+ */
+ virtual ~ProfileStat() {}
+
+ /*!
+ * \brief Save aggregate data for this stat
+ * \param data Stat data
+ */
+ virtual void SaveAggregate(AggregateStats::StatData *data) const {
+ if (data) {
+ data->type_ = AggregateStats::StatData::kOther;
+ }
+ }
+
+ protected:
+ /*!
+ * \brief Override to emit extra items within the json event data block. Append with a comma ",".
+ * \param os Output stream to write data to
+ * \param idx Sub-even index (index into items_) to write
+ */
+ virtual void EmitExtra(std::ostream *os, size_t idx) {}
+
+ /*!
+ * \brief Emit sub-event statistics
+ * \param os Output stream
+ * \param idx Sub-even index (index into items_) to write
+ */
+ void EmitSubEvent(std::ostream *os, size_t idx) {
+ const SubEvent &ev = items_[idx];
+ if (ev.enabled_) {
+ *os << " {\n"
+ << " \"name\": \"" << name_.c_str() << "\",\n"
+ << " \"cat\": " << "\"" << categories_.c_str() << "\",\n"
+ << " \"ph\": \"" << static_cast<char>(ev.event_type_) << "\",\n"
+ << " \"ts\": " << ev.timestamp_ << ",\n";
+ EmitExtra(os, idx);
+ *os << " \"pid\": " << process_id_ << ",\n"
+ << " \"tid\": " << thread_id_ << "\n"
+ << " }\n";
+ }
+ }
+};
+
+/*!
+ * \brief Device statistics
+ */
+struct DeviceStats {
+ using TQueue = dmlc::moodycamel::ConcurrentQueue<ProfileStat *>;
+ /*!
+ * \brief Destructor, clean up allocated objects
+ */
+ ~DeviceStats() {
+ std::shared_ptr<TQueue> es = opr_exec_stats_;
+ if (es) {
+ ProfileStat *stat = nullptr;
+ while (es->try_dequeue(stat)) {
+ delete stat;
+ }
+ }
+ }
+
+ /*! \brief device name */
+ std::string dev_name_;
+ /*! \brief operation execution statistics on this device */
+ std::shared_ptr<TQueue> opr_exec_stats_ = std::make_shared<TQueue>();
+};
+
+/*!
+ * _____ __ _ _
+ * | __ \ / _|(_)| |
+ * | |__) |_ __ ___ | |_ _ | | ___ _ __
+ * | ___/| '__|/ _ \| _|| || |/ _ \| '__|
+ * | | | | | (_) | | | || | __/| |
+ * |_| |_| \___/|_| |_||_|\___||_|
+ *
+ * \brief profiler that records the operation execution information
+ * and saves the profile statistics.
+ * \note Profiler class doesn't know anything about VTune
+ */
+class Profiler {
+ public:
+ enum ProfilerMode {
+ kSymbolic = 1,
+ kImperative = 2,
+ kAPI = 4,
+ kMemory = 8
+ };
+ enum ProfilerState {
+ kNotRunning = 0,
+ kRunning = 1
+ };
+
+ /*! \brief set state of profiler */
+ void SetState(ProfilerState state);
+ /*! \return state of profiler */
+ inline ProfilerState GetState() const {
+ return this->state_;
+ }
+ /*!
+ * \brief set profiler configuration
+ * \param mode flags, one or more of 'ProfilerMode'
+ * \param output_filename profile output file name
+ * \param continuous_dump true if profile information should be periodically dumped
+ * \param dump_period Period (in seconds) of profile info dumping
+ */
+ void SetConfig(int mode, std::string output_filename,
+ bool continuous_dump,
+ float dump_period,
+ bool aggregate_stats);
+
+ /*! \return mode of profiler */
+ inline int GetMode() const {
+ return this->mode_;
+ }
+
+ inline bool IsProfiling(const ProfilerMode pm) const {
+ return GetState() == kRunning && (GetMode() & pm) == pm;
+ }
+
+ /*! \return whether the profiler is enabled to output */
+ inline bool IsEnableOutput() const {
+ return this->enable_output_;
+ }
+ /*!
+ * \brief dump the profile file
+ * \param peform_cleanup Close off the json trace structures (ie last pass)
+ */
+ void DumpProfile(bool peform_cleanup = true);
+
+ /*! \return the profiler init time, time unit is microsecond (10^-6) s */
+ uint64_t MSHADOW_CINLINE GetInitTime() const {
+ return init_time_;
+ }
+ /*!
+ * \brief add one operation execution record in corresponding device statistics
+ * \tparam SetExtraInfoFunction
+ * \param dev_type
+ * \param dev_id
+ * \param set_extra_info_function
+ * \note Because when this function exits, the object is written to the profile queue,
+ * and at that point, could be consumed and/or destroyed at any moment,
+ * any preprocessing on the object is to be done in the set_extra_info_function
+ * callback. Another option is to use the CreateProfileStat()/AddProfileStat() pair,
+ * adding it only after
+ */
+ template<typename StatType, typename SetExtraInfoFunction, typename ...Args>
+ void AddNewProfileStat(SetExtraInfoFunction set_extra_info_function, Args... args) {
+ if (!paused_) {
+ std::unique_ptr<StatType> stat = CreateProfileStat<StatType>(args...);
+ set_extra_info_function(stat.get());
+ AddProfileStat(&stat);
+ }
+ }
+
+ /*!
+ * \brief Return aggregate statistic accumulator
+ * \return shared pointer to the 'ProfileStats' aggregate statistic accumulator
+ */
+ std::shared_ptr<AggregateStats> GetAggregateStats() const {
+ return aggregate_stats_;
+ }
+
+ /*!
+ * \brief Get a pointer to the Profiler singleton
+ * \return Profiler singleton
+ * \param sp Profiler shared pointer, only use for singleton ownership
+ */
+ static Profiler* Get(std::shared_ptr<Profiler> *sp = nullptr);
+
+ /*!
+ * \brief Set whether statistic collection is to be paused
+ * \param paused true if statistic collection is to be paused, otherwise
+ * resume statistic collection
+ * \note Pause/Resume is not recursive
+ */
+ void set_paused(bool paused) { paused_ = paused; }
+
+ /*!
+ * \brief Get the calculated device count (numb er of devices to track in profile data).
+ * \return Device count
+ * \note Number of CPU's + Number of GPU's + One for CPU-Pinned
+ */
+ size_t DeviceCount() const { return cpu_num_ + gpu_num_ + 2; }
+
+ /*!
+ * \brief Compute device index given device type and id
+ * \param dev_type Device type
+ * \param dev_id Device ID
+ * \return Device index for indexing into device-specific data
+ */
+ size_t DeviceIndex(mxnet::Context::DeviceType dev_type, int32_t dev_id);
+
+ /*!
+ * \brief Device name
+ * \param dev_type Device type
+ * \param dev_id Device ID
+ * \return Character pointer to device name
+ */
+ const char *DeviceName(mxnet::Context::DeviceType dev_type, int32_t dev_id);
+
+
+ /*!
+ * \brief Device name
+ * \param dev_type Device type
+ * \param dev_id Device ID
+ * \return Character pointer to device name
+ */
+ const char *DeviceName(const size_t index);
+
+ /*!
+ * \brief Whether aggregate stats are being collected
+ * \return true if aggregate stats are being collected
+ */
+ inline bool AggregateEnabled() const {
+ return aggregate_stats_.get() != nullptr;
+ }
+
+ public:
+ /*!
+ * \brief Constructor
+ */
+ Profiler();
+
+ /*!
+ * \brief Destructor
+ */
+ virtual ~Profiler();
+
+ private:
+ /*!
+ * \brief Create a new profile statistic object
+ * \tparam StatType The type of the profile statistic object
+ * \tparam Args Argument types to pass to the new object's constructor
+ * \param args Arguments to pass to the new object's constructor
+ * \return A unique_ptr to the new statistic object
+ */
+ template<typename StatType, typename ...Args>
+ static std::unique_ptr<typename std::enable_if<std::is_base_of<ProfileStat, StatType>::value,
+ StatType>::type> CreateProfileStat(Args... args) {
+ return std::unique_ptr<StatType>(new StatType(args...));
+ }
+
+ /*!
+ * \brief Add a general profile statistic object
+ * \tparam StatType Type of the statistic object
+ * \param stat The statistic object
+ */
+ template<typename StatType>
+ inline void AddProfileStat(std::unique_ptr<StatType> *stat) {
+ general_stats_.opr_exec_stats_->enqueue(stat->release());
+ }
+
+ /*! \brief generate device information following chrome profile file format */
+ void EmitPid(std::ostream *os, const std::string& name, size_t pid);
+
+ /*!
+ * \brief Set continuous asynchronous profile dump
+ * \param continuous_dump Whether to continuously dump profile information
+ * \param delay_in_seconds Delay between asynchronous dumps
+ */
+ void SetContinuousProfileDump(bool continuous_dump, float delay_in_seconds);
+
+ /*! \brief internal mutex of the profiler */
+ std::recursive_mutex m_;
+ /*! \brief indicate whether the profiler is running */
+ ProfilerState state_;
+ /*! \brief once running, enable profiler to output */
+ volatile bool enable_output_;
+ /*! \brief indicate what operator the profiler will record */
+ int mode_ = kSymbolic | kAPI | kMemory;
+ /*! \brief filename to output profile file */
+ std::string filename_ = "profile.json";
+ /*! \brief profile statistics consist of multiple device statistics */
+ DeviceStats* profile_stat;
+ /*! \brief Stats not associated directly with a device */
+ DeviceStats general_stats_;
+ /*! \brief Map category -> pid */
+ std::unordered_map<std::string, size_t> category_to_pid_;
+ /*! \brief cpu number on the machine */
+ unsigned int cpu_num_;
+ /*! \brief gpu number on the machine */
+ unsigned int gpu_num_;
+ /*! \brief the profiler init time */
+ uint64_t init_time_;
+ /*! \brief Continuously dump profile info */
+ volatile bool continuous_dump_ = false;
+ /*! \brief Number of non-meta profiling record emitted */
+ volatile uint64_t num_records_emitted_ = 0;
+ /*! \brief Number of times profile was dumped */
+ volatile uint64_t profile_dump_count_;
+ /*! \brief Whether profiling is paused */
+ volatile bool paused_ = false;
+ /*! \brief Maintain in-memory aggregate stats for print output.
+ * \warning This has a negative performance impact */
+ std::shared_ptr<AggregateStats> aggregate_stats_ = nullptr;
+ /*! \brief Asynchronous operation thread lifecycly control object */
+ std::shared_ptr<dmlc::ThreadGroup> thread_group_ = std::make_shared<dmlc::ThreadGroup>();
+ /* !\brief pids */
+ std::unordered_set<uint32_t> process_ids_;
+};
+
+#ifdef MXNET_USE_VTUNE
+#define VTUNE_ONLY_CODE(...) __VA_ARGS__ /* This is undefined at the bottom of this file */
+#else
+#define VTUNE_ONLY_CODE(...) /* */ /* This is undefined at the bottom of this file */
+#endif
+
+/**
+ * _____ __ _ _ _ ____ _ _ _
+ * | __ \ / _|(_)| |(_) / __ \| | (_) | |
+ * | |__) |_ __ ___ | |_ _ | | _ _ __ __ _ | | | | |__ _ ___ ___| |_ ___
+ * | ___/| '__|/ _ \| _|| || || | '_ \ / _` | | | | | '_ \| |/ _ \ / __| __|/ __|
+ * | | | | | (_) | | | || || | | | | (_| | | |__| | |_) | | __/| (__| |_ \__ \
+ * |_| |_| \___/|_| |_||_||_|_| |_|\__, | \____/|_.__/| |\___| \___|\__||___/
+ * __/ | _/ |
+ * |___/ |__/
+ */
+
+enum ProfileObjectType {
+ kDomain,
+ kCounter,
+ kTask,
+ kEvent,
+ kFrame
+};
+
+class ProfileObject {
+ public:
+ /*!
+ * \brief Virtual destructor for child classes
+ */
+ virtual ~ProfileObject() {}
+ /*!
+ * \brief Return profiling object object type (i.e. kTask, kEvent, ...)
+ * \return Profiling object type
+ */
+ virtual ProfileObjectType type() const = 0;
+};
+
+/*!
+ * \brief Tuning domain. Used in VTune to separate similar tasks/counters/etc. (for example).
+ * For chrome tracing, generally maps to category.
+ */
+struct ProfileDomain : public ProfileObject {
+ /*!:(
+ * \brief Constructor
+ * \param name Name of the domain
+ */
+ explicit ProfileDomain(const char *name) noexcept
+ : name_(name) {
+ CHECK_NOTNULL(name);
+ CHECK_NE(name[0], '\0');
+ VTUNE_ONLY_CODE(vtune_domain_.reset(new vtune::VTuneDomain(name)));
+ }
+ /*!
+ * \brief Get domain name
+ * \return Domain name
+ */
+ const char *name() const { return name_.c_str(); }
+ ProfileObjectType type() const override { return kDomain; }
+ VTUNE_ONLY_CODE(inline vtune::VTuneDomain *dom() { return vtune_domain_.get(); });
+ private:
+ /*! \brief Name of the domain */
+ profile_stat_string name_;
+ /*! \brief VTune domain object */
+ VTUNE_ONLY_CODE(std::unique_ptr<vtune::VTuneDomain> vtune_domain_);
+};
+
+/*!
+ * \brief Counter object and statistic item
+ */
+struct ProfileCounter : public ProfileObject {
+ /*!
+ * \brief Co9nstructor
+ * \param name Counter name
+ * \param domain Counter domain
+ */
+ ProfileCounter(const char *name, ProfileDomain *domain) noexcept
+ : name_(name)
+ , domain_(domain)
+ , value_(0) {
+ CHECK_NOTNULL(domain);
+ VTUNE_ONLY_CODE(vtune_.reset(new vtune::VTuneCounter(name, domain->dom())));
+ }
+ ~ProfileCounter() {}
+ /*! \brief operator: ++object */
+ inline uint64_t operator ++() {
+ return IncrementValue(1);
+ }
+ /*! \brief operator: object++ */
+ inline uint64_t operator ++(int) {
+ const uint64_t old = value_;
+ IncrementValue(1);
+ return old;
+ }
+ /*! \brief operator: --object */
+ inline uint64_t operator --() {
+ CHECK_GT(value_, 0);
+ return DecrementValue(1);
+ }
+ /*! \brief operator: object-- */
+ inline uint64_t operator --(int) {
+ CHECK_GT(value_, 0);
+ const uint64_t old = value_;
+ DecrementValue(1);
+ return old;
+ }
+ /*! \brief operator: object += v */
+ inline uint64_t operator +=(int64_t v) {
+ if (v >= 0) {
+ return IncrementValue(static_cast<uint64_t>(v));
+ } else {
+ v = -v;
+ return DecrementValue(static_cast<uint64_t>(v));
+ }
+ }
+ /*! \brief operator: object -= v */
+ inline uint64_t operator -=(int64_t v) {
+ CHECK_GE(value_, v);
+ if (v >= 0) {
+ return DecrementValue(static_cast<uint64_t>(v));
+ } else {
+ v = -v;
+ return IncrementValue(static_cast<uint64_t>(v));
+ }
+ }
+ /*! \brief operator: object = v */
+ inline ProfileCounter& operator = (uint64_t v) {
+ SetValue(v);
+ return *this;
+ }
+
+ ProfileObjectType type() const override { return kCounter; }
+
+ protected:
+ /*!
+ * \brief Count statistic object
+ */
+ struct ProfileCounterStat : public ProfileStat {
+ uint64_t value_;
+ explicit ProfileCounterStat(const char *name, uint64_t value) : value_(value) {
+ items_[0].enabled_ = true;
+ items_[0].event_type_ = kCounter;
+ items_->timestamp_ = NowInMicrosec();
+ name_.set(name);
+ }
+
+ /*!
+ * \brief Emit counter value as extra data for this statistic type
+ * \param os Output stream to write data to
+ * \param idx Sub-even index (index into items_) to write
+ */
+ void EmitExtra(std::ostream *os, size_t idx) override {
+ ProfileStat::EmitExtra(os, idx);
+ *os << " \"args\": { \"" << name_.c_str() << "\": " << value_ << " },\n";
+ }
+
+ /*!
+ * \brief Save aggregate data for this stat
+ * \param data Stat data
+ */
+ void SaveAggregate(AggregateStats::StatData *data) const override {
+ if (data) {
+ data->type_ = AggregateStats::StatData::kCounter;
+ ++data->total_count_;
+ data->total_aggregate_ = value_;
+ if (value_ > data->max_aggregate_) {
+ data->max_aggregate_ = value_;
+ }
+ if (value_ < data->min_aggregate_) {
+ data->min_aggregate_ = value_;
+ }
+ }
+ }
+ };
+
+ private:
+ /*!
+ * \brief Send this object's statistical datapoint to the profiler
+ */
+ inline void SendStat(uint64_t value) {
+ Profiler::Get()->AddNewProfileStat<ProfileCounterStat>([this](ProfileCounterStat *stat) {
+ stat->categories_.set(domain_->name());
+ },
+ name_.c_str(),
+ value);
+ }
+
+ /*!
+ * \brief Set counter value
+ * \param val Value to set the counter
+ */
+ inline void SetValue(uint64_t val) {
+ VTUNE_ONLY_CODE(*vtune_ = val);
+ value_ = val;
+ SendStat(val);
+ }
+
+ /*!
+ * \brief Adjust counter value by amount given
+ * \param value_change Value to change the counter by
+ */
+ inline uint64_t IncrementValue(uint64_t value_change) {
+ VTUNE_ONLY_CODE(*vtune_ += value_change);
+ const uint64_t v = (value_ += value_change);
+ SendStat(v);
+ return v;
+ }
+
+ inline uint64_t DecrementValue(uint64_t value_change) {
+ VTUNE_ONLY_CODE(*vtune_ -= value_change);
+ const uint64_t v = (value_ -= value_change);
+ SendStat(v);
+ return v;
+ }
+
+ /*! \brief Name of the counter */
+ profile_stat_string name_;
+ /*! \brief Domain of the counter */
+ ProfileDomain *domain_;
+ /*! \brief Value of the counter */
+ std::atomic<uint64_t> value_;
+ /*! \brief VTune counter object */
+ VTUNE_ONLY_CODE(std::unique_ptr<vtune::VTuneCounter> vtune_);
+};
+
+class ProfileDuration : public ProfileObject {
+ public:
+ virtual void start() = 0;
+ virtual void stop() = 0;
+
+ protected:
+ /*!
+ * \brief Basic duration statistic (start time, stop time)
+ */
+ struct DurationStat : public ProfileStat {
+ enum DurationStatIndex {
+ kStart, kStop
+ };
+ /*!
+ * \brief Constructor
+ * \param begin_event Event type for start point (default is kDurationBegin)
+ * \param end_event Event type for stop point (default is kDurationEnd)
+ */
+ DurationStat(ProfileStat::EventType begin_event = ProfileStat::kDurationBegin,
+ ProfileStat::EventType end_event = ProfileStat::kDurationEnd) {
+ items_[kStart].enabled_ = items_[kStop].enabled_ = true;
+ items_[kStart].event_type_ = begin_event;
+ items_[kStop].event_type_ = end_event;
+ }
+
+ /*!
+ * \brief Save aggregate data for this stat
+ * \param data Stat data
+ */
+ void SaveAggregate(AggregateStats::StatData *data) const override {
+ if (data) {
+ data->type_ = AggregateStats::StatData::kDuration;
+ ++data->total_count_;
+ CHECK_GE(items_[kStop].timestamp_, items_[kStart].timestamp_);
+ const uint64_t duration = items_[kStop].timestamp_ - items_[kStart].timestamp_;
+ data->total_aggregate_ += duration;
+ if (duration > data->max_aggregate_) {
+ data->max_aggregate_ = duration;
+ }
+ if (duration < data->min_aggregate_) {
+ data->min_aggregate_ = duration;
+ }
+ }
+ }
+ };
+};
+
+/*!
+ * \brief Task - Thread-granular nestable time block
+ */
+struct ProfileTask : public ProfileDuration {
+ /*!
+ * \brief Constructor
+ * \param name Name of the task
+ * \param domain Domain of the task
+ */
+ ProfileTask(const char *name, ProfileDomain *domain)
+ : name_(name)
+ , domain_(domain) {
+ CHECK_NOTNULL(domain);
+ categories_.set(domain_->name());
+ categories_.append(",task");
+ VTUNE_ONLY_CODE(vtune_task_.reset(new vtune::VTuneTask(name, domain->dom())));
+ }
+
+ /*!
+ * \brief Start the profiling scope
+ */
+ void start() override {
+ start_time_ = ProfileStat::NowInMicrosec();
+ VTUNE_ONLY_CODE(vtune_task_->start());
+ }
+
+ /*!
+ * \brief Stop the profiling scope
+ */
+ void stop() override {
+ VTUNE_ONLY_CODE(vtune_task_->stop());
+ SendStat();
+ }
+
+ ProfileObjectType type() const override { return kTask; }
+
+ protected:
+ /*!
+ * \brief Task statistic object
+ */
+ struct ProfileTaskStat : public DurationStat {
+ explicit ProfileTaskStat(const char *name, uint64_t start_time, uint64_t stop_time)
+ : DurationStat(ProfileStat::kAsyncNestableStart, ProfileStat::kAsyncNestableEnd) {
+ name_.set(name);
+ items_[0].timestamp_ = start_time;
+ items_[1].timestamp_ = stop_time;
+ }
+ void EmitExtra(std::ostream *os, size_t idx) override {
+ DurationStat::EmitExtra(os, idx);
+ *os << " \"id\": " << thread_id_ << ",\n";
+ }
+ };
+
+ private:
+ /*!
+ * \brief Send this object's statistical datapoint to the profiler
+ */
+ inline void SendStat() {
+ Profiler::Get()->AddNewProfileStat<ProfileTaskStat>([this](ProfileTaskStat *stat) {
+ stat->categories_.set(domain_->name());
+ }, name_.c_str(), start_time_, ProfileStat::NowInMicrosec());
+ }
+ /*! \brief Task name */
+ const profile_stat_string name_;
+ /*! \brief Task categories */
+ profile_stat_string categories_;
+ /*! \brief domain */
+ ProfileDomain *domain_;
+ /*! \brief VTune task object */
+ VTUNE_ONLY_CODE(std::unique_ptr<vtune::VTuneTask> vtune_task_);
+
+ protected:
+ /*! \brief Task's start tick */
+ uint64_t start_time_;
+};
+
+/*!
+ * \brief Event - Thread-granular time block
+ */
+struct ProfileEvent : public ProfileDuration {
+ /*!
+ * \brief Constructor
+ * \param name Name of the event
+ */
+ explicit inline ProfileEvent(const char *name)
+ : name_(name)
+ , categories_("event") {
+ VTUNE_ONLY_CODE(vtune_event_ = vtune::VTuneEvent::registry_.get(name));
+ }
+
+ /*!
+ * \brief Start the profiling scope
+ */
+ void start() override {
+ start_time_ = ProfileStat::NowInMicrosec();
+ VTUNE_ONLY_CODE(vtune_event_->start());
+ }
+
+ /*!
+ * \brief Stop the profiling scope
+ */
+ void stop() override {
+ VTUNE_ONLY_CODE(vtune_event_->stop());
+ SendStat();
+ }
+
+ /*!
+ * \brief Set catagories (used for chrome tracing)
+ * \param categories Comma-delimited categories
+ */
+ void SetCategories(const char *categories) {
+ categories_.set(categories);
+ }
+
+ ProfileObjectType type() const override { return kEvent; }
+
+ protected:
+ /*!
+ * \brief Event statistic object
+ */
+ struct ProfileEventStat : public DurationStat {
+ explicit ProfileEventStat(const char *name, uint64_t start_time, uint64_t stop_time)
+ : DurationStat(ProfileStat::kDurationBegin, ProfileStat::kDurationEnd) {
+ name_.set(name);
+ items_[0].timestamp_ = start_time;
+ items_[1].timestamp_ = stop_time;
+ }
+ };
+
+ private:
+ /*!
+ * \brief Send this object's statistical datapoint to the profiler
+ */
+ virtual void SendStat() {
+ Profiler::Get()->AddNewProfileStat<ProfileEventStat>([this](ProfileEventStat *stat) {
+ stat->categories_.set(categories_.c_str());
+ }, name_.c_str(), start_time_, ProfileStat::NowInMicrosec());
+ }
+ /*! \brief Event name */
+ const profile_stat_string name_;
+ /*! \brief Event categories (comma-delimited) */
+ profile_stat_string categories_;
+ /*! \brief VTune event object */
+ VTUNE_ONLY_CODE(vtune::VTuneEvent *vtune_event_);
+
+ protected:
+ /*! \brief Start time of the event */
+ uint64_t start_time_;
+};
+
+/*!
+ * \brief Frame - Process-granular time block
+ */
+struct ProfileFrame : public ProfileDuration {
+ /*!
+ * \brief Constructor
+ * \param name Name of the frame
+ * \param domain Domain of the frame
+ */
+ ProfileFrame(const char *name, ProfileDomain *domain)
+ : name_(name)
+ , domain_(domain) {
+ CHECK_NOTNULL(domain);
+ categories_.set(domain_->name());
+ categories_.append(",frame");
+ VTUNE_ONLY_CODE(vtune_frame_.reset(new vtune::VTuneFrame(domain->dom())));
+ }
+
+ /*!
+ * \brief Start the profiling scope
+ */
+ void start() override {
+ start_time_ = ProfileStat::NowInMicrosec();
+ VTUNE_ONLY_CODE(vtune_frame_->start());
+ }
+
+ /*!
+ * \brief Stop the profiling scope
+ */
+ void stop() override {
+ VTUNE_ONLY_CODE(vtune_frame_->stop());
+ SendStat();
+ }
+
+ ProfileObjectType type() const override { return kFrame; }
+
+ protected:
+ /*!
+ * \brief Frame statistic object
+ */
+ struct ProfileFrameStat : public DurationStat {
+ explicit ProfileFrameStat(const char *name, uint64_t start_time, uint64_t stop_time)
+ : DurationStat(ProfileStat::kContextEnter, ProfileStat::kContextLeave) {
+ name_.set(name);
+ items_[0].timestamp_ = start_time;
+ items_[1].timestamp_ = stop_time;
+ }
+ };
+
+ private:
+ /*!
+ * \brief Send this object's statistical datapoint to the profiler
+ */
+ inline void SendStat() {
+ Profiler::Get()->AddNewProfileStat<ProfileFrameStat>([this](ProfileFrameStat *stat) {
+ stat->categories_.set(categories_.c_str());
+ }, name_.c_str(), start_time_, ProfileStat::NowInMicrosec());
+ }
+ /*! \brief Frame name */
+ const profile_stat_string name_;
+ /*! \brief Frame categories (comma-delimited) */
+ profile_stat_string categories_;
+ /*! \brief Pointer to the domain */
+ ProfileDomain *domain_;
+ /*! \brief VTune Frame object */
+ VTUNE_ONLY_CODE(std::unique_ptr<vtune::VTuneFrame> vtune_frame_);
+
+ protected:
+ /*! \brief Frame start time */
+ uint64_t start_time_;
+};
+
+/*!
+ * \brief Marker - Mark an instance in time
+ */
+struct ProfileMarker {
+ enum MarkerScope { // Should equal VTune values
+ kUnknown, kGlobal, kProcess, kThread, kTask, kMarker
+ };
+
+ /*!
+ * \brief Constructor
+ * \param name Name of the instant marker
+ * \param domain Domain of the instant marker
+ * \param scope Scope of the instant marker
+ * \param nestable true if the instant marker is nestable
+ */
+ ProfileMarker(const char *name,
+ ProfileDomain *domain,
+ const MarkerScope scope,
+ bool nestable = true)
+ : name_(name)
+ , domain_(domain)
+ , scope_(scope)
+ , nestable_(nestable) {
+ categories_.set(domain_->name());
+ categories_.append(",instant_marker");
+ VTUNE_ONLY_CODE(vtune_instant_marker_.reset(
+ new vtune::VTuneInstantMarker(name, domain->dom(), static_cast<__itt_scope>(scope))));
+ }
+
+ /*!
+ * \brief Signal a marker at this instant
+ */
+ void mark() {
+ VTUNE_ONLY_CODE(vtune_instant_marker_->signal());
+ SendStat();
+ }
+
+ protected:
+ /*!
+ * \brief Instant-marker statistic object
+ */
+ struct ProfileMarkerStat : public ProfileStat {
+ explicit ProfileMarkerStat(const char *name, const char scope_char, bool nestable)
+ : scope_char_(scope_char) {
+ items_[0].enabled_ = true;
+ items_[0].event_type_ = nestable ? kAsyncNestableInstant : kInstant;
+ items_->timestamp_ = NowInMicrosec();
+ name_.set(name);
+ }
+ virtual void EmitExtra(std::ostream *os, size_t idx) {
+ ProfileStat::EmitExtra(os, idx);
+ *os << " \"s\": \"" << scope_char_ << "\",\n";
+ }
+ const char scope_char_;
+ };
+
+ private:
+ /*!
+ * \brief Send this object's statistical datapoint to the profiler
+ */
+ virtual void SendStat() {
+ Profiler::Get()->AddNewProfileStat<ProfileMarkerStat>([this](ProfileMarkerStat *stat) {
+ stat->categories_.set(categories_.c_str());
+ }, name_.c_str(), vtune_scope_to_chrome_scope(scope_), nestable_);
+ }
+
+ static char vtune_scope_to_chrome_scope(const MarkerScope scope) {
+ switch (scope) {
+ case kThread:
+ return 't';
+ case kGlobal:
+ return 'g';
+ case kProcess:
+ case kUnknown:
+ case kTask:
+ case kMarker:
+ default:
+ return 'p';
+ }
+ }
+
+ /*! \brief Name of the instant marker */
+ const profile_stat_string name_;
+ /*! \brief Categories of the instant marker (comma-delimited) */
+ profile_stat_string categories_;
+ /*! \brief Pointer to the domain of this instant marker */
+ ProfileDomain *domain_;
+ /*! \brief VTune scope */
+ const MarkerScope scope_;
+ /*! \brief Whether this marker is nestabe */
+ const bool nestable_;
+ /*! \brief VTune instant marker object */
+ VTUNE_ONLY_CODE(std::unique_ptr<vtune::VTuneInstantMarker> vtune_instant_marker_);
+};
+
+/*!
+ * \brief Operator profiler object. Logs as both an independent event and a task in
+ * the operator domain
+ */
+struct ProfileOperator : public ProfileEvent {
+ /*!
+ * \brief Operator attributes
+ */
+ struct Attributes {
+ std::vector<nnvm::TShape> inputs_;
+ std::vector<nnvm::TShape> outputs_;
+ std::unordered_map<std::string, std::string> attr_;
+ std::string to_string() const {
+ std::stringstream ss;
+ if (!inputs_.empty()) {
+ ss << "in: [";
+ for (size_t i = 0, n = inputs_.size(); i < n; ++i) {
+ if (i) {
+ ss << ",";
+ }
+ ss << inputs_[i];
+ }
+ ss << "]";
+ }
+ if (!outputs_.empty()) {
+ ss << "out: [";
+ for (size_t i = 0, n = outputs_.size(); i < n; ++i) {
+ if (i) {
+ ss << ",";
+ }
+ ss << outputs_[i];
+ }
+ ss << "]";
+ }
+ if (!attr_.empty()) {
+ for (const auto &tt : attr_) {
+ ss << " (" << tt.first << "=" << tt.second << ")";
+ }
+ }
+ return ss.str();
+ }
+ };
+
+ /*!
+ * \brief Constructor
+ * \param name Name of the operator
+ */
+ explicit inline ProfileOperator(const char *name, Attributes *attributes)
+ : ProfileEvent(name)
+ , as_task_(name, &domain_)
+ , name_(name)
+ , attributes_(attributes) {
+ SetCategories(domain_.name());
+ }
+ /*!
+ * \brief Start the profiling scope
+ * \param dev_type Device type that the profiling will occur on
+ * \param dev_id Device id associated with this opr
+ */
+ void start(mxnet::Context::DeviceType dev_type, uint32_t dev_id) {
+ dev_type_ = dev_type;
+ dev_id_ = dev_id;
+ ProfileEvent::start();
+ as_task_.start();
+ }
+ /*!
+ * \brief Stop the profiling scope
+ */
+ void stop() override {
+ as_task_.stop();
+ ProfileEvent::stop();
+ }
+
+ /*!
+ * \brief Operation execution statistics
+ */
+ struct OprExecStat : public DurationStat {
+ /*!
+ * \brief Constructor
+ * \param name Name of the operator
+ * \param dev_type Device type (i.e. CPU: 1, GPU: 2, CPUPinned: 3)
+ * \param dev_id Device ID (ie GPU number)
+ * \param start_time Time when operator starts
+ * \param stop_time Time when operator completes
+ */
+ inline OprExecStat(const char *name, mxnet::Context::DeviceType dev_type, uint32_t dev_id,
+ uint64_t start_time, uint64_t stop_time,
+ const Attributes *attributes)
+ : DurationStat(ProfileStat::kDurationBegin, ProfileStat::kDurationEnd)
+ , dev_type_(dev_type)
+ , dev_id_(dev_id) {
+ name_.set(name);
+ if (attributes) {
+ name_.append(attributes->to_string().c_str());
+ }
+ categories_.set("operator");
+ items_[kStart].timestamp_ = start_time;
+ items_[kStop].timestamp_ = stop_time;
+ }
+ /*! \brief device type: CPU: 1, GPU: 2, CPUPinned: 3 */
+ mxnet::Context::DeviceType dev_type_;
+ /*! \brief device id */
+ uint32_t dev_id_;
+ };
+
+ private:
+ /*!
+ * \brief Send this object's statistical datapoint to the profiler
+ */
+ void SendStat() override {
+ Profiler::Get()->AddNewProfileStat<OprExecStat>(
+ [this](OprExecStat *stat) {}, name_.c_str(), dev_type_, dev_id_,
+ start_time_, ProfileStat::NowInMicrosec(),
+ attributes_.get());
+ }
+ /*! \brief Also log the operator as a task in the operator domain */
+ ProfileTask as_task_;
+ /* !\brief Operator name */
+ profile_stat_string name_;
+ /*! \brief device type: CPU: 1, GPU: 2, CPUPinned: 3 */
+ Context::DeviceType dev_type_;
+ /*! \brief device id */
+ uint32_t dev_id_;
+ /*! \brief Operator domain */
+ static ProfileDomain domain_;
+ /*! \brief Optional operator attributes */
+ std::unique_ptr<Attributes> attributes_;
+};
+
+/*
+ * Profiler inline functions
+ */
+inline const char *Profiler::DeviceName(mxnet::Context::DeviceType dev_type, int32_t dev_id) {
+ return profile_stat[DeviceIndex(dev_type, dev_id)].dev_name_.c_str();
+}
+
+inline const char *Profiler::DeviceName(const size_t index) {
+ return profile_stat[index].dev_name_.c_str();
+}
+
+inline size_t Profiler::DeviceIndex(mxnet::Context::DeviceType dev_type, int32_t dev_id) {
+ switch (dev_type) {
+ case Context::kCPU:
+ return dev_id;
+ case Context::kGPU:
+ return cpu_num_ + dev_id;
+ case Context::kCPUPinned:
+ return cpu_num_ + gpu_num_;
+ case Context::kCPUShared:
+ return cpu_num_ + gpu_num_ + 1;
+ default:
+ LOG(FATAL) << "Unknown dev_type: " << dev_type;
+ return 0;
+ }
+}
+
+/*!
+ * \brief Explicit 'Profiler::AddProfileStat' override for 'OprExecStat'
+ * \param opr_stat Unique pointert to the operator statistic
+ */
+template<>
+inline void Profiler::AddProfileStat<ProfileOperator::OprExecStat>(
+ std::unique_ptr<ProfileOperator::OprExecStat> *opr_stat) {
+ const size_t idx = DeviceIndex((*opr_stat)->dev_type_, (*opr_stat)->dev_id_);
+ CHECK_LT(idx, DeviceCount());
+ DeviceStats& dev_stat = profile_stat[idx];
+ dev_stat.opr_exec_stats_->enqueue((*opr_stat).release());
+}
+
+#undef VTUNE_ONLY_CODE // This macro not meant to be used outside of this file
+
+} // namespace profiler
+} // namespace mxnet
+#endif // MXNET_PROFILER_PROFILER_H_
diff --git a/src/profiler/storage_profiler.h b/src/profiler/storage_profiler.h
new file mode 100644
index 0000000..b9a7e01
--- /dev/null
+++ b/src/profiler/storage_profiler.h
@@ -0,0 +1,111 @@
+/*
+ * 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.
+ */
+#ifndef MXNET_PROFILER_STORAGE_PROFILER_H_
+#define MXNET_PROFILER_STORAGE_PROFILER_H_
+
+#include <mxnet/storage.h>
+#include <string>
+#include <vector>
+#include "./profiler.h"
+
+namespace mxnet {
+namespace storage {
+
+#if MXNET_USE_PROFILER
+/*!
+ * \brief Storage allocation/deallocation profiling via ProfileCounters
+ */
+class DeviceStorageProfiler {
+ public:
+ /*!
+ * \brief Constructor
+ */
+ explicit DeviceStorageProfiler(const char *domain_name = "Device Storage")
+ : domain_(domain_name) {
+ }
+
+ /*!
+ * \brief Called when memory has been allocated in order to record the allocation size
+ * \param handle Handle to the allocated storage
+ */
+ void OnAlloc(const Storage::Handle &handle) {
+ if (handle.size > 0) {
+ profiler::Profiler *prof = profiler::Profiler::Get();
+ if (prof->IsProfiling(profiler::Profiler::kMemory)) {
+ Init();
+ const size_t idx = prof->DeviceIndex(handle.ctx.dev_type, handle.ctx.dev_id);
+ CHECK_LT(idx, mem_counters_.size()) << "Invalid device index: " << idx;
+ *mem_counters_[idx] += handle.size;
+ }
+ }
+ }
+
+ /*!
+ * \brief Called when memory has been freed in order to record the deallocation size
+ * \param handle Handle to the allocated storage
+ */
+ void OnFree(const Storage::Handle &handle) {
+ if (handle.size > 0) {
+ profiler::Profiler *prof = profiler::Profiler::Get();
+ if (prof->IsProfiling(profiler::Profiler::kMemory)) {
+ Init(); // In case of bug which tries to free first
+ const size_t idx = prof->DeviceIndex(handle.ctx.dev_type, handle.ctx.dev_id);
+ CHECK_LT(idx, mem_counters_.size()) << "Invalid device index: " << idx;
+ *mem_counters_[idx] -= handle.size;
+ }
+ }
+ }
+
+ private:
+ /*!
+ * \brief Lazy initialization. No locks occur except for on the first pass
+ * (or colliding parallel first passes)
+ */
+ void Init() {
+ if (mem_counters_.empty()) {
+ std::unique_lock<std::mutex> lk(init_mutex_);
+ // Check again in case of collision and someone else filled it
+ if (mem_counters_.empty()) {
+ profiler::Profiler *prof = profiler::Profiler::Get();
+ const size_t device_count = prof->DeviceCount();
+ mem_counters_.reserve(device_count);
+ for (size_t i = 0, n = device_count; i < n; ++i) {
+ std::string name = "Memory: ";
+ name += prof->DeviceName(i);
+ mem_counters_.emplace_back(std::make_shared<profiler::ProfileCounter>(name.c_str(),
+ &domain_));
+ }
+ }
+ }
+ }
+
+ /*! \brief Domain of the memory profiling information */
+ profiler::ProfileDomain domain_;
+ /*! \brief Mutex for lazy init */
+ std::mutex init_mutex_;
+ /*! \brief Constant-sized vector of memory profile counters */
+ std::vector<std::shared_ptr<profiler::ProfileCounter>> mem_counters_;
+};
+
+#endif // MXNET_USE_PROFILER
+
+} // namespace storage
+} // namespace mxnet
+
+#endif // MXNET_PROFILER_STORAGE_PROFILER_H_
diff --git a/src/profiler/vtune.cc b/src/profiler/vtune.cc
new file mode 100644
index 0000000..975bfb2
--- /dev/null
+++ b/src/profiler/vtune.cc
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+#include "./vtune.h"
+
+namespace mxnet {
+namespace profiler {
+namespace vtune {
+
+#if MXNET_USE_VTUNE
+VTuneRegistry<VTuneEvent> VTuneEvent::registry_;
+#endif // MXNET_USE_VTUNE
+
+} // namespace vtune
+} // namespace profiler
+} // namespace mxnet
diff --git a/src/profiler/vtune.h b/src/profiler/vtune.h
new file mode 100644
index 0000000..d048d6c
--- /dev/null
+++ b/src/profiler/vtune.h
@@ -0,0 +1,323 @@
+/*
+ * 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 vtune.h
+ * \brief VTune API classes.
+ */
+#ifndef MXNET_PROFILER_VTUNE_H_
+#define MXNET_PROFILER_VTUNE_H_
+
+#include <string.h>
+#include <dmlc/logging.h>
+#include <dmlc/thread_group.h>
+#include <mshadow/base.h>
+#include <atomic>
+#include <string>
+#include <unordered_map>
+
+#if MXNET_USE_VTUNE
+#include <ittnotify.h>
+#endif
+
+namespace mxnet {
+namespace profiler {
+namespace vtune {
+
+MSHADOW_CINLINE void vtune_pause() {
+#if MXNET_USE_VTUNE
+ __itt_pause();
+#endif
+}
+
+MSHADOW_CINLINE void vtune_resume() {
+#if MXNET_USE_VTUNE
+ __itt_resume();
+#endif
+}
+
+/*! \brief Pause VTune analysis */
+struct VTunePause {
+ MSHADOW_CINLINE VTunePause() {
+ vtune_pause();
+ }
+
+ MSHADOW_CINLINE ~VTunePause() {
+ vtune_resume();
+ }
+};
+
+/*! \brief Resume VTune analysis */
+struct VTuneResume {
+ MSHADOW_CINLINE VTuneResume() {
+ vtune_resume();
+ }
+
+ MSHADOW_CINLINE ~VTuneResume() {
+ vtune_pause();
+ }
+};
+
+#if MXNET_USE_VTUNE
+
+/*
+ * Intel VTune APIs. For API meanings, see:
+ * https://software.intel.com/en-us/vtune-amplifier-help-instrumentation-and-tracing-technology-api-reference NOLINT()
+ */
+class VTuneDomain {
+ public:
+ inline explicit VTuneDomain(const char *name) throw()
+ : domain_(__itt_domain_create(name)) {
+ CHECK_NOTNULL(domain_);
+ domain_->flags = 1;
+ }
+
+ inline operator __itt_domain *() { return domain_; }
+
+ inline __itt_domain *dom() { return domain_; }
+
+ inline const char *name() const { return domain_->nameA; }
+
+ private:
+ __itt_domain *domain_;
+};
+
+/*!
+ * \brief VTune object registry to hold create-once object by name
+ * \tparam VTuneObject
+ * \note Some objects are expensive to create, such as VTuneEvent, so it's more desirable to
+ * do the mutex lock and reuse
+ */
+template<typename VTuneObject>
+class VTuneRegistry {
+ public:
+ /*!
+ * \brief Retrieve (and create if needed) an object of type 'VTuneObject'
+ * \tparam Args Types of arguments to pass to constructor after 'name'
+ * \param name Name of the object
+ * \param args Arguments to pass to constructor after 'name'
+ * \return Pointer to the cached or new VTuneObject
+ */
+ template<typename ...Args>
+ inline VTuneObject *get(const char *name, Args... args) {
+ dmlc::ReadLock read_lock(m_);
+ auto iter = registry_.find(name);
+ if (iter == registry_.end()) {
+ dmlc::WriteLock write_lock(m_);
+ iter = registry_.emplace(name, std::unique_ptr<VTuneObject>(
+ new VTuneObject(name, args...))).first;
+ }
+ return iter->second.get();
+ }
+
+ /*!
+ * \brief Retrieve (and create if needed) an object of type 'VTuneObject'
+ * \tparam Args Types of arguments to pass to constructor after 'name'
+ * \param name Name of the object
+ * \param domain Domain of the object (used to create name key)
+ * \param args Arguments to pass to constructor after 'domain'
+ * \return Pointer to the cached or new VTuneObject
+ */
+ template<typename ...Args>
+ inline VTuneObject *get(const char *name, const VTuneDomain *domain, Args... args) {
+ dmlc::ReadLock read_lock(m_);
+ auto iter = registry_.find(name);
+ if (iter == registry_.end()) {
+ dmlc::WriteLock write_lock(m_);
+ std::unique_ptr<VTuneObject> ev(new VTuneObject(name, domain, args...));
+ iter = registry_.emplace(name, std::unique_ptr<VTuneObject>(
+ new VTuneObject(make_key(name, domain), args...))).first;
+ }
+ return iter->second.get();
+ }
+
+ private:
+ /*!
+ * \brief Make map lookup key given the name and domain of an object
+ * \param name Name of the object
+ * \param domain Domain of the object
+ * \return String key created form the name and domain names
+ */
+ static inline std::string make_key(const char *name, const VTuneDomain *domain) {
+ return std::string(domain->name()) + "::" + std::string(name);
+ }
+
+ dmlc::SharedMutex m_;
+ std::unordered_map<std::string, std::unique_ptr<VTuneObject>> registry_;
+};
+
+/*!
+ * \brief VTune Event (per-thread)
+ * \note https://software.intel.com/en-us/vtune-amplifier-help-event-api
+ * \remark This class has no dependency on the mxnet profiler
+ */
+class VTuneEvent {
+ public:
+ inline explicit VTuneEvent(const char *name) throw()
+ : itt_event_(__itt_event_create(name, strlen(name))) {}
+
+ inline void start() { __itt_event_start(itt_event_); }
+
+ inline void stop() { __itt_event_end(itt_event_); }
+
+ static VTuneRegistry<VTuneEvent> registry_;
+
+ private:
+ __itt_event itt_event_;
+};
+
+/*!
+ * \brief VTune Task (per-thread, nestable duration)
+ * \note https://software.intel.com/en-us/vtune-amplifier-help-task-api
+ * \remark This class has no dependency on the mxnet profiler
+ */
+class VTuneTask {
+ public:
+ inline VTuneTask(const char *name, VTuneDomain *domain) throw()
+ : name_(__itt_string_handle_create(name))
+ , domain_(domain) {
+ }
+
+ inline void start() { __itt_task_begin(domain()->dom(), __itt_null, __itt_null, name_); }
+
+ inline void stop() { __itt_task_end(domain()->dom()); }
+
+ inline VTuneDomain *domain() { return domain_; }
+
+ const char *name() const { return name_->strA; }
+
+ private:
+ __itt_string_handle *name_;
+ VTuneDomain *domain_;
+};
+
+/*!
+ * \brief Frame is a per-process time period with begin and end points (ie video frame)
+ * \note https://software.intel.com/en-us/vtune-amplifier-help-frame-api
+ * \remark This class has no dependency on the mxnet profiler
+ */
+class VTuneFrame {
+ public:
+ inline explicit VTuneFrame(VTuneDomain *domain) throw()
+ : domain_(domain) {
+ id_ = __itt_id_make(this, 0);
+ __itt_id_create(domain->dom(), id_);
+ }
+
+ ~VTuneFrame() {
+ __itt_id_destroy(domain_->dom(), id_);
+ }
+
+#ifdef MXNET_VTUNE_FRAME_GENERATE_ID
+ inline void start() { __itt_frame_begin_v3(domain()->dom(), nullptr); }
+ inline void stop() { __itt_frame_end_v3(domain()->dom(), nullptr); }
+#else
+
+ inline void start() { __itt_frame_begin_v3(domain()->dom(), &id_); }
+
+ inline void stop() { __itt_frame_end_v3(domain()->dom(), &id_); }
+
+#endif
+
+ inline operator __itt_id *() { return &id_; }
+
+ inline VTuneDomain *domain() { return domain_; }
+
+ private:
+ __itt_id id_;
+ VTuneDomain *domain_;
+};
+
+/*!
+ * \brief VTune Counter object
+ * \note https://software.intel.com/en-us/vtune-amplifier-help-frame-api
+ * \remark This class has no dependency on the mxnet profiler
+ */
+class VTuneCounter {
+ public:
+ inline VTuneCounter(const char *name, VTuneDomain *domain) throw()
+ : name_(name)
+ , domain_(domain)
+ , counter_(__itt_counter_create(name, domain->name())) {
+ CHECK_NOTNULL(counter_);
+ }
+
+ inline ~VTuneCounter() {
+ __itt_counter_destroy(counter_);
+ }
+
+ inline void operator++() { __itt_counter_inc_delta(counter_, 1); }
+
+ inline void operator++(int) { __itt_counter_inc_delta(counter_, 1); }
+
+ inline void operator--() { __itt_counter_dec_delta(counter_, 1); }
+
+ inline void operator--(int) { __itt_counter_dec_delta(counter_, 1); }
+
+ inline void operator+=(int64_t v) {
+ if (v > 0) {
+ __itt_counter_inc_delta(counter_, v);
+ } else if (v < 0) {
+ __itt_counter_dec_delta(counter_, v);
+ }
+ }
+
+ inline void operator-=(int64_t v) { this->operator+=(-v); }
+
+ inline VTuneCounter &operator=(uint64_t v) {
+ __itt_counter_set_value(counter_, &v);
+ return *this;
+ }
+
+ private:
+ const char *name_;
+ VTuneDomain *domain_;
+ __itt_counter counter_;
+};
+
+/*!
+ * \brief VTune single instant-in-time marker
+ * \remark This class has no dependency on the mxnet profiler
+ */
+class VTuneInstantMarker {
+ public:
+ inline VTuneInstantMarker(const char *name,
+ VTuneDomain *domain,
+ __itt_scope scope = __itt_scope_global) throw()
+ : name_(__itt_string_handle_create(name))
+ , domain_(domain)
+ , scope_(scope) {
+ }
+
+ void signal() { __itt_marker(domain_->dom(), __itt_null, name_, scope_); }
+
+ private:
+ __itt_string_handle *name_;
+ VTuneDomain *domain_;
+ __itt_scope scope_;
+};
+
+#endif // MXNET_USE_VTUNE
+
+} // namespace vtune
+} // namespace profiler
+} // namespace mxnet
+
+#endif // MXNET_PROFILER_VTUNE_H_
diff --git a/src/storage/pooled_storage_manager.h b/src/storage/pooled_storage_manager.h
index 4916bf1..3bf4373 100644
--- a/src/storage/pooled_storage_manager.h
+++ b/src/storage/pooled_storage_manager.h
@@ -68,6 +68,7 @@ class GPUPooledStorageManager final : public StorageManager {
DirectFreeNoLock(handle);
}
+ private:
void DirectFreeNoLock(Storage::Handle handle) {
cudaError_t err = cudaFree(handle.dptr);
size_t size = handle.size + NDEV;
diff --git a/src/storage/storage.cc b/src/storage/storage.cc
index ce40daa..e5c4e99 100644
--- a/src/storage/storage.cc
+++ b/src/storage/storage.cc
@@ -21,17 +21,14 @@
* Copyright (c) 2015 by Contributors
*/
#include <mxnet/storage.h>
-#include <mshadow/tensor.h>
-#include <dmlc/logging.h>
-#include <array>
#include "./storage_manager.h"
#include "./naive_storage_manager.h"
#include "./pooled_storage_manager.h"
#include "./cpu_shared_storage_manager.h"
#include "./cpu_device_storage.h"
#include "./pinned_memory_storage.h"
-#include "../common/cuda_utils.h"
#include "../common/lazy_alloc_array.h"
+#include "../profiler/storage_profiler.h"
namespace mxnet {
@@ -47,7 +44,6 @@ class StorageImpl : public Storage {
private:
static constexpr size_t kMaxNumberOfDevices = Context::kMaxDevType + 1;
- static constexpr size_t kMaxNumberOfDeviceIDs = Context::kMaxDevID + 1;
#if MXNET_USE_CUDA
static int num_gpu_device;
#endif // MXNET_USE_CUDA
@@ -72,6 +68,9 @@ class StorageImpl : public Storage {
// internal storage managers
std::array<common::LazyAllocArray<storage::StorageManager>,
kMaxNumberOfDevices> storage_managers_;
+#if MXNET_USE_PROFILER
+ storage::DeviceStorageProfiler profiler_;
+#endif // MXNET_USE_PROFILER
}; // struct Storage::Impl
#if MXNET_USE_CUDA
int StorageImpl::num_gpu_device = 0;
@@ -126,6 +125,9 @@ void StorageImpl::Alloc(Storage::Handle* handle) {
this->ActivateDevice(handle->ctx);
manager->Alloc(handle);
+#if MXNET_USE_PROFILER
+ profiler_.OnAlloc(*handle);
+#endif // MXNET_USE_PROFILER
}
void StorageImpl::Free(Storage::Handle handle) {
@@ -138,6 +140,9 @@ void StorageImpl::Free(Storage::Handle handle) {
});
this->ActivateDevice(ctx);
manager->Free(handle);
+#if MXNET_USE_PROFILER
+ profiler_.OnFree(handle);
+#endif // MXNET_USE_PROFILER
}
void StorageImpl::DirectFree(Storage::Handle handle) {
@@ -150,6 +155,9 @@ void StorageImpl::DirectFree(Storage::Handle handle) {
});
this->ActivateDevice(ctx);
manager->DirectFree(handle);
+#if MXNET_USE_PROFILER
+ profiler_.OnFree(handle);
+#endif // MXNET_USE_PROFILER
}
void StorageImpl::SharedIncrementRefCount(Storage::Handle handle) {
diff --git a/tests/cpp/include/test_core_op.h b/tests/cpp/include/test_core_op.h
index e85454c..63f5c91 100644
--- a/tests/cpp/include/test_core_op.h
+++ b/tests/cpp/include/test_core_op.h
@@ -26,6 +26,7 @@
#include <string>
#include <map>
#include "./test_op.h"
+#include "profiler/vtune.h"
#include "../../../src/imperative/imperative_utils.h"
namespace mxnet {
@@ -558,7 +559,7 @@ class CoreOpExecutor : public test::op::OperatorDataInitializer<DType>
inline void forward(const size_t count) {
perf::TimingItem timeF(&OperatorExecutorTiming::GetTiming(), kForward, "Forward", count);
- VTuneResume profile;
+ mxnet::profiler::vtune::VTuneResume profile;
for (size_t i = 0; i < count; ++i) {
Execute();
}
@@ -567,7 +568,7 @@ class CoreOpExecutor : public test::op::OperatorDataInitializer<DType>
inline void backward(const size_t count) {
CHECK(HasBackward());
perf::TimingItem timeF(&OperatorExecutorTiming::GetTiming(), kBackward, "Backward", count);
- VTuneResume profile;
+ mxnet::profiler::vtune::VTuneResume profile;
for (size_t i = 0; i < count; ++i) {
ExecuteBackward();
}
diff --git a/tests/cpp/include/test_legacy_op.h b/tests/cpp/include/test_legacy_op.h
index e4c5b3e..3e395a4 100644
--- a/tests/cpp/include/test_legacy_op.h
+++ b/tests/cpp/include/test_legacy_op.h
@@ -44,6 +44,7 @@
#include <map>
#include <vector>
#include <list>
+#include "profiler/vtune.h"
#include "../../../include/mxnet/operator.h"
#include "./test_op.h"
#include "./test_op_runner.h"
@@ -184,7 +185,7 @@ class LegacyOperatorExecutor : public OperatorDataInitializer<DType>
perf::TimingItem timeF(&OperatorExecutorTiming::GetTiming(), Forward,
"Forward", count);
if (!isGPU_) {
- VTuneResume profile; // VTune sample only this scope
+ mxnet::profiler::vtune::VTuneResume profile; // VTune sample only this scope
for (size_t x = 0; x < count; ++x) {
op()->Forward(opContext_,
c_.blob_input_vec_,
@@ -212,7 +213,7 @@ class LegacyOperatorExecutor : public OperatorDataInitializer<DType>
perf::TimingItem timeB(&OperatorExecutorTiming::GetTiming(), Backward,
"Backward", count);
if (!isGPU_) {
- VTuneResume profile; // VTune sample only this scope
+ mxnet::profiler::vtune::VTuneResume profile; // VTune sample only this scope
for (size_t x = 0; x < count; ++x) {
op()->Backward(opContext_,
c_.blob_out_grad_,
diff --git a/tests/cpp/include/test_perf.h b/tests/cpp/include/test_perf.h
index 8d2a264..62798d7 100644
--- a/tests/cpp/include/test_perf.h
+++ b/tests/cpp/include/test_perf.h
@@ -110,7 +110,7 @@ class TimedScope {
}
}
- explicit inline TimedScope(const std::string& msg, size_t count = 1, const bool start = true)
+ explicit inline TimedScope(const std::string &msg, size_t count = 1, const bool start = true)
: startTime_(start ? getMicroTickCount() : 0)
, count_(count) {
CHECK_NE(count, 0U);
@@ -163,7 +163,7 @@ class TimingInstrument {
: name_(name) {
}
void startTiming(int id, const char *s) {
- std::unique_lock<std::recursive_mutex> lk(mutex_);
+ std::unique_lock<std::recursive_mutex> lk(mutex_);
auto i = data_.find(id);
if (i == data_.end()) {
i = data_.emplace(std::make_pair(id, Info(s))).first;
@@ -173,7 +173,7 @@ class TimingInstrument {
}
}
void stopTiming(int id, const size_t subIterationCount = 1) {
- std::unique_lock<std::recursive_mutex> lk(mutex_);
+ std::unique_lock<std::recursive_mutex> lk(mutex_);
auto i = data_.find(id);
CHECK_NE(i == data_.end(), true) << "Can't stop timing on an object that we don't know about";
if (i != data_.end()) {
@@ -181,16 +181,16 @@ class TimingInstrument {
if (!--i->second.nestingCount_) {
CHECK_NE(i->second.baseTime_, 0U) << "Invalid base time";
i->second.duration_.fetch_add(getMicroTickCount() - i->second.baseTime_);
- i->second.baseTime_ = 0;
+ i->second.baseTime_ = 0;
i->second.cycleCount_.fetch_add(subIterationCount);
}
}
}
uint64_t getDuration(int id) {
- std::unique_lock<std::recursive_mutex> lk(mutex_);
+ std::unique_lock<std::recursive_mutex> lk(mutex_);
auto i = data_.find(id);
if (i != data_.end()) {
- const Info& info = i->second;
+ const Info &info = i->second;
const uint64_t duration = info.nestingCount_.load()
? info.duration_.load() +
(getMicroTickCount() - info.baseTime_.load())
@@ -207,23 +207,23 @@ class TimingInstrument {
return false;
}
- template <typename StreamType>
- void print(StreamType *os, const std::string& label_, bool doReset = false) {
- std::unique_lock<std::recursive_mutex> lk(mutex_);
+ template<typename StreamType>
+ void print(StreamType *os, const std::string &label_, bool doReset = false) {
+ std::unique_lock<std::recursive_mutex> lk(mutex_);
// Sorted output
std::map<int, Info> data(data_.begin(), data_.end());
for (std::map<int, Info>::const_iterator i = data.begin(), e = data.end();
- i != e; ++i) {
- const Info& info = i->second;
+ i != e; ++i) {
+ const Info &info = i->second;
const uint64_t duration = getDuration(i->first);
*os << label_ << ": " << name_ << " Timing [" << info.name_ << "] "
<< (info.nestingCount_.load() ? "*" : "")
<< MICRO2MSF(duration) << " ms";
- if (info.cycleCount_.load()) {
- *os << ", avg: " << (MICRO2MSF(duration) / info.cycleCount_)
- << " ms X " << info.cycleCount_ << " passes";
- }
- *os << std::endl;
+ if (info.cycleCount_.load()) {
+ *os << ", avg: " << (MICRO2MSF(duration) / info.cycleCount_)
+ << " ms X " << info.cycleCount_ << " passes";
+ }
+ *os << std::endl;
}
*os << std::flush;
if (doReset) {
@@ -232,9 +232,9 @@ class TimingInstrument {
}
void reset() {
- std::unique_lock<std::recursive_mutex> lk(mutex_);
+ std::unique_lock<std::recursive_mutex> lk(mutex_);
for (auto i = data_.begin(), e = data_.end();
- i != e; ++i) {
+ i != e; ++i) {
const int id = i->first;
const bool wasTiming = isTiming(id);
if (wasTiming) {
@@ -249,14 +249,14 @@ class TimingInstrument {
}
}
- TimingInstrument& operator += (const TimingInstrument& o) {
+ TimingInstrument &operator+=(const TimingInstrument &o) {
for (auto i = o.data_.begin(), e = o.data_.end();
- i != e; ++i) {
+ i != e; ++i) {
auto j = data_.find(i->first);
- if (j != data_.end()) {
+ if (j != data_.end()) {
const Info &oInfo = i->second;
CHECK_EQ(oInfo.nestingCount_, 0U);
- j->second.duration_ += oInfo.duration_;
+ j->second.duration_ += oInfo.duration_;
j->second.cycleCount_ += oInfo.cycleCount_;
} else {
data_.insert(std::make_pair(i->first, i->second));
@@ -308,13 +308,13 @@ class TimingInstrument {
typedef std::unordered_map<int, TimingInstrument::Info> timing_map_t;
- const timing_map_t& data() const {
+ const timing_map_t &data() const {
return data_;
}
private:
- std::string name_;
- mutable std::recursive_mutex mutex_;
+ std::string name_;
+ mutable std::recursive_mutex mutex_;
std::unordered_map<int, Info> data_;
};
@@ -342,8 +342,8 @@ class TimingItem {
private:
TimingInstrument *ti_;
- const int id_;
- const size_t subIterationCount_;
+ const int id_;
+ const size_t subIterationCount_;
};
diff --git a/tests/cpp/include/test_util.h b/tests/cpp/include/test_util.h
index 1e4faeb..a755698 100644
--- a/tests/cpp/include/test_util.h
+++ b/tests/cpp/include/test_util.h
@@ -49,35 +49,6 @@ extern bool quick_test;
extern bool performance_run;
extern bool csv;
-/*! \brief Pause VTune analysis */
-struct VTunePause {
- inline VTunePause() {
-#if MXNET_USE_VTUNE
- __itt_pause();
-#endif
- }
- inline ~VTunePause() {
-#if MXNET_USE_VTUNE
- __itt_resume();
-#endif
- }
-};
-
-/*! \brief Resume VTune analysis */
-struct VTuneResume {
- inline VTuneResume() {
-#if MXNET_USE_VTUNE
- __itt_resume();
-#endif
- }
- inline ~VTuneResume() {
-#if MXNET_USE_VTUNE
- __itt_pause();
-#endif
- }
-};
-
-
template<typename DType>
inline size_t shapeMemorySize(const TShape& shape) {
return shape.Size() * sizeof(DType);
diff --git a/tests/cpp/misc/memory_test.cc b/tests/cpp/misc/memory_test.cc
index 0e53137..8d789c1 100644
--- a/tests/cpp/misc/memory_test.cc
+++ b/tests/cpp/misc/memory_test.cc
@@ -51,7 +51,7 @@ static int GetOMPThreadCount() {
*/
TEST(MEMORY_TEST, MemsetAndMemcopyPerformance) {
const size_t GB = 1000000000; // memset never slower
- uint64_t base = 100000;
+ uint64_t base = 10000;
std::list<uint64_t> memset_times, omp_set_times, memcpy_times, omp_copy_times;
size_t pass = 0;
do {
diff --git a/tests/python/unittest/test_profiler.py b/tests/python/unittest/test_profiler.py
index 78baf4a..b3a9a70 100644
--- a/tests/python/unittest/test_profiler.py
+++ b/tests/python/unittest/test_profiler.py
@@ -16,18 +16,33 @@
# under the License.
from __future__ import print_function
+import logging
import mxnet as mx
from mxnet import profiler
import time
+import os
+
+def enable_profiler(run=True, continuous_dump=False, aggregate_stats=False):
+ profile_filename = 'test_profile.json'
+ profiler.set_config(profile_symbolic=True,
+ profile_imperative=True,
+ profile_memory=True,
+ profile_api=True,
+ filename=profile_filename,
+ continuous_dump=continuous_dump,
+ aggregate_stats=aggregate_stats
+ )
+ print('profile file save to {}'.format(profile_filename))
+ if run is True:
+ profiler.set_state('run')
+
def test_profiler():
- profile_filename = "test_profile.json"
iter_num = 5
begin_profiling_iter = 2
end_profiling_iter = 4
- profiler.profiler_set_config(mode='symbolic', filename=profile_filename)
- print('profile file save to {0}'.format(profile_filename))
+ enable_profiler(False, False)
A = mx.sym.Variable('A')
B = mx.sym.Variable('B')
@@ -46,10 +61,10 @@ def test_profiler():
print("Iteration {}/{}".format(i + 1, iter_num))
if i == begin_profiling_iter:
t0 = time.clock()
- profiler.profiler_set_state('run')
+ profiler.set_state('run')
if i == end_profiling_iter:
t1 = time.clock()
- profiler.profiler_set_state('stop')
+ profiler.set_state('stop')
executor.forward()
c = executor.outputs[0]
c.wait_to_read()
@@ -57,7 +72,167 @@ def test_profiler():
duration = t1 - t0
print('duration: {0}s'.format(duration))
print(' {0}ms/operator'.format(duration*1000/iter_num))
+ profiler.dump(True)
+ profiler.set_state('stop')
+
+
+def test_profile_create_domain():
+ enable_profiler()
+ domain = profiler.Domain(name='PythonDomain')
+ print("Domain created: {}".format(str(domain)))
+ profiler.set_state('stop')
+
+
+def test_profile_create_domain_dept():
+ profiler.profiler_set_config(mode='symbolic', filename='temp.json')
+ profiler.set_state('run')
+ domain = profiler.Domain(name='PythonDomain')
+ print("Domain created: {}".format(str(domain)))
profiler.dump_profile()
+ profiler.set_state('stop')
+
+def test_profile_task():
+ def makeParams():
+ objects = tuple('foo' for _ in range(50))
+ template = ''.join('{%d}' % i for i in range(len(objects)))
+ return template, objects
+
+ def doLog():
+ template, objects = makeParams()
+ for _ in range(100000):
+ logging.info(template.format(*objects))
+
+ logging.basicConfig()
+ enable_profiler()
+ python_domain = profiler.Domain('PythonDomain::test_profile_task')
+ task = profiler.Task(python_domain, "test_profile_task")
+ task.start()
+ start = time.time()
+ var = mx.nd.ones((1000, 500))
+ doLog()
+ var.asnumpy()
+ stop = time.time()
+ task.stop()
+ print('run took: %.3f' % (stop - start))
+ profiler.set_state('stop')
+
+
+def test_profile_frame():
+ def makeParams():
+ objects = tuple('foo' for _ in range(50))
+ template = ''.join('{%d}' % i for i in range(len(objects)))
+ return template, objects
+
+ def doLog():
+ template, objects = makeParams()
+ for _ in range(100000):
+ logging.info(template.format(*objects))
+
+ logging.basicConfig()
+ enable_profiler()
+ python_domain = profiler.Domain('PythonDomain::test_profile_frame')
+ frame = profiler.Frame(python_domain, "test_profile_frame")
+ frame.start()
+ start = time.time()
+ var = mx.nd.ones((1000, 500))
+ doLog()
+ var.asnumpy()
+ stop = time.time()
+ frame.stop()
+ print('run took: %.3f' % (stop - start))
+ profiler.set_state('stop')
+
+
+def test_profile_event(do_enable_profiler=True):
+ def makeParams():
+ objects = tuple('foo' for _ in range(50))
+ template = ''.join('{%d}' % i for i in range(len(objects)))
+ return template, objects
+
+ def doLog():
+ template, objects = makeParams()
+ for _ in range(100000):
+ logging.info(template.format(*objects))
+
+ logging.basicConfig()
+ if do_enable_profiler is True:
+ enable_profiler()
+ event = profiler.Event("test_profile_event")
+ event.start()
+ start = time.time()
+ var = mx.nd.ones((1000, 500))
+ doLog()
+ var.asnumpy()
+ stop = time.time()
+ event.stop()
+ print('run took: %.3f' % (stop - start))
+ if do_enable_profiler is True:
+ profiler.set_state('stop')
+
+
+def test_profile_tune_pause_resume():
+ enable_profiler()
+ profiler.pause()
+ # "test_profile_task" should *not* show up in tuning analysis
+ test_profile_task()
+ profiler.resume()
+ # "test_profile_event" should show up in tuning analysis
+ test_profile_event()
+ profiler.pause()
+ profiler.set_state('stop')
+
+
+def test_profile_counter(do_enable_profiler=True):
+ def makeParams():
+ objects = tuple('foo' for _ in range(50))
+ template = ''.join('{%d}' % i for i in range(len(objects)))
+ return template, objects
+
+ def doLog(counter):
+ template, objects = makeParams()
+ range_size = 100000
+ for i in range(range_size):
+ if i <= range_size / 2:
+ counter += 1
+ else:
+ counter -= 1
+ logging.info(template.format(*objects))
+
+ if do_enable_profiler is True:
+ enable_profiler()
+ python_domain = profiler.Domain('PythonDomain::test_profile_counter')
+ counter = profiler.Counter(python_domain, "PythonCounter::test_profile_counter")
+ counter.set_value(5)
+ counter += 1
+ start = time.time()
+ doLog(counter)
+ stop = time.time()
+ print('run took: %.3f' % (stop - start))
+ if do_enable_profiler is True:
+ profiler.set_state('stop')
+
+
+def test_continuous_profile_and_instant_marker():
+ enable_profiler(True, True, True)
+ python_domain = profiler.Domain('PythonDomain::test_continuous_profile')
+ last_file_size = 0
+ for i in range(5):
+ profiler.Marker(python_domain, "StartIteration-" + str(i)).mark('process')
+ print("{}...".format(i))
+ test_profile_event(False)
+ test_profile_counter(False)
+ profiler.dump(False)
+ # File size should keep increasing
+ new_file_size = os.path.getsize("test_profile.json")
+ assert new_file_size >= last_file_size
+ last_file_size = new_file_size
+ profiler.dump(False)
+ debug_str = profiler.dumps()
+ assert(len(debug_str) > 0)
+ print(debug_str)
+ profiler.set_state('stop')
+
if __name__ == '__main__':
- test_profiler()
+ import nose
+ nose.runmodule()
diff --git a/tools/profile/tune_mnist.sh b/tools/profile/tune_mnist.sh
new file mode 100755
index 0000000..bf395de
--- /dev/null
+++ b/tools/profile/tune_mnist.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+# 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.
+
+SCRIPTDIR=$(realpath $(dirname $0))
+TOPDIR=${SCRIPTDIR}/../../
+cd ${TOPDIR}
+/bin/bash ${SCRIPTDIR}/tune_python.sh example/image-classification/train_mnist.py --network=mlp --profile=all --num-epochs=1 $@
+
diff --git a/tools/profile/tune_python.sh b/tools/profile/tune_python.sh
new file mode 100755
index 0000000..ed4b0ac
--- /dev/null
+++ b/tools/profile/tune_python.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+# 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.
+
+SCRIPTDIR=$(realpath $(dirname $0))
+TOPDIR=$(realpath ${SCRIPTDIR}/../../)
+export LD_LIBRARY_PATH=${TOPDIR}/cmake-build-relwithdebinfo:${TOPDIR}/../cmake-build-relwithdebinfo/mxnet:$LD_LIBRARY_PATH
+export PYTHONPATH=${TOPDIR}/python:$PYTHONPATH
+cd ${TOPDIR}
+/opt/intel/vtune_amplifier_xe/bin64/amplxe-cl -collect hotspots -run-pass-thru=-timestamp=sys -knob analyze-openmp=true -knob sampling-interval=1 -knob enable-user-tasks=true -- /usr/bin/python $@
--
To stop receiving notification emails like this one, please contact
jxie@apache.org.