Upgrade to Pro — share decks privately, control downloads, hide ads and more …

POLYMORPHIC INPUT ITERATORS

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for DaveFar DaveFar
January 09, 2019

POLYMORPHIC INPUT ITERATORS

Polymorphic input iterators in Modern C++ via std::variant.

Avatar for DaveFar

DaveFar

January 09, 2019

More Decks by DaveFar

Other Decks in Programming

Transcript

  1. ITERATOR DESIGN PATTERN 2 Gang of Four Intent ... access

    the elements ...without exposing its underlying representation. Motivation ...the client commits to a particular collection. It would be better if we could change the collection without changing client code. We can do this by generalizing the iterator concept to support polymorphic iteration. Bjarne Stroustrup Polymorphism Providing a single interface to entities of different types.
  2. INPUT ITERATOR CONCEPTS 3 Legacy std::experimental::ranges WeaklyInc->Semiregular->DefaultConstructible WeaklyInc->Semiregular->Copyable->Movable->Swappable Swappable WeaklyInc->Semiregular->Copyable->CopyConstructible

    WeaklyInc->Semiregular->Copyable->Assignable WeaklyInc->Semiregular->Copyable->Movable WeaklyInc->Semiregular->Copyable->Assignable CopyConstructible, ConversionConstructible to const CopyAssignable Destructible Dereferenceable, Readable Dereferenceable, Dereference -> WeaklyInc WeaklyInc pre-incrementable, pre-increment post-increment equality-comparable inequality-comparable std::iterator_traits<II> has the 5 member typedefs std::iterator_traits<II> has the 5 member typedefs
  3. MOTIVATION AT QPR 4 algorithm classes data classes<X> ... ...

    ... ... ... ... ... ... ... ... ... ... ... ... ... Acollection<V> ... ... ... ... ... ... ... ... ... ... ... alternative classes <X> Sparse<V> ... ... DB<V> ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... InputIterate <X> Dense<V> ... ... <class _InputIterator> <class _InputIterator> <class _InputIterator>
  4. MOTIVATION AT QPR 5 algorithmen classes data classes <X> ...

    ... ... ... ... ... ... ... ... ... ... ... ... ... ... Acollection<V> ... ... ... ... ... ... ... ... ... ... ... alternative classes <X> Sparse<V> ... ... DB<V> ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... InputIterate <X> Dense<V> ... ... <<Interface>>A begin(),end():PolymorphicInputIterator<V> ...
  5. IMPLEMENTATION 6 ad-hoc vs. parametric semantics change to existing code

    templates (e.g. CRTP) OOP (virtual) std::variant (std::visit) Polymorphism in C++ (but not macros, overloading, coersion) optimizations, runtime J J J with lambda type deduction L reference sem., dyn. alloc. J value sematics, copying J value sematics, copying L code complexity (readabilty, error messages, compile time) J J J iff no code bloat J but less, see next slides J J L dynamic polymorphism L
  6. PERFORMANCE measured runtime of accumulating a vector with 3e+7 doubles

    (into double or ULL): dynamically in ms (wall clock time), statically in k cpu cycles (LLVM Machine Code Analyzer, see godbolt.org/z/H9bXPs) with –ffast-math w/o –ffast-math clang (trunk) -march=broadwell –O3 gcc (trunk) 0ULL 0.0 wrapped direct 212 277 20.5 22.4 17 217 5.7 14.4 0ULL 0.0 wrapped direct 196 279 20.5 22.4 32 218 4.3 25.4 ULL 0.0 wrapped direct 166 186 2.2 4.2 14 117 2.9 2.5 ULL 0.0 wrapped direct 166 204 2.2 4.0 44 119 2.4 2.7 9
  7. LLVM-MCA VON VECTOR-TESTS double with gcc (trunk) with -std=c++17 -O3

    -march=broadwell Wrapped: Runtime for vector size 30.000.000: TODO MCA: [0] Code Region - stopwatchedAccumulate Iterations: 100 Instructions: 4200 Total Cycles: 2196 Total uOps: 5000 Dispatch Width: 6 uOps Per Cycle: 2.28 IPC: 1.91 Block RThroughput: 8.3 Resource pressure per iteration: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] - - 7.62 7.95 5.70 5.72 7.00 7.81 7.62 4.58 Wrapped for accumulationition into ULL: Runtime for vector size 30.000.000: TODO MCA: [0] Code Region - stopwatchedAccumulate Iterations: 100 Instructions: 8200 Total Cycles: 4020 Total uOps: 9300 Dispatch Width: 6 uOps Per Cycle: 2.31 IPC: 2.04 Block RThroughput: 15.5 Resource pressure per iteration: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] - - 18.38 19.54 6.02 6.32 6.00 17.76 18.32 4.66