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

Added capacity() function and fix issue aidge#112

parent 0d33ca05
No related branches found
No related tags found
No related merge requests found
......@@ -182,6 +182,17 @@ public:
*/
inline std::size_t size() const noexcept { return mNbElts; }
/**
* @brief Return the current capacity of the tensor, i.e. the actual memory
* currently being allocated. It can be different from the size:
* - Capacity can be 0 if the tensor memory was not yet initialized (because
* of lazy initialization, memory is allocated only when it needs to be
* accessed the first time).
* - Capacity can be > size if the tensor was downsized but memory was not
* reallocated.
*/
virtual std::size_t capacity() const noexcept = 0;
/**
* @brief Return the size (in bytes) of one element (scalar).
*/
......
......@@ -43,6 +43,8 @@ public:
return std::make_shared<TensorImpl_cpu<T>>(device, dims);
}
inline std::size_t capacity() const noexcept override final { return mData.size(); }
inline std::size_t scalarSize() const noexcept override final { return sizeof(T); }
void zeros() override final;
......
......@@ -15,7 +15,9 @@
#include "aidge/utils/ErrorHandling.hpp"
void Aidge::TensorImpl::copyFrom(const TensorImpl& srcImpl, NbElts_t length, NbElts_t srcOffset, NbElts_t dstOffset) {
if (&srcImpl == this && srcOffset == dstOffset) {
// Returns if src and dst are the same
// OR if src capacity is 0 (no valid data must be copied)
if ((&srcImpl == this && srcOffset == dstOffset) || srcImpl.capacity() == 0) {
return;
}
......@@ -24,7 +26,7 @@ void Aidge::TensorImpl::copyFrom(const TensorImpl& srcImpl, NbElts_t length, NbE
// Same backend, but different device
copyFromDevice(srcImpl.rawPtr(srcOffset), srcImpl.device(), length, dstOffset);
}
else if (srcImpl.hostPtr() != nullptr) {
else if (srcImpl.hostPtr() != nullptr) { // capacity() is > 0 so hostPtr() will not assert
// Different backend, but input is valid on host
copyFromHost(srcImpl.hostPtr(srcOffset), length, dstOffset);
}
......
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