340 likes | 355 Views
Computational Methods in Astrophysics ASTR 5210. Dr Rob Thacker (AT319E) thacker@ap. 1 st Homework. Will put up on website at lunch Questions on Flynn’s taxonomy 2’s complement Rounding Floating point formats Testing rounding modes of fortran compilers. Today’s Lecture.
E N D
Computational Methods in Astrophysics ASTR 5210 Dr Rob Thacker (AT319E) thacker@ap
1st Homework • Will put up on website at lunch • Questions on • Flynn’s taxonomy • 2’s complement • Rounding • Floating point formats • Testing rounding modes of fortran compilers
Today’s Lecture • Programming paradigms and high level languages • What is expected? • You develop some knowledge of the different paradigms and what they mean • Pros and cons etc • You aren’t expected to be a whiz at programming in all programming styles
Programming Paradigms • Paradigm is used in many different ways, according to Webster’s: • “a philosophical and theoretical framework of a scientific school or discipline within which theories, laws, and generalizations and the experiments performed in support of them are formulated; broadly : a philosophical or theoretical framework of any kind” • A programming paradigm is a way of conceptualizing what it means to perform computation, and how tasks that are to be carried out on a computer should be structured and organized. • The same paradigm can be encapsulated in many different languages
How could we look at program execution? • We can view computation in many different ways, for example: • A list of instructions to be executed • A set of expressions to be evaluated • A list of rules to be applied • A set of objects to interact • A set of messages to be sent/received
Why look at programming paradigms? • Different paradigms provide different features and benefits • Understanding the commonality makes it easier to learn new languages • Some paradigms are more appropriate for one problem than another, allows you to select best approach • Object oriented great for database design • Imperative great for scientific programming • Address problems that are not easily tractable in one paradigm versus another • This will help develop the link between your algorithm knowledge and your programming skills
Influences of the programming paradigm • Programming technique • Frequently influenced by algorithmic approaches to solving a particular problem • Programming style • How are ideas expressed within the program? • Can we encapsulate ideas elegantly (or not?) • Programming culture
Four main programming paradigms • Although there are actually many programming paradigms, a reasonable division is as follows • The imperative paradigm • Includes the procedural paradigm • Includes the object-oriented paradigm • The declarative paradigm • Includes the functional paradigm • Includes the logical paradigm • Wikipedia has a long list of other possibilities if you are interested
Imperative programming • This was the first programming paradigm developed and represents a common view of programming • e.g. First do this and next do that • Similar in approach to instructions on how to make things, cook a meal, repair a car and so on • Strongly motivated by von Neumann architecture and a program counter stepping through a list of instructions • Can only support a low level of abstraction because the model is closely associated with computer design • The idea of a procedure is the most natural abstraction • A group of commands is abstracted into a single command • Commonly called procedural programming
Declarative: Functional Programming • Derived from mathematics and theory of functions • e.g. evaluate an expression and use the result • Programming is about defining relationships between data, rather than the steps involved • Very different feel to imperative programming • Functional programming is atemporal • Concepts derived from work by Alonzo Church in 1930s (Lambda calculus) • Beginning to filter into the mainstream, the Linspire OS team use the Haskell language for development
Contrasting functional and imperative programming # imperative style (Python) target = [] # create empty list for item in source_list: # iterate over each thing in source trans1 = G(item) # transform the item with the G() function trans2 = F(trans1) # second transform with the F() function target.append(trans2) # add transformed item to target A functional version has a different feel to it: # functional style (Python) def compose2(F, G): # FP-oriented languages often have standard compose() def C(item): # here we create utility-function for composition return F(G(item)) return C target = map(compose2(F,G), source_list) From Wikipedia
Strongly oriented toward AI, and designed around the formal concepts of proofs and axioms Can ask for a given hypothesis whether it is consistent with the underlying system i.e. we give a series of facts and relationships, and then ask whether another relationship is true is_a(abel,male). is_a(adam,male). ... etc ... is_a(amanda,female). is_a(anjali,female). ... etc ... is_father_of(abel,bert). is_father_of(abel,belinda). ... etc ... is_mother_of(anjali,bill). is_mother_of(anjali,bridget). ... etc ... is_grandparent_of(X, Z) :- is_parent_of(X, Y), is_parent_of(Y, Z). is_parent_of(X, Y) :- is_mother_of(X, Y). is_parent_of(X, Y) :- is_father_of(X, Y). Logical programming Example of a Prolog program
Object oriented programming (OOP) • No single agreed definition on what constitutes OOP • Based upon concepts and how humans interact with real world phenomena • If you simulate a system then each physical object in the system should be represented by a computational object • Shifts the idea of stepping through commands (instruction flow) to describing interactions between objects and methods (which are effectively functions) that describe an objects actions • In this paradigm there is a distinct relationship between data and functions (both are encapsulated within the object) • In much imperative-style programming this isn’t true
Common OOP terminology • How do we define an object? • Objects are a single instance of a class • Think of your dog “Fido” being an instance of the class of the dogs • The dog class will include all information about dogs and the messages they can send (by the bark method for example) • Classes allow us to define inheritance • Once you’ve defined dog, you might want to define the sub-class of poodles • This is efficient because you can reuse much of the information • This is the reason why object oriented methods are effective for very large software developments
Diagramatically Class e.g. tree class Object Pine object Methods Data specifications Has a height of x metres 5m height Data declarations OOP is sufficiently important that we will come back to it later
Set-oriented view of the paradigms Imperative Declarative Assembler Fortran Basic Functional Lisp Procedural Algol Excel Haskell Pascal C Object- oriented Logic Simula Prolog C++ Java
Have programming paradigms influenced machine design? • Not as much as you would think • By using translators to increase abstraction from the machine code level sophisticated logic systems can be implemented quite “easily” • However, the LISP language did spawn a number of dedicated computers because of its huge memory requirements • However, increasing availability of cheap memory has made this less worthwhile MIT LISP machine from Dan Keller’s homepage
High-level Languages • High-level languages were the natural progression from machine code and assembly language • Early in their development it was very clear that performance was an issue • FORTRAN, LISP & COBOL were the first three “big” HLLs • Idea: take a higher-level description of what we want to do, and let the computer translate it into the machine language as specified before • Called compiler because it might take a single high-level command, and compile a sequence of low-level commands • Input high-level language as text, store binary commands in executable file • Of course, we now also interpret commands on the fly
The Emergence of High-Level Sequential Languages The designers of the very first high level programming language were aware that their success depended on acceptable performance of the generated target programs: John Backus (1957): “… It was our belief that if FORTRAN … were to translate any reasonable scientific source program into an object program only half as fast as its hand-coded counterpart, then acceptance of our system would be in serious danger …” High-level algorithmic languages became generally accepted standards forprogramming as their advantages outweighed any performance drawbacks
Humour: How to shoot yourself in the foot • FORTRAN • You shoot yourself in each toe, iteratively, until you run out of toes, then you read in the next foot and repeat. If you run out of bullets, you continue anyway because you have no exception handling ability. • COBOL • USEing a COLT 45 HANDGUN, AIM gun at LEG.FOOT, THEN place ARM.HAND.FINGER on HANDGUN.TRIGGER and SQUEEZE. THEN return HANDGUN to HOLSTER. CHECK whether shoelace needs to be retied.
Humour: couple of other examples • JAVA • You attempt to shoot yourself in the foot using a bullet that will work in any gun in the world. But you discover that the "Microsoft Gun" is actually a cross bow. • FORTH • Foot in yourself shoot.
FORTRAN • FORTRAN (1953-1958, led by John Backus) • FORTRAN is a shortened form of FORmula TRANslater • First compiled high-level language • Fortran I (1956)50 years of FORTRAN! • I/O could formatted easily (ie print to screen, printer, file) • 6 characters variable names • The type of variables (i.e. floating point, integer) were implicitly declared by the first letter in the name of the variable • Close to the speed of assembly in many cases • Forced a simple programming style though • Fortran II (1958) • Added subroutines & fixed a few bugs
Evolution of FORTRAN • The development of other languages occurred while FORTRAN was still being used – why? • FORTRAN was initially very limited in terms of the data structures it could support • Handling of anything other than numbers was clumsy at best, absolutely hideous at worst (converting a numeric value to a string remains problematic) • FORTRAN IV (1962), 77 (1977), 90 (1990) all added new features, and latest versions include newer programming paradigms (such as object oriented programming) • New standards are still being defined (e.g. FORTRAN 2005) • Extremely efficient and readable • Once you’ve written one program it all seems easy
COBOL: The world’s most common language • COBOL (1959, CODASYL committee) • Common Business Language • Designed by committee, but heavily influenced by the ideas of (Rear Admiral) Grace Murray Hopper • It was estimated in that late 1990’s that 80% of all the lines of programming ever written were COBOL • about 240 billion lines • Designed to be easy to use, and uses words rather than formulas • Many useful features like macros, hierarchical data structures
C: Bringing together good ideas • C (1972, Dennis Ritchie) • Developed at Bell Labs • Derived from earlier languages (BCPL & B) using a best practices type methodology • No new features but the design was well thought out • C is closely tied to UNIX, and second generations of UNIX were written in C • Portable, which has lead to it being very widely used • Some times referred to as a “mid” level language • Interface to the operating system is easy to use
C++: Objects go mainstream • C++ (1982-1985, Bjarne Stroustroup) • Another development from Bell Labs • Motivation: a better way to deal with codes that had hundreds of thousands of lines • Windows 2000 has more than 30 million lines of code! • Layered OOP ideas implemented in the simula67 language onto C • Initially developed as a translator from C++ to C • Later versions compiled directly to linkable object files
Is there a “best” language to use? • We first need to set some criteria that we can use to evaluate languages with 1. Simplicity and Clarity: • Can the language be expressed & read easily? • Does an operator always mean the same thing? • How easy is the language to learn/teach? 2. Level of Abstraction: • Is the level of abstraction sufficient to enable new and powerful expressions? • Machine languages are very limited (only support “numbers”)
Efficiency & Specialization 3. Efficiency: • Can the language be implemented efficiently, or for that matter, can it even be implemented at all • Object oriented languages have tended to be very difficult to implement efficiently (C++ in particular) 4. Specialization: • What kind of facilities does the language provide for application specific operations? • FORTRAN90 has support for vector operations
Reliability & robustness 5. Reliability & robustness: • Does the program behave as anticipated under all circumstances? • Error checking is the key here • Does the compiler (or interpreter) check for errors (such as assigning the wrong type to a variable or an array address out of bounds) • What about dealing with exceptions? Can you make corrective action and continue or does the program need to exit? • This is a key issue, think about the Arianne 5 programming bug that we mentioned earlier!
Choose the best language for the application • This is why FORTRAN is still with us after 50 years! • It is still very efficient for many classes of scientific computation • C really doesn’t have that many advantages in this area • What about business applications? • Visual basic, COBOL • During my time at J.P. Morgan some of the traders wrote their option pricing software in VBasic • What about the internet? • Then you need something that deals with information processing easily: HTML, javascript, XML • What about AI? • Then you need a language that deals with logic and abstraction: LISP or Prolog
What is the “fastest” language? • Depends what you mean by fastest… FORTRAN C Time to solution should be the guide C++ Code execution speed Java Matlab Python Perl Programming speed
Why did Python become so popular? • It isn’t new (1991!) • Offers a lot of flexibility • It has a comparatively simple programming style (not lots of weird characters to remember) • Libraries often can add “good” performance • Main issue: starting to be adopted outside of academic => useful skill
Summary • At highest level programming paradigms separate into imperative and declarative models • Imperative model is strongly influenced by machine design • Declarative models are influenced by the desire to solve logic and arithmetic problems • Optimal solutions to problem requires an appreciation for the available paradigms and languages • There is no one best language, but we can choose on the basis of clarity, abstraction level, efficiency, specialization and reliability • High level languages began with FORTRAN, which continues to evolve 50 years later