MPSEvent.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright © 2023 Apple Inc.
  2. #pragma once
  3. #include <ATen/mps/MPSStream.h>
  4. #include <ctime>
  5. #include <stack>
  6. namespace at::mps {
  7. // NOTE: don't create instances of this class directly.
  8. // Use MPSEventPool to acquire instances of MPSEvent.
  9. class MPSEvent {
  10. public:
  11. explicit MPSEvent(id_t ID, MPSStream* stream, bool enable_timing);
  12. ~MPSEvent();
  13. // records an event on the stream
  14. void record(bool needsLock, bool syncEvent = false);
  15. // makes all future work submitted to the stream wait for this event.
  16. bool wait(bool needsLock, bool syncEvent = false);
  17. // schedules a notifyListener callback for the event.
  18. bool notify(bool needsLock, MTLSharedEventNotificationBlock block);
  19. // checks if events are already signaled.
  20. bool query() const;
  21. // blocks the CPU thread until all the GPU work that were scheduled
  22. // prior to recording this event are completed.
  23. bool synchronize();
  24. // resets this event with new parameters in case it gets reused from the event
  25. // pool
  26. void reset(MPSStream* stream, bool enable_timing);
  27. // returns the unique ID of the event instance
  28. id_t getID() const {
  29. return m_id;
  30. }
  31. // returns the completion timestamp of the event
  32. uint64_t getCompletionTime() const {
  33. return m_completion_time;
  34. }
  35. // if already recorded, waits for cpu_sync_cv to be signaled
  36. void waitForCpuSync();
  37. private:
  38. id_t m_id;
  39. // enables measuring the completion time of the notifyListener of this event
  40. bool m_enable_timing;
  41. uint64_t m_signalCounter = 0;
  42. MPSStream* m_stream = nullptr;
  43. MTLSharedEvent_t m_event = nullptr;
  44. MTLSharedEventListener* m_listener = nullptr;
  45. // used to sync the events created on this Stream with CPU
  46. std::mutex m_cpu_sync_mutex{};
  47. std::condition_variable m_cpu_sync_cv{};
  48. // CondVar predicate to sync the events created on this Stream with CPU
  49. bool m_cpu_sync_completed = false;
  50. // used to compute elapsed time
  51. uint64_t m_completion_time = 0;
  52. void recordLocked(bool syncEvent);
  53. bool waitLocked(bool syncEvent);
  54. bool notifyLocked(MTLSharedEventNotificationBlock block);
  55. void notifyCpuSync();
  56. static uint64_t getTime() {
  57. return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
  58. }
  59. };
  60. typedef std::unique_ptr<MPSEvent, std::function<void(MPSEvent*)>> MPSEventPtr;
  61. class MPSEventPool {
  62. public:
  63. explicit MPSEventPool(MPSStream* default_stream);
  64. ~MPSEventPool();
  65. MPSEventPtr acquireEvent(bool enable_timing, MPSStream* stream);
  66. void emptyCache();
  67. // these are mainly used for MPSHooks and torch.mps.Event() bindings
  68. id_t acquireEvent(bool enable_timing);
  69. void releaseEvent(id_t event_id);
  70. void recordEvent(id_t event_id, bool syncEvent);
  71. void waitForEvent(id_t event_id, bool syncEvent);
  72. void synchronizeEvent(id_t event_id);
  73. bool queryEvent(id_t event_id);
  74. // returns elapsed time between two recorded events in milliseconds
  75. double elapsedTime(id_t start_event_id, id_t end_event_id);
  76. private:
  77. MPSStream* m_default_stream = nullptr;
  78. std::recursive_mutex m_mutex;
  79. std::stack<std::unique_ptr<MPSEvent>> m_pool{};
  80. // dictionary to associate event IDs with event objects
  81. // used to retain in-use events out of the pool
  82. // for torch.mps.Event() bindings.
  83. std::unordered_map<id_t, MPSEventPtr> m_in_use_events{};
  84. uint64_t m_event_counter = 0;
  85. std::function<void(MPSEvent*)> m_default_deleter;
  86. MPSEvent* getInUseEvent(id_t event_id, bool locked = true);
  87. };
  88. // shared_ptr is used to get MPSEventPool destroyed after dependent instances
  89. std::shared_ptr<MPSEventPool> getMPSEventPool();
  90. } // namespace at::mps