Teadusarvutusedkodu.ut.ee/~eero/SC/lecture01.pdf17Introduction1.5 Classification of languages 1.5...
Transcript of Teadusarvutusedkodu.ut.ee/~eero/SC/lecture01.pdf17Introduction1.5 Classification of languages 1.5...
University of Tartu Institute of Computer Science
Scientific ComputingTeadusarvutused
Tartu, 2013
2 Practical information
Lectures: Liivi 2 - 207 Wed 10:15Computer classes: Liivi 2 - 205, Thu 16:15 – Oleg Batrašev [email protected] eapFinal grade forms from :
1. Active partitipation at lectures ≈ 10%
2. Exam
3. Computer class exercises
4. Project work
3 Introduction 1.1 Course overview
Course homepage (http://www.ut.ee/~eero/SC/)
1 Introduction
Scientific Computing = Science + Engineering (Computer Science) +Mathematics
1.1 Course overview
4 Introduction 1.1 Course overview
1
1by Prof. Rob Scheichl
5 Introduction 1.1 Course overview
Lectures:
• NumPy, SciPy
• Scientific Computing - an Overview
6 Introduction 1.1 Course overview
• Large problems in Linear Algebra, condition number
• Algorithm Complexity Analysis
• Memory hierarchies and making use of it
• BLAS and LAPACK libraries with some examples
• Numerical integration and differentiation
• Numerical solution of differential and integral equations
• Using parallel computers for large problems
• Parallel programming models
• MPI (Message Passing Inteface)
7 Introduction 1.2 Literature
1.2 Literature
Scientific Computing:
1. Victor Eijkhout, Introduction to High Performance Scientific Computing, 2010(Available online e.g. through http://www.lulu.com)
2. RH Landau, A First Course in Scientific Computing. Symbolic, Graphic, andNueric Modeling Using Maple, Java, Mathematica, and Fortran90. PrincentonUniversity Press, 2005.
3. LR Scott, T Clark, B Bagheri. Scientific Parallel Computing. Princenton Uni-versity Press, 2005.
4. MT Heath, Scientific Computing; ISBN: 007112229X, McGraw-Hill Compa-nies, 2001.
8 Introduction 1.2 Literature
5. JW Demmel, Applied Numerical Linear Algebra; ISBN: 0898713897, Societyfor Industrial & Applied Mathematics, Paperback, 1997.
Python:
1. Hans Petter Langetangen, Python Scripting for Computational Science. ThirdEdition, Springer 2008. Raamatu veebisait (http://folk.uio.no/hpl/scripting/).
2. Neeme Kahusk, Sissejuhatus Pythonisse (http://www.cl.ut.ee/inimesed/nkahusk/sissejuhatus-pythonisse/).
3. Brad Dayley, Python Phrasebook, Sams 2007.
4. Travis E. Oliphant, Guide to NumPy (http://www.tramy.us), TrelgolPublishing 2006
9 Introduction 1.2 Literature
MPI:
1. W Gropp, E Lusk, A Skjellum, Using MPI, MIT Press, 1994.
2. MPI Commands. (http://www-unix.mcs.anl.gov/mpi/www/index.html)
10 Introduction 1.3 Scripting vs programming
1.3 Scripting vs programming
1.3.1 What is a script?
• Very high-level, often short, programwritten in a high-level scripting language
• Scripting languages:
– Unix shells,
– Tcl,
– Perl,
– Python,
– Ruby,
– Scheme,
– Rexx,
– JavaScript,
– VisualBasic,
– ...
• This course: Python
11 Introduction 1.3 Scripting vs programming
1.3.2 Characteristics of a script
• Glue other programs together
• Extensive text processing
• File and directory manipulation
• Often special-purpose code
• Many small interacting scripts may yield a big system
• Perhaps a special-purpose GUI on top
• Portable across Unix, Windows, Mac
• Interpreted program (no compilation+linking)
12 Introduction 1.3 Scripting vs programming
1.3.3 Why not stick to Java, C/C++ or Fortran?
Features of Perl and Python compared with Java, C/C++ and Fortran:
• shorter, more high-level programs
• much faster software development
• more convenient programming
• you feel more productive
Two main reasons:
• no variable declarations,but lots of consistency checks at run time
• lots of standardized libraries and tools
13 Introduction 1.4 Scripts yield short code
1.4 Scripts yield short code
Consider reading real numbers from a file, where each line can contain an arbitrarynumber of real numbers:� �1.1 9 5.2
1.762543E-02
0 0.01 0.001 9 3 7�Python solution:� �
F = open(filename, ’r’)
n = F.read().split()�Perl solution:� �
open F, $filename;
14 Introduction 1.4 Scripts yield short code
$s = join "", <F>;
@n = split ’ ’, $s;�...Doing this in C++ or Java requires at least a loop, and in Fortran and C quite
some code lines are necessary
1.4.1 Using regular expressions
Suppose we want to read complex numbers written as text(-3, 1.4) or (-1.437625E-9, 7.11) or ( 4, 2 )
Python solution:� �m = re.search(r’\(\s*([^,]+)\s*,\s*([^,]+)\s*\)’,
’(-3,1.4)’)
re, im = [float(x) for x in m.groups()]�Perl solution:
15 Introduction 1.4 Scripts yield short code
� �$s="(-3, 1.4)";
($re,$im)= $s=~ /\(\s*([^,]+)\s*,\s*([^,]+)\s*\)/;�
16 Introduction 1.4 Scripts yield short code
Regular expressions like� �\(\s*([^,]+)\s*,\s*([^,]+)\s*\)�
constitute a powerful language for specifying text patterns
• Doing the same thing, without regular expressions, in Fortran and C requiresquite some low-level code at the character array level
Remark: we could read pairs (-3, 1.4) without using regular expressions,� �s = ’(-3, 1.4 )’
re, im = s[1:-1].split(’,’)�
17 Introduction 1.5 Classification of languages
1.5 Classification of languages
Many criteria to classify computer languages
• Dynamically vs statically typed languages
Python (dynamic):� �c = 1 # c is an integer
c = [1,2,3] # c is a list�C (static):� �
double c; c = 5.2; // c can only hold doubles
c = "a string..." // compiler error�
18 Introduction 1.5 Classification of languages
• Weakly vs strongly typed languages
Perl (weak):� �$b = ’1.2’
$c = 5*$b; # implicit type conversion: ’1.2’ -> 1.2�Python (strong):� �
b = ’1.2’
c = 5*b #
’1.21.21.21.21.2’ - no implicit type conversion�• Interpreted vs compiled languages
• High-level vs low-level languages (Python-C)
• Very high-level vs high-level languages (Python-C)
19 Introduction 1.5 Classification of languages
• Scripting vs system languages
• Object-oriented vs functional vs procedural
20 Introduction 1.6 Performance issues
1.6 Performance issues
1.6.1 Scripts can be slow
• Perl and Python scripts are first compiled to byte-code
• The byte-code is then interpreted
• Text processing is usually as fast as in C
• Loops over large data structures might be very slow
� �for i in range(len(A)):
A[i] = ...�
21 Introduction 1.6 Performance issues
• Fortran, C and C++ compilers are good at optimizing such loops at compiletime and produce very efficient assembly code (e.g. 100 times faster)
• Fortunately, long loops in scripts can easily be migrated to Fortran or C
1.6.2 Scripts may be fast enough
Read 100 000 (x,y) data from file and write (x,f(y)) out again
• Pure Python: 4s
• Pure Perl: 3s
• Pure Tcl: 11s
• Pure C (fscanf/fprintf): 1s
• Pure C++ (iostream): 3.6s
• Pure C++ (buffered streams): 2.5s
22 Introduction 1.6 Performance issues
• Numerical Python modules: 2.2s (!)
• Remark: in practice, 100 000 data points are written and read in binary format,resulting in much smaller differences
1.6.3 When scripting is convenient
• The application’s main task is to connect together existing components
• The application includes a graphical user interface
• The application performs extensive string/text manipulation
• The design of the application code is expected to change significantly
• CPU-time intensive parts can be migrated to C/C++ or Fortran
• The application can be made short if it operates heavily on list or hash structures
23 Introduction 1.6 Performance issues
• The application is supposed to communicate with Web servers
• The application should run without modifications on Unix, Windows, and Mac-intosh computers, also when a GUI is included
1.6.4 When to use C, C++, Java, Fortran
• Does the application implement complicated algorithms and data structures?
• Does the application manipulate large datasets so that execution speed iscritical?
• Are the application’s functions well-defined and changing slowly?
• Will type-safe languages be an advantage, e.g., in large development teams?
24 Python in SC 2.1 Numerical Python (NumPy)
2 Python in Scientfic Computing
2.1 Numerical Python (NumPy)
• NumPy enables efficient numerical computing in Python
• NumPy is a package of modules, which offers efficient arrays (contiguous stor-age) with associated array operations coded in C or Fortran
• There are three implementations of Numerical Python
• Numeric from the mid 90s (still widely used)
• numarray from about 2000
• numpy from 2006 (the new and leading implementation)
• We recommend to use numpy (by Travis Oliphant)
25 Python in SC 2.1 Numerical Python (NumPy)
� �1 # A taste of NumPy: a least-squares procedure
2 from scitools.all import *
3 n = 20 ; x = linspace(0.0, 1.0, n) # coordinates
4 y_line = -2*x + 3
5 y = y_line + random.normal(0, 0.25, n) # line with noise
6 # create and solve least squares system:
7 A = array([x, ones(n)])
8 A = A.transpose()
9 result = linalg.lstsq(A, y)
10 # result is a 4-tuple, the solution (a,b) is the 1st entry:
11 a, b = result[0]
12 plot(x, y, ’o’, # data points w/noise
13 x, y_line, ’r’, # original line
14 x, a*x + b, ’b’) # fitted lines
15 legend(’data points’, ’original line’, ’fitted line’)
16 hardcopy(’vahimruut.png’)�
26 Python in SC 2.1 Numerical Python (NumPy)
Resulting plot: