Skip to content
Snippets Groups Projects
Commit 12c3e45c authored by Christophe Guillon's avatar Christophe Guillon Committed by Maxence Naud
Browse files

[Tensor] Fix Tensor::getIdx() method

The getIdx() method generates segfault on 0-rank coords
due to an out-of-bound access.
Also the computation was wrong for rank of coords
lower than rank of tensor by 2 or more.
parent da92267d
No related branches found
No related tags found
2 merge requests!212Version 0.3.0,!171[Tensor] Fix invalid getIdx() method
...@@ -639,6 +639,7 @@ public: ...@@ -639,6 +639,7 @@ public:
* the remaining coordinates are assumed to be 0. * the remaining coordinates are assumed to be 0.
* Beware: the contiguous index will only correspond to the storage index * Beware: the contiguous index will only correspond to the storage index
* if the tensor is contiguous! * if the tensor is contiguous!
* Note that the coordIdx may be an empty vector.
* *
* @param coordIdx Coordinate to an element in the tensor * @param coordIdx Coordinate to an element in the tensor
* @return DimSize_t Contiguous index * @return DimSize_t Contiguous index
...@@ -646,12 +647,13 @@ public: ...@@ -646,12 +647,13 @@ public:
std::size_t getIdx(const std::vector<std::size_t>& coordIdx) const { std::size_t getIdx(const std::vector<std::size_t>& coordIdx) const {
AIDGE_ASSERT(coordIdx.size() <= mDims.size(), "Coordinates does not match number of dimensions"); AIDGE_ASSERT(coordIdx.size() <= mDims.size(), "Coordinates does not match number of dimensions");
std::size_t flatIdx = 0; std::size_t flatIdx = 0;
std::size_t i = 0; for(std::size_t i = 0; i < mDims.size(); ++i) {
for(; i < coordIdx.size() - 1; ++i) { auto coord = i < coordIdx.size() ? coordIdx[i]: 0;
AIDGE_ASSERT(coordIdx[i] < mDims[i], "Coordinates dimensions does not fit the dimensions of the tensor"); AIDGE_ASSERT(coord < mDims[i], "Coordinates dimensions does not fit the dimensions of the tensor");
flatIdx = (flatIdx + coordIdx[i]) * mDims[i + 1]; auto nextDimSize = i + 1 < mDims.size() ? mDims[i + 1]: 1;
flatIdx = (flatIdx + coord) * nextDimSize;
} }
return flatIdx + coordIdx[i]; return flatIdx;
} }
/** /**
......
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