Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Python Objects and Classes
C++ Programming Languages
. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism.
Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Ch 4. Memory Management Timothy Budd Oregon State University.
OOP Languages: Java vs C++
CSE333 SECTION 7. Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Working with Strings Lecture 2 Hartmut Kaiser
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Defining New Types Lecture 21 Hartmut Kaiser
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Workshop on Grid Application Programming 19th-21st July 2004, Edinburgh, 1 Hartmut Kaiser The C Implementation of the GAT OO Specification Hartmut Kaiser.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
C++ Memory Overview 4 major memory segments Key differences from Java
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Introduction to Object-Oriented Programming Lesson 2.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Introduction to Object Oriented Programming Chapter 10.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Advanced BioPSE NCRR Programming Conventions and Standards J. Davison de St. Germain Chief Software Engineer SCI Institute December 2003 J.
Writing GAT Adaptors Hartmut Kaiser
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
FASTFAST All rights reserved © MEP Make programming fun again.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Design issues for Object-Oriented Languages
Chapter 2 Objects and Classes
C# for C++ Programmers 1.
7. Inheritance and Polymorphism
Review: Two Programming Paradigms
C++, OBJECT ORIENTED PROGRAMMING
Object-Orientated Programming
Finally! Discussing a language!
Chapter 2 Objects and Classes
Lecture 9 Concepts of Programming Languages
Overview of Memory Layout in C++
Classes and Objects.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser

October, 24th 2005Digging into the GAT API2 Design Principles –Object orientation Derivation, Interfaces, Genericity –Memory Management Object lifetime, Instance tracking, Allocation responsibilities –Const Correctness –Error Handling

October, 24th 2005Digging into the GAT API3 Object Orientation - C GAT Specification is object oriented What’s an ‘Object’ –Some data and a set of related functions Representation in C –Internal struct GATFile_S { /*…*/ }; –External typedef struct GATFile_S *GATFile; –Constructor/Destructor GATFile_Create(), GATFile_Destroy() –Naming convention GATResult GATFile_Copy(GATFile, /*…*/);

October, 24th 2005Digging into the GAT API4 Object Orientation – C++ Separate namespaces: GAT (GAT::Adaptors) Defines reference counted wrappers on top of the C API Natural representation –Create new GAT object GAT::File file(name);// constructor, destructor –Wrap existing C objects (handles) GAT::File file = GAT::MakeFile(c_file); Naming convention: GATResult GATFile_Copy(GATFile, /*…*/); GAT::Result GAT::File::Copy(/*…*/);

October, 24th 2005Digging into the GAT API5 Object Orientation – Python Separate namespaces (modules): GAT (GAT.Adaptors) Defines Python classes on top of the C API Representation is natural –Create new GAT object file = GAT.File(name)// constructor, destructor –Wrapping not necessary, done by the PyGAT runtime (which is written in C/C++) Naming convention: GATResult GATFile_Copy(GATFile, /*…*/); GAT.Result GAT.File.Copy(/*…*/)

October, 24th 2005Digging into the GAT API6 Interfaces - C Certain GAT object types have to implement different interfaces (GATObject, GATStreamable, GATMonitorable) What’s an interface –A set of related functions, which may be called even not knowing the type of the object Representation in C –Emulation of virtual functions. Every object has a table of function pointers, one table for each interface –GetInterface(): helper function to get at the different function pointer tables

October, 24th 2005Digging into the GAT API7 GATFile Memory Layout

October, 24th 2005Digging into the GAT API8 Interfaces – C++ Implemented through static polymorphism and multiple inheritance from different base class templates (GAT::Streamable<>, GAT::Serialisable<>, GAT::Monitorable<> etc.)

October, 24th 2005Digging into the GAT API9 Interfaces – Python All classes simply have interface functions available, no derivation needed (although internally used for the implementation) Integration of serialisation into the Python language (pickling).

October, 24th 2005Digging into the GAT API10 Derivation - C Every GAT object type has to be derived from the GATObject What’s ‘Derivation’ –Reuse of common functionality –Conversion from and to GATObject should be possible Representation in C –Every GAT object type has a common set of functions with an identical signature GATType GATFile_GetType(GATFile_const); GATResult GATFile_Destroy(GATFile *); GATResult GATFile_Clone(GATFile_const, GATFile *); GATResult GATFile_GetInterface(GATFile_const, void **); GATResult GATFile_Equals(GATFile_const, GATFile_const, GATBool *);

October, 24th 2005Digging into the GAT API11 Derivation - C Conversion from any GAT type to GATObject should be possible, since all these ‘derived’ from GATObject. Conversion from GATObject to the corresponding GAT type should be possible as well. Representation in C –For every GAT type the following functions exist: Succeeds always: GATObject GATFile_ToGATObject(GATFile); GATObject_constGATFile_ToGATObject_const(GATFile_const); Succeeds only, if type matches: GATFile GATObject_ToGATFile(GATObject); GATFile_const GATObject_ToGATFile_const(GATObject_const);

October, 24th 2005Digging into the GAT API12 Derivation – C++ Representation in C++ –Every GAT++ object type is derived from the GAT::Object template: GAT::Type GAT::Object ::GetType(Derived const); GAT::Object ::Object(Derived const); bool GAT::Object ::operator==(Derived const); –Conversion is handled automatically by derivation

October, 24th 2005Digging into the GAT API13 Derivation - Python Representaion in Python –Every GAT. has a similar set of functions implemented: GAT.File.GetType() GAT.File.Clone() GAT.File.__cmp__() –Typeless language, no conversion problems, handled by PyGAT wrapper runtime

October, 24th 2005Digging into the GAT API14 Genericity - C Possibility to call a function for an arbitrary GAT type not knowing the concrete type Representation in C –For every interface function exists Concrete function implementation for every GAT type, which realises this interface GATResult GATFile_Serialise(GATFile file, GATObject stream, GATBool cleardirty); Generic function allowing to call the type specific function GATResult GATSerialisable_Serialise(GATObject object, GATObject stream, GATBool cleardirty);

October, 24th 2005Digging into the GAT API15 Genericity – C++ Representation in C++ –Works based on the inheritance scheme

October, 24th 2005Digging into the GAT API16 Genericity – Python No special support needed for that (Python is typeless)

October, 24th 2005Digging into the GAT API17 Memory Management - C All GAT object types have a …_Create() function, which returns a new instance of this type. All GAT object types have a …_Destroy function, which frees all associated memory. You are responsible to call …_Destroy! –for all objects you’ve created GATFile file = GATFile_Create(location); … /* do something useful with ‘file’ */ GATFile_Destroy(&file); –for all non const objects you get back from the engine GATPipe pipe = NULL; GATEndpoint_Connect(endpoint, &pipe); … /* do something useful with ‘pipe’ */ GATPipe_Destroy(&pipe);

October, 24th 2005Digging into the GAT API18 Memory Management - C The GAT objects returned from the engine are handles! (well actually pointers, but …) typedef struct GATFile_S * GATFile; You’re free to copy around those ‘objects’ without performance harm. But watch out! Don’t free any of these objects while you’re holding copies of it, which you still want to use. Never free a GATObject with free(). If you are using casting functions (as GATObject_ToGATFile ) please note, that the result refers to the same object, so don’t free twice.

October, 24th 2005Digging into the GAT API19 Memory Management – C++ The GAT++ objects handle all memory management issues (reference counted) You’re free to copy around those objects without performance harm. GAT::File file1(...);// creates C ‚object‘ GAT::File file2 = file; Underlying C handle get‘s free‘d only after file1 and file2 are destructed No need for explicit destruction, language (compiler) keeps track of that

October, 24th 2005Digging into the GAT API20 Memory Management – Python The PyGAT objects handle all memory management issues (Python is reference counted/garbage collected) You’re free to copy around those objects without performance harm. file1 = GAT.File(...);// creates C ‚object‘ file2 = file; Underlying C handle get‘s free‘d only after file1 and file2 are out of scope and got garbage collected Runtime keeps track of not needed instances

October, 24th 2005Digging into the GAT API21 Const correctness - C Const correctness introduced wherever possible Helps to enforce semantics, especially for memory management –You’ll have to free by yourself all objects and memory blocks given back from the engine, which are not const –Objects and memory blocks which are const are controlled by the GAT engine, you don’t want to free these Representation in C –First temptation to have: GATFile and GATFile const but this doesn’t give, what we want: typedef struct GATFile_S * GATFile; so ‚GATFile‘ const would be ‚GATFile_S * const‘ --- wrong! –As a result we’ve got: typedef struct GATFile_S * GATFile; typedef struct GATFile_S const * GATFile_const;

October, 24th 2005Digging into the GAT API22 Const correctness – C++ Const correctness introduced wherever possible, allows compiler to catch errors early Natural representation in C++ GAT::File, GAT::File const

October, 24th 2005Digging into the GAT API23 Const correctness - Python Const correctness not an issue (not supported by the language)

October, 24th 2005Digging into the GAT API24 Error Handling Every method (except constructor, destructor and certain simple accessors) return a GATResult value –Is a structured 32 bit unsigned int: Every CPI based object has additional error tracking inside the associated GAT Context: –Allows to print an error trace back of the full error history GATContext_GetCurrentStatus(context, &status); GATStatus_ErrorTrace(status);

October, 24th 2005Digging into the GAT API25 Error Handling (Explicit) - C #include GATResult RemoteFile_GetFile (GATContext context, char const *source_url, char const *target_url) { GATResult rc = GAT_FAIL; GATStatus status = NULL; GATLocation source = GATLocation_Create (source_url); GATLocation target = GATLocation_Create (target_url); GATFile file = GATFile_Create (context, source, NULL); if (NULL == source || NULL == target || NULL == file) { GATCreateStatus(“RemoteFile_GetFile”, &status, GAT_MEMORYFAILURE, context, __FILE__, __LINE__); return GATContext_SetCurrentStatus (context, &status); } rc = GATFile_Copy(file, target, GATFileMode_Overwrite); if (GAT_FAILED(rc)) { GATCreateStatus(“RemoteFile_GetFile”, &status, rc, context, __FILE__, __LINE__); return GATContext_SetCurrentStatus (context, &status); } GATFile_Destroy (&file); GATLocation_Destroy (&target); GATLocation_Destroy (&source); return GAT_SUCCEEDED; }

October, 24th 2005Digging into the GAT API26 Error Handling (macros) - C #include GATResult RemoteFile_GetFile (GATContext context, char const *source_url, char const *target_url) { GAT_USES_STATUS(context, “RemoteFile_GetFile”); GATLocation source = GATLocation_Create (source_url); GATLocation target = GATLocation_Create (target_url); GATFile file = GATFile_Create (context, source, NULL); if (NULL == source || NULL == target || NULL == file) { GAT_CREATE_STATUS(GAT_MEMRORYFAILURE); } else { GAT_CREATE_STATUS(GATFile_Copy(file, target, GATFileMode_Overwrite)); } GATFile_Destroy (&file); GATLocation_Destroy (&target); GATLocation_Destroy (&source); return GAT_RETURN_STATUS(); }

October, 24th 2005Digging into the GAT API27 Error Handling – C++ #include GAT::Result RemoteFile_GetFile (GAT::Context context, char const *source_url, char const *target_url) { try { GAT::Location source (source_url); GAT::Location target (target_url); GAT::File file (context, source); file.Copy(target); } catch (GAT::Exception const &e) { std::cout << e.what() << std::endl; return e.GetResult(); } return GAT_SUCCEEDED; }

October, 24th 2005Digging into the GAT API28 Error Handling – Python import GAT def RemoteFile_GetFile (context, source_url, target_url): try: source = GAT.Location(source_url); target = GAT.Location(target_url); file = GAT.File(context, source); file.Copy(target); except GAT.Status, err: print err.args[0].message print err.args[0].traceback return err.args[0].errcode return GAT.SUCCEEDED;

October, 24th 2005Digging into the GAT API29 Demo File Transfer Application –Using different languages (C, C++, Python)