profiler.py 1.2 KB

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