MPI and C-Language Seminars 2010

advertisement
MPI and C-Language Seminars 2010
Seminar Plan (1/3)

Aim:
Introduce the „C‟ Programming Language.

Plan to cover:
 Basic C, and programming techniques needed for HPC coursework.
 C-bindings for the Message Passing Interface (MPI).
 Performance modelling.

We won‟t be covering C++ (but many of the skills are
transferable).
Seminar Plan (2/3)

Week 1 – Introduction, Data Types, Control Flow, Pointers
Week 2 – Arrays, Structures, Enums, I/O, Memory
Week 3 – Compiler Options and Debugging

Week 4 – MPI in C and Using the HPSG Cluster

Week 5 – “How to Build a Performance Model”

Week 6-9 – Coursework Troubleshooting


(Seminar tutors available in their office)
Seminar Plan (3/3)

The first three weeks focus on developing basic C skills; much
of this will be familiar.

Feel free to skip this if you already feel confident.

Unless you have done this course before, or are on a PhD
programme with HPSG, it is a good idea to attend from Week
4 onwards.

Seminars will usually be held on Wednesdays, 11:00 – 12:00.
Contact Info

For C-related queries:
 John Pennycook (sjp@dcs.warwick.ac.uk)

For performance modelling queries:
 Oli Perks (o.perks@warwick.ac.uk)

Availability:
 Always on e-mail.
 High Performance Systems Group (CS 2.04).

Resources:
 http://go.warwick.ac.uk/ep/pg/csrgai/teaching/cs402
Books

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/pg/csrgai/teaching/cs402
Overview of C

Much of the Java language builds on C/C++. Some of the
language keywords and behaviour is very similar.

Beware – Though keywords and „names‟ of types may be
similar, they are often different.

Mixing between languages can introduce lots of bugs unless
you‟re sure what you‟re doing.
Data Types in C

There are only four basic data types in C:
1. char – a single byte (8-bits) capable of holding an ASCII character.
2. int – an integer value, the size of which is based on the word size of
the machine (often 32-bits).
3. float – a single precision floating point number (often 32-bit, IEEE).
4. double – a double precision floating point number (often 64-bit, IEEE).

Note:
These sizes are the most commonly encountered, but may
be different on some platforms (non-Intel/AMD).
Integers in C (1/2)

Two additional qualifiers to control the size of integer values:
1. short int – a „shorter‟ version of int.
2. long int – a „longer‟ version of int.
Rules:





short int <= int <= long int
short int >= 16 bits.
int >= 16 bits.
long int >= 32 bits.
Integers in C (2/2)

Two (more) additional qualifiers for integer types:
1. unsigned – interpret the value as starting from 0.
2. signed – allow negative numbers.

Use unsigned to increase the range of values available,
provided you are sure negative values will not be required.

Mixing unsigned and signed values can cause bugs, so be
careful.
Booleans in C

There is no boolean „type‟ in C.

Booleans are implemented and evaluated as integer values:
 TRUE = 1
 FALSE = 0

Keywords may not be pre-defined in every implementation of
C.

Conversely:
 0 = FALSE
 Non-zero = TRUE
Arrays in C (1/2)

Arrays in C are just allocated contiguous blocks of memory.

For example:
int myarray[5];
allocates space for 5 integers in one contiguous block.

Arrays are not objects, so you cannot access properties (like
length). You must record the maximum length of the array.
Arrays in C (2/2)

The name of the array variable can be used as a pointer:
int* pointer = myarray;

myarray is a pointer to the first element in the array.

We will come back to this when we start looking at pointers in
more detail...
Strings in C (1/2)

Strings are implemented using an array of characters, but with
an additional element at the end of the string.

Strings must terminate with the null character, „\0‟.

For example:
“Hello” = { „H‟, „e‟, „l‟, „l‟, „o‟, ‘\0’ }
Strings in C (2/2)

Most important functions are implemented in the string library.

To use the string library, put:
#include <string.h>
at the top of your file.

All string functions in the C libraries will use the „\0‟ character
to detect the end of a string.
Control Flow in C (1/2)

if, while, do ... while are identical, except that
conditions must produce an integer „boolean‟ result.

Consider:
if (2 % 2) {
...
}
... Will not run, since 2 % 2 = 0.
Control Flow in C (2/2)

for loops are not quite identical.

Depending on the language mode, you may not be able to
declare variable inside a for-loop definition:
int i;
for (i = 0; i < 10; i++) {
printf(“%d \n”, i);
}
Operators in C

Mathematical operators in C are identical to those in Java.
 +, -, *, /
 <=, >=, ==, !=, <, >, &&, ||
 !
 %
(modulus)
 &, |

++i, i++, --i, i-- (pre/post-increment/decrement)
Pointers (1/4)

Pointers are references to locations in memory.

They are one of the biggest causes of bugs in software
construction.

Worse – they are one of the biggest reasons coursework does
not produce correct solutions on marking day.
Pointers (2/4)

To declare a pointer:
int myvalue = 42;
int* mypointer;
mypointer = &myvalue;

int* creates a pointer to a location in memory. This is treated
as if it were an integer value.

&myvalue is the location of myvalue in memory.
Pointers (3/4)

Remember:
 *ptr – look at the address pointed to by ptr.
 &v – get the address at which v is stored.

Be careful – every year pointers cause problems.
The compiler can detect some common bugs, but not all of
them... The only way to fix these is to inspect your code
manually.
Pointers (4/4)

Common mistakes:
1.
Uninitialised Pointers
The pointer is not initialised before it is used.
An un-initialised pointer has a value of 0 by default.
This causes a segmentation fault.
2.
Out-of-bounds Memory Access
A pointer is used to access an array, and pushed outside of the array‟s
bounds.
This either corrupts memory or gives a segmentation fault.
Hello World

Typical “Hello World” program:
#include <stdio.h>
main (int argc, char *argv[]) {
printf(“Hello world!\n”);
}

The #include acts like a Java „import‟ statement.
Compilation (1/2)

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 the executable:
./myprogram
Compilation (2/2)

Compilers:
 Linux/Mac OS X – gcc
 Windows – Microsoft Visual C++/Intel C Compiler

Code and seminar contents should work with these compilers
(but no promises).

You may not be able to compile the MPI coursework at home
unless you compile and install the MPI runtimes.

Your code will be tested running on department machines; it
may be best to do your compilation here.
Download