Upload
catalin-toma
View
214
Download
0
Tags:
Embed Size (px)
DESCRIPTION
etc
Citation preview
Overview of C
for Programming with MPI and OpenMP
Hinnerk Stuben
Jacobs University Bremen
12–16 January 2015
History
• C was initially developed by Dennis Ritchie between 1969 and 1973 at AT&TBell Labs.
• C was designed as the implementation language of the Unix operating system.
• C is a relatively small language that remained remarkably stable.
• C is one of the most widely used programming languages of all time.
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 1
Programming paradigms
• procedural / subroutine-oriented
– Fortran (1957), C (1972)
• object-oriented
– C++ (1983), Java (1995)
• functional
– Haskell (1990)
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 2
C standardisation
year standard / (main) additions common name1978 The C Programming Language book K&R
by Kernighan and Ritchiestandard I/O library
1989 ANSI C standard C89syntax of function parameter declarations,
function prototypes
1999 ISO C standard C99inline functions, complex numbers, automatic
arrays, variadic macros, restrict qualifier,
one-line comments (//)
2011 ISO C standard C11alignment specifications, multi-threading support,
atomic primitives and types, improved Unicode support,
bounds-checking interfaces
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 3
What is important for programming with MPI and OpenMP?
• MPI is subprogram library
→ functions→ parameters
→ data types
• OpenMP is (mostly) a language extension
→ storage classes→ scope
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 4
Other C language topics
• expressions and operators
• statements and flow control
• I/O
– standard libraryrecall that the %d format specifier in printf does not stand for double but
rather for int in decimal representation
– will be addressed in conjunction with parallel (MPI) I/O
• preprocessor
– inclusion of header files– conditional compilation
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 5
Using functions – example 1
double f(double x, double y); // function prototype declarations . . .
void g(int n, double x[]); // . . . typically collected in header files
int main(void) // in C the main program is also a function
{
double x, a = 1.0, b = 2.0;...x = f(a, b); // function call
... // data types of parameters must match
return 0;}
double f(double x, double y) // function definition
{
return x + y;}
void g(int n, double x[]) { ... /* some work on x[] */ }
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 6
Using functions – example 2 (MPI Send)
• step 1
look up the function declaration (e.g. by man mpi send):
#include <mpi.h>int MPI_Send(void *buf, int count, MPI_Datatype datatype,
int dest, int tag, MPI_Comm comm);
• step 2
understand the meaning of the parameters (this course)
• step 3
call the function with data objects that have– matching data types (correct syntax)
– appropriate contents (right semantics)
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 7
Data types
• topics
– primitive (built-in) data types– (unions)– structures– arrays– pointers
– type conversion / type casting– typedef
– complicated type declarations
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 8
Primitive (built-in) data types
• integer types
examples:char
int
unsigned long int
• floating point (real number) types
examples:float
double
long double
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 9
Every data object has a type! – examples
• literal constants
42 is an int
42L is a long int
42.0F is a float
42.0 is a double, 42e0 is a double
• named constants
const int i = 42;
const long int j = 42;
const float x = 42.0;
const double y = 42.0;
• preprocessor definitions
#define I 42 defines an int
#define J 42L defines a long int
#define X 42.0F defines a float
#define Y 42.0 defines a double
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 10
Structures
• a structure is a data type defined by the programmer
• example:
struct point; // declaration (not necessary here)
struct point { double x, y; }; // definition
int main(void){
struct point p; // structure variable declaration . . .
struct point q = { 1.0, 1.5 }; // . . . and initialisation
p.x = 2.0; // accessing structure members
p.y = 2.5;...
}
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 11
Arrays
• an array is a data structure that stores elements of the same typeconsecutively in memory
• in standard C code the number of elements must be positive and known atcompile time (!)
• example:
double v[10]; // declaration of a one-dimensional array
double m[5][3]; // declaration of a two-dimensional array
v[0] = 0.1; // accessing the first element of v[]
m[4][2] = 0.0; // accessing the last element of m[][]
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 12
Variable-length arrays
• in C99 and GNU C variable-length arrays (automatic arrays) are implemented
• example:
double fun(int n){
double x[n];
...}
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 13
Pointers
• a pointer is a variable that holds the memory address of another variable
• there are two unary operators for working with pointers
– address / reference operator &– indirection / dereference operator *
• example:
double x, *px; // declaration of a double and a pointer to doubledouble y = 2.0; // declaration and initialisation of a doubledouble *py = &y; // declaration and initialisation of a pointer to double
x = 1.0; // assigning a value to xpx = &x; // assigning a value to px using the address operator
x = y; // these three statements have the same effect . . .
x = *py; // . . . here py is dereferenced
*px = y; // . . . here px is dereferenced
• there are also function pointers
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 14
Generic pointers
• a pointer declared as void* can hold any address
• example:
– the first parameter of the MPI Send function is a generic pointer
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 15
Pointers to structures
• there is a special syntax for accessing members via a pointer to a structure
• example:
struct point { double x, y; };struct point p;struct point *ptr = &p;
ptr -> x = 2.0;ptr -> y = 2.5;
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 16
Pointers and arrays (I)
• in C pointers and arrays are intimately connected
• example:
double x[10];double *px;int i = 3;
px = &x[0]; // these two statements have the same effect
px = x;
x[i] = 42; // these three statements have the same effect
*(x + i) = 42;
*(px + i) = 42;
px = &x[i]; // these two statements have the same effect
px = x + i;
• pointer arithmetic like x + i should be avoided !(compilers are smart enough to handle the more readable code &x[i])
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 17
Pointers and arrays (II)
• important to remember: the name of an array is a pointer
• there are two ways of declaring arrays as function parameters
void f(double *x);void f(double x[]);
again, the second form is better readable
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 18
Evaluation of function parameters (I)
• C uses call-by-value evaluation
→ a function always gets copies of the actual parameter values
→ passing back values through parameters can only be achieved bydereferencing pointers
• in C one can emulate call-by-reference evaluation by explicitly passingpointers (references)
– arrays are passed by referencethe array name is a pointer (character strings are arrays syntactically)
– structures are copied when passed to a function (!)to avoid performance degradation typically pointers to structures are used
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 19
Evaluation of function parameters (II)
• remarks
– the const qualifier can be used to write-protect locations– example:
double dist(const struct point *p, const struct point *q);
– if a scalar must be passed to function expecting a pointer, the addressoperator has to be employed
– example:double x;double y[];
MPI_Send(&x, ...);MPI_Send(y, ...);
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 20
Type conversion / type casting
• type casts can be used to cause an expression to be of a specific data type
• examples:
int i = 3;double x;double *a;double sin(double);
x = (double) i; // explicit cast
x = i; // implicit type conversion
x = sin(i); // implicit type conversion according to function prototype
a = (double*) malloc(100 * sizeof(double)); // malloc() returns void*
• type casting and conversion only works for integer, floating-point and pointertypes
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 21
typedef
• typedef can be used to define new names
• examples:
typedef double Temperature; // definition . . .
typedef struct point Point;typedef void MPI_User_function(void *invec, void *inoutvec,
int *len, MPI_Datatype *datatype);
Temperature T; // . . . and usage of new names
Point p, *q;MPI_User_function my_fun;
my_fun(void *in, void *inout, int *len, MPI_Datatype *datatype){
...}
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 22
Complicated declarations
• examples
double *f(...) // a function that returns double*
double (*g)(...) // a pointer to a function that returns double
double *(*h)(...) // a pointer to a function that returns double*
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 23
Illustration: MPI datatypes vs. C data types (I)
#include <stdio.h>
typedef long long MPI_Datatype; // define a new C type called MPI_Datatype// (strictly speaking this is an alias not a type)
const MPI_Datatype MPI_INT = 1; // define a C constant called MPI_INTconst MPI_Datatype MPI_DOUBLE = 2; // define a C constant called MPI_DOUBLE
void sum(void *p1, void *p2, void *p3, MPI_Datatype type);// declare a C function using the new type
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 24
Illustration: MPI datatypes vs. C data types (II)
int main(int argc, char *argv[]){
int i;int j = 47;int k = 11;double x;double y = 48;double z = 12;
MPI_Datatype my_int_type = MPI_INT; // define a C variable of type MPI_Datatype
sum(&i, &j, &k, my_int_type);sum(&x, &y, &z, MPI_DOUBLE);
printf("%d %f\n", i, x);
return 0;}
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 25
Illustration: MPI datatypes vs. C data types (III)
void sum(void *p1, void *p2, void *p3, MPI_Datatype type){
int *i1, *i2, *i3;double *x1, *x2, *x3;
if (type == MPI_INT) {
i1 = (int *) p1;i2 = (int *) p2;i3 = (int *) p3;
*i1 = *i2 + *i3;} else if (type == MPI_DOUBLE) {
x1 = (double *) p1;x2 = (double *) p2;x3 = (double *) p3;
*x1 = *x2 + *x3;}
}
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 26
Storage classes and scope
• storage class refers to the lifespan of a datum
– automatic variables are allocated and deallocated automatically accordingto the program flow
– static variables exist over the whole runtime of the program
• scope refers to the visibility of a datum
– local variables are only visible in a block or function– global variables are visible in the whole program
• remarks
– automatic variables are usually local
– global variables are usually static
– in C there is also file scope
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 27
Storage classes and scope in C
Specifiers Lifetime Scope Default initializerauto Block (stack) Block Uninitializedregister Block (stack or CPU register) Block Uninitializedstatic Program Block or compilation unit Zeroextern Program Block or compilation unit Zero(none) Dynamic (heap) Uninitialized
source: http://en.wikipedia.org/wiki/C syntax
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 28
Storage classes and scope in C – example
extern global_info; // global (no instance exists yet)
struct Global global_info; // static, global
static int status; // static, file scope
void sub(void){
double x; // automatic, local
int j = 1; // automatic, local
static int count = 0; // static, local
...
{
int i; // automatic, local
...}
}
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 29
References
• Rothwell, The GNU C Reference Manual
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.pdf
• http://en.wikipedia.org
• Kernighan and Ritchie, The C Programming Language
• Klemens, 21st Century C
H. Stuben – Overview of C – Jacobs University Bremen, January 2015 30