From deea0be070d67734f7cc113b38e4fa71965744ef Mon Sep 17 00:00:00 2001 From: Olivier BICHLER <olivier.bichler@cea.fr> Date: Mon, 29 Jan 2024 09:29:50 +0100 Subject: [PATCH] makeContiguous optimization (single copy for contiguous chuncks of memory) --- src/data/Tensor.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/data/Tensor.cpp b/src/data/Tensor.cpp index 6ab096201..d45dee563 100644 --- a/src/data/Tensor.cpp +++ b/src/data/Tensor.cpp @@ -45,10 +45,24 @@ void Aidge::Tensor::makeContiguous() { { // Create a new storage that will be contiguous std::shared_ptr<TensorImpl> newImpl = Registrar<Tensor>::create({mImpl->backend(), mDataType})(mImpl->device().second, mSize); - // Copy elements one by one from old to new storage - for (size_t idx = 0; idx < mSize; ++idx) { + // Copy elements from old to new storage + size_t idx = 0; + while (idx < mSize) { const size_t storageIdx = getStorageIdx(getCoord(idx)); - newImpl->copy(mImpl->rawPtr(mImplOffset + storageIdx), 1, idx); + + // Determine the size of the contiguous chunk + size_t copySize = 1; + while (idx + copySize < mSize && + getStorageIdx(getCoord(idx + copySize)) == storageIdx + copySize) + { + ++copySize; + } + + // Perform a single copy for the contiguous chunk + newImpl->copy(mImpl->rawPtr(mImplOffset + storageIdx), copySize, idx); + + // Move to the next index after the contiguous chunk + idx += copySize; } // Replace old storage by new, contiguous, storage setImpl(newImpl); -- GitLab