Comments (3)
This turns out to be quite of a problem. Since views are generated on the fly; the C++ iterator architecture is fitting badly.
//Iterator
struct ETALER_EXPORT iterator
{
iterator(Tensor& t) : t_(&t) {}
Tensor operator*() { return t_->view({curr_}); } // Can't return ref here
Tensor* operator->() {/*real? How am I going to return a pointer from a local variable*/}
bool operator==(iterator rhs) { return curr_ == rhs.curr_ && t_ == rhs.t_; }
bool operator!=(iterator rhs) { !(*this == rhs); }
iterator& operator++() {curr_ += 1; return *this;}
iterator operator++(int) {iterator retval = *this; ++(*this); return retval;}
Tensor* t_; // Using a pointer because Tensor is a incomplete type within the type itself
intmax_t curr_ = 0;
};
from etaler.
This is the best I can do
template <typename T>
struct ETALER_EXPORT TensorIterator
{
// Iterator properties
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using raw_value_type = std::remove_const_t<value_type>; // extra
using difference_type = intmax_t;
using pointer = std::unique_ptr<raw_value_type>;
using reference = T&;
using ThisIterator = TensorIterator<T>;
TensorIterator() = default;
TensorIterator(reference t, intmax_t offset = 0) : t_(&t), offset_(offset)
{static_assert(std::is_same_v<raw_value_type, Tensor>); }
value_type operator*() { return t_->view({offset_}); }
// Unfortunatelly returning a pointer is not doable
pointer operator->() { return std::make_unique<raw_value_type>(*(*this)); }
bool operator==(ThisIterator rhs) const { return offset_ == rhs.offset_ && t_ == rhs.t_; }
bool operator!=(ThisIterator rhs) const { return !(*this == rhs); }
ThisIterator& operator++() {offset_ += 1; return *this;}
ThisIterator operator++(int) {ThisIterator retval = *this; ++(*this); return retval;}
ThisIterator& operator--() {offset_ -= 1; return *this;}
ThisIterator operator--(int) {ThisIterator retval = *this; --(*this); return retval;}
value_type* t_ = nullptr; // Using a pointer because Tensor is a incomplete type here
intmax_t offset_ = 0;
};
It works 90% of the time and have few caveats
operator*
should return a referenceoperator->
should return a pointerswap()
(not in the shown code` should accept references, not values
from etaler.
#121 implements a bidirectional iterator. It should be enough for most use cases.
Feel free to reopen the issue if we need a random access iterator
from etaler.
Related Issues (20)
- Modulizating Etaler (C++20 modules)
- Indexing with negative indices failed HOT 1
- Rework of the view/indexing system
- Fix toHost<bool>() HOT 1
- Granularize error/pre/post-condition checking machenisim HOT 1
- Fix typos in variable names and comments HOT 1
- Move the visualizer to a seprate repository
- New serealization system
- Replace std::decay with std::remove_cvref when C++20 is avaliable
- Build fail on latest MSVC 2019 caused by et_check (possibe MSVC bug) HOT 1
- Setting up CI
- Tests failing when running on OpenCL backend, POCL w/ CPU
- Support 0D tensors
- Etaler backend re-architecture HOT 1
- AMD GPU support over Mesa + Clover HOT 1
- SmallVector::back() emits error under testing in a Debug build
- OpenCL program cache HOT 1
- Works on HaikuOS
- GridCellEncoder problems HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from etaler.