profiler.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import tempfile
  2. import contextlib
  3. from . import cudart, check_error
  4. DEFAULT_FLAGS = [
  5. "gpustarttimestamp",
  6. "gpuendtimestamp",
  7. "gridsize3d",
  8. "threadblocksize",
  9. "streamid",
  10. "enableonstart 0",
  11. "conckerneltrace",
  12. ]
  13. def init(output_file, flags=None, output_mode='key_value'):
  14. rt = cudart()
  15. if not hasattr(rt, 'cudaOutputMode'):
  16. raise AssertionError("HIP does not support profiler initialization!")
  17. flags = DEFAULT_FLAGS if flags is None else flags
  18. if output_mode == 'key_value':
  19. output_mode_enum = rt.cudaOutputMode.KeyValuePair
  20. elif output_mode == 'csv':
  21. output_mode_enum = rt.cudaOutputMode.CSV
  22. else:
  23. raise RuntimeError("supported CUDA profiler output modes are: key_value and csv")
  24. with tempfile.NamedTemporaryFile(delete=True) as f:
  25. f.write(b'\n'.join(f.encode('ascii') for f in flags))
  26. f.flush()
  27. check_error(rt.cudaProfilerInitialize(f.name, output_file, output_mode_enum))
  28. def start():
  29. check_error(cudart().cudaProfilerStart())
  30. def stop():
  31. check_error(cudart().cudaProfilerStop())
  32. @contextlib.contextmanager
  33. def profile():
  34. try:
  35. start()
  36. yield
  37. finally:
  38. stop()