High Performance Computing - Seminar Plan Welcome to the High Performance Computing seminars for 2009. Aims: Introduce the C Programming Language. Basic coverage of C and programming techniques needed for the High Performance Computing coursework Introduce C-bindings for the Message Passing Interface. Overview of Performance Modelling for a complex application. These seminars will not cover C++ (but many of the skills are transferable) High Performance Computing MPI and C-Language Seminars 2009 Photo Credit: NOAA (IBM Hardware) 1 2 High Performance Computing - Seminar Plan Public Health Warning! Seminar Plan for Weeks 1-5 The MEng students this year will have been the first to use C skills in the second year Operating Systems module. Week 1 - Introduction, Data Types, Control Flow, Pointers Week 2 - Arrays, Structures, Enumerations, I/O Week 3 - File I/O, Preprocessing, Compile Time Options The first three weeks of this course are based on developing basic C skills so much of this will be familiar. Week 4 - MPI in C Feel free to skip this if you feel confident enough. Week 5 - How to Build a Performance Model After week 4 we will move to MPI, Performance Modelling etc. Unless you ve done this course before or are on a PhD programme with HPSG then its probably best to come along. Week 6-9 - Coursework Troubleshooting (a number of seminar tutors will be available during these sessions - go to each tutor s office) 3 4
High Performance Computing - Contact Info Seminar/Coursework Books Si Hammond (Warwick Postgraduate Research Fellow) High Performance Systems Group sdh@dcs.warwick.ac.uk Room CS2.04 (Department of Computer Science) Slides & Resources are available at: http://go.warwick.ac.uk/ep-csrcbc/ Office Hours: 9-11, Wednesday morning Email if you want to see me outside of these hours Probably won t need a book solely for the coursework/seminars. Worth consulting (in the library): The C Programming Language, (2nd Edition), Kernighan & Ritchie, 1988 C - How to Program, (5th Edition), Deitel, 2006 Online Reference Book linked at: http://go.warwick.ac.uk/ep-csrcbc 5 6 Feedback for Seminars Building a Collection of Resources Seminar Feedback forms at: http://go.warwick.ac.uk/ep-csrcbc under teaching. You can submit this form as many times as you like. Help us gauge whether to speed seminars up or slow them down, what things are are getting right and what things we need to improve. Please make use of this (makes my job easier!) Or feel free to email me with suggestions as we go along through the course. Over the CS402 course I will add resources at: http:// go.warwick.ac.uk/ep-csrcbc under teaching. Feel free to make suggestions for this resource. Check it regularly - we will add papers, tutorials, links etc to help you in the coursework. This will be particularly useful for Performance Modelling and OpenMPI later in the course. 7 8
Overview of the C Language Much of the Java language builds on C/C++ - hence some of the language keywords and behaviour is very similar. Types, Control Flow & Pointers Beware - although the keywords and names of types may be similar they are often different. Mixing between languages can introduce lots of bugs unless you re sure of what you are doing (so be careful)! 9 10 Data Types in the C Language Data Types in the C Language There are only four basic types in the C language (although most implementations include more). char - a single byte (8-bits) capable of holding an ASCII character int - an integer value, the size of which is often based on the word size of the machine (most often 32-bit) float - a single precision floating point number (often 32-bit, IEEE) double - a double precision floating point number (often 64-bit, IEEE) The above are just commonly encountered situations, on some platforms (particularly non-intel/amd) these may be different) Two additional qualifiers in the language to control the size of integer values. short int a - defines a to be a shorter version of int long int b - defines b to be a longer version of int Rules: short int <= int <= long int short int >= 16 bits int >= 16 bits long int >= 32 bits 11 12
Data Types in the C Language Booleans in C So which Java type is missing? So which Java type is missing? There is no boolean type in C Booleans are implemented and evaluated as integer values. TRUE = 1 FALSE = 0 These may or may not be pre-defined in the implementation of C you are using. 13 14 Booleans in C Additional Qualifiers for Integer Types What are the boolean values of the following expressions? 1 && 1 0 0!1 (2-1) 0 2 % 2 There are two additional qualifiers for integer types: unsigned - interpret the value as starting from 0 (no negatives) signed - allow negative numbers These can be used to increase the range of values within the type system provided the programmer is sure negative values will not be required. Mixing unsigned and signed values can cause programmer induced bugs (so again, be careful!) 15 16
Arrays in C Arrays in C Arrays in C are just allocated contiguous blocks of memory. For instance: int myarray[5]; Allocates space for 5 integers in one block in memory (must be contiguous) Arrays are not objects (C does not have objects) so you cannot access properties such as myarray.length as you might do in Java. The name of the array variable can be used as a pointer. For instance: int myarray[5]; The myarray variable is a pointer to the first element in the array. int* pointtofirst = myarray; You have to record the maximum length of the array, if you exceed the array bounds your program will crash or corrupt its own memory 17 18 Arrays in C Strings in C We will return to arrays when we cover pointers later in the seminar series. How are strings implemented in Java? Remember to treat array bounds seriously - your program will crash or produce incorrect/inconsistent output if you do not. 19 20
Strings in C Strings in C How are strings implemented in Java? How are strings implemented in C? java.lang.string class String contents are held in a char[] Strings are immutable - if you want to change it you create a new version and delete the old 21 22 Strings in C Strings in C How are strings implemented in C? Using a char[] (as in Java) but with an additional element at the end of the string. Strings must terminate with the null character - \0 For instance: Hello = { H, e, l, l, o, \0 } All string functions in the C libraries will use the \0 element as a method of detecting the end of the string. The most important functions are implemented in the string library. To use these you need to put: #include <string.h> At the very top of your file. The functions you need will be imported by the compiler. The seminar sheet for this lab has exercises in using the string functions. 23 24
Control Flow in C Control Flow in the C language uses very similar keywords and code structures to Java. Control Flow if, while, do...while are identical (except the conditions must produce an integer boolean result) for loops are not quite the same. Depending on which language mode you are in you cannot declare variables inside the for-loop definition. int i; for(i = 0; i < 10; i++) { printf( %i \n, i); } 25 26 Operators Mathematical Operators are identical to Java. +, -, *, / <=, >=, ==,!=, <, >, &&,! % &, (modulus operator) Pointers ++i, i++, --i, i-- (pre/post increment/decrement) 27 28
Pointers Pointers Pointers are references to locations in memory. Instead of using a variable name to state which memory location you want to use, you are using a numerical value to locate the value. Pointers are one of the biggest causes of bugs in software construction. Worse - they are one of the biggest reasons students coursework does not produce the correct solution on marking day. They require a lot of care! To declare a pointer: int myvalue = 42; int* mypointer; mypointer = &myvalue; The int* creates a pointer to a location in memory which is treated as if it were an integer value (the compiler will generate code to read enough bytes for an integer). The &myvalue operator gets the location of myvalue in memory 29 30 Quick Recap Pointer Mistakes Remember: *ptr - means look at the address pointed to by ptr &v - means get the location at which v is stored. Be careful - every year pointers cause lots of problems. The compiler often detects some common bugs but there are others which are not caught. The only way to solve these is to inspect your code manually. There are two common mistakes: The pointer is not initialised and then it is used (by default it will have a value of 0). This causes a segmentation fault. Pointers are used to access arrays and it is pushed outside of the array bounds. This either corrupts memory or gives a segmentation fault. 31 32
Basic Program Structure Basic Structure of a C Program: #include <stdio.h> Getting Started main(int argc, char *argv[]) { printf( Hello World ); } The includes act as import statements for libraries which are required. 33 34 Compilation Compilation Save your code in a file, e.g. myfile.c To compile a C file to object code: gcc -c myfile.c To link object code in an executable gcc -o myprogram myfile.o To run your executable:./myprogram Which compiler? Linux/Mac OSX - GCC Windows - Microsoft Visual C++/Intel C Compiler The code and seminar contents should work on these compilers but no promises! You can download the compilers above (mostly free). Any ANSI compliant C compiler will be fine for learning the language. You will not be able to compile the MPI coursework at home unless you compile and install the MPI runtimes. 35 36
End of Seminar Thanks for coming, see you next week. 37