Skip to content

Accelerate the method Tensor::getElementPtr #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/glow/Base/Tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,16 @@ template <class ElemTy> class Handle final {
/// the list of indices may be incomplete.
size_t getElementPtr(llvm::ArrayRef<size_t> indices) const {
assert(indices.size() <= numDims && "Invalid number of indices");
return std::inner_product(indices.begin(), indices.end(),
std::begin(sizeIntegral), 0);
// The loop below can be rewritten using std::inner_product. Unfortunately
// std::inner_product does not optimize very well and loops that use this
// method don't get vectorized. Don't change this loop without benchmarking
// the program on a few compilers.
size_t index = 0;
for (int i = 0, e = indices.size(); i < e; i++) {
index += size_t(sizeIntegral[i]) * size_t(indices[i]);
}

return index;
}

/// \returns the value of the n'th dimension \p dim, for the raw index \p idx.
Expand Down