bkietz commented on code in PR #37040:
URL: https://github.com/apache/arrow/pull/37040#discussion_r1287279624


##########
cpp/src/arrow/device.h:
##########
@@ -105,6 +107,63 @@ class ARROW_EXPORT Device : public 
std::enable_shared_from_this<Device>,
   bool is_cpu_;
 };
 
+/// \brief EXPERIMENTAL: An object that provides event/stream sync primitives
+///
+///
+class ARROW_EXPORT DeviceSync {
+ public:
+  explicit DeviceSync(void* sync_event) : sync_event_{sync_event}, 
owns_event_{false} {}
+  virtual ~DeviceSync() = default;

Review Comment:
   The compiler-generated default constructor won't call release_event; I think 
this destructor should probably do that
   ```suggestion
     virtual ~DeviceSync() {
       if (owns_event_) {
         release_event(sync_event_);
       }
     }
   ```



##########
cpp/src/arrow/device.h:
##########
@@ -105,6 +107,63 @@ class ARROW_EXPORT Device : public 
std::enable_shared_from_this<Device>,
   bool is_cpu_;
 };
 
+/// \brief EXPERIMENTAL: An object that provides event/stream sync primitives
+///
+///
+class ARROW_EXPORT DeviceSync {
+ public:
+  explicit DeviceSync(void* sync_event) : sync_event_{sync_event}, 
owns_event_{false} {}
+  virtual ~DeviceSync() = default;
+
+  /// @brief Block until sync event is completed.
+  ///
+  /// Should be a no-op for CPU devices.
+  virtual Status wait() = 0;
+
+  /// @brief Make the provided stream wait on the sync event.
+  ///
+  /// Tells the provided stream that it should wait until the
+  /// synchronization event is completed without blocking the CPU.
+  /// @param stream Should be appropriate for the underlying device
+  virtual Status stream_wait(void* stream) = 0;
+
+  virtual void set_stream(void* stream) { stream_ = stream; }
+
+  Result<void*> get_event() {
+    if (!sync_event_) {
+      ARROW_ASSIGN_OR_RAISE(sync_event_, create_event());
+      owns_event_ = true;
+    }
+    return sync_event_;
+  }
+
+  void clear_event() {

Review Comment:
   Would this be used (instead of just destroying the DeviceSync)? I think it'd 
be best to keep the interface minimal until we can document the usage which 
each function anticipates



##########
cpp/src/arrow/device.h:
##########
@@ -165,6 +224,8 @@ class ARROW_EXPORT MemoryManager : public 
std::enable_shared_from_this<MemoryMan
   static Result<std::shared_ptr<Buffer>> ViewBuffer(
       const std::shared_ptr<Buffer>& source, const 
std::shared_ptr<MemoryManager>& to);
 
+  virtual Result<std::shared_ptr<DeviceSync>> MakeDeviceSync(void* sync_event 
= nullptr);

Review Comment:
   Nit: don't put default arguments on virtual functions. Also, if possible: 
for virtual functions which aren't pure virtual, make the default impl obvious 
by inlining
   ```suggestion
     virtual Result<std::shared_ptr<DeviceSync>> MakeDeviceSync() {
       return nullptr;
     }
     Result<std::shared_ptr<DeviceSync>> MakeDeviceSync(void *sync_event);
   ```



##########
cpp/src/arrow/device.h:
##########
@@ -105,6 +107,63 @@ class ARROW_EXPORT Device : public 
std::enable_shared_from_this<Device>,
   bool is_cpu_;
 };
 
+/// \brief EXPERIMENTAL: An object that provides event/stream sync primitives
+///
+///
+class ARROW_EXPORT DeviceSync {
+ public:
+  explicit DeviceSync(void* sync_event) : sync_event_{sync_event}, 
owns_event_{false} {}

Review Comment:
   This constructor should probably be protected; IIUC we'll always be 
constructing a device-specific derived class and never a bare DeviceSync



##########
cpp/src/arrow/device.h:
##########
@@ -105,6 +107,63 @@ class ARROW_EXPORT Device : public 
std::enable_shared_from_this<Device>,
   bool is_cpu_;
 };
 
+/// \brief EXPERIMENTAL: An object that provides event/stream sync primitives
+///
+///
+class ARROW_EXPORT DeviceSync {
+ public:
+  explicit DeviceSync(void* sync_event) : sync_event_{sync_event}, 
owns_event_{false} {}
+  virtual ~DeviceSync() = default;
+
+  /// @brief Block until sync event is completed.
+  ///
+  /// Should be a no-op for CPU devices.
+  virtual Status wait() = 0;
+
+  /// @brief Make the provided stream wait on the sync event.
+  ///
+  /// Tells the provided stream that it should wait until the
+  /// synchronization event is completed without blocking the CPU.
+  /// @param stream Should be appropriate for the underlying device
+  virtual Status stream_wait(void* stream) = 0;

Review Comment:
   Similarly: it's not clear when this would be used instead of set_stream() 
then wait()



##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -1362,7 +1362,7 @@ namespace {
 // The ArrowArray is released on destruction.
 struct ImportedArrayData {
   struct ArrowArray array_;
-  void* sync_event_;
+  std::shared_ptr<DeviceSync> device_sync;

Review Comment:
   ```suggestion
     std::shared_ptr<DeviceSync> device_sync_;
   ```



##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -739,19 +738,20 @@ Status ExportDeviceArray(const Array& array, RawSyncEvent 
sync_event,
   exporter.Finish(&out->array);
 
   auto* pdata = 
reinterpret_cast<ExportedArrayPrivateData*>(out->array.private_data);
-  pdata->sync_event_ = sync_event;
-  out->sync_event = sync_event.sync_event;
+  pdata->sync_ = sync;

Review Comment:
   ... then move into `pdata`
   ```suggestion
     pdata->sync_ = std::move(sync);
   ```



##########
cpp/src/arrow/buffer.h:
##########
@@ -346,6 +346,8 @@ class ARROW_EXPORT Buffer {
   static Result<std::shared_ptr<Buffer>> ViewOrCopy(
       std::shared_ptr<Buffer> source, const std::shared_ptr<MemoryManager>& 
to);
 
+  virtual std::shared_ptr<DeviceSync> get_device_sync() { return nullptr; }

Review Comment:
   My vote for the bikeshed is:
   ```
   // nest the class inside Device
   DeviceSync -> Device::SyncEvent
   
   // for trivial property accessors we don't usually prefix `get_`
   get_device_sync -> device_sync_event
              (or) -> GetDeviceSyncEvent
   ```



##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -714,11 +713,11 @@ Result<std::pair<std::optional<DeviceAllocationType>, 
int64_t>> ValidateDeviceIn
   return std::make_pair(device_type, device_id);
 }
 
-Status ExportDeviceArray(const Array& array, RawSyncEvent sync_event,
+Status ExportDeviceArray(const Array& array, std::shared_ptr<DeviceSync>& sync,

Review Comment:
   Agreed, please take this argument by value
   ```suggestion
   Status ExportDeviceArray(const Array& array, std::shared_ptr<DeviceSync> 
sync,
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to