profiler.py 1.9 KB
Newer Older
D
dangqingqing 已提交
1
import paddle.v2.fluid.core as core
2
import subprocess
D
dangqingqing 已提交
3

4
__all__ = ['CudaProfiler']
D
dangqingqing 已提交
5

6 7 8 9 10 11 12 13 14 15 16 17
NV_FLAGS = [
    "gpustarttimestamp",
    "gpuendtimestamp",
    "gridsize3d",
    "threadblocksize",
    "streamid",
    "enableonstart 0",
    "conckerneltrace",
]


def nvporf_init(output_file, output_mode=None, flags=None):
D
dangqingqing 已提交
18 19 20 21 22 23 24 25
    """
    Initialize the CUDA profiler.
    This methods must be called before nvprof_start.

    :param output_file: The output file name.
    :type output_file: string
    :param output_mode: The output mode has Key-Value pair format and
                        Comma separated values format.
26
                        It should be 'kv' or 'csv'.
D
dangqingqing 已提交
27 28 29 30
    :type output_mode: string
    """
    if output_mode is None:
        output_mode = 'csv'
31
    if output_mode not in ['kv', 'csv']:
D
dangqingqing 已提交
32
        raise ValueError("The output mode must be 'key-value' or 'csv'.")
33 34
    flags = NV_FLAGS if flags is None else flags
    core.nvprof_init(output_file, output_mode, flags)
D
dangqingqing 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50


def nvporf_start():
    """
    Enables profiler collection by the active CUDA profiling tool.
    """
    core.nvprof_start()


def nvporf_stop():
    """
    Disables profiler collection.
    """
    core.nvprof_stop()


51 52
class CudaProfiler(object):
    def __init__(self, output_file, output_mode=None, flags=None, enabled=True):
D
dangqingqing 已提交
53 54 55 56
        self.enabled = enabled
        if not self.enabled:
            return
        self.entered = False
57 58
        self.out_file = output_file
        nvporf_init(output_file, output_mode, flags)
D
dangqingqing 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    def __enter__(self):
        if not self.enabled:
            return
        if self.entered:
            raise RuntimeError("The profiler traces are not reentrant")
        self.entered = True
        nvporf_start()
        return self

    def __exit__(self, exc_type, exc_value, tb):
        if exc_value is not None:
            raise exc_value
        if not self.enabled:
            return
        nvporf_stop()