Skip to content

Commit

Permalink
simplify the to_cpu op
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsanbear committed Apr 11, 2024
1 parent ef69643 commit 79647c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
2 changes: 2 additions & 0 deletions candle-core/src/metal_backend/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ impl MetalDevice {

/// Copies data from one buffer to another.
/// This method is blocking as it relies on GPU-CPU synchronization.
/// NOTE: this method does not end the current command buffer, it is the responsibility of the caller
/// to do so via the [`close_compute_buffer`] function.
pub fn copy_buffer(
&self,
source_buffer: &BufferRef,
Expand Down
18 changes: 4 additions & 14 deletions candle-core/src/metal_backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,21 +1594,11 @@ impl MetalStorage {
let size = (self.count * self.dtype.size_in_bytes()) as NSUInteger;
let buffer = self.device.new_buffer_managed(size)?;

// Handle the creation and initialization of a new command buffer
{
// Finalize current compute operations
self.device.end_compute_encoding()?;

let command_buffer = self.device.command_buffer()?;
// Enqueue the copy operation
self.device.copy_buffer(&self.buffer, 0, &buffer, 0, size)?;

// Setup the blit encoder to perform the copy operation
let blit = command_buffer.new_blit_command_encoder();
blit.copy_from_buffer(&self.buffer, 0, &buffer, 0, size);
blit.end_encoding();

// Commit the command buffer to the queue
self.device.close_compute_buffer()?;
}
// Commit the command buffer to the queue to ensure the copy operation is completed
self.device.close_compute_buffer()?;

let result = read_to_vec(&buffer, self.count);
Ok(result)
Expand Down

0 comments on commit 79647c8

Please sign in to comment.