c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope...

50
c++ and python – modern programming techniques Jakša Vučičević IPB, Tuesday, February 28 th , 2017

Upload: others

Post on 31-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic

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

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
Page 4: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic
Page 5: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic

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

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

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

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
Page 10: c++and python –modern programming techniques · 2017-03-03 · lambda functions = scope wormholes. ... ,Green’s functions, second-quantized Hamiltonians, etc.) •provide generic

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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!