introducing cling - root · the c++ in cern • data analysis = interfacing with experiments’...

41
Introducing cling Axel Naumann, CERN @ Google Zurich, 2012-03-15

Upload: others

Post on 31-May-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Introducing clingAxel Naumann, CERN

@ Google Zurich, 2012-03-15

Page 2: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

cling?

C++

Page 3: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

cling?

C++LH

Page 4: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

cling?

C++LHcling$

Page 5: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

cling?

C++LH(int const) 42cling$

Page 6: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

CERN, LHC

• fundamental research: high energy physics

• international organization in Geneva, CH

• main tool: Large Hadron Collider

• proton smasher’s measurements:8TeV, 2K, 27km

• several experiments, 10’000 users

Page 7: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Postcard From CERN

Page 8: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Postcard From CERN

Page 9: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Underground Science

Page 10: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Big Questions, Big Tools

Page 11: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

The C++ in CERN

• data analysis = interfacing with experiments’ code

• several GB of libraries, hundreds of thousands of types / templates,50MLOC C++ code

• physics is the goal, computing the tool

Page 12: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

http:// ROOT .cern.ch

• data analysis (math), persistency (I/O), visualization (graphics),...

• about 20’000 users, also outside science

• core software element for experiments

• interface point for experiment’s code

• C++ interpreter CINT almost 20 years old

Page 13: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Use of Interpreters

access data1970

today

Page 14: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Use of Interpreters

access data

simple expressions

1970

today

Page 15: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Use of Interpreters

access data

simple expressions

simple algorithms

1970

today

Page 16: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Use of Interpreters

access data

simple expressions

simple algorithms

prototype algorithm

1970

today

Page 17: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Use of Interpreters

access data

simple expressions

simple algorithms

prototype algorithm

develop code

1970

today

Page 18: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Setup

Experiment’s code

Third-party libraries

Persistent dataUser code Interpreter

Page 19: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Current Use

• IDE-on-the-prompt

• interpreter = not linking + state

• rapid edit / “compile” / run cycles

• exploration-driven development

• matches physics analysis approach with its gradual optimizations

Page 20: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

cling: interpreting C++

• http://cern.ch/cling

• based on LLVM http://llvm.org+ clang http://clang.llvm.org

• developed by team @ CERN and Fermilab

• maps interpreter to compiler concept

• not an interpreter: JIT!

Page 21: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

cling’s Translation Unit

• AST keeps growing as input comes

• incremental parsing, codegen, evaluation

• action at end of translation unit now at end of input transaction

• pending template instantiation

• codegen

Page 22: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Expr vs Decl

• decls must stay visible across input lines

• expressions must be evaluated

• ...and is it decl or expr or both?

cling$  int  i  =  1;  sin(i++)

Page 23: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Expr vs Decl (2)

1. determine whether decl or expr:manipulation of input string

void  wrap_0()  {    int  i  =  1;  sin(i++);  }

cling$  int  i  =  1;  sin(i++)

Page 24: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Expr vs Decl (3)

2. extend decl lifetime: move onto global scope (AST editing)

void  wrap_0()  {    int  i  =  1;  sin(i++);  }

cling$  int  i  =  1;  sin(i++)

int  i  =  1;void  wrap_0()  {  sin(i++);  }

Page 25: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Expressions

3. call wrap_0() to evaluate expressions

4. print value if no trailing ‘;’uses template magic on AST level:

int  i  =  1;void  wrap_0()  {  sin(i++);  }

cling$  int  i  =  1;  sin(i++)(double  const)  8.414710e-­‐01

Page 26: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Details, details

• global initialization after each input - but not re-initialization

• collect global destructors to run at ~cling()

• error recovery reverting whole input transaction and its AST nodes

Page 27: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Dynamic Scopes

• inject serialized C++ objects into scope

• delay expression evaluation until runtime

• compiler as a service - sort of like DLR

if  (date  %  2)  {    TFile  f(getFilename());    objInFile-­‐>Draw();}

Page 28: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

JIT

• optimized code

• ABI-compatible:in-memory layout vs serialization

• calls into native libraries

$  echo  'const  char*  zlibVersion();zlibVersion()'  |  cling  -­‐x  c  -­‐lz(const  char  *  const)  "1.2.3.4"

Page 29: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Reflection

• weak point in C++

• can tap clang’s AST!+ target info!+ ABI!

• dynamic, two ways: query + edit AST

Page 30: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Growing cling

• clang vs. Windows C++ ABI!

• clang as front-end, thus C++11

• ObjC[++] can be extended (not by CERN)

• OpenCL could be done

• or new front-ends? LINQ, anyone?

Page 31: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Where?

• available in subversion:http://root.cern.ch/svn/root/trunk/cint/cling

• stable: few interface changes, only 10k LOC

• works on anything with clang + LLVM-JIT (thus not native Windows ABI)

• stand-alone binary plus modular C++ libraries like LLVM + clang

NEW!

FREE!

SALE!

Page 32: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Where?

• available in subversion:http://root.cern.ch/svn/root/trunk/cint/cling

• stable: few interface changes, only 10k LOC

• works on anything with clang + LLVM-JIT (thus not native Windows ABI)

• stand-alone binary plus modular C++ libraries like LLVM + clang

NEW!

FREE!

SALE!

MIT LICENSE!

Page 33: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Interpreter Language

• C++: complex syntax, verbose, precise; improvements from C++11

• Current #1 alternative python: simple syntax, casual code

Page 34: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

PyROOT

• bi-directional

• map concepts (iterators, dictionary)

TPython::Exec("print  \'PyROOT!\'");

from  ROOT  import  TLorentzVectorb  =  TLorentzVector()b.SetX(1.0)

Page 35: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

PyROOT Internals

• reflection-, not stub-based: highly dynamic

• injects C++ methods into python

• objects traverse language boundary

• performant: caches, annotates python objects with PyROOT metadata

Page 36: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

iC++ vs iPython

• migrating python code to C++: difficult

• interfacing C++ though python: difficult

• writing 1 MLOC python: easy

• python is slow (“thanks to python, we are not I/O bound anymore!”)

Page 37: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Conclusion: Interpreter

• interpreters enable dynamic access to huge binary worlds

• different approach to programming

• reflection is the key: interpreter binding, serialization, dynamic behavior

Page 38: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Conclusion: Language

• choice of languages still limits us

• python is simple but slow; interface withC++ difficult but possible

• C++ easily too complex for novices

• either language dead-end: no interfaces from other languages (extern “C++”)

Page 39: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Conclusion: cling

• based on decades of experience with novices, code development, large libraries - and CINT

• clang + llvm make miracles happen

• it’s stable and fun: enjoy it!

Page 40: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

Offline Demo

• demo by “arbitrary (smart) user” Thomas Gahr showing powers of Qt + cling

• http://youtu.be/BrjV1ZgYbbA

• recursive youtube!

Page 41: Introducing cling - ROOT · The C++ in CERN • data analysis = interfacing with experiments’ code • several GB of libraries, hundreds of thousands of types / templates, 50MLOC

http://youtu.be/BrjV1ZgYbbA