ME964 High Performance Computing for Engineering Applications

Similar documents
ME964 High Performance Computing for Engineering Applications

ECE/ME/EMA/CS 759 High Performance Computing for Engineering Applications

A Quick Introduction to C Programming

A Quick Introduction to C Programming

ME964 High Performance Computing for Engineering Applications

Linux and C Programming Language. Department of Computer Science and Engineering Yonghong Yan

Lecture XX: Linux and C Programming Language CSCE 790: Parallel Programming Models for Multicore and Manycore Processors

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Review of the C Programming Language for Principles of Operating Systems

High Performance Computing in C and C++

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Review of the C Programming Language

PRINCIPLES OF OPERATING SYSTEMS

CS 61c: Great Ideas in Computer Architecture

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Computers Programming Course 5. Iulian Năstac

EL6483: Brief Overview of C Programming Language

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Work relative to other classes

C++ Tutorial AM 225. Dan Fortunato

211: Computer Architecture Summer 2016

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Lecture 3: C Programm

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

CS 61C: Great Ideas in Computer Architecture Introduction to C

CS 0449 Sample Midterm

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Fundamental of Programming (C)

Chapter 2. Procedural Programming

Pointers. Pointers. Pointers (cont) CS 217

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Memory and C/C++ modules

Procedural programming with C

Homework #3 CS2255 Fall 2012

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

UNIT- 3 Introduction to C++

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

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

Computers Programming Course 6. Iulian Năstac

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Short Notes of CS201

CS 61c: Great Ideas in Computer Architecture

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Intermediate Programming, Spring 2017*

CS201 - Introduction to Programming Glossary By

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Final CSE 131B Spring 2004

COMP 2355 Introduction to Systems Programming

CSCI 171 Chapter Outlines

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays and Pointers. CSE 2031 Fall November 11, 2013

by Pearson Education, Inc. All Rights Reserved.

Arrays, Pointers and Memory Management

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

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

ECEN 449 Microprocessor System Design. Review of C Programming

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Introduction to C Language

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Embedded Systems - FS 2018

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Understanding Pointers

CS Programming In C

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Arrays and Pointers (part 1)

A Fast Review of C Essentials Part I

Reminder: compiling & linking

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

Lectures 5-6: Introduction to C

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Lecture 2: C Programm

Data Representation and Storage. Some definitions (in C)

Lecture Notes on Types in C

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Programming refresher and intro to C programming

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Object Reference and Memory Allocation. Questions:

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

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

Computer Systems Lecture 9

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

We do not teach programming

Programming in C - Part 2

CSE 303: Concepts and Tools for Software Development

A brief introduction to C programming for Java programmers

Arrays and Pointers (part 1)

Data Structure Series

Transcription:

ME964 High Performance Computing for Engineering Applications Quick Overview of C Programming January 20, 2011 Dan Negrut, 2011 ME964 UW-Madison There is no reason for any individual to have a computer in their home. Ken Olson, president and founder, Digital Equipment Corporation, 1977.

Before We Get Started Last time Course logistics & syllabus overview Discussed Midterm Projects Discrete Element Method on the GPU Collision Detection on the GPU Basic Finite Element Analysis on the GPU Sparse Linear Solver on the GPU Today Quick overview of C Programming Essential read: Chapter 5 of The C Programming Language (Kernighan and Ritchie) Acknowledgement: Slides on this C Intro include material due to Donghui Zhang and Lewis Girod Correction: Email your homework a this address: me964uw@gmail.com 2

Auditing the Course Why auditing? Large participation justifies another offering of this course Augments your experience with this class You can get an account on the GPU cluster You will be added to the email list Can post questions on the forum How to register for auditing: In order to audit a course, a student must first enroll in the course as usual. Then the student must request to audit the course online. (There is a tutorial available through the Office of the Registrar.) Finally, the student must save & print the form. Once they have obtained the necessary signatures, the form should be turned in to the Academic Dean in the Grad School at 217 Bascom. The Grad School offers more information on Auditing Courses in their Academic Policies and Procedures. Tutorial website: http://www.registrar.wisc.edu/isis_helpdocs/enrollment_demos/v90coursechangerequest/v90coursechangerequest.htm Auditing Courses: http://www.grad.wisc.edu/education/acadpolicy/guidelines.html#13 3

C Syntax and Hello World What do the < > mean? #include inserts another file..h files are called header files. They contain declarations/definitions needed to interface to libraries and code in other.c files. A comment, ignored by the compiler #include <stdio.h> /* The simplest C Program */ int main(int argc, char **argv) printf( Hello World\n ); return 0; The main() function is always where your program starts running. Blocks of code ( lexical scopes ) are marked by Return 0 from this function 4

Lexical Scoping Every Variable is Defined within some scope. A Variable cannot be referenced by name (a.k.a. Symbol) from outside of that scope. Lexical scopes are defined with curly braces. The scope of Function Arguments is the complete body of that function. The scope of Variables defined inside a function starts at the definition and ends at the closing brace of the containing block The scope of Variables defined outside a function starts at the definition and ends at the end of the file. Called Global Vars. void p(char x) /* p,x */ char y; /* p,x,y */ char z; /* p,x,y,z */ /* p */ char z; /* p,z */ void q(char a) char b; /* p,z,q,a,b */ char c; /* p,z,q,a,b,c */ char d; /* p,z,q,a,b,d (not c) */ /* p,z,q */ char b? legal? 5

Comparison and Mathematical Operators == equal to < less than <= less than or equal > greater than >= greater than or equal!= not equal && logical and logical or! logical not Beware division: 5 / 10 0 whereas 5 / 10.0 0.5 Division by 0 will cause a FPE + plus - minus * mult / divide % modulo & bitwise and bitwise or ^ bitwise xor ~ bitwise not << shift left >> shift right Don t confuse & and &&.. 1 & 2 0 whereas 1 && 2 <true> The rules of precedence are clearly defined but often difficult to remember or non-intuitive. When in doubt, add parentheses to make it explicit. 6

Assignment Operators x = y assign y to x x++ post-increment x ++x pre-increment x x-- post-decrement x --x pre-decrement x x += y assign (x+y) to x x -= y assign (x-y) to x x *= y assign (x*y) to x x /= y assign (x/y) to x x %= y assign (x%y) to x Note the difference between ++x and x++ (high vs low priority (precedence)): int x=5; int y; y = ++x; /* x == 6, y == 6 */ int x=5; int y; y = x++; /* x == 6, y == 5 */ Don t confuse = and ==! int x=5; if (x==6) /* false */ /*... */ /* x is still 5 */ int x=5; if (x=6) /* always true */ /* x is now 6 */ /*... */ 7

A Quick Digression About the Compiler #include <stdio.h> /* The simplest C Program */ int main(int argc, char **argv) printf( Hello World\n ); return 0; Preprocess Compilation occurs in two steps: Preprocessing and Compiling extension typedef unsigned long long int dev_t; extension typedef unsigned int uid_t; extension typedef unsigned int gid_t; extension typedef unsigned long int ino_t; extension typedef unsigned long long int ino64_t; extension typedef unsigned int nlink_t; extension typedef long int off_t; extension typedef long long int off64_t; extern void flockfile (FILE * stream) ; extern int ftrylockfile (FILE * stream) ; extern void funlockfile (FILE * stream) ; int main(int argc, char **argv) printf( Hello World\n ); return 0; my_program Compile In Preprocessing, source code is expanded into a larger form that is simpler for the compiler to understand. Any line that starts with # is a line that is interpreted by the Preprocessor. Include files are pasted in (#include) Macros are expanded (#define) Comments are stripped out ( /* */, // ) Continued lines are joined ( \ ) The compiler then converts the resulting text (called translation unit) into binary code the CPU can execute. 8

C Memory Pointers To discuss memory pointers, we need to talk a bit about the concept of memory We ll conclude by touching on a couple of other C elements: Arrays, typedef, and structs 9

The memory Memory: similar to a big table of numbered slots where bytes of data are stored. The number of a slot is its Address. One byte Value can be stored in each slot. Some data values span more than one slot, like the character string Hello\n A Type provides a logical meaning to a span of memory. Some simple types are: char char [10] int float int64_t a single character (1 slot) an array of 10 characters signed 4 byte integer 4 byte floating point signed 8 byte integer Addr Value 0 1 2 3 4 H (72) 5 e (101) 6 l (108) 7 l (108) 8 o (111) 9 \n (10) 10 \0 (0) 11 12 10

What is a Variable? symbol table? A Variable names a place in memory where you store a Value of a certain Type. You first Declare a variable by giving it a name and specifying its type and optionally an initial value declare vs. define char x; char y= e ; Type is single character (char) 11 Initial value Variable x declared but undefined Name What names are legal? extern? static? const? The compiler puts x and y somewhere in memory. Symbol Addr Value 0 1 2 3 x 4 Some garbage y 5 e (101) 6 7 8 9 10 11 12

Multi-byte Variables Different types require different amounts of memory. Most architectures store data on word boundaries, or even multiples of the size of a primitive data type (int, char) char x; char y= e ; int z = 0x01020304; 0x means the constant is written in hex padding An int requires 4 bytes Symbol Addr Value 0 1 2 3 x 4 Some garbage y 5 e (101) 6 7 z 8 4 9 3 10 2 11 1 12 12

Memory, a more detailed view A sequential list of words, starting from 0. 0 4 8 word 0 word 1 word 2 On 32bit architectures (e.g. Win32): each word is 4 bytes. Stack Local variables are stored in the stack Dynamically allocated memory is set aside on the heap (more on this later ) For multiple-byte variables, the address is that of the smallest byte (little endian). Heap 13

Example +3 +2 +1 +0 V4 V3 900 904 908 912 V1 V2 916 920 924 928 932 936 940 944 14

Another Example #include <iostream> int main() char c[10]; int d[10]; int* darr; darr = (int *)(malloc(10*sizeof(int))); size_t sizec = sizeof(c); size_t sized = sizeof(d); size_t sizedarr = sizeof(darr); What is the value of: - sizec - sized - sizedarr free(darr); return 0; NOTE: sizeof is a compile-time operator that returns the size, in multiples of the size of char, of the variable or parenthesized type-specifier that it precedes. 15

Can a C function modify its arguments? What if we wanted to implement a function pow_assign() that modified its argument, so that these are equivalent: float p = 2.0; /* p is 2.0 here */ p = pow(p, 5); /* p is 32.0 here */? float p = 2.0; /* p is 2.0 here */ pow_assign(p, 5); /* Is p is 32.0 here? */ Native function, to use you need #include <math.h> Would this work? void pow_assign(float x, uint exp) float result=1.0; int i; for (i=0; (i < exp); i++) result = result * x; x = result; 16

In C you can t change the value of any variable passed as an argument in a function call void pow_assign(float x, uint exp) float result=1.0; int i; for (i=0; (i < exp); i++) result = result * x; x = result; // a code snippet that uses above // function float p=2.0; pow_assign(p, 5); // the value of p is 2 here In C, all arguments are passed by value Keep in mind: pass by value requires the variable to be copied. That copy is then passed to the function. Sometime generating a copy can be expensive But, what if the argument is the address of a variable? 17

C Pointers What is a pointer? A variable that contains the memory address of another variable or of a function In general, it is safe to assume that on 32 bit architectures pointers occupy one word Pointers to int, char, float, void, etc. ( int*, char*, *float, void* ), they all occupy 4 bytes (one word). Pointers: *very* many bugs in C programs are traced back to mishandling of pointers 18

Pointers (cont.) The need for pointers Needed when you want to modify a variable (its value) inside a function The pointer is passed to that function as an argument Passing large objects to functions without the overhead of copying them first Accessing memory allocated on the heap Referring to functions 19

Pointer Validity A Valid pointer is one that points to memory that your program controls. Using invalid pointers will cause non-deterministic behavior Very often the code will crash with a SEGV, that is, Segment Violation, or Segmentation Fault. There are two general causes for these errors: Coding errors that end up setting the pointer to a strange number Use of a pointer that was at one time valid, but later became invalid Good practice: Initialize pointers to 0 (or NULL). NULL is never a valid pointer value, but it is known to be invalid and means no pointer set. char * get_pointer() char x=0; return &x; Will ptr be valid or invalid? char * ptr = get_pointer(); *ptr = 12; /* valid? */ 20

Answer: No, it s invalid A pointer to a variable allocated on the stack becomes invalid when that variable goes out of scope and the stack frame is popped. The pointer will point to an area of the memory that may later get reused and rewritten. char * get_pointer() char x=0; return &x; int main() char * ptr = get_pointer(); *ptr = 12; /* valid? */ other_function(); return 0; But now, ptr points to a location that s no longer in use, and will be reused the next time a function is called! Here is what I get in DevStudio when compiling: main.cpp(6) : warning C4172: returning address of local variable or temporary 21

Example: What gets printed out? int main() +3 +2 +1 +0 int d; char c; 900 short s; int* p; int arr[2]; printf( %p, %p, %p, %p, %p\n,&d, &c, &s, &p, arr ); return 0; s arr p d c 904 908 912 916 920 NOTE: Here &d = 920 (in practice a 4- byte hex number such as 0x22FC3A08) 924 928 932 Q: What does get printed out by the printf call in the code snippet above? 936 940 944 22

Example: Usage of Pointers & Pointer Arithmetic int main() int d; char c; short s; int* p; int arr[2]; p = &d; *p = 10; c = (char)1; p = arr; *(p+1) = 5; p[0] = d; *( (char*)p + 1 ) = c; return 0; +3 s +2 arr[0] arr[1] = 920 p d +1 = 10 +0 c = 1 900 904 908 912 916 920 924 928 932 936 940 944 Q: What are the values stored in arr? [assume little endian architecture] 23

Example [Cntd.] p = &d; *p = 10; c = (char)1; p = arr; *(p+1) = 5; // int* p; p[0] = d; +3 s +2 arr[0] arr[1] p d +1 = 904 = 10 +0 = 10 = 5 c = 1 900 904 908 912 916 920 924 928 *( (char*)p + 1 ) = c; 932 936 940 Question: arr[0] =? 944 24

Use of pointers, another example Pass pointer parameters into function void swap(int *px, int *py) int temp; temp = *px; *px = *py; *py = temp; int a = 5; int b = 6; swap(&a, &b); What will happen here? int * a; int * b; swap(a, b); 25

Dynamic Memory Allocation (Allocation on the Heap) Allows the program to determine how much memory it needs at run time and to allocate exactly the right amount of storage. It is your responsibility to clean up after you (free the dynamic memory you allocated) The region of memory where dynamic allocation and deallocation of memory can take place is called the heap. 26