Skip to content
Snippets Groups Projects
Commit deea0be0 authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

makeContiguous optimization (single copy for contiguous chuncks of memory)

parent 9c18776a
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment