MPSEvent.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 pool
  25. void reset(MPSStream* stream, bool enable_timing);
  26. // returns the unique ID of the event instance
  27. id_t getID() const { return m_id; }
  28. // returns the completion timestamp of the event
  29. uint64_t getCompletionTime() const { return m_completion_time; }
  30. // if already recorded, waits for cpu_sync_cv to be signaled
  31. void waitForCpuSync();
  32. private:
  33. id_t m_id;
  34. // enables measuring the completion time of the notifyListener of this event
  35. bool m_enable_timing;
  36. uint64_t m_signalCounter = 0;
  37. MPSStream* m_stream = nullptr;
  38. MTLSharedEvent_t m_event = nullptr;
  39. MTLSharedEventListener* m_listener = nullptr;
  40. // used to sync the events created on this Stream with CPU
  41. std::mutex m_cpu_sync_mutex{};
  42. std::condition_variable m_cpu_sync_cv{};
  43. // CondVar predicate to sync the events created on this Stream with CPU
  44. bool m_cpu_sync_completed = false;
  45. // used to compute elapsed time
  46. uint64_t m_completion_time = 0;
  47. void recordLocked(bool syncEvent);
  48. bool waitLocked(bool syncEvent);
  49. bool notifyLocked(MTLSharedEventNotificationBlock block);
  50. void notifyCpuSync();
  51. static uint64_t getTime() {
  52. return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
  53. }
  54. };
  55. typedef std::unique_ptr<MPSEvent, std::function<void(MPSEvent*)>> MPSEventPtr;
  56. class MPSEventPool {
  57. public:
  58. explicit MPSEventPool(MPSStream* default_stream);
  59. ~MPSEventPool();
  60. MPSEventPtr acquireEvent(bool enable_timing, MPSStream* stream);
  61. void emptyCache();
  62. // these are mainly used for MPSHooks and torch.mps.Event() bindings
  63. id_t acquireEvent(bool enable_timing);
  64. void releaseEvent(id_t event_id);
  65. void recordEvent(id_t event_id, bool syncEvent);
  66. void waitForEvent(id_t event_id, bool syncEvent);
  67. void synchronizeEvent(id_t event_id);
  68. bool queryEvent(id_t event_id);
  69. // returns elapsed time between two recorded events in milliseconds
  70. double elapsedTime(id_t start_event_id, id_t end_event_id);
  71. private:
  72. MPSStream* m_default_stream = nullptr;
  73. std::recursive_mutex m_mutex;
  74. std::stack<std::unique_ptr<MPSEvent>> m_pool{};
  75. // dictionary to associate event IDs with event objects
  76. // used to retain in-use events out of the pool
  77. // for torch.mps.Event() bindings.
  78. std::unordered_map<id_t, MPSEventPtr> m_in_use_events{};
  79. uint64_t m_event_counter = 0;
  80. std::function<void(MPSEvent*)> m_default_deleter;
  81. MPSEvent* getInUseEvent(id_t event_id, bool locked = true);
  82. };
  83. // shared_ptr is used to get MPSEventPool destroyed after dependent instances
  84. std::shared_ptr<MPSEventPool> getMPSEventPool();
  85. } // namespace at::mps