profiler.py 2.5 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
D
dangqingqing 已提交
14
import paddle.v2.fluid.core as core
D
dangqingqing 已提交
15
from contextlib import contextmanager
16
import os
D
dangqingqing 已提交
17

18
__all__ = ['CudaProfiler']
D
dangqingqing 已提交
19

D
dangqingqing 已提交
20
NVPROF_CONFIG = [
21 22 23 24 25 26
    "gpustarttimestamp",
    "gpuendtimestamp",
    "gridsize3d",
    "threadblocksize",
    "streamid",
    "enableonstart 0",
D
dangqingqing 已提交
27
    "conckerneltrace",
28 29 30
]


D
dangqingqing 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
@contextmanager
def cuda_profiler(output_file, output_mode=None, config=None):
    """The CUDA profiler.
    This fuctions is used to profile CUDA program by CUDA runtime application
    programming interface. The profiling result will be written into
    `output_file` with Key-Value pair format or Comma separated values format.
    The user can set the output mode by `output_mode` argument and set the
    counters/options for profiling by `config` argument. The default config
    is ['gpustarttimestamp', 'gpustarttimestamp', 'gridsize3d',
    'threadblocksize', 'streamid', 'enableonstart 0', 'conckerneltrace'].

    Args:
        output_file (string) : The output file name, the result will be
            written into this file.
        output_mode (string) : The output mode has Key-Value pair format and
            Comma separated values format. It should be 'kvp' or 'csv'.
47 48
        config (list of string) : The profiler options and counters can refer
            to "Compute Command Line Profiler User Guide".
D
dangqingqing 已提交
49 50 51
    """
    if output_mode is None:
        output_mode = 'csv'
D
dangqingqing 已提交
52 53 54
    if output_mode not in ['kvp', 'csv']:
        raise ValueError("The output mode must be 'kvp' or 'csv'.")
    config = NVPROF_CONFIG if config is None else config
55 56 57 58
    config_file = 'nvprof_config_file'
    with open(config_file, 'wb') as fp:
        fp.writelines(["%s\n" % item for item in config])
    core.nvprof_init(output_file, output_mode, config_file)
D
dangqingqing 已提交
59
    # Enables profiler collection by the active CUDA profiling tool.
D
dangqingqing 已提交
60
    core.nvprof_start()
D
dangqingqing 已提交
61 62
    yield
    # Disables profiler collection.
D
dangqingqing 已提交
63
    core.nvprof_stop()
64
    os.remove(config_file)