|
1 | 1 | import paddle.v2.fluid.core as core
|
2 |
| -import subprocess |
| 2 | +from contextlib import contextmanager |
3 | 3 |
|
4 | 4 | __all__ = ['CudaProfiler']
|
5 | 5 |
|
6 |
| -NV_FLAGS = [ |
| 6 | +NVPROF_CONFIG = [ |
7 | 7 | "gpustarttimestamp",
|
8 | 8 | "gpuendtimestamp",
|
9 | 9 | "gridsize3d",
|
|
14 | 14 | ]
|
15 | 15 |
|
16 | 16 |
|
17 |
| -def nvporf_init(output_file, output_mode=None, flags=None): |
18 |
| - """ |
19 |
| - Initialize the CUDA profiler. |
20 |
| - This methods must be called before nvprof_start. |
21 |
| -
|
22 |
| - :param output_file: The output file name. |
23 |
| - :type output_file: string |
24 |
| - :param output_mode: The output mode has Key-Value pair format and |
25 |
| - Comma separated values format. |
26 |
| - It should be 'kv' or 'csv'. |
27 |
| - :type output_mode: string |
| 17 | +@contextmanager |
| 18 | +def cuda_profiler(output_file, output_mode=None, config=None): |
| 19 | + """The CUDA profiler. |
| 20 | + This fuctions is used to profile CUDA program by CUDA runtime application |
| 21 | + programming interface. The profiling result will be written into |
| 22 | + `output_file` with Key-Value pair format or Comma separated values format. |
| 23 | + The user can set the output mode by `output_mode` argument and set the |
| 24 | + counters/options for profiling by `config` argument. The default config |
| 25 | + caontains 'gpustarttimestamp', 'gpustarttimestamp', 'gridsize3d', |
| 26 | + 'threadblocksize', 'streamid', 'enableonstart 0', 'conckerneltrace'. |
| 27 | +
|
| 28 | + Args: |
| 29 | + output_file (string) : The output file name, the result will be |
| 30 | + written into this file. |
| 31 | + output_mode (string) : The output mode has Key-Value pair format and |
| 32 | + Comma separated values format. It should be 'kv' or 'csv'. |
| 33 | + config (string) : The profiler options and counters can refer to |
| 34 | + "Compute Command Line Profiler User Guide". |
28 | 35 | """
|
29 | 36 | if output_mode is None:
|
30 | 37 | output_mode = 'csv'
|
31 | 38 | if output_mode not in ['kv', 'csv']:
|
32 | 39 | raise ValueError("The output mode must be 'key-value' or 'csv'.")
|
33 |
| - flags = NV_FLAGS if flags is None else flags |
34 |
| - core.nvprof_init(output_file, output_mode, flags) |
35 |
| - |
36 |
| - |
37 |
| -def nvporf_start(): |
38 |
| - """ |
39 |
| - Enables profiler collection by the active CUDA profiling tool. |
40 |
| - """ |
| 40 | + config = NVPROF_CONFIG if config is None else config |
| 41 | + core.nvprof_init(output_file, output_mode, config) |
| 42 | + # Enables profiler collection by the active CUDA profiling tool. |
41 | 43 | core.nvprof_start()
|
42 |
| - |
43 |
| - |
44 |
| -def nvporf_stop(): |
45 |
| - """ |
46 |
| - Disables profiler collection. |
47 |
| - """ |
| 44 | + yield |
| 45 | + # Disables profiler collection. |
48 | 46 | core.nvprof_stop()
|
49 |
| - |
50 |
| - |
51 |
| -class CudaProfiler(object): |
52 |
| - def __init__(self, output_file, output_mode=None, flags=None, enabled=True): |
53 |
| - self.enabled = enabled |
54 |
| - if not self.enabled: |
55 |
| - return |
56 |
| - self.entered = False |
57 |
| - self.out_file = output_file |
58 |
| - nvporf_init(output_file, output_mode, flags) |
59 |
| - |
60 |
| - def __enter__(self): |
61 |
| - if not self.enabled: |
62 |
| - return |
63 |
| - if self.entered: |
64 |
| - raise RuntimeError("The profiler traces are not reentrant") |
65 |
| - self.entered = True |
66 |
| - nvporf_start() |
67 |
| - return self |
68 |
| - |
69 |
| - def __exit__(self, exc_type, exc_value, tb): |
70 |
| - if exc_value is not None: |
71 |
| - raise exc_value |
72 |
| - if not self.enabled: |
73 |
| - return |
74 |
| - nvporf_stop() |
0 commit comments