ME964High Performance Computing for Engineering Applications“There is no reason for any individual to have a computer in their home.” Ken Olson, president and founder, Digital Equipment Corporation, 1977.© Dan Negrut, 2011ME964 UW-MadisonQuick Overview of C ProgrammingJanuary 20, 2011Before 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: [email protected] 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.htmAuditing Courses: http://www.grad.wisc.edu/education/acadpolicy/guidelines.html#133C Syntax and Hello World#include <stdio.h>#include <stdio.h>#include <stdio.h>#include <stdio.h>/* The simplest C Program *//* The simplest C Program *//* The simplest C Program *//* The simplest C Program */int main(int argc, char **argv)int main(int argc, char **argv)int main(int argc, char **argv)int main(int argc, char **argv){{{{printf(“Hello Worldprintf(“Hello Worldprintf(“Hello Worldprintf(“Hello World\\\\n”);n”);n”);n”);return 0;return 0;return 0;return 0;}}}}The main() function is always where your program starts running. #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 compilerBlocks of code (“lexical scopes”) are marked by { … }Return ‘0’ from this function What do the < > mean?4Lexical ScopingEvery Variable is Defined within some scope. A Variable cannot be referenced by name (a.k.a. Symbol) from outside of that scope.The scope of Function Arguments is the complete body of that function.void p(char x)void p(char x)void p(char x)void p(char x){{{{/* /* /* /* p,xp,xp,xp,x */*/*/*/char y;char y;char y;char y;/* /* /* /* p,x,yp,x,yp,x,yp,x,y */*/*/*/char z;char z;char z;char z;/* /* /* /* p,x,y,zp,x,y,zp,x,y,zp,x,y,z */*/*/*/}}}}/* /* /* /* pppp */*/*/*/char z;char z;char z;char z;/* /* /* /* p,zp,zp,zp,z */*/*/*/void q(char a)void q(char a)void q(char a)void q(char a){{{{char b;char b;char b;char b;/* /* /* /* p,z,q,a,bp,z,q,a,bp,z,q,a,bp,z,q,a,b */*/*/*/{{{{char c;char c;char c;char c;/* /* /* /* p,z,q,a,b,cp,z,q,a,b,cp,z,q,a,b,cp,z,q,a,b,c */*/*/*/}}}}char d;char d;char d;char d;/* /* /* /* p,z,q,a,b,dp,z,q,a,b,dp,z,q,a,b,dp,z,q,a,b,d (not c)(not c)(not c)(not c) */*/*/*/}}}}/* /* /* /* p,z,qp,z,qp,z,qp,z,q */*/*/*/The scope of Variables defined inside a function starts at the definition and ends at the closing brace of the containing blockLexical scopes are defined with curly braces { }.The scope of Variables defined outside a function starts at the definition and ends at the end of the file. Called “Global” Vars.legal?char b?5Comparison and Mathematical Operators== equal to== equal to== equal to== equal to< less than< less than< less than< less than<= less than or equal<= less than or equal<= less than or equal<= less than or equal> greater than> greater than> greater than> greater than>= greater than or equal>= greater than or equal>= greater than or equal>= greater than or equal!= not equal!= not equal!= not equal!= not equal&& logical and&& logical and&& logical and&& logical and|| logical or|| logical or|| logical or|| logical or! logical not! logical not! logical not! logical not+ plus+ plus+ plus+ plus- minusminusminusminus* mult* mult* mult* mult/ divide / divide / divide / divide % modulo% modulo% modulo% moduloThe rules of precedence are clearly defined but often difficult to remember or non-intuitive. When in doubt, add parentheses to make it explicit. Beware division:• 5 / 10 0 whereas 5 / 10.0 0.5• Division by 0 will cause a FPE& bitwise and& bitwise and& bitwise and& bitwise and| bitwise or| bitwise or| bitwise or| bitwise or^ bitwise xor^ bitwise xor^ bitwise xor^ bitwise xor~ bitwise not~ bitwise not~ bitwise not~ bitwise not<< shift left<< shift left<< shift left<< shift left>> shift right>> shift right>> shift right>> shift rightDon’t confuse & and &&.. 1 & 2 0 whereas 1 && 2 <true>6Assignment Operatorsx = y assign y to xx = y assign y to xx = y assign y to xx = y assign y to xx++ postx++ postx++ postx++ post----increment xincrement xincrement xincrement x++x pre++x pre++x pre++x pre----increment xincrement xincrement xincrement xxxxx-------- postpostpostpost----decrement xdecrement xdecrement xdecrement x--------x prex prex prex pre----decrement xdecrement xdecrement xdecrement xNote the difference between ++x and x++ (high vs low priority (precedence)):Don’t confuse “=“ and “==“! int x=5;int x=5;int x=5;int x=5;int y;int y;int y;int y;y = ++x;y = ++x;y = ++x;y = ++x;/* x == 6, y == 6 *//* x == 6, y == 6 *//* x == 6, y == 6 *//* x == 6, y == 6 */intintintint x=5;x=5;x=5;x=5;intintintint y;y;y;y;y = x++;y = x++;y = x++;y = x++;/* x == 6, y == 5 *//* x == 6, y == 5 *//* x == 6, y == 5 *//* x == 6, y == 5 */int x=5;int x=5;int x=5;int x=5;if (x=6) /* if (x=6) /* if (x=6) /* if (x=6) /* always truealways truealways truealways true */ */ */ */ {{{{/* /* /* /* x is now 6x is now 6x is now 6x is now 6
View Full Document