![Page 1: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/1.jpg)
c++ and python – modern programming techniques
Jakša Vučičević
IPB, Tuesday, February 28th, 2017
![Page 2: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/2.jpg)
Outline
• Introduction to Python, Java and C++• programming paradigms• compilation model
• Template programming vs. class hierarchies
• Various examples (C++ vs. python)• auto typing• templates• templates• meta-programming (functors, partial evaluation, lambdas)• Variadic templates• generic DMFT loop
• TRIQS library• c++2py• numpy.array and triqs::array: linear algebra made easy• HDF5 data storage made easy
• Take away messages
![Page 3: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/3.jpg)
![Page 4: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/4.jpg)
![Page 5: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/5.jpg)
simplest genericprogramming
types-to-be-specified-later
![Page 6: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/6.jpg)
std library support for function objectspartial evaluation
![Page 7: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/7.jpg)
almost full generic programming – auto typing, type “copying”, variadic templatesfull meta-programming – passing around code instead of objects
lambda functions = scope wormholes
![Page 8: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/8.jpg)
no need for templates – all types automatic!introspection – all info on all types and scopes available at runtime
![Page 9: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/9.jpg)
![Page 10: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/10.jpg)
C++ compile time ahead-of-time compilation
C++ run time
![Page 11: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/11.jpg)
Java compile time just-in-time compilation
“byte-code” compiled and executed by JVM
Java run time
![Page 12: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/12.jpg)
Python – no “compile time”interpretation
Interactive!!!Introspective!!!
dynamic typing!!!
Python run time
![Page 13: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/13.jpg)
Generic programming
templating vs. class hierarchies
![Page 14: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/14.jpg)
Generic programming
templating vs. class hierarchies
![Page 15: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/15.jpg)
Generic programming
templating vs. class hierarchies
![Page 16: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/16.jpg)
Generic programming
derived class cast to the base class
base class
functionis “color-blind”
the “old way”
class hierarchies
![Page 17: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/17.jpg)
Generic programming
derived class cast to the base class
paradigm fails in the context of basic types,and various function objects
classes and dependences proliferate... many issues
base class
functionis “color-blind”
![Page 18: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/18.jpg)
Generic programming
You write
Compiler writeswhichever necessary
![Page 19: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/19.jpg)
to achieve this we need• templatesbut• auto type helpful• compile time introspection quite useful
we are making full use of generic programming ifwe have ways of passing functionswe have ways of passing functions• since c++98, custom functors do the job,
but too much overhead• since c++0x, std library support for functors – much better• since c++11, lambda functions – as meta as it gets• since c++14, generic lambdas – as if we’re writing python
let’s see some examples...
![Page 20: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/20.jpg)
C++98
Auto typing
Python
![Page 21: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/21.jpg)
C++11
Auto typing
Python
![Page 22: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/22.jpg)
C++11 “auto” doesn’t work in function arguments
as return type only since c++14
since c++11 more “introspective”
Auto typing
Python
![Page 23: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/23.jpg)
pre C++98
Templates
Python
![Page 24: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/24.jpg)
C++98
Templates
Python
![Page 25: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/25.jpg)
C++14
Templates
Python
![Page 26: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/26.jpg)
C++98Templates
Python
![Page 27: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/27.jpg)
C++98Templates
Python
![Page 28: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/28.jpg)
C++98
Can’t compile!!!
Templates
Python
![Page 29: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/29.jpg)
C++17
Can’t compile!!!
Templates
Python
whatever is declare “constexpr”is evaluated at compile-time!! (since c++11)
![Page 30: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/30.jpg)
C++17
Can’t compile!!!
Templates
Python
whatever is declare “constexpr”is evaluated at compile-time!! (since c++11)
![Page 31: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/31.jpg)
Functional meta-programmingtreating functions as data
why is it useful?
C++
![Page 32: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/32.jpg)
C++98 C++98function objects!!!
![Page 33: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/33.jpg)
Functional meta-programmingtreating functions as data
C++0x automatic function objectsparital evaluation made easysince c++0x
c++11
![Page 34: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/34.jpg)
Functional meta-programmingtreating functions as data
C++11
Lambda functions –passing snippet of codethrough a scope wormhole!!!
![Page 35: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/35.jpg)
Python
Functional meta-programmingtreating functions as data
![Page 36: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/36.jpg)
Variadic templates
C++11
List/aggregate initializer
![Page 37: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/37.jpg)
Variadic templates
Python
![Page 38: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/38.jpg)
Generic DMFT loopdifferent shapes used in different ways
![Page 39: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/39.jpg)
Leave the details to be determined later. The basic structure is always the same! The number of circles may vary
![Page 40: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/40.jpg)
Generic DMFT-like loop
C++11
![Page 41: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/41.jpg)
Generic DMFT-like loop
Python
![Page 42: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/42.jpg)
TRIQSToolbox for Research on Interacting Quantum Systems
![Page 43: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/43.jpg)
TRIQSToolbox for Research on Interacting Quantum Systems
Main goals• act as a bridge between c++ and python so as
to allow for both painless manipulationof data (in python) and high-optimization of critical routines (c++)
• provide containers for common objects • provide containers for common objects in condensed matter theory (multidimensional arrays (in c++) ,Green’s functions,second-quantized Hamiltonians, etc.)
• provide generic implementation of commonalgorithms (monte carlo, Hilbert transform, FT, tail fitting...)
• provide a simplified and intuitive interface to MPI and HDF5
![Page 44: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/44.jpg)
c++2py wrapper
• allows for dynamical linking of python code with precompiledc++ libraries• the “wrapping” produces python modules with “pythonically”
callable functions and classes- each c++ class gets a python version of itself andone may even choose which properties of the class will bevisible in pythonvisible in python
• basic types are simply equated between python and c++no need to invoke any special integers, floats, string, etc.
• std types also wrapped up naturally (e.g. vector -> list)•wrapper is based on the intermediate representation ofclang compiler and cmake project structure• the python modules are generated automatically with a single command
![Page 45: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/45.jpg)
triqs::array• c++ std library has no convenient multidimensional array container• triqs::array is analogous to numpy.array in pythonand allows for many of the same functionalities• linear algebra made easy!
![Page 46: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/46.jpg)
triqs::array• c++ std library has no convenient multidimensional array container• triqs::array is analogous to numpy.array in pythonand allows for many of the same functionalities• linear algebra made easy!
![Page 47: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/47.jpg)
triqs::array• c++ std library has no convenient multidimensional array container• triqs::array is analogous to numpy.array in pythonand allows for many of the same functionalities• linear algebra made easy!
![Page 48: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/48.jpg)
HDF5 Hierarchical Data Format
•HDF5 is a data model, library, and file format for storing and managing data•standard, well maintained and widely used•supports an unlimited variety of data types, and is designed for flexible and efficient I/O and for high volume and complex data•portable and is extensible•portable and is extensible•HDF5 can be read and written in many languages
![Page 49: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/49.jpg)
HDF5 Hierarchical Data Format
![Page 50: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic](https://reader034.vdocument.in/reader034/viewer/2022042311/5ed920286714ca7f47693206/html5/thumbnails/50.jpg)
Take away messages
•High-level programming makes life easier!•Generic and meta programming allow for separation between various levels of detail of an algorithm• no huge class hierarchies – instead small pieces we put together as we need them, when we need them
•C++ is no longer so low-level, but still allows for huge •C++ is no longer so low-level, but still allows for huge optimizations and should be used for computationally intensive routines.
•Combination of c++ and python ideal! Install triqs give it a try!
•Need multidim array in c++? triqs::array does the job
•No more text files, no more formatting or mysterious binaries –store data in HDF5 format!