INF3380: Parallel Programming for Scientific Problems

Similar documents
INF3380: Parallel Programming for Natural Sciences

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Basic Communication Operations Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture 28: Introduction to the Message Passing Interface (MPI) (Start of Module 3 on Distribution and Locality)

Consultation for CZ4102

High Performance Computing and Programming, Lecture 3

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

CSE P 501 Exam 12/1/11

Principle of Polyhedral model for loop optimization. cschen 陳鍾樞

22. Two-Dimensional Arrays. Topics Motivation The numpy Module Subscripting functions and 2d Arrays GooglePage Rank

Shared-memory Parallel Programming with Cilk Plus

Lecture 3: C Programm

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Pace University. Fundamental Concepts of CS121 1

OpenACC Course. Office Hour #2 Q&A

Lectures 5-6: Introduction to C

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Lecture 7: More about MPI programming. Lecture 7: More about MPI programming p. 1

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

Limitations of Memory System Performance

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

CS 223: Data Structures and Programming Techniques. Exam 2

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Lectures 5-6: Introduction to C

Dr Tay Seng Chuan Tel: Office: S16-02, Dean s s Office at Level 2 URL:

BLM2031 Structured Programming. Zeyneb KURT

Motivation was to facilitate development of systems software, especially OS development.

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Arrays and Pointers in C & C++

Dense Matrix Algorithms

Signals, Instruments, and Systems W2. C Programming (continued)

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Class Information ANNOUCEMENTS

Motivation was to facilitate development of systems software, especially OS development.

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Government Polytechnic Muzaffarpur.

JCudaMP: OpenMP/Java on CUDA

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 2. Memory locality optimizations Address space organization

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Shared-memory Parallel Programming with Cilk Plus

First, the need for parallel processing and the limitations of uniprocessors are introduced.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Sums. Here's a (poor) program to add three numbers

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

C++ Tutorial AM 225. Dan Fortunato

Parallel Programming. Exploring local computational resources OpenMP Parallel programming for multiprocessors for loops

Introduction to Parallel Computing

Pointers, Dynamic Data, and Reference Types

Lecture 2: C Programm

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

Introduction to Programming Using Java (98-388)

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

ARRAYS(II Unit Part II)

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Introduction to C An overview of the programming language C, syntax, data types and input/output

Inf1-OOP. Arrays 1. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

Inf1-OOP. Arrays. Many Variables of the Same Type. Arrays Introduction to Arrays Arrays in Java Shuffling a Deck Multidimensional Arrays Summary/Admin

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community


Dynamic memory allocation (malloc)

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

EL2310 Scientific Programming

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

High Performance Computing in C and C++

Self-assessment Exercise

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Principles of Programming. Chapter 6: Arrays

ECEN 449 Microprocessor System Design. Review of C Programming

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Computers Programming Course 6. Iulian Năstac

Warm-up sheet: Programming in C

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

Introduction: The Unix shell and C programming

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Introduction to parallel computers and parallel programming. Introduction to parallel computersand parallel programming p. 1

C Programming Review CSC 4320/6320

Main differences with Java

1. Lab exercise C programming language

Leftover. UMA vs. NUMA Parallel Computer Architecture. Shared-memory Distributed-memory

Parallel and Distributed Computing Programming Assignment 1

EECS2031 Software Tools

C introduction: part 1

Dynamic memory allocation

Transcription:

INF3380: Parallel Programming for Scientific Problems Xing Cai Simula Research Laboratory, and Dept. of Informatics, Univ. of Oslo INF3380: Parallel Programming for Scientific Problems p. 1

Course overview & recap of serial programming Course overview & recap of serial programming p. 2

Motivations Many problems in natural sciences can benefit from large-scale computations more details better accuracy more advanced models Example of huge computations: Detailed weather analysis of the entire globe surface area: 510,072,000 km 2 spatial resolution 1 1km 2 5.1 10 8 small patches spatial resolution 100 100m 2 5.1 10 10 small patches additional layers in the vertical direction high resolution in the time direction Traditional single-cpu computers are limited in capacity typical clock frequency 2 4 GHz typical memory size 4 16 GB Course overview & recap of serial programming p. 3

Motivations (cont d) Parallel computers are now everywhere! CPUs nowadyas have more than one core on a chip One computer may have several multicore chips There are also accelerator-based parallel architectures GPGPU Clusters of different kinds Course overview & recap of serial programming p. 4

An example of multicore-based cluster Core Core Core Core Core Core Core Core Cache Cache Cache Cache Bus Memory Compute Node Core Core Core Core Core Core Core Core Interconnect Network Cache Cache Cache Cache Bus Memory Compute Node Core Core Core Core Core Core Core Core Cache Cache Cache Cache Bus Memory Compute Node Course overview & recap of serial programming p. 5

Why learning parallel programming? Parallel computing a form of parallel processing by concurrently utilizing multiple computing units for one computational problem shortening computing time solving larger problems However... modern multicore-based computers are good at multi-tasking, but not good at automatically computing one problem in parallel automatic parallelization compilers have had little success special parallel programming languages have had little success serial computer programs have to be modified or rewritten to utilize parallel computers Learning parallel programming is thus important! Course overview & recap of serial programming p. 6

What will you learn in INF3380? An introduction to parallel programming important concepts basic parallel programming skills (MPI and OpenMP) use of multicore PCs and PC clusters a peek into GPU computing After finishing the course, you should be able to write simple parallel programs You should also be able to learn more about advanced parallel programming on your own later Course overview & recap of serial programming p. 7

Teaching approaches Focus on fundamental issues parallel programming = serial programming + finding parallelism + enforcing work division and collaboration Use of examples relevant for natural sciences mathematical details are not required understanding basic numerical algorithms is needed implementing basic numerical algorithms is essential Hands-on programming exercises and tutoring Course overview & recap of serial programming p. 8

Some important info Textbook: Ananth Grama, George Karypis, Vipin Kumar and Anshul Gupta, Introduction to Parallel Computing, 2nd edition, Addison Wesley, 2003 Lecture slides Two mandatory assignments Written exam with grades A F Course overview & recap of serial programming p. 9

Recapitulation of serial programming Recapitulation of serial programming p. 10

What is serial programming? Roughly, a computer program executes a sequence of operations applied to data structures A program is normally written in a programming language Data structures: variables of primitive data types (char, int, float, double etc.) variables of composite and abstract data types (struct in C, class in Java & Python) array variables Operations: statements and expressions functions Recapitulation of serial programming p. 11

Variables In a dynamically typed programming language (e.g. Python) variables can be used without declaration beforehand a = 1.0 b = 2.5 c = a + b In statically typed languages (e.g. Java and C) declaration of variables must be done first double a, b, c; a = 1.0; b = 2.5; c = a + b; Recapitulation of serial programming p. 12

Simple example Suppose we have temperature measurement for each hour during a day t 1 is the temperature at 1:00 o clock, t 2 is the temperature at 2:00 o clock, and so on. How to find the average temperature of the day? We need to first add up all the 24 temperature measurements: T = t 1 +t 2 +...+t 24 = 24 i=1 t i The average temperature can then be calculated as T 24. Recapitulation of serial programming p. 13

Simple example (cont d) How to implement the calculations as a computer program? First, create an array of 24 floating-point numbers to store the 24 temperatures. That is, t[0] stores t 1, t[1] stores t 2 and so on. Note that array index starts from 0! Sum up all the values in the array t Same syntax for the computational loop in Java & C: T = 0; for (i=0; i<24; i++) T = T + t[i]; Syntax for Python: T = 0 for i in range(0,24): T = T + t[i] Finally, t average = T/24; Recapitulation of serial programming p. 14

Similarities and differences between languages For scientific applications, arrays of numerical values are the most important basic building blocks of data structures Extensive use of for-loops for doing computations Different syntax details allocation and deallocation of arrays Java: double[] v=new double[n]; C: double *v=malloc(n*sizeof(double)); Python: v=zeros(n,dtype=float64) (using NumPy) definition of composite and abstract data types I/O Recapitulation of serial programming p. 15

C as the main choice of programming language C is one of the dominant programming languages in computational sciences Syntax of C inspired many newer languages (C++, Java, Python) Good computational efficiency C is ideal for using MPI and OpenMP (also GPU programming) We will thus choose C as the main programming language This lecture will give a crash course on scientific programming, with syntax details in C Recapitulation of serial programming p. 16

Some words about pointers in C A variable in a program has a name and type, its value is stored somewhere in memory Type *p declares a pointer to a variable of datatype Type A pointer is actually a special type of variable, used to hold the memory address of a variable From a variable to its pointer: int a; int *p; p = &a; We can use a pointer to change the variable value *p = 2; A pointer can also be used to hold the memory address of the first entry of an array (such as returned by malloc) Array indexing: p[0], p[1]... Pointer arithmetic: int *p = (int*)malloc(10*sizeof(int)); int *p2 = p + 3; /* p2 is now pointing to p[3] */ Recapitulation of serial programming p. 17

Allocating multi-dimensional arrays (1) Let s allocate a 2D array for representing a m n matrix a 11 a 12... a 1n a 21 a 22... a 2n A =........ a m1 a m2... a mn Java: double[][] A = new double[m][n]; C: double **A = (double**)malloc(m*sizeof(double*)); for (i=0; i<m; i++) A[i] = (double*)malloc(n*sizeof(double)); Same syntax in Java and C for indexing and traversing a 2D array for (i=0; i<m; i++) for (j=0; j<n; j++) A[i][j] = i+j; Recapitulation of serial programming p. 18

Allocating multi-dimensional arrays (2) Use of NumPy makes array allocation very simple in Python from numpy import * A = zeros((m,n), dtype=float64) Indexing and traversing a 2D array in Python for i in range(0,m): for j in range(0,n): A[i,j] = i+j; Recapitulation of serial programming p. 19

More about two-dimensional arrays in C (1) C doesn t have true multi-dimensional arrays, a 2D array is actually an array of 1D arrays (like Java) A[i] is a pointer to row number i+1 It is also possible to use static memory allocation of fix-sized 2D arrays, for example: double A[10][8]; However, the size of the array is decided at compiler time (not runtime) Recapitulation of serial programming p. 20

More about two-dimensional arrays in C (2) Dynamic memory allocation of 2D arrays through e.g. malloc Another way of dynamic allocation, to ensure contiguous underlying data storage (for good use of cache): double *A_storage=(double*)malloc(n*n*sizeof(double)); double **A = (double**)malloc(n*sizeof(double*)); for (i=0; i<n; i++) A[i] = &(A_storage[i*n]); double**......... double* Recapitulation of serial programming p. 21

Deallocation of arrays in C If an array is dynamically allocated, it is important to free the storage when the array is not used any more Example 1 int *p = (int*)malloc(n*sizeof(int)); /*... */ free(p); Example 2 double **A = (double**)malloc(m*sizeof(double*)); for (i=0; i<m; i++) A[i] = (double*)malloc(n*sizeof(double)); /*... */ for (i=0; i<m; i++) free(a[i]); free(a); Be careful! Memory allocation and deallocation can easily lead to errors Recapitulation of serial programming p. 22

The form of a C program A program in C is made up of functions A stand-alone C program must at least implement function main, which will be executed by the operating system Functions are made up of statements and declarations Variables must be declared before usage Possible to use functions and variables declared in libraries Recapitulation of serial programming p. 23

Some syntax details in C Semicolon (;) terminates a statement Braces ({}) are used to group statements into a block Square brackets ([]) are used in connection with arrays Comments can be added between /* and */ Recapitulation of serial programming p. 24

Functions in C Function declaration specifies name, type of return value, and (optionally) a list of parameters Function definition consists of declaration and a block of code, which encapsulates some operation and/or computation return_type function_name (parameter declarations) { declarations of local variables statements } Recapitulation of serial programming p. 25

Function arguments All arguments to a C function are passed by value That is, a copy of each argument is passed to the function void function test (int i) { i = 10; } The change of i inside test has no effect when the function returns Passing pointers as function arguments can be used to get output void function test (int *i) { *i = 10; } The change of i inside test now has effect Recapitulation of serial programming p. 26

Function example 1: swapping two values void swap (int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; } Recapitulation of serial programming p. 27

Function example 2: smoothing a vector We want to smooth the values of a vector v by the following formula: v new i = v i +c(v i 1 2v i +v i+1 ), 2 i n 1 where c is a constant void smooth (double *v_new, double *v, int n, double c) { int i; for (i=1; i<n-1; i++) v_new[i] = v[i] + c*(v[i-1]-2*v[i]+v[i+1]); v_new[0] = v[0]; v_new[n-1] = v[n-1]; } Similar computations occur frequently in numerical computations Recapitulation of serial programming p. 28

Function example 3: matrix-vector multiplication We want to compute y = Ax, where A is a m n matrix, y is a vector of length m and x is a vector of length n: y i = A i1 x 1 +A i2 x 2 +...A in x n = n A ij x j, 1 i m j=1 void mat_vec_prod (double **A, double *y, double *x, int m, int n) { int i,j; for (i=0; i<m; i++) { y[i] = 0.0; for (j=0; j<n; j++) y[i] += A[i][j]*x[j]; } } Recapitulation of serial programming p. 29

Example of a complete C program #include <stdio.h> /* import standard I/O functions */ int myfunction(int x) /* define a function */ { int r; r = x*x + 2*x + 3; return r; } int main (int nargs, char** args) { int x,y; x = atoi(args[1]); /* read x from command line */ y = myfunction(x); /* invoke myfunction */ printf("x=%d, y=%d\n",x,y); return 0; } Recapitulation of serial programming p. 30

Compilation Suppose a file named first.c contains the C program Suppose we use GNU C compiler gcc Step 1: Creation of a file of object code: gcc -c first.c An object file named first.o will be produced. Step 2: Creation of the executable: gcc -o run first.o The executable will have name run. Alternatively (two steps in one), gcc -o run first.c Better to use the 2-step approach for complex examples Recapitulation of serial programming p. 31

Some important compiler options During compilation: Option -O turns on optimization flag of the compiler Option -c produces an object file for each source file listed Option -Ixxx suggests directory xxx for search of header files During linkage: Option -lxxx links with a specified library with name libxxx.a or libxxx.so Option -Lxxx suggests directory xxx for search of library files Option -o specifies the name of the resulting executable Recapitulation of serial programming p. 32