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

 ##########
 File path: python/mxnet/profiler.py
 ##########
 @@ -56,3 +55,228 @@ 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())
+
+def create_domain(name):
+    domain_handle = ProfileHandle()
+    check_call(_LIB.MXProfileCreateDomain(c_str(name), 
ctypes.byref(domain_handle)))
+    return domain_handle
+
+def create_task(domain_handle, name):
+    task_handle = ProfileHandle()
+    check_call(_LIB.MXProfileCreateTask(domain_handle,
+                                        c_str(name),
+                                        ctypes.byref(task_handle)))
+    return task_handle
+
+def destroy_task(task_handle):
+    check_call(_LIB.MXProfileDestroyHandle(task_handle))
+
+def task_start(task_handle):
+    check_call(_LIB.MXProfileDurationStart(task_handle))
+
+def task_stop(task_handle):
+    check_call(_LIB.MXProfileDurationStop(task_handle))
+
+def create_frame(domain_handle, name):
+    frame_handle = ProfileHandle()
+    check_call(_LIB.MXProfileCreateFrame(domain_handle,
+                                         c_str(name),
+                                         ctypes.byref(frame_handle)))
+    return frame_handle
+
+def destroy_frame(frame_handle):
+    check_call(_LIB.MXProfileDestroyHandle(frame_handle))
+
+def frame_start(frame_handle):
+    check_call(_LIB.MXProfileDurationStart(frame_handle))
+
+def frame_stop(frame_handle):
+    check_call(_LIB.MXProfileDurationStop(frame_handle))
+
+def create_event(name):
+    event_handle = ProfileHandle()
+    check_call(_LIB.MXProfileCreateEvent(c_str(name), 
ctypes.byref(event_handle)))
+    return event_handle
+
+def destroy_event(event_handle):
+    check_call(_LIB.MXProfileDestroyHandle(event_handle))
+
+def event_start(event_handle):
+    check_call(_LIB.MXProfileDurationStart(event_handle))
+
+def event_stop(event_handle):
+    check_call(_LIB.MXProfileDurationStop(event_handle))
+
+def tune_pause():
+    check_call(_LIB.MXProfilePause(int(1)))
+
+def tune_resume():
+    check_call(_LIB.MXProfilePause(int(0)))
+
+def create_counter(domain_handle, name, value=None):
+    counter_handle = ProfileHandle()
+    check_call(_LIB.MXProfileCreateCounter(domain_handle,
+                                           c_str(name),
+                                           ctypes.byref(counter_handle)))
+    if value is not None:
+        set_counter(counter_handle, value)
+    return counter_handle
+
+def destroy_counter(counter_handle):
+    check_call(_LIB.MXProfileDestroyHandle(counter_handle))
+
+def set_counter(counter_handle, value):
+    check_call(_LIB.MXProfileSetCounter(counter_handle, int(value)))
+
+def increment_counter(counter_handle, by_value):
+    check_call(_LIB.MXProfileAdjustCounter(counter_handle, int(by_value)))
+
+def decrement_counter(counter_handle, by_value):
+    check_call(_LIB.MXProfileAdjustCounter(counter_handle, -int(by_value)))
+
+def set_continuous_dump(continuous_dump=True, delay_in_seconds=1.0):
+    if continuous_dump is False:
+        cd = 0
+    else:
+        cd = 1
+    ds = float(delay_in_seconds)
+    check_call(_LIB.MXSetContinuousProfileDump(ctypes.c_int(cd), 
ctypes.c_float(ds)))
+
+def set_instant_marker(domain_handle, name, scope='process'):
+    check_call(_LIB.MXProfileSetInstantMarker(domain_handle, c_str(name), 
c_str(scope)))
+
+
+class Domain:
+    """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
+    """
+    def __init__(self, name):
+        self.name = name
+        self.handle = create_domain(name)
+
+    def __str__(self):
+        return self.name
+
+
+class Task:
+    """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
+    """
+    def __init__(self, domain, name):
+        self.domain = domain
+        self.name = name
+        self.handle = create_task(domain.handle, name)
+
+    def start(self):
+        task_start(self.handle)
+
+    def stop(self):
+        task_stop(self.handle)
+
+    def __str__(self):
+        return self.name
+
+    def __del__(self):
+        if self.handle is not None:
+            destroy_task(self.handle)
+
+
+class Frame:
+    """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.
+    """
+    def __init__(self, domain, name):
+        self.domain = domain
+        self.name = name
+        self.handle = create_frame(domain.handle, name)
+
+    def start(self):
+        frame_start(self.handle)
+
+    def stop(self):
+        frame_stop(self.handle)
+
+    def __str__(self):
+        return self.name
+
+    def __del__(self):
+        if self.handle is not None:
+            destroy_frame(self.handle)
+
+
+class Event:
+    """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.
+    """
+    def __init__(self, name):
+        self.name = name
+        self.handle = create_event(name)
+
+    def start(self):
+        event_start(self.handle)
+
+    def stop(self):
+        event_stop(self.handle)
+
+    def __str__(self):
+        return self.name
+
+    def __del__(self):
+        if self.handle is not None:
+            destroy_event(self.handle)
+
+
+class Counter:
+    """Profiling Counter class
+    The counter event can track a value as it changes over time.
+    """
+    def __init__(self, domain, name, value=0):
+        self.name = name
+        self.handle = create_counter(domain.handle, name, value)
+
+    def set_value(self, value):
+        set_counter(self.handle, value)
+
+    def increment(self, value_change):
+        increment_counter(self.handle, value_change)
+
+    def decrement(self, value_change):
+        decrement_counter(self.handle, value_change)
+
+    def __iadd__(self, value_change):
+        self.increment(value_change)
+        return self
+
+    def __isub__(self, value_change):
+        self.decrement(value_change)
+        return self
+
+    def __str__(self):
+        return self.name
+
+    def __del__(self):
+        if self.handle is not None:
+            destroy_counter(self.handle)
+
+
+class InstantMarker:
+    """Set marker for an instant in time"""
+    def __init__(self, domain, name):
+        self.name = name
+        self.domain = domain
+
+    def signal(self, scope='process'):
 
 Review comment:
   `def mark()`

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


With regards,
Apache Git Services

Reply via email to