Introduction to Scientific Programming
Introduction to Scientific Programming
Programming
1
Outline
• Knowledge Base
• Programming Models
• C/C++ Language
2
3
https://www.cs.cmu.edu/~15110-s13/Wing06-ct.pdf
• https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-fall-2011/
• https://www3.cs.stonybrook.edu/~pramod.ganapathi/CSE595.html
Parallel Computing
• a process of breaking down larger problems into smaller,
independent, often similar parts that can be executed simultaneously
by multiple processing/computing elements communicating via some
communication mechanisms (e.g., shared memory, TCP/IP protocol);
the results of which are combined upon completion as part of an
overall algorithm.
4
Why Parallel Computing
• Solving an 𝑛𝑛 × 𝑛𝑛 linear system Ax=b by using
1 3
Gaussian elimination takes ≈ 𝑛𝑛 flops.
3
• On Core i7 975 @ 4.0 GHz, which is capable of about
60-70 Gigaflops (109flops)
𝑛𝑛 flops time
1000 3.3×108 0.006 seconds
1000000 3.3×1017 57.9 days
5
Different Levels of Parallelism
1. Task/thread level parallelism (e.g., multicore + distributed memory/shared
memory).
6
Languages for Writing Parallel Code
• Common solutions for creating parallel code are OpenMP (open multi-
processing), MPI (message passing interface) and GPUs (graphics processing
unit). Solutions are limited to the C/C++ or Fortran programming languages.
• Both support OOP. But ??? vs. generic programming, template (good for reusable codes) etc.
Knowledge Base
1. Computer Architecture (von Neumann machine)
• Memory, CPU, Op., process/thread, file system, I/O.
• See appendix: S_4_Comp_Arch
2. Declarative (code declares properties of the desired result, but not how
to compute it)
• Functional programming
https://en.wikipedia.org/wiki/Programming_paradigm
11
Procedural Programming Model
• implement the behavior of a computer program as procedures (functions, subroutines)
that call each other. The resulting program is a series of steps that forms a hierarchy of calls
to its constituent procedures.
• Development practices (enhance quality and lower development and maintenance costs.)
• Modularity and scoping
Modularity is about organizing the procedures of a program into separate modules -- each of which has a
specific and understandable purpose. Minimizing the scope of variables and procedures can enhance
software quality by reducing the cognitive load of procedures and modules. A program lacking modularity
or wide scoping tends to have procedures that consume many variables that other procedures also
consume. The resulting code is relatively hard to understand and to maintain.
• Sharing/reuse
specify a well-defined interface and be self-contained -- in particular via the software library.
12
Object-Oriented Programming (OOP)
• based on the concept of objects, which can contain data and code: data in
the form of fields (often known as attributes or properties), and code in
the form of procedures (often known as methods). Computer programs
are designed by making them out of objects that interact with one
another.
• Languages: C++, Java, Python,…
13
Functional Programming
• programs are constructed by applying and composing functions. It is a
declarative programming paradigm in which function definitions are
trees of expressions that map values to other values, rather than a
sequence of imperative statements which update the running state of
the program.
• Languages: C++11, Java, Python, Fortan95, …
• lambda, map, closures, functools
14
Program Flow Chart
http://www.programiz.com/article/flowchart-programming
15
16
17
• OOP: C++ class hierarchy; abstraction … (finite difference for Poisson equation)
• Final project