Vous êtes sur la page 1sur 31

Overview of C

for Programming with MPI and OpenMP


Hinnerk Stu
ben

Jacobs University Bremen


1216 January 2015

History
C was initially developed by Dennis Ritchie between 1969 and 1973 at AT&T
Bell 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. St
uben Overview of C Jacobs University Bremen, January 2015

Programming paradigms
procedural / subroutine-oriented
Fortran (1957), C (1972)
object-oriented
C++ (1983), Java (1995)
functional
Haskell (1990)

H. St
uben Overview of C Jacobs University Bremen, January 2015

C standardisation
year
1978

standard / (main) additions


The C Programming Language book
by Kernighan and Ritchie

common name
K&R

standard I/O library

1989

ANSI C standard

C89

syntax of function parameter declarations,


function prototypes

1999

ISO C standard

C99

inline functions, complex numbers, automatic


arrays, variadic macros, restrict qualifier,
one-line comments (//)

2011

ISO C standard

C11

alignment specifications, multi-threading support,


atomic primitives and types, improved Unicode support,
bounds-checking interfaces

H. St
uben Overview of C Jacobs University Bremen, January 2015

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. St
uben Overview of C Jacobs University Bremen, January 2015

Other C language topics


expressions and operators
statements and flow control
I/O
standard library
recall 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. St
uben Overview of C Jacobs University Bremen, January 2015

Using functions example 1


double f(double x, double y);
void g(int n, double x[]);

// function prototype declarations . . .


// . . . 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)
{
return x + y;
}

// function definition

void g(int n, double x[]) { ... /* some work on x[] */ }

H. St
uben Overview of C Jacobs University Bremen, January 2015

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. St
uben Overview of C Jacobs University Bremen, January 2015

Data types
topics

primitive (built-in) data types


(unions)
structures
arrays
pointers

type conversion / type casting


typedef
complicated type declarations

H. St
uben Overview of C Jacobs University Bremen, January 2015

Primitive (built-in) data types


integer types
examples:
char
int
unsigned long int

floating point (real number) types


examples:
float
double
long double

H. St
uben Overview of C Jacobs University Bremen, January 2015

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
const
const
const

int i = 42;
long int j = 42;
float x = 42.0;
double y = 42.0;

preprocessor definitions
#define
#define
#define
#define

I
J
X
Y

42 defines an int
42L defines a long int
42.0F defines a float
42.0 defines a double

H. St
uben Overview of C Jacobs University Bremen, January 2015

10

Structures
a structure is a data type defined by the programmer
example:
struct point;
struct point { double x, y; };

// declaration (not necessary here)


// definition

int main(void)
{
struct point p;
// structure variable declaration . . .
struct point q = { 1.0, 1.5 }; // . . . and initialisation
p.x = 2.0;
p.y = 2.5;
...

// accessing structure members

H. St
uben Overview of C Jacobs University Bremen, January 2015

11

Arrays
an array is a data structure that stores elements of the same type
consecutively in memory
in standard C code the number of elements must be positive and known at
compile 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. St
uben 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. St
uben 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;
double y = 2.0;
double *py = &y;

// declaration of a double and a pointer to double


// declaration and initialisation of a double
// declaration and initialisation of a pointer to double

x = 1.0;
px = &x;

// assigning a value to x
// assigning a value to px using the address operator

x = y;
x = *py;
*px = y;

// these three statements have the same effect . . .


// . . . here py is dereferenced
// . . . here px is dereferenced

there are also function pointers


H. St
uben 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. St
uben 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. St
uben 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];
px = x;

// these two statements have the same effect

x[i] = 42;
// these three statements have the same effect
*(x + i) = 42;
*(px + i) = 42;
px = &x[i];
px = x + i;

// these two statements have the same effect

pointer arithmetic like x + i should be avoided !


(compilers are smart enough to handle the more readable code &x[i])
H. St
uben 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. St
uben 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 by
dereferencing pointers
in C one can emulate call-by-reference evaluation by explicitly passing
pointers (references)
arrays are passed by reference
the 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. St
uben 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 address


operator has to be employed
example:
double x;
double y[];
MPI_Send(&x, ...);
MPI_Send(y, ...);

H. St
uben 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;
x = i;
x = sin(i);

// explicit cast
// implicit type conversion
// 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 pointer
types
H. St
uben 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;
Point p, *q;
MPI_User_function my_fun;

// . . . and usage of new names

my_fun(void *in, void *inout, int *len, MPI_Datatype *datatype)


{
...
}

H. St
uben 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. St
uben 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_INT
const 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. St
uben Overview of C Jacobs University Bremen, January 2015

24

Illustration: MPI datatypes vs. C data types (II)


int main(int
{
int
i;
int
j =
int
k =
double x;
double y =
double z =

argc, char *argv[])

47;
11;
48;
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. St
uben 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. St
uben 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 according
to 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. St
uben Overview of C Jacobs University Bremen, January 2015

27

Storage classes and scope in C


Specifiers
auto
register
static
extern
(none)

Lifetime
Block (stack)
Block (stack or CPU register)
Program
Program
Dynamic (heap)

Scope
Block
Block
Block or compilation unit
Block or compilation unit

Default initializer
Uninitialized
Uninitialized
Zero
Zero
Uninitialized

source: http://en.wikipedia.org/wiki/C syntax

H. St
uben 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;
int j = 1;
static int count = 0;

// automatic, local
// automatic, local
// static, local

...
{
int i;
...

// automatic, local

}
}

H. St
uben 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. St
uben Overview of C Jacobs University Bremen, January 2015

30

Vous aimerez peut-être aussi