| 56 | } |
| 57 | |
| 58 | Result<std::shared_ptr<Buffer>> MemoryManager::CopyBuffer( |
| 59 | const std::shared_ptr<Buffer>& buf, const std::shared_ptr<MemoryManager>& to) { |
| 60 | const auto& from = buf->memory_manager(); |
| 61 | auto maybe_buffer = to->CopyBufferFrom(buf, from); |
| 62 | COPY_BUFFER_RETURN(maybe_buffer, to); |
| 63 | // `to` doesn't support copying from `from`, try the other way |
| 64 | maybe_buffer = from->CopyBufferTo(buf, to); |
| 65 | COPY_BUFFER_RETURN(maybe_buffer, to); |
| 66 | if (!from->is_cpu() && !to->is_cpu()) { |
| 67 | // Try an intermediate view on the CPU |
| 68 | auto cpu_mm = default_cpu_memory_manager(); |
| 69 | maybe_buffer = from->ViewBufferTo(buf, cpu_mm); |
| 70 | if (!COPY_BUFFER_SUCCESS(maybe_buffer)) { |
| 71 | // View failed, try a copy instead |
| 72 | // XXX should we have a MemoryManager::IsCopySupportedTo(MemoryManager) |
| 73 | // to avoid copying to CPU if copy from CPU to dest is unsupported? |
| 74 | maybe_buffer = from->CopyBufferTo(buf, cpu_mm); |
| 75 | } |
| 76 | if (COPY_BUFFER_SUCCESS(maybe_buffer)) { |
| 77 | // Copy from source to CPU succeeded, now try to copy from CPU into dest |
| 78 | maybe_buffer = to->CopyBufferFrom(*maybe_buffer, cpu_mm); |
| 79 | if (COPY_BUFFER_SUCCESS(maybe_buffer)) { |
| 80 | return maybe_buffer; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return Status::NotImplemented("Copying buffer from ", from->device()->ToString(), |
| 86 | " to ", to->device()->ToString(), " not supported"); |
| 87 | } |
| 88 | |
| 89 | Result<std::unique_ptr<Buffer>> MemoryManager::CopyNonOwned( |
| 90 | const Buffer& buf, const std::shared_ptr<MemoryManager>& to) { |
nothing calls this directly
no test coverage detected