printing values in interactive root cern summer student program 2015 supervisors: axel naumann,...

Download Printing values in interactive ROOT CERN SUMMER STUDENT PROGRAM 2015 SUPERVISORS: AXEL NAUMANN, DANILO PIPARO, PH-SFT STUDENT: BORIS PEROVIĆ, EPFL Geneva,

If you can't read please download the document

Upload: dorothy-baldwin

Post on 19-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

Content Interpreter, interactivity Previous state and requirements Idea and new solution Challenges Extra features Demo Conclusion Future work Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVIĆ 3

TRANSCRIPT

Printing values in interactive ROOT CERN SUMMER STUDENT PROGRAM 2015 SUPERVISORS: AXEL NAUMANN, DANILO PIPARO, PH-SFT STUDENT: BORIS PEROVI, EPFL Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 1 Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 2 Value printing team Boris PeroviDanilo PiparoAxel Naumann Content Interpreter, interactivity Previous state and requirements Idea and new solution Challenges Extra features Demo Conclusion Future work Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 3 Cling Highly efficient C++ interpreter at the core of the ROOT Data Analysis Framework Unique solution, turning a compiled language into an interpreted one Added value: interactivity! Interactivity is important make working with code as easy as possible Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 4 Interpreter interactivity An interactive interpreter has 3 basic tasks: 1.Read read the user input from standard in 2.Evaluate compile the input and run the compiled code, evaluating the result 3.Print output the result of the evaluation This common behavior is called REPL (Read, Evaluate, Print) Loop Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 5 Previous state (recap) root[0] #include root[1] std::vector myData = {1.4, 42.0, 7.44, } (std::vector &) 0xb4da7b14 root[2] myData (std::vector &) 0xb4da7b14 Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 6 Not very user friendly! Idea Make value printing possible for all types, even the user-defined ones Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 7 What do we need? Information about the type of the value Access to data that the Value holds (boxes) Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 8 1 st challenge: How to print? cling::Value contains: o clang::QualType qualified type of the evaluated value o union Storage internal value storage containing or pointing to the actual data Just what we need!? Not really how to cast, what about user- defined classes? To support all types, interpret printing! Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 9 2 nd challenge: Interpretation 1.Evaluate the user input 2.Generate the printing code 3.Evaluate the printing code in the interpreter and output the result We want to keep JIT compilation to the least amount possible (maintain performance, avoid compilation errors) Generate only a function invocation Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 10 The function cling::printValue was born Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 11 3 rd challenge: Generic printing How to implement this solution in the most generic way, with as much code reuse as possible and allow it to be extendable? Naturally, use templates and template specializations / function overloads: std::string printValue(const void *ptr) std::string printValue(const char val) template std::string printValue(const T (&obj)[N]) Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 12 3 rd challenge: Generic printing containers template std::string printValue(const std::vector & val) template std::string printValue(const std::map & val)... There are so many containers: array, deque, list, ( unordered_ )( multi ) map Printing code exactly the same (iterate, print) We surely do not want duplicate code so much prone to copy / paste errors, difficult to maintain What to do? Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 13 3 rd challenge: Generic printing SFINAE SFINAE Substitution Failure Is Not An Error Simply said, it allows us to narrow down the specialization to a specific type (set of types) which contains specific methods Made much easier in C++11! Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 14 3 rd challenge: Generic printing SFINAE example // Collections internal declaration namespace collectionPrinterInternal { // Maps declaration template auto printValue_impl(const CollectionType *obj, short) -> decltype(++(obj->begin()), obj->end(), obj->begin()->first, obj->begin()->second, std::string()); // Vector, set, deque etc. declaration template auto printValue_impl(const CollectionType *obj, int) -> decltype(++(obj->begin()), obj->end(), *(obj->begin()), std::string()); } // Collections template auto printValue(const CollectionType *obj) -> decltype(collectionPrinterInternal::printValue_impl(obj, 0), std::string()) { return collectionPrinterInternal::printValue_impl(obj, (short)0); // short -> int } Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 15 Success! First part done Primitive types printing refactored to conform to the new convention Containers being printed properly Geneva, September 2015 CERN SUMMER STUDENT PROGRAM 2015, PROJECT PRESENTATION BORIS PEROVI 16 ROOT types Implement cling::printValue for TObject in order to print at least the name and the title for all of the ROOT types std::string cling::printValue(const TObject &val) { std::ostringstream strm; strm