Caffe2 - Python API
A deep learning, cross platform ML framework
profiler.py
1 import ctypes
2 import tempfile
3 import contextlib
4 from . import cudart, check_error
5 
6 
7 class cudaOutputMode(object):
8  cudaKeyValuePair = ctypes.c_int(0)
9  cudaCSV = ctypes.c_int(1)
10 
11  @staticmethod
12  def for_key(key):
13  if key == 'key_value':
14  return cudaOutputMode.cudaKeyValuePair
15  elif key == 'csv':
16  return cudaOutputMode.cudaCSV
17  else:
18  raise RuntimeError("supported CUDA profiler output modes are: key_value and csv")
19 
20 DEFAULT_FLAGS = [
21  "gpustarttimestamp",
22  "gpuendtimestamp",
23  "gridsize3d",
24  "threadblocksize",
25  "streamid",
26  "enableonstart 0",
27  "conckerneltrace",
28 ]
29 
30 
31 def init(output_file, flags=None, output_mode='key_value'):
32  flags = DEFAULT_FLAGS if flags is None else flags
33  output_mode = cudaOutputMode.for_key(output_mode)
34  with tempfile.NamedTemporaryFile(delete=True) as f:
35  f.write(b'\n'.join(map(lambda f: f.encode('ascii'), flags)))
36  f.flush()
37  check_error(cudart().cudaProfilerInitialize(
38  ctypes.c_char_p(f.name.encode('ascii')), ctypes.c_char_p(output_file.encode('ascii')), output_mode))
39 
40 
41 def start():
42  check_error(cudart().cudaProfilerStart())
43 
44 
45 def stop():
46  check_error(cudart().cudaProfilerStop())
47 
48 
49 @contextlib.contextmanager
50 def profile():
51  try:
52  start()
53  yield
54  finally:
55  stop()